blob: f852e881029edc3e8bef239ea326f49d2e278afe [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 Wileyfdeb0f42015-09-11 15:38:22 -070066 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080067};
68
69struct Expression
70{
Christopher Wileydf03d972015-09-29 11:10:47 -070071 virtual ~Expression() = default;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070072 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080073};
74
75struct LiteralExpression : public Expression
76{
77 string value;
78
79 LiteralExpression(const string& value);
Christopher Wileydf03d972015-09-29 11:10:47 -070080 virtual ~LiteralExpression() = default;
81 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080082};
83
84// TODO: also escape the contents. not needed for now
85struct StringLiteralExpression : public Expression
86{
87 string value;
88
89 StringLiteralExpression(const string& value);
Christopher Wileydf03d972015-09-29 11:10:47 -070090 virtual ~StringLiteralExpression() = default;
91 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080092};
93
94struct Variable : public Expression
95{
Christopher Wileydf03d972015-09-29 11:10:47 -070096 const Type* type = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -080097 string name;
Christopher Wileydf03d972015-09-29 11:10:47 -070098 int dimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080099
Christopher Wileydf03d972015-09-29 11:10:47 -0700100 Variable() = default;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700101 Variable(const Type* type, const string& name);
102 Variable(const Type* type, const string& name, int dimension);
Christopher Wileydf03d972015-09-29 11:10:47 -0700103 virtual ~Variable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700105 void WriteDeclaration(CodeWriter* to) const;
106 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800107};
108
109struct FieldVariable : public Expression
110{
111 Expression* object;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700112 const Type* clazz;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800113 string name;
114
115 FieldVariable(Expression* object, const string& name);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700116 FieldVariable(const Type* clazz, const string& name);
Christopher Wileydf03d972015-09-29 11:10:47 -0700117 virtual ~FieldVariable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700119 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800120};
121
122struct Field : public ClassElement
123{
124 string comment;
Christopher Wileydf03d972015-09-29 11:10:47 -0700125 int modifiers = 0;
126 Variable *variable = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800127 string value;
128
Christopher Wileydf03d972015-09-29 11:10:47 -0700129 Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800130 Field(int modifiers, Variable* variable);
Christopher Wileydf03d972015-09-29 11:10:47 -0700131 virtual ~Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132
Christopher Wileydf03d972015-09-29 11:10:47 -0700133 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134};
135
136struct Statement
137{
Christopher Wileydf03d972015-09-29 11:10:47 -0700138 virtual ~Statement() = default;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700139 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800140};
141
142struct StatementBlock : public Statement
143{
144 vector<Statement*> statements;
145
Christopher Wileydf03d972015-09-29 11:10:47 -0700146 StatementBlock() = default;
147 virtual ~StatementBlock() = default;
148 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149
150 void Add(Statement* statement);
151 void Add(Expression* expression);
152};
153
154struct ExpressionStatement : public Statement
155{
156 Expression* expression;
157
158 ExpressionStatement(Expression* expression);
Christopher Wileydf03d972015-09-29 11:10:47 -0700159 virtual ~ExpressionStatement() = default;
160 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800161};
162
163struct Assignment : public Expression
164{
165 Variable* lvalue;
166 Expression* rvalue;
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700167 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800168
169 Assignment(Variable* lvalue, Expression* rvalue);
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700170 Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
Christopher Wileydf03d972015-09-29 11:10:47 -0700171 virtual ~Assignment() = default;
172 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800173};
174
175struct MethodCall : public Expression
176{
Christopher Wileydf03d972015-09-29 11:10:47 -0700177 Expression* obj = nullptr;
178 const Type* clazz = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800179 string name;
180 vector<Expression*> arguments;
181 vector<string> exceptions;
182
183 MethodCall(const string& name);
184 MethodCall(const string& name, int argc, ...);
185 MethodCall(Expression* obj, const string& name);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700186 MethodCall(const Type* clazz, const string& name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800187 MethodCall(Expression* obj, const string& name, int argc, ...);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700188 MethodCall(const Type* clazz, const string& name, int argc, ...);
Christopher Wileydf03d972015-09-29 11:10:47 -0700189 virtual ~MethodCall() = default;
190 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800191
192private:
193 void init(int n, va_list args);
194};
195
196struct Comparison : public Expression
197{
198 Expression* lvalue;
199 string op;
200 Expression* rvalue;
201
202 Comparison(Expression* lvalue, const string& op, Expression* rvalue);
Christopher Wileydf03d972015-09-29 11:10:47 -0700203 virtual ~Comparison() = default;
204 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800205};
206
207struct NewExpression : public Expression
208{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700209 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800210 vector<Expression*> arguments;
211
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700212 NewExpression(const Type* type);
213 NewExpression(const Type* type, int argc, ...);
Christopher Wileydf03d972015-09-29 11:10:47 -0700214 virtual ~NewExpression() = default;
215 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216
217private:
218 void init(int n, va_list args);
219};
220
221struct NewArrayExpression : public Expression
222{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700223 const Type* type;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224 Expression* size;
225
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700226 NewArrayExpression(const Type* type, Expression* size);
Christopher Wileydf03d972015-09-29 11:10:47 -0700227 virtual ~NewArrayExpression() = default;
228 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800229};
230
231struct Ternary : public Expression
232{
Christopher Wileydf03d972015-09-29 11:10:47 -0700233 Expression* condition = nullptr;
234 Expression* ifpart = nullptr;
235 Expression* elsepart = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236
Christopher Wileydf03d972015-09-29 11:10:47 -0700237 Ternary() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800238 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
Christopher Wileydf03d972015-09-29 11:10:47 -0700239 virtual ~Ternary() = default;
240 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800241};
242
243struct Cast : public Expression
244{
Christopher Wileydf03d972015-09-29 11:10:47 -0700245 const Type* type = nullptr;
246 Expression* expression = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247
Christopher Wileydf03d972015-09-29 11:10:47 -0700248 Cast() = default;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700249 Cast(const Type* type, Expression* expression);
Christopher Wileydf03d972015-09-29 11:10:47 -0700250 virtual ~Cast() = default;
251 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252};
253
254struct VariableDeclaration : public Statement
255{
Christopher Wileydf03d972015-09-29 11:10:47 -0700256 Variable* lvalue = nullptr;
257 const Type* cast = nullptr;
258 Expression* rvalue = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259
260 VariableDeclaration(Variable* lvalue);
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700261 VariableDeclaration(Variable* lvalue, Expression* rvalue, const Type* cast = NULL);
Christopher Wileydf03d972015-09-29 11:10:47 -0700262 virtual ~VariableDeclaration() = default;
263 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800264};
265
266struct IfStatement : public Statement
267{
Christopher Wileydf03d972015-09-29 11:10:47 -0700268 Expression* expression = nullptr;
269 StatementBlock* statements = new StatementBlock;
270 IfStatement* elseif = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800271
Christopher Wileydf03d972015-09-29 11:10:47 -0700272 IfStatement() = default;
273 virtual ~IfStatement() = default;
274 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800275};
276
277struct ReturnStatement : public Statement
278{
279 Expression* expression;
280
281 ReturnStatement(Expression* expression);
Christopher Wileydf03d972015-09-29 11:10:47 -0700282 virtual ~ReturnStatement() = default;
283 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800284};
285
286struct TryStatement : public Statement
287{
Christopher Wileydf03d972015-09-29 11:10:47 -0700288 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800289
Christopher Wileydf03d972015-09-29 11:10:47 -0700290 TryStatement() = default;
291 virtual ~TryStatement() = default;
292 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800293};
294
295struct CatchStatement : public Statement
296{
297 StatementBlock* statements;
298 Variable* exception;
299
300 CatchStatement(Variable* exception);
Christopher Wileydf03d972015-09-29 11:10:47 -0700301 virtual ~CatchStatement() = default;
302 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303};
304
305struct FinallyStatement : public Statement
306{
Christopher Wileydf03d972015-09-29 11:10:47 -0700307 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308
Christopher Wileydf03d972015-09-29 11:10:47 -0700309 FinallyStatement() = default;
310 virtual ~FinallyStatement() = default;
311 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800312};
313
314struct Case
315{
316 vector<string> cases;
Christopher Wileydf03d972015-09-29 11:10:47 -0700317 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800318
Christopher Wileydf03d972015-09-29 11:10:47 -0700319 Case() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800320 Case(const string& c);
Christopher Wileydf03d972015-09-29 11:10:47 -0700321 virtual ~Case() = default;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700322 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800323};
324
325struct SwitchStatement : public Statement
326{
327 Expression* expression;
328 vector<Case*> cases;
329
330 SwitchStatement(Expression* expression);
Christopher Wileydf03d972015-09-29 11:10:47 -0700331 virtual ~SwitchStatement() = default;
332 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800333};
334
335struct Break : public Statement
336{
Christopher Wileydf03d972015-09-29 11:10:47 -0700337 Break() = default;
338 virtual ~Break() = default;
339 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800340};
341
342struct Method : public ClassElement
343{
344 string comment;
Christopher Wileydf03d972015-09-29 11:10:47 -0700345 int modifiers = 0;
346 const Type* returnType = nullptr; // nullptr means constructor
347 size_t returnTypeDimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348 string name;
349 vector<Variable*> parameters;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700350 vector<const Type*> exceptions;
Christopher Wileydf03d972015-09-29 11:10:47 -0700351 StatementBlock* statements = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800352
Christopher Wileydf03d972015-09-29 11:10:47 -0700353 Method() = default;
354 virtual ~Method() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355
Christopher Wileydf03d972015-09-29 11:10:47 -0700356 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357};
358
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800359struct Constant : public ClassElement
360{
361 string name;
362 int value;
363
364 Constant() = default;
365 virtual ~Constant() = default;
366
367 void Write(CodeWriter* to) const override;
368};
369
Adam Lesinskiffa16862014-01-23 18:17:42 -0800370struct Class : public ClassElement
371{
372 enum {
373 CLASS,
374 INTERFACE
375 };
376
377 string comment;
Christopher Wileydf03d972015-09-29 11:10:47 -0700378 int modifiers = 0;
379 int what = CLASS; // CLASS or INTERFACE
380 const Type* type = nullptr;
381 const Type* extends = nullptr;
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700382 vector<const Type*> interfaces;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383 vector<ClassElement*> elements;
384
Christopher Wileydf03d972015-09-29 11:10:47 -0700385 Class() = default;
386 virtual ~Class() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387
Christopher Wileydf03d972015-09-29 11:10:47 -0700388 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389};
390
391struct Document
392{
393 string comment;
394 string package;
395 string originalSrc;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800396 vector<Class*> classes;
397
Christopher Wileydf03d972015-09-29 11:10:47 -0700398 Document() = default;
399 virtual ~Document() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800400
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700401 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800402};
403
Christopher Wileydb154a52015-09-28 16:32:25 -0700404} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700405} // namespace aidl
406} // namespace android
407
Christopher Wiley038485e2015-09-12 11:14:14 -0700408#endif // AIDL_AST_JAVA_H_