blob: 10fa1567b787f796ba631418d3e97f4962abfa0c [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 Moreland7ae3d542017-01-18 16:46:01 -080022#include <hidl-util/FQName.h>
Steven Morelandd537ab02016-09-12 10:32:01 -070023#include <map>
Andreas Huber737080b2016-08-02 15:38:04 -070024#include <set>
Andreas Hubereb1081f2016-07-28 13:13:24 -070025#include <string>
Andreas Huber70a59e12016-08-16 12:57:01 -070026#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070028#include "Scope.h"
Andreas Huber881227d2016-08-02 14:20:21 -070029#include "Type.h"
Andreas Huberda51b8e2016-07-28 16:00:57 -070030
Andreas Huberc9410c72016-07-28 12:18:40 -070031namespace android {
32
Andreas Huber5345ec22016-07-29 13:33:27 -070033struct Coordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070034struct Formatter;
Andreas Huber6cb08cf2016-08-03 15:44:51 -070035struct Interface;
Yifan Honga4b53d02016-10-31 17:29:10 -070036struct Location;
Andreas Huber881227d2016-08-02 14:20:21 -070037struct Method;
Andreas Huber31629bc2016-08-03 09:06:40 -070038struct NamedType;
Andreas Huber881227d2016-08-02 14:20:21 -070039struct TypedVar;
Yifan Hongf24fa852016-09-23 11:03:15 -070040struct EnumValue;
Andreas Huberc9410c72016-07-28 12:18:40 -070041
42struct AST {
Steven Moreland28b9b532017-05-12 17:02:58 -070043 AST(const Coordinator *coordinator, const std::string &path);
Andreas Huberc9410c72016-07-28 12:18:40 -070044
Andreas Huber84f89de2016-07-28 15:39:51 -070045 bool setPackage(const char *package);
46 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070047
Andreas Hubera2723d22016-07-29 15:36:07 -070048 // package and version really.
49 FQName package() const;
Steven Moreland19f11b52017-05-12 18:22:21 -070050 bool isInterface() const;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070051 bool containsInterfaces() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070052
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070053 // Returns true iff successful.
54 bool addTypeDef(const char* localName, Type* type, const Location& location,
55 std::string* errorMsg, Scope* scope);
Andreas Huber5a545442016-08-03 10:44:56 -070056
57 // Returns true iff successful.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070058 bool addScopedType(NamedType* type, std::string* errorMsg, Scope* scope);
Andreas Huberc9410c72016-07-28 12:18:40 -070059
Andreas Huber0d0f9a22016-08-17 10:26:11 -070060 const std::string &getFilename() const;
61
Yifan Hongf24fa852016-09-23 11:03:15 -070062 // Look up an enum value by "FQName:valueName".
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070063 EnumValue* lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope);
Yifan Hongf24fa852016-09-23 11:03:15 -070064
Andreas Huber5345ec22016-07-29 13:33:27 -070065 // Look up a type by FQName, "pure" names, i.e. those without package
66 // or version are first looked up in the current scope chain.
67 // After that lookup proceeds to imports.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070068 Type* lookupType(const FQName& fqName, Scope* scope);
Andreas Huber5345ec22016-07-29 13:33:27 -070069
Andreas Huber39fa7182016-08-19 14:27:33 -070070 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070071
Andreas Huberb82318c2016-08-02 14:45:54 -070072 status_t generateCpp(const std::string &outputPath) const;
Steven Moreland1cbf0362017-05-09 14:32:53 -070073 status_t generateCppHeaders(const std::string &outputPath) const;
74 status_t generateCppSources(const std::string &outputPath) const;
Steven Moreland9c387612016-09-07 09:54:26 -070075 status_t generateCppImpl(const std::string &outputPath) const;
Steven Morelandf2a0f5b2017-07-18 20:39:11 -070076 status_t generateStubImplHeader(const std::string& outputPath) const;
77 status_t generateStubImplSource(const std::string& outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070078
Andreas Huber0fa9e392016-08-31 09:05:44 -070079 status_t generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -070080 const std::string &outputPath,
81 const std::string &limitToType) const;
Andreas Huber0fa9e392016-08-31 09:05:44 -070082
83 status_t generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070084 const std::string &outputPath,
85 const std::string &limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070086
Iliyan Malchev5bb14022016-08-09 15:04:39 -070087 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070088
Yifan Hong40a373d2016-11-30 15:16:47 -080089 // Run getImportedPackages on this, then run getImportedPackages on
90 // each AST in each package referenced in importSet.
91 void getImportedPackagesHierarchy(std::set<FQName> *importSet) const;
92
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070093 status_t generateVts(const std::string &outputPath) const;
94
Andreas Huber0fa9e392016-08-31 09:05:44 -070095 bool isJavaCompatible() const;
96
Iliyan Malcheved0509b2016-09-07 12:37:11 -070097 // Return the set of FQNames for those interfaces and types that are
98 // actually referenced in the AST, not merely imported.
Iliyan Malcheved0509b2016-09-07 12:37:11 -070099 const std::set<FQName>& getImportedNames() const {
100 return mImportedNames;
101 }
102
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800103 // Get transitive closure of imported interface/types.
104 void getAllImportedNames(std::set<FQName> *allImportSet) const;
105
Andreas Huber019d21d2016-10-03 12:59:47 -0700106 void appendToExportedTypesVector(
107 std::vector<const Type *> *exportedTypes) const;
108
Yifan Hongbe627b32016-10-28 18:38:56 -0700109 // used by the parser.
110 void addSyntaxError();
111 size_t syntaxErrors() const;
112
Yifan Hongc8934042016-11-17 17:10:52 -0800113 bool isIBase() const;
114
Steven Moreland19f11b52017-05-12 18:22:21 -0700115 // or nullptr if not isInterface
Yifan Hong78b38d12017-02-13 18:14:46 +0000116 const Interface *getInterface() const;
117
Steven Moreland19f11b52017-05-12 18:22:21 -0700118 // types or Interface base name (e.x. Foo)
119 std::string getBaseName() const;
120
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700121 Scope* getRootScope();
122
123 private:
Steven Moreland28b9b532017-05-12 17:02:58 -0700124 const Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700125 std::string mPath;
Andreas Huberc9410c72016-07-28 12:18:40 -0700126
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700127 RootScope mRootScope;
Andreas Huberc9410c72016-07-28 12:18:40 -0700128
Andreas Huberda51b8e2016-07-28 16:00:57 -0700129 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700130
Andreas Huber39fa7182016-08-19 14:27:33 -0700131 // A set of all external interfaces/types that are _actually_ referenced
132 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700133 std::set<FQName> mImportedNames;
134
Andreas Huber39fa7182016-08-19 14:27:33 -0700135 // A set of all ASTs we explicitly or implicitly (types.hal) import.
136 std::set<AST *> mImportedASTs;
137
Yifan Hong1977ea32016-10-05 12:49:08 -0700138 // If a single type (instead of the whole AST) is imported, the AST will be
139 // present as a key to this map, with the value being a list of types
140 // imported from this AST. If an AST appears in mImportedASTs but not in
141 // mImportedTypes, then the whole AST is imported.
142 std::map<AST *, std::set<Type *>> mImportedTypes;
143
Andreas Huber39fa7182016-08-19 14:27:33 -0700144 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700145 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700146
Yifan Hongbe627b32016-10-28 18:38:56 -0700147 // used by the parser.
148 size_t mSyntaxErrors = 0;
149
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700150 bool addScopedTypeInternal(NamedType* type, std::string* errorMsg, Scope* scope);
Andreas Huber39fa7182016-08-19 14:27:33 -0700151
Yifan Hong87ff8232017-01-09 12:07:05 -0800152 // Helper functions for lookupType.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700153 Type* lookupTypeLocally(const FQName& fqName, Scope* scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800154 status_t lookupAutofilledType(const FQName &fqName, Type **returnedType);
155 Type *lookupTypeFromImports(const FQName &fqName);
156
Andreas Huber39fa7182016-08-19 14:27:33 -0700157 // Find a type matching fqName (which may be partial) and if found
158 // return the associated type and fill in the full "matchingName".
159 // Only types defined in this very AST are considered.
160 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
161
Andreas Huber881227d2016-08-02 14:20:21 -0700162 void getPackageComponents(std::vector<std::string> *components) const;
163
164 void getPackageAndVersionComponents(
165 std::vector<std::string> *components, bool cpp_compatible) const;
166
Steven Morelandee88eed2016-10-31 17:49:00 -0700167 static void generateCppPackageInclude(
168 Formatter &out,
169 const FQName &package,
170 const std::string &klass);
171
Steven Moreland5708edf2016-11-04 15:33:31 +0000172 std::string makeHeaderGuard(const std::string &baseName,
173 bool indicateGenerated = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700174 void enterLeaveNamespace(Formatter &out, bool enter) const;
175
Steven Moreland67f67b42016-09-29 08:59:02 -0700176 static void generateCheckNonNull(Formatter &out, const std::string &nonNull);
177
Andreas Huberb82318c2016-08-02 14:45:54 -0700178 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700179 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700180 status_t generateStubHeader(const std::string &outputPath) const;
181 status_t generateProxyHeader(const std::string &outputPath) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700182 status_t generatePassthroughHeader(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700183
184 status_t generateTypeSource(
185 Formatter &out, const std::string &ifaceName) const;
186
Yifan Hong068c5522016-10-31 14:07:25 -0700187 // a method, and in which interface is it originally defined.
188 // be careful of the case where method.isHidlReserved(), where interface
189 // is effectively useless.
190 using MethodGenerator = std::function<status_t(const Method *, const Interface *)>;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700191
Steven Moreland0b843772017-06-23 16:33:38 -0700192 void generateTemplatizationLink(Formatter& out) const;
Steven Moreland1a52e822017-07-27 13:56:29 -0700193 void generateCppTag(Formatter& out, const std::string& tag) const;
Steven Moreland0b843772017-06-23 16:33:38 -0700194
Yifan Hong068c5522016-10-31 14:07:25 -0700195 status_t generateMethods(Formatter &out, MethodGenerator gen) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700196 status_t generateStubImplMethod(Formatter &out,
197 const std::string &className,
Yifan Hong068c5522016-10-31 14:07:25 -0700198 const Method *method) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700199 status_t generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700200 const Method *method) const;
201 status_t generateProxyMethodSource(Formatter &out,
Steven Moreland69e7c702016-09-09 11:16:32 -0700202 const std::string &className,
203 const Method *method,
Yifan Hong068c5522016-10-31 14:07:25 -0700204 const Interface *superInterface) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700205
206 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700207
Andreas Huber881227d2016-08-02 14:20:21 -0700208 status_t generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -0800209 Formatter &out, const FQName &fqName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700210
211 status_t generateStubSource(
Yifan Hongeefe4f22017-01-04 15:32:42 -0800212 Formatter &out, const Interface *iface) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700213
214 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700215 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700216
Steven Moreland69e7c702016-09-09 11:16:32 -0700217 status_t generatePassthroughSource(Formatter &out) const;
218
Yifan Hongfe95aa22016-10-19 17:26:45 -0700219 status_t generateInterfaceSource(Formatter &out) const;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700220
221 enum InstrumentationEvent {
222 SERVER_API_ENTRY = 0,
223 SERVER_API_EXIT,
224 CLIENT_API_ENTRY,
225 CLIENT_API_EXIT,
226 SYNC_CALLBACK_ENTRY,
227 SYNC_CALLBACK_EXIT,
228 ASYNC_CALLBACK_ENTRY,
229 ASYNC_CALLBACK_EXIT,
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700230 PASSTHROUGH_ENTRY,
231 PASSTHROUGH_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700232 };
233
Martijn Coenen7b295242016-11-04 16:52:56 +0100234 status_t generateCppAtraceCall(
235 Formatter &out,
236 InstrumentationEvent event,
237 const Method *method) const;
238
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700239 status_t generateCppInstrumentationCall(
240 Formatter &out,
241 InstrumentationEvent event,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700242 const Method *method) const;
243
Andreas Hubere7ff2282016-08-16 13:50:03 -0700244 void declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -0700245 Formatter &out,
246 const std::vector<TypedVar *> &arg,
247 bool forResults) const;
Andreas Hubere7ff2282016-08-16 13:50:03 -0700248
Andreas Huber881227d2016-08-02 14:20:21 -0700249 void emitCppReaderWriter(
250 Formatter &out,
251 const std::string &parcelObj,
252 bool parcelObjIsPointer,
253 const TypedVar *arg,
254 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -0700255 Type::ErrorMode mode,
256 bool addPrefixToName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700257
Yifan Hongbf459bc2016-08-23 16:50:37 -0700258 void emitCppResolveReferences(
259 Formatter &out,
260 const std::string &parcelObj,
261 bool parcelObjIsPointer,
262 const TypedVar *arg,
263 bool isReader,
264 Type::ErrorMode mode,
265 bool addPrefixToName) const;
266
Andreas Huber2831d512016-08-15 09:33:47 -0700267 void emitJavaReaderWriter(
268 Formatter &out,
269 const std::string &parcelObj,
270 const TypedVar *arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800271 bool isReader,
272 bool addPrefixToName) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700273
Andreas Huber881227d2016-08-02 14:20:21 -0700274 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700275 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700276 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700277
Andreas Huberc9410c72016-07-28 12:18:40 -0700278 DISALLOW_COPY_AND_ASSIGN(AST);
279};
280
281} // namespace android
282
283#endif // AST_H_