blob: cd13ac9a40204766199a2350528d42eb12e44e55 [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;
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;
Andreas Huberc9410c72016-07-28 12:18:40 -070098 Vector<Scope *> mScopePath;
99
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.
117 KeyedVector<FQName, Type *> mDefinedTypesByFullName;
118
119 bool addScopedTypeInternal(
120 const char *localName,
121 Type *type,
122 std::string *errorMsg,
123 bool isTypeDef);
124
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;
Andreas Huber881227d2016-08-02 14:20:21 -0700143
144 status_t generateTypeSource(
145 Formatter &out, const std::string &ifaceName) const;
146
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700147 status_t generateHeaderMethodSignatures(
148 Formatter &out, bool abstract) const;
149
Andreas Huber881227d2016-08-02 14:20:21 -0700150 status_t generateProxySource(
151 Formatter &out, const std::string &baseName) const;
152
153 status_t generateStubSource(
154 Formatter &out, const std::string &baseName) const;
155
156 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700157 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700158
Andreas Hubere7ff2282016-08-16 13:50:03 -0700159 void declareCppReaderLocals(
160 Formatter &out, const std::vector<TypedVar *> &arg) const;
161
Andreas Huber881227d2016-08-02 14:20:21 -0700162 void emitCppReaderWriter(
163 Formatter &out,
164 const std::string &parcelObj,
165 bool parcelObjIsPointer,
166 const TypedVar *arg,
167 bool isReader,
168 Type::ErrorMode mode) const;
169
Andreas Huber2831d512016-08-15 09:33:47 -0700170 void emitJavaReaderWriter(
171 Formatter &out,
172 const std::string &parcelObj,
173 const TypedVar *arg,
174 bool isReader) const;
175
Andreas Huber881227d2016-08-02 14:20:21 -0700176 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700177 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700178 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700179
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700180 // Helper function that generates vts type declaration from the current
181 // AST and the transitive closure of imported ASTs.
182 status_t emitVtsTypeDeclarationsHelper(
183 Formatter &out,
184 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700185
Andreas Huberc9410c72016-07-28 12:18:40 -0700186 DISALLOW_COPY_AND_ASSIGN(AST);
187};
188
189} // namespace android
190
191#endif // AST_H_