Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | D [0-9] |
| 18 | L [a-zA-Z_] |
| 19 | H [a-fA-F0-9] |
| 20 | E [Ee][+-]?{D}+ |
| 21 | FS (f|F|l|L) |
| 22 | IS (u|U|l|L)* |
| 23 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 24 | COMPONENT {L}({L}|{D})* |
| 25 | DOT [.] |
| 26 | PATH {COMPONENT}({DOT}{COMPONENT})* |
| 27 | AT [@] |
| 28 | VERSION {AT}{D}+{DOT}{D}+ |
| 29 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | %{ |
| 31 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 32 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | #include "AST.h" |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 34 | #include "ArrayType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 35 | #include "CompoundType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 36 | #include "ConstantExpression.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 37 | #include "DeathRecipientType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 38 | #include "EnumType.h" |
| 39 | #include "HandleType.h" |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 40 | #include "MemoryType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | #include "Method.h" |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 42 | #include "PointerType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | #include "ScalarType.h" |
| 44 | #include "StringType.h" |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 45 | #include "VectorType.h" |
| 46 | #include "RefType.h" |
Hridya Valsaraju | a32bde8 | 2016-12-27 11:47:46 -0800 | [diff] [blame] | 47 | #include "FmqType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 48 | |
| 49 | #include "hidl-gen_y.h" |
| 50 | |
| 51 | #include <assert.h> |
| 52 | |
| 53 | using namespace android; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 54 | using token = yy::parser::token; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 55 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 56 | int check_type(yyscan_t yyscanner, struct yyguts_t *yyg); |
| 57 | |
| 58 | #define SCALAR_TYPE(kind) \ |
| 59 | do { \ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 60 | yylval->type = new ScalarType(ScalarType::kind); \ |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 61 | return token::TYPE; \ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 62 | } while (0) |
| 63 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 64 | #define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng); |
| 65 | |
| 66 | #pragma clang diagnostic push |
| 67 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Steven Moreland | 191c1fe | 2016-09-13 13:05:46 -0700 | [diff] [blame] | 68 | #pragma clang diagnostic ignored "-Wdeprecated-register" |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 69 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 70 | %} |
| 71 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 72 | %option yylineno |
| 73 | %option noyywrap |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 74 | %option reentrant |
| 75 | %option bison-bridge |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 76 | %option bison-locations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 77 | |
Steven Moreland | 605c421 | 2016-09-06 10:19:53 -0700 | [diff] [blame] | 78 | %x COMMENT_STATE |
| 79 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 80 | %% |
| 81 | |
Steven Moreland | 605c421 | 2016-09-06 10:19:53 -0700 | [diff] [blame] | 82 | "/*" { BEGIN(COMMENT_STATE); } |
| 83 | <COMMENT_STATE>"*/" { BEGIN(INITIAL); } |
| 84 | <COMMENT_STATE>[\n] { yylloc->lines(); } |
| 85 | <COMMENT_STATE>. { } |
| 86 | |
| 87 | "//"[^\r\n]* { /* skip C++ style comment */ } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 88 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 89 | "enum" { return token::ENUM; } |
| 90 | "extends" { return token::EXTENDS; } |
| 91 | "generates" { return token::GENERATES; } |
| 92 | "import" { return token::IMPORT; } |
| 93 | "interface" { return token::INTERFACE; } |
| 94 | "package" { return token::PACKAGE; } |
| 95 | "struct" { return token::STRUCT; } |
| 96 | "typedef" { return token::TYPEDEF; } |
| 97 | "union" { return token::UNION; } |
Yifan Hong | c57c8bb | 2016-12-01 11:37:18 -0800 | [diff] [blame] | 98 | "bitfield" { yylval->templatedType = new BitFieldType; return token::TEMPLATED; } |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 99 | "vec" { yylval->templatedType = new VectorType; return token::TEMPLATED; } |
| 100 | "ref" { yylval->templatedType = new RefType; return token::TEMPLATED; } |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 101 | "oneway" { return token::ONEWAY; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 102 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 103 | "bool" { SCALAR_TYPE(KIND_BOOL); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 104 | "int8_t" { SCALAR_TYPE(KIND_INT8); } |
| 105 | "uint8_t" { SCALAR_TYPE(KIND_UINT8); } |
| 106 | "int16_t" { SCALAR_TYPE(KIND_INT16); } |
| 107 | "uint16_t" { SCALAR_TYPE(KIND_UINT16); } |
| 108 | "int32_t" { SCALAR_TYPE(KIND_INT32); } |
| 109 | "uint32_t" { SCALAR_TYPE(KIND_UINT32); } |
| 110 | "int64_t" { SCALAR_TYPE(KIND_INT64); } |
| 111 | "uint64_t" { SCALAR_TYPE(KIND_UINT64); } |
| 112 | "float" { SCALAR_TYPE(KIND_FLOAT); } |
| 113 | "double" { SCALAR_TYPE(KIND_DOUBLE); } |
| 114 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 115 | "death_recipient" { yylval->type = new DeathRecipientType; return token::TYPE; } |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 116 | "handle" { yylval->type = new HandleType; return token::TYPE; } |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 117 | "memory" { yylval->type = new MemoryType; return token::TYPE; } |
| 118 | "pointer" { yylval->type = new PointerType; return token::TYPE; } |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 119 | "string" { yylval->type = new StringType; return token::TYPE; } |
| 120 | |
Hridya Valsaraju | a32bde8 | 2016-12-27 11:47:46 -0800 | [diff] [blame] | 121 | "fmq_sync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorSync"); return token::TEMPLATED; } |
| 122 | "fmq_unsync" { yylval->type = new FmqType("::android::hardware", "MQDescriptorUnsync"); return token::TEMPLATED; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 123 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 124 | "(" { return('('); } |
| 125 | ")" { return(')'); } |
| 126 | "<" { return('<'); } |
| 127 | ">" { return('>'); } |
| 128 | "{" { return('{'); } |
| 129 | "}" { return('}'); } |
| 130 | "[" { return('['); } |
| 131 | "]" { return(']'); } |
| 132 | ":" { return(':'); } |
| 133 | ";" { return(';'); } |
| 134 | "," { return(','); } |
| 135 | "." { return('.'); } |
| 136 | "=" { return('='); } |
| 137 | "+" { return('+'); } |
| 138 | "-" { return('-'); } |
| 139 | "*" { return('*'); } |
| 140 | "/" { return('/'); } |
| 141 | "%" { return('%'); } |
| 142 | "&" { return('&'); } |
| 143 | "|" { return('|'); } |
| 144 | "^" { return('^'); } |
| 145 | "<<" { return(token::LSHIFT); } |
| 146 | ">>" { return(token::RSHIFT); } |
| 147 | "&&" { return(token::LOGICAL_AND); } |
| 148 | "||" { return(token::LOGICAL_OR); } |
| 149 | "!" { return('!'); } |
| 150 | "~" { return('~'); } |
| 151 | "<=" { return(token::LEQ); } |
| 152 | ">=" { return(token::GEQ); } |
| 153 | "==" { return(token::EQUALITY); } |
| 154 | "!=" { return(token::NEQ); } |
| 155 | "?" { return('?'); } |
| 156 | "@" { return('@'); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 157 | |
Yifan Hong | 333a6d2 | 2017-01-12 12:28:02 -0800 | [diff] [blame^] | 158 | {PATH}{VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; } |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 159 | {VERSION}"::"{PATH} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 160 | {PATH}{VERSION} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 161 | {COMPONENT}({DOT}{COMPONENT})+ { yylval->str = strdup(yytext); return token::FQNAME; } |
| 162 | {COMPONENT} { yylval->str = strdup(yytext); return token::IDENTIFIER; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 163 | |
Yifan Hong | 333a6d2 | 2017-01-12 12:28:02 -0800 | [diff] [blame^] | 164 | {PATH}{VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 165 | {VERSION}"::"{PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 166 | {PATH}":"{COMPONENT} { yylval->str = strdup(yytext); return token::FQNAME; } |
| 167 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 168 | 0[xX]{H}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 169 | 0{D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 170 | {D}+{IS}? { yylval->str = strdup(yytext); return token::INTEGER; } |
| 171 | L?\"(\\.|[^\\"])*\" { yylval->str = strdup(yytext); return token::STRING_LITERAL; } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 172 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 173 | {D}+{E}{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
| 174 | {D}+\.{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
| 175 | {D}*\.{D}+{E}?{FS}? { yylval->str = strdup(yytext); return token::FLOAT; } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 176 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 177 | [\n] { yylloc->lines(); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 178 | . { /* ignore bad characters */ } |
| 179 | |
| 180 | %% |
| 181 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 182 | #pragma clang diagnostic pop |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 183 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 184 | status_t parseFile(AST *ast) { |
| 185 | FILE *file = fopen(ast->getFilename().c_str(), "rb"); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 186 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 187 | if (file == NULL) { |
| 188 | return -errno; |
| 189 | } |
| 190 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 191 | yyscan_t scanner; |
| 192 | yylex_init_extra(ast, &scanner); |
| 193 | ast->setScanner(scanner); |
| 194 | |
| 195 | yyset_in(file, scanner); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 196 | int res = yy::parser(ast).parse(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 197 | |
| 198 | yylex_destroy(scanner); |
| 199 | ast->setScanner(NULL); |
| 200 | |
| 201 | fclose(file); |
| 202 | file = NULL; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 203 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 204 | if (res != 0 || ast->syntaxErrors() != 0) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 205 | return UNKNOWN_ERROR; |
| 206 | } |
| 207 | |
| 208 | return OK; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 209 | } |