blob: b4270f883a284c99ab721d9b60c77002ce0c8921 [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 Morelandd537ab02016-09-12 10:32:01 -070022#include <map>
Andreas Huber737080b2016-08-02 15:38:04 -070023#include <set>
Andreas Hubereb1081f2016-07-28 13:13:24 -070024#include <string>
Andreas Huber70a59e12016-08-16 12:57:01 -070025#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070026
Andreas Huberda51b8e2016-07-28 16:00:57 -070027#include "FQName.h"
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;
Andreas Huber881227d2016-08-02 14:20:21 -070035struct Method;
Andreas Huber31629bc2016-08-03 09:06:40 -070036struct NamedType;
Andreas Huber881227d2016-08-02 14:20:21 -070037struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070038struct Scope;
Yifan Hongf24fa852016-09-23 11:03:15 -070039struct EnumValue;
Andreas Huberc9410c72016-07-28 12:18:40 -070040
41struct AST {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070042 AST(Coordinator *coordinator, const std::string &path);
Andreas Huberc9410c72016-07-28 12:18:40 -070043 ~AST();
44
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;
50 bool isInterface(std::string *ifaceName) const;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070051 bool containsInterfaces() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070052
Andreas Huberc9410c72016-07-28 12:18:40 -070053 void enterScope(Scope *container);
54 void leaveScope();
55 Scope *scope();
Andreas Huber5a545442016-08-03 10:44:56 -070056
57 // Returns true iff successful.
Andreas Huber39fa7182016-08-19 14:27:33 -070058 bool addTypeDef(const char *localName, Type *type, std::string *errorMsg);
59
60 // Returns true iff successful.
Andreas Huber9ed827c2016-08-22 12:31:13 -070061 bool addScopedType(NamedType *type, std::string *errorMsg);
Andreas Huberc9410c72016-07-28 12:18:40 -070062
63 void *scanner();
64 void setScanner(void *scanner);
65
Andreas Huber0d0f9a22016-08-17 10:26:11 -070066 const std::string &getFilename() const;
67
Yifan Hongf24fa852016-09-23 11:03:15 -070068 // Look up an enum value by "FQName:valueName".
69 EnumValue *lookupEnumValue(const FQName &fqName, std::string *errorMsg);
70
Andreas Huber5345ec22016-07-29 13:33:27 -070071 // 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 Hongae16eed2016-09-23 13:25:25 -070074 Type *lookupType(const FQName &fqName);
Andreas Huber5345ec22016-07-29 13:33:27 -070075
Andreas Huber39fa7182016-08-19 14:27:33 -070076 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070077
Andreas Huberb82318c2016-08-02 14:45:54 -070078 status_t generateCpp(const std::string &outputPath) const;
Steven Moreland9c387612016-09-07 09:54:26 -070079 status_t generateCppImpl(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070080
Andreas Huber0fa9e392016-08-31 09:05:44 -070081 status_t generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -070082 const std::string &outputPath,
83 const std::string &limitToType) const;
Andreas Huber0fa9e392016-08-31 09:05:44 -070084
85 status_t generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070086 const std::string &outputPath,
87 const std::string &limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070088
Iliyan Malchev5bb14022016-08-09 15:04:39 -070089 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070090
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070091 status_t generateVts(const std::string &outputPath) const;
92
Andreas Huber0fa9e392016-08-31 09:05:44 -070093 bool isJavaCompatible() const;
94
Iliyan Malcheved0509b2016-09-07 12:37:11 -070095 // 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 Huber019d21d2016-10-03 12:59:47 -0700102 void appendToExportedTypesVector(
103 std::vector<const Type *> *exportedTypes) const;
104
Andreas Huberc9410c72016-07-28 12:18:40 -0700105private:
Andreas Huber5345ec22016-07-29 13:33:27 -0700106 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700107 std::string mPath;
Steven Morelandd537ab02016-09-12 10:32:01 -0700108 std::vector<Scope *> mScopePath;
Andreas Huberc9410c72016-07-28 12:18:40 -0700109
110 void *mScanner;
111 Scope *mRootScope;
112
Andreas Huberda51b8e2016-07-28 16:00:57 -0700113 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700114
Andreas Huber39fa7182016-08-19 14:27:33 -0700115 // 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 Huber737080b2016-08-02 15:38:04 -0700117 std::set<FQName> mImportedNames;
118
Andreas Huber85eabdb2016-08-25 11:24:49 -0700119 // Similar to mImportedNames, but all types references from "types.hal"
120 // are individually listed.
121 std::set<FQName> mImportedNamesForJava;
122
Andreas Huber39fa7182016-08-19 14:27:33 -0700123 // A set of all ASTs we explicitly or implicitly (types.hal) import.
124 std::set<AST *> mImportedASTs;
125
Yifan Hong1977ea32016-10-05 12:49:08 -0700126 // 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 Huber39fa7182016-08-19 14:27:33 -0700132 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700133 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700134
135 bool addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700136 NamedType *type,
137 std::string *errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700138
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 Huber881227d2016-08-02 14:20:21 -0700144 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 Moreland67f67b42016-09-29 08:59:02 -0700152 static void generateCheckNonNull(Formatter &out, const std::string &nonNull);
153
Andreas Huberb82318c2016-08-02 14:45:54 -0700154 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700155 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700156 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 Moreland69e7c702016-09-09 11:16:32 -0700159 status_t generatePassthroughHeader(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700160
161 status_t generateTypeSource(
162 Formatter &out, const std::string &ifaceName) const;
163
Yifan Hong068c5522016-10-31 14:07:25 -0700164 // a method, and in which interface is it originally defined.
165 // be careful of the case where method.isHidlReserved(), where interface
166 // is effectively useless.
167 using MethodGenerator = std::function<status_t(const Method *, const Interface *)>;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700168
Steven Moreland9c387612016-09-07 09:54:26 -0700169 status_t generateStubImplHeader(const std::string &outputPath) const;
170 status_t generateStubImplSource(const std::string &outputPath) const;
171
Yifan Hong068c5522016-10-31 14:07:25 -0700172 status_t generateMethods(Formatter &out, MethodGenerator gen) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700173 status_t generateStubMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700174 const Method *method) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700175 status_t generateStubImplMethod(Formatter &out,
176 const std::string &className,
Yifan Hong068c5522016-10-31 14:07:25 -0700177 const Method *method) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700178 status_t generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700179 const Method *method) const;
180 status_t generateProxyMethodSource(Formatter &out,
Steven Moreland69e7c702016-09-09 11:16:32 -0700181 const std::string &className,
182 const Method *method,
Yifan Hong068c5522016-10-31 14:07:25 -0700183 const Interface *superInterface) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700184
185 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700186
Andreas Huber881227d2016-08-02 14:20:21 -0700187 status_t generateProxySource(
188 Formatter &out, const std::string &baseName) const;
189
190 status_t generateStubSource(
191 Formatter &out, const std::string &baseName) const;
192
193 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700194 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700195
Steven Moreland69e7c702016-09-09 11:16:32 -0700196 status_t generatePassthroughSource(Formatter &out) const;
197
Yifan Hongfe95aa22016-10-19 17:26:45 -0700198 status_t generateInterfaceSource(Formatter &out) const;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700199
200 enum InstrumentationEvent {
201 SERVER_API_ENTRY = 0,
202 SERVER_API_EXIT,
203 CLIENT_API_ENTRY,
204 CLIENT_API_EXIT,
205 SYNC_CALLBACK_ENTRY,
206 SYNC_CALLBACK_EXIT,
207 ASYNC_CALLBACK_ENTRY,
208 ASYNC_CALLBACK_EXIT,
209 };
210
211 status_t generateCppInstrumentationCall(
212 Formatter &out,
213 InstrumentationEvent event,
214 const Interface *iface,
215 const Method *method) const;
216
Andreas Hubere7ff2282016-08-16 13:50:03 -0700217 void declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -0700218 Formatter &out,
219 const std::vector<TypedVar *> &arg,
220 bool forResults) const;
Andreas Hubere7ff2282016-08-16 13:50:03 -0700221
Andreas Huber881227d2016-08-02 14:20:21 -0700222 void emitCppReaderWriter(
223 Formatter &out,
224 const std::string &parcelObj,
225 bool parcelObjIsPointer,
226 const TypedVar *arg,
227 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -0700228 Type::ErrorMode mode,
229 bool addPrefixToName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700230
Yifan Hongbf459bc2016-08-23 16:50:37 -0700231 void emitCppResolveReferences(
232 Formatter &out,
233 const std::string &parcelObj,
234 bool parcelObjIsPointer,
235 const TypedVar *arg,
236 bool isReader,
237 Type::ErrorMode mode,
238 bool addPrefixToName) const;
239
Andreas Huber2831d512016-08-15 09:33:47 -0700240 void emitJavaReaderWriter(
241 Formatter &out,
242 const std::string &parcelObj,
243 const TypedVar *arg,
244 bool isReader) const;
245
Andreas Huber881227d2016-08-02 14:20:21 -0700246 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700247 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700248 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700249
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700250 // Helper function that generates vts type declaration from the current
251 // AST and the transitive closure of imported ASTs.
252 status_t emitVtsTypeDeclarationsHelper(
253 Formatter &out,
254 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700255
Andreas Huberc9410c72016-07-28 12:18:40 -0700256 DISALLOW_COPY_AND_ASSIGN(AST);
257};
258
259} // namespace android
260
261#endif // AST_H_