blob: 181fb72259a56c5289d56a29c4b449fd32988dcc [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{
Christopher Wileydf03d972015-09-29 11:10:47 -070063 ClassElement() = default;
64 virtual ~ClassElement() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080065
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{
Christopher Wileydf03d972015-09-29 11:10:47 -070072 virtual ~Expression() = default;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -070081 virtual ~LiteralExpression() = default;
82 void Write(CodeWriter* to) const override;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -070091 virtual ~StringLiteralExpression() = default;
92 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080093};
94
95struct Variable : public Expression
96{
Christopher Wileydf03d972015-09-29 11:10:47 -070097 const Type* type = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -080098 string name;
Christopher Wileydf03d972015-09-29 11:10:47 -070099 int dimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100
Christopher Wileydf03d972015-09-29 11:10:47 -0700101 Variable() = default;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700104 virtual ~Variable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800105
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700119 virtual ~FieldVariable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800120
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;
Christopher Wileydf03d972015-09-29 11:10:47 -0700127 int modifiers = 0;
128 Variable *variable = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800129 string value;
130
Christopher Wileydf03d972015-09-29 11:10:47 -0700131 Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132 Field(int modifiers, Variable* variable);
Christopher Wileydf03d972015-09-29 11:10:47 -0700133 virtual ~Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134
Christopher Wileydf03d972015-09-29 11:10:47 -0700135 void GatherTypes(set<const Type*>* types) const override;
136 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800137};
138
139struct Statement
140{
Christopher Wileydf03d972015-09-29 11:10:47 -0700141 virtual ~Statement() = default;
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
Christopher Wileydf03d972015-09-29 11:10:47 -0700149 StatementBlock() = default;
150 virtual ~StatementBlock() = default;
151 void Write(CodeWriter* to) const override;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700162 virtual ~ExpressionStatement() = default;
163 void Write(CodeWriter* to) const override;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700174 virtual ~Assignment() = default;
175 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800176};
177
178struct MethodCall : public Expression
179{
Christopher Wileydf03d972015-09-29 11:10:47 -0700180 Expression* obj = nullptr;
181 const Type* clazz = nullptr;
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, ...);
Christopher Wileydf03d972015-09-29 11:10:47 -0700192 virtual ~MethodCall() = default;
193 void Write(CodeWriter* to) const override;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700206 virtual ~Comparison() = default;
207 void Write(CodeWriter* to) const override;
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, ...);
Christopher Wileydf03d972015-09-29 11:10:47 -0700217 virtual ~NewExpression() = default;
218 void Write(CodeWriter* to) const override;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700230 virtual ~NewArrayExpression() = default;
231 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232};
233
234struct Ternary : public Expression
235{
Christopher Wileydf03d972015-09-29 11:10:47 -0700236 Expression* condition = nullptr;
237 Expression* ifpart = nullptr;
238 Expression* elsepart = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239
Christopher Wileydf03d972015-09-29 11:10:47 -0700240 Ternary() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800241 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
Christopher Wileydf03d972015-09-29 11:10:47 -0700242 virtual ~Ternary() = default;
243 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800244};
245
246struct Cast : public Expression
247{
Christopher Wileydf03d972015-09-29 11:10:47 -0700248 const Type* type = nullptr;
249 Expression* expression = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250
Christopher Wileydf03d972015-09-29 11:10:47 -0700251 Cast() = default;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700252 Cast(const Type* type, Expression* expression);
Christopher Wileydf03d972015-09-29 11:10:47 -0700253 virtual ~Cast() = default;
254 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255};
256
257struct VariableDeclaration : public Statement
258{
Christopher Wileydf03d972015-09-29 11:10:47 -0700259 Variable* lvalue = nullptr;
260 const Type* cast = nullptr;
261 Expression* rvalue = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800262
263 VariableDeclaration(Variable* lvalue);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700264 VariableDeclaration(Variable* lvalue, Expression* rvalue, const Type* cast = NULL);
Christopher Wileydf03d972015-09-29 11:10:47 -0700265 virtual ~VariableDeclaration() = default;
266 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800267};
268
269struct IfStatement : public Statement
270{
Christopher Wileydf03d972015-09-29 11:10:47 -0700271 Expression* expression = nullptr;
272 StatementBlock* statements = new StatementBlock;
273 IfStatement* elseif = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800274
Christopher Wileydf03d972015-09-29 11:10:47 -0700275 IfStatement() = default;
276 virtual ~IfStatement() = default;
277 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278};
279
280struct ReturnStatement : public Statement
281{
282 Expression* expression;
283
284 ReturnStatement(Expression* expression);
Christopher Wileydf03d972015-09-29 11:10:47 -0700285 virtual ~ReturnStatement() = default;
286 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800287};
288
289struct TryStatement : public Statement
290{
Christopher Wileydf03d972015-09-29 11:10:47 -0700291 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800292
Christopher Wileydf03d972015-09-29 11:10:47 -0700293 TryStatement() = default;
294 virtual ~TryStatement() = default;
295 void Write(CodeWriter* to) const override;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700304 virtual ~CatchStatement() = default;
305 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306};
307
308struct FinallyStatement : public Statement
309{
Christopher Wileydf03d972015-09-29 11:10:47 -0700310 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311
Christopher Wileydf03d972015-09-29 11:10:47 -0700312 FinallyStatement() = default;
313 virtual ~FinallyStatement() = default;
314 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800315};
316
317struct Case
318{
319 vector<string> cases;
Christopher Wileydf03d972015-09-29 11:10:47 -0700320 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800321
Christopher Wileydf03d972015-09-29 11:10:47 -0700322 Case() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800323 Case(const string& c);
Christopher Wileydf03d972015-09-29 11:10:47 -0700324 virtual ~Case() = default;
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);
Christopher Wileydf03d972015-09-29 11:10:47 -0700334 virtual ~SwitchStatement() = default;
335 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336};
337
338struct Break : public Statement
339{
Christopher Wileydf03d972015-09-29 11:10:47 -0700340 Break() = default;
341 virtual ~Break() = default;
342 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343};
344
345struct Method : public ClassElement
346{
347 string comment;
Christopher Wileydf03d972015-09-29 11:10:47 -0700348 int modifiers = 0;
349 const Type* returnType = nullptr; // nullptr means constructor
350 size_t returnTypeDimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800351 string name;
352 vector<Variable*> parameters;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700353 vector<const Type*> exceptions;
Christopher Wileydf03d972015-09-29 11:10:47 -0700354 StatementBlock* statements = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355
Christopher Wileydf03d972015-09-29 11:10:47 -0700356 Method() = default;
357 virtual ~Method() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800358
Christopher Wileydf03d972015-09-29 11:10:47 -0700359 void GatherTypes(set<const Type*>* types) const override;
360 void Write(CodeWriter* to) const override;
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;
Christopher Wileydf03d972015-09-29 11:10:47 -0700371 int modifiers = 0;
372 int what = CLASS; // CLASS or INTERFACE
373 const Type* type = nullptr;
374 const Type* extends = nullptr;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700375 vector<const Type*> interfaces;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800376 vector<ClassElement*> elements;
377
Christopher Wileydf03d972015-09-29 11:10:47 -0700378 Class() = default;
379 virtual ~Class() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800380
Christopher Wileydf03d972015-09-29 11:10:47 -0700381 void GatherTypes(set<const Type*>* types) const override;
382 void Write(CodeWriter* to) const override;
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
Christopher Wileydf03d972015-09-29 11:10:47 -0700393 Document() = default;
394 virtual ~Document() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800395
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_