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