blob: d5fe3ba46b2b4a0fcd5198790927dd8895c2e5cb [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 Huber0d0f9a22016-08-17 10:26:11 -070044 bool addScopedType(
Andreas Huber39fa7182016-08-19 14:27:33 -070045 const char *localName, NamedType *type, std::string *errorMsg);
Andreas Huberc9410c72016-07-28 12:18:40 -070046
47 void *scanner();
48 void setScanner(void *scanner);
49
Andreas Huber0d0f9a22016-08-17 10:26:11 -070050 const std::string &getFilename() const;
51
Andreas Huber5345ec22016-07-29 13:33:27 -070052 // Look up a type by FQName, "pure" names, i.e. those without package
53 // or version are first looked up in the current scope chain.
54 // After that lookup proceeds to imports.
Andreas Huberfd4afab2016-08-03 13:02:57 -070055 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070056
Andreas Huber39fa7182016-08-19 14:27:33 -070057 void addImportedAST(AST *ast);
Andreas Huberc9410c72016-07-28 12:18:40 -070058
Andreas Huberb82318c2016-08-02 14:45:54 -070059 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber2831d512016-08-15 09:33:47 -070060 status_t generateJava(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070061
Iliyan Malchev5bb14022016-08-09 15:04:39 -070062 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070063
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070064 status_t generateVts(const std::string &outputPath) const;
65
Andreas Huberc9410c72016-07-28 12:18:40 -070066private:
Andreas Huber5345ec22016-07-29 13:33:27 -070067 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070068 std::string mPath;
Andreas Huberc9410c72016-07-28 12:18:40 -070069 Vector<Scope *> mScopePath;
70
71 void *mScanner;
72 Scope *mRootScope;
73
Andreas Huberda51b8e2016-07-28 16:00:57 -070074 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070075
Andreas Huber39fa7182016-08-19 14:27:33 -070076 // A set of all external interfaces/types that are _actually_ referenced
77 // in this AST, this is a subset of those specified in import statements.
Andreas Huber737080b2016-08-02 15:38:04 -070078 std::set<FQName> mImportedNames;
79
Andreas Huber39fa7182016-08-19 14:27:33 -070080 // A set of all ASTs we explicitly or implicitly (types.hal) import.
81 std::set<AST *> mImportedASTs;
82
83 // Types keyed by full names defined in this AST.
84 KeyedVector<FQName, Type *> mDefinedTypesByFullName;
85
86 bool addScopedTypeInternal(
87 const char *localName,
88 Type *type,
89 std::string *errorMsg,
90 bool isTypeDef);
91
92 // Find a type matching fqName (which may be partial) and if found
93 // return the associated type and fill in the full "matchingName".
94 // Only types defined in this very AST are considered.
95 Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
96
Andreas Huber881227d2016-08-02 14:20:21 -070097 void getPackageComponents(std::vector<std::string> *components) const;
98
99 void getPackageAndVersionComponents(
100 std::vector<std::string> *components, bool cpp_compatible) const;
101
102 std::string makeHeaderGuard(const std::string &baseName) const;
103 void enterLeaveNamespace(Formatter &out, bool enter) const;
104
Andreas Huberb82318c2016-08-02 14:45:54 -0700105 status_t generateInterfaceHeader(const std::string &outputPath) const;
106 status_t generateStubHeader(const std::string &outputPath) const;
107 status_t generateProxyHeader(const std::string &outputPath) const;
108 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700109
110 status_t generateTypeSource(
111 Formatter &out, const std::string &ifaceName) const;
112
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700113 status_t generateHeaderMethodSignatures(
114 Formatter &out, bool abstract) const;
115
Andreas Huber881227d2016-08-02 14:20:21 -0700116 status_t generateProxySource(
117 Formatter &out, const std::string &baseName) const;
118
119 status_t generateStubSource(
120 Formatter &out, const std::string &baseName) const;
121
122 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700123 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700124
Andreas Hubere7ff2282016-08-16 13:50:03 -0700125 void declareCppReaderLocals(
126 Formatter &out, const std::vector<TypedVar *> &arg) const;
127
Andreas Huber881227d2016-08-02 14:20:21 -0700128 void emitCppReaderWriter(
129 Formatter &out,
130 const std::string &parcelObj,
131 bool parcelObjIsPointer,
132 const TypedVar *arg,
133 bool isReader,
134 Type::ErrorMode mode) const;
135
Andreas Huber2831d512016-08-15 09:33:47 -0700136 void emitJavaReaderWriter(
137 Formatter &out,
138 const std::string &parcelObj,
139 const TypedVar *arg,
140 bool isReader) const;
141
Andreas Huber881227d2016-08-02 14:20:21 -0700142 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700143 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700144 status_t emitVtsTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700145
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700146 // Helper function that generates vts type declaration from the current
147 // AST and the transitive closure of imported ASTs.
148 status_t emitVtsTypeDeclarationsHelper(
149 Formatter &out,
150 std::set<AST*> *allImportSet) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700151
Andreas Huberc9410c72016-07-28 12:18:40 -0700152 DISALLOW_COPY_AND_ASSIGN(AST);
153};
154
155} // namespace android
156
157#endif // AST_H_