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 | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 22 | #include <map> |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 23 | #include <set> |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 24 | #include <string> |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 25 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 26 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 27 | #include "FQName.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 28 | #include "Type.h" |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 29 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 32 | struct Coordinator; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | struct Formatter; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 34 | struct Interface; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 35 | struct Method; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 36 | struct NamedType; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 37 | struct TypedVar; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 38 | struct Scope; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 39 | struct EnumValue; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 40 | |
| 41 | struct AST { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 42 | AST(Coordinator *coordinator, const std::string &path); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | ~AST(); |
| 44 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 45 | bool setPackage(const char *package); |
| 46 | bool addImport(const char *import); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 47 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 48 | // package and version really. |
| 49 | FQName package() const; |
| 50 | bool isInterface(std::string *ifaceName) const; |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 51 | bool containsInterfaces() const; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 52 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 53 | void enterScope(Scope *container); |
| 54 | void leaveScope(); |
| 55 | Scope *scope(); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 56 | |
| 57 | // Returns true iff successful. |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 58 | bool addTypeDef(const char *localName, Type *type, std::string *errorMsg); |
| 59 | |
| 60 | // Returns true iff successful. |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 61 | bool addScopedType(NamedType *type, std::string *errorMsg); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 62 | |
| 63 | void *scanner(); |
| 64 | void setScanner(void *scanner); |
| 65 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 66 | const std::string &getFilename() const; |
| 67 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 68 | // Look up an enum value by "FQName:valueName". |
| 69 | EnumValue *lookupEnumValue(const FQName &fqName, std::string *errorMsg); |
| 70 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 71 | // Look up a type by FQName, "pure" names, i.e. those without package |
| 72 | // or version are first looked up in the current scope chain. |
| 73 | // After that lookup proceeds to imports. |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 74 | Type *lookupType(const FQName &fqName); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 75 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 76 | void addImportedAST(AST *ast); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 77 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 78 | status_t generateCpp(const std::string &outputPath) const; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 79 | status_t generateCppImpl(const std::string &outputPath) const; |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 80 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 81 | status_t generateJava( |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 82 | const std::string &outputPath, |
| 83 | const std::string &limitToType) const; |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 84 | |
| 85 | status_t generateJavaTypes( |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 86 | const std::string &outputPath, |
| 87 | const std::string &limitToType) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 88 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 89 | void getImportedPackages(std::set<FQName> *importSet) const; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 90 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 91 | status_t generateVts(const std::string &outputPath) const; |
| 92 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 93 | bool isJavaCompatible() const; |
| 94 | |
Iliyan Malchev | ed0509b | 2016-09-07 12:37:11 -0700 | [diff] [blame] | 95 | // Return the set of FQNames for those interfaces and types that are |
| 96 | // actually referenced in the AST, not merely imported. |
| 97 | |
| 98 | const std::set<FQName>& getImportedNames() const { |
| 99 | return mImportedNames; |
| 100 | } |
| 101 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 102 | void appendToExportedTypesVector( |
| 103 | std::vector<const Type *> *exportedTypes) const; |
| 104 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 105 | private: |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 106 | Coordinator *mCoordinator; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 107 | std::string mPath; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 108 | std::vector<Scope *> mScopePath; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 109 | |
| 110 | void *mScanner; |
| 111 | Scope *mRootScope; |
| 112 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 113 | FQName mPackage; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 114 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 115 | // A set of all external interfaces/types that are _actually_ referenced |
| 116 | // in this AST, this is a subset of those specified in import statements. |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 117 | std::set<FQName> mImportedNames; |
| 118 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 119 | // Similar to mImportedNames, but all types references from "types.hal" |
| 120 | // are individually listed. |
| 121 | std::set<FQName> mImportedNamesForJava; |
| 122 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 123 | // A set of all ASTs we explicitly or implicitly (types.hal) import. |
| 124 | std::set<AST *> mImportedASTs; |
| 125 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 126 | // If a single type (instead of the whole AST) is imported, the AST will be |
| 127 | // present as a key to this map, with the value being a list of types |
| 128 | // imported from this AST. If an AST appears in mImportedASTs but not in |
| 129 | // mImportedTypes, then the whole AST is imported. |
| 130 | std::map<AST *, std::set<Type *>> mImportedTypes; |
| 131 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 132 | // Types keyed by full names defined in this AST. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 133 | std::map<FQName, Type *> mDefinedTypesByFullName; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 134 | |
| 135 | bool addScopedTypeInternal( |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 136 | NamedType *type, |
| 137 | std::string *errorMsg); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 138 | |
| 139 | // Find a type matching fqName (which may be partial) and if found |
| 140 | // return the associated type and fill in the full "matchingName". |
| 141 | // Only types defined in this very AST are considered. |
| 142 | Type *findDefinedType(const FQName &fqName, FQName *matchingName) const; |
| 143 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 144 | void getPackageComponents(std::vector<std::string> *components) const; |
| 145 | |
| 146 | void getPackageAndVersionComponents( |
| 147 | std::vector<std::string> *components, bool cpp_compatible) const; |
| 148 | |
| 149 | std::string makeHeaderGuard(const std::string &baseName) const; |
| 150 | void enterLeaveNamespace(Formatter &out, bool enter) const; |
| 151 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 152 | static void generateCheckNonNull(Formatter &out, const std::string &nonNull); |
| 153 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 154 | status_t generateInterfaceHeader(const std::string &outputPath) const; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 155 | status_t generateHwBinderHeader(const std::string &outputPath) const; |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 156 | status_t generateStubHeader(const std::string &outputPath) const; |
| 157 | status_t generateProxyHeader(const std::string &outputPath) const; |
| 158 | status_t generateAllSource(const std::string &outputPath) const; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 159 | status_t generatePassthroughHeader(const std::string &outputPath) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 160 | |
| 161 | status_t generateTypeSource( |
| 162 | Formatter &out, const std::string &ifaceName) const; |
| 163 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 164 | enum MethodLocation { |
| 165 | PROXY_HEADER, |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 166 | STUB_HEADER, |
| 167 | IMPL_HEADER, |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 168 | IMPL_SOURCE, |
| 169 | PASSTHROUGH_HEADER |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 172 | status_t generateStubImplHeader(const std::string &outputPath) const; |
| 173 | status_t generateStubImplSource(const std::string &outputPath) const; |
| 174 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 175 | status_t generateMethods(Formatter &out, |
| 176 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 177 | MethodLocation type, |
| 178 | bool specifyNamespaces) const; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 179 | status_t generateStubMethod(Formatter &out, |
| 180 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 181 | const Method *method, |
| 182 | bool specifyNamespaces) const; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 183 | status_t generateProxyDeclaration(Formatter &out, |
| 184 | const std::string &className, |
| 185 | const Method *method, |
| 186 | bool specifyNamespaces) const; |
| 187 | status_t generateStubImplDeclaration(Formatter &out, |
| 188 | const std::string &className, |
| 189 | const Method *method, |
| 190 | bool specifyNamespaces) const; |
| 191 | status_t generateStubImplMethod(Formatter &out, |
| 192 | const std::string &className, |
| 193 | const Method *method, |
| 194 | bool specifyNamespaces) const; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 195 | status_t generatePassthroughMethod(Formatter &out, |
| 196 | const std::string &className, |
| 197 | const Method *method, |
| 198 | bool specifyNamespaces) const; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 199 | |
| 200 | void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 201 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 202 | status_t generateProxySource( |
| 203 | Formatter &out, const std::string &baseName) const; |
| 204 | |
| 205 | status_t generateStubSource( |
| 206 | Formatter &out, const std::string &baseName) const; |
| 207 | |
| 208 | status_t generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 209 | Formatter &out, const Interface *iface, const Method *method) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 210 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 211 | status_t generatePassthroughSource(Formatter &out) const; |
| 212 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 213 | |
| 214 | enum InstrumentationEvent { |
| 215 | SERVER_API_ENTRY = 0, |
| 216 | SERVER_API_EXIT, |
| 217 | CLIENT_API_ENTRY, |
| 218 | CLIENT_API_EXIT, |
| 219 | SYNC_CALLBACK_ENTRY, |
| 220 | SYNC_CALLBACK_EXIT, |
| 221 | ASYNC_CALLBACK_ENTRY, |
| 222 | ASYNC_CALLBACK_EXIT, |
| 223 | }; |
| 224 | |
| 225 | status_t generateCppInstrumentationCall( |
| 226 | Formatter &out, |
| 227 | InstrumentationEvent event, |
| 228 | const Interface *iface, |
| 229 | const Method *method) const; |
| 230 | |
| 231 | void emitCppInstrumentationDecl(Formatter &out) const; |
| 232 | |
| 233 | void emitCppInstrumentationInit( |
| 234 | Formatter &out, |
| 235 | const std::string &baseName) const; |
| 236 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 237 | void declareCppReaderLocals( |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 238 | Formatter &out, |
| 239 | const std::vector<TypedVar *> &arg, |
| 240 | bool forResults) const; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 241 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 242 | void emitCppReaderWriter( |
| 243 | Formatter &out, |
| 244 | const std::string &parcelObj, |
| 245 | bool parcelObjIsPointer, |
| 246 | const TypedVar *arg, |
| 247 | bool isReader, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 248 | Type::ErrorMode mode, |
| 249 | bool addPrefixToName) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 250 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 251 | void emitCppResolveReferences( |
| 252 | Formatter &out, |
| 253 | const std::string &parcelObj, |
| 254 | bool parcelObjIsPointer, |
| 255 | const TypedVar *arg, |
| 256 | bool isReader, |
| 257 | Type::ErrorMode mode, |
| 258 | bool addPrefixToName) const; |
| 259 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 260 | void emitJavaReaderWriter( |
| 261 | Formatter &out, |
| 262 | const std::string &parcelObj, |
| 263 | const TypedVar *arg, |
| 264 | bool isReader) const; |
| 265 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 266 | status_t emitTypeDeclarations(Formatter &out) const; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 267 | status_t emitJavaTypeDeclarations(Formatter &out) const; |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 268 | status_t emitVtsTypeDeclarations(Formatter &out) const; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 269 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 270 | // Helper function that generates vts type declaration from the current |
| 271 | // AST and the transitive closure of imported ASTs. |
| 272 | status_t emitVtsTypeDeclarationsHelper( |
| 273 | Formatter &out, |
| 274 | std::set<AST*> *allImportSet) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 275 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 276 | DISALLOW_COPY_AND_ASSIGN(AST); |
| 277 | }; |
| 278 | |
| 279 | } // namespace android |
| 280 | |
| 281 | #endif // AST_H_ |