blob: da8f655b1a0eb51e7742ca2ed76048eb18a4da2d [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 Huberc9410c72016-07-28 12:18:40 -070010#include <utils/Vector.h>
11
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 Huber5345ec22016-07-29 13:33:27 -070026 AST(Coordinator *coordinator);
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.
41 bool addScopedType(const char *localName, NamedType *type);
Andreas Huberc9410c72016-07-28 12:18:40 -070042
43 void *scanner();
44 void setScanner(void *scanner);
45
Andreas Huber5345ec22016-07-29 13:33:27 -070046 // Look up a type by FQName, "pure" names, i.e. those without package
47 // or version are first looked up in the current scope chain.
48 // After that lookup proceeds to imports.
Andreas Huberfd4afab2016-08-03 13:02:57 -070049 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070050
51 // Takes dot-separated path components to a type possibly inside this AST.
52 // Name resolution goes from root scope downwards, i.e. the path must be
53 // absolute.
54 Type *lookupTypeInternal(const std::string &namePath) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Andreas Huberb82318c2016-08-02 14:45:54 -070056 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber2831d512016-08-15 09:33:47 -070057 status_t generateJava(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070058
Iliyan Malchev5bb14022016-08-09 15:04:39 -070059 void getImportedPackages(std::set<FQName> *importSet) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070060
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070061 status_t generateVts(const std::string &outputPath) const;
62
63
Andreas Huberc9410c72016-07-28 12:18:40 -070064private:
Andreas Huber5345ec22016-07-29 13:33:27 -070065 Coordinator *mCoordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070066 Vector<Scope *> mScopePath;
67
68 void *mScanner;
69 Scope *mRootScope;
70
Andreas Huberda51b8e2016-07-28 16:00:57 -070071 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070072
Andreas Huber737080b2016-08-02 15:38:04 -070073 std::set<FQName> mImportedNames;
74
Andreas Huber881227d2016-08-02 14:20:21 -070075 void getPackageComponents(std::vector<std::string> *components) const;
76
77 void getPackageAndVersionComponents(
78 std::vector<std::string> *components, bool cpp_compatible) const;
79
80 std::string makeHeaderGuard(const std::string &baseName) const;
81 void enterLeaveNamespace(Formatter &out, bool enter) const;
82
Andreas Huberb82318c2016-08-02 14:45:54 -070083 status_t generateInterfaceHeader(const std::string &outputPath) const;
84 status_t generateStubHeader(const std::string &outputPath) const;
85 status_t generateProxyHeader(const std::string &outputPath) const;
86 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070087
88 status_t generateTypeSource(
89 Formatter &out, const std::string &ifaceName) const;
90
91 status_t generateProxySource(
92 Formatter &out, const std::string &baseName) const;
93
94 status_t generateStubSource(
95 Formatter &out, const std::string &baseName) const;
96
97 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -070098 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -070099
100 void emitCppReaderWriter(
101 Formatter &out,
102 const std::string &parcelObj,
103 bool parcelObjIsPointer,
104 const TypedVar *arg,
105 bool isReader,
106 Type::ErrorMode mode) const;
107
Andreas Huber2831d512016-08-15 09:33:47 -0700108 void emitJavaReaderWriter(
109 Formatter &out,
110 const std::string &parcelObj,
111 const TypedVar *arg,
112 bool isReader) const;
113
Andreas Huber881227d2016-08-02 14:20:21 -0700114 status_t emitTypeDeclarations(Formatter &out) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700115 status_t emitJavaTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700116 status_t emitVtsTypeDeclarations(Formatter &out, Vector<Type*> types) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700117
Andreas Huberc9410c72016-07-28 12:18:40 -0700118 DISALLOW_COPY_AND_ASSIGN(AST);
119};
120
121} // namespace android
122
123#endif // AST_H_