blob: 4e81a4909dc24e6caaacbab0acb69428397154d0 [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 Hongbe627b32016-10-28 18:38:56 -0700110%type<str> error_stmt opt_error_stmt error
Yifan Hong6a2fedf2016-10-11 13:44:07 -0700111%type<str> package
Yifan Hongae16eed2016-09-23 13:25:25 -0700112%type<fqName> fqname
113%type<type> fqtype
Andreas Huberc9410c72016-07-28 12:18:40 -0700114
115%type<type> type opt_storage_type
Yifan Hongbd33e382016-11-02 13:30:17 -0700116%type<type> array_type_base
117%type<arrayType> array_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700118%type<type> opt_extends
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700119%type<type> type_declaration_body interface_declaration typedef_declaration
120%type<type> named_struct_or_union_declaration named_enum_declaration
121%type<type> compound_declaration annotated_compound_declaration
Andreas Huberc9410c72016-07-28 12:18:40 -0700122
123%type<field> field_declaration
124%type<fields> field_declarations struct_or_union_body
Yifan Hong52165692016-08-12 18:06:40 -0700125%type<constantExpression> const_expr
Andreas Huberc9410c72016-07-28 12:18:40 -0700126%type<enumValue> enum_value
Yifan Hongf24fa852016-09-23 11:03:15 -0700127%type<enumValues> enum_values enum_declaration_body
Andreas Huberc9410c72016-07-28 12:18:40 -0700128%type<typedVars> typed_vars
129%type<typedVar> typed_var
130%type<method> method_declaration
131%type<compoundStyle> struct_or_union_keyword
Yifan Hongf24fa852016-09-23 11:03:15 -0700132%type<stringVec> annotation_string_values annotation_string_value
133%type<constExprVec> annotation_const_expr_values annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700134%type<annotationParam> annotation_param
135%type<annotationParams> opt_annotation_params annotation_params
136%type<annotation> annotation
137%type<annotations> opt_annotations
Andreas Huberc9410c72016-07-28 12:18:40 -0700138
139%start program
140
141%union {
142 const char *str;
143 android::Type *type;
Yifan Hongbd33e382016-11-02 13:30:17 -0700144 android::ArrayType *arrayType;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700145 android::TemplatedType *templatedType;
Yifan Hongae16eed2016-09-23 13:25:25 -0700146 android::FQName *fqName;
Andreas Huberc9410c72016-07-28 12:18:40 -0700147 android::CompoundType *compoundType;
148 android::CompoundField *field;
Andreas Huber881227d2016-08-02 14:20:21 -0700149 std::vector<android::CompoundField *> *fields;
Andreas Huberc9410c72016-07-28 12:18:40 -0700150 android::EnumValue *enumValue;
Yifan Hong52165692016-08-12 18:06:40 -0700151 android::ConstantExpression *constantExpression;
Andreas Huber881227d2016-08-02 14:20:21 -0700152 std::vector<android::EnumValue *> *enumValues;
Andreas Huberc9410c72016-07-28 12:18:40 -0700153 android::TypedVar *typedVar;
Andreas Huber881227d2016-08-02 14:20:21 -0700154 std::vector<android::TypedVar *> *typedVars;
Andreas Huberc9410c72016-07-28 12:18:40 -0700155 android::Method *method;
156 android::CompoundType::Style compoundStyle;
Andreas Huber3599d922016-08-09 10:42:57 -0700157 std::vector<std::string> *stringVec;
Yifan Hongf24fa852016-09-23 11:03:15 -0700158 std::vector<android::ConstantExpression *> *constExprVec;
Steven Morelandd537ab02016-09-12 10:32:01 -0700159 android::AnnotationParam *annotationParam;
160 android::AnnotationParamVector *annotationParams;
Andreas Huber3599d922016-08-09 10:42:57 -0700161 android::Annotation *annotation;
Steven Morelandd537ab02016-09-12 10:32:01 -0700162 std::vector<android::Annotation *> *annotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700163}
164
165%%
166
Andreas Huber3599d922016-08-09 10:42:57 -0700167opt_annotations
168 : /* empty */
169 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700170 $$ = new std::vector<Annotation *>;
Andreas Huber3599d922016-08-09 10:42:57 -0700171 }
172 | opt_annotations annotation
173 {
174 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700175 $$->push_back($2);
Andreas Huber3599d922016-08-09 10:42:57 -0700176 }
177 ;
178
179annotation
180 : '@' IDENTIFIER opt_annotation_params
181 {
182 $$ = new Annotation($2, $3);
183 }
184 ;
185
186opt_annotation_params
187 : /* empty */
188 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700189 $$ = new AnnotationParamVector;
Andreas Huber3599d922016-08-09 10:42:57 -0700190 }
191 | '(' annotation_params ')'
192 {
193 $$ = $2;
194 }
195 ;
196
197annotation_params
198 : annotation_param
199 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700200 $$ = new AnnotationParamVector;
201 $$->push_back($1);
Andreas Huber3599d922016-08-09 10:42:57 -0700202 }
203 | annotation_params ',' annotation_param
204 {
205 $$ = $1;
Steven Morelandd537ab02016-09-12 10:32:01 -0700206 $$->push_back($3);
Andreas Huber3599d922016-08-09 10:42:57 -0700207 }
208 ;
209
210annotation_param
Yifan Hongf24fa852016-09-23 11:03:15 -0700211 : IDENTIFIER '=' annotation_string_value
212 {
213 $$ = new AnnotationParam($1, $3);
214 }
215 | IDENTIFIER '=' annotation_const_expr_value
Andreas Huber3599d922016-08-09 10:42:57 -0700216 {
Steven Morelandd537ab02016-09-12 10:32:01 -0700217 $$ = new AnnotationParam($1, $3);
Andreas Huber3599d922016-08-09 10:42:57 -0700218 }
219 ;
220
Yifan Hongf24fa852016-09-23 11:03:15 -0700221annotation_string_value
Andreas Huber3599d922016-08-09 10:42:57 -0700222 : STRING_LITERAL
223 {
224 $$ = new std::vector<std::string>;
225 $$->push_back($1);
226 }
227 | '{' annotation_string_values '}' { $$ = $2; }
228 ;
229
230annotation_string_values
231 : STRING_LITERAL
232 {
233 $$ = new std::vector<std::string>;
234 $$->push_back($1);
235 }
236 | annotation_string_values ',' STRING_LITERAL
237 {
238 $$ = $1;
239 $$->push_back($3);
240 }
241 ;
242
Yifan Hongf24fa852016-09-23 11:03:15 -0700243annotation_const_expr_value
244 : const_expr
245 {
246 $$ = new std::vector<ConstantExpression *>;
247 $$->push_back($1);
248 }
249 | '{' annotation_const_expr_values '}' { $$ = $2; }
250 ;
251
252annotation_const_expr_values
253 : const_expr
254 {
255 $$ = new std::vector<ConstantExpression *>;
256 $$->push_back($1);
257 }
258 | annotation_const_expr_values ',' const_expr
259 {
260 $$ = $1;
261 $$->push_back($3);
262 }
263 ;
264
Yifan Hongbe627b32016-10-28 18:38:56 -0700265error_stmt
266 : error ';'
267 {
268 $$ = $1;
269 ast->addSyntaxError();
270 // std::cerr << "WARNING: skipping errors until " << @2 << ".\n";
271 }
272 ;
273
274opt_error_stmt
275 : /* empty */ { $$ = NULL; }
276 | error_stmt { $$ = $1; }
277 ;
278
279require_semicolon
280 : ';'
281 | /* empty */
282 {
283 std::cerr << "ERROR: missing ; at " << @$ << "\n";
284 ast->addSyntaxError();
285 }
286 ;
287
Andreas Huberc9410c72016-07-28 12:18:40 -0700288program
Yifan Hongbe627b32016-10-28 18:38:56 -0700289 : opt_error_stmt
290 package
291 imports
292 body
Andreas Huber84f89de2016-07-28 15:39:51 -0700293 ;
294
295fqname
296 : FQNAME
297 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700298 $$ = new FQName($1);
299 if(!$$->isValid()) {
300 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700301 << @1
Yifan Hongae16eed2016-09-23 13:25:25 -0700302 << ".\n";
Andreas Huber84f89de2016-07-28 15:39:51 -0700303 YYERROR;
304 }
305 }
306 | IDENTIFIER
307 {
Yifan Hongae16eed2016-09-23 13:25:25 -0700308 $$ = new FQName($1);
309 if(!$$->isValid()) {
310 std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
311 << @1
312 << ".\n";
313 YYERROR;
314 }
315 }
316 ;
317
318fqtype
319 : fqname
320 {
321 $$ = ast->lookupType(*($1));
Andreas Huber84f89de2016-07-28 15:39:51 -0700322 if ($$ == NULL) {
Yifan Hongae16eed2016-09-23 13:25:25 -0700323 std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at "
324 << @1
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700325 << "\n";
326
Andreas Huber84f89de2016-07-28 15:39:51 -0700327 YYERROR;
328 }
329 }
Hridya Valsarajucd91bf62016-10-25 12:41:04 -0700330 | TYPE
Andreas Huber84f89de2016-07-28 15:39:51 -0700331 ;
Andreas Huberc9410c72016-07-28 12:18:40 -0700332
333package
Yifan Hongbe627b32016-10-28 18:38:56 -0700334 : PACKAGE FQNAME require_semicolon
Andreas Hubereb1081f2016-07-28 13:13:24 -0700335 {
Andreas Huber84f89de2016-07-28 15:39:51 -0700336 if (!ast->setPackage($2)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700337 std::cerr << "ERROR: Malformed package identifier '"
338 << $2
339 << "' at "
340 << @2
341 << "\n";
342
Andreas Huber84f89de2016-07-28 15:39:51 -0700343 YYERROR;
344 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700345 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700346
Yifan Hongbe627b32016-10-28 18:38:56 -0700347import_stmt
348 : IMPORT FQNAME require_semicolon
349 {
350 if (!ast->addImport($2)) {
351 std::cerr << "ERROR: Unable to import '" << $2 << "' at " << @2
352 << "\n";
353 ast->addSyntaxError();
354 }
355 }
356 | IMPORT IDENTIFIER require_semicolon
357 {
358 if (!ast->addImport($2)) {
359 std::cerr << "ERROR: Unable to import '" << $2 << "' at " << @2
360 << "\n";
361 ast->addSyntaxError();
362 }
363 }
364 | IMPORT error_stmt
365 ;
366
367
Andreas Huberc9410c72016-07-28 12:18:40 -0700368imports
369 : /* empty */
Yifan Hongbe627b32016-10-28 18:38:56 -0700370 | imports import_stmt
Andreas Huberc9410c72016-07-28 12:18:40 -0700371 ;
372
373opt_extends
374 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700375 | EXTENDS fqtype { $$ = $2; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700376
377body
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700378 : type_declarations
Andreas Huberc9410c72016-07-28 12:18:40 -0700379 ;
380
381interface_declarations
382 : /* empty */
383 | interface_declarations type_declaration
384 | interface_declarations method_declaration
385 {
Yifan Hongbe627b32016-10-28 18:38:56 -0700386 if ($2 != nullptr) {
387 if (!ast->scope()->isInterface()) {
388 std::cerr << "ERROR: unknown error in interface declaration at "
389 << @2 << "\n";
390 YYERROR;
391 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700392
Yifan Hongbe627b32016-10-28 18:38:56 -0700393 Interface *iface = static_cast<Interface *>(ast->scope());
394 if (!iface->addMethod($2)) {
395 std::cerr << "ERROR: Unable to add method '" << $2->name()
396 << "' at " << @2 << "\n";
397
398 YYERROR;
399 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700400 }
Yifan Hongbe627b32016-10-28 18:38:56 -0700401 // ignore if $2 is nullptr (from error recovery)
Andreas Huberc9410c72016-07-28 12:18:40 -0700402 }
403 ;
404
405type_declarations
Andreas Hubera2723d22016-07-29 15:36:07 -0700406 : /* empty */
Yifan Hongbe627b32016-10-28 18:38:56 -0700407 | error_stmt
Andreas Huberc9410c72016-07-28 12:18:40 -0700408 | type_declarations type_declaration
409 ;
410
411type_declaration
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700412 : opt_annotations type_declaration_body
413 {
414 if ($2 != nullptr) {
415 $2->setAnnotations($1);
416 } else if (!$1->empty()) {
417 // Since typedefs are always resolved to their target it makes
418 // little sense to annotate them and have their annotations
419 // impose semantics other than their target type.
420 std::cerr << "ERROR: typedefs cannot be annotated. at " << @2
421 << "\n";
422
423 YYERROR;
424 }
425 }
426 ;
427
428type_declaration_body
Yifan Hongbe627b32016-10-28 18:38:56 -0700429 : named_struct_or_union_declaration require_semicolon
430 | named_enum_declaration require_semicolon
431 | typedef_declaration require_semicolon
432 | interface_declaration require_semicolon
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700433 ;
434
435interface_declaration
436 : INTERFACE IDENTIFIER opt_extends
437 {
438 if ($3 != NULL && !$3->isInterface()) {
Yifan Hongbe627b32016-10-28 18:38:56 -0700439 std::cerr << "ERROR: You can only extend interfaces. at " << @3
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700440 << "\n";
441
442 YYERROR;
443 }
444
445 if ($2[0] != 'I') {
446 std::cerr << "ERROR: All interface names must start with an 'I' "
447 << "prefix. at " << @2 << "\n";
448
449 YYERROR;
450 }
451
Yifan Honga4b53d02016-10-31 17:29:10 -0700452 Interface *iface = new Interface($2, convertYYLoc(@2), static_cast<Interface *>($3));
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700453
454 // Register interface immediately so it can be referenced inside
455 // definition.
456 std::string errorMsg;
457 if (!ast->addScopedType(iface, &errorMsg)) {
458 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
459 YYERROR;
460 }
461
462 ast->enterScope(iface);
463 }
464 '{' interface_declarations '}'
465 {
Yifan Hongbe627b32016-10-28 18:38:56 -0700466 if (!ast->scope()->isInterface()) {
467 std::cerr << "ERROR: unknown error in interface declaration at "
468 << @5 << "\n";
469 YYERROR;
470 }
471
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700472 Interface *iface = static_cast<Interface *>(ast->scope());
473
474 ast->leaveScope();
475
476 $$ = iface;
477 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700478 ;
479
480typedef_declaration
481 : TYPEDEF type IDENTIFIER
482 {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700483 std::string errorMsg;
Yifan Honga4b53d02016-10-31 17:29:10 -0700484 if (!ast->addTypeDef($3, $2, convertYYLoc(@3), &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700485 std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700486 YYERROR;
487 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700488
489 $$ = nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700490 }
491 ;
492
Yifan Hong52165692016-08-12 18:06:40 -0700493const_expr
Yifan Hongf24fa852016-09-23 11:03:15 -0700494 : INTEGER { $$ = new ConstantExpression($1); }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700495 | fqname
496 {
Yifan Hongf24fa852016-09-23 11:03:15 -0700497 if(!$1->isValidValueName()) {
498 std::cerr << "ERROR: '" << $1->string()
499 << "' does not refer to an enum value at "
500 << @1 << ".\n";
501 YYERROR;
502 }
503 if($1->isIdentifier()) {
504 std::string identifier = $1->name();
505 LocalIdentifier *iden = ast->scope()->lookupIdentifier(identifier);
506 if(!iden) {
Yifan Hongbe627b32016-10-28 18:38:56 -0700507 std::cerr << "ERROR: identifier " << $1->string()
508 << " could not be found at " << @1 << ".\n";
Yifan Hongf24fa852016-09-23 11:03:15 -0700509 YYERROR;
510 }
511 if(!iden->isEnumValue()) {
Yifan Hongbe627b32016-10-28 18:38:56 -0700512 std::cerr << "ERROR: identifier " << $1->string()
513 << " is not an enum value at " << @1 << ".\n";
Yifan Hongf24fa852016-09-23 11:03:15 -0700514 YYERROR;
515 }
516 $$ = new ConstantExpression(
517 *(static_cast<EnumValue *>(iden)->constExpr()), $1->string());
518 } else {
519 std::string errorMsg;
520 EnumValue *v = ast->lookupEnumValue(*($1), &errorMsg);
521 if(v == nullptr) {
522 std::cerr << "ERROR: " << errorMsg << " at " << @1 << ".\n";
523 YYERROR;
524 }
525 $$ = new ConstantExpression(*(v->constExpr()), $1->string());
526 }
Yifan Hongb44a6c82016-09-22 15:50:18 -0700527 }
Yifan Hong52165692016-08-12 18:06:40 -0700528 | const_expr '?' const_expr ':' const_expr
529 {
Yifan Hongb44a6c82016-09-22 15:50:18 -0700530 $$ = new ConstantExpression($1, $3, $5);
Yifan Hong52165692016-08-12 18:06:40 -0700531 }
532 | const_expr LOGICAL_OR const_expr { $$ = new ConstantExpression($1, "||", $3); }
533 | const_expr LOGICAL_AND const_expr { $$ = new ConstantExpression($1, "&&", $3); }
534 | const_expr '|' const_expr { $$ = new ConstantExpression($1, "|" , $3); }
535 | const_expr '^' const_expr { $$ = new ConstantExpression($1, "^" , $3); }
536 | const_expr '&' const_expr { $$ = new ConstantExpression($1, "&" , $3); }
537 | const_expr EQUALITY const_expr { $$ = new ConstantExpression($1, "==", $3); }
538 | const_expr NEQ const_expr { $$ = new ConstantExpression($1, "!=", $3); }
539 | const_expr '<' const_expr { $$ = new ConstantExpression($1, "<" , $3); }
540 | const_expr '>' const_expr { $$ = new ConstantExpression($1, ">" , $3); }
541 | const_expr LEQ const_expr { $$ = new ConstantExpression($1, "<=", $3); }
542 | const_expr GEQ const_expr { $$ = new ConstantExpression($1, ">=", $3); }
543 | const_expr LSHIFT const_expr { $$ = new ConstantExpression($1, "<<", $3); }
544 | const_expr RSHIFT const_expr { $$ = new ConstantExpression($1, ">>", $3); }
545 | const_expr '+' const_expr { $$ = new ConstantExpression($1, "+" , $3); }
546 | const_expr '-' const_expr { $$ = new ConstantExpression($1, "-" , $3); }
547 | const_expr '*' const_expr { $$ = new ConstantExpression($1, "*" , $3); }
548 | const_expr '/' const_expr { $$ = new ConstantExpression($1, "/" , $3); }
549 | const_expr '%' const_expr { $$ = new ConstantExpression($1, "%" , $3); }
550 | '+' const_expr %prec UNARY_PLUS { $$ = new ConstantExpression("+", $2); }
551 | '-' const_expr %prec UNARY_MINUS { $$ = new ConstantExpression("-", $2); }
552 | '!' const_expr { $$ = new ConstantExpression("!", $2); }
553 | '~' const_expr { $$ = new ConstantExpression("~", $2); }
554 | '(' const_expr ')' { $$ = $2; }
Yifan Hongbe627b32016-10-28 18:38:56 -0700555 | '(' error ')'
556 {
557 ast->addSyntaxError();
558 // to avoid segfaults
559 $$ = new ConstantExpression(ConstantExpression::Zero(ScalarType::KIND_INT32));
560 }
Yifan Hong52165692016-08-12 18:06:40 -0700561 ;
562
Andreas Huberc9410c72016-07-28 12:18:40 -0700563method_declaration
Yifan Hongbe627b32016-10-28 18:38:56 -0700564 : error_stmt { $$ = nullptr; }
565 | opt_annotations IDENTIFIER '(' typed_vars ')' require_semicolon
Andreas Huberc9410c72016-07-28 12:18:40 -0700566 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700567 $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1);
568 }
Yifan Hongbe627b32016-10-28 18:38:56 -0700569 | opt_annotations ONEWAY IDENTIFIER '(' typed_vars ')' require_semicolon
Iliyan Malchev639bff82016-08-13 14:24:11 -0700570 {
571 $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700572 }
Yifan Hongbe627b32016-10-28 18:38:56 -0700573 | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' require_semicolon
Andreas Huberc9410c72016-07-28 12:18:40 -0700574 {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700575 $$ = new Method($2, $4, $8, false, $1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700576 }
577 ;
578
579typed_vars
580 : /* empty */
581 {
Andreas Huber881227d2016-08-02 14:20:21 -0700582 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700583 }
584 | typed_var
585 {
Andreas Huber881227d2016-08-02 14:20:21 -0700586 $$ = new std::vector<TypedVar *>;
Andreas Huberc9410c72016-07-28 12:18:40 -0700587 $$->push_back($1);
588 }
589 | typed_vars ',' typed_var
590 {
591 $$ = $1;
592 $$->push_back($3);
593 }
594 ;
595
596typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); }
597 ;
598
599
600struct_or_union_keyword
601 : STRUCT { $$ = CompoundType::STYLE_STRUCT; }
602 | UNION { $$ = CompoundType::STYLE_UNION; }
603 ;
604
605named_struct_or_union_declaration
606 : struct_or_union_keyword IDENTIFIER
607 {
Yifan Honga4b53d02016-10-31 17:29:10 -0700608 CompoundType *container = new CompoundType($1, $2, convertYYLoc(@2));
Andreas Huberc9410c72016-07-28 12:18:40 -0700609 ast->enterScope(container);
610 }
611 struct_or_union_body
612 {
Yifan Hongbe627b32016-10-28 18:38:56 -0700613 if (!ast->scope()->isCompoundType()) {
614 std::cerr << "ERROR: unknown error in struct or union declaration at "
615 << @4 << "\n";
616 YYERROR;
617 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700618 CompoundType *container = static_cast<CompoundType *>(ast->scope());
619
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700620 std::string errorMsg;
621 if (!container->setFields($4, &errorMsg)) {
622 std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700623 YYERROR;
624 }
625
Andreas Huberc9410c72016-07-28 12:18:40 -0700626 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700627
Andreas Huber9ed827c2016-08-22 12:31:13 -0700628 if (!ast->addScopedType(container, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700629 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700630 YYERROR;
631 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700632
633 $$ = container;
Andreas Huberc9410c72016-07-28 12:18:40 -0700634 }
635 ;
636
Andreas Huberc9410c72016-07-28 12:18:40 -0700637struct_or_union_body
638 : '{' field_declarations '}' { $$ = $2; }
639 ;
640
641field_declarations
Andreas Huber881227d2016-08-02 14:20:21 -0700642 : /* empty */ { $$ = new std::vector<CompoundField *>; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700643 | field_declarations field_declaration
644 {
645 $$ = $1;
646
647 if ($2 != NULL) {
648 $$->push_back($2);
649 }
650 }
651 ;
652
653field_declaration
Yifan Hongbe627b32016-10-28 18:38:56 -0700654 : error_stmt { $$ = nullptr; }
655 | type IDENTIFIER require_semicolon { $$ = new CompoundField($2, $1); }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700656 | annotated_compound_declaration ';' { $$ = NULL; }
657 ;
658
659annotated_compound_declaration
660 : opt_annotations compound_declaration
661 {
662 $2->setAnnotations($1);
663 $$ = $2;
664 }
665 ;
666
667compound_declaration
Yifan Honga2855012016-10-11 13:36:54 -0700668 : named_struct_or_union_declaration { $$ = $1; }
Yifan Hong6a2fedf2016-10-11 13:44:07 -0700669 | named_enum_declaration { $$ = $1; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700670 ;
671
672opt_storage_type
673 : /* empty */ { $$ = NULL; }
Yifan Hongae16eed2016-09-23 13:25:25 -0700674 | ':' fqtype
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700675 {
676 $$ = $2;
677
678 if ($$ != NULL && !$$->isValidEnumStorageType()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700679 std::cerr << "ERROR: Invalid enum storage type specified. at "
680 << @2 << "\n";
681
Yifan Hongbe627b32016-10-28 18:38:56 -0700682 YYERROR;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700683 }
684 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700685 ;
686
687opt_comma
688 : /* empty */
689 | ','
690 ;
691
692named_enum_declaration
Yifan Hongf24fa852016-09-23 11:03:15 -0700693 : ENUM IDENTIFIER opt_storage_type
Andreas Huberc9410c72016-07-28 12:18:40 -0700694 {
Yifan Honga4b53d02016-10-31 17:29:10 -0700695 ast->enterScope(new EnumType($2, convertYYLoc(@2), $3));
Yifan Hongf24fa852016-09-23 11:03:15 -0700696 }
697 enum_declaration_body
698 {
Yifan Hongbe627b32016-10-28 18:38:56 -0700699 if (!ast->scope()->isEnum()) {
700 std::cerr << "ERROR: unknown error in enum declaration at "
701 << @5 << "\n";
702 YYERROR;
703 }
704
Yifan Hongf24fa852016-09-23 11:03:15 -0700705 EnumType *enumType = static_cast<EnumType *>(ast->scope());
706 ast->leaveScope();
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700707
708 std::string errorMsg;
Andreas Huber9ed827c2016-08-22 12:31:13 -0700709 if (!ast->addScopedType(enumType, &errorMsg)) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700710 std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n";
Andreas Huber5a545442016-08-03 10:44:56 -0700711 YYERROR;
712 }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700713
714 $$ = enumType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700715 }
716 ;
717
Yifan Hongf24fa852016-09-23 11:03:15 -0700718enum_declaration_body
719 : '{' enum_values opt_comma '}' { $$ = $2; }
720 ;
721
Andreas Huberc9410c72016-07-28 12:18:40 -0700722enum_value
723 : IDENTIFIER { $$ = new EnumValue($1); }
Yifan Hong57886972016-08-17 10:42:15 -0700724 | IDENTIFIER '=' const_expr { $$ = new EnumValue($1, $3); }
Andreas Huberc9410c72016-07-28 12:18:40 -0700725 ;
726
727enum_values
728 : /* empty */
Yifan Hongf24fa852016-09-23 11:03:15 -0700729 { /* do nothing */ }
Andreas Huberc9410c72016-07-28 12:18:40 -0700730 | enum_value
731 {
Yifan Hongbe627b32016-10-28 18:38:56 -0700732 if (!ast->scope()->isEnum()) {
733 std::cerr << "ERROR: unknown error in enum declaration at "
734 << @1 << "\n";
735 YYERROR;
736 }
737
Yifan Hongf24fa852016-09-23 11:03:15 -0700738 static_cast<EnumType *>(ast->scope())->addValue($1);
Andreas Huberc9410c72016-07-28 12:18:40 -0700739 }
740 | enum_values ',' enum_value
741 {
Yifan Hongbe627b32016-10-28 18:38:56 -0700742 if (!ast->scope()->isEnum()) {
743 std::cerr << "ERROR: unknown error in enum declaration at "
744 << @3 << "\n";
745 YYERROR;
746 }
747
Yifan Hongf24fa852016-09-23 11:03:15 -0700748 static_cast<EnumType *>(ast->scope())->addValue($3);
Andreas Huberc9410c72016-07-28 12:18:40 -0700749 }
750 ;
751
Yifan Hongbd33e382016-11-02 13:30:17 -0700752array_type_base
Yifan Hongae16eed2016-09-23 13:25:25 -0700753 : fqtype { $$ = $1; }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700754 | TEMPLATED '<' type '>'
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700755 {
Andreas Huber86a112b2016-10-19 14:25:16 -0700756 if (!$1->isVector() && $3->isBinder()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700757 std::cerr << "ERROR: TemplatedType of interface types are not "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700758 << "supported. at " << @3 << "\n";
Andreas Huber70a59e12016-08-16 12:57:01 -0700759
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700760 YYERROR;
761 }
Yifan Hongbf459bc2016-08-23 16:50:37 -0700762 $1->setElementType($3);
763 $$ = $1;
764 }
765 | TEMPLATED '<' TEMPLATED '<' type RSHIFT
766 {
767 if ($5->isBinder()) {
768 std::cerr << "ERROR: TemplatedType of interface types are not "
769 << "supported. at " << @5 << "\n";
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700770
Yifan Hongbf459bc2016-08-23 16:50:37 -0700771 YYERROR;
772 }
773 $3->setElementType($5);
774 $1->setElementType($3);
775 $$ = $1;
Andreas Huberb95ea8a2016-08-15 15:35:42 -0700776 }
Yifan Hongbd33e382016-11-02 13:30:17 -0700777 ;
778
779array_type
780 : array_type_base '[' const_expr ']'
781 {
782 if ($1->isBinder()) {
783 std::cerr << "ERROR: Arrays of interface types are not supported."
784 << " at " << @1 << "\n";
785
786 YYERROR;
787 }
788 if ($1->isArray()) {
789 $$ = new ArrayType(static_cast<ArrayType *>($1), $3);
790 } else {
791 $$ = new ArrayType($1, $3);
792 }
793 }
794 | array_type '[' const_expr ']'
795 {
796 $$ = $1;
797 $$->appendDimension($3);
798 }
799 ;
800
801type
802 : array_type_base { $$ = $1; }
803 | array_type { $$ = $1; }
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700804 | annotated_compound_declaration { $$ = $1; }
Andreas Huber295ad302016-08-16 11:35:00 -0700805 | INTERFACE { $$ = new GenericBinder; }
Andreas Huberc9410c72016-07-28 12:18:40 -0700806 ;
807
Andreas Huberc9410c72016-07-28 12:18:40 -0700808%%
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700809
810#include <android-base/logging.h>
811
812void yy::parser::error(
813 const yy::parser::location_type &where,
814 const std::string &errstr) {
815 std::cerr << "ERROR: " << errstr << " at " << where << "\n";
816}
817