blob: f5c940624dabd9285eab48f7465f240966cf8660 [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
Yifan Hongbd33e382016-11-02 13:30:17 -0700115%type<type> array_type_base
116%type<arrayType> array_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700117%type<type> opt_extends
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700118%type<type> type_declaration_body interface_declaration typedef_declaration
119%type<type> named_struct_or_union_declaration named_enum_declaration
120%type<type> compound_declaration annotated_compound_declaration
Andreas Huberc9410c72016-07-28 12:18:40 -0700121
122%type<field> field_declaration
123%type<fields> field_declarations struct_or_union_body
Yifan Hong52165692016-08-12 18:06:40 -0700124%type<constantExpression> const_expr
Andreas Huberc9410c72016-07-28 12:18:40 -0700125%type<enumValue> enum_value
Yifan Hongf24fa852016-09-23 11:03:15 -0700126%type<enumValues> enum_values enum_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700127%type<typedVars> typed_vars
128%type<typedVar> typed_var
129%type<method> method_declaration
130%type<compoundStyle> struct_or_union_keyword
Yifan Hongf24fa852016-09-23 11:03:15 -0700131%type<stringVec> annotation_string_values annotation_string_value
132%type<constExprVec> annotation_const_expr_values annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700133%type<annotationParam> annotation_param
134%type<annotationParams> opt_annotation_params annotation_params
135%type<annotation> annotation
136%type<annotations> opt_annotations
Andreas Huberc9410c72016-07-28 12:18:40 -0700137
138%start program
139
140%union {
141 const char *str;
142 android::Type *type;
Yifan Hongbd33e382016-11-02 13:30:17 -0700143 android::ArrayType *arrayType;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700144 android::TemplatedType *templatedType;
Yifan Hongae16eed2016-09-23 13:25:25 -0700145 android::FQName *fqName;
Andreas Huberc9410c72016-07-28 12:18:40 -0700146 android::CompoundType *compoundType;
147 android::CompoundField *field;
Andreas Huber881227d2016-08-02 14:20:21 -0700148 std::vector<android::CompoundField *> *fields;
Andreas Huberc9410c72016-07-28 12:18:40 -0700149 android::EnumValue *enumValue;
Yifan Hong52165692016-08-12 18:06:40 -0700150 android::ConstantExpression *constantExpression;
Andreas Huber881227d2016-08-02 14:20:21 -0700151 std::vector<android::EnumValue *> *enumValues;
Andreas Huberc9410c72016-07-28 12:18:40 -0700152 android::TypedVar *typedVar;
Andreas Huber881227d2016-08-02 14:20:21 -0700153 std::vector<android::TypedVar *> *typedVars;
Andreas Huberc9410c72016-07-28 12:18:40 -0700154 android::Method *method;
155 android::CompoundType::Style compoundStyle;
Andreas Huber3599d922016-08-09 10:42:57 -0700156 std::vector<std::string> *stringVec;
Yifan Hongf24fa852016-09-23 11:03:15 -0700157 std::vector<android::ConstantExpression *> *constExprVec;
Steven Morelandd537ab02016-09-12 10:32:01 -0700158 android::AnnotationParam *annotationParam;
159 android::AnnotationParamVector *annotationParams;
Andreas Huber3599d922016-08-09 10:42:57 -0700160 android::Annotation *annotation;
Steven Morelandd537ab02016-09-12 10:32:01 -0700161 std::vector<android::Annotation *> *annotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700162}
163
164%%
165
Andreas Huber3599d922016-08-09 10:42:57 -0700166opt_annotations
167 : /* empty */
168 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700169 $$ = new std::vector<Annotation *>;
Andreas Huber3599d922016-08-09 10:42:57 -0700170 }
171 | opt_annotations annotation
172 {
173 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700174 $$->push_back($2);
Andreas Huber3599d922016-08-09 10:42:57 -0700175 }
176 ;
177
178annotation
179 : '@' IDENTIFIER opt_annotation_params
180 {
181 $$ = new Annotation($2, $3);
182 }
183 ;
184
185opt_annotation_params
186 : /* empty */
187 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700188 $$ = new AnnotationParamVector;
Andreas Huber3599d922016-08-09 10:42:57 -0700189 }
190 | '(' annotation_params ')'
191 {
192 $$ = $2;
193 }
194 ;
195
196annotation_params
197 : annotation_param
198 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700199 $$ = new AnnotationParamVector;
200 $$->push_back($1);
Andreas Huber3599d922016-08-09 10:42:57 -0700201 }
202 | annotation_params ',' annotation_param
203 {
204 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700205 $$->push_back($3);
Andreas Huber3599d922016-08-09 10:42:57 -0700206 }
207 ;
208
209annotation_param
Yifan Hongf24fa852016-09-23 11:03:15 -0700210 : IDENTIFIER '=' annotation_string_value
211 {
212 $$ = new AnnotationParam($1, $3);
213 }
214 | IDENTIFIER '=' annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700215 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700216 $$ = new AnnotationParam($1, $3);
Andreas Huber3599d922016-08-09 10:42:57 -0700217 }
218 ;
219
Yifan Hongf24fa852016-09-23 11:03:15 -0700220annotation_string_value
Andreas Huber3599d922016-08-09 10:42:57 -0700221 : STRING_LITERAL
222 {
223 $$ = new std::vector<std::string>;
224 $$->push_back($1);
225 }
226 | '{' annotation_string_values '}' { $$ = $2; }
227 ;
228
229annotation_string_values
230 : STRING_LITERAL
231 {
232 $$ = new std::vector<std::string>;
233 $$->push_back($1);
234 }
235 | annotation_string_values ',' STRING_LITERAL
236 {
237 $$ = $1;
238 $$->push_back($3);
239 }
240 ;
241
Yifan Hongf24fa852016-09-23 11:03:15 -0700242annotation_const_expr_value
243 : const_expr
244 {
245 $$ = new std::vector<ConstantExpression *>;
246 $$->push_back($1);
247 }
248 | '{' annotation_const_expr_values '}' { $$ = $2; }
249 ;
250
251annotation_const_expr_values
252 : const_expr
253 {
254 $$ = new std::vector<ConstantExpression *>;
255 $$->push_back($1);
256 }
257 | annotation_const_expr_values ',' const_expr
258 {
259 $$ = $1;
260 $$->push_back($3);
261 }
262 ;
263
Andreas Huberc9410c72016-07-28 12:18:40 -0700264program
Andreas Huberda51b8e2016-07-28 16:00:57 -0700265 : package imports body
Andreas Huber84f89de2016-07-28 15:39:51 -0700266 ;
267
268fqname
269 : FQNAME
270 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700271 $$ = new FQName($1);
272 if(!$$->isValid()) {
273 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700274 << @1
Yifan Hongae16eed2016-09-23 13:25:25 -0700275 << ".\n";
Andreas Huber84f89de2016-07-28 15:39:51 -0700276 YYERROR;
277 }
278 }
279 | IDENTIFIER
280 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700281 $$ = new FQName($1);
282 if(!$$->isValid()) {
283 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
284 << @1
285 << ".\n";
286 YYERROR;
287 }
288 }
289 ;
290
291fqtype
292 : fqname
293 {
294 $$ = ast->lookupType(*($1));
Andreas Huber84f89de2016-07-28 15:39:51 -0700295 if ($$ == NULL) {
Yifan Hongae16eed2016-09-23 13:25:25 -0700296 std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at "
297 << @1
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700298 << "\n";
299
Andreas Huber84f89de2016-07-28 15:39:51 -0700300 YYERROR;
301 }
302 }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700303 | TYPE
Andreas Huber84f89de2016-07-28 15:39:51 -0700304 ;
Andreas Huberc9410c72016-07-28 12:18:40 -0700305
306package
Andreas Huber84f89de2016-07-28 15:39:51 -0700307 : PACKAGE FQNAME ';'
Andreas Hubereb1081f2016-07-28 13:13:24 -0700308 {
Andreas Huber84f89de2016-07-28 15:39:51 -0700309 if (!ast->setPackage($2)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700310 std::cerr << "ERROR: Malformed package identifier '"
311 << $2
312 << "' at "
313 << @2
314 << "\n";
315
Andreas Huber84f89de2016-07-28 15:39:51 -0700316 YYERROR;
317 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700318 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700319
Andreas Huberc9410c72016-07-28 12:18:40 -0700320imports
321 : /* empty */
Andreas Huber84f89de2016-07-28 15:39:51 -0700322 | imports IMPORT FQNAME ';'
Andreas Hubereb1081f2016-07-28 13:13:24 -0700323 {
Andreas Huber68f24592016-07-29 14:53:48 -0700324 if (!ast->addImport($3)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700325 std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3
326 << "\n";
Andreas Huber68f24592016-07-29 14:53:48 -0700327
328 YYERROR;
329 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700330 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700331 | imports IMPORT IDENTIFIER ';'
332 {
Andreas Huber68f24592016-07-29 14:53:48 -0700333 if (!ast->addImport($3)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700334 std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3
335 << "\n";
Andreas Huber68f24592016-07-29 14:53:48 -0700336
337 YYERROR;
338 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700339 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700340 ;
341
342opt_extends
343 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700344 | EXTENDS fqtype { $$ = $2; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700345
346body
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700347 : type_declarations
Andreas Huberc9410c72016-07-28 12:18:40 -0700348 ;
349
350interface_declarations
351 : /* empty */
352 | interface_declarations type_declaration
353 | interface_declarations method_declaration
354 {
355 Interface *iface = static_cast<Interface *>(ast->scope());
Steven Moreland14ee6742016-10-18 12:58:28 -0700356 if (!iface->addMethod($2)) {
357 std::cerr << "ERROR: Unable to add method '" << $2->name()
358 << "' at " << @2 << "\n";
359
360 YYERROR;
361 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700362 }
363 ;
364
365type_declarations
Andreas Hubera2723d22016-07-29 15:36:07 -0700366 : /* empty */
Andreas Huberc9410c72016-07-28 12:18:40 -0700367 | type_declarations type_declaration
368 ;
369
370type_declaration
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700371 : opt_annotations type_declaration_body
372 {
373 if ($2 != nullptr) {
374 $2->setAnnotations($1);
375 } else if (!$1->empty()) {
376 // Since typedefs are always resolved to their target it makes
377 // little sense to annotate them and have their annotations
378 // impose semantics other than their target type.
379 std::cerr << "ERROR: typedefs cannot be annotated. at " << @2
380 << "\n";
381
382 YYERROR;
383 }
384 }
385 ;
386
387type_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700388 : named_struct_or_union_declaration ';'
389 | named_enum_declaration ';'
390 | typedef_declaration ';'
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700391 | interface_declaration ';'
392 ;
393
394interface_declaration
395 : INTERFACE IDENTIFIER opt_extends
396 {
397 if ($3 != NULL && !$3->isInterface()) {
398 std::cerr << "ERROR: You can only extend interfaces. at" << @3
399 << "\n";
400
401 YYERROR;
402 }
403
404 if ($2[0] != 'I') {
405 std::cerr << "ERROR: All interface names must start with an 'I' "
406 << "prefix. at " << @2 << "\n";
407
408 YYERROR;
409 }
410
Yifan Honga4b53d02016-10-31 17:29:10 -0700411 Interface *iface = new Interface($2, convertYYLoc(@2), static_cast<Interface *>($3));
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700412
413 // Register interface immediately so it can be referenced inside
414 // definition.
415 std::string errorMsg;
416 if (!ast->addScopedType(iface, &errorMsg)) {
417 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
418 YYERROR;
419 }
420
421 ast->enterScope(iface);
422 }
423 '{' interface_declarations '}'
424 {
425 Interface *iface = static_cast<Interface *>(ast->scope());
426
427 ast->leaveScope();
428
429 $$ = iface;
430 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700431 ;
432
433typedef_declaration
434 : TYPEDEF type IDENTIFIER
435 {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700436 std::string errorMsg;
Yifan Honga4b53d02016-10-31 17:29:10 -0700437 if (!ast->addTypeDef($3, $2, convertYYLoc(@3), &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700438 std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700439 YYERROR;
440 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700441
442 $$ = nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700443 }
444 ;
445
Yifan Hong52165692016-08-12 18:06:40 -0700446const_expr
Yifan Hongf24fa852016-09-23 11:03:15 -0700447 : INTEGER { $$ = new ConstantExpression($1); }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700448 | fqname
449 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700450 if(!$1->isValidValueName()) {
451 std::cerr << "ERROR: '" << $1->string()
452 << "' does not refer to an enum value at "
453 << @1 << ".\n";
454 YYERROR;
455 }
456 if($1->isIdentifier()) {
457 std::string identifier = $1->name();
458 LocalIdentifier *iden = ast->scope()->lookupIdentifier(identifier);
459 if(!iden) {
Steven Morelandc5908e62016-09-27 14:02:50 -0700460 std::cerr << "ERROR: at " << @1 << ", identifier " << $1->string()
Yifan Hongf24fa852016-09-23 11:03:15 -0700461 << " could not be found.\n";
462 YYERROR;
463 }
464 if(!iden->isEnumValue()) {
Steven Morelandc5908e62016-09-27 14:02:50 -0700465 std::cerr << "ERROR: at " << @1 << ", identifier " << $1->string()
Yifan Hongf24fa852016-09-23 11:03:15 -0700466 << " is not an enum value.\n";
467 YYERROR;
468 }
469 $$ = new ConstantExpression(
470 *(static_cast<EnumValue *>(iden)->constExpr()), $1->string());
471 } else {
472 std::string errorMsg;
473 EnumValue *v = ast->lookupEnumValue(*($1), &errorMsg);
474 if(v == nullptr) {
475 std::cerr << "ERROR: " << errorMsg << " at " << @1 << ".\n";
476 YYERROR;
477 }
478 $$ = new ConstantExpression(*(v->constExpr()), $1->string());
479 }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700480 }
Yifan Hong52165692016-08-12 18:06:40 -0700481 | const_expr '?' const_expr ':' const_expr
482 {
Yifan Hongb44a6c82016-09-22 15:50:18 -0700483 $$ = new ConstantExpression($1, $3, $5);
Yifan Hong52165692016-08-12 18:06:40 -0700484 }
485 | const_expr LOGICAL_OR const_expr { $$ = new ConstantExpression($1, "||", $3); }
486 | const_expr LOGICAL_AND const_expr { $$ = new ConstantExpression($1, "&&", $3); }
487 | const_expr '|' const_expr { $$ = new ConstantExpression($1, "|" , $3); }
488 | const_expr '^' const_expr { $$ = new ConstantExpression($1, "^" , $3); }
489 | const_expr '&' const_expr { $$ = new ConstantExpression($1, "&" , $3); }
490 | const_expr EQUALITY const_expr { $$ = new ConstantExpression($1, "==", $3); }
491 | const_expr NEQ const_expr { $$ = new ConstantExpression($1, "!=", $3); }
492 | const_expr '<' const_expr { $$ = new ConstantExpression($1, "<" , $3); }
493 | const_expr '>' const_expr { $$ = new ConstantExpression($1, ">" , $3); }
494 | const_expr LEQ const_expr { $$ = new ConstantExpression($1, "<=", $3); }
495 | const_expr GEQ const_expr { $$ = new ConstantExpression($1, ">=", $3); }
496 | const_expr LSHIFT const_expr { $$ = new ConstantExpression($1, "<<", $3); }
497 | const_expr RSHIFT 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 '*' const_expr { $$ = new ConstantExpression($1, "*" , $3); }
501 | const_expr '/' const_expr { $$ = new ConstantExpression($1, "/" , $3); }
502 | const_expr '%' const_expr { $$ = new ConstantExpression($1, "%" , $3); }
503 | '+' const_expr %prec UNARY_PLUS { $$ = new ConstantExpression("+", $2); }
504 | '-' const_expr %prec UNARY_MINUS { $$ = new ConstantExpression("-", $2); }
505 | '!' const_expr { $$ = new ConstantExpression("!", $2); }
506 | '~' const_expr { $$ = new ConstantExpression("~", $2); }
507 | '(' const_expr ')' { $$ = $2; }
508 ;
509
Andreas Huberc9410c72016-07-28 12:18:40 -0700510method_declaration
Andreas Huber3599d922016-08-09 10:42:57 -0700511 : opt_annotations IDENTIFIER '(' typed_vars ')' ';'
Andreas Huberc9410c72016-07-28 12:18:40 -0700512 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700513 $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1);
514 }
515 | opt_annotations ONEWAY IDENTIFIER '(' typed_vars ')' ';'
516 {
517 $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700518 }
Andreas Huber3599d922016-08-09 10:42:57 -0700519 | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' ';'
Andreas Huberc9410c72016-07-28 12:18:40 -0700520 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700521 $$ = new Method($2, $4, $8, false, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700522 }
523 ;
524
525typed_vars
526 : /* empty */
527 {
Andreas Huber881227d2016-08-02 14:20:21 -0700528 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700529 }
530 | typed_var
531 {
Andreas Huber881227d2016-08-02 14:20:21 -0700532 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700533 $$->push_back($1);
534 }
535 | typed_vars ',' typed_var
536 {
537 $$ = $1;
538 $$->push_back($3);
539 }
540 ;
541
542typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); }
543 ;
544
545
546struct_or_union_keyword
547 : STRUCT { $$ = CompoundType::STYLE_STRUCT; }
548 | UNION { $$ = CompoundType::STYLE_UNION; }
549 ;
550
551named_struct_or_union_declaration
552 : struct_or_union_keyword IDENTIFIER
553 {
Yifan Honga4b53d02016-10-31 17:29:10 -0700554 CompoundType *container = new CompoundType($1, $2, convertYYLoc(@2));
Andreas Huberc9410c72016-07-28 12:18:40 -0700555 ast->enterScope(container);
556 }
557 struct_or_union_body
558 {
559 CompoundType *container = static_cast<CompoundType *>(ast->scope());
560
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700561 std::string errorMsg;
562 if (!container->setFields($4, &errorMsg)) {
563 std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700564 YYERROR;
565 }
566
Andreas Huberc9410c72016-07-28 12:18:40 -0700567 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700568
Andreas Huber9ed827c2016-08-22 12:31:13 -0700569 if (!ast->addScopedType(container, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700570 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700571 YYERROR;
572 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700573
574 $$ = container;
Andreas Huberc9410c72016-07-28 12:18:40 -0700575 }
576 ;
577
Andreas Huberc9410c72016-07-28 12:18:40 -0700578struct_or_union_body
579 : '{' field_declarations '}' { $$ = $2; }
580 ;
581
582field_declarations
Andreas Huber881227d2016-08-02 14:20:21 -0700583 : /* empty */ { $$ = new std::vector<CompoundField *>; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700584 | field_declarations field_declaration
585 {
586 $$ = $1;
587
588 if ($2 != NULL) {
589 $$->push_back($2);
590 }
591 }
592 ;
593
594field_declaration
595 : type IDENTIFIER ';' { $$ = new CompoundField($2, $1); }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700596 | annotated_compound_declaration ';' { $$ = NULL; }
597 ;
598
599annotated_compound_declaration
600 : opt_annotations compound_declaration
601 {
602 $2->setAnnotations($1);
603 $$ = $2;
604 }
605 ;
606
607compound_declaration
Yifan Honga2855012016-10-11 13:36:54 -0700608 : named_struct_or_union_declaration { $$ = $1; }
Yifan Hong6a2fedf2016-10-11 13:44:07 -0700609 | named_enum_declaration { $$ = $1; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700610 ;
611
612opt_storage_type
613 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700614 | ':' fqtype
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700615 {
616 $$ = $2;
617
618 if ($$ != NULL && !$$->isValidEnumStorageType()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700619 std::cerr << "ERROR: Invalid enum storage type specified. at "
620 << @2 << "\n";
621
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700622 YYABORT;
623 }
624 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700625 ;
626
627opt_comma
628 : /* empty */
629 | ','
630 ;
631
632named_enum_declaration
Yifan Hongf24fa852016-09-23 11:03:15 -0700633 : ENUM IDENTIFIER opt_storage_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700634 {
Yifan Honga4b53d02016-10-31 17:29:10 -0700635 ast->enterScope(new EnumType($2, convertYYLoc(@2), $3));
Yifan Hongf24fa852016-09-23 11:03:15 -0700636 }
637 enum_declaration_body
638 {
639 EnumType *enumType = static_cast<EnumType *>(ast->scope());
640 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700641
642 std::string errorMsg;
Andreas Huber9ed827c2016-08-22 12:31:13 -0700643 if (!ast->addScopedType(enumType, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700644 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700645 YYERROR;
646 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700647
648 $$ = enumType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700649 }
650 ;
651
Yifan Hongf24fa852016-09-23 11:03:15 -0700652enum_declaration_body
653 : '{' enum_values opt_comma '}' { $$ = $2; }
654 ;
655
Andreas Huberc9410c72016-07-28 12:18:40 -0700656enum_value
657 : IDENTIFIER { $$ = new EnumValue($1); }
Yifan Hong57886972016-08-17 10:42:15 -0700658 | IDENTIFIER '=' const_expr { $$ = new EnumValue($1, $3); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700659 ;
660
661enum_values
662 : /* empty */
Yifan Hongf24fa852016-09-23 11:03:15 -0700663 { /* do nothing */ }
Andreas Huberc9410c72016-07-28 12:18:40 -0700664 | enum_value
665 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700666 static_cast<EnumType *>(ast->scope())->addValue($1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700667 }
668 | enum_values ',' enum_value
669 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700670 static_cast<EnumType *>(ast->scope())->addValue($3);
Andreas Huberc9410c72016-07-28 12:18:40 -0700671 }
672 ;
673
Yifan Hongbd33e382016-11-02 13:30:17 -0700674array_type_base
Yifan Hongae16eed2016-09-23 13:25:25 -0700675 : fqtype { $$ = $1; }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700676 | TEMPLATED '<' type '>'
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700677 {
Andreas Huber86a112b2016-10-19 14:25:16 -0700678 if (!$1->isVector() && $3->isBinder()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700679 std::cerr << "ERROR: TemplatedType of interface types are not "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700680 << "supported. at " << @3 << "\n";
Andreas Huber70a59e12016-08-16 12:57:01 -0700681
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700682 YYERROR;
683 }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700684 $1->setElementType($3);
685 $$ = $1;
686 }
687 | TEMPLATED '<' TEMPLATED '<' type RSHIFT
688 {
689 if ($5->isBinder()) {
690 std::cerr << "ERROR: TemplatedType of interface types are not "
691 << "supported. at " << @5 << "\n";
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700692
Yifan Hongbf459bc2016-08-23 16:50:37 -0700693 YYERROR;
694 }
695 $3->setElementType($5);
696 $1->setElementType($3);
697 $$ = $1;
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700698 }
Yifan Hongbd33e382016-11-02 13:30:17 -0700699 ;
700
701array_type
702 : array_type_base '[' const_expr ']'
703 {
704 if ($1->isBinder()) {
705 std::cerr << "ERROR: Arrays of interface types are not supported."
706 << " at " << @1 << "\n";
707
708 YYERROR;
709 }
710 if ($1->isArray()) {
711 $$ = new ArrayType(static_cast<ArrayType *>($1), $3);
712 } else {
713 $$ = new ArrayType($1, $3);
714 }
715 }
716 | array_type '[' const_expr ']'
717 {
718 $$ = $1;
719 $$->appendDimension($3);
720 }
721 ;
722
723type
724 : array_type_base { $$ = $1; }
725 | array_type { $$ = $1; }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700726 | annotated_compound_declaration { $$ = $1; }
Andreas Huber295ad302016-08-16 11:35:00 -0700727 | INTERFACE { $$ = new GenericBinder; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700728 ;
729
Andreas Huberc9410c72016-07-28 12:18:40 -0700730%%
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700731
732#include <android-base/logging.h>
733
734void yy::parser::error(
735 const yy::parser::location_type &where,
736 const std::string &errstr) {
737 std::cerr << "ERROR: " << errstr << " at " << where << "\n";
738}
739