blob: 3e8e5a51a5f4157c968a2ed342efc28897847d42 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huberc9410c72016-07-28 12:18:40 -070017#ifndef AST_H_
18
19#define AST_H_
20
21#include <android-base/macros.h>
Steven Moreland04dea8d2018-02-06 13:11:24 -080022#include <hidl-hash/Hash.h>
Steven Moreland7ae3d542017-01-18 16:46:01 -080023#include <hidl-util/FQName.h>
Timur Iskhakov891a8662017-08-25 21:53:48 -070024#include <functional>
Steven Morelandd537ab02016-09-12 10:32:01 -070025#include <map>
Andreas Huber737080b2016-08-02 15:38:04 -070026#include <set>
Andreas Hubereb1081f2016-07-28 13:13:24 -070027#include <string>
Andreas Huber70a59e12016-08-16 12:57:01 -070028#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070029
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070030#include "Scope.h"
Andreas Huber881227d2016-08-02 14:20:21 -070031#include "Type.h"
Andreas Huberda51b8e2016-07-28 16:00:57 -070032
Andreas Huberc9410c72016-07-28 12:18:40 -070033namespace android {
34
Andreas Huber5345ec22016-07-29 13:33:27 -070035struct Coordinator;
Timur Iskhakov891a8662017-08-25 21:53:48 -070036struct ConstantExpression;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070037struct EnumValue;
Andreas Huberc9410c72016-07-28 12:18:40 -070038struct Formatter;
Andreas Huber6cb08cf2016-08-03 15:44:51 -070039struct Interface;
Yifan Honga4b53d02016-10-31 17:29:10 -070040struct Location;
Andreas Huber881227d2016-08-02 14:20:21 -070041struct Method;
Andreas Huber31629bc2016-08-03 09:06:40 -070042struct NamedType;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070043template <class T>
44struct NamedReference;
45struct Type;
Andreas Huberc9410c72016-07-28 12:18:40 -070046
47struct AST {
Steven Moreland04dea8d2018-02-06 13:11:24 -080048 AST(const Coordinator* coordinator, const Hash* fileHash);
Andreas Huberc9410c72016-07-28 12:18:40 -070049
Andreas Huber84f89de2016-07-28 15:39:51 -070050 bool setPackage(const char *package);
51 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070052
Andreas Hubera2723d22016-07-29 15:36:07 -070053 // package and version really.
54 FQName package() const;
Steven Moreland19f11b52017-05-12 18:22:21 -070055 bool isInterface() const;
Steven Morelandb47a2622018-07-11 09:04:25 -070056 bool definesInterfaces() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070057
Timur Iskhakov565b0132017-09-06 18:07:11 -070058 // Adds package, version and scope stack to local name
59 FQName makeFullName(const char* localName, Scope* scope) const;
60
61 void addScopedType(NamedType* type, Scope* scope);
Andreas Huberc9410c72016-07-28 12:18:40 -070062
Steven Moreland04dea8d2018-02-06 13:11:24 -080063 const std::string& getFilename() const;
64 const Hash* getFileHash() const;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070065
Timur Iskhakov82c048e2017-09-09 01:20:53 -070066 // Look up local identifier.
67 // It could be plain identifier or enum value as described by lookupEnumValue.
68 LocalIdentifier* lookupLocalIdentifier(const Reference<LocalIdentifier>& ref, Scope* scope);
69
Yifan Hongf24fa852016-09-23 11:03:15 -070070 // Look up an enum value by "FQName:valueName".
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070071 EnumValue* lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope);
Yifan Hongf24fa852016-09-23 11:03:15 -070072
Andreas Huber5345ec22016-07-29 13:33:27 -070073 // Look up a type by FQName, "pure" names, i.e. those without package
74 // or version are first looked up in the current scope chain.
75 // After that lookup proceeds to imports.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070076 Type* lookupType(const FQName& fqName, Scope* scope);
Andreas Huber5345ec22016-07-29 13:33:27 -070077
Andreas Huber39fa7182016-08-19 14:27:33 -070078 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070079
Timur Iskhakov33431e62017-08-21 17:31:23 -070080 // Calls all passes after parsing required before
81 // being ready to generate output.
82 status_t postParse();
83
Timur Iskhakov891a8662017-08-25 21:53:48 -070084 // Recursive pass on constant expression tree
85 status_t constantExpressionRecursivePass(
Timur Iskhakov82c048e2017-09-09 01:20:53 -070086 const std::function<status_t(ConstantExpression*)>& func, bool processBeforeDependencies);
87
88 // Recursive tree pass that looks up all referenced types
89 status_t lookupTypes();
90
91 // Recursive tree pass that looks up all referenced local identifiers
92 status_t lookupLocalIdentifiers();
Timur Iskhakov891a8662017-08-25 21:53:48 -070093
Timur Iskhakov565b0132017-09-06 18:07:11 -070094 // Recursive tree pass that validates that all defined types
95 // have unique names in their scopes.
96 status_t validateDefinedTypesUniqueNames() const;
97
Timur Iskhakovcec46c42017-08-09 00:22:02 -070098 // Recursive tree pass that completes type declarations
99 // that depend on super types
100 status_t resolveInheritance();
101
102 // Recursive tree pass that evaluates constant expressions
103 status_t evaluate();
104
105 // Recursive tree pass that validates all type-related
106 // syntax restrictions
107 status_t validate() const;
108
Timur Iskhakov40731af2017-08-24 14:18:35 -0700109 // Recursive tree pass that ensures that type definitions and references
Timur Iskhakov458ca362017-09-12 23:16:03 -0700110 // are acyclic and reorderes type definitions in reversed topological order.
111 status_t topologicalReorder();
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700112
113 // Recursive tree pass that ensures that constant expressions
114 // are acyclic.
115 status_t checkAcyclicConstantExpressions() const;
Timur Iskhakov40731af2017-08-24 14:18:35 -0700116
Timur Iskhakov041fdfe2017-09-06 15:56:01 -0700117 // Recursive tree pass that checks C++ forward declaration restrictions.
118 status_t checkForwardReferenceRestrictions() const;
119
Andreas Huber308d8a22017-11-06 14:46:52 -0800120 status_t gatherReferencedTypes();
121
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800122 void generateCppSource(Formatter& out) const;
Steven Moreland6d688552017-09-15 11:03:02 -0700123
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800124 void generateInterfaceHeader(Formatter& out) const;
125 void generateHwBinderHeader(Formatter& out) const;
126 void generateStubHeader(Formatter& out) const;
127 void generateProxyHeader(Formatter& out) const;
128 void generatePassthroughHeader(Formatter& out) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700129
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800130 void generateCppImplHeader(Formatter& out) const;
131 void generateCppImplSource(Formatter& out) const;
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700132
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800133 void generateCppAdapterHeader(Formatter& out) const;
134 void generateCppAdapterSource(Formatter& out) const;
Steven Moreland5abcf012018-02-08 18:50:18 -0800135
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800136 void generateJava(Formatter& out, const std::string& limitToType) const;
137 void generateJavaTypes(Formatter& out, const std::string& limitToType) const;
Steven Moreland5abcf012018-02-08 18:50:18 -0800138
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800139 void generateVts(Formatter& out) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700140
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700141 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -0700142
Yifan Hong40a373d2016-11-30 15:16:47 -0800143 // Run getImportedPackages on this, then run getImportedPackages on
144 // each AST in each package referenced in importSet.
145 void getImportedPackagesHierarchy(std::set<FQName> *importSet) const;
146
Andreas Huber0fa9e392016-08-31 09:05:44 -0700147 bool isJavaCompatible() const;
148
Steven Moreland06a81cf2018-01-17 11:13:46 -0800149 // Warning: this only includes names explicitly referenced in code.
150 // It does not include all names which are imported.
151 //
152 // Currently, there is one valid usecase for this: importing exactly
153 // the names which need to be imported in generated code. If you import
154 // based on getAllImportedNamesGranular instead, you will import things
155 // that aren't actually used in the resultant code.
156 //
Andreas Huber4ba5c972017-11-29 11:06:25 -0800157 // Get transitive closure of imported interface/types. This will add
158 // everything exported by a package even if only a single type from
159 // that package was explicitly imported!
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800160 void getAllImportedNames(std::set<FQName> *allImportSet) const;
161
Andreas Huber4ba5c972017-11-29 11:06:25 -0800162 // Get imported types, this includes those explicitly imported as well
163 // as all types defined in imported packages.
164 void getAllImportedNamesGranular(std::set<FQName> *allImportSet) const;
165
Andreas Huber019d21d2016-10-03 12:59:47 -0700166 void appendToExportedTypesVector(
167 std::vector<const Type *> *exportedTypes) const;
168
Yifan Hongbe627b32016-10-28 18:38:56 -0700169 // used by the parser.
170 void addSyntaxError();
171 size_t syntaxErrors() const;
172
Yifan Hongc8934042016-11-17 17:10:52 -0800173 bool isIBase() const;
174
Steven Moreland19f11b52017-05-12 18:22:21 -0700175 // or nullptr if not isInterface
Yifan Hong78b38d12017-02-13 18:14:46 +0000176 const Interface *getInterface() const;
177
Steven Moreland19f11b52017-05-12 18:22:21 -0700178 // types or Interface base name (e.x. Foo)
179 std::string getBaseName() const;
180
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700181 Scope* getRootScope();
182
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700183 static void generateCppPackageInclude(Formatter& out, const FQName& package,
184 const std::string& klass);
185
Andreas Huber308d8a22017-11-06 14:46:52 -0800186 void addDefinedTypes(std::set<FQName> *definedTypes) const;
187 void addReferencedTypes(std::set<FQName> *referencedTypes) const;
188
Andreas Huber4ba5c972017-11-29 11:06:25 -0800189 void addToImportedNamesGranular(const FQName &fqName);
190
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700191 private:
Steven Morelande6d7f092018-02-08 13:25:45 -0800192 const Coordinator* mCoordinator;
Steven Moreland04dea8d2018-02-06 13:11:24 -0800193 const Hash* mFileHash;
Andreas Huberc9410c72016-07-28 12:18:40 -0700194
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700195 RootScope mRootScope;
Andreas Huberc9410c72016-07-28 12:18:40 -0700196
Andreas Huberda51b8e2016-07-28 16:00:57 -0700197 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700198
Andreas Huber39fa7182016-08-19 14:27:33 -0700199 // A set of all external interfaces/types that are _actually_ referenced
200 // in this AST, this is a subset of those specified in import statements.
Andreas Huber4ba5c972017-11-29 11:06:25 -0800201 // Note that this set only resolves to the granularity of either an
202 // interface type or a whole package.
Andreas Huber737080b2016-08-02 15:38:04 -0700203 std::set<FQName> mImportedNames;
204
Andreas Huber4ba5c972017-11-29 11:06:25 -0800205 // This is the set of actually imported types.
206 std::set<FQName> mImportedNamesGranular;
207
Steven Moreland06a81cf2018-01-17 11:13:46 -0800208 // Warning: this only includes names explicitly referenced in code.
209 // It does not include all names which are imported.
210 //
Andreas Huber39fa7182016-08-19 14:27:33 -0700211 // A set of all ASTs we explicitly or implicitly (types.hal) import.
212 std::set<AST *> mImportedASTs;
213
Yifan Hong1977ea32016-10-05 12:49:08 -0700214 // If a single type (instead of the whole AST) is imported, the AST will be
215 // present as a key to this map, with the value being a list of types
216 // imported from this AST. If an AST appears in mImportedASTs but not in
217 // mImportedTypes, then the whole AST is imported.
218 std::map<AST *, std::set<Type *>> mImportedTypes;
219
Andreas Huber39fa7182016-08-19 14:27:33 -0700220 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700221 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700222
Yifan Hongbe627b32016-10-28 18:38:56 -0700223 // used by the parser.
224 size_t mSyntaxErrors = 0;
225
Andreas Huber308d8a22017-11-06 14:46:52 -0800226 std::set<FQName> mReferencedTypeNames;
227
Yifan Hong87ff8232017-01-09 12:07:05 -0800228 // Helper functions for lookupType.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700229 Type* lookupTypeLocally(const FQName& fqName, Scope* scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800230 status_t lookupAutofilledType(const FQName &fqName, Type **returnedType);
231 Type *lookupTypeFromImports(const FQName &fqName);
232
Andreas Huber39fa7182016-08-19 14:27:33 -0700233 // Find a type matching fqName (which may be partial) and if found
234 // return the associated type and fill in the full "matchingName".
235 // Only types defined in this very AST are considered.
236 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
237
Andreas Huber881227d2016-08-02 14:20:21 -0700238 void getPackageComponents(std::vector<std::string> *components) const;
239
240 void getPackageAndVersionComponents(
241 std::vector<std::string> *components, bool cpp_compatible) const;
242
Steven Moreland5708edf2016-11-04 15:33:31 +0000243 std::string makeHeaderGuard(const std::string &baseName,
244 bool indicateGenerated = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700245 void enterLeaveNamespace(Formatter &out, bool enter) const;
246
Steven Moreland67f67b42016-09-29 08:59:02 -0700247 static void generateCheckNonNull(Formatter &out, const std::string &nonNull);
248
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800249 void generateTypeSource(Formatter& out, const std::string& ifaceName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700250
Yifan Hong068c5522016-10-31 14:07:25 -0700251 // a method, and in which interface is it originally defined.
252 // be careful of the case where method.isHidlReserved(), where interface
253 // is effectively useless.
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800254 using MethodGenerator = std::function<void(const Method*, const Interface*)>;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700255
Steven Moreland0b843772017-06-23 16:33:38 -0700256 void generateTemplatizationLink(Formatter& out) const;
Steven Moreland1a52e822017-07-27 13:56:29 -0700257 void generateCppTag(Formatter& out, const std::string& tag) const;
Steven Moreland0b843772017-06-23 16:33:38 -0700258
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800259 void generateMethods(Formatter& out, const MethodGenerator& gen,
260 bool includeParents = true) const;
261 void generateStubImplMethod(Formatter& out, const std::string& className,
262 const Method* method) const;
263 void generatePassthroughMethod(Formatter& out, const Method* method) const;
264 void generateStaticProxyMethodSource(Formatter& out, const std::string& className,
265 const Method* method) const;
266 void generateProxyMethodSource(Formatter& out, const std::string& className,
267 const Method* method, const Interface* superInterface) const;
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700268 void generateAdapterMethod(Formatter& out, const Method* method) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700269
270 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700271
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800272 void generateProxySource(Formatter& out, const FQName& fqName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700273
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800274 void generateStubSource(Formatter& out, const Interface* iface) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700275
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800276 void generateStubSourceForMethod(Formatter& out, const Method* method,
277 const Interface* superInterface) const;
278 void generateStaticStubMethodSource(Formatter& out, const FQName& fqName,
279 const Method* method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700280
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800281 void generatePassthroughSource(Formatter& out) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700282
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800283 void generateInterfaceSource(Formatter& out) const;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700284
285 enum InstrumentationEvent {
286 SERVER_API_ENTRY = 0,
287 SERVER_API_EXIT,
288 CLIENT_API_ENTRY,
289 CLIENT_API_EXIT,
290 SYNC_CALLBACK_ENTRY,
291 SYNC_CALLBACK_EXIT,
292 ASYNC_CALLBACK_ENTRY,
293 ASYNC_CALLBACK_EXIT,
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700294 PASSTHROUGH_ENTRY,
295 PASSTHROUGH_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700296 };
297
Steven Moreland92a08a72017-07-31 14:57:37 -0700298 void generateCppAtraceCall(
Martijn Coenen7b295242016-11-04 16:52:56 +0100299 Formatter &out,
300 InstrumentationEvent event,
301 const Method *method) const;
302
Steven Moreland92a08a72017-07-31 14:57:37 -0700303 void generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700304 Formatter &out,
305 InstrumentationEvent event,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700306 const Method *method) const;
307
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700308 void declareCppReaderLocals(Formatter& out, const std::vector<NamedReference<Type>*>& arg,
309 bool forResults) const;
Andreas Hubere7ff2282016-08-16 13:50:03 -0700310
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700311 void emitCppReaderWriter(Formatter& out, const std::string& parcelObj, bool parcelObjIsPointer,
312 const NamedReference<Type>* arg, bool isReader, Type::ErrorMode mode,
313 bool addPrefixToName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700314
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700315 void emitCppResolveReferences(Formatter& out, const std::string& parcelObj,
316 bool parcelObjIsPointer, const NamedReference<Type>* arg,
317 bool isReader, Type::ErrorMode mode, bool addPrefixToName) const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700318
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700319 void emitJavaReaderWriter(Formatter& out, const std::string& parcelObj,
320 const NamedReference<Type>* arg, bool isReader,
321 bool addPrefixToName) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700322
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800323 void emitTypeDeclarations(Formatter& out) const;
324 void emitJavaTypeDeclarations(Formatter& out) const;
325 void emitVtsTypeDeclarations(Formatter& out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700326
Andreas Huberc9410c72016-07-28 12:18:40 -0700327 DISALLOW_COPY_AND_ASSIGN(AST);
328};
329
330} // namespace android
331
332#endif // AST_H_