• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AST_H_
18 
19 #define AST_H_
20 
21 #include <android-base/macros.h>
22 #include <hidl-util/FQName.h>
23 #include <map>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "Type.h"
29 
30 namespace android {
31 
32 struct Coordinator;
33 struct Formatter;
34 struct Interface;
35 struct Location;
36 struct Method;
37 struct NamedType;
38 struct TypedVar;
39 struct Scope;
40 struct EnumValue;
41 
42 struct AST {
43     AST(Coordinator *coordinator, const std::string &path);
44     ~AST();
45 
46     bool setPackage(const char *package);
47     bool addImport(const char *import);
48 
49     // package and version really.
50     FQName package() const;
51     bool isInterface(std::string *ifaceName) const;
52     bool containsInterfaces() const;
53 
54     void enterScope(Scope *container);
55     void leaveScope();
56     Scope *scope();
57 
58     // Returns true iff successful.
59     bool addTypeDef(const char *localName, Type *type, const Location &location,
60             std::string *errorMsg);
61 
62     // Returns true iff successful.
63     bool addScopedType(NamedType *type, std::string *errorMsg);
64 
65     void *scanner();
66     void setScanner(void *scanner);
67 
68     const std::string &getFilename() const;
69 
70     // Look up an enum value by "FQName:valueName".
71     EnumValue *lookupEnumValue(const FQName &fqName, std::string *errorMsg);
72 
73     // Look up a type by FQName, "pure" names, i.e. those without package
74     // or version are first looked up in the current scope chain.
75     // After that lookup proceeds to imports.
76     Type *lookupType(const FQName &fqName);
77 
78     void addImportedAST(AST *ast);
79 
80     status_t generateCpp(const std::string &outputPath) const;
81     status_t generateCppHeaders(const std::string &outputPath) const;
82     status_t generateCppSources(const std::string &outputPath) const;
83     status_t generateCppImpl(const std::string &outputPath) const;
84 
85     status_t generateJava(
86             const std::string &outputPath,
87             const std::string &limitToType) const;
88 
89     status_t generateJavaTypes(
90             const std::string &outputPath,
91             const std::string &limitToType) const;
92 
93     void getImportedPackages(std::set<FQName> *importSet) const;
94 
95     // Run getImportedPackages on this, then run getImportedPackages on
96     // each AST in each package referenced in importSet.
97     void getImportedPackagesHierarchy(std::set<FQName> *importSet) const;
98 
99     status_t generateVts(const std::string &outputPath) const;
100 
101     bool isJavaCompatible() const;
102 
103     // Return the set of FQNames for those interfaces and types that are
104     // actually referenced in the AST, not merely imported.
getImportedNamesAST105     const std::set<FQName>& getImportedNames() const {
106         return mImportedNames;
107     }
108 
109     // Get transitive closure of imported interface/types.
110     void getAllImportedNames(std::set<FQName> *allImportSet) const;
111 
112     void appendToExportedTypesVector(
113             std::vector<const Type *> *exportedTypes) const;
114 
115     // used by the parser.
116     void addSyntaxError();
117     size_t syntaxErrors() const;
118 
119     bool isIBase() const;
120 
121     const Interface *getInterface() const;
122 
123 private:
124     Coordinator *mCoordinator;
125     std::string mPath;
126     std::vector<Scope *> mScopePath;
127 
128     void *mScanner;
129     Scope *mRootScope;
130 
131     FQName mPackage;
132 
133     // A set of all external interfaces/types that are _actually_ referenced
134     // in this AST, this is a subset of those specified in import statements.
135     std::set<FQName> mImportedNames;
136 
137     // A set of all ASTs we explicitly or implicitly (types.hal) import.
138     std::set<AST *> mImportedASTs;
139 
140     // If a single type (instead of the whole AST) is imported, the AST will be
141     // present as a key to this map, with the value being a list of types
142     // imported from this AST. If an AST appears in mImportedASTs but not in
143     // mImportedTypes, then the whole AST is imported.
144     std::map<AST *, std::set<Type *>> mImportedTypes;
145 
146     // Types keyed by full names defined in this AST.
147     std::map<FQName, Type *> mDefinedTypesByFullName;
148 
149     // used by the parser.
150     size_t mSyntaxErrors = 0;
151 
152     bool addScopedTypeInternal(
153             NamedType *type,
154             std::string *errorMsg);
155 
156     // Helper functions for lookupType.
157     Type *lookupTypeLocally(const FQName &fqName);
158     status_t lookupAutofilledType(const FQName &fqName, Type **returnedType);
159     Type *lookupTypeFromImports(const FQName &fqName);
160 
161     // Find a type matching fqName (which may be partial) and if found
162     // return the associated type and fill in the full "matchingName".
163     // Only types defined in this very AST are considered.
164     Type *findDefinedType(const FQName &fqName, FQName *matchingName) const;
165 
166     void getPackageComponents(std::vector<std::string> *components) const;
167 
168     void getPackageAndVersionComponents(
169             std::vector<std::string> *components, bool cpp_compatible) const;
170 
171     static void generateCppPackageInclude(
172             Formatter &out,
173             const FQName &package,
174             const std::string &klass);
175 
176     std::string makeHeaderGuard(const std::string &baseName,
177                                 bool indicateGenerated = true) const;
178     void enterLeaveNamespace(Formatter &out, bool enter) const;
179 
180     static void generateCheckNonNull(Formatter &out, const std::string &nonNull);
181 
182     status_t generateInterfaceHeader(const std::string &outputPath) const;
183     status_t generateHwBinderHeader(const std::string &outputPath) const;
184     status_t generateStubHeader(const std::string &outputPath) const;
185     status_t generateProxyHeader(const std::string &outputPath) const;
186     status_t generatePassthroughHeader(const std::string &outputPath) const;
187 
188     status_t generateTypeSource(
189             Formatter &out, const std::string &ifaceName) const;
190 
191     // a method, and in which interface is it originally defined.
192     // be careful of the case where method.isHidlReserved(), where interface
193     // is effectively useless.
194     using MethodGenerator = std::function<status_t(const Method *, const Interface *)>;
195 
196     status_t generateStubImplHeader(const std::string &outputPath) const;
197     status_t generateStubImplSource(const std::string &outputPath) const;
198 
199     status_t generateMethods(Formatter &out, MethodGenerator gen) const;
200     status_t generateStubImplMethod(Formatter &out,
201                                     const std::string &className,
202                                     const Method *method) const;
203     status_t generatePassthroughMethod(Formatter &out,
204                                        const Method *method) const;
205     status_t generateProxyMethodSource(Formatter &out,
206                                        const std::string &className,
207                                        const Method *method,
208                                        const Interface *superInterface) const;
209 
210     void generateFetchSymbol(Formatter &out, const std::string &ifaceName) const;
211 
212     status_t generateProxySource(
213             Formatter &out, const FQName &fqName) const;
214 
215     status_t generateStubSource(
216             Formatter &out, const Interface *iface) const;
217 
218     status_t generateStubSourceForMethod(
219             Formatter &out, const Interface *iface, const Method *method) const;
220 
221     status_t generatePassthroughSource(Formatter &out) const;
222 
223     status_t generateInterfaceSource(Formatter &out) const;
224 
225     enum InstrumentationEvent {
226         SERVER_API_ENTRY = 0,
227         SERVER_API_EXIT,
228         CLIENT_API_ENTRY,
229         CLIENT_API_EXIT,
230         SYNC_CALLBACK_ENTRY,
231         SYNC_CALLBACK_EXIT,
232         ASYNC_CALLBACK_ENTRY,
233         ASYNC_CALLBACK_EXIT,
234         PASSTHROUGH_ENTRY,
235         PASSTHROUGH_EXIT,
236     };
237 
238     status_t generateCppAtraceCall(
239             Formatter &out,
240             InstrumentationEvent event,
241             const Method *method) const;
242 
243     status_t generateCppInstrumentationCall(
244             Formatter &out,
245             InstrumentationEvent event,
246             const Method *method) const;
247 
248     void declareCppReaderLocals(
249             Formatter &out,
250             const std::vector<TypedVar *> &arg,
251             bool forResults) const;
252 
253     void emitCppReaderWriter(
254             Formatter &out,
255             const std::string &parcelObj,
256             bool parcelObjIsPointer,
257             const TypedVar *arg,
258             bool isReader,
259             Type::ErrorMode mode,
260             bool addPrefixToName) const;
261 
262     void emitCppResolveReferences(
263             Formatter &out,
264             const std::string &parcelObj,
265             bool parcelObjIsPointer,
266             const TypedVar *arg,
267             bool isReader,
268             Type::ErrorMode mode,
269             bool addPrefixToName) const;
270 
271     void emitJavaReaderWriter(
272             Formatter &out,
273             const std::string &parcelObj,
274             const TypedVar *arg,
275             bool isReader,
276             bool addPrefixToName) const;
277 
278     status_t emitTypeDeclarations(Formatter &out) const;
279     status_t emitJavaTypeDeclarations(Formatter &out) const;
280     status_t emitVtsTypeDeclarations(Formatter &out) const;
281 
282     DISALLOW_COPY_AND_ASSIGN(AST);
283 };
284 
285 }  // namespace android
286 
287 #endif  // AST_H_
288