Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #ifndef AST_H_ |
| 2 | |
| 3 | #define AST_H_ |
| 4 | |
| 5 | #include <android-base/macros.h> |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 6 | #include <set> |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 7 | #include <string> |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 8 | #include <utils/KeyedVector.h> |
| 9 | #include <utils/RefBase.h> |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 10 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 11 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 12 | #include "FQName.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 13 | #include "Type.h" |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 14 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 15 | namespace android { |
| 16 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 17 | struct Coordinator; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 18 | struct Formatter; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 19 | struct Interface; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 20 | struct Method; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 21 | struct NamedType; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 22 | struct TypedVar; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | struct Scope; |
| 24 | |
| 25 | struct AST { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 26 | AST(Coordinator *coordinator, const std::string &path); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | ~AST(); |
| 28 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 29 | bool setPackage(const char *package); |
| 30 | bool addImport(const char *import); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 31 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 32 | // package and version really. |
| 33 | FQName package() const; |
| 34 | bool isInterface(std::string *ifaceName) const; |
| 35 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 36 | void enterScope(Scope *container); |
| 37 | void leaveScope(); |
| 38 | Scope *scope(); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 39 | |
| 40 | // Returns true iff successful. |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame^] | 41 | bool addTypeDef(const char *localName, Type *type, std::string *errorMsg); |
| 42 | |
| 43 | // Returns true iff successful. |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 44 | bool addScopedType( |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame^] | 45 | const char *localName, NamedType *type, std::string *errorMsg); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 46 | |
| 47 | void *scanner(); |
| 48 | void setScanner(void *scanner); |
| 49 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 50 | const std::string &getFilename() const; |
| 51 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 52 | // Look up a type by FQName, "pure" names, i.e. those without package |
| 53 | // or version are first looked up in the current scope chain. |
| 54 | // After that lookup proceeds to imports. |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 55 | Type *lookupType(const char *name); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 56 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame^] | 57 | void addImportedAST(AST *ast); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 58 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 59 | status_t generateCpp(const std::string &outputPath) const; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 60 | status_t generateJava(const std::string &outputPath) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 61 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 62 | void getImportedPackages(std::set<FQName> *importSet) const; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 63 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 64 | status_t generateVts(const std::string &outputPath) const; |
| 65 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 66 | private: |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 67 | Coordinator *mCoordinator; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 68 | std::string mPath; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 69 | Vector<Scope *> mScopePath; |
| 70 | |
| 71 | void *mScanner; |
| 72 | Scope *mRootScope; |
| 73 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 74 | FQName mPackage; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 75 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame^] | 76 | // A set of all external interfaces/types that are _actually_ referenced |
| 77 | // in this AST, this is a subset of those specified in import statements. |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 78 | std::set<FQName> mImportedNames; |
| 79 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame^] | 80 | // A set of all ASTs we explicitly or implicitly (types.hal) import. |
| 81 | std::set<AST *> mImportedASTs; |
| 82 | |
| 83 | // Types keyed by full names defined in this AST. |
| 84 | KeyedVector<FQName, Type *> mDefinedTypesByFullName; |
| 85 | |
| 86 | bool addScopedTypeInternal( |
| 87 | const char *localName, |
| 88 | Type *type, |
| 89 | std::string *errorMsg, |
| 90 | bool isTypeDef); |
| 91 | |
| 92 | // Find a type matching fqName (which may be partial) and if found |
| 93 | // return the associated type and fill in the full "matchingName". |
| 94 | // Only types defined in this very AST are considered. |
| 95 | Type *findDefinedType(const FQName &fqName, FQName *matchingName) const; |
| 96 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 97 | void getPackageComponents(std::vector<std::string> *components) const; |
| 98 | |
| 99 | void getPackageAndVersionComponents( |
| 100 | std::vector<std::string> *components, bool cpp_compatible) const; |
| 101 | |
| 102 | std::string makeHeaderGuard(const std::string &baseName) const; |
| 103 | void enterLeaveNamespace(Formatter &out, bool enter) const; |
| 104 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 105 | status_t generateInterfaceHeader(const std::string &outputPath) const; |
| 106 | status_t generateStubHeader(const std::string &outputPath) const; |
| 107 | status_t generateProxyHeader(const std::string &outputPath) const; |
| 108 | status_t generateAllSource(const std::string &outputPath) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 109 | |
| 110 | status_t generateTypeSource( |
| 111 | Formatter &out, const std::string &ifaceName) const; |
| 112 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 113 | status_t generateHeaderMethodSignatures( |
| 114 | Formatter &out, bool abstract) const; |
| 115 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 116 | status_t generateProxySource( |
| 117 | Formatter &out, const std::string &baseName) const; |
| 118 | |
| 119 | status_t generateStubSource( |
| 120 | Formatter &out, const std::string &baseName) const; |
| 121 | |
| 122 | status_t generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 123 | Formatter &out, const Interface *iface, const Method *method) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 124 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 125 | void declareCppReaderLocals( |
| 126 | Formatter &out, const std::vector<TypedVar *> &arg) const; |
| 127 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 128 | void emitCppReaderWriter( |
| 129 | Formatter &out, |
| 130 | const std::string &parcelObj, |
| 131 | bool parcelObjIsPointer, |
| 132 | const TypedVar *arg, |
| 133 | bool isReader, |
| 134 | Type::ErrorMode mode) const; |
| 135 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 136 | void emitJavaReaderWriter( |
| 137 | Formatter &out, |
| 138 | const std::string &parcelObj, |
| 139 | const TypedVar *arg, |
| 140 | bool isReader) const; |
| 141 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 142 | status_t emitTypeDeclarations(Formatter &out) const; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 143 | status_t emitJavaTypeDeclarations(Formatter &out) const; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 144 | |
| 145 | status_t emitVtsTypeDeclarations( |
| 146 | Formatter &out, const std::vector<Type *> &types) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 147 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(AST); |
| 149 | }; |
| 150 | |
| 151 | } // namespace android |
| 152 | |
| 153 | #endif // AST_H_ |