Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | %{ |
| 2 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 3 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 4 | #include "AST.h" |
| 5 | #include "ArrayType.h" |
| 6 | #include "CompoundType.h" |
| 7 | #include "Constant.h" |
| 8 | #include "EnumType.h" |
| 9 | #include "Interface.h" |
| 10 | #include "Method.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 11 | #include "TypeDef.h" |
| 12 | #include "VectorType.h" |
| 13 | |
| 14 | #include "hidl-gen_y.h" |
| 15 | |
| 16 | #include <stdio.h> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 17 | #include <utils/String8.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 18 | |
| 19 | using namespace android; |
| 20 | |
| 21 | extern int yylex(YYSTYPE *yylval_param, void *yyscanner); |
| 22 | extern int column; |
| 23 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 24 | int yyerror(AST *, const char *s) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | fflush(stdout); |
| 26 | printf("\n%*s\n%*s\n", column, "^", column, s); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | #define scanner ast->scanner() |
| 32 | |
| 33 | %} |
| 34 | |
| 35 | %parse-param { android::AST *ast } |
| 36 | %lex-param { void *scanner } |
| 37 | %pure-parser |
| 38 | |
| 39 | %token<str> CONST |
| 40 | %token<str> ENUM |
| 41 | %token<str> EXTENDS |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 42 | %token<str> FQNAME |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | %token<str> GENERATES |
| 44 | %token<str> IDENTIFIER |
| 45 | %token<str> IMPORT |
| 46 | %token<str> INTEGER |
| 47 | %token<str> INTERFACE |
| 48 | %token<str> PACKAGE |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 49 | %token<type> SCALAR |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 50 | %token<str> STRUCT |
| 51 | %token<str> STRING_LITERAL |
| 52 | %token<str> TYPEDEF |
| 53 | %token<str> UNION |
| 54 | %token<str> VEC |
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 | %type<str> optIdentifier package |
| 57 | %type<str> const_value |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 58 | %type<type> fqname |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 59 | |
| 60 | %type<type> type opt_storage_type |
| 61 | %type<type> enum_declaration |
| 62 | %type<type> struct_or_union_declaration |
| 63 | %type<type> opt_extends |
| 64 | |
| 65 | %type<field> field_declaration |
| 66 | %type<fields> field_declarations struct_or_union_body |
| 67 | %type<enumValue> enum_value |
| 68 | %type<enumValues> enum_values |
| 69 | %type<typedVars> typed_vars |
| 70 | %type<typedVar> typed_var |
| 71 | %type<method> method_declaration |
| 72 | %type<compoundStyle> struct_or_union_keyword |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 73 | %type<stringVec> annotation_string_values annotation_value |
| 74 | %type<annotationParam> annotation_param |
| 75 | %type<annotationParams> opt_annotation_params annotation_params |
| 76 | %type<annotation> annotation |
| 77 | %type<annotations> opt_annotations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 78 | |
| 79 | %start program |
| 80 | |
| 81 | %union { |
| 82 | const char *str; |
| 83 | android::Type *type; |
| 84 | android::CompoundType *compoundType; |
| 85 | android::CompoundField *field; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 86 | std::vector<android::CompoundField *> *fields; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 87 | android::EnumValue *enumValue; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 88 | std::vector<android::EnumValue *> *enumValues; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 89 | android::TypedVar *typedVar; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 90 | std::vector<android::TypedVar *> *typedVars; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 91 | android::Method *method; |
| 92 | android::CompoundType::Style compoundStyle; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 93 | std::vector<std::string> *stringVec; |
| 94 | std::pair<std::string, std::vector<std::string> *> *annotationParam; |
| 95 | android::KeyedVector<std::string, std::vector<std::string> *> *annotationParams; |
| 96 | android::Annotation *annotation; |
| 97 | android::KeyedVector<std::string, android::Annotation *> *annotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | %% |
| 101 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 102 | opt_annotations |
| 103 | : /* empty */ |
| 104 | { |
| 105 | $$ = new KeyedVector<std::string, Annotation *>; |
| 106 | } |
| 107 | | opt_annotations annotation |
| 108 | { |
| 109 | $$ = $1; |
| 110 | $$->add($2->name(), $2); |
| 111 | } |
| 112 | ; |
| 113 | |
| 114 | annotation |
| 115 | : '@' IDENTIFIER opt_annotation_params |
| 116 | { |
| 117 | $$ = new Annotation($2, $3); |
| 118 | } |
| 119 | ; |
| 120 | |
| 121 | opt_annotation_params |
| 122 | : /* empty */ |
| 123 | { |
| 124 | $$ = new KeyedVector<std::string, std::vector<std::string> *>; |
| 125 | } |
| 126 | | '(' annotation_params ')' |
| 127 | { |
| 128 | $$ = $2; |
| 129 | } |
| 130 | ; |
| 131 | |
| 132 | annotation_params |
| 133 | : annotation_param |
| 134 | { |
| 135 | $$ = new KeyedVector<std::string, std::vector<std::string> *>; |
| 136 | $$->add($1->first, $1->second); |
| 137 | } |
| 138 | | annotation_params ',' annotation_param |
| 139 | { |
| 140 | $$ = $1; |
| 141 | $$->add($3->first, $3->second); |
| 142 | } |
| 143 | ; |
| 144 | |
| 145 | annotation_param |
| 146 | : IDENTIFIER '=' annotation_value |
| 147 | { |
| 148 | $$ = new std::pair<std::string, std::vector<std::string> *>($1, $3); |
| 149 | } |
| 150 | ; |
| 151 | |
| 152 | annotation_value |
| 153 | : STRING_LITERAL |
| 154 | { |
| 155 | $$ = new std::vector<std::string>; |
| 156 | $$->push_back($1); |
| 157 | } |
| 158 | | '{' annotation_string_values '}' { $$ = $2; } |
| 159 | ; |
| 160 | |
| 161 | annotation_string_values |
| 162 | : STRING_LITERAL |
| 163 | { |
| 164 | $$ = new std::vector<std::string>; |
| 165 | $$->push_back($1); |
| 166 | } |
| 167 | | annotation_string_values ',' STRING_LITERAL |
| 168 | { |
| 169 | $$ = $1; |
| 170 | $$->push_back($3); |
| 171 | } |
| 172 | ; |
| 173 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 174 | program |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 175 | : package imports body |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 176 | ; |
| 177 | |
| 178 | fqname |
| 179 | : FQNAME |
| 180 | { |
| 181 | $$ = ast->lookupType($1); |
| 182 | if ($$ == NULL) { |
| 183 | yyerror(ast, |
| 184 | android::String8::format( |
| 185 | "Failed to lookup type '%s'.", $1).string()); |
| 186 | YYERROR; |
| 187 | } |
| 188 | } |
| 189 | | IDENTIFIER |
| 190 | { |
| 191 | $$ = ast->lookupType($1); |
| 192 | if ($$ == NULL) { |
| 193 | yyerror(ast, |
| 194 | android::String8::format( |
| 195 | "Failed to lookup type '%s'.", $1).string()); |
| 196 | YYERROR; |
| 197 | } |
| 198 | } |
| 199 | | SCALAR |
| 200 | ; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 201 | |
| 202 | package |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 203 | : PACKAGE FQNAME ';' |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 204 | { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 205 | if (!ast->setPackage($2)) { |
| 206 | yyerror(ast, "Malformed package identifier."); |
| 207 | YYERROR; |
| 208 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 209 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 210 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 211 | imports |
| 212 | : /* empty */ |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 213 | | imports IMPORT FQNAME ';' |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 214 | { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 215 | if (!ast->addImport($3)) { |
| 216 | yyerror(ast, |
| 217 | android::String8::format( |
| 218 | "Unable to import '%s'.", $3)); |
| 219 | |
| 220 | YYERROR; |
| 221 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 222 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 223 | | imports IMPORT IDENTIFIER ';' |
| 224 | { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 225 | if (!ast->addImport($3)) { |
| 226 | yyerror(ast, |
| 227 | android::String8::format( |
| 228 | "Unable to import '%s'.", $3)); |
| 229 | |
| 230 | YYERROR; |
| 231 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 232 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 233 | ; |
| 234 | |
| 235 | opt_extends |
| 236 | : /* empty */ { $$ = NULL; } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 237 | | EXTENDS fqname { $$ = $2; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 238 | |
| 239 | body |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame^] | 240 | : opt_annotations INTERFACE IDENTIFIER opt_extends |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 241 | { |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame^] | 242 | if ($4 != NULL && !$4->isInterface()) { |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 243 | fprintf(stderr, "You can only extend interfaces.\n"); |
| 244 | YYERROR; |
| 245 | } |
| 246 | |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame^] | 247 | Interface *iface = new Interface(static_cast<Interface *>($4), $1); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 248 | |
| 249 | // Register interface immediately so it can be referenced inside |
| 250 | // definition. |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame^] | 251 | if (!ast->addScopedType($3, iface)) { |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 252 | YYERROR; |
| 253 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 254 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 255 | ast->enterScope(iface); |
| 256 | } |
| 257 | '{' interface_declarations '}' ';' |
| 258 | { |
| 259 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 260 | |
| 261 | ast->leaveScope(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 262 | } |
| 263 | | type_declarations |
| 264 | ; |
| 265 | |
| 266 | interface_declarations |
| 267 | : /* empty */ |
| 268 | | interface_declarations type_declaration |
| 269 | | interface_declarations method_declaration |
| 270 | { |
| 271 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 272 | iface->addMethod($2); |
| 273 | } |
| 274 | ; |
| 275 | |
| 276 | type_declarations |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 277 | : /* empty */ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 278 | | type_declarations type_declaration |
| 279 | ; |
| 280 | |
| 281 | type_declaration |
| 282 | : named_struct_or_union_declaration ';' |
| 283 | | named_enum_declaration ';' |
| 284 | | typedef_declaration ';' |
| 285 | | const_declaration ';' |
| 286 | ; |
| 287 | |
| 288 | typedef_declaration |
| 289 | : TYPEDEF type IDENTIFIER |
| 290 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 291 | TypeDef *def = new TypeDef($2); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 292 | if (!ast->addScopedType($3, def)) { |
| 293 | YYERROR; |
| 294 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 295 | } |
| 296 | ; |
| 297 | |
| 298 | const_value |
| 299 | : INTEGER |
| 300 | | STRING_LITERAL ; |
| 301 | |
| 302 | const_declaration |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 303 | : CONST fqname IDENTIFIER '=' const_value |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 304 | { |
| 305 | Constant *constant = new Constant($3, $2, $5); |
| 306 | ast->scope()->addConstant(constant); |
| 307 | } |
| 308 | ; |
| 309 | |
| 310 | method_declaration |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 311 | : opt_annotations IDENTIFIER '(' typed_vars ')' ';' |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 312 | { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 313 | $$ = new Method($2, $4, new std::vector<TypedVar *>, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 314 | } |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 315 | | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' ';' |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 316 | { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 317 | $$ = new Method($2, $4, $8, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 318 | } |
| 319 | ; |
| 320 | |
| 321 | typed_vars |
| 322 | : /* empty */ |
| 323 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 324 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 325 | } |
| 326 | | typed_var |
| 327 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 328 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 329 | $$->push_back($1); |
| 330 | } |
| 331 | | typed_vars ',' typed_var |
| 332 | { |
| 333 | $$ = $1; |
| 334 | $$->push_back($3); |
| 335 | } |
| 336 | ; |
| 337 | |
| 338 | typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); } |
| 339 | ; |
| 340 | |
| 341 | |
| 342 | struct_or_union_keyword |
| 343 | : STRUCT { $$ = CompoundType::STYLE_STRUCT; } |
| 344 | | UNION { $$ = CompoundType::STYLE_UNION; } |
| 345 | ; |
| 346 | |
| 347 | named_struct_or_union_declaration |
| 348 | : struct_or_union_keyword IDENTIFIER |
| 349 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 350 | CompoundType *container = new CompoundType($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 351 | ast->enterScope(container); |
| 352 | } |
| 353 | struct_or_union_body |
| 354 | { |
| 355 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 356 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 357 | if (!container->setFields($4)) { |
| 358 | YYERROR; |
| 359 | } |
| 360 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 361 | ast->leaveScope(); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 362 | if (!ast->addScopedType($2, container)) { |
| 363 | YYERROR; |
| 364 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 365 | } |
| 366 | ; |
| 367 | |
| 368 | struct_or_union_declaration |
| 369 | : struct_or_union_keyword optIdentifier |
| 370 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 371 | CompoundType *container = new CompoundType($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 372 | ast->enterScope(container); |
| 373 | } |
| 374 | struct_or_union_body |
| 375 | { |
| 376 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 377 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 378 | if (!container->setFields($4)) { |
| 379 | YYERROR; |
| 380 | } |
| 381 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 382 | ast->leaveScope(); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 383 | if (!ast->addScopedType($2, container)) { |
| 384 | YYERROR; |
| 385 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 386 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 387 | $$ = container->ref(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 388 | } |
| 389 | ; |
| 390 | |
| 391 | struct_or_union_body |
| 392 | : '{' field_declarations '}' { $$ = $2; } |
| 393 | ; |
| 394 | |
| 395 | field_declarations |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 396 | : /* empty */ { $$ = new std::vector<CompoundField *>; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 397 | | field_declarations field_declaration |
| 398 | { |
| 399 | $$ = $1; |
| 400 | |
| 401 | if ($2 != NULL) { |
| 402 | $$->push_back($2); |
| 403 | } |
| 404 | } |
| 405 | ; |
| 406 | |
| 407 | field_declaration |
| 408 | : type IDENTIFIER ';' { $$ = new CompoundField($2, $1); } |
| 409 | | struct_or_union_declaration ';' { $$ = NULL; } |
| 410 | | enum_declaration ';' { $$ = NULL; } |
| 411 | ; |
| 412 | |
| 413 | opt_storage_type |
| 414 | : /* empty */ { $$ = NULL; } |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 415 | | ':' fqname |
| 416 | { |
| 417 | $$ = $2; |
| 418 | |
| 419 | if ($$ != NULL && !$$->isValidEnumStorageType()) { |
| 420 | fprintf(stderr, "Invalid enum storage type specified.\n"); |
| 421 | YYABORT; |
| 422 | } |
| 423 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 424 | ; |
| 425 | |
| 426 | opt_comma |
| 427 | : /* empty */ |
| 428 | | ',' |
| 429 | ; |
| 430 | |
| 431 | named_enum_declaration |
| 432 | : ENUM IDENTIFIER opt_storage_type '{' enum_values opt_comma '}' |
| 433 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 434 | EnumType *enumType = new EnumType($5, $3); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 435 | if (!ast->addScopedType($2, enumType)) { |
| 436 | YYERROR; |
| 437 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 438 | } |
| 439 | ; |
| 440 | |
| 441 | enum_declaration |
| 442 | : ENUM '{' enum_values opt_comma '}' |
| 443 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 444 | EnumType *enumType = new EnumType($3); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 445 | if (!ast->addScopedType(NULL /* localName */, enumType)) { |
| 446 | YYERROR; |
| 447 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 448 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 449 | $$ = enumType->ref(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 450 | } |
| 451 | | ENUM IDENTIFIER opt_storage_type '{' enum_values opt_comma '}' |
| 452 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 453 | EnumType *enumType = new EnumType($5, $3); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 454 | if (!ast->addScopedType($2, enumType)) { |
| 455 | YYERROR; |
| 456 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 457 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 458 | $$ = enumType->ref(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 459 | } |
| 460 | ; |
| 461 | |
| 462 | enum_value |
| 463 | : IDENTIFIER { $$ = new EnumValue($1); } |
| 464 | | IDENTIFIER '=' INTEGER { $$ = new EnumValue($1, $3); } |
| 465 | | IDENTIFIER '=' IDENTIFIER { $$ = new EnumValue($1, $3); } |
Andreas Huber | 006189f | 2016-07-29 15:51:17 -0700 | [diff] [blame] | 466 | | IDENTIFIER '=' IDENTIFIER '+' INTEGER |
| 467 | { |
| 468 | $$ = new EnumValue( |
| 469 | $1, strdup( |
| 470 | android::String8::format("%s + %s", $3, $5).string())); |
| 471 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 472 | ; |
| 473 | |
| 474 | enum_values |
| 475 | : /* empty */ |
| 476 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 477 | $$ = new std::vector<EnumValue *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 478 | } |
| 479 | | enum_value |
| 480 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 481 | $$ = new std::vector<EnumValue *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 482 | $$->push_back($1); |
| 483 | } |
| 484 | | enum_values ',' enum_value |
| 485 | { |
| 486 | $$ = $1; |
| 487 | $$->push_back($3); |
| 488 | } |
| 489 | ; |
| 490 | |
| 491 | type |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 492 | : fqname { $$ = $1; } |
| 493 | | fqname '[' INTEGER ']' { $$ = new ArrayType($1, $3); } |
| 494 | | VEC '<' fqname '>' { $$ = new VectorType($3); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 495 | | struct_or_union_declaration { $$ = $1; } |
| 496 | | enum_declaration { $$ = $1; } |
| 497 | ; |
| 498 | |
| 499 | optIdentifier |
| 500 | : /* empty */ { $$ = NULL; } |
| 501 | | IDENTIFIER { $$ = $1; } |
| 502 | ; |
| 503 | |
| 504 | %% |