blob: 814e9dc37982b52aefbf12ef20c6717842498802 [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 Huber6cb08cf2016-08-03 15:44:51 -070017struct Interface;
Andreas Huber881227d2016-08-02 14:20:21 -070018struct Method;
Andreas Huber31629bc2016-08-03 09:06:40 -070019struct NamedType;
Andreas Huber881227d2016-08-02 14:20:21 -070020struct TypedVar;
Andreas Huberc9410c72016-07-28 12:18:40 -070021struct Scope;
22
23struct AST {
Andreas Huber5345ec22016-07-29 13:33:27 -070024 AST(Coordinator *coordinator);
Andreas Huberc9410c72016-07-28 12:18:40 -070025 ~AST();
26
Andreas Huber84f89de2016-07-28 15:39:51 -070027 bool setPackage(const char *package);
28 bool addImport(const char *import);
Andreas Hubereb1081f2016-07-28 13:13:24 -070029
Andreas Hubera2723d22016-07-29 15:36:07 -070030 // package and version really.
31 FQName package() const;
32 bool isInterface(std::string *ifaceName) const;
33
Andreas Huberc9410c72016-07-28 12:18:40 -070034 void enterScope(Scope *container);
35 void leaveScope();
36 Scope *scope();
Andreas Huber5a545442016-08-03 10:44:56 -070037
38 // Returns true iff successful.
39 bool addScopedType(const char *localName, NamedType *type);
Andreas Huberc9410c72016-07-28 12:18:40 -070040
41 void *scanner();
42 void setScanner(void *scanner);
43
Andreas Huber5345ec22016-07-29 13:33:27 -070044 // Look up a type by FQName, "pure" names, i.e. those without package
45 // or version are first looked up in the current scope chain.
46 // After that lookup proceeds to imports.
Andreas Huberfd4afab2016-08-03 13:02:57 -070047 Type *lookupType(const char *name);
Andreas Huber5345ec22016-07-29 13:33:27 -070048
49 // Takes dot-separated path components to a type possibly inside this AST.
50 // Name resolution goes from root scope downwards, i.e. the path must be
51 // absolute.
52 Type *lookupTypeInternal(const std::string &namePath) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070053
Andreas Huberb82318c2016-08-02 14:45:54 -070054 status_t generateCpp(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070055
Andreas Huberc9410c72016-07-28 12:18:40 -070056private:
Andreas Huber5345ec22016-07-29 13:33:27 -070057 Coordinator *mCoordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070058 Vector<Scope *> mScopePath;
59
60 void *mScanner;
61 Scope *mRootScope;
62
Andreas Huberda51b8e2016-07-28 16:00:57 -070063 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070064
Andreas Huber737080b2016-08-02 15:38:04 -070065 std::set<FQName> mImportedNames;
66
Andreas Huber881227d2016-08-02 14:20:21 -070067 void getPackageComponents(std::vector<std::string> *components) const;
68
69 void getPackageAndVersionComponents(
70 std::vector<std::string> *components, bool cpp_compatible) const;
71
72 std::string makeHeaderGuard(const std::string &baseName) const;
73 void enterLeaveNamespace(Formatter &out, bool enter) const;
74
Andreas Huberb82318c2016-08-02 14:45:54 -070075 status_t generateInterfaceHeader(const std::string &outputPath) const;
76 status_t generateStubHeader(const std::string &outputPath) const;
77 status_t generateProxyHeader(const std::string &outputPath) const;
78 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070079
80 status_t generateTypeSource(
81 Formatter &out, const std::string &ifaceName) const;
82
83 status_t generateProxySource(
84 Formatter &out, const std::string &baseName) const;
85
86 status_t generateStubSource(
87 Formatter &out, const std::string &baseName) const;
88
89 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -070090 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -070091
92 void emitCppReaderWriter(
93 Formatter &out,
94 const std::string &parcelObj,
95 bool parcelObjIsPointer,
96 const TypedVar *arg,
97 bool isReader,
98 Type::ErrorMode mode) const;
99
100 status_t emitTypeDeclarations(Formatter &out) const;
101
Andreas Huberc9410c72016-07-28 12:18:40 -0700102 DISALLOW_COPY_AND_ASSIGN(AST);
103};
104
105} // namespace android
106
107#endif // AST_H_