blob: 1360e7efcb8e1a8d9f609f1d9f6e78ecace2cd9d [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.
Andreas Huberfd4afab2016-08-03 13:02:57 -070069 Type *lookupType(const char *name);
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(
77 const std::string &outputPath, const char *limitToType) const;
78
79 status_t generateJavaTypes(
80 const std::string &outputPath, const char *limitToType) const;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Iliyan Malchev5bb14022016-08-09 15:04:39 -070082 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070083
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070084 status_t generateVts(const std::string &outputPath) const;
85
Andreas Huber0fa9e392016-08-31 09:05:44 -070086 bool isJavaCompatible() const;
87
Iliyan Malcheved0509b2016-09-07 12:37:11 -070088 // Return the set of FQNames for those interfaces and types that are
89 // actually referenced in the AST, not merely imported.
90
91 const std::set<FQName>& getImportedNames() const {
92 return mImportedNames;
93 }
94
Andreas Huberc9410c72016-07-28 12:18:40 -070095private:
Andreas Huber5345ec22016-07-29 13:33:27 -070096 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070097 std::string mPath;
Steven Morelandd537ab02016-09-12 10:32:01 -070098 std::vector<Scope *> mScopePath;
Andreas Huberc9410c72016-07-28 12:18:40 -070099
100 void *mScanner;
101 Scope *mRootScope;
102
Andreas Huberda51b8e2016-07-28 16:00:57 -0700103 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -0700104
Andreas Huber39fa7182016-08-19 14:27:33 -0700105 // A set of all external interfaces/types that are _actually_ referenced
106 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700107 std::set<FQName> mImportedNames;
108
Andreas Huber85eabdb2016-08-25 11:24:49 -0700109 // Similar to mImportedNames, but all types references from "types.hal"
110 // are individually listed.
111 std::set<FQName> mImportedNamesForJava;
112
Andreas Huber39fa7182016-08-19 14:27:33 -0700113 // A set of all ASTs we explicitly or implicitly (types.hal) import.
114 std::set<AST *> mImportedASTs;
115
116 // Types keyed by full names defined in this AST.
Steven Morelandd537ab02016-09-12 10:32:01 -0700117 std::map<FQName, Type *> mDefinedTypesByFullName;
Andreas Huber39fa7182016-08-19 14:27:33 -0700118
119 bool addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700120 NamedType *type,
121 std::string *errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700122
123 // Find a type matching fqName (which may be partial) and if found
124 // return the associated type and fill in the full "matchingName".
125 // Only types defined in this very AST are considered.
126 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
127
Andreas Huber881227d2016-08-02 14:20:21 -0700128 void getPackageComponents(std::vector<std::string> *components) const;
129
130 void getPackageAndVersionComponents(
131 std::vector<std::string> *components, bool cpp_compatible) const;
132
133 std::string makeHeaderGuard(const std::string &baseName) const;
134 void enterLeaveNamespace(Formatter &out, bool enter) const;
135
Andreas Huberb82318c2016-08-02 14:45:54 -0700136 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700137 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700138 status_t generateStubHeader(const std::string &outputPath) const;
139 status_t generateProxyHeader(const std::string &outputPath) const;
140 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700141
142 status_t generateTypeSource(
143 Formatter &out, const std::string &ifaceName) const;
144
Steven Morelanda7a421a2016-09-07 08:35:18 -0700145 enum MethodLocation {
146 PROXY_HEADER,
Steven Moreland9c387612016-09-07 09:54:26 -0700147 STUB_HEADER,
148 IMPL_HEADER,
149 IMPL_SOURCE
Steven Morelanda7a421a2016-09-07 08:35:18 -0700150 };
151
Steven Moreland9c387612016-09-07 09:54:26 -0700152 status_t generateStubImplHeader(const std::string &outputPath) const;
153 status_t generateStubImplSource(const std::string &outputPath) const;
154
Steven Morelanda7a421a2016-09-07 08:35:18 -0700155 status_t generateMethods(Formatter &out,
156 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700157 MethodLocation type,
158 bool specifyNamespaces) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -0700159 status_t generateStubMethod(Formatter &out,
160 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700161 const Method *method,
162 bool specifyNamespaces) const;
Steven Moreland9c387612016-09-07 09:54:26 -0700163 status_t generateProxyDeclaration(Formatter &out,
164 const std::string &className,
165 const Method *method,
166 bool specifyNamespaces) const;
167 status_t generateStubImplDeclaration(Formatter &out,
168 const std::string &className,
169 const Method *method,
170 bool specifyNamespaces) const;
171 status_t generateStubImplMethod(Formatter &out,
172 const std::string &className,
173 const Method *method,
174 bool specifyNamespaces) const;
175
176 void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700177
Andreas Huber881227d2016-08-02 14:20:21 -0700178 status_t generateProxySource(
179 Formatter &out, const std::string &baseName) const;
180
181 status_t generateStubSource(
182 Formatter &out, const std::string &baseName) const;
183
184 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700185 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700186
Andreas Hubere7ff2282016-08-16 13:50:03 -0700187 void declareCppReaderLocals(
188 Formatter &out, const std::vector<TypedVar *> &arg) const;
189
Andreas Huber881227d2016-08-02 14:20:21 -0700190 void emitCppReaderWriter(
191 Formatter &out,
192 const std::string &parcelObj,
193 bool parcelObjIsPointer,
194 const TypedVar *arg,
195 bool isReader,
196 Type::ErrorMode mode) const;
197
Andreas Huber2831d512016-08-15 09:33:47 -0700198 void emitJavaReaderWriter(
199 Formatter &out,
200 const std::string &parcelObj,
201 const TypedVar *arg,
202 bool isReader) const;
203
Andreas Huber881227d2016-08-02 14:20:21 -0700204 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700205 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700206 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700207
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700208 // Helper function that generates vts type declaration from the current
209 // AST and the transitive closure of imported ASTs.
210 status_t emitVtsTypeDeclarationsHelper(
211 Formatter &out,
212 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700213
Andreas Huberc9410c72016-07-28 12:18:40 -0700214 DISALLOW_COPY_AND_ASSIGN(AST);
215};
216
217} // namespace android
218
219#endif // AST_H_