blob: 83502120fd925a3f9761507bbcd5e4c3500bcda8 [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
Andreas Huber881227d2016-08-02 14:20:21 -070028#include "Type.h"
Andreas Huberda51b8e2016-07-28 16:00:57 -070029
Andreas Huberc9410c72016-07-28 12:18:40 -070030namespace android {
31
Andreas Huber5345ec22016-07-29 13:33:27 -070032struct Coordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070033struct Formatter;
Andreas Huber6cb08cf2016-08-03 15:44:51 -070034struct Interface;
Yifan Honga4b53d02016-10-31 17:29:10 -070035struct Location;
Andreas Huber881227d2016-08-02 14:20:21 -070036struct Method;
Andreas Huber31629bc2016-08-03 09:06:40 -070037struct NamedType;
Andreas Huber881227d2016-08-02 14:20:21 -070038struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070039struct Scope;
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 ~AST();
45
Andreas Huber84f89de2016-07-28 15:39:51 -070046 bool setPackage(const char *package);
47 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070048
Andreas Hubera2723d22016-07-29 15:36:07 -070049 // package and version really.
50 FQName package() const;
Steven Moreland19f11b52017-05-12 18:22:21 -070051 bool isInterface() const;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070052 bool containsInterfaces() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070053
Andreas Huberc9410c72016-07-28 12:18:40 -070054 void enterScope(Scope *container);
55 void leaveScope();
56 Scope *scope();
Andreas Huber5a545442016-08-03 10:44:56 -070057
58 // Returns true iff successful.
Yifan Honga4b53d02016-10-31 17:29:10 -070059 bool addTypeDef(const char *localName, Type *type, const Location &location,
60 std::string *errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -070061
62 // Returns true iff successful.
Andreas Huber9ed827c2016-08-22 12:31:13 -070063 bool addScopedType(NamedType *type, std::string *errorMsg);
Andreas Huberc9410c72016-07-28 12:18:40 -070064
65 void *scanner();
66 void setScanner(void *scanner);
67
Andreas Huber0d0f9a22016-08-17 10:26:11 -070068 const std::string &getFilename() const;
69
Yifan Hongf24fa852016-09-23 11:03:15 -070070 // Look up an enum value by "FQName:valueName".
71 EnumValue *lookupEnumValue(const FQName &fqName, std::string *errorMsg);
72
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.
Yifan Hongae16eed2016-09-23 13:25:25 -070076 Type *lookupType(const FQName &fqName);
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
Andreas Huberb82318c2016-08-02 14:45:54 -070080 status_t generateCpp(const std::string &outputPath) const;
Steven Moreland1cbf0362017-05-09 14:32:53 -070081 status_t generateCppHeaders(const std::string &outputPath) const;
82 status_t generateCppSources(const std::string &outputPath) const;
Steven Moreland9c387612016-09-07 09:54:26 -070083 status_t generateCppImpl(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070084
Andreas Huber0fa9e392016-08-31 09:05:44 -070085 status_t generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -070086 const std::string &outputPath,
87 const std::string &limitToType) const;
Andreas Huber0fa9e392016-08-31 09:05:44 -070088
89 status_t generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070090 const std::string &outputPath,
91 const std::string &limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070092
Iliyan Malchev5bb14022016-08-09 15:04:39 -070093 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070094
Yifan Hong40a373d2016-11-30 15:16:47 -080095 // Run getImportedPackages on this, then run getImportedPackages on
96 // each AST in each package referenced in importSet.
97 void getImportedPackagesHierarchy(std::set<FQName> *importSet) const;
98
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070099 status_t generateVts(const std::string &outputPath) const;
100
Andreas Huber0fa9e392016-08-31 09:05:44 -0700101 bool isJavaCompatible() const;
102
Iliyan Malcheved0509b2016-09-07 12:37:11 -0700103 // Return the set of FQNames for those interfaces and types that are
104 // actually referenced in the AST, not merely imported.
Iliyan Malcheved0509b2016-09-07 12:37:11 -0700105 const std::set<FQName>& getImportedNames() const {
106 return mImportedNames;
107 }
108
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800109 // Get transitive closure of imported interface/types.
110 void getAllImportedNames(std::set<FQName> *allImportSet) const;
111
Andreas Huber019d21d2016-10-03 12:59:47 -0700112 void appendToExportedTypesVector(
113 std::vector<const Type *> *exportedTypes) const;
114
Yifan Hongbe627b32016-10-28 18:38:56 -0700115 // used by the parser.
116 void addSyntaxError();
117 size_t syntaxErrors() const;
118
Yifan Hongc8934042016-11-17 17:10:52 -0800119 bool isIBase() const;
120
Steven Moreland19f11b52017-05-12 18:22:21 -0700121 // or nullptr if not isInterface
Yifan Hong78b38d12017-02-13 18:14:46 +0000122 const Interface *getInterface() const;
123
Steven Moreland19f11b52017-05-12 18:22:21 -0700124 // types or Interface base name (e.x. Foo)
125 std::string getBaseName() const;
126
Andreas Huberc9410c72016-07-28 12:18:40 -0700127private:
Steven Moreland28b9b532017-05-12 17:02:58 -0700128 const Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700129 std::string mPath;
Steven Morelandd537ab02016-09-12 10:32:01 -0700130 std::vector<Scope *> mScopePath;
Andreas Huberc9410c72016-07-28 12:18:40 -0700131
132 void *mScanner;
133 Scope *mRootScope;
134
Andreas Huberda51b8e2016-07-28 16:00:57 -0700135 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700136
Andreas Huber39fa7182016-08-19 14:27:33 -0700137 // A set of all external interfaces/types that are _actually_ referenced
138 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700139 std::set<FQName> mImportedNames;
140
Andreas Huber39fa7182016-08-19 14:27:33 -0700141 // A set of all ASTs we explicitly or implicitly (types.hal) import.
142 std::set<AST *> mImportedASTs;
143
Yifan Hong1977ea32016-10-05 12:49:08 -0700144 // If a single type (instead of the whole AST) is imported, the AST will be
145 // present as a key to this map, with the value being a list of types
146 // imported from this AST. If an AST appears in mImportedASTs but not in
147 // mImportedTypes, then the whole AST is imported.
148 std::map<AST *, std::set<Type *>> mImportedTypes;
149
Andreas Huber39fa7182016-08-19 14:27:33 -0700150 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700151 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700152
Yifan Hongbe627b32016-10-28 18:38:56 -0700153 // used by the parser.
154 size_t mSyntaxErrors = 0;
155
Andreas Huber39fa7182016-08-19 14:27:33 -0700156 bool addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700157 NamedType *type,
158 std::string *errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700159
Yifan Hong87ff8232017-01-09 12:07:05 -0800160 // Helper functions for lookupType.
161 Type *lookupTypeLocally(const FQName &fqName);
162 status_t lookupAutofilledType(const FQName &fqName, Type **returnedType);
163 Type *lookupTypeFromImports(const FQName &fqName);
164
Andreas Huber39fa7182016-08-19 14:27:33 -0700165 // Find a type matching fqName (which may be partial) and if found
166 // return the associated type and fill in the full "matchingName".
167 // Only types defined in this very AST are considered.
168 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
169
Andreas Huber881227d2016-08-02 14:20:21 -0700170 void getPackageComponents(std::vector<std::string> *components) const;
171
172 void getPackageAndVersionComponents(
173 std::vector<std::string> *components, bool cpp_compatible) const;
174
Steven Morelandee88eed2016-10-31 17:49:00 -0700175 static void generateCppPackageInclude(
176 Formatter &out,
177 const FQName &package,
178 const std::string &klass);
179
Steven Moreland5708edf2016-11-04 15:33:31 +0000180 std::string makeHeaderGuard(const std::string &baseName,
181 bool indicateGenerated = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700182 void enterLeaveNamespace(Formatter &out, bool enter) const;
183
Steven Moreland67f67b42016-09-29 08:59:02 -0700184 static void generateCheckNonNull(Formatter &out, const std::string &nonNull);
185
Andreas Huberb82318c2016-08-02 14:45:54 -0700186 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700187 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700188 status_t generateStubHeader(const std::string &outputPath) const;
189 status_t generateProxyHeader(const std::string &outputPath) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700190 status_t generatePassthroughHeader(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700191
192 status_t generateTypeSource(
193 Formatter &out, const std::string &ifaceName) const;
194
Yifan Hong068c5522016-10-31 14:07:25 -0700195 // a method, and in which interface is it originally defined.
196 // be careful of the case where method.isHidlReserved(), where interface
197 // is effectively useless.
198 using MethodGenerator = std::function<status_t(const Method *, const Interface *)>;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700199
Steven Moreland0b843772017-06-23 16:33:38 -0700200 void generateTemplatizationLink(Formatter& out) const;
201
Steven Moreland9c387612016-09-07 09:54:26 -0700202 status_t generateStubImplHeader(const std::string &outputPath) const;
203 status_t generateStubImplSource(const std::string &outputPath) const;
204
Yifan Hong068c5522016-10-31 14:07:25 -0700205 status_t generateMethods(Formatter &out, MethodGenerator gen) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700206 status_t generateStubImplMethod(Formatter &out,
207 const std::string &className,
Yifan Hong068c5522016-10-31 14:07:25 -0700208 const Method *method) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700209 status_t generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700210 const Method *method) const;
211 status_t generateProxyMethodSource(Formatter &out,
Steven Moreland69e7c702016-09-09 11:16:32 -0700212 const std::string &className,
213 const Method *method,
Yifan Hong068c5522016-10-31 14:07:25 -0700214 const Interface *superInterface) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700215
216 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700217
Andreas Huber881227d2016-08-02 14:20:21 -0700218 status_t generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -0800219 Formatter &out, const FQName &fqName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700220
221 status_t generateStubSource(
Yifan Hongeefe4f22017-01-04 15:32:42 -0800222 Formatter &out, const Interface *iface) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700223
224 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700225 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700226
Steven Moreland69e7c702016-09-09 11:16:32 -0700227 status_t generatePassthroughSource(Formatter &out) const;
228
Yifan Hongfe95aa22016-10-19 17:26:45 -0700229 status_t generateInterfaceSource(Formatter &out) const;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700230
231 enum InstrumentationEvent {
232 SERVER_API_ENTRY = 0,
233 SERVER_API_EXIT,
234 CLIENT_API_ENTRY,
235 CLIENT_API_EXIT,
236 SYNC_CALLBACK_ENTRY,
237 SYNC_CALLBACK_EXIT,
238 ASYNC_CALLBACK_ENTRY,
239 ASYNC_CALLBACK_EXIT,
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700240 PASSTHROUGH_ENTRY,
241 PASSTHROUGH_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700242 };
243
Martijn Coenen7b295242016-11-04 16:52:56 +0100244 status_t generateCppAtraceCall(
245 Formatter &out,
246 InstrumentationEvent event,
247 const Method *method) const;
248
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700249 status_t generateCppInstrumentationCall(
250 Formatter &out,
251 InstrumentationEvent event,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700252 const Method *method) const;
253
Andreas Hubere7ff2282016-08-16 13:50:03 -0700254 void declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -0700255 Formatter &out,
256 const std::vector<TypedVar *> &arg,
257 bool forResults) const;
Andreas Hubere7ff2282016-08-16 13:50:03 -0700258
Andreas Huber881227d2016-08-02 14:20:21 -0700259 void emitCppReaderWriter(
260 Formatter &out,
261 const std::string &parcelObj,
262 bool parcelObjIsPointer,
263 const TypedVar *arg,
264 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -0700265 Type::ErrorMode mode,
266 bool addPrefixToName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700267
Yifan Hongbf459bc2016-08-23 16:50:37 -0700268 void emitCppResolveReferences(
269 Formatter &out,
270 const std::string &parcelObj,
271 bool parcelObjIsPointer,
272 const TypedVar *arg,
273 bool isReader,
274 Type::ErrorMode mode,
275 bool addPrefixToName) const;
276
Andreas Huber2831d512016-08-15 09:33:47 -0700277 void emitJavaReaderWriter(
278 Formatter &out,
279 const std::string &parcelObj,
280 const TypedVar *arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800281 bool isReader,
282 bool addPrefixToName) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700283
Andreas Huber881227d2016-08-02 14:20:21 -0700284 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700285 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700286 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700287
Andreas Huberc9410c72016-07-28 12:18:40 -0700288 DISALLOW_COPY_AND_ASSIGN(AST);
289};
290
291} // namespace android
292
293#endif // AST_H_