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