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