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