blob: 2d674bac401bd12b244e79a3bec6b86617f53bed [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 Huber0d0f9a22016-08-17 10:26:11 -070041 bool addScopedType(
42 const char *localName,
43 NamedType *type,
44 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
56 // Takes dot-separated path components to a type possibly inside this AST.
57 // Name resolution goes from root scope downwards, i.e. the path must be
58 // absolute.
59 Type *lookupTypeInternal(const std::string &namePath) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070060
Andreas Huberb82318c2016-08-02 14:45:54 -070061 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber2831d512016-08-15 09:33:47 -070062 status_t generateJava(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070063
Iliyan Malchev5bb14022016-08-09 15:04:39 -070064 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070065
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070066 status_t generateVts(const std::string &outputPath) const;
67
Andreas Huberc9410c72016-07-28 12:18:40 -070068private:
Andreas Huber5345ec22016-07-29 13:33:27 -070069 Coordinator *mCoordinator;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070070 std::string mPath;
Andreas Huberc9410c72016-07-28 12:18:40 -070071 Vector<Scope *> mScopePath;
72
73 void *mScanner;
74 Scope *mRootScope;
75
Andreas Huberda51b8e2016-07-28 16:00:57 -070076 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070077
Andreas Huber737080b2016-08-02 15:38:04 -070078 std::set<FQName> mImportedNames;
79
Andreas Huber881227d2016-08-02 14:20:21 -070080 void getPackageComponents(std::vector<std::string> *components) const;
81
82 void getPackageAndVersionComponents(
83 std::vector<std::string> *components, bool cpp_compatible) const;
84
85 std::string makeHeaderGuard(const std::string &baseName) const;
86 void enterLeaveNamespace(Formatter &out, bool enter) const;
87
Andreas Huberb82318c2016-08-02 14:45:54 -070088 status_t generateInterfaceHeader(const std::string &outputPath) const;
89 status_t generateStubHeader(const std::string &outputPath) const;
90 status_t generateProxyHeader(const std::string &outputPath) const;
91 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070092
93 status_t generateTypeSource(
94 Formatter &out, const std::string &ifaceName) const;
95
Iliyan Malchev62c3d182016-08-16 20:33:39 -070096 status_t generateHeaderMethodSignatures(
97 Formatter &out, bool abstract) const;
98
Andreas Huber881227d2016-08-02 14:20:21 -070099 status_t generateProxySource(
100 Formatter &out, const std::string &baseName) const;
101
102 status_t generateStubSource(
103 Formatter &out, const std::string &baseName) const;
104
105 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700106 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700107
Andreas Hubere7ff2282016-08-16 13:50:03 -0700108 void declareCppReaderLocals(
109 Formatter &out, const std::vector<TypedVar *> &arg) const;
110
Andreas Huber881227d2016-08-02 14:20:21 -0700111 void emitCppReaderWriter(
112 Formatter &out,
113 const std::string &parcelObj,
114 bool parcelObjIsPointer,
115 const TypedVar *arg,
116 bool isReader,
117 Type::ErrorMode mode) const;
118
Andreas Huber2831d512016-08-15 09:33:47 -0700119 void emitJavaReaderWriter(
120 Formatter &out,
121 const std::string &parcelObj,
122 const TypedVar *arg,
123 bool isReader) const;
124
Andreas Huber881227d2016-08-02 14:20:21 -0700125 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700126 status_t emitJavaTypeDeclarations(Formatter &out) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700127
128 status_t emitVtsTypeDeclarations(
129 Formatter &out, const std::vector<Type *> &types) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700130
Andreas Huberc9410c72016-07-28 12:18:40 -0700131 DISALLOW_COPY_AND_ASSIGN(AST);
132};
133
134} // namespace android
135
136#endif // AST_H_