blob: 4b217d3f1418ac5b68f11943fc873b37b2d8954c [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
Adam Lesinskiffa16862014-01-23 18:17:42 -080020#include <stdarg.h>
21#include <stdio.h>
Christopher Wileyae589972016-01-29 11:19:23 -080022#include <string>
23#include <vector>
Adam Lesinskiffa16862014-01-23 18:17:42 -080024
Adam Lesinskiffa16862014-01-23 18:17:42 -080025enum {
Christopher Wileyae589972016-01-29 11:19:23 -080026 PACKAGE_PRIVATE = 0x00000000,
27 PUBLIC = 0x00000001,
28 PRIVATE = 0x00000002,
29 PROTECTED = 0x00000003,
30 SCOPE_MASK = 0x00000003,
Adam Lesinskiffa16862014-01-23 18:17:42 -080031
Christopher Wileyae589972016-01-29 11:19:23 -080032 STATIC = 0x00000010,
33 FINAL = 0x00000020,
34 ABSTRACT = 0x00000040,
Adam Lesinskiffa16862014-01-23 18:17:42 -080035
Christopher Wileyae589972016-01-29 11:19:23 -080036 OVERRIDE = 0x00000100,
Adam Lesinskiffa16862014-01-23 18:17:42 -080037
Christopher Wileyae589972016-01-29 11:19:23 -080038 ALL_MODIFIERS = 0xffffffff
Adam Lesinskiffa16862014-01-23 18:17:42 -080039};
40
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070041namespace android {
42namespace aidl {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070043class CodeWriter;
Christopher Wileydb154a52015-09-28 16:32:25 -070044} // namespace aidl
45} // namespace android
46
47namespace android {
48namespace aidl {
49namespace java {
50
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070051class Type;
52
Adam Lesinskiffa16862014-01-23 18:17:42 -080053// Write the modifiers that are set in both mod and mask
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070054void WriteModifiers(CodeWriter* to, int mod, int mask);
Adam Lesinskiffa16862014-01-23 18:17:42 -080055
Christopher Wileyae589972016-01-29 11:19:23 -080056struct ClassElement {
57 ClassElement() = default;
58 virtual ~ClassElement() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Christopher Wileyae589972016-01-29 11:19:23 -080060 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080061};
62
Christopher Wileyae589972016-01-29 11:19:23 -080063struct Expression {
64 virtual ~Expression() = default;
65 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080066};
67
Christopher Wileyae589972016-01-29 11:19:23 -080068struct LiteralExpression : public Expression {
Christopher Wiley12e894a2016-01-29 11:55:07 -080069 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -080070
Christopher Wiley12e894a2016-01-29 11:55:07 -080071 LiteralExpression(const std::string& value);
Christopher Wileyae589972016-01-29 11:19:23 -080072 virtual ~LiteralExpression() = default;
73 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080074};
75
76// TODO: also escape the contents. not needed for now
Christopher Wileyae589972016-01-29 11:19:23 -080077struct StringLiteralExpression : public Expression {
Christopher Wiley12e894a2016-01-29 11:55:07 -080078 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -080079
Christopher Wiley12e894a2016-01-29 11:55:07 -080080 StringLiteralExpression(const std::string& value);
Christopher Wileyae589972016-01-29 11:19:23 -080081 virtual ~StringLiteralExpression() = default;
82 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080083};
84
Christopher Wileyae589972016-01-29 11:19:23 -080085struct Variable : public Expression {
86 const Type* type = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080087 std::string name;
Christopher Wileyae589972016-01-29 11:19:23 -080088 int dimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080089
Christopher Wileyae589972016-01-29 11:19:23 -080090 Variable() = default;
Christopher Wiley12e894a2016-01-29 11:55:07 -080091 Variable(const Type* type, const std::string& name);
92 Variable(const Type* type, const std::string& name, int dimension);
Christopher Wileyae589972016-01-29 11:19:23 -080093 virtual ~Variable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080094
Christopher Wileyae589972016-01-29 11:19:23 -080095 void WriteDeclaration(CodeWriter* to) const;
96 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -080097};
98
Christopher Wileyae589972016-01-29 11:19:23 -080099struct FieldVariable : public Expression {
100 Expression* object;
101 const Type* clazz;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800102 std::string name;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103
Christopher Wiley12e894a2016-01-29 11:55:07 -0800104 FieldVariable(Expression* object, const std::string& name);
105 FieldVariable(const Type* clazz, const std::string& name);
Christopher Wileyae589972016-01-29 11:19:23 -0800106 virtual ~FieldVariable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800107
Christopher Wileyae589972016-01-29 11:19:23 -0800108 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109};
110
Christopher Wileyae589972016-01-29 11:19:23 -0800111struct Field : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800112 std::string comment;
Christopher Wileyae589972016-01-29 11:19:23 -0800113 int modifiers = 0;
114 Variable* variable = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800115 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800116
Christopher Wileyae589972016-01-29 11:19:23 -0800117 Field() = default;
118 Field(int modifiers, Variable* variable);
119 virtual ~Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800120
Christopher Wileyae589972016-01-29 11:19:23 -0800121 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122};
123
Christopher Wileyae589972016-01-29 11:19:23 -0800124struct Statement {
125 virtual ~Statement() = default;
126 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800127};
128
Christopher Wileyae589972016-01-29 11:19:23 -0800129struct StatementBlock : public Statement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800130 std::vector<Statement*> statements;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131
Christopher Wileyae589972016-01-29 11:19:23 -0800132 StatementBlock() = default;
133 virtual ~StatementBlock() = default;
134 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800135
Christopher Wileyae589972016-01-29 11:19:23 -0800136 void Add(Statement* statement);
137 void Add(Expression* expression);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800138};
139
Christopher Wileyae589972016-01-29 11:19:23 -0800140struct ExpressionStatement : public Statement {
141 Expression* expression;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800142
Christopher Wileyae589972016-01-29 11:19:23 -0800143 ExpressionStatement(Expression* expression);
144 virtual ~ExpressionStatement() = default;
145 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800146};
147
Christopher Wileyae589972016-01-29 11:19:23 -0800148struct Assignment : public Expression {
149 Variable* lvalue;
150 Expression* rvalue;
151 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152
Christopher Wileyae589972016-01-29 11:19:23 -0800153 Assignment(Variable* lvalue, Expression* rvalue);
154 Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
155 virtual ~Assignment() = default;
156 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800157};
158
Christopher Wileyae589972016-01-29 11:19:23 -0800159struct MethodCall : public Expression {
160 Expression* obj = nullptr;
161 const Type* clazz = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800162 std::string name;
163 std::vector<Expression*> arguments;
164 std::vector<std::string> exceptions;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800165
Christopher Wiley12e894a2016-01-29 11:55:07 -0800166 MethodCall(const std::string& name);
167 MethodCall(const std::string& name, int argc, ...);
168 MethodCall(Expression* obj, const std::string& name);
169 MethodCall(const Type* clazz, const std::string& name);
170 MethodCall(Expression* obj, const std::string& name, int argc, ...);
171 MethodCall(const Type* clazz, const std::string& name, int argc, ...);
Christopher Wileyae589972016-01-29 11:19:23 -0800172 virtual ~MethodCall() = default;
173 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800174
Christopher Wileyae589972016-01-29 11:19:23 -0800175 private:
176 void init(int n, va_list args);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800177};
178
Christopher Wileyae589972016-01-29 11:19:23 -0800179struct Comparison : public Expression {
180 Expression* lvalue;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800181 std::string op;
Christopher Wileyae589972016-01-29 11:19:23 -0800182 Expression* rvalue;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183
Christopher Wiley12e894a2016-01-29 11:55:07 -0800184 Comparison(Expression* lvalue, const std::string& op, Expression* rvalue);
Christopher Wileyae589972016-01-29 11:19:23 -0800185 virtual ~Comparison() = default;
186 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800187};
188
Christopher Wileyae589972016-01-29 11:19:23 -0800189struct NewExpression : public Expression {
190 const Type* type;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800191 std::vector<Expression*> arguments;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800192
Christopher Wileyae589972016-01-29 11:19:23 -0800193 NewExpression(const Type* type);
194 NewExpression(const Type* type, int argc, ...);
195 virtual ~NewExpression() = default;
196 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800197
Christopher Wileyae589972016-01-29 11:19:23 -0800198 private:
199 void init(int n, va_list args);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800200};
201
Christopher Wileyae589972016-01-29 11:19:23 -0800202struct NewArrayExpression : public Expression {
203 const Type* type;
204 Expression* size;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800205
Christopher Wileyae589972016-01-29 11:19:23 -0800206 NewArrayExpression(const Type* type, Expression* size);
207 virtual ~NewArrayExpression() = default;
208 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800209};
210
Christopher Wileyae589972016-01-29 11:19:23 -0800211struct Ternary : public Expression {
212 Expression* condition = nullptr;
213 Expression* ifpart = nullptr;
214 Expression* elsepart = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800215
Christopher Wileyae589972016-01-29 11:19:23 -0800216 Ternary() = default;
217 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
218 virtual ~Ternary() = default;
219 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800220};
221
Christopher Wileyae589972016-01-29 11:19:23 -0800222struct Cast : public Expression {
223 const Type* type = nullptr;
224 Expression* expression = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225
Christopher Wileyae589972016-01-29 11:19:23 -0800226 Cast() = default;
227 Cast(const Type* type, Expression* expression);
228 virtual ~Cast() = default;
229 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800230};
231
Christopher Wileyae589972016-01-29 11:19:23 -0800232struct VariableDeclaration : public Statement {
233 Variable* lvalue = nullptr;
234 const Type* cast = nullptr;
235 Expression* rvalue = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236
Christopher Wileyae589972016-01-29 11:19:23 -0800237 VariableDeclaration(Variable* lvalue);
238 VariableDeclaration(Variable* lvalue, Expression* rvalue,
239 const Type* cast = NULL);
240 virtual ~VariableDeclaration() = default;
241 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800242};
243
Christopher Wileyae589972016-01-29 11:19:23 -0800244struct IfStatement : public Statement {
245 Expression* expression = nullptr;
246 StatementBlock* statements = new StatementBlock;
247 IfStatement* elseif = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800248
Christopher Wileyae589972016-01-29 11:19:23 -0800249 IfStatement() = default;
250 virtual ~IfStatement() = default;
251 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252};
253
Christopher Wileyae589972016-01-29 11:19:23 -0800254struct ReturnStatement : public Statement {
255 Expression* expression;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800256
Christopher Wileyae589972016-01-29 11:19:23 -0800257 ReturnStatement(Expression* expression);
258 virtual ~ReturnStatement() = default;
259 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800260};
261
Christopher Wileyae589972016-01-29 11:19:23 -0800262struct TryStatement : public Statement {
263 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800264
Christopher Wileyae589972016-01-29 11:19:23 -0800265 TryStatement() = default;
266 virtual ~TryStatement() = default;
267 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800268};
269
Christopher Wileyae589972016-01-29 11:19:23 -0800270struct CatchStatement : public Statement {
271 StatementBlock* statements;
272 Variable* exception;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800273
Christopher Wileyae589972016-01-29 11:19:23 -0800274 CatchStatement(Variable* exception);
275 virtual ~CatchStatement() = default;
276 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800277};
278
Christopher Wileyae589972016-01-29 11:19:23 -0800279struct FinallyStatement : public Statement {
280 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281
Christopher Wileyae589972016-01-29 11:19:23 -0800282 FinallyStatement() = default;
283 virtual ~FinallyStatement() = default;
284 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800285};
286
Christopher Wileyae589972016-01-29 11:19:23 -0800287struct Case {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800288 std::vector<std::string> cases;
Christopher Wileyae589972016-01-29 11:19:23 -0800289 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290
Christopher Wileyae589972016-01-29 11:19:23 -0800291 Case() = default;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800292 Case(const std::string& c);
Christopher Wileyae589972016-01-29 11:19:23 -0800293 virtual ~Case() = default;
294 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800295};
296
Christopher Wileyae589972016-01-29 11:19:23 -0800297struct SwitchStatement : public Statement {
298 Expression* expression;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800299 std::vector<Case*> cases;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300
Christopher Wileyae589972016-01-29 11:19:23 -0800301 SwitchStatement(Expression* expression);
302 virtual ~SwitchStatement() = default;
303 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304};
305
Christopher Wileyae589972016-01-29 11:19:23 -0800306struct Break : public Statement {
307 Break() = default;
308 virtual ~Break() = default;
309 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800310};
311
Christopher Wileyae589972016-01-29 11:19:23 -0800312struct Method : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800313 std::string comment;
Christopher Wileyae589972016-01-29 11:19:23 -0800314 int modifiers = 0;
315 const Type* returnType = nullptr; // nullptr means constructor
316 size_t returnTypeDimension = 0;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800317 std::string name;
318 std::vector<Variable*> parameters;
319 std::vector<const Type*> exceptions;
Christopher Wileyae589972016-01-29 11:19:23 -0800320 StatementBlock* statements = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800321
Christopher Wileyae589972016-01-29 11:19:23 -0800322 Method() = default;
323 virtual ~Method() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324
Christopher Wileyae589972016-01-29 11:19:23 -0800325 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800326};
327
Christopher Wileyae589972016-01-29 11:19:23 -0800328struct Constant : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800329 std::string name;
Christopher Wileyae589972016-01-29 11:19:23 -0800330 int value;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800331
Christopher Wileyae589972016-01-29 11:19:23 -0800332 Constant() = default;
333 virtual ~Constant() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800334
Christopher Wileyae589972016-01-29 11:19:23 -0800335 void Write(CodeWriter* to) const override;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800336};
337
Christopher Wileyae589972016-01-29 11:19:23 -0800338struct Class : public ClassElement {
339 enum { CLASS, INTERFACE };
Adam Lesinskiffa16862014-01-23 18:17:42 -0800340
Christopher Wiley12e894a2016-01-29 11:55:07 -0800341 std::string comment;
Christopher Wileyae589972016-01-29 11:19:23 -0800342 int modifiers = 0;
343 int what = CLASS; // CLASS or INTERFACE
344 const Type* type = nullptr;
345 const Type* extends = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800346 std::vector<const Type*> interfaces;
347 std::vector<ClassElement*> elements;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348
Christopher Wileyae589972016-01-29 11:19:23 -0800349 Class() = default;
350 virtual ~Class() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800351
Christopher Wileyae589972016-01-29 11:19:23 -0800352 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353};
354
Christopher Wileyae589972016-01-29 11:19:23 -0800355struct Document {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800356 std::string comment;
357 std::string package;
358 std::string originalSrc;
359 std::vector<Class*> classes;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800360
Christopher Wileyae589972016-01-29 11:19:23 -0800361 Document() = default;
362 virtual ~Document() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800363
Christopher Wileyae589972016-01-29 11:19:23 -0800364 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800365};
366
Christopher Wileydb154a52015-09-28 16:32:25 -0700367} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700368} // namespace aidl
369} // namespace android
370
Christopher Wileyae589972016-01-29 11:19:23 -0800371#endif // AIDL_AST_JAVA_H_