blob: 3040c221ede4b3802c426b1c4d4cce660ea31bef [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>
Andreas Huber737080b2016-08-02 15:38:04 -070022#include <set>
Andreas Hubereb1081f2016-07-28 13:13:24 -070023#include <string>
Andreas Huber3599d922016-08-09 10:42:57 -070024#include <utils/KeyedVector.h>
25#include <utils/RefBase.h>
Andreas Huber70a59e12016-08-16 12:57:01 -070026#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Andreas Huberda51b8e2016-07-28 16:00:57 -070028#include "FQName.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;
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;
40
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
Andreas Huber5345ec22016-07-29 13:33:27 -070067 // Look up a type by FQName, "pure" names, i.e. those without package
68 // or version are first looked up in the current scope chain.
69 // After that lookup proceeds to imports.
Andreas Huberfd4afab2016-08-03 13:02:57 -070070 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070071
Andreas Huber39fa7182016-08-19 14:27:33 -070072 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070073
Andreas Huberb82318c2016-08-02 14:45:54 -070074 status_t generateCpp(const std::string &outputPath) const;
Steven Moreland9c387612016-09-07 09:54:26 -070075 status_t generateCppImpl(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070076
Andreas Huber0fa9e392016-08-31 09:05:44 -070077 status_t generateJava(
78 const std::string &outputPath, const char *limitToType) const;
79
80 status_t generateJavaTypes(
81 const std::string &outputPath, const char *limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070082
Iliyan Malchev5bb14022016-08-09 15:04:39 -070083 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070084
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070085 status_t generateVts(const std::string &outputPath) const;
86
Andreas Huber0fa9e392016-08-31 09:05:44 -070087 bool isJavaCompatible() const;
88
Iliyan Malcheved0509b2016-09-07 12:37:11 -070089 // Return the set of FQNames for those interfaces and types that are
90 // actually referenced in the AST, not merely imported.
91
92 const std::set<FQName>& getImportedNames() const {
93 return mImportedNames;
94 }
95
Andreas Huberc9410c72016-07-28 12:18:40 -070096private:
Andreas Huber5345ec22016-07-29 13:33:27 -070097 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070098 std::string mPath;
Andreas Huberc9410c72016-07-28 12:18:40 -070099 Vector<Scope *> mScopePath;
100
101 void *mScanner;
102 Scope *mRootScope;
103
Andreas Huberda51b8e2016-07-28 16:00:57 -0700104 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700105
Andreas Huber39fa7182016-08-19 14:27:33 -0700106 // A set of all external interfaces/types that are _actually_ referenced
107 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700108 std::set<FQName> mImportedNames;
109
Andreas Huber85eabdb2016-08-25 11:24:49 -0700110 // Similar to mImportedNames, but all types references from "types.hal"
111 // are individually listed.
112 std::set<FQName> mImportedNamesForJava;
113
Andreas Huber39fa7182016-08-19 14:27:33 -0700114 // A set of all ASTs we explicitly or implicitly (types.hal) import.
115 std::set<AST *> mImportedASTs;
116
117 // Types keyed by full names defined in this AST.
118 KeyedVector<FQName, Type *> mDefinedTypesByFullName;
119
120 bool addScopedTypeInternal(
121 const char *localName,
122 Type *type,
123 std::string *errorMsg,
124 bool isTypeDef);
125
126 // Find a type matching fqName (which may be partial) and if found
127 // return the associated type and fill in the full "matchingName".
128 // Only types defined in this very AST are considered.
129 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
130
Andreas Huber881227d2016-08-02 14:20:21 -0700131 void getPackageComponents(std::vector<std::string> *components) const;
132
133 void getPackageAndVersionComponents(
134 std::vector<std::string> *components, bool cpp_compatible) const;
135
136 std::string makeHeaderGuard(const std::string &baseName) const;
137 void enterLeaveNamespace(Formatter &out, bool enter) const;
138
Andreas Huberb82318c2016-08-02 14:45:54 -0700139 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700140 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700141 status_t generateStubHeader(const std::string &outputPath) const;
142 status_t generateProxyHeader(const std::string &outputPath) const;
143 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700144
145 status_t generateTypeSource(
146 Formatter &out, const std::string &ifaceName) const;
147
Steven Morelanda7a421a2016-09-07 08:35:18 -0700148 enum MethodLocation {
149 PROXY_HEADER,
Steven Moreland9c387612016-09-07 09:54:26 -0700150 STUB_HEADER,
151 IMPL_HEADER,
152 IMPL_SOURCE
Steven Morelanda7a421a2016-09-07 08:35:18 -0700153 };
154
Steven Moreland9c387612016-09-07 09:54:26 -0700155 status_t generateStubImplHeader(const std::string &outputPath) const;
156 status_t generateStubImplSource(const std::string &outputPath) const;
157
Steven Morelanda7a421a2016-09-07 08:35:18 -0700158 status_t generateMethods(Formatter &out,
159 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700160 MethodLocation type,
161 bool specifyNamespaces) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700162 status_t generateStubMethod(Formatter &out,
163 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700164 const Method *method,
165 bool specifyNamespaces) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700166 status_t generateProxyDeclaration(Formatter &out,
167 const std::string &className,
168 const Method *method,
169 bool specifyNamespaces) const;
170 status_t generateStubImplDeclaration(Formatter &out,
171 const std::string &className,
172 const Method *method,
173 bool specifyNamespaces) const;
174 status_t generateStubImplMethod(Formatter &out,
175 const std::string &className,
176 const Method *method,
177 bool specifyNamespaces) const;
178
179 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700180
Andreas Huber881227d2016-08-02 14:20:21 -0700181 status_t generateProxySource(
182 Formatter &out, const std::string &baseName) const;
183
184 status_t generateStubSource(
185 Formatter &out, const std::string &baseName) const;
186
187 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700188 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700189
Andreas Hubere7ff2282016-08-16 13:50:03 -0700190 void declareCppReaderLocals(
191 Formatter &out, const std::vector<TypedVar *> &arg) const;
192
Andreas Huber881227d2016-08-02 14:20:21 -0700193 void emitCppReaderWriter(
194 Formatter &out,
195 const std::string &parcelObj,
196 bool parcelObjIsPointer,
197 const TypedVar *arg,
198 bool isReader,
199 Type::ErrorMode mode) const;
200
Andreas Huber2831d512016-08-15 09:33:47 -0700201 void emitJavaReaderWriter(
202 Formatter &out,
203 const std::string &parcelObj,
204 const TypedVar *arg,
205 bool isReader) const;
206
Andreas Huber881227d2016-08-02 14:20:21 -0700207 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700208 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700209 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700210
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700211 // Helper function that generates vts type declaration from the current
212 // AST and the transitive closure of imported ASTs.
213 status_t emitVtsTypeDeclarationsHelper(
214 Formatter &out,
215 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700216
Andreas Huberc9410c72016-07-28 12:18:40 -0700217 DISALLOW_COPY_AND_ASSIGN(AST);
218};
219
220} // namespace android
221
222#endif // AST_H_