blob: 0640b2a6a591653946b300763330560e55523f48 [file] [log] [blame]
Christopher Wiley2f774172015-08-30 10:57:07 -07001#ifndef AIDL_AST_H_
2#define AIDL_AST_H_
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003
4#include <string>
5#include <vector>
6#include <set>
7#include <stdarg.h>
Jack Palevichc7410f62009-06-24 19:27:30 -07008#include <stdio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009
Christopher Wiley1eaa9ed2015-08-24 14:07:32 -070010using std::set;
11using std::string;
12using std::vector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013
14class Type;
15
16enum {
17 PACKAGE_PRIVATE = 0x00000000,
18 PUBLIC = 0x00000001,
19 PRIVATE = 0x00000002,
20 PROTECTED = 0x00000003,
21 SCOPE_MASK = 0x00000003,
22
23 STATIC = 0x00000010,
24 FINAL = 0x00000020,
25 ABSTRACT = 0x00000040,
26
Xavier Ducrohet7ea9d792009-08-03 19:51:54 -070027 OVERRIDE = 0x00000100,
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 ALL_MODIFIERS = 0xffffffff
30};
31
32// Write the modifiers that are set in both mod and mask
33void WriteModifiers(FILE* to, int mod, int mask);
34
35struct ClassElement
36{
37 ClassElement();
38 virtual ~ClassElement();
39
40 virtual void GatherTypes(set<Type*>* types) const = 0;
41 virtual void Write(FILE* to) = 0;
42};
43
44struct Expression
45{
46 virtual ~Expression();
47 virtual void Write(FILE* to) = 0;
48};
49
50struct LiteralExpression : public Expression
51{
52 string value;
53
54 LiteralExpression(const string& value);
55 virtual ~LiteralExpression();
56 virtual void Write(FILE* to);
57};
58
Joe Onoratofdfe2ff2011-08-30 17:24:17 -070059// TODO: also escape the contents. not needed for now
60struct StringLiteralExpression : public Expression
61{
62 string value;
63
64 StringLiteralExpression(const string& value);
65 virtual ~StringLiteralExpression();
66 virtual void Write(FILE* to);
67};
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069struct Variable : public Expression
70{
71 Type* type;
72 string name;
73 int dimension;
74
75 Variable();
76 Variable(Type* type, const string& name);
77 Variable(Type* type, const string& name, int dimension);
78 virtual ~Variable();
79
80 virtual void GatherTypes(set<Type*>* types) const;
81 void WriteDeclaration(FILE* to);
82 void Write(FILE* to);
83};
84
85struct FieldVariable : public Expression
86{
87 Expression* object;
88 Type* clazz;
89 string name;
90
91 FieldVariable(Expression* object, const string& name);
92 FieldVariable(Type* clazz, const string& name);
93 virtual ~FieldVariable();
94
95 void Write(FILE* to);
96};
97
98struct Field : public ClassElement
99{
100 string comment;
101 int modifiers;
102 Variable *variable;
103 string value;
104
105 Field();
106 Field(int modifiers, Variable* variable);
107 virtual ~Field();
108
109 virtual void GatherTypes(set<Type*>* types) const;
110 virtual void Write(FILE* to);
111};
112
113struct Statement
114{
115 virtual ~Statement();
116 virtual void Write(FILE* to) = 0;
117};
118
Joe Onorato7db766c2011-09-15 21:31:15 -0700119struct StatementBlock : public Statement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120{
121 vector<Statement*> statements;
122
123 StatementBlock();
124 virtual ~StatementBlock();
125 virtual void Write(FILE* to);
126
127 void Add(Statement* statement);
128 void Add(Expression* expression);
129};
130
131struct ExpressionStatement : public Statement
132{
133 Expression* expression;
134
135 ExpressionStatement(Expression* expression);
136 virtual ~ExpressionStatement();
137 virtual void Write(FILE* to);
138};
139
140struct Assignment : public Expression
141{
142 Variable* lvalue;
143 Expression* rvalue;
144 Type* cast;
145
146 Assignment(Variable* lvalue, Expression* rvalue);
147 Assignment(Variable* lvalue, Expression* rvalue, Type* cast);
148 virtual ~Assignment();
149 virtual void Write(FILE* to);
150};
151
152struct MethodCall : public Expression
153{
154 Expression* obj;
155 Type* clazz;
156 string name;
157 vector<Expression*> arguments;
158 vector<string> exceptions;
159
160 MethodCall(const string& name);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700161 MethodCall(const string& name, int argc, ...);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 MethodCall(Expression* obj, const string& name);
163 MethodCall(Type* clazz, const string& name);
164 MethodCall(Expression* obj, const string& name, int argc, ...);
165 MethodCall(Type* clazz, const string& name, int argc, ...);
166 virtual ~MethodCall();
167 virtual void Write(FILE* to);
168
169private:
170 void init(int n, va_list args);
171};
172
173struct Comparison : public Expression
174{
175 Expression* lvalue;
176 string op;
177 Expression* rvalue;
178
179 Comparison(Expression* lvalue, const string& op, Expression* rvalue);
180 virtual ~Comparison();
181 virtual void Write(FILE* to);
182};
183
184struct NewExpression : public Expression
185{
186 Type* type;
187 vector<Expression*> arguments;
188
189 NewExpression(Type* type);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700190 NewExpression(Type* type, int argc, ...);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 virtual ~NewExpression();
192 virtual void Write(FILE* to);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700193
194private:
195 void init(int n, va_list args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196};
197
198struct NewArrayExpression : public Expression
199{
200 Type* type;
201 Expression* size;
202
203 NewArrayExpression(Type* type, Expression* size);
204 virtual ~NewArrayExpression();
205 virtual void Write(FILE* to);
206};
207
208struct Ternary : public Expression
209{
210 Expression* condition;
211 Expression* ifpart;
212 Expression* elsepart;
213
214 Ternary();
215 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
216 virtual ~Ternary();
217 virtual void Write(FILE* to);
218};
219
220struct Cast : public Expression
221{
222 Type* type;
223 Expression* expression;
224
225 Cast();
226 Cast(Type* type, Expression* expression);
227 virtual ~Cast();
228 virtual void Write(FILE* to);
229};
230
231struct VariableDeclaration : public Statement
232{
233 Variable* lvalue;
234 Type* cast;
235 Expression* rvalue;
236
237 VariableDeclaration(Variable* lvalue);
238 VariableDeclaration(Variable* lvalue, Expression* rvalue, Type* cast = NULL);
239 virtual ~VariableDeclaration();
240 virtual void Write(FILE* to);
241};
242
243struct IfStatement : public Statement
244{
245 Expression* expression;
246 StatementBlock* statements;
247 IfStatement* elseif;
248
249 IfStatement();
250 virtual ~IfStatement();
251 virtual void Write(FILE* to);
252};
253
254struct ReturnStatement : public Statement
255{
256 Expression* expression;
257
258 ReturnStatement(Expression* expression);
259 virtual ~ReturnStatement();
260 virtual void Write(FILE* to);
261};
262
263struct TryStatement : public Statement
264{
265 StatementBlock* statements;
266
267 TryStatement();
268 virtual ~TryStatement();
269 virtual void Write(FILE* to);
270};
271
272struct CatchStatement : public Statement
273{
274 StatementBlock* statements;
275 Variable* exception;
276
277 CatchStatement(Variable* exception);
278 virtual ~CatchStatement();
279 virtual void Write(FILE* to);
280};
281
282struct FinallyStatement : public Statement
283{
284 StatementBlock* statements;
285
286 FinallyStatement();
287 virtual ~FinallyStatement();
288 virtual void Write(FILE* to);
289};
290
291struct Case
292{
293 vector<string> cases;
294 StatementBlock* statements;
295
296 Case();
297 Case(const string& c);
298 virtual ~Case();
299 virtual void Write(FILE* to);
300};
301
302struct SwitchStatement : public Statement
303{
304 Expression* expression;
305 vector<Case*> cases;
306
307 SwitchStatement(Expression* expression);
308 virtual ~SwitchStatement();
309 virtual void Write(FILE* to);
310};
311
Joe Onorato05ffbe72011-09-02 15:28:36 -0700312struct Break : public Statement
313{
314 Break();
315 virtual ~Break();
316 virtual void Write(FILE* to);
317};
318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319struct Method : public ClassElement
320{
321 string comment;
322 int modifiers;
323 Type* returnType;
324 size_t returnTypeDimension;
325 string name;
326 vector<Variable*> parameters;
327 vector<Type*> exceptions;
328 StatementBlock* statements;
329
330 Method();
331 virtual ~Method();
332
333 virtual void GatherTypes(set<Type*>* types) const;
334 virtual void Write(FILE* to);
335};
336
337struct Class : public ClassElement
338{
339 enum {
340 CLASS,
341 INTERFACE
342 };
343
344 string comment;
345 int modifiers;
346 int what; // CLASS or INTERFACE
347 Type* type;
348 Type* extends;
349 vector<Type*> interfaces;
350 vector<ClassElement*> elements;
351
352 Class();
353 virtual ~Class();
354
355 virtual void GatherTypes(set<Type*>* types) const;
356 virtual void Write(FILE* to);
357};
358
359struct Document
360{
361 string comment;
362 string package;
363 string originalSrc;
364 set<Type*> imports;
365 vector<Class*> classes;
366
367 Document();
368 virtual ~Document();
369
370 virtual void Write(FILE* to);
371};
372
Christopher Wiley2f774172015-08-30 10:57:07 -0700373#endif // AIDL_AST_H_