blob: c0fe6b0318b0184b3f8d6e10719bb73e1756d6eb [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 Huberc9410c72016-07-28 12:18:40 -07008#include <utils/Vector.h>
9
Andreas Huberda51b8e2016-07-28 16:00:57 -070010#include "FQName.h"
Andreas Huber881227d2016-08-02 14:20:21 -070011#include "Type.h"
Andreas Huberda51b8e2016-07-28 16:00:57 -070012
Andreas Huberc9410c72016-07-28 12:18:40 -070013namespace android {
14
Andreas Huber5345ec22016-07-29 13:33:27 -070015struct Coordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070016struct Formatter;
Andreas Huber881227d2016-08-02 14:20:21 -070017struct Method;
18struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070019struct Scope;
20
21struct AST {
Andreas Huber5345ec22016-07-29 13:33:27 -070022 AST(Coordinator *coordinator);
Andreas Huberc9410c72016-07-28 12:18:40 -070023 ~AST();
24
Andreas Huber84f89de2016-07-28 15:39:51 -070025 bool setPackage(const char *package);
26 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070027
Andreas Hubera2723d22016-07-29 15:36:07 -070028 // package and version really.
29 FQName package() const;
30 bool isInterface(std::string *ifaceName) const;
31
Andreas Huberc9410c72016-07-28 12:18:40 -070032 void enterScope(Scope *container);
33 void leaveScope();
34 Scope *scope();
35
36 void *scanner();
37 void setScanner(void *scanner);
38
Andreas Huber5345ec22016-07-29 13:33:27 -070039 // Look up a type by FQName, "pure" names, i.e. those without package
40 // or version are first looked up in the current scope chain.
41 // After that lookup proceeds to imports.
Andreas Huber737080b2016-08-02 15:38:04 -070042 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070043
44 // Takes dot-separated path components to a type possibly inside this AST.
45 // Name resolution goes from root scope downwards, i.e. the path must be
46 // absolute.
47 Type *lookupTypeInternal(const std::string &namePath) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070048
49 void dump(Formatter &out) const;
50
Andreas Huberb82318c2016-08-02 14:45:54 -070051 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070052
Andreas Huberc9410c72016-07-28 12:18:40 -070053private:
Andreas Huber5345ec22016-07-29 13:33:27 -070054 Coordinator *mCoordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070055 Vector<Scope *> mScopePath;
56
57 void *mScanner;
58 Scope *mRootScope;
59
Andreas Huberda51b8e2016-07-28 16:00:57 -070060 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070061
Andreas Huber737080b2016-08-02 15:38:04 -070062 std::set<FQName> mImportedNames;
63
64 static void GetPackageComponents(
65 const FQName &fqName, std::vector<std::string> *components);
66
67 static void GetPackageAndVersionComponents(
68 const FQName &fqName,
69 std::vector<std::string> *components,
70 bool cpp_compatible);
71
Andreas Huber881227d2016-08-02 14:20:21 -070072 void getPackageComponents(std::vector<std::string> *components) const;
73
74 void getPackageAndVersionComponents(
75 std::vector<std::string> *components, bool cpp_compatible) const;
76
77 std::string makeHeaderGuard(const std::string &baseName) const;
78 void enterLeaveNamespace(Formatter &out, bool enter) const;
79
Andreas Huberb82318c2016-08-02 14:45:54 -070080 status_t generateInterfaceHeader(const std::string &outputPath) const;
81 status_t generateStubHeader(const std::string &outputPath) const;
82 status_t generateProxyHeader(const std::string &outputPath) const;
83 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070084
85 status_t generateTypeSource(
86 Formatter &out, const std::string &ifaceName) const;
87
88 status_t generateProxySource(
89 Formatter &out, const std::string &baseName) const;
90
91 status_t generateStubSource(
92 Formatter &out, const std::string &baseName) const;
93
94 status_t generateStubSourceForMethod(
95 Formatter &out, const Method *method) const;
96
97 void emitCppReaderWriter(
98 Formatter &out,
99 const std::string &parcelObj,
100 bool parcelObjIsPointer,
101 const TypedVar *arg,
102 bool isReader,
103 Type::ErrorMode mode) const;
104
105 status_t emitTypeDeclarations(Formatter &out) const;
106
Andreas Huberc9410c72016-07-28 12:18:40 -0700107 DISALLOW_COPY_AND_ASSIGN(AST);
108};
109
110} // namespace android
111
112#endif // AST_H_