blob: aa16056826b0c88596a6ca10310af59ef1a8b7f6 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#ifndef AST_H_
2
3#define AST_H_
4
5#include <android-base/macros.h>
Andreas Huber737080b2016-08-02 15:38:04 -07006#include <set>
Andreas Hubereb1081f2016-07-28 13:13:24 -07007#include <string>
Andreas Huber3599d922016-08-09 10:42:57 -07008#include <utils/KeyedVector.h>
9#include <utils/RefBase.h>
Andreas Huber70a59e12016-08-16 12:57:01 -070010#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070011
Andreas Huberda51b8e2016-07-28 16:00:57 -070012#include "FQName.h"
Andreas Huber881227d2016-08-02 14:20:21 -070013#include "Type.h"
Andreas Huberda51b8e2016-07-28 16:00:57 -070014
Andreas Huberc9410c72016-07-28 12:18:40 -070015namespace android {
16
Andreas Huber5345ec22016-07-29 13:33:27 -070017struct Coordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070018struct Formatter;
Andreas Huber6cb08cf2016-08-03 15:44:51 -070019struct Interface;
Andreas Huber881227d2016-08-02 14:20:21 -070020struct Method;
Andreas Huber31629bc2016-08-03 09:06:40 -070021struct NamedType;
Andreas Huber881227d2016-08-02 14:20:21 -070022struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070023struct Scope;
24
25struct AST {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070026 AST(Coordinator *coordinator, const std::string &path);
Andreas Huberc9410c72016-07-28 12:18:40 -070027 ~AST();
28
Andreas Huber84f89de2016-07-28 15:39:51 -070029 bool setPackage(const char *package);
30 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070031
Andreas Hubera2723d22016-07-29 15:36:07 -070032 // package and version really.
33 FQName package() const;
34 bool isInterface(std::string *ifaceName) const;
35
Andreas Huberc9410c72016-07-28 12:18:40 -070036 void enterScope(Scope *container);
37 void leaveScope();
38 Scope *scope();
Andreas Huber5a545442016-08-03 10:44:56 -070039
40 // Returns true iff successful.
Andreas Huber39fa7182016-08-19 14:27:33 -070041 bool addTypeDef(const char *localName, Type *type, std::string *errorMsg);
42
43 // Returns true iff successful.
Andreas Huber9ed827c2016-08-22 12:31:13 -070044 bool addScopedType(NamedType *type, std::string *errorMsg);
Andreas Huberc9410c72016-07-28 12:18:40 -070045
46 void *scanner();
47 void setScanner(void *scanner);
48
Andreas Huber0d0f9a22016-08-17 10:26:11 -070049 const std::string &getFilename() const;
50
Andreas Huber5345ec22016-07-29 13:33:27 -070051 // Look up a type by FQName, "pure" names, i.e. those without package
52 // or version are first looked up in the current scope chain.
53 // After that lookup proceeds to imports.
Andreas Huberfd4afab2016-08-03 13:02:57 -070054 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070055
Andreas Huber39fa7182016-08-19 14:27:33 -070056 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070057
Andreas Huberb82318c2016-08-02 14:45:54 -070058 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070059
Andreas Huber2831d512016-08-15 09:33:47 -070060 status_t generateJava(const std::string &outputPath) const;
Andreas Huber85eabdb2016-08-25 11:24:49 -070061 status_t generateJavaTypes(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070062
Iliyan Malchev5bb14022016-08-09 15:04:39 -070063 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070064
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070065 status_t generateVts(const std::string &outputPath) const;
66
Andreas Huberc9410c72016-07-28 12:18:40 -070067private:
Andreas Huber5345ec22016-07-29 13:33:27 -070068 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070069 std::string mPath;
Andreas Huberc9410c72016-07-28 12:18:40 -070070 Vector<Scope *> mScopePath;
71
72 void *mScanner;
73 Scope *mRootScope;
74
Andreas Huberda51b8e2016-07-28 16:00:57 -070075 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070076
Andreas Huber39fa7182016-08-19 14:27:33 -070077 // A set of all external interfaces/types that are _actually_ referenced
78 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -070079 std::set<FQName> mImportedNames;
80
Andreas Huber85eabdb2016-08-25 11:24:49 -070081 // Similar to mImportedNames, but all types references from "types.hal"
82 // are individually listed.
83 std::set<FQName> mImportedNamesForJava;
84
Andreas Huber39fa7182016-08-19 14:27:33 -070085 // A set of all ASTs we explicitly or implicitly (types.hal) import.
86 std::set<AST *> mImportedASTs;
87
88 // Types keyed by full names defined in this AST.
89 KeyedVector<FQName, Type *> mDefinedTypesByFullName;
90
91 bool addScopedTypeInternal(
92 const char *localName,
93 Type *type,
94 std::string *errorMsg,
95 bool isTypeDef);
96
97 // Find a type matching fqName (which may be partial) and if found
98 // return the associated type and fill in the full "matchingName".
99 // Only types defined in this very AST are considered.
100 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
101
Andreas Huber881227d2016-08-02 14:20:21 -0700102 void getPackageComponents(std::vector<std::string> *components) const;
103
104 void getPackageAndVersionComponents(
105 std::vector<std::string> *components, bool cpp_compatible) const;
106
107 std::string makeHeaderGuard(const std::string &baseName) const;
108 void enterLeaveNamespace(Formatter &out, bool enter) const;
109
Andreas Huberb82318c2016-08-02 14:45:54 -0700110 status_t generateInterfaceHeader(const std::string &outputPath) const;
111 status_t generateStubHeader(const std::string &outputPath) const;
112 status_t generateProxyHeader(const std::string &outputPath) const;
113 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700114
115 status_t generateTypeSource(
116 Formatter &out, const std::string &ifaceName) const;
117
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700118 status_t generateHeaderMethodSignatures(
119 Formatter &out, bool abstract) const;
120
Andreas Huber881227d2016-08-02 14:20:21 -0700121 status_t generateProxySource(
122 Formatter &out, const std::string &baseName) const;
123
124 status_t generateStubSource(
125 Formatter &out, const std::string &baseName) const;
126
127 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700128 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700129
Andreas Hubere7ff2282016-08-16 13:50:03 -0700130 void declareCppReaderLocals(
131 Formatter &out, const std::vector<TypedVar *> &arg) const;
132
Andreas Huber881227d2016-08-02 14:20:21 -0700133 void emitCppReaderWriter(
134 Formatter &out,
135 const std::string &parcelObj,
136 bool parcelObjIsPointer,
137 const TypedVar *arg,
138 bool isReader,
139 Type::ErrorMode mode) const;
140
Andreas Huber2831d512016-08-15 09:33:47 -0700141 void emitJavaReaderWriter(
142 Formatter &out,
143 const std::string &parcelObj,
144 const TypedVar *arg,
145 bool isReader) const;
146
Andreas Huber881227d2016-08-02 14:20:21 -0700147 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700148 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700149 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700150
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700151 // Helper function that generates vts type declaration from the current
152 // AST and the transitive closure of imported ASTs.
153 status_t emitVtsTypeDeclarationsHelper(
154 Formatter &out,
155 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700156
Andreas Huberc9410c72016-07-28 12:18:40 -0700157 DISALLOW_COPY_AND_ASSIGN(AST);
158};
159
160} // namespace android
161
162#endif // AST_H_