blob: 7bb07c1f370bb073685011fe498ffe60c985abd5 [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;
Andreas Huber31629bc2016-08-03 09:06:40 -070018struct NamedType;
Andreas Huber881227d2016-08-02 14:20:21 -070019struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070020struct Scope;
21
22struct AST {
Andreas Huber5345ec22016-07-29 13:33:27 -070023 AST(Coordinator *coordinator);
Andreas Huberc9410c72016-07-28 12:18:40 -070024 ~AST();
25
Andreas Huber84f89de2016-07-28 15:39:51 -070026 bool setPackage(const char *package);
27 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070028
Andreas Hubera2723d22016-07-29 15:36:07 -070029 // package and version really.
30 FQName package() const;
31 bool isInterface(std::string *ifaceName) const;
32
Andreas Huberc9410c72016-07-28 12:18:40 -070033 void enterScope(Scope *container);
34 void leaveScope();
35 Scope *scope();
Andreas Huber31629bc2016-08-03 09:06:40 -070036 void addScopedType(const char *localName, NamedType *type);
Andreas Huberc9410c72016-07-28 12:18:40 -070037
38 void *scanner();
39 void setScanner(void *scanner);
40
Andreas Huber5345ec22016-07-29 13:33:27 -070041 // Look up a type by FQName, "pure" names, i.e. those without package
42 // or version are first looked up in the current scope chain.
43 // After that lookup proceeds to imports.
Andreas Huber737080b2016-08-02 15:38:04 -070044 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070045
46 // Takes dot-separated path components to a type possibly inside this AST.
47 // Name resolution goes from root scope downwards, i.e. the path must be
48 // absolute.
49 Type *lookupTypeInternal(const std::string &namePath) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070050
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_