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