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 | #ifndef AST_H_ |
| 18 | |
| 19 | #define AST_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 22 | #include <hidl-hash/Hash.h> |
Steven Moreland | 7ae3d54 | 2017-01-18 16:46:01 -0800 | [diff] [blame] | 23 | #include <hidl-util/FQName.h> |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 24 | #include <functional> |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 25 | #include <map> |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 26 | #include <set> |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 27 | #include <string> |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 28 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 29 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 30 | #include "Scope.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 31 | #include "Type.h" |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 32 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 35 | struct Coordinator; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 36 | struct ConstantExpression; |
Steven Moreland | 4d89ee2 | 2019-03-08 13:25:32 -0800 | [diff] [blame] | 37 | struct DocComment; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 38 | struct EnumValue; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 39 | struct Formatter; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 40 | struct Interface; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 41 | struct Location; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 42 | struct Method; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 43 | struct NamedType; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 44 | template <class T> |
| 45 | struct NamedReference; |
| 46 | struct Type; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 47 | |
Neel Mehta | 55c065e | 2019-05-31 13:30:12 -0700 | [diff] [blame] | 48 | struct ImportStatement { |
| 49 | FQName fqName; |
| 50 | Location location; |
| 51 | }; |
| 52 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 53 | struct AST { |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 54 | AST(const Coordinator* coordinator, const Hash* fileHash); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 55 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 56 | bool setPackage(const char *package); |
Neel Mehta | 55c065e | 2019-05-31 13:30:12 -0700 | [diff] [blame] | 57 | bool addImport(const char* import, const Location& location); |
Neel Mehta | 8b0f06a | 2019-07-11 18:13:21 -0700 | [diff] [blame] | 58 | bool addImplicitImport(const FQName& fqName); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 59 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 60 | // package and version really. |
| 61 | FQName package() const; |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 62 | bool isInterface() const; |
Steven Moreland | b47a262 | 2018-07-11 09:04:25 -0700 | [diff] [blame] | 63 | bool definesInterfaces() const; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 64 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 65 | // Adds package, version and scope stack to local name |
| 66 | FQName makeFullName(const char* localName, Scope* scope) const; |
| 67 | |
| 68 | void addScopedType(NamedType* type, Scope* scope); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 69 | |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 70 | const std::string& getFilename() const; |
| 71 | const Hash* getFileHash() const; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 72 | |
Neel Mehta | f6293d3 | 2019-06-12 17:16:38 -0700 | [diff] [blame] | 73 | const Coordinator& getCoordinator() const; |
| 74 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 75 | // Look up local identifier. |
| 76 | // It could be plain identifier or enum value as described by lookupEnumValue. |
Steven Moreland | 8f8e862 | 2019-11-04 12:38:38 -0800 | [diff] [blame] | 77 | LocalIdentifier* lookupLocalIdentifier(const Reference<LocalIdentifier>& ref, |
| 78 | const Scope* scope); |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 79 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 80 | // Look up an enum value by "FQName:valueName". |
Steven Moreland | 8f8e862 | 2019-11-04 12:38:38 -0800 | [diff] [blame] | 81 | EnumValue* lookupEnumValue(const FQName& fqName, std::string* errorMsg, const Scope* scope); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 82 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 83 | // Look up a type by FQName, "pure" names, i.e. those without package |
| 84 | // or version are first looked up in the current scope chain. |
| 85 | // After that lookup proceeds to imports. |
Steven Moreland | 8f8e862 | 2019-11-04 12:38:38 -0800 | [diff] [blame] | 86 | Type* lookupType(const FQName& fqName, const Scope* scope); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 87 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 88 | void addImportedAST(AST *ast); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 89 | |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 90 | // Calls all passes after parsing required before |
| 91 | // being ready to generate output. |
| 92 | status_t postParse(); |
| 93 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 94 | // Recursive pass on constant expression tree |
| 95 | status_t constantExpressionRecursivePass( |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 96 | const std::function<status_t(ConstantExpression*)>& func, bool processBeforeDependencies); |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 97 | status_t constantExpressionRecursivePass( |
| 98 | const std::function<status_t(const ConstantExpression*)>& func, |
| 99 | bool processBeforeDependencies) const; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 100 | |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 101 | // Recursive tree pass that sets ParseStage of all types to newStage. |
| 102 | status_t setParseStage(Type::ParseStage oldStage, Type::ParseStage newStage); |
| 103 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 104 | // Recursive tree pass that looks up all referenced types |
| 105 | status_t lookupTypes(); |
| 106 | |
| 107 | // Recursive tree pass that looks up all referenced local identifiers |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 108 | // and types referenced by constant expressions |
| 109 | status_t lookupConstantExpressions(); |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 110 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 111 | // Recursive tree pass that validates that all defined types |
| 112 | // have unique names in their scopes. |
| 113 | status_t validateDefinedTypesUniqueNames() const; |
| 114 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 115 | // Recursive tree pass that completes type declarations |
| 116 | // that depend on super types |
| 117 | status_t resolveInheritance(); |
| 118 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 119 | // Recursive tree pass that validates constant expressions |
| 120 | status_t validateConstantExpressions() const; |
| 121 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 122 | // Recursive tree pass that evaluates constant expressions |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 123 | status_t evaluateConstantExpressions(); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 124 | |
| 125 | // Recursive tree pass that validates all type-related |
| 126 | // syntax restrictions |
| 127 | status_t validate() const; |
| 128 | |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 129 | // Recursive tree pass that ensures that type definitions and references |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 130 | // are acyclic and reorderes type definitions in reversed topological order. |
| 131 | status_t topologicalReorder(); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 132 | |
| 133 | // Recursive tree pass that ensures that constant expressions |
| 134 | // are acyclic. |
| 135 | status_t checkAcyclicConstantExpressions() const; |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 136 | |
Timur Iskhakov | 041fdfe | 2017-09-06 15:56:01 -0700 | [diff] [blame] | 137 | // Recursive tree pass that checks C++ forward declaration restrictions. |
| 138 | status_t checkForwardReferenceRestrictions() const; |
| 139 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 140 | status_t gatherReferencedTypes(); |
| 141 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 142 | void generateCppSource(Formatter& out) const; |
Steven Moreland | 6d68855 | 2017-09-15 11:03:02 -0700 | [diff] [blame] | 143 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 144 | void generateInterfaceHeader(Formatter& out) const; |
| 145 | void generateHwBinderHeader(Formatter& out) const; |
| 146 | void generateStubHeader(Formatter& out) const; |
| 147 | void generateProxyHeader(Formatter& out) const; |
| 148 | void generatePassthroughHeader(Formatter& out) const; |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 149 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 150 | void generateCppImplHeader(Formatter& out) const; |
| 151 | void generateCppImplSource(Formatter& out) const; |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 152 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 153 | void generateJava(Formatter& out, const std::string& limitToType) const; |
Neel Mehta | 4b6f439 | 2019-05-09 16:03:47 -0700 | [diff] [blame] | 154 | void generateJavaImpl(Formatter& out) const; |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 155 | void generateJavaTypes(Formatter& out, const std::string& limitToType) const; |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 156 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 157 | void generateVts(Formatter& out) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 158 | |
Yifan Hong | e401011 | 2018-10-05 11:44:15 -0700 | [diff] [blame] | 159 | void generateDependencies(Formatter& out) const; |
Daniel Norman | cb0d836 | 2019-07-08 11:32:15 -0700 | [diff] [blame] | 160 | void generateInheritanceHierarchy(Formatter& out) const; |
Yifan Hong | e401011 | 2018-10-05 11:44:15 -0700 | [diff] [blame] | 161 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 162 | void generateFormattedHidl(Formatter& out) const; |
| 163 | |
Neel Mehta | 55c065e | 2019-05-31 13:30:12 -0700 | [diff] [blame] | 164 | const std::vector<ImportStatement>& getImportStatements() const; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 165 | void getImportedPackages(std::set<FQName> *importSet) const; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 166 | |
Yifan Hong | 40a373d | 2016-11-30 15:16:47 -0800 | [diff] [blame] | 167 | // Run getImportedPackages on this, then run getImportedPackages on |
| 168 | // each AST in each package referenced in importSet. |
| 169 | void getImportedPackagesHierarchy(std::set<FQName> *importSet) const; |
| 170 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 171 | bool isJavaCompatible() const; |
| 172 | |
Steven Moreland | 06a81cf | 2018-01-17 11:13:46 -0800 | [diff] [blame] | 173 | // Warning: this only includes names explicitly referenced in code. |
| 174 | // It does not include all names which are imported. |
| 175 | // |
| 176 | // Currently, there is one valid usecase for this: importing exactly |
| 177 | // the names which need to be imported in generated code. If you import |
| 178 | // based on getAllImportedNamesGranular instead, you will import things |
| 179 | // that aren't actually used in the resultant code. |
| 180 | // |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 181 | // Get transitive closure of imported interface/types. This will add |
| 182 | // everything exported by a package even if only a single type from |
| 183 | // that package was explicitly imported! |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 184 | void getAllImportedNames(std::set<FQName> *allImportSet) const; |
| 185 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 186 | // Get imported types, this includes those explicitly imported as well |
| 187 | // as all types defined in imported packages. |
| 188 | void getAllImportedNamesGranular(std::set<FQName> *allImportSet) const; |
| 189 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 190 | void appendToExportedTypesVector( |
| 191 | std::vector<const Type *> *exportedTypes) const; |
| 192 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 193 | // used by the parser. |
| 194 | void addSyntaxError(); |
| 195 | size_t syntaxErrors() const; |
| 196 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 197 | bool isIBase() const; |
| 198 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 199 | // or nullptr if not isInterface |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 200 | const Interface *getInterface() const; |
| 201 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 202 | // types or Interface base name (e.x. Foo) |
| 203 | std::string getBaseName() const; |
| 204 | |
Neel Mehta | 693169b | 2019-05-29 18:45:25 -0700 | [diff] [blame] | 205 | Scope* getMutableRootScope(); |
| 206 | const Scope& getRootScope() const; |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 207 | |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 208 | static void generateCppPackageInclude(Formatter& out, const FQName& package, |
| 209 | const std::string& klass); |
| 210 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 211 | void addDefinedTypes(std::set<FQName> *definedTypes) const; |
| 212 | void addReferencedTypes(std::set<FQName> *referencedTypes) const; |
| 213 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 214 | void addToImportedNamesGranular(const FQName &fqName); |
| 215 | |
Neel Mehta | 0ee353f | 2019-05-30 17:40:29 -0700 | [diff] [blame] | 216 | bool addMethod(Method* method, Interface* iface); |
| 217 | bool addAllReservedMethodsToInterface(Interface* iface); |
| 218 | |
Steven Moreland | 4d89ee2 | 2019-03-08 13:25:32 -0800 | [diff] [blame] | 219 | void setHeader(const DocComment* header); |
| 220 | const DocComment* getHeader() const; |
| 221 | |
| 222 | // TODO: Clean up all interface usages of unhandled comments and ensure they are attached to the |
| 223 | // right element |
| 224 | void addUnhandledComment(const DocComment* docComment); |
| 225 | const std::vector<const DocComment*> getUnhandledComments() const; |
| 226 | |
Neel Mehta | 0ee353f | 2019-05-30 17:40:29 -0700 | [diff] [blame] | 227 | private: |
Steven Moreland | e6d7f09 | 2018-02-08 13:25:45 -0800 | [diff] [blame] | 228 | const Coordinator* mCoordinator; |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 229 | const Hash* mFileHash; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 230 | |
Steven Moreland | 0ecc7b8 | 2017-07-19 12:59:23 -0700 | [diff] [blame] | 231 | RootScope mRootScope; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 232 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 233 | FQName mPackage; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 234 | |
Steven Moreland | 4d89ee2 | 2019-03-08 13:25:32 -0800 | [diff] [blame] | 235 | // Header for the file |
| 236 | const DocComment* mHeader = nullptr; |
| 237 | |
| 238 | // A list of trailing DocComments. |
| 239 | std::vector<const DocComment*> mUnhandledComments; |
| 240 | |
Neel Mehta | 55c065e | 2019-05-31 13:30:12 -0700 | [diff] [blame] | 241 | // A list of the FQNames present in the import statements |
| 242 | std::vector<ImportStatement> mImportStatements; |
| 243 | |
Neel Mehta | 8b0f06a | 2019-07-11 18:13:21 -0700 | [diff] [blame] | 244 | // A list of FQNames that are imported implicitly |
| 245 | std::vector<FQName> mImplicitImports; |
| 246 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 247 | // A set of all external interfaces/types that are _actually_ referenced |
| 248 | // in this AST, this is a subset of those specified in import statements. |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 249 | // Note that this set only resolves to the granularity of either an |
| 250 | // interface type or a whole package. |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 251 | std::set<FQName> mImportedNames; |
| 252 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 253 | // This is the set of actually imported types. |
| 254 | std::set<FQName> mImportedNamesGranular; |
| 255 | |
Steven Moreland | 06a81cf | 2018-01-17 11:13:46 -0800 | [diff] [blame] | 256 | // Warning: this only includes names explicitly referenced in code. |
| 257 | // It does not include all names which are imported. |
| 258 | // |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 259 | // A set of all ASTs we explicitly or implicitly (types.hal) import. |
| 260 | std::set<AST *> mImportedASTs; |
| 261 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 262 | // If a single type (instead of the whole AST) is imported, the AST will be |
| 263 | // present as a key to this map, with the value being a list of types |
| 264 | // imported from this AST. If an AST appears in mImportedASTs but not in |
| 265 | // mImportedTypes, then the whole AST is imported. |
| 266 | std::map<AST *, std::set<Type *>> mImportedTypes; |
| 267 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 268 | // Types keyed by full names defined in this AST. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 269 | std::map<FQName, Type *> mDefinedTypesByFullName; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 270 | |
Neel Mehta | 0ee353f | 2019-05-30 17:40:29 -0700 | [diff] [blame] | 271 | // contains all the hidl reserved methods part of this AST |
| 272 | std::map<std::string, Method*> mAllReservedMethods; |
| 273 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 274 | // used by the parser. |
| 275 | size_t mSyntaxErrors = 0; |
| 276 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 277 | std::set<FQName> mReferencedTypeNames; |
| 278 | |
Neel Mehta | 8b0f06a | 2019-07-11 18:13:21 -0700 | [diff] [blame] | 279 | // importFQName will try to import fqName by parsing any file that might contain it |
| 280 | bool importFQName(const FQName& fqName); |
| 281 | |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 282 | // Helper functions for lookupType. |
Steven Moreland | 8f8e862 | 2019-11-04 12:38:38 -0800 | [diff] [blame] | 283 | Type* lookupTypeLocally(const FQName& fqName, const Scope* scope); |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 284 | status_t lookupAutofilledType(const FQName &fqName, Type **returnedType); |
| 285 | Type *lookupTypeFromImports(const FQName &fqName); |
| 286 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 287 | // Find a type matching fqName (which may be partial) and if found |
| 288 | // return the associated type and fill in the full "matchingName". |
| 289 | // Only types defined in this very AST are considered. |
| 290 | Type *findDefinedType(const FQName &fqName, FQName *matchingName) const; |
| 291 | |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 292 | std::string makeHeaderGuard(const std::string &baseName, |
| 293 | bool indicateGenerated = true) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 294 | void enterLeaveNamespace(Formatter &out, bool enter) const; |
| 295 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 296 | void generateTypeSource(Formatter& out, const std::string& ifaceName) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 297 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 298 | // a method, and in which interface is it originally defined. |
| 299 | // be careful of the case where method.isHidlReserved(), where interface |
| 300 | // is effectively useless. |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 301 | using MethodGenerator = std::function<void(const Method*, const Interface*)>; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 302 | |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 303 | void generateTemplatizationLink(Formatter& out) const; |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 304 | void generateCppTag(Formatter& out, const std::string& tag) const; |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 305 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 306 | void generateMethods(Formatter& out, const MethodGenerator& gen, |
| 307 | bool includeParents = true) const; |
| 308 | void generateStubImplMethod(Formatter& out, const std::string& className, |
| 309 | const Method* method) const; |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 310 | void generatePassthroughMethod(Formatter& out, const Method* method, const Interface* superInterface) const; |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 311 | void generateStaticProxyMethodSource(Formatter& out, const std::string& className, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 312 | const Method* method, const Interface* superInterface) const; |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 313 | void generateProxyMethodSource(Formatter& out, const std::string& className, |
| 314 | const Method* method, const Interface* superInterface) const; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 315 | void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 316 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 317 | void generateProxySource(Formatter& out, const FQName& fqName) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 318 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 319 | void generateStubSource(Formatter& out, const Interface* iface) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 320 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 321 | void generateStubSourceForMethod(Formatter& out, const Method* method, |
| 322 | const Interface* superInterface) const; |
| 323 | void generateStaticStubMethodSource(Formatter& out, const FQName& fqName, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 324 | const Method* method, const Interface* superInterface) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 325 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 326 | void generatePassthroughSource(Formatter& out) const; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 327 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 328 | void generateInterfaceSource(Formatter& out) const; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 329 | |
| 330 | enum InstrumentationEvent { |
| 331 | SERVER_API_ENTRY = 0, |
| 332 | SERVER_API_EXIT, |
| 333 | CLIENT_API_ENTRY, |
| 334 | CLIENT_API_EXIT, |
| 335 | SYNC_CALLBACK_ENTRY, |
| 336 | SYNC_CALLBACK_EXIT, |
| 337 | ASYNC_CALLBACK_ENTRY, |
| 338 | ASYNC_CALLBACK_EXIT, |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 339 | PASSTHROUGH_ENTRY, |
| 340 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 341 | }; |
| 342 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 343 | void generateCppAtraceCall( |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 344 | Formatter &out, |
| 345 | InstrumentationEvent event, |
| 346 | const Method *method) const; |
| 347 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 348 | void generateCppInstrumentationCall( |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 349 | Formatter &out, |
| 350 | InstrumentationEvent event, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 351 | const Method *method, |
| 352 | const Interface* superInterface) const; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 353 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 354 | void declareCppReaderLocals(Formatter& out, const std::vector<NamedReference<Type>*>& arg, |
| 355 | bool forResults) const; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 356 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 357 | void emitCppReaderWriter(Formatter& out, const std::string& parcelObj, bool parcelObjIsPointer, |
| 358 | const NamedReference<Type>* arg, bool isReader, Type::ErrorMode mode, |
| 359 | bool addPrefixToName) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 360 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 361 | void emitJavaReaderWriter(Formatter& out, const std::string& parcelObj, |
| 362 | const NamedReference<Type>* arg, bool isReader, |
| 363 | bool addPrefixToName) const; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 364 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 365 | void emitVtsTypeDeclarations(Formatter& out) const; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 366 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 367 | DISALLOW_COPY_AND_ASSIGN(AST); |
| 368 | }; |
| 369 | |
| 370 | } // namespace android |
| 371 | |
| 372 | #endif // AST_H_ |