blob: a159c590948da8e4f36a69f33aea1b542f77aeb1 [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 Hubereb1081f2016-07-28 13:13:24 -07006#include <string>
Andreas Huberc9410c72016-07-28 12:18:40 -07007#include <utils/Vector.h>
8
Andreas Huberda51b8e2016-07-28 16:00:57 -07009#include "FQName.h"
Andreas Huber881227d2016-08-02 14:20:21 -070010#include "Type.h"
Andreas Huberda51b8e2016-07-28 16:00:57 -070011
Andreas Huberc9410c72016-07-28 12:18:40 -070012namespace android {
13
Andreas Huber5345ec22016-07-29 13:33:27 -070014struct Coordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070015struct Formatter;
Andreas Huber881227d2016-08-02 14:20:21 -070016struct Method;
17struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070018struct Scope;
19
20struct AST {
Andreas Huber5345ec22016-07-29 13:33:27 -070021 AST(Coordinator *coordinator);
Andreas Huberc9410c72016-07-28 12:18:40 -070022 ~AST();
23
Andreas Huber84f89de2016-07-28 15:39:51 -070024 bool setPackage(const char *package);
25 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070026
Andreas Hubera2723d22016-07-29 15:36:07 -070027 // package and version really.
28 FQName package() const;
29 bool isInterface(std::string *ifaceName) const;
30
Andreas Huberc9410c72016-07-28 12:18:40 -070031 void enterScope(Scope *container);
32 void leaveScope();
33 Scope *scope();
34
35 void *scanner();
36 void setScanner(void *scanner);
37
Andreas Huber5345ec22016-07-29 13:33:27 -070038 // Look up a type by FQName, "pure" names, i.e. those without package
39 // or version are first looked up in the current scope chain.
40 // After that lookup proceeds to imports.
41 Type *lookupType(const char *name) const;
42
43 // Takes dot-separated path components to a type possibly inside this AST.
44 // Name resolution goes from root scope downwards, i.e. the path must be
45 // absolute.
46 Type *lookupTypeInternal(const std::string &namePath) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070047
48 void dump(Formatter &out) const;
49
Andreas Huberb82318c2016-08-02 14:45:54 -070050 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070051
Andreas Huberc9410c72016-07-28 12:18:40 -070052private:
Andreas Huber5345ec22016-07-29 13:33:27 -070053 Coordinator *mCoordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070054 Vector<Scope *> mScopePath;
55
56 void *mScanner;
57 Scope *mRootScope;
58
Andreas Huberda51b8e2016-07-28 16:00:57 -070059 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070060
Andreas Huber881227d2016-08-02 14:20:21 -070061 void getPackageComponents(std::vector<std::string> *components) const;
62
63 void getPackageAndVersionComponents(
64 std::vector<std::string> *components, bool cpp_compatible) const;
65
66 std::string makeHeaderGuard(const std::string &baseName) const;
67 void enterLeaveNamespace(Formatter &out, bool enter) const;
68
Andreas Huberb82318c2016-08-02 14:45:54 -070069 status_t generateInterfaceHeader(const std::string &outputPath) const;
70 status_t generateStubHeader(const std::string &outputPath) const;
71 status_t generateProxyHeader(const std::string &outputPath) const;
72 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070073
74 status_t generateTypeSource(
75 Formatter &out, const std::string &ifaceName) const;
76
77 status_t generateProxySource(
78 Formatter &out, const std::string &baseName) const;
79
80 status_t generateStubSource(
81 Formatter &out, const std::string &baseName) const;
82
83 status_t generateStubSourceForMethod(
84 Formatter &out, const Method *method) const;
85
86 void emitCppReaderWriter(
87 Formatter &out,
88 const std::string &parcelObj,
89 bool parcelObjIsPointer,
90 const TypedVar *arg,
91 bool isReader,
92 Type::ErrorMode mode) const;
93
94 status_t emitTypeDeclarations(Formatter &out) const;
95
Andreas Huberc9410c72016-07-28 12:18:40 -070096 DISALLOW_COPY_AND_ASSIGN(AST);
97};
98
99} // namespace android
100
101#endif // AST_H_