Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | %{ |
| 2 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 3 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 4 | #include "AST.h" |
| 5 | #include "ArrayType.h" |
| 6 | #include "CompoundType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 7 | #include "ConstantExpression.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 8 | #include "EnumType.h" |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 9 | #include "GenericBinder.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 10 | #include "Interface.h" |
| 11 | #include "Method.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 12 | #include "VectorType.h" |
| 13 | |
| 14 | #include "hidl-gen_y.h" |
| 15 | |
| 16 | #include <stdio.h> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 17 | #include <utils/String8.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 18 | |
| 19 | using namespace android; |
| 20 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 21 | extern int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 22 | |
| 23 | #define scanner ast->scanner() |
| 24 | |
| 25 | %} |
| 26 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 27 | %initial-action { |
| 28 | // Initialize the initial location. |
| 29 | @$.begin.filename = @$.end.filename = |
| 30 | const_cast<std::string *>(&ast->getFilename()); |
| 31 | } |
| 32 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | %parse-param { android::AST *ast } |
| 34 | %lex-param { void *scanner } |
| 35 | %pure-parser |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 36 | %skeleton "glr.cc" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 37 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 38 | %token<str> ENUM |
| 39 | %token<str> EXTENDS |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 40 | %token<str> FQNAME |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | %token<str> GENERATES |
| 42 | %token<str> IDENTIFIER |
| 43 | %token<str> IMPORT |
| 44 | %token<str> INTEGER |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 45 | %token<str> FLOAT |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 46 | %token<str> INTERFACE |
| 47 | %token<str> PACKAGE |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 48 | %token<type> SCALAR |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 49 | %token<str> STRUCT |
| 50 | %token<str> STRING_LITERAL |
| 51 | %token<str> TYPEDEF |
| 52 | %token<str> UNION |
| 53 | %token<str> VEC |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 54 | %token<void> ONEWAY |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 55 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 56 | /* Operator precedence and associativity, as per |
| 57 | * http://en.cppreference.com/w/cpp/language/operator_precedence */ |
| 58 | /* Precedence level 15 ternary operator */ |
| 59 | %right '?' ':' |
| 60 | /* Precedence level 13 - 14, LTR, logical operators*/ |
| 61 | %left LOGICAL_OR |
| 62 | %left LOGICAL_AND |
| 63 | /* Precedence level 10 - 12, LTR, bitwise operators*/ |
| 64 | %left '|' |
| 65 | %left '^' |
| 66 | %left '&' |
| 67 | /* Precedence level 9, LTR */ |
| 68 | %left EQUALITY NEQ |
| 69 | /* Precedence level 8, LTR */ |
| 70 | %left '<' '>' LEQ GEQ |
| 71 | /* Precedence level 7, LTR */ |
| 72 | %left LSHIFT RSHIFT |
| 73 | /* Precedence level 6, LTR */ |
| 74 | %left '+' '-' |
| 75 | /* Precedence level 5, LTR */ |
| 76 | %left '*' '/' '%' |
| 77 | /* Precedence level 3, RTL; but we have to use %left here */ |
| 78 | %left UNARY_MINUS UNARY_PLUS '!' '~' |
| 79 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 80 | %type<str> optIdentifier package |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 81 | %type<type> fqname |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 82 | |
| 83 | %type<type> type opt_storage_type |
| 84 | %type<type> enum_declaration |
| 85 | %type<type> struct_or_union_declaration |
| 86 | %type<type> opt_extends |
| 87 | |
| 88 | %type<field> field_declaration |
| 89 | %type<fields> field_declarations struct_or_union_body |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 90 | %type<constantExpression> const_expr |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 91 | %type<enumValue> enum_value |
| 92 | %type<enumValues> enum_values |
| 93 | %type<typedVars> typed_vars |
| 94 | %type<typedVar> typed_var |
| 95 | %type<method> method_declaration |
| 96 | %type<compoundStyle> struct_or_union_keyword |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 97 | %type<stringVec> annotation_string_values annotation_value |
| 98 | %type<annotationParam> annotation_param |
| 99 | %type<annotationParams> opt_annotation_params annotation_params |
| 100 | %type<annotation> annotation |
| 101 | %type<annotations> opt_annotations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 102 | |
| 103 | %start program |
| 104 | |
| 105 | %union { |
| 106 | const char *str; |
| 107 | android::Type *type; |
| 108 | android::CompoundType *compoundType; |
| 109 | android::CompoundField *field; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 110 | std::vector<android::CompoundField *> *fields; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 111 | android::EnumValue *enumValue; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 112 | android::ConstantExpression *constantExpression; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 113 | std::vector<android::EnumValue *> *enumValues; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 114 | android::TypedVar *typedVar; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 115 | std::vector<android::TypedVar *> *typedVars; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 116 | android::Method *method; |
| 117 | android::CompoundType::Style compoundStyle; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 118 | std::vector<std::string> *stringVec; |
| 119 | std::pair<std::string, std::vector<std::string> *> *annotationParam; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 120 | android::DefaultKeyedVector<std::string, std::vector<std::string> *> *annotationParams; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 121 | android::Annotation *annotation; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 122 | android::DefaultKeyedVector<std::string, android::Annotation *> *annotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | %% |
| 126 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 127 | opt_annotations |
| 128 | : /* empty */ |
| 129 | { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 130 | $$ = new DefaultKeyedVector<std::string, Annotation *>; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 131 | } |
| 132 | | opt_annotations annotation |
| 133 | { |
| 134 | $$ = $1; |
| 135 | $$->add($2->name(), $2); |
| 136 | } |
| 137 | ; |
| 138 | |
| 139 | annotation |
| 140 | : '@' IDENTIFIER opt_annotation_params |
| 141 | { |
| 142 | $$ = new Annotation($2, $3); |
| 143 | } |
| 144 | ; |
| 145 | |
| 146 | opt_annotation_params |
| 147 | : /* empty */ |
| 148 | { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 149 | $$ = new DefaultKeyedVector<std::string, std::vector<std::string> *>; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 150 | } |
| 151 | | '(' annotation_params ')' |
| 152 | { |
| 153 | $$ = $2; |
| 154 | } |
| 155 | ; |
| 156 | |
| 157 | annotation_params |
| 158 | : annotation_param |
| 159 | { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 160 | $$ = new DefaultKeyedVector<std::string, std::vector<std::string> *>; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 161 | $$->add($1->first, $1->second); |
| 162 | } |
| 163 | | annotation_params ',' annotation_param |
| 164 | { |
| 165 | $$ = $1; |
| 166 | $$->add($3->first, $3->second); |
| 167 | } |
| 168 | ; |
| 169 | |
| 170 | annotation_param |
| 171 | : IDENTIFIER '=' annotation_value |
| 172 | { |
| 173 | $$ = new std::pair<std::string, std::vector<std::string> *>($1, $3); |
| 174 | } |
| 175 | ; |
| 176 | |
| 177 | annotation_value |
| 178 | : STRING_LITERAL |
| 179 | { |
| 180 | $$ = new std::vector<std::string>; |
| 181 | $$->push_back($1); |
| 182 | } |
| 183 | | '{' annotation_string_values '}' { $$ = $2; } |
| 184 | ; |
| 185 | |
| 186 | annotation_string_values |
| 187 | : STRING_LITERAL |
| 188 | { |
| 189 | $$ = new std::vector<std::string>; |
| 190 | $$->push_back($1); |
| 191 | } |
| 192 | | annotation_string_values ',' STRING_LITERAL |
| 193 | { |
| 194 | $$ = $1; |
| 195 | $$->push_back($3); |
| 196 | } |
| 197 | ; |
| 198 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 199 | program |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 200 | : package imports body |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 201 | ; |
| 202 | |
| 203 | fqname |
| 204 | : FQNAME |
| 205 | { |
| 206 | $$ = ast->lookupType($1); |
| 207 | if ($$ == NULL) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 208 | std::cerr << "ERROR: Failed to lookup type '" << $1 << "' at " |
| 209 | << @1 |
| 210 | << "\n"; |
| 211 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 212 | YYERROR; |
| 213 | } |
| 214 | } |
| 215 | | IDENTIFIER |
| 216 | { |
| 217 | $$ = ast->lookupType($1); |
| 218 | if ($$ == NULL) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 219 | std::cerr << "Failed to lookup type '" << $1 << "' at " << @1 |
| 220 | << "\n"; |
| 221 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 222 | YYERROR; |
| 223 | } |
| 224 | } |
| 225 | | SCALAR |
| 226 | ; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 227 | |
| 228 | package |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 229 | : PACKAGE FQNAME ';' |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 230 | { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 231 | if (!ast->setPackage($2)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 232 | std::cerr << "ERROR: Malformed package identifier '" |
| 233 | << $2 |
| 234 | << "' at " |
| 235 | << @2 |
| 236 | << "\n"; |
| 237 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 238 | YYERROR; |
| 239 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 240 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 241 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 242 | imports |
| 243 | : /* empty */ |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 244 | | imports IMPORT FQNAME ';' |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 245 | { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 246 | if (!ast->addImport($3)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 247 | std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3 |
| 248 | << "\n"; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 249 | |
| 250 | YYERROR; |
| 251 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 252 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 253 | | imports IMPORT IDENTIFIER ';' |
| 254 | { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 255 | if (!ast->addImport($3)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 256 | std::cerr << "ERROR: Unable to import '" << $3 << "' at " << @3 |
| 257 | << "\n"; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 258 | |
| 259 | YYERROR; |
| 260 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 261 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 262 | ; |
| 263 | |
| 264 | opt_extends |
| 265 | : /* empty */ { $$ = NULL; } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 266 | | EXTENDS fqname { $$ = $2; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 267 | |
| 268 | body |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 269 | : opt_annotations INTERFACE IDENTIFIER opt_extends |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 270 | { |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 271 | if ($4 != NULL && !$4->isInterface()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 272 | std::cerr << "ERROR: You can only extend interfaces. at" << @4 |
| 273 | << "\n"; |
| 274 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 275 | YYERROR; |
| 276 | } |
| 277 | |
Andreas Huber | 991b864 | 2016-08-15 16:40:44 -0700 | [diff] [blame] | 278 | if ($3[0] != 'I') { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 279 | std::cerr << "ERROR: All interface names must start with an 'I' " |
| 280 | << "prefix. at " << @3 << "\n"; |
Andreas Huber | 991b864 | 2016-08-15 16:40:44 -0700 | [diff] [blame] | 281 | |
| 282 | YYERROR; |
| 283 | } |
| 284 | |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 285 | Interface *iface = new Interface(static_cast<Interface *>($4), $1); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 286 | |
| 287 | // Register interface immediately so it can be referenced inside |
| 288 | // definition. |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 289 | std::string errorMsg; |
| 290 | if (!ast->addScopedType($3, iface, &errorMsg)) { |
| 291 | std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 292 | YYERROR; |
| 293 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 294 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 295 | ast->enterScope(iface); |
| 296 | } |
| 297 | '{' interface_declarations '}' ';' |
| 298 | { |
| 299 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 300 | |
| 301 | ast->leaveScope(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 302 | } |
| 303 | | type_declarations |
| 304 | ; |
| 305 | |
| 306 | interface_declarations |
| 307 | : /* empty */ |
| 308 | | interface_declarations type_declaration |
| 309 | | interface_declarations method_declaration |
| 310 | { |
| 311 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 312 | iface->addMethod($2); |
| 313 | } |
| 314 | ; |
| 315 | |
| 316 | type_declarations |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 317 | : /* empty */ |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 318 | | type_declarations type_declaration |
| 319 | ; |
| 320 | |
| 321 | type_declaration |
| 322 | : named_struct_or_union_declaration ';' |
| 323 | | named_enum_declaration ';' |
| 324 | | typedef_declaration ';' |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 325 | ; |
| 326 | |
| 327 | typedef_declaration |
| 328 | : TYPEDEF type IDENTIFIER |
| 329 | { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 330 | std::string errorMsg; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame^] | 331 | if (!ast->addTypeDef($3, $2, &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 332 | std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 333 | YYERROR; |
| 334 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 335 | } |
| 336 | ; |
| 337 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 338 | const_expr |
| 339 | : INTEGER { $$ = new ConstantExpression($1); } |
| 340 | | IDENTIFIER { $$ = new ConstantExpression($1); } |
| 341 | | const_expr '?' const_expr ':' const_expr |
| 342 | { |
| 343 | $$ = new ConstantExpression($1, $3, $5); |
| 344 | } |
| 345 | | const_expr LOGICAL_OR const_expr { $$ = new ConstantExpression($1, "||", $3); } |
| 346 | | const_expr LOGICAL_AND const_expr { $$ = new ConstantExpression($1, "&&", $3); } |
| 347 | | const_expr '|' const_expr { $$ = new ConstantExpression($1, "|" , $3); } |
| 348 | | const_expr '^' const_expr { $$ = new ConstantExpression($1, "^" , $3); } |
| 349 | | const_expr '&' const_expr { $$ = new ConstantExpression($1, "&" , $3); } |
| 350 | | const_expr EQUALITY const_expr { $$ = new ConstantExpression($1, "==", $3); } |
| 351 | | const_expr NEQ const_expr { $$ = new ConstantExpression($1, "!=", $3); } |
| 352 | | const_expr '<' const_expr { $$ = new ConstantExpression($1, "<" , $3); } |
| 353 | | const_expr '>' const_expr { $$ = new ConstantExpression($1, ">" , $3); } |
| 354 | | const_expr LEQ const_expr { $$ = new ConstantExpression($1, "<=", $3); } |
| 355 | | const_expr GEQ const_expr { $$ = new ConstantExpression($1, ">=", $3); } |
| 356 | | const_expr LSHIFT const_expr { $$ = new ConstantExpression($1, "<<", $3); } |
| 357 | | const_expr RSHIFT const_expr { $$ = new ConstantExpression($1, ">>", $3); } |
| 358 | | const_expr '+' const_expr { $$ = new ConstantExpression($1, "+" , $3); } |
| 359 | | const_expr '-' const_expr { $$ = new ConstantExpression($1, "-" , $3); } |
| 360 | | const_expr '*' const_expr { $$ = new ConstantExpression($1, "*" , $3); } |
| 361 | | const_expr '/' const_expr { $$ = new ConstantExpression($1, "/" , $3); } |
| 362 | | const_expr '%' const_expr { $$ = new ConstantExpression($1, "%" , $3); } |
| 363 | | '+' const_expr %prec UNARY_PLUS { $$ = new ConstantExpression("+", $2); } |
| 364 | | '-' const_expr %prec UNARY_MINUS { $$ = new ConstantExpression("-", $2); } |
| 365 | | '!' const_expr { $$ = new ConstantExpression("!", $2); } |
| 366 | | '~' const_expr { $$ = new ConstantExpression("~", $2); } |
| 367 | | '(' const_expr ')' { $$ = $2; } |
| 368 | ; |
| 369 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 370 | method_declaration |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 371 | : opt_annotations IDENTIFIER '(' typed_vars ')' ';' |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 372 | { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 373 | $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1); |
| 374 | } |
| 375 | | opt_annotations ONEWAY IDENTIFIER '(' typed_vars ')' ';' |
| 376 | { |
| 377 | $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 378 | } |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 379 | | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' ';' |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 380 | { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 381 | $$ = new Method($2, $4, $8, false, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 382 | } |
| 383 | ; |
| 384 | |
| 385 | typed_vars |
| 386 | : /* empty */ |
| 387 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 388 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 389 | } |
| 390 | | typed_var |
| 391 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 392 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 393 | $$->push_back($1); |
| 394 | } |
| 395 | | typed_vars ',' typed_var |
| 396 | { |
| 397 | $$ = $1; |
| 398 | $$->push_back($3); |
| 399 | } |
| 400 | ; |
| 401 | |
| 402 | typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); } |
| 403 | ; |
| 404 | |
| 405 | |
| 406 | struct_or_union_keyword |
| 407 | : STRUCT { $$ = CompoundType::STYLE_STRUCT; } |
| 408 | | UNION { $$ = CompoundType::STYLE_UNION; } |
| 409 | ; |
| 410 | |
| 411 | named_struct_or_union_declaration |
| 412 | : struct_or_union_keyword IDENTIFIER |
| 413 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 414 | CompoundType *container = new CompoundType($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 415 | ast->enterScope(container); |
| 416 | } |
| 417 | struct_or_union_body |
| 418 | { |
| 419 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 420 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 421 | std::string errorMsg; |
| 422 | if (!container->setFields($4, &errorMsg)) { |
| 423 | std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 424 | YYERROR; |
| 425 | } |
| 426 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 427 | ast->leaveScope(); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 428 | |
| 429 | if (!ast->addScopedType($2, container, &errorMsg)) { |
| 430 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 431 | YYERROR; |
| 432 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 433 | } |
| 434 | ; |
| 435 | |
| 436 | struct_or_union_declaration |
| 437 | : struct_or_union_keyword optIdentifier |
| 438 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 439 | CompoundType *container = new CompoundType($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 440 | ast->enterScope(container); |
| 441 | } |
| 442 | struct_or_union_body |
| 443 | { |
| 444 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 445 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 446 | std::string errorMsg; |
| 447 | if (!container->setFields($4, &errorMsg)) { |
| 448 | std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 449 | YYERROR; |
| 450 | } |
| 451 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 452 | ast->leaveScope(); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 453 | |
| 454 | if (!ast->addScopedType($2, container, &errorMsg)) { |
| 455 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 456 | YYERROR; |
| 457 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 458 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 459 | $$ = container->ref(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 460 | } |
| 461 | ; |
| 462 | |
| 463 | struct_or_union_body |
| 464 | : '{' field_declarations '}' { $$ = $2; } |
| 465 | ; |
| 466 | |
| 467 | field_declarations |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 468 | : /* empty */ { $$ = new std::vector<CompoundField *>; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 469 | | field_declarations field_declaration |
| 470 | { |
| 471 | $$ = $1; |
| 472 | |
| 473 | if ($2 != NULL) { |
| 474 | $$->push_back($2); |
| 475 | } |
| 476 | } |
| 477 | ; |
| 478 | |
| 479 | field_declaration |
| 480 | : type IDENTIFIER ';' { $$ = new CompoundField($2, $1); } |
| 481 | | struct_or_union_declaration ';' { $$ = NULL; } |
| 482 | | enum_declaration ';' { $$ = NULL; } |
| 483 | ; |
| 484 | |
| 485 | opt_storage_type |
| 486 | : /* empty */ { $$ = NULL; } |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 487 | | ':' fqname |
| 488 | { |
| 489 | $$ = $2; |
| 490 | |
| 491 | if ($$ != NULL && !$$->isValidEnumStorageType()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 492 | std::cerr << "ERROR: Invalid enum storage type specified. at " |
| 493 | << @2 << "\n"; |
| 494 | |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 495 | YYABORT; |
| 496 | } |
| 497 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 498 | ; |
| 499 | |
| 500 | opt_comma |
| 501 | : /* empty */ |
| 502 | | ',' |
| 503 | ; |
| 504 | |
| 505 | named_enum_declaration |
| 506 | : ENUM IDENTIFIER opt_storage_type '{' enum_values opt_comma '}' |
| 507 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 508 | EnumType *enumType = new EnumType($5, $3); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 509 | |
| 510 | std::string errorMsg; |
| 511 | if (!ast->addScopedType($2, enumType, &errorMsg)) { |
| 512 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 513 | YYERROR; |
| 514 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 515 | } |
| 516 | ; |
| 517 | |
| 518 | enum_declaration |
| 519 | : ENUM '{' enum_values opt_comma '}' |
| 520 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 521 | EnumType *enumType = new EnumType($3); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 522 | |
| 523 | std::string errorMsg; |
| 524 | if (!ast->addScopedType(NULL /* localName */, enumType, &errorMsg)) { |
| 525 | // This should never fail. |
| 526 | std::cerr << "ERROR: " << errorMsg << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 527 | YYERROR; |
| 528 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 529 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 530 | $$ = enumType->ref(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 531 | } |
| 532 | | ENUM IDENTIFIER opt_storage_type '{' enum_values opt_comma '}' |
| 533 | { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 534 | EnumType *enumType = new EnumType($5, $3); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 535 | |
| 536 | std::string errorMsg; |
| 537 | if (!ast->addScopedType($2, enumType, &errorMsg)) { |
| 538 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 539 | YYERROR; |
| 540 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 541 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 542 | $$ = enumType->ref(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 543 | } |
| 544 | ; |
| 545 | |
| 546 | enum_value |
| 547 | : IDENTIFIER { $$ = new EnumValue($1); } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 548 | | IDENTIFIER '=' const_expr { $$ = new EnumValue($1, $3->value()); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 549 | ; |
| 550 | |
| 551 | enum_values |
| 552 | : /* empty */ |
| 553 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 554 | $$ = new std::vector<EnumValue *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 555 | } |
| 556 | | enum_value |
| 557 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 558 | $$ = new std::vector<EnumValue *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 559 | $$->push_back($1); |
| 560 | } |
| 561 | | enum_values ',' enum_value |
| 562 | { |
| 563 | $$ = $1; |
| 564 | $$->push_back($3); |
| 565 | } |
| 566 | ; |
| 567 | |
| 568 | type |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 569 | : fqname { $$ = $1; } |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 570 | | fqname '[' INTEGER ']' |
| 571 | { |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 572 | if ($1->isBinder()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 573 | std::cerr << "ERROR: Arrays of interface types are not supported." |
| 574 | << " at " << @1 << "\n"; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 575 | |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 576 | YYERROR; |
| 577 | } |
| 578 | |
| 579 | $$ = new ArrayType($1, $3); |
| 580 | } |
| 581 | | VEC '<' fqname '>' |
| 582 | { |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 583 | if ($3->isBinder()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 584 | std::cerr << "ERROR: Vectors of interface types are not " |
| 585 | << "supported. at " << @3 << "\n"; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 586 | |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 587 | YYERROR; |
| 588 | } |
| 589 | |
| 590 | $$ = new VectorType($3); |
| 591 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 592 | | struct_or_union_declaration { $$ = $1; } |
| 593 | | enum_declaration { $$ = $1; } |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 594 | | INTERFACE { $$ = new GenericBinder; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 595 | ; |
| 596 | |
| 597 | optIdentifier |
| 598 | : /* empty */ { $$ = NULL; } |
| 599 | | IDENTIFIER { $$ = $1; } |
| 600 | ; |
| 601 | |
| 602 | %% |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 603 | |
| 604 | #include <android-base/logging.h> |
| 605 | |
| 606 | void yy::parser::error( |
| 607 | const yy::parser::location_type &where, |
| 608 | const std::string &errstr) { |
| 609 | std::cerr << "ERROR: " << errstr << " at " << where << "\n"; |
| 610 | } |
| 611 | |