blob: 7f6b9dcc24fa2f5a67920407cedb4032938d1e5e [file] [log] [blame]
Christopher Wiley038485e2015-09-12 11:14:14 -07001/*
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#ifndef AIDL_AST_JAVA_H_
18#define AIDL_AST_JAVA_H_
Adam Lesinskiffa16862014-01-23 18:17:42 -080019
20#include <string>
21#include <vector>
22#include <set>
23#include <stdarg.h>
24#include <stdio.h>
25
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070026using std::set;
27using std::string;
28using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080029
Adam Lesinskiffa16862014-01-23 18:17:42 -080030enum {
31 PACKAGE_PRIVATE = 0x00000000,
32 PUBLIC = 0x00000001,
33 PRIVATE = 0x00000002,
34 PROTECTED = 0x00000003,
35 SCOPE_MASK = 0x00000003,
36
37 STATIC = 0x00000010,
38 FINAL = 0x00000020,
39 ABSTRACT = 0x00000040,
40
41 OVERRIDE = 0x00000100,
42
43 ALL_MODIFIERS = 0xffffffff
44};
45
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070046namespace android {
47namespace aidl {
48
49class CodeWriter;
50class Type;
51
Adam Lesinskiffa16862014-01-23 18:17:42 -080052// Write the modifiers that are set in both mod and mask
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070053void WriteModifiers(CodeWriter* to, int mod, int mask);
Adam Lesinskiffa16862014-01-23 18:17:42 -080054
55struct ClassElement
56{
57 ClassElement();
58 virtual ~ClassElement();
59
60 virtual void GatherTypes(set<Type*>* types) const = 0;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070061 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080062};
63
64struct Expression
65{
66 virtual ~Expression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070067 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080068};
69
70struct LiteralExpression : public Expression
71{
72 string value;
73
74 LiteralExpression(const string& value);
75 virtual ~LiteralExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070076 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -080077};
78
79// TODO: also escape the contents. not needed for now
80struct StringLiteralExpression : public Expression
81{
82 string value;
83
84 StringLiteralExpression(const string& value);
85 virtual ~StringLiteralExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070086 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -080087};
88
89struct Variable : public Expression
90{
91 Type* type;
92 string name;
93 int dimension;
94
95 Variable();
96 Variable(Type* type, const string& name);
97 Variable(Type* type, const string& name, int dimension);
98 virtual ~Variable();
99
100 virtual void GatherTypes(set<Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700101 void WriteDeclaration(CodeWriter* to) const;
102 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103};
104
105struct FieldVariable : public Expression
106{
107 Expression* object;
108 Type* clazz;
109 string name;
110
111 FieldVariable(Expression* object, const string& name);
112 FieldVariable(Type* clazz, const string& name);
113 virtual ~FieldVariable();
114
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700115 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800116};
117
118struct Field : public ClassElement
119{
120 string comment;
121 int modifiers;
122 Variable *variable;
123 string value;
124
125 Field();
126 Field(int modifiers, Variable* variable);
127 virtual ~Field();
128
129 virtual void GatherTypes(set<Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700130 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131};
132
133struct Statement
134{
135 virtual ~Statement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700136 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800137};
138
139struct StatementBlock : public Statement
140{
141 vector<Statement*> statements;
142
143 StatementBlock();
144 virtual ~StatementBlock();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700145 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800146
147 void Add(Statement* statement);
148 void Add(Expression* expression);
149};
150
151struct ExpressionStatement : public Statement
152{
153 Expression* expression;
154
155 ExpressionStatement(Expression* expression);
156 virtual ~ExpressionStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700157 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800158};
159
160struct Assignment : public Expression
161{
162 Variable* lvalue;
163 Expression* rvalue;
164 Type* cast;
165
166 Assignment(Variable* lvalue, Expression* rvalue);
167 Assignment(Variable* lvalue, Expression* rvalue, Type* cast);
168 virtual ~Assignment();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700169 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800170};
171
172struct MethodCall : public Expression
173{
174 Expression* obj;
175 Type* clazz;
176 string name;
177 vector<Expression*> arguments;
178 vector<string> exceptions;
179
180 MethodCall(const string& name);
181 MethodCall(const string& name, int argc, ...);
182 MethodCall(Expression* obj, const string& name);
183 MethodCall(Type* clazz, const string& name);
184 MethodCall(Expression* obj, const string& name, int argc, ...);
185 MethodCall(Type* clazz, const string& name, int argc, ...);
186 virtual ~MethodCall();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700187 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188
189private:
190 void init(int n, va_list args);
191};
192
193struct Comparison : public Expression
194{
195 Expression* lvalue;
196 string op;
197 Expression* rvalue;
198
199 Comparison(Expression* lvalue, const string& op, Expression* rvalue);
200 virtual ~Comparison();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700201 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800202};
203
204struct NewExpression : public Expression
205{
206 Type* type;
207 vector<Expression*> arguments;
208
209 NewExpression(Type* type);
210 NewExpression(Type* type, int argc, ...);
211 virtual ~NewExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700212 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800213
214private:
215 void init(int n, va_list args);
216};
217
218struct NewArrayExpression : public Expression
219{
220 Type* type;
221 Expression* size;
222
223 NewArrayExpression(Type* type, Expression* size);
224 virtual ~NewArrayExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700225 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226};
227
228struct Ternary : public Expression
229{
230 Expression* condition;
231 Expression* ifpart;
232 Expression* elsepart;
233
234 Ternary();
235 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
236 virtual ~Ternary();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700237 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800238};
239
240struct Cast : public Expression
241{
242 Type* type;
243 Expression* expression;
244
245 Cast();
246 Cast(Type* type, Expression* expression);
247 virtual ~Cast();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700248 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249};
250
251struct VariableDeclaration : public Statement
252{
253 Variable* lvalue;
254 Type* cast;
255 Expression* rvalue;
256
257 VariableDeclaration(Variable* lvalue);
258 VariableDeclaration(Variable* lvalue, Expression* rvalue, Type* cast = NULL);
259 virtual ~VariableDeclaration();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700260 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800261};
262
263struct IfStatement : public Statement
264{
265 Expression* expression;
266 StatementBlock* statements;
267 IfStatement* elseif;
268
269 IfStatement();
270 virtual ~IfStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700271 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800272};
273
274struct ReturnStatement : public Statement
275{
276 Expression* expression;
277
278 ReturnStatement(Expression* expression);
279 virtual ~ReturnStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700280 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281};
282
283struct TryStatement : public Statement
284{
285 StatementBlock* statements;
286
287 TryStatement();
288 virtual ~TryStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700289 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290};
291
292struct CatchStatement : public Statement
293{
294 StatementBlock* statements;
295 Variable* exception;
296
297 CatchStatement(Variable* exception);
298 virtual ~CatchStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700299 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300};
301
302struct FinallyStatement : public Statement
303{
304 StatementBlock* statements;
305
306 FinallyStatement();
307 virtual ~FinallyStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700308 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800309};
310
311struct Case
312{
313 vector<string> cases;
314 StatementBlock* statements;
315
316 Case();
317 Case(const string& c);
318 virtual ~Case();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700319 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800320};
321
322struct SwitchStatement : public Statement
323{
324 Expression* expression;
325 vector<Case*> cases;
326
327 SwitchStatement(Expression* expression);
328 virtual ~SwitchStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700329 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800330};
331
332struct Break : public Statement
333{
334 Break();
335 virtual ~Break();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700336 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800337};
338
339struct Method : public ClassElement
340{
341 string comment;
342 int modifiers;
343 Type* returnType;
344 size_t returnTypeDimension;
345 string name;
346 vector<Variable*> parameters;
347 vector<Type*> exceptions;
348 StatementBlock* statements;
349
350 Method();
351 virtual ~Method();
352
353 virtual void GatherTypes(set<Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700354 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355};
356
357struct Class : public ClassElement
358{
359 enum {
360 CLASS,
361 INTERFACE
362 };
363
364 string comment;
365 int modifiers;
366 int what; // CLASS or INTERFACE
367 Type* type;
368 Type* extends;
369 vector<Type*> interfaces;
370 vector<ClassElement*> elements;
371
372 Class();
373 virtual ~Class();
374
375 virtual void GatherTypes(set<Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700376 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800377};
378
379struct Document
380{
381 string comment;
382 string package;
383 string originalSrc;
384 set<Type*> imports;
385 vector<Class*> classes;
386
387 Document();
388 virtual ~Document();
389
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700390 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800391};
392
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700393} // namespace aidl
394} // namespace android
395
Christopher Wiley038485e2015-09-12 11:14:14 -0700396#endif // AIDL_AST_JAVA_H_