blob: 2f76f3db1b70531fe483d3406421d5fd7f9987c1 [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 Huberd2943e12016-08-05 11:59:31 -070056 void addImportedPackages(std::set<FQName> *importSet) const;
57
Andreas Huberc9410c72016-07-28 12:18:40 -070058private:
Andreas Huber5345ec22016-07-29 13:33:27 -070059 Coordinator *mCoordinator;
Andreas Huberc9410c72016-07-28 12:18:40 -070060 Vector<Scope *> mScopePath;
61
62 void *mScanner;
63 Scope *mRootScope;
64
Andreas Huberda51b8e2016-07-28 16:00:57 -070065 FQName mPackage;
Andreas Huber84f89de2016-07-28 15:39:51 -070066
Andreas Huber737080b2016-08-02 15:38:04 -070067 std::set<FQName> mImportedNames;
68
Andreas Huber881227d2016-08-02 14:20:21 -070069 void getPackageComponents(std::vector<std::string> *components) const;
70
71 void getPackageAndVersionComponents(
72 std::vector<std::string> *components, bool cpp_compatible) const;
73
74 std::string makeHeaderGuard(const std::string &baseName) const;
75 void enterLeaveNamespace(Formatter &out, bool enter) const;
76
Andreas Huberb82318c2016-08-02 14:45:54 -070077 status_t generateInterfaceHeader(const std::string &outputPath) const;
78 status_t generateStubHeader(const std::string &outputPath) const;
79 status_t generateProxyHeader(const std::string &outputPath) const;
80 status_t generateAllSource(const std::string &outputPath) const;
Andreas Huber881227d2016-08-02 14:20:21 -070081
82 status_t generateTypeSource(
83 Formatter &out, const std::string &ifaceName) const;
84
85 status_t generateProxySource(
86 Formatter &out, const std::string &baseName) const;
87
88 status_t generateStubSource(
89 Formatter &out, const std::string &baseName) const;
90
91 status_t generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -070092 Formatter &out, const Interface *iface, const Method *method) const;
Andreas Huber881227d2016-08-02 14:20:21 -070093
94 void emitCppReaderWriter(
95 Formatter &out,
96 const std::string &parcelObj,
97 bool parcelObjIsPointer,
98 const TypedVar *arg,
99 bool isReader,
100 Type::ErrorMode mode) const;
101
102 status_t emitTypeDeclarations(Formatter &out) const;
103
Andreas Huberc9410c72016-07-28 12:18:40 -0700104 DISALLOW_COPY_AND_ASSIGN(AST);
105};
106
107} // namespace android
108
109#endif // AST_H_