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 #define AST_H_
19 
20 #include "Scope.h"
21 #include "Expression.h"
22 
23 #include <hidl-util/Formatter.h>
24 #include <android-base/macros.h>
25 #include <android-base/logging.h>
26 #include <string>
27 #include <vector>
28 #include <utils/Errors.h>
29 
30 namespace android {
31 
32 struct Include;
33 struct Define;
34 struct CompositeDeclaration;
35 struct Declaration;
36 
37 struct AST {
38     AST(const std::string &path,
39         const std::string &outputDir,
40         const std::string &package,
41         bool isOpenGl);
42     ~AST();
43 
44     void *scanner();
45     void setScanner(void *scanner);
46 
47     bool isOpenGl() const;
48 
49     const std::string &getFilename() const;
50 
51     void setDeclarations(std::vector<Declaration *> *declarations);
52     void setIncludes(std::vector<Include *> *includes);
53 
54     Expression *getExpression() const;
55     void setExpression(Expression *expression);
56 
57     status_t generateCode() const;
58 
59     void processContents();
60 
61     const Scope<Define *> &getDefinesScope() const;
62     Scope<Define *> &getDefinesScope();
63 
64 private:
65     void * mScanner = nullptr;
66     std::string mPath;
67     std::string mOutputDir;
68     std::string mPackage;
69 
70     bool mIsOpenGl;
71 
72     Expression* mExpression = nullptr;
73 
74     std::vector<Declaration *> *mDeclarations = nullptr;
75     std::vector<CompositeDeclaration *> *mInterfaces = nullptr;
76     std::vector<Include *> *mIncludes = nullptr;
77 
78     Scope<Define *> mDefinesScope;
79 
80     const std::string getFileDir() const;
81 
82     status_t generateFile(CompositeDeclaration* declaration) const;
83     status_t generateTypesFile() const;
84 
85     void generateIncludes(Formatter &out) const;
86     void generatePackageLine(Formatter &out) const;
87 
88     void isolateInterfaces();
89     void isolateGlobalInterface();
90     void isolateIncludes();
91     void isolateConstants(Expression::Type ofType);
92 
93     DISALLOW_COPY_AND_ASSIGN(AST);
94 };
95 
96 }  // namespace android
97 
98 #endif  // AST_H_
99