Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | %{ |
| 18 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 19 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 20 | #include "AST.h" |
| 21 | #include "ArrayType.h" |
| 22 | #include "CompoundType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 23 | #include "ConstantExpression.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 24 | #include "EnumType.h" |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 25 | #include "FQName.h" |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 26 | #include "GenericBinder.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | #include "Interface.h" |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 28 | #include "Location.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 29 | #include "Method.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | #include "VectorType.h" |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 31 | #include "RefType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | |
| 33 | #include "hidl-gen_y.h" |
| 34 | |
Andreas Huber | 709b62d | 2016-09-19 11:21:18 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | |
| 38 | using namespace android; |
| 39 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 40 | extern int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | |
| 42 | #define scanner ast->scanner() |
| 43 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 44 | ::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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 52 | %} |
| 53 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 54 | %initial-action { |
| 55 | // Initialize the initial location. |
| 56 | @$.begin.filename = @$.end.filename = |
| 57 | const_cast<std::string *>(&ast->getFilename()); |
| 58 | } |
| 59 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 60 | %parse-param { android::AST *ast } |
| 61 | %lex-param { void *scanner } |
| 62 | %pure-parser |
Steven Moreland | 4ab9b79 | 2016-09-26 14:14:07 -0700 | [diff] [blame] | 63 | %glr-parser |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 64 | %skeleton "glr.cc" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 65 | |
Steven Moreland | 4ab9b79 | 2016-09-26 14:14:07 -0700 | [diff] [blame] | 66 | %expect-rr 0 |
| 67 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 68 | %token<str> ENUM |
| 69 | %token<str> EXTENDS |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 70 | %token<str> FQNAME |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 71 | %token<str> GENERATES |
| 72 | %token<str> IDENTIFIER |
| 73 | %token<str> IMPORT |
| 74 | %token<str> INTEGER |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 75 | %token<str> FLOAT |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 76 | %token<str> INTERFACE |
| 77 | %token<str> PACKAGE |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 78 | %token<type> TYPE |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 79 | %token<str> STRUCT |
| 80 | %token<str> STRING_LITERAL |
| 81 | %token<str> TYPEDEF |
| 82 | %token<str> UNION |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 83 | %token<templatedType> TEMPLATED |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 84 | %token<void> ONEWAY |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 85 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 86 | /* 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 Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 110 | %type<str> error_stmt opt_error_stmt error |
Yifan Hong | 6a2fedf | 2016-10-11 13:44:07 -0700 | [diff] [blame] | 111 | %type<str> package |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 112 | %type<fqName> fqname |
| 113 | %type<type> fqtype |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 114 | |
| 115 | %type<type> type opt_storage_type |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 116 | %type<type> array_type_base |
| 117 | %type<arrayType> array_type |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 118 | %type<type> opt_extends |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 119 | %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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 122 | |
| 123 | %type<field> field_declaration |
| 124 | %type<fields> field_declarations struct_or_union_body |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 125 | %type<constantExpression> const_expr |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 126 | %type<enumValue> enum_value |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 127 | %type<enumValues> enum_values enum_declaration_body |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 128 | %type<typedVars> typed_vars |
| 129 | %type<typedVar> typed_var |
| 130 | %type<method> method_declaration |
| 131 | %type<compoundStyle> struct_or_union_keyword |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 132 | %type<stringVec> annotation_string_values annotation_string_value |
| 133 | %type<constExprVec> annotation_const_expr_values annotation_const_expr_value |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 134 | %type<annotationParam> annotation_param |
| 135 | %type<annotationParams> opt_annotation_params annotation_params |
| 136 | %type<annotation> annotation |
| 137 | %type<annotations> opt_annotations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 138 | |
| 139 | %start program |
| 140 | |
| 141 | %union { |
| 142 | const char *str; |
| 143 | android::Type *type; |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 144 | android::ArrayType *arrayType; |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 145 | android::TemplatedType *templatedType; |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 146 | android::FQName *fqName; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 147 | android::CompoundType *compoundType; |
| 148 | android::CompoundField *field; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 149 | std::vector<android::CompoundField *> *fields; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 150 | android::EnumValue *enumValue; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 151 | android::ConstantExpression *constantExpression; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 152 | std::vector<android::EnumValue *> *enumValues; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 153 | android::TypedVar *typedVar; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 154 | std::vector<android::TypedVar *> *typedVars; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 155 | android::Method *method; |
| 156 | android::CompoundType::Style compoundStyle; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 157 | std::vector<std::string> *stringVec; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 158 | std::vector<android::ConstantExpression *> *constExprVec; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 159 | android::AnnotationParam *annotationParam; |
| 160 | android::AnnotationParamVector *annotationParams; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 161 | android::Annotation *annotation; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 162 | std::vector<android::Annotation *> *annotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | %% |
| 166 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 167 | opt_annotations |
| 168 | : /* empty */ |
| 169 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 170 | $$ = new std::vector<Annotation *>; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 171 | } |
| 172 | | opt_annotations annotation |
| 173 | { |
| 174 | $$ = $1; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 175 | $$->push_back($2); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 176 | } |
| 177 | ; |
| 178 | |
| 179 | annotation |
| 180 | : '@' IDENTIFIER opt_annotation_params |
| 181 | { |
| 182 | $$ = new Annotation($2, $3); |
| 183 | } |
| 184 | ; |
| 185 | |
| 186 | opt_annotation_params |
| 187 | : /* empty */ |
| 188 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 189 | $$ = new AnnotationParamVector; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 190 | } |
| 191 | | '(' annotation_params ')' |
| 192 | { |
| 193 | $$ = $2; |
| 194 | } |
| 195 | ; |
| 196 | |
| 197 | annotation_params |
| 198 | : annotation_param |
| 199 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 200 | $$ = new AnnotationParamVector; |
| 201 | $$->push_back($1); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 202 | } |
| 203 | | annotation_params ',' annotation_param |
| 204 | { |
| 205 | $$ = $1; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 206 | $$->push_back($3); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 207 | } |
| 208 | ; |
| 209 | |
| 210 | annotation_param |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 211 | : IDENTIFIER '=' annotation_string_value |
| 212 | { |
| 213 | $$ = new AnnotationParam($1, $3); |
| 214 | } |
| 215 | | IDENTIFIER '=' annotation_const_expr_value |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 216 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 217 | $$ = new AnnotationParam($1, $3); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 218 | } |
| 219 | ; |
| 220 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 221 | annotation_string_value |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 222 | : STRING_LITERAL |
| 223 | { |
| 224 | $$ = new std::vector<std::string>; |
| 225 | $$->push_back($1); |
| 226 | } |
| 227 | | '{' annotation_string_values '}' { $$ = $2; } |
| 228 | ; |
| 229 | |
| 230 | annotation_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 Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 243 | annotation_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 | |
| 252 | annotation_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 Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 265 | error_stmt |
| 266 | : error ';' |
| 267 | { |
| 268 | $$ = $1; |
| 269 | ast->addSyntaxError(); |
| 270 | // std::cerr << "WARNING: skipping errors until " << @2 << ".\n"; |
| 271 | } |
| 272 | ; |
| 273 | |
| 274 | opt_error_stmt |
| 275 | : /* empty */ { $$ = NULL; } |
| 276 | | error_stmt { $$ = $1; } |
| 277 | ; |
| 278 | |
| 279 | require_semicolon |
| 280 | : ';' |
| 281 | | /* empty */ |
| 282 | { |
| 283 | std::cerr << "ERROR: missing ; at " << @$ << "\n"; |
| 284 | ast->addSyntaxError(); |
| 285 | } |
| 286 | ; |
| 287 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 288 | program |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 289 | : opt_error_stmt |
| 290 | package |
| 291 | imports |
| 292 | body |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 293 | ; |
| 294 | |
| 295 | fqname |
| 296 | : FQNAME |
| 297 | { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 298 | $$ = new FQName($1); |
| 299 | if(!$$->isValid()) { |
| 300 | std::cerr << "ERROR: FQName '" << $1 << "' is not valid at " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 301 | << @1 |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 302 | << ".\n"; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 303 | YYERROR; |
| 304 | } |
| 305 | } |
| 306 | | IDENTIFIER |
| 307 | { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 308 | $$ = 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 | |
| 318 | fqtype |
| 319 | : fqname |
| 320 | { |
| 321 | $$ = ast->lookupType(*($1)); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 322 | if ($$ == NULL) { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 323 | std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at " |
| 324 | << @1 |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 325 | << "\n"; |
| 326 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 327 | YYERROR; |
| 328 | } |
| 329 | } |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 330 | | TYPE |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 331 | ; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 332 | |
| 333 | package |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 334 | : PACKAGE FQNAME require_semicolon |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 335 | { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 336 | if (!ast->setPackage($2)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 337 | std::cerr << "ERROR: Malformed package identifier '" |
| 338 | << $2 |
| 339 | << "' at " |
| 340 | << @2 |
| 341 | << "\n"; |
| 342 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 343 | YYERROR; |
| 344 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 345 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 346 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 347 | import_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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 368 | imports |
| 369 | : /* empty */ |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 370 | | imports import_stmt |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 371 | ; |
| 372 | |
| 373 | opt_extends |
| 374 | : /* empty */ { $$ = NULL; } |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 375 | | EXTENDS fqtype { $$ = $2; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 376 | |
| 377 | body |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 378 | : type_declarations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 379 | ; |
| 380 | |
| 381 | interface_declarations |
| 382 | : /* empty */ |
| 383 | | interface_declarations type_declaration |
| 384 | | interface_declarations method_declaration |
| 385 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 386 | 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 Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 392 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 393 | 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 Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 400 | } |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 401 | // ignore if $2 is nullptr (from error recovery) |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 402 | } |
| 403 | ; |
| 404 | |
| 405 | type_declarations |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 406 | : /* empty */ |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 407 | | error_stmt |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 408 | | type_declarations type_declaration |
| 409 | ; |
| 410 | |
| 411 | type_declaration |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 412 | : 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 | |
| 428 | type_declaration_body |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 429 | : named_struct_or_union_declaration require_semicolon |
| 430 | | named_enum_declaration require_semicolon |
| 431 | | typedef_declaration require_semicolon |
| 432 | | interface_declaration require_semicolon |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 433 | ; |
| 434 | |
| 435 | interface_declaration |
| 436 | : INTERFACE IDENTIFIER opt_extends |
| 437 | { |
| 438 | if ($3 != NULL && !$3->isInterface()) { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 439 | std::cerr << "ERROR: You can only extend interfaces. at " << @3 |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 440 | << "\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 Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 452 | Interface *iface = new Interface($2, convertYYLoc(@2), static_cast<Interface *>($3)); |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 453 | |
| 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 Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 466 | if (!ast->scope()->isInterface()) { |
| 467 | std::cerr << "ERROR: unknown error in interface declaration at " |
| 468 | << @5 << "\n"; |
| 469 | YYERROR; |
| 470 | } |
| 471 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 472 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 473 | |
| 474 | ast->leaveScope(); |
| 475 | |
| 476 | $$ = iface; |
| 477 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 478 | ; |
| 479 | |
| 480 | typedef_declaration |
| 481 | : TYPEDEF type IDENTIFIER |
| 482 | { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 483 | std::string errorMsg; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 484 | if (!ast->addTypeDef($3, $2, convertYYLoc(@3), &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 485 | std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 486 | YYERROR; |
| 487 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 488 | |
| 489 | $$ = nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 490 | } |
| 491 | ; |
| 492 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 493 | const_expr |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 494 | : INTEGER { $$ = new ConstantExpression($1); } |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 495 | | fqname |
| 496 | { |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 497 | 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 Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 507 | std::cerr << "ERROR: identifier " << $1->string() |
| 508 | << " could not be found at " << @1 << ".\n"; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 509 | YYERROR; |
| 510 | } |
| 511 | if(!iden->isEnumValue()) { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 512 | std::cerr << "ERROR: identifier " << $1->string() |
| 513 | << " is not an enum value at " << @1 << ".\n"; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 514 | 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 Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 527 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 528 | | const_expr '?' const_expr ':' const_expr |
| 529 | { |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 530 | $$ = new ConstantExpression($1, $3, $5); |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 531 | } |
| 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 Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 555 | | '(' error ')' |
| 556 | { |
| 557 | ast->addSyntaxError(); |
| 558 | // to avoid segfaults |
| 559 | $$ = new ConstantExpression(ConstantExpression::Zero(ScalarType::KIND_INT32)); |
| 560 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 561 | ; |
| 562 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 563 | method_declaration |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 564 | : error_stmt { $$ = nullptr; } |
| 565 | | opt_annotations IDENTIFIER '(' typed_vars ')' require_semicolon |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 566 | { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 567 | $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1); |
| 568 | } |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 569 | | opt_annotations ONEWAY IDENTIFIER '(' typed_vars ')' require_semicolon |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 570 | { |
| 571 | $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 572 | } |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 573 | | opt_annotations IDENTIFIER '(' typed_vars ')' GENERATES '(' typed_vars ')' require_semicolon |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 574 | { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 575 | $$ = new Method($2, $4, $8, false, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 576 | } |
| 577 | ; |
| 578 | |
| 579 | typed_vars |
| 580 | : /* empty */ |
| 581 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 582 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 583 | } |
| 584 | | typed_var |
| 585 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 586 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 587 | $$->push_back($1); |
| 588 | } |
| 589 | | typed_vars ',' typed_var |
| 590 | { |
| 591 | $$ = $1; |
| 592 | $$->push_back($3); |
| 593 | } |
| 594 | ; |
| 595 | |
| 596 | typed_var : type IDENTIFIER { $$ = new TypedVar($2, $1); } |
| 597 | ; |
| 598 | |
| 599 | |
| 600 | struct_or_union_keyword |
| 601 | : STRUCT { $$ = CompoundType::STYLE_STRUCT; } |
| 602 | | UNION { $$ = CompoundType::STYLE_UNION; } |
| 603 | ; |
| 604 | |
| 605 | named_struct_or_union_declaration |
| 606 | : struct_or_union_keyword IDENTIFIER |
| 607 | { |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 608 | CompoundType *container = new CompoundType($1, $2, convertYYLoc(@2)); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 609 | ast->enterScope(container); |
| 610 | } |
| 611 | struct_or_union_body |
| 612 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 613 | if (!ast->scope()->isCompoundType()) { |
| 614 | std::cerr << "ERROR: unknown error in struct or union declaration at " |
| 615 | << @4 << "\n"; |
| 616 | YYERROR; |
| 617 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 618 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 619 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 620 | std::string errorMsg; |
| 621 | if (!container->setFields($4, &errorMsg)) { |
| 622 | std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 623 | YYERROR; |
| 624 | } |
| 625 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 626 | ast->leaveScope(); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 627 | |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 628 | if (!ast->addScopedType(container, &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 629 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 630 | YYERROR; |
| 631 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 632 | |
| 633 | $$ = container; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 634 | } |
| 635 | ; |
| 636 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 637 | struct_or_union_body |
| 638 | : '{' field_declarations '}' { $$ = $2; } |
| 639 | ; |
| 640 | |
| 641 | field_declarations |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 642 | : /* empty */ { $$ = new std::vector<CompoundField *>; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 643 | | field_declarations field_declaration |
| 644 | { |
| 645 | $$ = $1; |
| 646 | |
| 647 | if ($2 != NULL) { |
| 648 | $$->push_back($2); |
| 649 | } |
| 650 | } |
| 651 | ; |
| 652 | |
| 653 | field_declaration |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 654 | : error_stmt { $$ = nullptr; } |
| 655 | | type IDENTIFIER require_semicolon { $$ = new CompoundField($2, $1); } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 656 | | annotated_compound_declaration ';' { $$ = NULL; } |
| 657 | ; |
| 658 | |
| 659 | annotated_compound_declaration |
| 660 | : opt_annotations compound_declaration |
| 661 | { |
| 662 | $2->setAnnotations($1); |
| 663 | $$ = $2; |
| 664 | } |
| 665 | ; |
| 666 | |
| 667 | compound_declaration |
Yifan Hong | a285501 | 2016-10-11 13:36:54 -0700 | [diff] [blame] | 668 | : named_struct_or_union_declaration { $$ = $1; } |
Yifan Hong | 6a2fedf | 2016-10-11 13:44:07 -0700 | [diff] [blame] | 669 | | named_enum_declaration { $$ = $1; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 670 | ; |
| 671 | |
| 672 | opt_storage_type |
| 673 | : /* empty */ { $$ = NULL; } |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 674 | | ':' fqtype |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 675 | { |
| 676 | $$ = $2; |
| 677 | |
| 678 | if ($$ != NULL && !$$->isValidEnumStorageType()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 679 | std::cerr << "ERROR: Invalid enum storage type specified. at " |
| 680 | << @2 << "\n"; |
| 681 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 682 | YYERROR; |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 685 | ; |
| 686 | |
| 687 | opt_comma |
| 688 | : /* empty */ |
| 689 | | ',' |
| 690 | ; |
| 691 | |
| 692 | named_enum_declaration |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 693 | : ENUM IDENTIFIER opt_storage_type |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 694 | { |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 695 | ast->enterScope(new EnumType($2, convertYYLoc(@2), $3)); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 696 | } |
| 697 | enum_declaration_body |
| 698 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 699 | if (!ast->scope()->isEnum()) { |
| 700 | std::cerr << "ERROR: unknown error in enum declaration at " |
| 701 | << @5 << "\n"; |
| 702 | YYERROR; |
| 703 | } |
| 704 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 705 | EnumType *enumType = static_cast<EnumType *>(ast->scope()); |
| 706 | ast->leaveScope(); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 707 | |
| 708 | std::string errorMsg; |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 709 | if (!ast->addScopedType(enumType, &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 710 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 711 | YYERROR; |
| 712 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 713 | |
| 714 | $$ = enumType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 715 | } |
| 716 | ; |
| 717 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 718 | enum_declaration_body |
| 719 | : '{' enum_values opt_comma '}' { $$ = $2; } |
| 720 | ; |
| 721 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 722 | enum_value |
| 723 | : IDENTIFIER { $$ = new EnumValue($1); } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 724 | | IDENTIFIER '=' const_expr { $$ = new EnumValue($1, $3); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 725 | ; |
| 726 | |
| 727 | enum_values |
| 728 | : /* empty */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 729 | { /* do nothing */ } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 730 | | enum_value |
| 731 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 732 | if (!ast->scope()->isEnum()) { |
| 733 | std::cerr << "ERROR: unknown error in enum declaration at " |
| 734 | << @1 << "\n"; |
| 735 | YYERROR; |
| 736 | } |
| 737 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 738 | static_cast<EnumType *>(ast->scope())->addValue($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 739 | } |
| 740 | | enum_values ',' enum_value |
| 741 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame^] | 742 | if (!ast->scope()->isEnum()) { |
| 743 | std::cerr << "ERROR: unknown error in enum declaration at " |
| 744 | << @3 << "\n"; |
| 745 | YYERROR; |
| 746 | } |
| 747 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 748 | static_cast<EnumType *>(ast->scope())->addValue($3); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 749 | } |
| 750 | ; |
| 751 | |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 752 | array_type_base |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 753 | : fqtype { $$ = $1; } |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 754 | | TEMPLATED '<' type '>' |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 755 | { |
Andreas Huber | 86a112b | 2016-10-19 14:25:16 -0700 | [diff] [blame] | 756 | if (!$1->isVector() && $3->isBinder()) { |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 757 | std::cerr << "ERROR: TemplatedType of interface types are not " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 758 | << "supported. at " << @3 << "\n"; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 759 | |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 760 | YYERROR; |
| 761 | } |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 762 | $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 Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 770 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 771 | YYERROR; |
| 772 | } |
| 773 | $3->setElementType($5); |
| 774 | $1->setElementType($3); |
| 775 | $$ = $1; |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 776 | } |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 777 | ; |
| 778 | |
| 779 | array_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 | |
| 801 | type |
| 802 | : array_type_base { $$ = $1; } |
| 803 | | array_type { $$ = $1; } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 804 | | annotated_compound_declaration { $$ = $1; } |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 805 | | INTERFACE { $$ = new GenericBinder; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 806 | ; |
| 807 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 808 | %% |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 809 | |
| 810 | #include <android-base/logging.h> |
| 811 | |
| 812 | void 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 | |