blob: 8c4d825b92b76b3918d77fb7e5d5ada3f275d11d [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 {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070048class CodeWriter;
Christopher Wileydb154a52015-09-28 16:32:25 -070049} // namespace aidl
50} // namespace android
51
52namespace android {
53namespace aidl {
54namespace java {
55
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070056class Type;
57
Adam Lesinskiffa16862014-01-23 18:17:42 -080058// Write the modifiers that are set in both mod and mask
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070059void WriteModifiers(CodeWriter* to, int mod, int mask);
Adam Lesinskiffa16862014-01-23 18:17:42 -080060
61struct ClassElement
62{
63 ClassElement();
64 virtual ~ClassElement();
65
Christopher Wiley8f6816e2015-09-22 17:03:47 -070066 virtual void GatherTypes(set<const Type*>* types) const = 0;
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 Expression
71{
72 virtual ~Expression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070073 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080074};
75
76struct LiteralExpression : public Expression
77{
78 string value;
79
80 LiteralExpression(const string& value);
81 virtual ~LiteralExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070082 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -080083};
84
85// TODO: also escape the contents. not needed for now
86struct StringLiteralExpression : public Expression
87{
88 string value;
89
90 StringLiteralExpression(const string& value);
91 virtual ~StringLiteralExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070092 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -080093};
94
95struct Variable : public Expression
96{
Christopher Wiley8f6816e2015-09-22 17:03:47 -070097 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -080098 string name;
99 int dimension;
100
101 Variable();
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700102 Variable(const Type* type, const string& name);
103 Variable(const Type* type, const string& name, int dimension);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104 virtual ~Variable();
105
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700106 virtual void GatherTypes(set<const Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700107 void WriteDeclaration(CodeWriter* to) const;
108 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109};
110
111struct FieldVariable : public Expression
112{
113 Expression* object;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700114 const Type* clazz;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115 string name;
116
117 FieldVariable(Expression* object, const string& name);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700118 FieldVariable(const Type* clazz, const string& name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800119 virtual ~FieldVariable();
120
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700121 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122};
123
124struct Field : public ClassElement
125{
126 string comment;
127 int modifiers;
128 Variable *variable;
129 string value;
130
131 Field();
132 Field(int modifiers, Variable* variable);
133 virtual ~Field();
134
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700135 virtual void GatherTypes(set<const Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700136 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800137};
138
139struct Statement
140{
141 virtual ~Statement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700142 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800143};
144
145struct StatementBlock : public Statement
146{
147 vector<Statement*> statements;
148
149 StatementBlock();
150 virtual ~StatementBlock();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700151 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152
153 void Add(Statement* statement);
154 void Add(Expression* expression);
155};
156
157struct ExpressionStatement : public Statement
158{
159 Expression* expression;
160
161 ExpressionStatement(Expression* expression);
162 virtual ~ExpressionStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700163 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800164};
165
166struct Assignment : public Expression
167{
168 Variable* lvalue;
169 Expression* rvalue;
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700170 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800171
172 Assignment(Variable* lvalue, Expression* rvalue);
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700173 Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800174 virtual ~Assignment();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700175 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800176};
177
178struct MethodCall : public Expression
179{
180 Expression* obj;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700181 const Type* clazz;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800182 string name;
183 vector<Expression*> arguments;
184 vector<string> exceptions;
185
186 MethodCall(const string& name);
187 MethodCall(const string& name, int argc, ...);
188 MethodCall(Expression* obj, const string& name);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700189 MethodCall(const Type* clazz, const string& name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800190 MethodCall(Expression* obj, const string& name, int argc, ...);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700191 MethodCall(const Type* clazz, const string& name, int argc, ...);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800192 virtual ~MethodCall();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700193 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800194
195private:
196 void init(int n, va_list args);
197};
198
199struct Comparison : public Expression
200{
201 Expression* lvalue;
202 string op;
203 Expression* rvalue;
204
205 Comparison(Expression* lvalue, const string& op, Expression* rvalue);
206 virtual ~Comparison();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700207 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800208};
209
210struct NewExpression : public Expression
211{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700212 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800213 vector<Expression*> arguments;
214
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700215 NewExpression(const Type* type);
216 NewExpression(const Type* type, int argc, ...);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217 virtual ~NewExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700218 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219
220private:
221 void init(int n, va_list args);
222};
223
224struct NewArrayExpression : public Expression
225{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700226 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800227 Expression* size;
228
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700229 NewArrayExpression(const Type* type, Expression* size);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800230 virtual ~NewArrayExpression();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700231 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232};
233
234struct Ternary : public Expression
235{
236 Expression* condition;
237 Expression* ifpart;
238 Expression* elsepart;
239
240 Ternary();
241 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
242 virtual ~Ternary();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700243 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800244};
245
246struct Cast : public Expression
247{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700248 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249 Expression* expression;
250
251 Cast();
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700252 Cast(const Type* type, Expression* expression);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253 virtual ~Cast();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700254 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255};
256
257struct VariableDeclaration : public Statement
258{
259 Variable* lvalue;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700260 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800261 Expression* rvalue;
262
263 VariableDeclaration(Variable* lvalue);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700264 VariableDeclaration(Variable* lvalue, Expression* rvalue, const Type* cast = NULL);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265 virtual ~VariableDeclaration();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700266 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800267};
268
269struct IfStatement : public Statement
270{
271 Expression* expression;
272 StatementBlock* statements;
273 IfStatement* elseif;
274
275 IfStatement();
276 virtual ~IfStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700277 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278};
279
280struct ReturnStatement : public Statement
281{
282 Expression* expression;
283
284 ReturnStatement(Expression* expression);
285 virtual ~ReturnStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700286 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800287};
288
289struct TryStatement : public Statement
290{
291 StatementBlock* statements;
292
293 TryStatement();
294 virtual ~TryStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700295 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296};
297
298struct CatchStatement : public Statement
299{
300 StatementBlock* statements;
301 Variable* exception;
302
303 CatchStatement(Variable* exception);
304 virtual ~CatchStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700305 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306};
307
308struct FinallyStatement : public Statement
309{
310 StatementBlock* statements;
311
312 FinallyStatement();
313 virtual ~FinallyStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700314 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800315};
316
317struct Case
318{
319 vector<string> cases;
320 StatementBlock* statements;
321
322 Case();
323 Case(const string& c);
324 virtual ~Case();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700325 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800326};
327
328struct SwitchStatement : public Statement
329{
330 Expression* expression;
331 vector<Case*> cases;
332
333 SwitchStatement(Expression* expression);
334 virtual ~SwitchStatement();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700335 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336};
337
338struct Break : public Statement
339{
340 Break();
341 virtual ~Break();
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700342 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343};
344
345struct Method : public ClassElement
346{
347 string comment;
348 int modifiers;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700349 const Type* returnType;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350 size_t returnTypeDimension;
351 string name;
352 vector<Variable*> parameters;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700353 vector<const Type*> exceptions;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800354 StatementBlock* statements;
355
356 Method();
357 virtual ~Method();
358
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700359 virtual void GatherTypes(set<const Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700360 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800361};
362
363struct Class : public ClassElement
364{
365 enum {
366 CLASS,
367 INTERFACE
368 };
369
370 string comment;
371 int modifiers;
372 int what; // CLASS or INTERFACE
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700373 const Type* type;
374 const Type* extends;
375 vector<const Type*> interfaces;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800376 vector<ClassElement*> elements;
377
378 Class();
379 virtual ~Class();
380
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700381 virtual void GatherTypes(set<const Type*>* types) const;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700382 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383};
384
385struct Document
386{
387 string comment;
388 string package;
389 string originalSrc;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700390 set<const Type*> imports;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800391 vector<Class*> classes;
392
393 Document();
394 virtual ~Document();
395
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700396 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800397};
398
Christopher Wileydb154a52015-09-28 16:32:25 -0700399} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700400} // namespace aidl
401} // namespace android
402
Christopher Wiley038485e2015-09-12 11:14:14 -0700403#endif // AIDL_AST_JAVA_H_