blob: 0e7483eb5eb11e32f0abfc168148a5f34ef3c00c [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huberc9410c72016-07-28 12:18:40 -070017%{
18
Andreas Huber3599d922016-08-09 10:42:57 -070019#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070020#include "AST.h"
21#include "ArrayType.h"
22#include "CompoundType.h"
Yifan Hong52165692016-08-12 18:06:40 -070023#include "ConstantExpression.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070024#include "EnumType.h"
Yifan Hongf24fa852016-09-23 11:03:15 -070025#include "FQName.h"
Andreas Huber295ad302016-08-16 11:35:00 -070026#include "GenericBinder.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070027#include "Interface.h"
28#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070029#include "VectorType.h"
30
31#include "hidl-gen_y.h"
32
Andreas Huber709b62d2016-09-19 11:21:18 -070033#include <android-base/logging.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070034#include <stdio.h>
35
36using namespace android;
37
Andreas Huber0d0f9a22016-08-17 10:26:11 -070038extern int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
Andreas Huberc9410c72016-07-28 12:18:40 -070039
40#define scanner ast->scanner()
41
42%}
43
Andreas Huber0d0f9a22016-08-17 10:26:11 -070044%initial-action {
45 // Initialize the initial location.
46 @$.begin.filename = @$.end.filename =
47 const_cast<std::string *>(&ast->getFilename());
48}
49
Andreas Huberc9410c72016-07-28 12:18:40 -070050%parse-param { android::AST *ast }
51%lex-param { void *scanner }
52%pure-parser
Steven Moreland4ab9b792016-09-26 14:14:07 -070053%glr-parser
Andreas Huber0d0f9a22016-08-17 10:26:11 -070054%skeleton "glr.cc"
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Steven Moreland4ab9b792016-09-26 14:14:07 -070056%expect-rr 0
57
Andreas Huberc9410c72016-07-28 12:18:40 -070058%token<str> ENUM
59%token<str> EXTENDS
Andreas Huber84f89de2016-07-28 15:39:51 -070060%token<str> FQNAME
Andreas Huberc9410c72016-07-28 12:18:40 -070061%token<str> GENERATES
62%token<str> IDENTIFIER
63%token<str> IMPORT
64%token<str> INTEGER
Yifan Hong52165692016-08-12 18:06:40 -070065%token<str> FLOAT
Andreas Huberc9410c72016-07-28 12:18:40 -070066%token<str> INTERFACE
67%token<str> PACKAGE
Andreas Huber84f89de2016-07-28 15:39:51 -070068%token<type> SCALAR
Andreas Huberc9410c72016-07-28 12:18:40 -070069%token<str> STRUCT
70%token<str> STRING_LITERAL
71%token<str> TYPEDEF
72%token<str> UNION
73%token<str> VEC
Iliyan Malchev639bff82016-08-13 14:24:11 -070074%token<void> ONEWAY
Andreas Huberc9410c72016-07-28 12:18:40 -070075
Yifan Hong52165692016-08-12 18:06:40 -070076/* Operator precedence and associativity, as per
77 * http://en.cppreference.com/w/cpp/language/operator_precedence */
78/* Precedence level 15 ternary operator */
79%right '?' ':'
80/* Precedence level 13 - 14, LTR, logical operators*/
81%left LOGICAL_OR
82%left LOGICAL_AND
83/* Precedence level 10 - 12, LTR, bitwise operators*/
84%left '|'
85%left '^'
86%left '&'
87/* Precedence level 9, LTR */
88%left EQUALITY NEQ
89/* Precedence level 8, LTR */
90%left '<' '>' LEQ GEQ
91/* Precedence level 7, LTR */
92%left LSHIFT RSHIFT
93/* Precedence level 6, LTR */
94%left '+' '-'
95/* Precedence level 5, LTR */
96%left '*' '/' '%'
97/* Precedence level 3, RTL; but we have to use %left here */
98%left UNARY_MINUS UNARY_PLUS '!' '~'
99
Andreas Huberc9410c72016-07-28 12:18:40 -0700100%type<str> optIdentifier package
Yifan Hongae16eed2016-09-23 13:25:25 -0700101%type<fqName> fqname
102%type<type> fqtype
Andreas Huberc9410c72016-07-28 12:18:40 -0700103
104%type<type> type opt_storage_type
105%type<type> enum_declaration
106%type<type> struct_or_union_declaration
107%type<type> opt_extends
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700108%type<type> type_declaration_body interface_declaration typedef_declaration
109%type<type> named_struct_or_union_declaration named_enum_declaration
110%type<type> compound_declaration annotated_compound_declaration
Andreas Huberc9410c72016-07-28 12:18:40 -0700111
112%type<field> field_declaration
113%type<fields> field_declarations struct_or_union_body
Yifan Hong52165692016-08-12 18:06:40 -0700114%type<constantExpression> const_expr
Andreas Huberc9410c72016-07-28 12:18:40 -0700115%type<enumValue> enum_value
Yifan Hongf24fa852016-09-23 11:03:15 -0700116%type<enumValues> enum_values enum_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700117%type<typedVars> typed_vars
118%type<typedVar> typed_var
119%type<method> method_declaration
120%type<compoundStyle> struct_or_union_keyword
Yifan Hongf24fa852016-09-23 11:03:15 -0700121%type<stringVec> annotation_string_values annotation_string_value
122%type<constExprVec> annotation_const_expr_values annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700123%type<annotationParam> annotation_param
124%type<annotationParams> opt_annotation_params annotation_params
125%type<annotation> annotation
126%type<annotations> opt_annotations
Andreas Huberc9410c72016-07-28 12:18:40 -0700127
128%start program
129
130%union {
131 const char *str;
132 android::Type *type;
Yifan Hongae16eed2016-09-23 13:25:25 -0700133 android::FQName *fqName;
Andreas Huberc9410c72016-07-28 12:18:40 -0700134 android::CompoundType *compoundType;
135 android::CompoundField *field;
Andreas Huber881227d2016-08-02 14:20:21 -0700136 std::vector<android::CompoundField *> *fields;
Andreas Huberc9410c72016-07-28 12:18:40 -0700137 android::EnumValue *enumValue;
Yifan Hong52165692016-08-12 18:06:40 -0700138 android::ConstantExpression *constantExpression;
Andreas Huber881227d2016-08-02 14:20:21 -0700139 std::vector<android::EnumValue *> *enumValues;
Andreas Huberc9410c72016-07-28 12:18:40 -0700140 android::TypedVar *typedVar;
Andreas Huber881227d2016-08-02 14:20:21 -0700141 std::vector<android::TypedVar *> *typedVars;
Andreas Huberc9410c72016-07-28 12:18:40 -0700142 android::Method *method;
143 android::CompoundType::Style compoundStyle;
Andreas Huber3599d922016-08-09 10:42:57 -0700144 std::vector<std::string> *stringVec;
Yifan Hongf24fa852016-09-23 11:03:15 -0700145 std::vector<android::ConstantExpression *> *constExprVec;
Steven Morelandd537ab02016-09-12 10:32:01 -0700146 android::AnnotationParam *annotationParam;
147 android::AnnotationParamVector *annotationParams;
Andreas Huber3599d922016-08-09 10:42:57 -0700148 android::Annotation *annotation;
Steven Morelandd537ab02016-09-12 10:32:01 -0700149 std::vector<android::Annotation *> *annotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700150}
151
152%%
153
Andreas Huber3599d922016-08-09 10:42:57 -0700154opt_annotations
155 : /* empty */
156 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700157 $$ = new std::vector<Annotation *>;
Andreas Huber3599d922016-08-09 10:42:57 -0700158 }
159 | opt_annotations annotation
160 {
161 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700162 $$->push_back($2);
Andreas Huber3599d922016-08-09 10:42:57 -0700163 }
164 ;
165
166annotation
167 : '@' IDENTIFIER opt_annotation_params
168 {
169 $$ = new Annotation($2, $3);
170 }
171 ;
172
173opt_annotation_params
174 : /* empty */
175 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700176 $$ = new AnnotationParamVector;
Andreas Huber3599d922016-08-09 10:42:57 -0700177 }
178 | '(' annotation_params ')'
179 {
180 $$ = $2;
181 }
182 ;
183
184annotation_params
185 : annotation_param
186 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700187 $$ = new AnnotationParamVector;
188 $$->push_back($1);
Andreas Huber3599d922016-08-09 10:42:57 -0700189 }
190 | annotation_params ',' annotation_param
191 {
192 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700193 $$->push_back($3);
Andreas Huber3599d922016-08-09 10:42:57 -0700194 }
195 ;
196
197annotation_param
Yifan Hongf24fa852016-09-23 11:03:15 -0700198 : IDENTIFIER '=' annotation_string_value
199 {
200 $$ = new AnnotationParam($1, $3);
201 }
202 | IDENTIFIER '=' annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700203 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700204 $$ = new AnnotationParam($1, $3);
Andreas Huber3599d922016-08-09 10:42:57 -0700205 }
206 ;
207
Yifan Hongf24fa852016-09-23 11:03:15 -0700208annotation_string_value
Andreas Huber3599d922016-08-09 10:42:57 -0700209 : STRING_LITERAL
210 {
211 $$ = new std::vector<std::string>;
212 $$->push_back($1);
213 }
214 | '{' annotation_string_values '}' { $$ = $2; }
215 ;
216
217annotation_string_values
218 : STRING_LITERAL
219 {
220 $$ = new std::vector<std::string>;
221 $$->push_back($1);
222 }
223 | annotation_string_values ',' STRING_LITERAL
224 {
225 $$ = $1;
226 $$->push_back($3);
227 }
228 ;
229
Yifan Hongf24fa852016-09-23 11:03:15 -0700230annotation_const_expr_value
231 : const_expr
232 {
233 $$ = new std::vector<ConstantExpression *>;
234 $$->push_back($1);
235 }
236 | '{' annotation_const_expr_values '}' { $$ = $2; }
237 ;
238
239annotation_const_expr_values
240 : const_expr
241 {
242 $$ = new std::vector<ConstantExpression *>;
243 $$->push_back($1);
244 }
245 | annotation_const_expr_values ',' const_expr
246 {
247 $$ = $1;
248 $$->push_back($3);
249 }
250 ;
251
Andreas Huberc9410c72016-07-28 12:18:40 -0700252program
Andreas Huberda51b8e2016-07-28 16:00:57 -0700253 : package imports body
Andreas Huber84f89de2016-07-28 15:39:51 -0700254 ;
255
256fqname
257 : FQNAME
258 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700259 $$ = new FQName($1);
260 if(!$$->isValid()) {
261 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700262 << @1
Yifan Hongae16eed2016-09-23 13:25:25 -0700263 << ".\n";
Andreas Huber84f89de2016-07-28 15:39:51 -0700264 YYERROR;
265 }
266 }
267 | IDENTIFIER
268 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700269 $$ = new FQName($1);
270 if(!$$->isValid()) {
271 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
272 << @1
273 << ".\n";
274 YYERROR;
275 }
276 }
277 ;
278
279fqtype
280 : fqname
281 {
282 $$ = ast->lookupType(*($1));
Andreas Huber84f89de2016-07-28 15:39:51 -0700283 if ($$ == NULL) {
Yifan Hongae16eed2016-09-23 13:25:25 -0700284 std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at "
285 << @1
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700286 << "\n";
287
Andreas Huber84f89de2016-07-28 15:39:51 -0700288 YYERROR;
289 }
290 }
291 | SCALAR
292 ;
Andreas Huberc9410c72016-07-28 12:18:40 -0700293
294package
Andreas Huber84f89de2016-07-28 15:39:51 -0700295 : PACKAGE FQNAME ';'
Andreas Hubereb1081f2016-07-28 13:13:24 -0700296 {
Andreas Huber84f89de2016-07-28 15:39:51 -0700297 if (!ast->setPackage($2)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700298 std::cerr << "ERROR: Malformed package identifier '"
299 << $2
300 << "' at "
301 << @2
302 << "\n";
303
Andreas Huber84f89de2016-07-28 15:39:51 -0700304 YYERROR;
305 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700306 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700307
Andreas Huberc9410c72016-07-28 12:18:40 -0700308imports
309 : /* empty */
Andreas Huber84f89de2016-07-28 15:39:51 -0700310 | imports IMPORT FQNAME ';'
Andreas Hubereb1081f2016-07-28 13:13:24 -0700311 {
Andreas Huber68f24592016-07-29 14:53:48 -0700312 if (!ast->addImport($3)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700313 std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3
314 << "\n";
Andreas Huber68f24592016-07-29 14:53:48 -0700315
316 YYERROR;
317 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700318 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700319 | imports IMPORT IDENTIFIER ';'
320 {
Andreas Huber68f24592016-07-29 14:53:48 -0700321 if (!ast->addImport($3)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700322 std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3
323 << "\n";
Andreas Huber68f24592016-07-29 14:53:48 -0700324
325 YYERROR;
326 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700327 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700328 ;
329
330opt_extends
331 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700332 | EXTENDS fqtype { $$ = $2; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700333
334body
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700335 : type_declarations
Andreas Huberc9410c72016-07-28 12:18:40 -0700336 ;
337
338interface_declarations
339 : /* empty */
340 | interface_declarations type_declaration
341 | interface_declarations method_declaration
342 {
343 Interface *iface = static_cast<Interface *>(ast->scope());
344 iface->addMethod($2);
345 }
346 ;
347
348type_declarations
Andreas Hubera2723d22016-07-29 15:36:07 -0700349 : /* empty */
Andreas Huberc9410c72016-07-28 12:18:40 -0700350 | type_declarations type_declaration
351 ;
352
353type_declaration
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700354 : opt_annotations type_declaration_body
355 {
356 if ($2 != nullptr) {
357 $2->setAnnotations($1);
358 } else if (!$1->empty()) {
359 // Since typedefs are always resolved to their target it makes
360 // little sense to annotate them and have their annotations
361 // impose semantics other than their target type.
362 std::cerr << "ERROR: typedefs cannot be annotated. at " << @2
363 << "\n";
364
365 YYERROR;
366 }
367 }
368 ;
369
370type_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700371 : named_struct_or_union_declaration ';'
372 | named_enum_declaration ';'
373 | typedef_declaration ';'
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700374 | interface_declaration ';'
375 ;
376
377interface_declaration
378 : INTERFACE IDENTIFIER opt_extends
379 {
380 if ($3 != NULL && !$3->isInterface()) {
381 std::cerr << "ERROR: You can only extend interfaces. at" << @3
382 << "\n";
383
384 YYERROR;
385 }
386
387 if ($2[0] != 'I') {
388 std::cerr << "ERROR: All interface names must start with an 'I' "
389 << "prefix. at " << @2 << "\n";
390
391 YYERROR;
392 }
393
394 Interface *iface = new Interface($2, static_cast<Interface *>($3));
395
396 // Register interface immediately so it can be referenced inside
397 // definition.
398 std::string errorMsg;
399 if (!ast->addScopedType(iface, &errorMsg)) {
400 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
401 YYERROR;
402 }
403
404 ast->enterScope(iface);
405 }
406 '{' interface_declarations '}'
407 {
408 Interface *iface = static_cast<Interface *>(ast->scope());
409
410 ast->leaveScope();
411
412 $$ = iface;
413 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700414 ;
415
416typedef_declaration
417 : TYPEDEF type IDENTIFIER
418 {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700419 std::string errorMsg;
Andreas Huber39fa7182016-08-19 14:27:33 -0700420 if (!ast->addTypeDef($3, $2, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700421 std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700422 YYERROR;
423 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700424
425 $$ = nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700426 }
427 ;
428
Yifan Hong52165692016-08-12 18:06:40 -0700429const_expr
Yifan Hongf24fa852016-09-23 11:03:15 -0700430 : INTEGER { $$ = new ConstantExpression($1); }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700431 | fqname
432 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700433 if(!$1->isValidValueName()) {
434 std::cerr << "ERROR: '" << $1->string()
435 << "' does not refer to an enum value at "
436 << @1 << ".\n";
437 YYERROR;
438 }
439 if($1->isIdentifier()) {
440 std::string identifier = $1->name();
441 LocalIdentifier *iden = ast->scope()->lookupIdentifier(identifier);
442 if(!iden) {
Steven Morelandc5908e62016-09-27 14:02:50 -0700443 std::cerr << "ERROR: at " << @1 << ", identifier " << $1->string()
Yifan Hongf24fa852016-09-23 11:03:15 -0700444 << " could not be found.\n";
445 YYERROR;
446 }
447 if(!iden->isEnumValue()) {
Steven Morelandc5908e62016-09-27 14:02:50 -0700448 std::cerr << "ERROR: at " << @1 << ", identifier " << $1->string()
Yifan Hongf24fa852016-09-23 11:03:15 -0700449 << " is not an enum value.\n";
450 YYERROR;
451 }
452 $$ = new ConstantExpression(
453 *(static_cast<EnumValue *>(iden)->constExpr()), $1->string());
454 } else {
455 std::string errorMsg;
456 EnumValue *v = ast->lookupEnumValue(*($1), &errorMsg);
457 if(v == nullptr) {
458 std::cerr << "ERROR: " << errorMsg << " at " << @1 << ".\n";
459 YYERROR;
460 }
461 $$ = new ConstantExpression(*(v->constExpr()), $1->string());
462 }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700463 }
Yifan Hong52165692016-08-12 18:06:40 -0700464 | const_expr '?' const_expr ':' const_expr
465 {
Yifan Hongb44a6c82016-09-22 15:50:18 -0700466 $$ = new ConstantExpression($1, $3, $5);
Yifan Hong52165692016-08-12 18:06:40 -0700467 }
468 | const_expr LOGICAL_OR const_expr { $$ = new ConstantExpression($1, "||", $3); }
469 | const_expr LOGICAL_AND const_expr { $$ = new ConstantExpression($1, "&&", $3); }
470 | const_expr '|' const_expr { $$ = new ConstantExpression($1, "|" , $3); }
471 | const_expr '^' const_expr { $$ = new ConstantExpression($1, "^" , $3); }
472 | const_expr '&' const_expr { $$ = new ConstantExpression($1, "&" , $3); }
473 | const_expr EQUALITY const_expr { $$ = new ConstantExpression($1, "==", $3); }
474 | const_expr NEQ const_expr { $$ = new ConstantExpression($1, "!=", $3); }
475 | const_expr '<' const_expr { $$ = new ConstantExpression($1, "<" , $3); }
476 | const_expr '>' const_expr { $$ = new ConstantExpression($1, ">" , $3); }
477 | const_expr LEQ const_expr { $$ = new ConstantExpression($1, "<=", $3); }
478 | const_expr GEQ const_expr { $$ = new ConstantExpression($1, ">=", $3); }
479 | const_expr LSHIFT const_expr { $$ = new ConstantExpression($1, "<<", $3); }
480 | const_expr RSHIFT const_expr { $$ = new ConstantExpression($1, ">>", $3); }
481 | const_expr '+' const_expr { $$ = new ConstantExpression($1, "+" , $3); }
482 | const_expr '-' const_expr { $$ = new ConstantExpression($1, "-" , $3); }
483 | const_expr '*' const_expr { $$ = new ConstantExpression($1, "*" , $3); }
484 | const_expr '/' const_expr { $$ = new ConstantExpression($1, "/" , $3); }
485 | const_expr '%' const_expr { $$ = new ConstantExpression($1, "%" , $3); }
486 | '+' const_expr %prec UNARY_PLUS { $$ = new ConstantExpression("+", $2); }
487 | '-' const_expr %prec UNARY_MINUS { $$ = new ConstantExpression("-", $2); }
488 | '!' const_expr { $$ = new ConstantExpression("!", $2); }
489 | '~' const_expr { $$ = new ConstantExpression("~", $2); }
490 | '(' const_expr ')' { $$ = $2; }
491 ;
492
Andreas Huberc9410c72016-07-28 12:18:40 -0700493method_declaration
Andreas Huber3599d922016-08-09 10:42:57 -0700494 : opt_annotations IDENTIFIER '(' typed_vars ')' ';'
Andreas Huberc9410c72016-07-28 12:18:40 -0700495 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700496 $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1);
497 }
498 | opt_annotations ONEWAY IDENTIFIER '(' typed_vars ')' ';'
499 {
500 $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700501 }
Andreas Huber3599d922016-08-09 10:42:57 -0700502 | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' ';'
Andreas Huberc9410c72016-07-28 12:18:40 -0700503 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700504 $$ = new Method($2, $4, $8, false, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700505 }
506 ;
507
508typed_vars
509 : /* empty */
510 {
Andreas Huber881227d2016-08-02 14:20:21 -0700511 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700512 }
513 | typed_var
514 {
Andreas Huber881227d2016-08-02 14:20:21 -0700515 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700516 $$->push_back($1);
517 }
518 | typed_vars ',' typed_var
519 {
520 $$ = $1;
521 $$->push_back($3);
522 }
523 ;
524
525typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); }
526 ;
527
528
529struct_or_union_keyword
530 : STRUCT { $$ = CompoundType::STYLE_STRUCT; }
531 | UNION { $$ = CompoundType::STYLE_UNION; }
532 ;
533
534named_struct_or_union_declaration
535 : struct_or_union_keyword IDENTIFIER
536 {
Andreas Huber9ed827c2016-08-22 12:31:13 -0700537 CompoundType *container = new CompoundType($1, $2);
Andreas Huberc9410c72016-07-28 12:18:40 -0700538 ast->enterScope(container);
539 }
540 struct_or_union_body
541 {
542 CompoundType *container = static_cast<CompoundType *>(ast->scope());
543
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700544 std::string errorMsg;
545 if (!container->setFields($4, &errorMsg)) {
546 std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700547 YYERROR;
548 }
549
Andreas Huberc9410c72016-07-28 12:18:40 -0700550 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700551
Andreas Huber9ed827c2016-08-22 12:31:13 -0700552 if (!ast->addScopedType(container, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700553 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700554 YYERROR;
555 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700556
557 $$ = container;
Andreas Huberc9410c72016-07-28 12:18:40 -0700558 }
559 ;
560
561struct_or_union_declaration
562 : struct_or_union_keyword optIdentifier
563 {
Andreas Huber9ed827c2016-08-22 12:31:13 -0700564 const char *localName = $2;
565 std::string anonName;
566 if (localName == nullptr) {
567 anonName = ast->scope()->pickUniqueAnonymousName();
568 localName = anonName.c_str();
569 }
570
571 CompoundType *container = new CompoundType($1, localName);
Andreas Huberc9410c72016-07-28 12:18:40 -0700572 ast->enterScope(container);
573 }
574 struct_or_union_body
575 {
576 CompoundType *container = static_cast<CompoundType *>(ast->scope());
577
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700578 std::string errorMsg;
579 if (!container->setFields($4, &errorMsg)) {
580 std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700581 YYERROR;
582 }
583
Andreas Huberc9410c72016-07-28 12:18:40 -0700584 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700585
Andreas Huber9ed827c2016-08-22 12:31:13 -0700586 if (!ast->addScopedType(container, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700587 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700588 YYERROR;
589 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700590
Andreas Huberfd4afab2016-08-03 13:02:57 -0700591 $$ = container->ref();
Andreas Huberc9410c72016-07-28 12:18:40 -0700592 }
593 ;
594
595struct_or_union_body
596 : '{' field_declarations '}' { $$ = $2; }
597 ;
598
599field_declarations
Andreas Huber881227d2016-08-02 14:20:21 -0700600 : /* empty */ { $$ = new std::vector<CompoundField *>; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700601 | field_declarations field_declaration
602 {
603 $$ = $1;
604
605 if ($2 != NULL) {
606 $$->push_back($2);
607 }
608 }
609 ;
610
611field_declaration
612 : type IDENTIFIER ';' { $$ = new CompoundField($2, $1); }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700613 | annotated_compound_declaration ';' { $$ = NULL; }
614 ;
615
616annotated_compound_declaration
617 : opt_annotations compound_declaration
618 {
619 $2->setAnnotations($1);
620 $$ = $2;
621 }
622 ;
623
624compound_declaration
625 : struct_or_union_declaration { $$ = $1; }
626 | enum_declaration { $$ = $1; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700627 ;
628
629opt_storage_type
630 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700631 | ':' fqtype
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700632 {
633 $$ = $2;
634
635 if ($$ != NULL && !$$->isValidEnumStorageType()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700636 std::cerr << "ERROR: Invalid enum storage type specified. at "
637 << @2 << "\n";
638
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700639 YYABORT;
640 }
641 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700642 ;
643
644opt_comma
645 : /* empty */
646 | ','
647 ;
648
649named_enum_declaration
Yifan Hongf24fa852016-09-23 11:03:15 -0700650 : ENUM IDENTIFIER opt_storage_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700651 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700652 ast->enterScope(new EnumType($2, $3));
653 }
654 enum_declaration_body
655 {
656 EnumType *enumType = static_cast<EnumType *>(ast->scope());
657 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700658
659 std::string errorMsg;
Andreas Huber9ed827c2016-08-22 12:31:13 -0700660 if (!ast->addScopedType(enumType, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700661 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700662 YYERROR;
663 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700664
665 $$ = enumType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700666 }
667 ;
668
669enum_declaration
Yifan Hongf24fa852016-09-23 11:03:15 -0700670 : ENUM
Andreas Huberc9410c72016-07-28 12:18:40 -0700671 {
Andreas Huber9ed827c2016-08-22 12:31:13 -0700672 std::string anonName = ast->scope()->pickUniqueAnonymousName();
Yifan Hongf24fa852016-09-23 11:03:15 -0700673 ast->enterScope(new EnumType(anonName.c_str()));
674 }
675 enum_declaration_body
676 {
Andreas Huber9ed827c2016-08-22 12:31:13 -0700677
Yifan Hongf24fa852016-09-23 11:03:15 -0700678 EnumType *enumType = static_cast<EnumType *>(ast->scope());
679 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700680
681 std::string errorMsg;
Andreas Huber9ed827c2016-08-22 12:31:13 -0700682 if (!ast->addScopedType(enumType, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700683 // This should never fail.
684 std::cerr << "ERROR: " << errorMsg << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700685 YYERROR;
686 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700687
Andreas Huberfd4afab2016-08-03 13:02:57 -0700688 $$ = enumType->ref();
Andreas Huberc9410c72016-07-28 12:18:40 -0700689 }
Yifan Hongf24fa852016-09-23 11:03:15 -0700690 | ENUM IDENTIFIER opt_storage_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700691 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700692 ast->enterScope(new EnumType($2, $3));
693 }
694 enum_declaration_body
695 {
696 EnumType *enumType = static_cast<EnumType *>(ast->scope());
697 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700698
699 std::string errorMsg;
Andreas Huber9ed827c2016-08-22 12:31:13 -0700700 if (!ast->addScopedType(enumType, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700701 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700702 YYERROR;
703 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700704
Andreas Huberfd4afab2016-08-03 13:02:57 -0700705 $$ = enumType->ref();
Andreas Huberc9410c72016-07-28 12:18:40 -0700706 }
707 ;
708
Yifan Hongf24fa852016-09-23 11:03:15 -0700709enum_declaration_body
710 : '{' enum_values opt_comma '}' { $$ = $2; }
711 ;
712
Andreas Huberc9410c72016-07-28 12:18:40 -0700713enum_value
714 : IDENTIFIER { $$ = new EnumValue($1); }
Yifan Hong57886972016-08-17 10:42:15 -0700715 | IDENTIFIER '=' const_expr { $$ = new EnumValue($1, $3); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700716 ;
717
718enum_values
719 : /* empty */
Yifan Hongf24fa852016-09-23 11:03:15 -0700720 { /* do nothing */ }
Andreas Huberc9410c72016-07-28 12:18:40 -0700721 | enum_value
722 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700723 static_cast<EnumType *>(ast->scope())->addValue($1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700724 }
725 | enum_values ',' enum_value
726 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700727 static_cast<EnumType *>(ast->scope())->addValue($3);
Andreas Huberc9410c72016-07-28 12:18:40 -0700728 }
729 ;
730
731type
Yifan Hongae16eed2016-09-23 13:25:25 -0700732 : fqtype { $$ = $1; }
Yifan Hongf24fa852016-09-23 11:03:15 -0700733 | fqtype '[' const_expr ']'
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700734 {
Andreas Huber295ad302016-08-16 11:35:00 -0700735 if ($1->isBinder()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700736 std::cerr << "ERROR: Arrays of interface types are not supported."
737 << " at " << @1 << "\n";
Andreas Huber70a59e12016-08-16 12:57:01 -0700738
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700739 YYERROR;
740 }
741
Andreas Huber709b62d2016-09-19 11:21:18 -0700742 if ($1->isArray()) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700743 $$ = new ArrayType(static_cast<ArrayType *>($1), $3);
Andreas Huber709b62d2016-09-19 11:21:18 -0700744 } else {
Yifan Hongf24fa852016-09-23 11:03:15 -0700745 $$ = new ArrayType($1, $3);
Andreas Huber709b62d2016-09-19 11:21:18 -0700746 }
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700747 }
Yifan Hongae16eed2016-09-23 13:25:25 -0700748 | VEC '<' fqtype '>'
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700749 {
Andreas Huber295ad302016-08-16 11:35:00 -0700750 if ($3->isBinder()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700751 std::cerr << "ERROR: Vectors of interface types are not "
752 << "supported. at " << @3 << "\n";
Andreas Huber70a59e12016-08-16 12:57:01 -0700753
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700754 YYERROR;
755 }
756
757 $$ = new VectorType($3);
758 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700759 | annotated_compound_declaration { $$ = $1; }
Andreas Huber295ad302016-08-16 11:35:00 -0700760 | INTERFACE { $$ = new GenericBinder; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700761 ;
762
763optIdentifier
764 : /* empty */ { $$ = NULL; }
765 | IDENTIFIER { $$ = $1; }
766 ;
767
768%%
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700769
770#include <android-base/logging.h>
771
772void yy::parser::error(
773 const yy::parser::location_type &where,
774 const std::string &errstr) {
775 std::cerr << "ERROR: " << errstr << " at " << where << "\n";
776}
777