blob: 8937da47f790557d53d386098921fa95665801f7 [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"
Yifan Honga4b53d02016-10-31 17:29:10 -070028#include "Location.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070029#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070030#include "VectorType.h"
Yifan Hongbf459bc2016-08-23 16:50:37 -070031#include "RefType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070032
33#include "hidl-gen_y.h"
34
Andreas Huber709b62d2016-09-19 11:21:18 -070035#include <android-base/logging.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070036#include <stdio.h>
37
38using namespace android;
39
Andreas Huber0d0f9a22016-08-17 10:26:11 -070040extern int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
Andreas Huberc9410c72016-07-28 12:18:40 -070041
42#define scanner ast->scanner()
43
Yifan Honga4b53d02016-10-31 17:29:10 -070044::android::Location convertYYLoc(const yy::parser::location_type &loc) {
45 return ::android::Location(
46 ::android::Position(*(loc.begin.filename), loc.begin.line, loc.begin.column),
47 ::android::Position(*(loc.end.filename), loc.end.line, loc.end.column)
48 );
49}
50
51
Andreas Huberc9410c72016-07-28 12:18:40 -070052%}
53
Andreas Huber0d0f9a22016-08-17 10:26:11 -070054%initial-action {
55 // Initialize the initial location.
56 @$.begin.filename = @$.end.filename =
57 const_cast<std::string *>(&ast->getFilename());
58}
59
Andreas Huberc9410c72016-07-28 12:18:40 -070060%parse-param { android::AST *ast }
61%lex-param { void *scanner }
62%pure-parser
Steven Moreland4ab9b792016-09-26 14:14:07 -070063%glr-parser
Andreas Huber0d0f9a22016-08-17 10:26:11 -070064%skeleton "glr.cc"
Andreas Huberc9410c72016-07-28 12:18:40 -070065
Steven Moreland4ab9b792016-09-26 14:14:07 -070066%expect-rr 0
67
Andreas Huberc9410c72016-07-28 12:18:40 -070068%token<str> ENUM
69%token<str> EXTENDS
Andreas Huber84f89de2016-07-28 15:39:51 -070070%token<str> FQNAME
Andreas Huberc9410c72016-07-28 12:18:40 -070071%token<str> GENERATES
72%token<str> IDENTIFIER
73%token<str> IMPORT
74%token<str> INTEGER
Yifan Hong52165692016-08-12 18:06:40 -070075%token<str> FLOAT
Andreas Huberc9410c72016-07-28 12:18:40 -070076%token<str> INTERFACE
77%token<str> PACKAGE
Hridya Valsarajucd91bf62016-10-25 12:41:04 -070078%token<type> TYPE
Andreas Huberc9410c72016-07-28 12:18:40 -070079%token<str> STRUCT
80%token<str> STRING_LITERAL
81%token<str> TYPEDEF
82%token<str> UNION
Yifan Hongbf459bc2016-08-23 16:50:37 -070083%token<templatedType> TEMPLATED
Iliyan Malchev639bff82016-08-13 14:24:11 -070084%token<void> ONEWAY
Andreas Huberc9410c72016-07-28 12:18:40 -070085
Yifan Hong52165692016-08-12 18:06:40 -070086/* Operator precedence and associativity, as per
87 * http://en.cppreference.com/w/cpp/language/operator_precedence */
88/* Precedence level 15 ternary operator */
89%right '?' ':'
90/* Precedence level 13 - 14, LTR, logical operators*/
91%left LOGICAL_OR
92%left LOGICAL_AND
93/* Precedence level 10 - 12, LTR, bitwise operators*/
94%left '|'
95%left '^'
96%left '&'
97/* Precedence level 9, LTR */
98%left EQUALITY NEQ
99/* Precedence level 8, LTR */
100%left '<' '>' LEQ GEQ
101/* Precedence level 7, LTR */
102%left LSHIFT RSHIFT
103/* Precedence level 6, LTR */
104%left '+' '-'
105/* Precedence level 5, LTR */
106%left '*' '/' '%'
107/* Precedence level 3, RTL; but we have to use %left here */
108%left UNARY_MINUS UNARY_PLUS '!' '~'
109
Yifan Hong6a2fedf2016-10-11 13:44:07 -0700110%type<str> package
Yifan Hongae16eed2016-09-23 13:25:25 -0700111%type<fqName> fqname
112%type<type> fqtype
Andreas Huberc9410c72016-07-28 12:18:40 -0700113
114%type<type> type opt_storage_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700115%type<type> opt_extends
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700116%type<type> type_declaration_body interface_declaration typedef_declaration
117%type<type> named_struct_or_union_declaration named_enum_declaration
118%type<type> compound_declaration annotated_compound_declaration
Andreas Huberc9410c72016-07-28 12:18:40 -0700119
120%type<field> field_declaration
121%type<fields> field_declarations struct_or_union_body
Yifan Hong52165692016-08-12 18:06:40 -0700122%type<constantExpression> const_expr
Andreas Huberc9410c72016-07-28 12:18:40 -0700123%type<enumValue> enum_value
Yifan Hongf24fa852016-09-23 11:03:15 -0700124%type<enumValues> enum_values enum_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700125%type<typedVars> typed_vars
126%type<typedVar> typed_var
127%type<method> method_declaration
128%type<compoundStyle> struct_or_union_keyword
Yifan Hongf24fa852016-09-23 11:03:15 -0700129%type<stringVec> annotation_string_values annotation_string_value
130%type<constExprVec> annotation_const_expr_values annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700131%type<annotationParam> annotation_param
132%type<annotationParams> opt_annotation_params annotation_params
133%type<annotation> annotation
134%type<annotations> opt_annotations
Andreas Huberc9410c72016-07-28 12:18:40 -0700135
136%start program
137
138%union {
139 const char *str;
140 android::Type *type;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700141 android::TemplatedType *templatedType;
Yifan Hongae16eed2016-09-23 13:25:25 -0700142 android::FQName *fqName;
Andreas Huberc9410c72016-07-28 12:18:40 -0700143 android::CompoundType *compoundType;
144 android::CompoundField *field;
Andreas Huber881227d2016-08-02 14:20:21 -0700145 std::vector<android::CompoundField *> *fields;
Andreas Huberc9410c72016-07-28 12:18:40 -0700146 android::EnumValue *enumValue;
Yifan Hong52165692016-08-12 18:06:40 -0700147 android::ConstantExpression *constantExpression;
Andreas Huber881227d2016-08-02 14:20:21 -0700148 std::vector<android::EnumValue *> *enumValues;
Andreas Huberc9410c72016-07-28 12:18:40 -0700149 android::TypedVar *typedVar;
Andreas Huber881227d2016-08-02 14:20:21 -0700150 std::vector<android::TypedVar *> *typedVars;
Andreas Huberc9410c72016-07-28 12:18:40 -0700151 android::Method *method;
152 android::CompoundType::Style compoundStyle;
Andreas Huber3599d922016-08-09 10:42:57 -0700153 std::vector<std::string> *stringVec;
Yifan Hongf24fa852016-09-23 11:03:15 -0700154 std::vector<android::ConstantExpression *> *constExprVec;
Steven Morelandd537ab02016-09-12 10:32:01 -0700155 android::AnnotationParam *annotationParam;
156 android::AnnotationParamVector *annotationParams;
Andreas Huber3599d922016-08-09 10:42:57 -0700157 android::Annotation *annotation;
Steven Morelandd537ab02016-09-12 10:32:01 -0700158 std::vector<android::Annotation *> *annotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700159}
160
161%%
162
Andreas Huber3599d922016-08-09 10:42:57 -0700163opt_annotations
164 : /* empty */
165 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700166 $$ = new std::vector<Annotation *>;
Andreas Huber3599d922016-08-09 10:42:57 -0700167 }
168 | opt_annotations annotation
169 {
170 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700171 $$->push_back($2);
Andreas Huber3599d922016-08-09 10:42:57 -0700172 }
173 ;
174
175annotation
176 : '@' IDENTIFIER opt_annotation_params
177 {
178 $$ = new Annotation($2, $3);
179 }
180 ;
181
182opt_annotation_params
183 : /* empty */
184 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700185 $$ = new AnnotationParamVector;
Andreas Huber3599d922016-08-09 10:42:57 -0700186 }
187 | '(' annotation_params ')'
188 {
189 $$ = $2;
190 }
191 ;
192
193annotation_params
194 : annotation_param
195 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700196 $$ = new AnnotationParamVector;
197 $$->push_back($1);
Andreas Huber3599d922016-08-09 10:42:57 -0700198 }
199 | annotation_params ',' annotation_param
200 {
201 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700202 $$->push_back($3);
Andreas Huber3599d922016-08-09 10:42:57 -0700203 }
204 ;
205
206annotation_param
Yifan Hongf24fa852016-09-23 11:03:15 -0700207 : IDENTIFIER '=' annotation_string_value
208 {
209 $$ = new AnnotationParam($1, $3);
210 }
211 | IDENTIFIER '=' annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700212 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700213 $$ = new AnnotationParam($1, $3);
Andreas Huber3599d922016-08-09 10:42:57 -0700214 }
215 ;
216
Yifan Hongf24fa852016-09-23 11:03:15 -0700217annotation_string_value
Andreas Huber3599d922016-08-09 10:42:57 -0700218 : STRING_LITERAL
219 {
220 $$ = new std::vector<std::string>;
221 $$->push_back($1);
222 }
223 | '{' annotation_string_values '}' { $$ = $2; }
224 ;
225
226annotation_string_values
227 : STRING_LITERAL
228 {
229 $$ = new std::vector<std::string>;
230 $$->push_back($1);
231 }
232 | annotation_string_values ',' STRING_LITERAL
233 {
234 $$ = $1;
235 $$->push_back($3);
236 }
237 ;
238
Yifan Hongf24fa852016-09-23 11:03:15 -0700239annotation_const_expr_value
240 : const_expr
241 {
242 $$ = new std::vector<ConstantExpression *>;
243 $$->push_back($1);
244 }
245 | '{' annotation_const_expr_values '}' { $$ = $2; }
246 ;
247
248annotation_const_expr_values
249 : const_expr
250 {
251 $$ = new std::vector<ConstantExpression *>;
252 $$->push_back($1);
253 }
254 | annotation_const_expr_values ',' const_expr
255 {
256 $$ = $1;
257 $$->push_back($3);
258 }
259 ;
260
Andreas Huberc9410c72016-07-28 12:18:40 -0700261program
Andreas Huberda51b8e2016-07-28 16:00:57 -0700262 : package imports body
Andreas Huber84f89de2016-07-28 15:39:51 -0700263 ;
264
265fqname
266 : FQNAME
267 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700268 $$ = new FQName($1);
269 if(!$$->isValid()) {
270 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700271 << @1
Yifan Hongae16eed2016-09-23 13:25:25 -0700272 << ".\n";
Andreas Huber84f89de2016-07-28 15:39:51 -0700273 YYERROR;
274 }
275 }
276 | IDENTIFIER
277 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700278 $$ = new FQName($1);
279 if(!$$->isValid()) {
280 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
281 << @1
282 << ".\n";
283 YYERROR;
284 }
285 }
286 ;
287
288fqtype
289 : fqname
290 {
291 $$ = ast->lookupType(*($1));
Andreas Huber84f89de2016-07-28 15:39:51 -0700292 if ($$ == NULL) {
Yifan Hongae16eed2016-09-23 13:25:25 -0700293 std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at "
294 << @1
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700295 << "\n";
296
Andreas Huber84f89de2016-07-28 15:39:51 -0700297 YYERROR;
298 }
299 }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700300 | TYPE
Andreas Huber84f89de2016-07-28 15:39:51 -0700301 ;
Andreas Huberc9410c72016-07-28 12:18:40 -0700302
303package
Andreas Huber84f89de2016-07-28 15:39:51 -0700304 : PACKAGE FQNAME ';'
Andreas Hubereb1081f2016-07-28 13:13:24 -0700305 {
Andreas Huber84f89de2016-07-28 15:39:51 -0700306 if (!ast->setPackage($2)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700307 std::cerr << "ERROR: Malformed package identifier '"
308 << $2
309 << "' at "
310 << @2
311 << "\n";
312
Andreas Huber84f89de2016-07-28 15:39:51 -0700313 YYERROR;
314 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700315 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700316
Andreas Huberc9410c72016-07-28 12:18:40 -0700317imports
318 : /* empty */
Andreas Huber84f89de2016-07-28 15:39:51 -0700319 | imports IMPORT FQNAME ';'
Andreas Hubereb1081f2016-07-28 13:13:24 -0700320 {
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 Hubereb1081f2016-07-28 13:13:24 -0700327 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700328 | imports IMPORT IDENTIFIER ';'
329 {
Andreas Huber68f24592016-07-29 14:53:48 -0700330 if (!ast->addImport($3)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700331 std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3
332 << "\n";
Andreas Huber68f24592016-07-29 14:53:48 -0700333
334 YYERROR;
335 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700336 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700337 ;
338
339opt_extends
340 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700341 | EXTENDS fqtype { $$ = $2; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700342
343body
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700344 : type_declarations
Andreas Huberc9410c72016-07-28 12:18:40 -0700345 ;
346
347interface_declarations
348 : /* empty */
349 | interface_declarations type_declaration
350 | interface_declarations method_declaration
351 {
352 Interface *iface = static_cast<Interface *>(ast->scope());
Steven Moreland14ee6742016-10-18 12:58:28 -0700353 if (!iface->addMethod($2)) {
354 std::cerr << "ERROR: Unable to add method '" << $2->name()
355 << "' at " << @2 << "\n";
356
357 YYERROR;
358 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700359 }
360 ;
361
362type_declarations
Andreas Hubera2723d22016-07-29 15:36:07 -0700363 : /* empty */
Andreas Huberc9410c72016-07-28 12:18:40 -0700364 | type_declarations type_declaration
365 ;
366
367type_declaration
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700368 : opt_annotations type_declaration_body
369 {
370 if ($2 != nullptr) {
371 $2->setAnnotations($1);
372 } else if (!$1->empty()) {
373 // Since typedefs are always resolved to their target it makes
374 // little sense to annotate them and have their annotations
375 // impose semantics other than their target type.
376 std::cerr << "ERROR: typedefs cannot be annotated. at " << @2
377 << "\n";
378
379 YYERROR;
380 }
381 }
382 ;
383
384type_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700385 : named_struct_or_union_declaration ';'
386 | named_enum_declaration ';'
387 | typedef_declaration ';'
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700388 | interface_declaration ';'
389 ;
390
391interface_declaration
392 : INTERFACE IDENTIFIER opt_extends
393 {
394 if ($3 != NULL && !$3->isInterface()) {
395 std::cerr << "ERROR: You can only extend interfaces. at" << @3
396 << "\n";
397
398 YYERROR;
399 }
400
401 if ($2[0] != 'I') {
402 std::cerr << "ERROR: All interface names must start with an 'I' "
403 << "prefix. at " << @2 << "\n";
404
405 YYERROR;
406 }
407
Yifan Honga4b53d02016-10-31 17:29:10 -0700408 Interface *iface = new Interface($2, convertYYLoc(@2), static_cast<Interface *>($3));
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700409
410 // Register interface immediately so it can be referenced inside
411 // definition.
412 std::string errorMsg;
413 if (!ast->addScopedType(iface, &errorMsg)) {
414 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
415 YYERROR;
416 }
417
418 ast->enterScope(iface);
419 }
420 '{' interface_declarations '}'
421 {
422 Interface *iface = static_cast<Interface *>(ast->scope());
423
424 ast->leaveScope();
425
426 $$ = iface;
427 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700428 ;
429
430typedef_declaration
431 : TYPEDEF type IDENTIFIER
432 {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700433 std::string errorMsg;
Yifan Honga4b53d02016-10-31 17:29:10 -0700434 if (!ast->addTypeDef($3, $2, convertYYLoc(@3), &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700435 std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700436 YYERROR;
437 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700438
439 $$ = nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700440 }
441 ;
442
Yifan Hong52165692016-08-12 18:06:40 -0700443const_expr
Yifan Hongf24fa852016-09-23 11:03:15 -0700444 : INTEGER { $$ = new ConstantExpression($1); }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700445 | fqname
446 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700447 if(!$1->isValidValueName()) {
448 std::cerr << "ERROR: '" << $1->string()
449 << "' does not refer to an enum value at "
450 << @1 << ".\n";
451 YYERROR;
452 }
453 if($1->isIdentifier()) {
454 std::string identifier = $1->name();
455 LocalIdentifier *iden = ast->scope()->lookupIdentifier(identifier);
456 if(!iden) {
Steven Morelandc5908e62016-09-27 14:02:50 -0700457 std::cerr << "ERROR: at " << @1 << ", identifier " << $1->string()
Yifan Hongf24fa852016-09-23 11:03:15 -0700458 << " could not be found.\n";
459 YYERROR;
460 }
461 if(!iden->isEnumValue()) {
Steven Morelandc5908e62016-09-27 14:02:50 -0700462 std::cerr << "ERROR: at " << @1 << ", identifier " << $1->string()
Yifan Hongf24fa852016-09-23 11:03:15 -0700463 << " is not an enum value.\n";
464 YYERROR;
465 }
466 $$ = new ConstantExpression(
467 *(static_cast<EnumValue *>(iden)->constExpr()), $1->string());
468 } else {
469 std::string errorMsg;
470 EnumValue *v = ast->lookupEnumValue(*($1), &errorMsg);
471 if(v == nullptr) {
472 std::cerr << "ERROR: " << errorMsg << " at " << @1 << ".\n";
473 YYERROR;
474 }
475 $$ = new ConstantExpression(*(v->constExpr()), $1->string());
476 }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700477 }
Yifan Hong52165692016-08-12 18:06:40 -0700478 | const_expr '?' const_expr ':' const_expr
479 {
Yifan Hongb44a6c82016-09-22 15:50:18 -0700480 $$ = new ConstantExpression($1, $3, $5);
Yifan Hong52165692016-08-12 18:06:40 -0700481 }
482 | const_expr LOGICAL_OR const_expr { $$ = new ConstantExpression($1, "||", $3); }
483 | const_expr LOGICAL_AND 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 '&' const_expr { $$ = new ConstantExpression($1, "&" , $3); }
487 | const_expr EQUALITY const_expr { $$ = new ConstantExpression($1, "==", $3); }
488 | const_expr NEQ const_expr { $$ = new ConstantExpression($1, "!=", $3); }
489 | const_expr '<' const_expr { $$ = new ConstantExpression($1, "<" , $3); }
490 | const_expr '>' const_expr { $$ = new ConstantExpression($1, ">" , $3); }
491 | const_expr LEQ const_expr { $$ = new ConstantExpression($1, "<=", $3); }
492 | const_expr GEQ const_expr { $$ = new ConstantExpression($1, ">=", $3); }
493 | const_expr LSHIFT const_expr { $$ = new ConstantExpression($1, "<<", $3); }
494 | const_expr RSHIFT const_expr { $$ = new ConstantExpression($1, ">>", $3); }
495 | const_expr '+' const_expr { $$ = new ConstantExpression($1, "+" , $3); }
496 | const_expr '-' const_expr { $$ = new ConstantExpression($1, "-" , $3); }
497 | const_expr '*' const_expr { $$ = new ConstantExpression($1, "*" , $3); }
498 | const_expr '/' const_expr { $$ = new ConstantExpression($1, "/" , $3); }
499 | const_expr '%' const_expr { $$ = new ConstantExpression($1, "%" , $3); }
500 | '+' const_expr %prec UNARY_PLUS { $$ = new ConstantExpression("+", $2); }
501 | '-' const_expr %prec UNARY_MINUS { $$ = new ConstantExpression("-", $2); }
502 | '!' const_expr { $$ = new ConstantExpression("!", $2); }
503 | '~' const_expr { $$ = new ConstantExpression("~", $2); }
504 | '(' const_expr ')' { $$ = $2; }
505 ;
506
Andreas Huberc9410c72016-07-28 12:18:40 -0700507method_declaration
Andreas Huber3599d922016-08-09 10:42:57 -0700508 : opt_annotations IDENTIFIER '(' typed_vars ')' ';'
Andreas Huberc9410c72016-07-28 12:18:40 -0700509 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700510 $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1);
511 }
512 | opt_annotations ONEWAY IDENTIFIER '(' typed_vars ')' ';'
513 {
514 $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700515 }
Andreas Huber3599d922016-08-09 10:42:57 -0700516 | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' ';'
Andreas Huberc9410c72016-07-28 12:18:40 -0700517 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700518 $$ = new Method($2, $4, $8, false, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700519 }
520 ;
521
522typed_vars
523 : /* empty */
524 {
Andreas Huber881227d2016-08-02 14:20:21 -0700525 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700526 }
527 | typed_var
528 {
Andreas Huber881227d2016-08-02 14:20:21 -0700529 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700530 $$->push_back($1);
531 }
532 | typed_vars ',' typed_var
533 {
534 $$ = $1;
535 $$->push_back($3);
536 }
537 ;
538
539typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); }
540 ;
541
542
543struct_or_union_keyword
544 : STRUCT { $$ = CompoundType::STYLE_STRUCT; }
545 | UNION { $$ = CompoundType::STYLE_UNION; }
546 ;
547
548named_struct_or_union_declaration
549 : struct_or_union_keyword IDENTIFIER
550 {
Yifan Honga4b53d02016-10-31 17:29:10 -0700551 CompoundType *container = new CompoundType($1, $2, convertYYLoc(@2));
Andreas Huberc9410c72016-07-28 12:18:40 -0700552 ast->enterScope(container);
553 }
554 struct_or_union_body
555 {
556 CompoundType *container = static_cast<CompoundType *>(ast->scope());
557
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700558 std::string errorMsg;
559 if (!container->setFields($4, &errorMsg)) {
560 std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700561 YYERROR;
562 }
563
Andreas Huberc9410c72016-07-28 12:18:40 -0700564 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700565
Andreas Huber9ed827c2016-08-22 12:31:13 -0700566 if (!ast->addScopedType(container, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700567 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700568 YYERROR;
569 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700570
571 $$ = container;
Andreas Huberc9410c72016-07-28 12:18:40 -0700572 }
573 ;
574
Andreas Huberc9410c72016-07-28 12:18:40 -0700575struct_or_union_body
576 : '{' field_declarations '}' { $$ = $2; }
577 ;
578
579field_declarations
Andreas Huber881227d2016-08-02 14:20:21 -0700580 : /* empty */ { $$ = new std::vector<CompoundField *>; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700581 | field_declarations field_declaration
582 {
583 $$ = $1;
584
585 if ($2 != NULL) {
586 $$->push_back($2);
587 }
588 }
589 ;
590
591field_declaration
592 : type IDENTIFIER ';' { $$ = new CompoundField($2, $1); }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700593 | annotated_compound_declaration ';' { $$ = NULL; }
594 ;
595
596annotated_compound_declaration
597 : opt_annotations compound_declaration
598 {
599 $2->setAnnotations($1);
600 $$ = $2;
601 }
602 ;
603
604compound_declaration
Yifan Honga2855012016-10-11 13:36:54 -0700605 : named_struct_or_union_declaration { $$ = $1; }
Yifan Hong6a2fedf2016-10-11 13:44:07 -0700606 | named_enum_declaration { $$ = $1; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700607 ;
608
609opt_storage_type
610 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700611 | ':' fqtype
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700612 {
613 $$ = $2;
614
615 if ($$ != NULL && !$$->isValidEnumStorageType()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700616 std::cerr << "ERROR: Invalid enum storage type specified. at "
617 << @2 << "\n";
618
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700619 YYABORT;
620 }
621 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700622 ;
623
624opt_comma
625 : /* empty */
626 | ','
627 ;
628
629named_enum_declaration
Yifan Hongf24fa852016-09-23 11:03:15 -0700630 : ENUM IDENTIFIER opt_storage_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700631 {
Yifan Honga4b53d02016-10-31 17:29:10 -0700632 ast->enterScope(new EnumType($2, convertYYLoc(@2), $3));
Yifan Hongf24fa852016-09-23 11:03:15 -0700633 }
634 enum_declaration_body
635 {
636 EnumType *enumType = static_cast<EnumType *>(ast->scope());
637 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700638
639 std::string errorMsg;
Andreas Huber9ed827c2016-08-22 12:31:13 -0700640 if (!ast->addScopedType(enumType, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700641 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700642 YYERROR;
643 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700644
645 $$ = enumType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700646 }
647 ;
648
Yifan Hongf24fa852016-09-23 11:03:15 -0700649enum_declaration_body
650 : '{' enum_values opt_comma '}' { $$ = $2; }
651 ;
652
Andreas Huberc9410c72016-07-28 12:18:40 -0700653enum_value
654 : IDENTIFIER { $$ = new EnumValue($1); }
Yifan Hong57886972016-08-17 10:42:15 -0700655 | IDENTIFIER '=' const_expr { $$ = new EnumValue($1, $3); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700656 ;
657
658enum_values
659 : /* empty */
Yifan Hongf24fa852016-09-23 11:03:15 -0700660 { /* do nothing */ }
Andreas Huberc9410c72016-07-28 12:18:40 -0700661 | enum_value
662 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700663 static_cast<EnumType *>(ast->scope())->addValue($1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700664 }
665 | enum_values ',' enum_value
666 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700667 static_cast<EnumType *>(ast->scope())->addValue($3);
Andreas Huberc9410c72016-07-28 12:18:40 -0700668 }
669 ;
670
671type
Yifan Hongae16eed2016-09-23 13:25:25 -0700672 : fqtype { $$ = $1; }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700673 | type '[' const_expr ']'
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700674 {
Andreas Huber295ad302016-08-16 11:35:00 -0700675 if ($1->isBinder()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700676 std::cerr << "ERROR: Arrays of interface types are not supported."
677 << " at " << @1 << "\n";
Andreas Huber70a59e12016-08-16 12:57:01 -0700678
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700679 YYERROR;
680 }
681
Andreas Huber709b62d2016-09-19 11:21:18 -0700682 if ($1->isArray()) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700683 $$ = new ArrayType(static_cast<ArrayType *>($1), $3);
Andreas Huber709b62d2016-09-19 11:21:18 -0700684 } else {
Yifan Hongf24fa852016-09-23 11:03:15 -0700685 $$ = new ArrayType($1, $3);
Andreas Huber709b62d2016-09-19 11:21:18 -0700686 }
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700687 }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700688 | TEMPLATED '<' type '>'
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700689 {
Andreas Huber86a112b2016-10-19 14:25:16 -0700690 if (!$1->isVector() && $3->isBinder()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700691 std::cerr << "ERROR: TemplatedType of interface types are not "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700692 << "supported. at " << @3 << "\n";
Andreas Huber70a59e12016-08-16 12:57:01 -0700693
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700694 YYERROR;
695 }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700696 $1->setElementType($3);
697 $$ = $1;
698 }
699 | TEMPLATED '<' TEMPLATED '<' type RSHIFT
700 {
701 if ($5->isBinder()) {
702 std::cerr << "ERROR: TemplatedType of interface types are not "
703 << "supported. at " << @5 << "\n";
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700704
Yifan Hongbf459bc2016-08-23 16:50:37 -0700705 YYERROR;
706 }
707 $3->setElementType($5);
708 $1->setElementType($3);
709 $$ = $1;
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700710 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700711 | annotated_compound_declaration { $$ = $1; }
Andreas Huber295ad302016-08-16 11:35:00 -0700712 | INTERFACE { $$ = new GenericBinder; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700713 ;
714
Andreas Huberc9410c72016-07-28 12:18:40 -0700715%%
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700716
717#include <android-base/logging.h>
718
719void yy::parser::error(
720 const yy::parser::location_type &where,
721 const std::string &errstr) {
722 std::cerr << "ERROR: " << errstr << " at " << where << "\n";
723}
724