blob: 2d6497255ff1a8400481340e7a8d73a25ac5fe4a [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;
51
Andreas Huberc9410c72016-07-28 12:18:40 -070052 void enterScope(Scope *container);
53 void leaveScope();
54 Scope *scope();
Andreas Huber5a545442016-08-03 10:44:56 -070055
56 // Returns true iff successful.
Andreas Huber39fa7182016-08-19 14:27:33 -070057 bool addTypeDef(const char *localName, Type *type, std::string *errorMsg);
58
59 // Returns true iff successful.
Andreas Huber9ed827c2016-08-22 12:31:13 -070060 bool addScopedType(NamedType *type, std::string *errorMsg);
Andreas Huberc9410c72016-07-28 12:18:40 -070061
62 void *scanner();
63 void setScanner(void *scanner);
64
Andreas Huber0d0f9a22016-08-17 10:26:11 -070065 const std::string &getFilename() const;
66
Yifan Hongf24fa852016-09-23 11:03:15 -070067 // Look up an enum value by "FQName:valueName".
68 EnumValue *lookupEnumValue(const FQName &fqName, std::string *errorMsg);
69
Andreas Huber5345ec22016-07-29 13:33:27 -070070 // Look up a type by FQName, "pure" names, i.e. those without package
71 // or version are first looked up in the current scope chain.
72 // After that lookup proceeds to imports.
Yifan Hongae16eed2016-09-23 13:25:25 -070073 Type *lookupType(const FQName &fqName);
Andreas Huber5345ec22016-07-29 13:33:27 -070074
Andreas Huber39fa7182016-08-19 14:27:33 -070075 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070076
Andreas Huberb82318c2016-08-02 14:45:54 -070077 status_t generateCpp(const std::string &outputPath) const;
Steven Moreland9c387612016-09-07 09:54:26 -070078 status_t generateCppImpl(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070079
Andreas Huber0fa9e392016-08-31 09:05:44 -070080 status_t generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -070081 const std::string &outputPath,
82 const std::string &limitToType) const;
Andreas Huber0fa9e392016-08-31 09:05:44 -070083
84 status_t generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070085 const std::string &outputPath,
86 const std::string &limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070087
Iliyan Malchev5bb14022016-08-09 15:04:39 -070088 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070089
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070090 status_t generateVts(const std::string &outputPath) const;
91
Andreas Huber0fa9e392016-08-31 09:05:44 -070092 bool isJavaCompatible() const;
93
Iliyan Malcheved0509b2016-09-07 12:37:11 -070094 // Return the set of FQNames for those interfaces and types that are
95 // actually referenced in the AST, not merely imported.
96
97 const std::set<FQName>& getImportedNames() const {
98 return mImportedNames;
99 }
100
Andreas Huberc9410c72016-07-28 12:18:40 -0700101private:
Andreas Huber5345ec22016-07-29 13:33:27 -0700102 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700103 std::string mPath;
Steven Morelandd537ab02016-09-12 10:32:01 -0700104 std::vector<Scope *> mScopePath;
Andreas Huberc9410c72016-07-28 12:18:40 -0700105
106 void *mScanner;
107 Scope *mRootScope;
108
Andreas Huberda51b8e2016-07-28 16:00:57 -0700109 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700110
Andreas Huber39fa7182016-08-19 14:27:33 -0700111 // A set of all external interfaces/types that are _actually_ referenced
112 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700113 std::set<FQName> mImportedNames;
114
Andreas Huber85eabdb2016-08-25 11:24:49 -0700115 // Similar to mImportedNames, but all types references from "types.hal"
116 // are individually listed.
117 std::set<FQName> mImportedNamesForJava;
118
Andreas Huber39fa7182016-08-19 14:27:33 -0700119 // A set of all ASTs we explicitly or implicitly (types.hal) import.
120 std::set<AST *> mImportedASTs;
121
122 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700123 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700124
125 bool addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700126 NamedType *type,
127 std::string *errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700128
129 // Find a type matching fqName (which may be partial) and if found
130 // return the associated type and fill in the full "matchingName".
131 // Only types defined in this very AST are considered.
132 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
133
Andreas Huber881227d2016-08-02 14:20:21 -0700134 void getPackageComponents(std::vector<std::string> *components) const;
135
136 void getPackageAndVersionComponents(
137 std::vector<std::string> *components, bool cpp_compatible) const;
138
139 std::string makeHeaderGuard(const std::string &baseName) const;
140 void enterLeaveNamespace(Formatter &out, bool enter) const;
141
Andreas Huberb82318c2016-08-02 14:45:54 -0700142 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700143 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700144 status_t generateStubHeader(const std::string &outputPath) const;
145 status_t generateProxyHeader(const std::string &outputPath) const;
146 status_t generateAllSource(const std::string &outputPath) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700147 status_t generatePassthroughHeader(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700148
149 status_t generateTypeSource(
150 Formatter &out, const std::string &ifaceName) const;
151
Steven Morelanda7a421a2016-09-07 08:35:18 -0700152 enum MethodLocation {
153 PROXY_HEADER,
Steven Moreland9c387612016-09-07 09:54:26 -0700154 STUB_HEADER,
155 IMPL_HEADER,
Steven Moreland69e7c702016-09-09 11:16:32 -0700156 IMPL_SOURCE,
157 PASSTHROUGH_HEADER
Steven Morelanda7a421a2016-09-07 08:35:18 -0700158 };
159
Steven Moreland9c387612016-09-07 09:54:26 -0700160 status_t generateStubImplHeader(const std::string &outputPath) const;
161 status_t generateStubImplSource(const std::string &outputPath) const;
162
Steven Morelanda7a421a2016-09-07 08:35:18 -0700163 status_t generateMethods(Formatter &out,
164 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700165 MethodLocation type,
166 bool specifyNamespaces) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700167 status_t generateStubMethod(Formatter &out,
168 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700169 const Method *method,
170 bool specifyNamespaces) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700171 status_t generateProxyDeclaration(Formatter &out,
172 const std::string &className,
173 const Method *method,
174 bool specifyNamespaces) const;
175 status_t generateStubImplDeclaration(Formatter &out,
176 const std::string &className,
177 const Method *method,
178 bool specifyNamespaces) const;
179 status_t generateStubImplMethod(Formatter &out,
180 const std::string &className,
181 const Method *method,
182 bool specifyNamespaces) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700183 status_t generatePassthroughMethod(Formatter &out,
184 const std::string &className,
185 const Method *method,
186 bool specifyNamespaces) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700187
188 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700189
Andreas Huber881227d2016-08-02 14:20:21 -0700190 status_t generateProxySource(
191 Formatter &out, const std::string &baseName) const;
192
193 status_t generateStubSource(
194 Formatter &out, const std::string &baseName) const;
195
196 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700197 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700198
Steven Moreland69e7c702016-09-09 11:16:32 -0700199 status_t generatePassthroughSource(Formatter &out) const;
200
Andreas Hubere7ff2282016-08-16 13:50:03 -0700201 void declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -0700202 Formatter &out,
203 const std::vector<TypedVar *> &arg,
204 bool forResults) const;
Andreas Hubere7ff2282016-08-16 13:50:03 -0700205
Andreas Huber881227d2016-08-02 14:20:21 -0700206 void emitCppReaderWriter(
207 Formatter &out,
208 const std::string &parcelObj,
209 bool parcelObjIsPointer,
210 const TypedVar *arg,
211 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -0700212 Type::ErrorMode mode,
213 bool addPrefixToName) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700214
Andreas Huber2831d512016-08-15 09:33:47 -0700215 void emitJavaReaderWriter(
216 Formatter &out,
217 const std::string &parcelObj,
218 const TypedVar *arg,
219 bool isReader) const;
220
Andreas Huber881227d2016-08-02 14:20:21 -0700221 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700222 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700223 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700224
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700225 // Helper function that generates vts type declaration from the current
226 // AST and the transitive closure of imported ASTs.
227 status_t emitVtsTypeDeclarationsHelper(
228 Formatter &out,
229 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700230
Andreas Huberc9410c72016-07-28 12:18:40 -0700231 DISALLOW_COPY_AND_ASSIGN(AST);
232};
233
234} // namespace android
235
236#endif // AST_H_