• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015, 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 #pragma once
18 
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <variant>
25 #include <vector>
26 
27 enum {
28   PACKAGE_PRIVATE = 0x00000000,
29   PUBLIC = 0x00000001,
30   PRIVATE = 0x00000002,
31   PROTECTED = 0x00000003,
32   SCOPE_MASK = 0x00000003,
33 
34   STATIC = 0x00000010,
35   FINAL = 0x00000020,
36   ABSTRACT = 0x00000040,
37 
38   OVERRIDE = 0x00000100,
39 
40   ALL_MODIFIERS = 0xffffffff
41 };
42 
43 namespace android {
44 namespace aidl {
45 class CodeWriter;
46 }  // namespace aidl
47 }  // namespace android
48 
49 namespace android {
50 namespace aidl {
51 namespace java {
52 
53 // Write the modifiers that are set in both mod and mask
54 void WriteModifiers(CodeWriter* to, int mod, int mask);
55 
56 struct AstNode {
57   AstNode() = default;
58   virtual ~AstNode() = default;
59   virtual void Write(CodeWriter* to) const = 0;
60   std::string ToString();
61 };
62 
63 struct ClassElement : public AstNode {
64   ClassElement() = default;
65   virtual ~ClassElement() = default;
66 };
67 
68 struct Expression : public AstNode {
69   virtual ~Expression() = default;
70 };
71 
72 struct LiteralExpression : public Expression {
73   std::string value;
74 
75   explicit LiteralExpression(const std::string& value);
76   virtual ~LiteralExpression() = default;
77   void Write(CodeWriter* to) const override;
78 };
79 
80 // TODO: also escape the contents.  not needed for now
81 struct StringLiteralExpression : public Expression {
82   std::string value;
83 
84   explicit StringLiteralExpression(const std::string& value);
85   virtual ~StringLiteralExpression() = default;
86   void Write(CodeWriter* to) const override;
87 };
88 
89 struct Variable : public Expression {
90   const std::string type;
91   std::string name;
92   int dimension = 0;
93 
94   Variable() = default;
95   Variable(const std::string& type, const std::string& name);
96   Variable(const std::string& type, const std::string& name, int dimension);
97   virtual ~Variable() = default;
98 
99   void WriteDeclaration(CodeWriter* to) const;
100   void Write(CodeWriter* to) const;
101 };
102 
103 struct FieldVariable : public Expression {
104   std::variant<Expression*, std::string> receiver;
105   std::string name;
106 
107   FieldVariable(Expression* object, const std::string& name);
108   FieldVariable(const std::string& clazz, const std::string& name);
109   virtual ~FieldVariable() = default;
110 
111   void Write(CodeWriter* to) const;
112 };
113 
114 struct Field : public ClassElement {
115   std::string comment;
116   std::vector<std::string> annotations;
117   int modifiers = 0;
118   Variable* variable = nullptr;
119   std::string value;
120 
121   Field() = default;
122   Field(int modifiers, Variable* variable);
123   virtual ~Field() = default;
124 
125   void Write(CodeWriter* to) const override;
126 };
127 
128 struct Statement : public AstNode {
129   virtual ~Statement() = default;
130 };
131 
132 struct LiteralStatement : public Statement {
133  public:
134   LiteralStatement(const std::string& value);
135   virtual ~LiteralStatement() = default;
136   void Write(CodeWriter* to) const override;
137 
138  private:
139   const std::string value_;
140 };
141 
142 struct StatementBlock : public Statement {
143   std::vector<Statement*> statements;
144 
145   StatementBlock() = default;
146   virtual ~StatementBlock() = default;
147   void Write(CodeWriter* to) const override;
148 
149   void Add(Statement* statement);
150   void Add(Expression* expression);
151 };
152 
153 struct ExpressionStatement : public Statement {
154   Expression* expression;
155 
156   explicit ExpressionStatement(Expression* expression);
157   virtual ~ExpressionStatement() = default;
158   void Write(CodeWriter* to) const override;
159 };
160 
161 struct Assignment : public Expression {
162   Variable* lvalue;
163   Expression* rvalue;
164   std::optional<std::string> cast = std::nullopt;
165 
166   Assignment(Variable* lvalue, Expression* rvalue);
167   Assignment(Variable* lvalue, Expression* rvalue, std::string cast);
168   virtual ~Assignment() = default;
169   void Write(CodeWriter* to) const override;
170 };
171 
172 struct MethodCall : public Expression {
173   std::variant<std::monostate, Expression*, std::string> receiver;
174   std::string name;
175   std::vector<Expression*> arguments;
176   std::vector<std::string> exceptions;
177 
178   explicit MethodCall(const std::string& name);
179   MethodCall(const std::string& name, int argc, ...);
180   MethodCall(Expression* obj, const std::string& name);
181   MethodCall(const std::string& clazz, const std::string& name);
182   MethodCall(Expression* obj, const std::string& name, int argc, ...);
183   MethodCall(const std::string&, const std::string& name, int argc, ...);
184   virtual ~MethodCall() = default;
185   void Write(CodeWriter* to) const override;
186 
187  private:
188   void init(int n, va_list args);
189 };
190 
191 struct Comparison : public Expression {
192   Expression* lvalue;
193   std::string op;
194   Expression* rvalue;
195 
196   Comparison(Expression* lvalue, const std::string& op, Expression* rvalue);
197   virtual ~Comparison() = default;
198   void Write(CodeWriter* to) const override;
199 };
200 
201 struct NewExpression : public Expression {
202   const std::string instantiableName;
203   std::vector<Expression*> arguments;
204 
205   explicit NewExpression(const std::string& name);
206   NewExpression(const std::string& name, int argc, ...);
207   virtual ~NewExpression() = default;
208   void Write(CodeWriter* to) const override;
209 
210  private:
211   void init(int n, va_list args);
212 };
213 
214 struct NewArrayExpression : public Expression {
215   const std::string type;
216   Expression* size;
217 
218   NewArrayExpression(const std::string& type, Expression* size);
219   virtual ~NewArrayExpression() = default;
220   void Write(CodeWriter* to) const override;
221 };
222 
223 struct Cast : public Expression {
224   const std::string type;
225   Expression* expression = nullptr;
226 
227   Cast() = default;
228   Cast(const std::string& type, Expression* expression);
229   virtual ~Cast() = default;
230   void Write(CodeWriter* to) const override;
231 };
232 
233 struct VariableDeclaration : public Statement {
234   Variable* lvalue = nullptr;
235   Expression* rvalue = nullptr;
236 
237   explicit VariableDeclaration(Variable* lvalue);
238   VariableDeclaration(Variable* lvalue, Expression* rvalue);
239   virtual ~VariableDeclaration() = default;
240   void Write(CodeWriter* to) const override;
241 };
242 
243 struct IfStatement : public Statement {
244   Expression* expression = nullptr;
245   StatementBlock* statements = new StatementBlock;
246   IfStatement* elseif = nullptr;
247 
248   IfStatement() = default;
249   virtual ~IfStatement() = default;
250   void Write(CodeWriter* to) const override;
251 };
252 
253 struct ReturnStatement : public Statement {
254   Expression* expression;
255 
256   explicit ReturnStatement(Expression* expression);
257   virtual ~ReturnStatement() = default;
258   void Write(CodeWriter* to) const override;
259 };
260 
261 struct TryStatement : public Statement {
262   StatementBlock* statements = new StatementBlock;
263 
264   TryStatement() = default;
265   virtual ~TryStatement() = default;
266   void Write(CodeWriter* to) const override;
267 };
268 
269 struct FinallyStatement : public Statement {
270   StatementBlock* statements = new StatementBlock;
271 
272   FinallyStatement() = default;
273   virtual ~FinallyStatement() = default;
274   void Write(CodeWriter* to) const override;
275 };
276 
277 struct Case : public AstNode {
278   std::vector<std::string> cases;
279   StatementBlock* statements = new StatementBlock;
280 
281   Case() = default;
282   explicit Case(const std::string& c);
283   virtual ~Case() = default;
284   void Write(CodeWriter* to) const override;
285 };
286 
287 struct SwitchStatement : public Statement {
288   Expression* expression;
289   std::vector<Case*> cases;
290 
291   explicit SwitchStatement(Expression* expression);
292   virtual ~SwitchStatement() = default;
293   void Write(CodeWriter* to) const override;
294 };
295 
296 struct Method : public ClassElement {
297   std::string comment;
298   std::vector<std::string> annotations;
299   int modifiers = 0;
300   std::optional<std::string> returnType = std::nullopt;  // nullopt means constructor
301   size_t returnTypeDimension = 0;
302   std::string name;
303   std::vector<Variable*> parameters;
304   std::vector<std::string> exceptions;
305   StatementBlock* statements = nullptr;
306 
307   Method() = default;
308   virtual ~Method() = default;
309 
310   void Write(CodeWriter* to) const override;
311 };
312 
313 struct LiteralClassElement : public ClassElement {
314   std::string element;
315 
LiteralClassElementLiteralClassElement316   LiteralClassElement(std::string e) : element(e) {}
317   virtual ~LiteralClassElement() = default;
318 
319   void Write(CodeWriter* to) const override;
320 };
321 
322 struct Class : public ClassElement {
323   enum { CLASS, INTERFACE };
324 
325   std::string comment;
326   std::vector<std::string> annotations;
327   int modifiers = 0;
328   int what = CLASS;  // CLASS or INTERFACE
329   std::string type;
330   std::optional<std::string> extends = std::nullopt;
331   std::vector<std::string> interfaces;
332   std::vector<ClassElement*> elements;
333 
334   Class() = default;
335   virtual ~Class() = default;
336 
337   void Write(CodeWriter* to) const override;
338 };
339 
340 class Document : public AstNode {
341  public:
342   Document(const std::string& comment,
343            const std::string& package,
344            std::unique_ptr<Class> clazz);
345   virtual ~Document() = default;
346   void Write(CodeWriter* to) const override;
347 
348  private:
349   std::string comment_;
350   std::string package_;
351   std::unique_ptr<Class> clazz_;
352 };
353 
354 }  // namespace java
355 }  // namespace aidl
356 }  // namespace android
357