blob: 401018b0f1d8be3b648e7e6d207a4ae6204e197b [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
Christopher Wiley8f6816e2015-09-22 17:03:47 -070060 virtual void GatherTypes(set<const 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{
Christopher Wiley8f6816e2015-09-22 17:03:47 -070091 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -080092 string name;
93 int dimension;
94
95 Variable();
Christopher Wiley8f6816e2015-09-22 17:03:47 -070096 Variable(const Type* type, const string& name);
97 Variable(const Type* type, const string& name, int dimension);
Adam Lesinskiffa16862014-01-23 18:17:42 -080098 virtual ~Variable();
99
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700100 virtual void GatherTypes(set<const 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;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700108 const Type* clazz;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109 string name;
110
111 FieldVariable(Expression* object, const string& name);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700112 FieldVariable(const Type* clazz, const string& name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800113 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
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700129 virtual void GatherTypes(set<const 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;
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700164 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800165
166 Assignment(Variable* lvalue, Expression* rvalue);
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700167 Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800168 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;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700175 const Type* clazz;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800176 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);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700183 MethodCall(const Type* clazz, const string& name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800184 MethodCall(Expression* obj, const string& name, int argc, ...);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700185 MethodCall(const Type* clazz, const string& name, int argc, ...);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800186 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{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700206 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800207 vector<Expression*> arguments;
208
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700209 NewExpression(const Type* type);
210 NewExpression(const Type* type, int argc, ...);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800211 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{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700220 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800221 Expression* size;
222
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700223 NewArrayExpression(const Type* type, Expression* size);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224 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{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700242 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800243 Expression* expression;
244
245 Cast();
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700246 Cast(const Type* type, Expression* expression);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247 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;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700254 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255 Expression* rvalue;
256
257 VariableDeclaration(Variable* lvalue);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700258 VariableDeclaration(Variable* lvalue, Expression* rvalue, const Type* cast = NULL);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259 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;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700343 const Type* returnType;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800344 size_t returnTypeDimension;
345 string name;
346 vector<Variable*> parameters;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700347 vector<const Type*> exceptions;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348 StatementBlock* statements;
349
350 Method();
351 virtual ~Method();
352
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700353 virtual void GatherTypes(set<const 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
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700367 const Type* type;
368 const Type* extends;
369 vector<const Type*> interfaces;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800370 vector<ClassElement*> elements;
371
372 Class();
373 virtual ~Class();
374
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700375 virtual void GatherTypes(set<const 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;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700384 set<const Type*> imports;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800385 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_