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