blob: 1f9d4f0ccead09fc0cfbab81e0ce845239ab8f3d [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;
39
40struct AST {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070041 AST(Coordinator *coordinator, const std::string &path);
Andreas Huberc9410c72016-07-28 12:18:40 -070042 ~AST();
43
Andreas Huber84f89de2016-07-28 15:39:51 -070044 bool setPackage(const char *package);
45 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070046
Andreas Hubera2723d22016-07-29 15:36:07 -070047 // package and version really.
48 FQName package() const;
49 bool isInterface(std::string *ifaceName) const;
50
Andreas Huberc9410c72016-07-28 12:18:40 -070051 void enterScope(Scope *container);
52 void leaveScope();
53 Scope *scope();
Andreas Huber5a545442016-08-03 10:44:56 -070054
55 // Returns true iff successful.
Andreas Huber39fa7182016-08-19 14:27:33 -070056 bool addTypeDef(const char *localName, Type *type, std::string *errorMsg);
57
58 // Returns true iff successful.
Andreas Huber9ed827c2016-08-22 12:31:13 -070059 bool addScopedType(NamedType *type, std::string *errorMsg);
Andreas Huberc9410c72016-07-28 12:18:40 -070060
61 void *scanner();
62 void setScanner(void *scanner);
63
Andreas Huber0d0f9a22016-08-17 10:26:11 -070064 const std::string &getFilename() const;
65
Andreas Huber5345ec22016-07-29 13:33:27 -070066 // Look up a type by FQName, "pure" names, i.e. those without package
67 // or version are first looked up in the current scope chain.
68 // After that lookup proceeds to imports.
Yifan Hongae16eed2016-09-23 13:25:25 -070069 Type *lookupType(const FQName &fqName);
Andreas Huber5345ec22016-07-29 13:33:27 -070070
Andreas Huber39fa7182016-08-19 14:27:33 -070071 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070072
Andreas Huberb82318c2016-08-02 14:45:54 -070073 status_t generateCpp(const std::string &outputPath) const;
Steven Moreland9c387612016-09-07 09:54:26 -070074 status_t generateCppImpl(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070075
Andreas Huber0fa9e392016-08-31 09:05:44 -070076 status_t generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -070077 const std::string &outputPath,
78 const std::string &limitToType) const;
Andreas Huber0fa9e392016-08-31 09:05:44 -070079
80 status_t generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070081 const std::string &outputPath,
82 const std::string &limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070083
Iliyan Malchev5bb14022016-08-09 15:04:39 -070084 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070085
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070086 status_t generateVts(const std::string &outputPath) const;
87
Andreas Huber0fa9e392016-08-31 09:05:44 -070088 bool isJavaCompatible() const;
89
Iliyan Malcheved0509b2016-09-07 12:37:11 -070090 // Return the set of FQNames for those interfaces and types that are
91 // actually referenced in the AST, not merely imported.
92
93 const std::set<FQName>& getImportedNames() const {
94 return mImportedNames;
95 }
96
Andreas Huberc9410c72016-07-28 12:18:40 -070097private:
Andreas Huber5345ec22016-07-29 13:33:27 -070098 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070099 std::string mPath;
Steven Morelandd537ab02016-09-12 10:32:01 -0700100 std::vector<Scope *> mScopePath;
Andreas Huberc9410c72016-07-28 12:18:40 -0700101
102 void *mScanner;
103 Scope *mRootScope;
104
Andreas Huberda51b8e2016-07-28 16:00:57 -0700105 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700106
Andreas Huber39fa7182016-08-19 14:27:33 -0700107 // A set of all external interfaces/types that are _actually_ referenced
108 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700109 std::set<FQName> mImportedNames;
110
Andreas Huber85eabdb2016-08-25 11:24:49 -0700111 // Similar to mImportedNames, but all types references from "types.hal"
112 // are individually listed.
113 std::set<FQName> mImportedNamesForJava;
114
Andreas Huber39fa7182016-08-19 14:27:33 -0700115 // A set of all ASTs we explicitly or implicitly (types.hal) import.
116 std::set<AST *> mImportedASTs;
117
118 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700119 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700120
121 bool addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700122 NamedType *type,
123 std::string *errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700124
125 // Find a type matching fqName (which may be partial) and if found
126 // return the associated type and fill in the full "matchingName".
127 // Only types defined in this very AST are considered.
128 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
129
Andreas Huber881227d2016-08-02 14:20:21 -0700130 void getPackageComponents(std::vector<std::string> *components) const;
131
132 void getPackageAndVersionComponents(
133 std::vector<std::string> *components, bool cpp_compatible) const;
134
135 std::string makeHeaderGuard(const std::string &baseName) const;
136 void enterLeaveNamespace(Formatter &out, bool enter) const;
137
Andreas Huberb82318c2016-08-02 14:45:54 -0700138 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700139 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700140 status_t generateStubHeader(const std::string &outputPath) const;
141 status_t generateProxyHeader(const std::string &outputPath) const;
142 status_t generateAllSource(const std::string &outputPath) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700143 status_t generatePassthroughHeader(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,
Steven Moreland69e7c702016-09-09 11:16:32 -0700152 IMPL_SOURCE,
153 PASSTHROUGH_HEADER
Steven Morelanda7a421a2016-09-07 08:35:18 -0700154 };
155
Steven Moreland9c387612016-09-07 09:54:26 -0700156 status_t generateStubImplHeader(const std::string &outputPath) const;
157 status_t generateStubImplSource(const std::string &outputPath) const;
158
Steven Morelanda7a421a2016-09-07 08:35:18 -0700159 status_t generateMethods(Formatter &out,
160 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700161 MethodLocation type,
162 bool specifyNamespaces) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700163 status_t generateStubMethod(Formatter &out,
164 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700165 const Method *method,
166 bool specifyNamespaces) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700167 status_t generateProxyDeclaration(Formatter &out,
168 const std::string &className,
169 const Method *method,
170 bool specifyNamespaces) const;
171 status_t generateStubImplDeclaration(Formatter &out,
172 const std::string &className,
173 const Method *method,
174 bool specifyNamespaces) const;
175 status_t generateStubImplMethod(Formatter &out,
176 const std::string &className,
177 const Method *method,
178 bool specifyNamespaces) const;
Steven Moreland69e7c702016-09-09 11:16:32 -0700179 status_t generatePassthroughMethod(Formatter &out,
180 const std::string &className,
181 const Method *method,
182 bool specifyNamespaces) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700183
184 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700185
Andreas Huber881227d2016-08-02 14:20:21 -0700186 status_t generateProxySource(
187 Formatter &out, const std::string &baseName) const;
188
189 status_t generateStubSource(
190 Formatter &out, const std::string &baseName) const;
191
192 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700193 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700194
Steven Moreland69e7c702016-09-09 11:16:32 -0700195 status_t generatePassthroughSource(Formatter &out) const;
196
Andreas Hubere7ff2282016-08-16 13:50:03 -0700197 void declareCppReaderLocals(
198 Formatter &out, const std::vector<TypedVar *> &arg) const;
199
Andreas Huber881227d2016-08-02 14:20:21 -0700200 void emitCppReaderWriter(
201 Formatter &out,
202 const std::string &parcelObj,
203 bool parcelObjIsPointer,
204 const TypedVar *arg,
205 bool isReader,
206 Type::ErrorMode mode) const;
207
Andreas Huber2831d512016-08-15 09:33:47 -0700208 void emitJavaReaderWriter(
209 Formatter &out,
210 const std::string &parcelObj,
211 const TypedVar *arg,
212 bool isReader) const;
213
Andreas Huber881227d2016-08-02 14:20:21 -0700214 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700215 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700216 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700217
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700218 // Helper function that generates vts type declaration from the current
219 // AST and the transitive closure of imported ASTs.
220 status_t emitVtsTypeDeclarationsHelper(
221 Formatter &out,
222 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700223
Andreas Huberc9410c72016-07-28 12:18:40 -0700224 DISALLOW_COPY_AND_ASSIGN(AST);
225};
226
227} // namespace android
228
229#endif // AST_H_