blob: 6af5604886bedfa491161c93305862ffd01c2983 [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
Andreas Huberc9410c72016-07-28 12:18:40 -070088private:
Andreas Huber5345ec22016-07-29 13:33:27 -070089 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070090 std::string mPath;
Andreas Huberc9410c72016-07-28 12:18:40 -070091 Vector<Scope *> mScopePath;
92
93 void *mScanner;
94 Scope *mRootScope;
95
Andreas Huberda51b8e2016-07-28 16:00:57 -070096 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070097
Andreas Huber39fa7182016-08-19 14:27:33 -070098 // A set of all external interfaces/types that are _actually_ referenced
99 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -0700100 std::set<FQName> mImportedNames;
101
Andreas Huber85eabdb2016-08-25 11:24:49 -0700102 // Similar to mImportedNames, but all types references from "types.hal"
103 // are individually listed.
104 std::set<FQName> mImportedNamesForJava;
105
Andreas Huber39fa7182016-08-19 14:27:33 -0700106 // A set of all ASTs we explicitly or implicitly (types.hal) import.
107 std::set<AST *> mImportedASTs;
108
109 // Types keyed by full names defined in this AST.
110 KeyedVector<FQName, Type *> mDefinedTypesByFullName;
111
112 bool addScopedTypeInternal(
113 const char *localName,
114 Type *type,
115 std::string *errorMsg,
116 bool isTypeDef);
117
118 // Find a type matching fqName (which may be partial) and if found
119 // return the associated type and fill in the full "matchingName".
120 // Only types defined in this very AST are considered.
121 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
122
Andreas Huber881227d2016-08-02 14:20:21 -0700123 void getPackageComponents(std::vector<std::string> *components) const;
124
125 void getPackageAndVersionComponents(
126 std::vector<std::string> *components, bool cpp_compatible) const;
127
128 std::string makeHeaderGuard(const std::string &baseName) const;
129 void enterLeaveNamespace(Formatter &out, bool enter) const;
130
Andreas Huberb82318c2016-08-02 14:45:54 -0700131 status_t generateInterfaceHeader(const std::string &outputPath) const;
Steven Moreland40786312016-08-16 10:29:40 -0700132 status_t generateHwBinderHeader(const std::string &outputPath) const;
Andreas Huberb82318c2016-08-02 14:45:54 -0700133 status_t generateStubHeader(const std::string &outputPath) const;
134 status_t generateProxyHeader(const std::string &outputPath) const;
135 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700136
137 status_t generateTypeSource(
138 Formatter &out, const std::string &ifaceName) const;
139
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700140 status_t generateHeaderMethodSignatures(
141 Formatter &out, bool abstract) const;
142
Andreas Huber881227d2016-08-02 14:20:21 -0700143 status_t generateProxySource(
144 Formatter &out, const std::string &baseName) const;
145
146 status_t generateStubSource(
147 Formatter &out, const std::string &baseName) const;
148
149 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700150 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700151
Andreas Hubere7ff2282016-08-16 13:50:03 -0700152 void declareCppReaderLocals(
153 Formatter &out, const std::vector<TypedVar *> &arg) const;
154
Andreas Huber881227d2016-08-02 14:20:21 -0700155 void emitCppReaderWriter(
156 Formatter &out,
157 const std::string &parcelObj,
158 bool parcelObjIsPointer,
159 const TypedVar *arg,
160 bool isReader,
161 Type::ErrorMode mode) const;
162
Andreas Huber2831d512016-08-15 09:33:47 -0700163 void emitJavaReaderWriter(
164 Formatter &out,
165 const std::string &parcelObj,
166 const TypedVar *arg,
167 bool isReader) const;
168
Andreas Huber881227d2016-08-02 14:20:21 -0700169 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700170 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700171 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700172
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700173 // Helper function that generates vts type declaration from the current
174 // AST and the transitive closure of imported ASTs.
175 status_t emitVtsTypeDeclarationsHelper(
176 Formatter &out,
177 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700178
Andreas Huberc9410c72016-07-28 12:18:40 -0700179 DISALLOW_COPY_AND_ASSIGN(AST);
180};
181
182} // namespace android
183
184#endif // AST_H_