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> |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 36 | #include <hidl-util/StringHelper.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | |
| 39 | using namespace android; |
| 40 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 41 | extern int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 42 | |
| 43 | #define scanner ast->scanner() |
| 44 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 45 | ::android::Location convertYYLoc(const yy::parser::location_type &loc) { |
| 46 | return ::android::Location( |
| 47 | ::android::Position(*(loc.begin.filename), loc.begin.line, loc.begin.column), |
| 48 | ::android::Position(*(loc.end.filename), loc.end.line, loc.end.column) |
| 49 | ); |
| 50 | } |
| 51 | |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 52 | bool isValidInterfaceField(const char *identifier, std::string *errorMsg) { |
| 53 | static const std::vector<std::string> reserved({ |
| 54 | // Injected names to interfaces by auto-generated code |
| 55 | "isRemote", "interfaceChain", "descriptor", "hidlStaticBlock", "onTransact", |
| 56 | "castFrom", "version", "getInterfaceVersion", |
| 57 | |
| 58 | // Inherited names by interfaces from IInterface / IBinder |
| 59 | "onAsBinder", "asBinder", "queryLocalInterface", "getInterfaceDescriptor", "isBinderAlive", |
| 60 | "pingBinder", "dump", "transact", "linkToDeath", "unlinkToDeath", "checkSubclass", |
| 61 | "attachObject", "findObject", "detachObject", "localBinder", "remoteBinder", "mImpl", |
| 62 | }); |
| 63 | std::string idstr(identifier); |
| 64 | if (std::find(reserved.begin(), reserved.end(), idstr) != reserved.end()) { |
| 65 | *errorMsg = idstr + " cannot be a name inside an interface"; |
| 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool isValidStructField(const char *identifier, std::string *errorMsg) { |
| 72 | static const std::vector<std::string> reserved({ |
| 73 | // Injected names to structs and unions by auto-generated code |
| 74 | "readEmbeddedFromParcel", "writeEmbeddedToParcel", "readVectorFromParcel", |
| 75 | "writeVectorToParcel", "writeEmbeddedToBlob", |
| 76 | }); |
| 77 | std::string idstr(identifier); |
| 78 | if (std::find(reserved.begin(), reserved.end(), idstr) != reserved.end()) { |
| 79 | *errorMsg = idstr + " cannot be a name inside an struct or union"; |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool isValidIdentifier(const char *identifier, std::string *errorMsg) { |
| 86 | static const std::vector<std::string> keywords({ |
| 87 | "uint8_t", "uint16_t", "uint32_t", "uint64_t", |
| 88 | "int8_t", "int16_t", "int32_t", "int64_t", "bool", "float", "double", |
| 89 | "interface", "struct", "union", "string", "vec", "enum", "ref", "handle", |
| 90 | "package", "import", "typedef", "generates", "oneway", "extends", |
| 91 | "MQDescriptorSync", "MQDescriptorUnsync", |
| 92 | }); |
| 93 | static const std::vector<std::string> cppKeywords({ |
| 94 | "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", |
| 95 | "atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch", |
| 96 | "char", "char16_t", "char32_t", "class", "compl", "concept", "const", "constexpr", |
| 97 | "const_cast", "continue", "decltype", "default", "delete", "do", "double", |
| 98 | "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", |
| 99 | "for", "friend", "goto", "if", "inline", "int", "import", "long", "module", "mutable", |
| 100 | "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", |
| 101 | "private", "protected", "public", "register", "reinterpret_cast", "requires", "return", |
| 102 | "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", |
| 103 | "switch", "synchronized", "template", "this", "thread_local", "throw", "true", "try", |
| 104 | "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", |
| 105 | "volatile", "wchar_t", "while", "xor", "xor_eq", |
| 106 | }); |
| 107 | static const std::vector<std::string> javaKeywords({ |
| 108 | "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", |
| 109 | "synchronized", "boolean", "do", "if", "private", "this", "break", "double", |
| 110 | "implements", "protected", "throw", "byte", "else", "import", "public", "throws", |
| 111 | "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", |
| 112 | "short", "try", "char", "final", "interface", "static", "void", "class", "finally", |
| 113 | "long", "strictfp", "volatile", "const", "float", "native", "super", "while", |
| 114 | }); |
| 115 | static const std::vector<std::string> cppCollide({ |
| 116 | "size_t", "offsetof", |
| 117 | "DECLARE_REGISTER_AND_GET_SERVICE", "IMPLEMENT_HWBINDER_META_INTERFACE", |
| 118 | "IMPLEMENT_REGISTER_AND_GET_SERVICE" |
| 119 | }); |
| 120 | static const std::vector<std::string> hidlReserved({ |
| 121 | // Part of HidlSupport |
| 122 | "hidl_string", "hidl_vec", "hidl_array", "hidl_version", "toBinder", "castInterface", |
| 123 | "make_hidl_version" |
| 124 | }); |
| 125 | |
| 126 | // errors |
| 127 | std::string idstr(identifier); |
| 128 | if (std::find(keywords.begin(), keywords.end(), idstr) != keywords.end()) { |
| 129 | *errorMsg = idstr + " is a HIDL keyword " |
| 130 | "and is therefore not a valid identifier"; |
| 131 | return false; |
| 132 | } |
| 133 | if (std::find(cppKeywords.begin(), cppKeywords.end(), idstr) != cppKeywords.end()) { |
| 134 | *errorMsg = idstr + " is a C++ keyword " |
| 135 | "and is therefore not a valid identifier"; |
| 136 | return false; |
| 137 | } |
| 138 | if (std::find(javaKeywords.begin(), javaKeywords.end(), idstr) != javaKeywords.end()) { |
| 139 | *errorMsg = idstr + " is a Java keyword " |
| 140 | "and is therefore not a valid identifier"; |
| 141 | return false; |
| 142 | } |
| 143 | if (std::find(cppCollide.begin(), cppCollide.end(), idstr) != cppCollide.end()) { |
| 144 | *errorMsg = idstr + " collides with reserved names in C++ code " |
| 145 | "and is therefore not a valid identifier"; |
| 146 | return false; |
| 147 | } |
| 148 | if (StringHelper::StartsWith(idstr, "_hidl_")) { |
| 149 | *errorMsg = idstr + " starts with _hidl_ " |
| 150 | "and is therefore not a valid identifier"; |
| 151 | return false; |
| 152 | } |
| 153 | if (StringHelper::EndsWith(idstr, "_cb")) { |
| 154 | *errorMsg = idstr + " ends with _cb " |
| 155 | "and is therefore not a valid identifier"; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // warnings |
| 160 | if (std::find(hidlReserved.begin(), hidlReserved.end(), idstr) != hidlReserved.end()) { |
| 161 | *errorMsg = idstr + " is a name reserved by HIDL and should be avoided"; |
| 162 | } |
| 163 | return true; |
| 164 | } |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 165 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 166 | %} |
| 167 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 168 | %initial-action { |
| 169 | // Initialize the initial location. |
| 170 | @$.begin.filename = @$.end.filename = |
| 171 | const_cast<std::string *>(&ast->getFilename()); |
| 172 | } |
| 173 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 174 | %parse-param { android::AST *ast } |
| 175 | %lex-param { void *scanner } |
| 176 | %pure-parser |
Steven Moreland | 4ab9b79 | 2016-09-26 14:14:07 -0700 | [diff] [blame] | 177 | %glr-parser |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 178 | %skeleton "glr.cc" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 179 | |
Steven Moreland | 4ab9b79 | 2016-09-26 14:14:07 -0700 | [diff] [blame] | 180 | %expect-rr 0 |
| 181 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 182 | %token<str> ENUM |
| 183 | %token<str> EXTENDS |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 184 | %token<str> FQNAME |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 185 | %token<str> GENERATES |
| 186 | %token<str> IDENTIFIER |
| 187 | %token<str> IMPORT |
| 188 | %token<str> INTEGER |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 189 | %token<str> FLOAT |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 190 | %token<str> INTERFACE |
| 191 | %token<str> PACKAGE |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 192 | %token<type> TYPE |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 193 | %token<str> STRUCT |
| 194 | %token<str> STRING_LITERAL |
| 195 | %token<str> TYPEDEF |
| 196 | %token<str> UNION |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 197 | %token<templatedType> TEMPLATED |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 198 | %token<void> ONEWAY |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 199 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 200 | /* Operator precedence and associativity, as per |
| 201 | * http://en.cppreference.com/w/cpp/language/operator_precedence */ |
| 202 | /* Precedence level 15 ternary operator */ |
| 203 | %right '?' ':' |
| 204 | /* Precedence level 13 - 14, LTR, logical operators*/ |
| 205 | %left LOGICAL_OR |
| 206 | %left LOGICAL_AND |
| 207 | /* Precedence level 10 - 12, LTR, bitwise operators*/ |
| 208 | %left '|' |
| 209 | %left '^' |
| 210 | %left '&' |
| 211 | /* Precedence level 9, LTR */ |
| 212 | %left EQUALITY NEQ |
| 213 | /* Precedence level 8, LTR */ |
| 214 | %left '<' '>' LEQ GEQ |
| 215 | /* Precedence level 7, LTR */ |
| 216 | %left LSHIFT RSHIFT |
| 217 | /* Precedence level 6, LTR */ |
| 218 | %left '+' '-' |
| 219 | /* Precedence level 5, LTR */ |
| 220 | %left '*' '/' '%' |
| 221 | /* Precedence level 3, RTL; but we have to use %left here */ |
| 222 | %left UNARY_MINUS UNARY_PLUS '!' '~' |
| 223 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 224 | %type<str> error_stmt opt_error_stmt error |
Yifan Hong | 6a2fedf | 2016-10-11 13:44:07 -0700 | [diff] [blame] | 225 | %type<str> package |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 226 | %type<fqName> fqname |
| 227 | %type<type> fqtype |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 228 | %type<str> valid_identifier |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 229 | |
Steven Moreland | 1c71fd5 | 2016-11-29 14:03:33 -0800 | [diff] [blame^] | 230 | %type<type> type enum_storage_type |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 231 | %type<type> array_type_base |
| 232 | %type<arrayType> array_type |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 233 | %type<type> opt_extends |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 234 | %type<type> type_declaration type_declaration_body interface_declaration typedef_declaration |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 235 | %type<type> named_struct_or_union_declaration named_enum_declaration |
| 236 | %type<type> compound_declaration annotated_compound_declaration |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 237 | |
| 238 | %type<field> field_declaration |
| 239 | %type<fields> field_declarations struct_or_union_body |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 240 | %type<constantExpression> const_expr |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 241 | %type<enumValue> enum_value |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 242 | %type<enumValues> enum_values enum_declaration_body |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 243 | %type<typedVars> typed_vars |
| 244 | %type<typedVar> typed_var |
| 245 | %type<method> method_declaration |
| 246 | %type<compoundStyle> struct_or_union_keyword |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 247 | %type<stringVec> annotation_string_values annotation_string_value |
| 248 | %type<constExprVec> annotation_const_expr_values annotation_const_expr_value |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 249 | %type<annotationParam> annotation_param |
| 250 | %type<annotationParams> opt_annotation_params annotation_params |
| 251 | %type<annotation> annotation |
| 252 | %type<annotations> opt_annotations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 253 | |
| 254 | %start program |
| 255 | |
| 256 | %union { |
| 257 | const char *str; |
| 258 | android::Type *type; |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 259 | android::ArrayType *arrayType; |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 260 | android::TemplatedType *templatedType; |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 261 | android::FQName *fqName; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 262 | android::CompoundType *compoundType; |
| 263 | android::CompoundField *field; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 264 | std::vector<android::CompoundField *> *fields; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 265 | android::EnumValue *enumValue; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 266 | android::ConstantExpression *constantExpression; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 267 | std::vector<android::EnumValue *> *enumValues; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 268 | android::TypedVar *typedVar; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 269 | std::vector<android::TypedVar *> *typedVars; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 270 | android::Method *method; |
| 271 | android::CompoundType::Style compoundStyle; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 272 | std::vector<std::string> *stringVec; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 273 | std::vector<android::ConstantExpression *> *constExprVec; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 274 | android::AnnotationParam *annotationParam; |
| 275 | android::AnnotationParamVector *annotationParams; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 276 | android::Annotation *annotation; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 277 | std::vector<android::Annotation *> *annotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | %% |
| 281 | |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 282 | valid_identifier |
| 283 | : IDENTIFIER |
| 284 | { |
| 285 | std::string errorMsg; |
| 286 | if (!isValidIdentifier($1, &errorMsg)) { |
| 287 | std::cerr << "ERROR: " << errorMsg << " at " << @1 << "\n"; |
| 288 | YYERROR; |
| 289 | } |
| 290 | if (!errorMsg.empty()) { |
| 291 | std::cerr << "WARNING: " << errorMsg << " at " << @1 << "\n"; |
| 292 | } |
| 293 | $$ = $1; |
| 294 | } |
| 295 | ; |
| 296 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 297 | opt_annotations |
| 298 | : /* empty */ |
| 299 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 300 | $$ = new std::vector<Annotation *>; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 301 | } |
| 302 | | opt_annotations annotation |
| 303 | { |
| 304 | $$ = $1; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 305 | $$->push_back($2); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 306 | } |
| 307 | ; |
| 308 | |
| 309 | annotation |
| 310 | : '@' IDENTIFIER opt_annotation_params |
| 311 | { |
| 312 | $$ = new Annotation($2, $3); |
| 313 | } |
| 314 | ; |
| 315 | |
| 316 | opt_annotation_params |
| 317 | : /* empty */ |
| 318 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 319 | $$ = new AnnotationParamVector; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 320 | } |
| 321 | | '(' annotation_params ')' |
| 322 | { |
| 323 | $$ = $2; |
| 324 | } |
| 325 | ; |
| 326 | |
| 327 | annotation_params |
| 328 | : annotation_param |
| 329 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 330 | $$ = new AnnotationParamVector; |
| 331 | $$->push_back($1); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 332 | } |
| 333 | | annotation_params ',' annotation_param |
| 334 | { |
| 335 | $$ = $1; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 336 | $$->push_back($3); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 337 | } |
| 338 | ; |
| 339 | |
| 340 | annotation_param |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 341 | : IDENTIFIER '=' annotation_string_value |
| 342 | { |
| 343 | $$ = new AnnotationParam($1, $3); |
| 344 | } |
| 345 | | IDENTIFIER '=' annotation_const_expr_value |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 346 | { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 347 | $$ = new AnnotationParam($1, $3); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 348 | } |
| 349 | ; |
| 350 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 351 | annotation_string_value |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 352 | : STRING_LITERAL |
| 353 | { |
| 354 | $$ = new std::vector<std::string>; |
| 355 | $$->push_back($1); |
| 356 | } |
| 357 | | '{' annotation_string_values '}' { $$ = $2; } |
| 358 | ; |
| 359 | |
| 360 | annotation_string_values |
| 361 | : STRING_LITERAL |
| 362 | { |
| 363 | $$ = new std::vector<std::string>; |
| 364 | $$->push_back($1); |
| 365 | } |
| 366 | | annotation_string_values ',' STRING_LITERAL |
| 367 | { |
| 368 | $$ = $1; |
| 369 | $$->push_back($3); |
| 370 | } |
| 371 | ; |
| 372 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 373 | annotation_const_expr_value |
| 374 | : const_expr |
| 375 | { |
| 376 | $$ = new std::vector<ConstantExpression *>; |
| 377 | $$->push_back($1); |
| 378 | } |
| 379 | | '{' annotation_const_expr_values '}' { $$ = $2; } |
| 380 | ; |
| 381 | |
| 382 | annotation_const_expr_values |
| 383 | : const_expr |
| 384 | { |
| 385 | $$ = new std::vector<ConstantExpression *>; |
| 386 | $$->push_back($1); |
| 387 | } |
| 388 | | annotation_const_expr_values ',' const_expr |
| 389 | { |
| 390 | $$ = $1; |
| 391 | $$->push_back($3); |
| 392 | } |
| 393 | ; |
| 394 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 395 | error_stmt |
| 396 | : error ';' |
| 397 | { |
| 398 | $$ = $1; |
| 399 | ast->addSyntaxError(); |
| 400 | // std::cerr << "WARNING: skipping errors until " << @2 << ".\n"; |
| 401 | } |
| 402 | ; |
| 403 | |
| 404 | opt_error_stmt |
| 405 | : /* empty */ { $$ = NULL; } |
| 406 | | error_stmt { $$ = $1; } |
| 407 | ; |
| 408 | |
| 409 | require_semicolon |
| 410 | : ';' |
| 411 | | /* empty */ |
| 412 | { |
| 413 | std::cerr << "ERROR: missing ; at " << @$ << "\n"; |
| 414 | ast->addSyntaxError(); |
| 415 | } |
| 416 | ; |
| 417 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 418 | program |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 419 | : opt_error_stmt |
| 420 | package |
| 421 | imports |
| 422 | body |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 423 | ; |
| 424 | |
| 425 | fqname |
| 426 | : FQNAME |
| 427 | { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 428 | $$ = new FQName($1); |
| 429 | if(!$$->isValid()) { |
| 430 | std::cerr << "ERROR: FQName '" << $1 << "' is not valid at " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 431 | << @1 |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 432 | << ".\n"; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 433 | YYERROR; |
| 434 | } |
| 435 | } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 436 | | valid_identifier |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 437 | { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 438 | $$ = new FQName($1); |
| 439 | if(!$$->isValid()) { |
| 440 | std::cerr << "ERROR: FQName '" << $1 << "' is not valid at " |
| 441 | << @1 |
| 442 | << ".\n"; |
| 443 | YYERROR; |
| 444 | } |
| 445 | } |
| 446 | ; |
| 447 | |
| 448 | fqtype |
| 449 | : fqname |
| 450 | { |
| 451 | $$ = ast->lookupType(*($1)); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 452 | if ($$ == NULL) { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 453 | std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at " |
| 454 | << @1 |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 455 | << "\n"; |
| 456 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 457 | YYERROR; |
| 458 | } |
| 459 | } |
Hridya Valsaraju | cd91bf6 | 2016-10-25 12:41:04 -0700 | [diff] [blame] | 460 | | TYPE |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 461 | ; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 462 | |
| 463 | package |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 464 | : PACKAGE FQNAME require_semicolon |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 465 | { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 466 | if (!ast->setPackage($2)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 467 | std::cerr << "ERROR: Malformed package identifier '" |
| 468 | << $2 |
| 469 | << "' at " |
| 470 | << @2 |
| 471 | << "\n"; |
| 472 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 473 | YYERROR; |
| 474 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 475 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 476 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 477 | import_stmt |
| 478 | : IMPORT FQNAME require_semicolon |
| 479 | { |
| 480 | if (!ast->addImport($2)) { |
| 481 | std::cerr << "ERROR: Unable to import '" << $2 << "' at " << @2 |
| 482 | << "\n"; |
| 483 | ast->addSyntaxError(); |
| 484 | } |
| 485 | } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 486 | | IMPORT valid_identifier require_semicolon |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 487 | { |
| 488 | if (!ast->addImport($2)) { |
| 489 | std::cerr << "ERROR: Unable to import '" << $2 << "' at " << @2 |
| 490 | << "\n"; |
| 491 | ast->addSyntaxError(); |
| 492 | } |
| 493 | } |
| 494 | | IMPORT error_stmt |
| 495 | ; |
| 496 | |
| 497 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 498 | imports |
| 499 | : /* empty */ |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 500 | | imports import_stmt |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 501 | ; |
| 502 | |
| 503 | opt_extends |
| 504 | : /* empty */ { $$ = NULL; } |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 505 | | EXTENDS fqtype { $$ = $2; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 506 | |
| 507 | body |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 508 | : type_declarations |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 509 | ; |
| 510 | |
| 511 | interface_declarations |
| 512 | : /* empty */ |
| 513 | | interface_declarations type_declaration |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 514 | { |
| 515 | std::string errorMsg; |
| 516 | if ($2 != nullptr && |
| 517 | $2->isNamedType() && |
| 518 | !isValidInterfaceField(static_cast<NamedType *>($2)->localName().c_str(), |
| 519 | &errorMsg)) { |
| 520 | std::cerr << "ERROR: " << errorMsg << " at " |
| 521 | << @2 << "\n"; |
| 522 | YYERROR; |
| 523 | } |
| 524 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 525 | | interface_declarations method_declaration |
| 526 | { |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 527 | std::string errorMsg; |
| 528 | if ($2 != nullptr && |
| 529 | !isValidInterfaceField($2->name().c_str(), &errorMsg)) { |
| 530 | std::cerr << "ERROR: " << errorMsg << " at " |
| 531 | << @2 << "\n"; |
| 532 | YYERROR; |
| 533 | } |
| 534 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 535 | if ($2 != nullptr) { |
| 536 | if (!ast->scope()->isInterface()) { |
| 537 | std::cerr << "ERROR: unknown error in interface declaration at " |
| 538 | << @2 << "\n"; |
| 539 | YYERROR; |
| 540 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 541 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 542 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 543 | if (!iface->addMethod($2)) { |
| 544 | std::cerr << "ERROR: Unable to add method '" << $2->name() |
| 545 | << "' at " << @2 << "\n"; |
| 546 | |
| 547 | YYERROR; |
| 548 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 549 | } |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 550 | // ignore if $2 is nullptr (from error recovery) |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 551 | } |
| 552 | ; |
| 553 | |
| 554 | type_declarations |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 555 | : /* empty */ |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 556 | | error_stmt |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 557 | | type_declarations type_declaration |
| 558 | ; |
| 559 | |
| 560 | type_declaration |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 561 | : opt_annotations type_declaration_body |
| 562 | { |
| 563 | if ($2 != nullptr) { |
| 564 | $2->setAnnotations($1); |
| 565 | } else if (!$1->empty()) { |
| 566 | // Since typedefs are always resolved to their target it makes |
| 567 | // little sense to annotate them and have their annotations |
| 568 | // impose semantics other than their target type. |
| 569 | std::cerr << "ERROR: typedefs cannot be annotated. at " << @2 |
| 570 | << "\n"; |
| 571 | |
| 572 | YYERROR; |
| 573 | } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 574 | $$ = $2; |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 575 | } |
| 576 | ; |
| 577 | |
| 578 | type_declaration_body |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 579 | : named_struct_or_union_declaration require_semicolon |
| 580 | | named_enum_declaration require_semicolon |
| 581 | | typedef_declaration require_semicolon |
| 582 | | interface_declaration require_semicolon |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 583 | ; |
| 584 | |
| 585 | interface_declaration |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 586 | : INTERFACE valid_identifier opt_extends |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 587 | { |
| 588 | if ($3 != NULL && !$3->isInterface()) { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 589 | std::cerr << "ERROR: You can only extend interfaces. at " << @3 |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 590 | << "\n"; |
| 591 | |
| 592 | YYERROR; |
| 593 | } |
| 594 | |
| 595 | if ($2[0] != 'I') { |
| 596 | std::cerr << "ERROR: All interface names must start with an 'I' " |
| 597 | << "prefix. at " << @2 << "\n"; |
| 598 | |
| 599 | YYERROR; |
| 600 | } |
| 601 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 602 | Interface *iface = new Interface($2, convertYYLoc(@2), static_cast<Interface *>($3)); |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 603 | |
| 604 | // Register interface immediately so it can be referenced inside |
| 605 | // definition. |
| 606 | std::string errorMsg; |
| 607 | if (!ast->addScopedType(iface, &errorMsg)) { |
| 608 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
| 609 | YYERROR; |
| 610 | } |
| 611 | |
| 612 | ast->enterScope(iface); |
| 613 | } |
| 614 | '{' interface_declarations '}' |
| 615 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 616 | if (!ast->scope()->isInterface()) { |
| 617 | std::cerr << "ERROR: unknown error in interface declaration at " |
| 618 | << @5 << "\n"; |
| 619 | YYERROR; |
| 620 | } |
| 621 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 622 | Interface *iface = static_cast<Interface *>(ast->scope()); |
| 623 | |
| 624 | ast->leaveScope(); |
| 625 | |
| 626 | $$ = iface; |
| 627 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 628 | ; |
| 629 | |
| 630 | typedef_declaration |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 631 | : TYPEDEF type valid_identifier |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 632 | { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 633 | std::string errorMsg; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 634 | if (!ast->addTypeDef($3, $2, convertYYLoc(@3), &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 635 | std::cerr << "ERROR: " << errorMsg << " at " << @3 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 636 | YYERROR; |
| 637 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 638 | |
| 639 | $$ = nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 640 | } |
| 641 | ; |
| 642 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 643 | const_expr |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 644 | : INTEGER { $$ = new ConstantExpression($1); } |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 645 | | fqname |
| 646 | { |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 647 | if(!$1->isValidValueName()) { |
| 648 | std::cerr << "ERROR: '" << $1->string() |
| 649 | << "' does not refer to an enum value at " |
| 650 | << @1 << ".\n"; |
| 651 | YYERROR; |
| 652 | } |
| 653 | if($1->isIdentifier()) { |
| 654 | std::string identifier = $1->name(); |
| 655 | LocalIdentifier *iden = ast->scope()->lookupIdentifier(identifier); |
| 656 | if(!iden) { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 657 | std::cerr << "ERROR: identifier " << $1->string() |
| 658 | << " could not be found at " << @1 << ".\n"; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 659 | YYERROR; |
| 660 | } |
| 661 | if(!iden->isEnumValue()) { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 662 | std::cerr << "ERROR: identifier " << $1->string() |
| 663 | << " is not an enum value at " << @1 << ".\n"; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 664 | YYERROR; |
| 665 | } |
| 666 | $$ = new ConstantExpression( |
| 667 | *(static_cast<EnumValue *>(iden)->constExpr()), $1->string()); |
| 668 | } else { |
| 669 | std::string errorMsg; |
| 670 | EnumValue *v = ast->lookupEnumValue(*($1), &errorMsg); |
| 671 | if(v == nullptr) { |
| 672 | std::cerr << "ERROR: " << errorMsg << " at " << @1 << ".\n"; |
| 673 | YYERROR; |
| 674 | } |
| 675 | $$ = new ConstantExpression(*(v->constExpr()), $1->string()); |
| 676 | } |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 677 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 678 | | const_expr '?' const_expr ':' const_expr |
| 679 | { |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 680 | $$ = new ConstantExpression($1, $3, $5); |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 681 | } |
| 682 | | const_expr LOGICAL_OR const_expr { $$ = new ConstantExpression($1, "||", $3); } |
| 683 | | const_expr LOGICAL_AND const_expr { $$ = new ConstantExpression($1, "&&", $3); } |
| 684 | | const_expr '|' const_expr { $$ = new ConstantExpression($1, "|" , $3); } |
| 685 | | const_expr '^' const_expr { $$ = new ConstantExpression($1, "^" , $3); } |
| 686 | | const_expr '&' const_expr { $$ = new ConstantExpression($1, "&" , $3); } |
| 687 | | const_expr EQUALITY const_expr { $$ = new ConstantExpression($1, "==", $3); } |
| 688 | | const_expr NEQ const_expr { $$ = new ConstantExpression($1, "!=", $3); } |
| 689 | | const_expr '<' const_expr { $$ = new ConstantExpression($1, "<" , $3); } |
| 690 | | const_expr '>' const_expr { $$ = new ConstantExpression($1, ">" , $3); } |
| 691 | | const_expr LEQ const_expr { $$ = new ConstantExpression($1, "<=", $3); } |
| 692 | | const_expr GEQ const_expr { $$ = new ConstantExpression($1, ">=", $3); } |
| 693 | | const_expr LSHIFT const_expr { $$ = new ConstantExpression($1, "<<", $3); } |
| 694 | | const_expr RSHIFT const_expr { $$ = new ConstantExpression($1, ">>", $3); } |
| 695 | | const_expr '+' const_expr { $$ = new ConstantExpression($1, "+" , $3); } |
| 696 | | const_expr '-' const_expr { $$ = new ConstantExpression($1, "-" , $3); } |
| 697 | | const_expr '*' const_expr { $$ = new ConstantExpression($1, "*" , $3); } |
| 698 | | const_expr '/' const_expr { $$ = new ConstantExpression($1, "/" , $3); } |
| 699 | | const_expr '%' const_expr { $$ = new ConstantExpression($1, "%" , $3); } |
| 700 | | '+' const_expr %prec UNARY_PLUS { $$ = new ConstantExpression("+", $2); } |
| 701 | | '-' const_expr %prec UNARY_MINUS { $$ = new ConstantExpression("-", $2); } |
| 702 | | '!' const_expr { $$ = new ConstantExpression("!", $2); } |
| 703 | | '~' const_expr { $$ = new ConstantExpression("~", $2); } |
| 704 | | '(' const_expr ')' { $$ = $2; } |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 705 | | '(' error ')' |
| 706 | { |
| 707 | ast->addSyntaxError(); |
| 708 | // to avoid segfaults |
| 709 | $$ = new ConstantExpression(ConstantExpression::Zero(ScalarType::KIND_INT32)); |
| 710 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 711 | ; |
| 712 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 713 | method_declaration |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 714 | : error_stmt { $$ = nullptr; } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 715 | | opt_annotations valid_identifier '(' typed_vars ')' require_semicolon |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 716 | { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 717 | $$ = new Method($2, $4, new std::vector<TypedVar *>, false, $1); |
| 718 | } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 719 | | opt_annotations ONEWAY valid_identifier '(' typed_vars ')' require_semicolon |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 720 | { |
| 721 | $$ = new Method($3, $5, new std::vector<TypedVar *>, true, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 722 | } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 723 | | opt_annotations valid_identifier '(' typed_vars ')' GENERATES '(' typed_vars ')' require_semicolon |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 724 | { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 725 | $$ = new Method($2, $4, $8, false, $1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 726 | } |
| 727 | ; |
| 728 | |
| 729 | typed_vars |
| 730 | : /* empty */ |
| 731 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 732 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 733 | } |
| 734 | | typed_var |
| 735 | { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 736 | $$ = new std::vector<TypedVar *>; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 737 | $$->push_back($1); |
| 738 | } |
| 739 | | typed_vars ',' typed_var |
| 740 | { |
| 741 | $$ = $1; |
| 742 | $$->push_back($3); |
| 743 | } |
| 744 | ; |
| 745 | |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 746 | typed_var : type valid_identifier { $$ = new TypedVar($2, $1); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 747 | ; |
| 748 | |
| 749 | |
| 750 | struct_or_union_keyword |
| 751 | : STRUCT { $$ = CompoundType::STYLE_STRUCT; } |
| 752 | | UNION { $$ = CompoundType::STYLE_UNION; } |
| 753 | ; |
| 754 | |
| 755 | named_struct_or_union_declaration |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 756 | : struct_or_union_keyword valid_identifier |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 757 | { |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 758 | CompoundType *container = new CompoundType($1, $2, convertYYLoc(@2)); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 759 | ast->enterScope(container); |
| 760 | } |
| 761 | struct_or_union_body |
| 762 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 763 | if (!ast->scope()->isCompoundType()) { |
| 764 | std::cerr << "ERROR: unknown error in struct or union declaration at " |
| 765 | << @4 << "\n"; |
| 766 | YYERROR; |
| 767 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 768 | CompoundType *container = static_cast<CompoundType *>(ast->scope()); |
| 769 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 770 | std::string errorMsg; |
| 771 | if (!container->setFields($4, &errorMsg)) { |
| 772 | std::cerr << "ERROR: " << errorMsg << " at " << @4 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 773 | YYERROR; |
| 774 | } |
| 775 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 776 | ast->leaveScope(); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 777 | |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 778 | if (!ast->addScopedType(container, &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 779 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 780 | YYERROR; |
| 781 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 782 | |
| 783 | $$ = container; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 784 | } |
| 785 | ; |
| 786 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 787 | struct_or_union_body |
| 788 | : '{' field_declarations '}' { $$ = $2; } |
| 789 | ; |
| 790 | |
| 791 | field_declarations |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 792 | : /* empty */ { $$ = new std::vector<CompoundField *>; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 793 | | field_declarations field_declaration |
| 794 | { |
| 795 | $$ = $1; |
| 796 | |
| 797 | if ($2 != NULL) { |
| 798 | $$->push_back($2); |
| 799 | } |
| 800 | } |
| 801 | ; |
| 802 | |
| 803 | field_declaration |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 804 | : error_stmt { $$ = nullptr; } |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 805 | | type valid_identifier require_semicolon |
| 806 | { |
| 807 | std::string errorMsg; |
| 808 | if (ast->scope()->isCompoundType() && |
| 809 | static_cast<CompoundType *>(ast->scope())->style() == CompoundType::STYLE_STRUCT && |
| 810 | !isValidStructField($2, &errorMsg)) { |
| 811 | std::cerr << "ERROR: " << errorMsg << " at " |
| 812 | << @2 << "\n"; |
| 813 | YYERROR; |
| 814 | } |
| 815 | $$ = new CompoundField($2, $1); |
| 816 | } |
| 817 | | annotated_compound_declaration ';' |
| 818 | { |
| 819 | std::string errorMsg; |
| 820 | if (ast->scope()->isCompoundType() && |
| 821 | static_cast<CompoundType *>(ast->scope())->style() == CompoundType::STYLE_STRUCT && |
| 822 | $1 != nullptr && |
| 823 | $1->isNamedType() && |
| 824 | !isValidStructField(static_cast<NamedType *>($1)->localName().c_str(), &errorMsg)) { |
| 825 | std::cerr << "ERROR: " << errorMsg << " at " |
| 826 | << @2 << "\n"; |
| 827 | YYERROR; |
| 828 | } |
| 829 | $$ = NULL; |
| 830 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 831 | ; |
| 832 | |
| 833 | annotated_compound_declaration |
| 834 | : opt_annotations compound_declaration |
| 835 | { |
| 836 | $2->setAnnotations($1); |
| 837 | $$ = $2; |
| 838 | } |
| 839 | ; |
| 840 | |
| 841 | compound_declaration |
Yifan Hong | a285501 | 2016-10-11 13:36:54 -0700 | [diff] [blame] | 842 | : named_struct_or_union_declaration { $$ = $1; } |
Yifan Hong | 6a2fedf | 2016-10-11 13:44:07 -0700 | [diff] [blame] | 843 | | named_enum_declaration { $$ = $1; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 844 | ; |
| 845 | |
Steven Moreland | 1c71fd5 | 2016-11-29 14:03:33 -0800 | [diff] [blame^] | 846 | enum_storage_type |
| 847 | : ':' fqtype |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 848 | { |
| 849 | $$ = $2; |
| 850 | |
| 851 | if ($$ != NULL && !$$->isValidEnumStorageType()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 852 | std::cerr << "ERROR: Invalid enum storage type specified. at " |
| 853 | << @2 << "\n"; |
| 854 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 855 | YYERROR; |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 858 | ; |
| 859 | |
| 860 | opt_comma |
| 861 | : /* empty */ |
| 862 | | ',' |
| 863 | ; |
| 864 | |
| 865 | named_enum_declaration |
Steven Moreland | 1c71fd5 | 2016-11-29 14:03:33 -0800 | [diff] [blame^] | 866 | : ENUM valid_identifier enum_storage_type |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 867 | { |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 868 | ast->enterScope(new EnumType($2, convertYYLoc(@2), $3)); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 869 | } |
| 870 | enum_declaration_body |
| 871 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 872 | if (!ast->scope()->isEnum()) { |
| 873 | std::cerr << "ERROR: unknown error in enum declaration at " |
| 874 | << @5 << "\n"; |
| 875 | YYERROR; |
| 876 | } |
| 877 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 878 | EnumType *enumType = static_cast<EnumType *>(ast->scope()); |
| 879 | ast->leaveScope(); |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 880 | |
| 881 | std::string errorMsg; |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 882 | if (!ast->addScopedType(enumType, &errorMsg)) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 883 | std::cerr << "ERROR: " << errorMsg << " at " << @2 << "\n"; |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 884 | YYERROR; |
| 885 | } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 886 | |
| 887 | $$ = enumType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 888 | } |
| 889 | ; |
| 890 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 891 | enum_declaration_body |
| 892 | : '{' enum_values opt_comma '}' { $$ = $2; } |
| 893 | ; |
| 894 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 895 | enum_value |
Yifan Hong | 27e85db | 2016-11-09 15:45:52 -0800 | [diff] [blame] | 896 | : valid_identifier { $$ = new EnumValue($1); } |
| 897 | | valid_identifier '=' const_expr { $$ = new EnumValue($1, $3); } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 898 | ; |
| 899 | |
| 900 | enum_values |
| 901 | : /* empty */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 902 | { /* do nothing */ } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 903 | | enum_value |
| 904 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 905 | if (!ast->scope()->isEnum()) { |
| 906 | std::cerr << "ERROR: unknown error in enum declaration at " |
| 907 | << @1 << "\n"; |
| 908 | YYERROR; |
| 909 | } |
| 910 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 911 | static_cast<EnumType *>(ast->scope())->addValue($1); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 912 | } |
| 913 | | enum_values ',' enum_value |
| 914 | { |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 915 | if (!ast->scope()->isEnum()) { |
| 916 | std::cerr << "ERROR: unknown error in enum declaration at " |
| 917 | << @3 << "\n"; |
| 918 | YYERROR; |
| 919 | } |
| 920 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 921 | static_cast<EnumType *>(ast->scope())->addValue($3); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 922 | } |
| 923 | ; |
| 924 | |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 925 | array_type_base |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 926 | : fqtype { $$ = $1; } |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 927 | | TEMPLATED '<' type '>' |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 928 | { |
Andreas Huber | 86a112b | 2016-10-19 14:25:16 -0700 | [diff] [blame] | 929 | if (!$1->isVector() && $3->isBinder()) { |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 930 | std::cerr << "ERROR: TemplatedType of interface types are not " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 931 | << "supported. at " << @3 << "\n"; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 932 | |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 933 | YYERROR; |
| 934 | } |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 935 | $1->setElementType($3); |
| 936 | $$ = $1; |
| 937 | } |
| 938 | | TEMPLATED '<' TEMPLATED '<' type RSHIFT |
| 939 | { |
| 940 | if ($5->isBinder()) { |
| 941 | std::cerr << "ERROR: TemplatedType of interface types are not " |
| 942 | << "supported. at " << @5 << "\n"; |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 943 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 944 | YYERROR; |
| 945 | } |
| 946 | $3->setElementType($5); |
| 947 | $1->setElementType($3); |
| 948 | $$ = $1; |
Andreas Huber | b95ea8a | 2016-08-15 15:35:42 -0700 | [diff] [blame] | 949 | } |
Yifan Hong | bd33e38 | 2016-11-02 13:30:17 -0700 | [diff] [blame] | 950 | ; |
| 951 | |
| 952 | array_type |
| 953 | : array_type_base '[' const_expr ']' |
| 954 | { |
| 955 | if ($1->isBinder()) { |
| 956 | std::cerr << "ERROR: Arrays of interface types are not supported." |
| 957 | << " at " << @1 << "\n"; |
| 958 | |
| 959 | YYERROR; |
| 960 | } |
| 961 | if ($1->isArray()) { |
| 962 | $$ = new ArrayType(static_cast<ArrayType *>($1), $3); |
| 963 | } else { |
| 964 | $$ = new ArrayType($1, $3); |
| 965 | } |
| 966 | } |
| 967 | | array_type '[' const_expr ']' |
| 968 | { |
| 969 | $$ = $1; |
| 970 | $$->appendDimension($3); |
| 971 | } |
| 972 | ; |
| 973 | |
| 974 | type |
| 975 | : array_type_base { $$ = $1; } |
| 976 | | array_type { $$ = $1; } |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 977 | | annotated_compound_declaration { $$ = $1; } |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 978 | | INTERFACE { $$ = new GenericBinder; } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 979 | ; |
| 980 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 981 | %% |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 982 | |
| 983 | #include <android-base/logging.h> |
| 984 | |
| 985 | void yy::parser::error( |
| 986 | const yy::parser::location_type &where, |
| 987 | const std::string &errstr) { |
| 988 | std::cerr << "ERROR: " << errstr << " at " << where << "\n"; |
| 989 | } |
| 990 | |