blob: 3ec48b7239fbcb3615804a06a9997f3f716c59e6 [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
Christopher Wileyf76b59a2016-01-29 11:32:11 -080020#include <memory>
Adam Lesinskiffa16862014-01-23 18:17:42 -080021#include <stdarg.h>
22#include <stdio.h>
Christopher Wileyae589972016-01-29 11:19:23 -080023#include <string>
24#include <vector>
Adam Lesinskiffa16862014-01-23 18:17:42 -080025
Adam Lesinskiffa16862014-01-23 18:17:42 -080026enum {
Christopher Wileyae589972016-01-29 11:19:23 -080027 PACKAGE_PRIVATE = 0x00000000,
28 PUBLIC = 0x00000001,
29 PRIVATE = 0x00000002,
30 PROTECTED = 0x00000003,
31 SCOPE_MASK = 0x00000003,
Adam Lesinskiffa16862014-01-23 18:17:42 -080032
Christopher Wileyae589972016-01-29 11:19:23 -080033 STATIC = 0x00000010,
34 FINAL = 0x00000020,
35 ABSTRACT = 0x00000040,
Adam Lesinskiffa16862014-01-23 18:17:42 -080036
Christopher Wileyae589972016-01-29 11:19:23 -080037 OVERRIDE = 0x00000100,
Adam Lesinskiffa16862014-01-23 18:17:42 -080038
Christopher Wileyae589972016-01-29 11:19:23 -080039 ALL_MODIFIERS = 0xffffffff
Adam Lesinskiffa16862014-01-23 18:17:42 -080040};
41
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070042namespace android {
43namespace aidl {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070044class CodeWriter;
Christopher Wileydb154a52015-09-28 16:32:25 -070045} // namespace aidl
46} // namespace android
47
48namespace android {
49namespace aidl {
50namespace java {
51
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070052class Type;
53
Adam Lesinskiffa16862014-01-23 18:17:42 -080054// Write the modifiers that are set in both mod and mask
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070055void WriteModifiers(CodeWriter* to, int mod, int mask);
Adam Lesinskiffa16862014-01-23 18:17:42 -080056
Christopher Wileyae589972016-01-29 11:19:23 -080057struct ClassElement {
58 ClassElement() = default;
59 virtual ~ClassElement() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080060
Christopher Wileyae589972016-01-29 11:19:23 -080061 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080062};
63
Christopher Wileyae589972016-01-29 11:19:23 -080064struct Expression {
65 virtual ~Expression() = default;
66 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080067};
68
Christopher Wileyae589972016-01-29 11:19:23 -080069struct LiteralExpression : public Expression {
Christopher Wiley12e894a2016-01-29 11:55:07 -080070 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -080071
Christopher Wiley12e894a2016-01-29 11:55:07 -080072 LiteralExpression(const std::string& value);
Christopher Wileyae589972016-01-29 11:19:23 -080073 virtual ~LiteralExpression() = default;
74 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080075};
76
77// TODO: also escape the contents. not needed for now
Christopher Wileyae589972016-01-29 11:19:23 -080078struct StringLiteralExpression : public Expression {
Christopher Wiley12e894a2016-01-29 11:55:07 -080079 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -080080
Christopher Wiley12e894a2016-01-29 11:55:07 -080081 StringLiteralExpression(const std::string& value);
Christopher Wileyae589972016-01-29 11:19:23 -080082 virtual ~StringLiteralExpression() = default;
83 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080084};
85
Christopher Wileyae589972016-01-29 11:19:23 -080086struct Variable : public Expression {
87 const Type* type = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080088 std::string name;
Christopher Wileyae589972016-01-29 11:19:23 -080089 int dimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080090
Christopher Wileyae589972016-01-29 11:19:23 -080091 Variable() = default;
Christopher Wiley12e894a2016-01-29 11:55:07 -080092 Variable(const Type* type, const std::string& name);
93 Variable(const Type* type, const std::string& name, int dimension);
Christopher Wileyae589972016-01-29 11:19:23 -080094 virtual ~Variable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080095
Christopher Wileyae589972016-01-29 11:19:23 -080096 void WriteDeclaration(CodeWriter* to) const;
97 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -080098};
99
Christopher Wileyae589972016-01-29 11:19:23 -0800100struct FieldVariable : public Expression {
101 Expression* object;
102 const Type* clazz;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800103 std::string name;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104
Christopher Wiley12e894a2016-01-29 11:55:07 -0800105 FieldVariable(Expression* object, const std::string& name);
106 FieldVariable(const Type* clazz, const std::string& name);
Christopher Wileyae589972016-01-29 11:19:23 -0800107 virtual ~FieldVariable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800108
Christopher Wileyae589972016-01-29 11:19:23 -0800109 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110};
111
Christopher Wileyae589972016-01-29 11:19:23 -0800112struct Field : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800113 std::string comment;
Christopher Wileyae589972016-01-29 11:19:23 -0800114 int modifiers = 0;
115 Variable* variable = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800116 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800117
Christopher Wileyae589972016-01-29 11:19:23 -0800118 Field() = default;
119 Field(int modifiers, Variable* variable);
120 virtual ~Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121
Christopher Wileyae589972016-01-29 11:19:23 -0800122 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800123};
124
Christopher Wileyae589972016-01-29 11:19:23 -0800125struct Statement {
126 virtual ~Statement() = default;
127 virtual void Write(CodeWriter* to) const = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800128};
129
Christopher Wileyae589972016-01-29 11:19:23 -0800130struct StatementBlock : public Statement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800131 std::vector<Statement*> statements;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132
Christopher Wileyae589972016-01-29 11:19:23 -0800133 StatementBlock() = default;
134 virtual ~StatementBlock() = default;
135 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800136
Christopher Wileyae589972016-01-29 11:19:23 -0800137 void Add(Statement* statement);
138 void Add(Expression* expression);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800139};
140
Christopher Wileyae589972016-01-29 11:19:23 -0800141struct ExpressionStatement : public Statement {
142 Expression* expression;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800143
Christopher Wileyae589972016-01-29 11:19:23 -0800144 ExpressionStatement(Expression* expression);
145 virtual ~ExpressionStatement() = default;
146 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147};
148
Christopher Wileyae589972016-01-29 11:19:23 -0800149struct Assignment : public Expression {
150 Variable* lvalue;
151 Expression* rvalue;
152 const Type* cast;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153
Christopher Wileyae589972016-01-29 11:19:23 -0800154 Assignment(Variable* lvalue, Expression* rvalue);
155 Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
156 virtual ~Assignment() = default;
157 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800158};
159
Christopher Wileyae589972016-01-29 11:19:23 -0800160struct MethodCall : public Expression {
161 Expression* obj = nullptr;
162 const Type* clazz = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800163 std::string name;
164 std::vector<Expression*> arguments;
165 std::vector<std::string> exceptions;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800166
Christopher Wiley12e894a2016-01-29 11:55:07 -0800167 MethodCall(const std::string& name);
168 MethodCall(const std::string& name, int argc, ...);
169 MethodCall(Expression* obj, const std::string& name);
170 MethodCall(const Type* clazz, const std::string& name);
171 MethodCall(Expression* obj, const std::string& name, int argc, ...);
172 MethodCall(const Type* clazz, const std::string& name, int argc, ...);
Christopher Wileyae589972016-01-29 11:19:23 -0800173 virtual ~MethodCall() = default;
174 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800175
Christopher Wileyae589972016-01-29 11:19:23 -0800176 private:
177 void init(int n, va_list args);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800178};
179
Christopher Wileyae589972016-01-29 11:19:23 -0800180struct Comparison : public Expression {
181 Expression* lvalue;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800182 std::string op;
Christopher Wileyae589972016-01-29 11:19:23 -0800183 Expression* rvalue;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800184
Christopher Wiley12e894a2016-01-29 11:55:07 -0800185 Comparison(Expression* lvalue, const std::string& op, Expression* rvalue);
Christopher Wileyae589972016-01-29 11:19:23 -0800186 virtual ~Comparison() = default;
187 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188};
189
Christopher Wileyae589972016-01-29 11:19:23 -0800190struct NewExpression : public Expression {
191 const Type* type;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800192 std::vector<Expression*> arguments;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800193
Christopher Wileyae589972016-01-29 11:19:23 -0800194 NewExpression(const Type* type);
195 NewExpression(const Type* type, int argc, ...);
196 virtual ~NewExpression() = default;
197 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800198
Christopher Wileyae589972016-01-29 11:19:23 -0800199 private:
200 void init(int n, va_list args);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800201};
202
Christopher Wileyae589972016-01-29 11:19:23 -0800203struct NewArrayExpression : public Expression {
204 const Type* type;
205 Expression* size;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800206
Christopher Wileyae589972016-01-29 11:19:23 -0800207 NewArrayExpression(const Type* type, Expression* size);
208 virtual ~NewArrayExpression() = default;
209 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800210};
211
Christopher Wileyae589972016-01-29 11:19:23 -0800212struct Ternary : public Expression {
213 Expression* condition = nullptr;
214 Expression* ifpart = nullptr;
215 Expression* elsepart = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216
Christopher Wileyae589972016-01-29 11:19:23 -0800217 Ternary() = default;
218 Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
219 virtual ~Ternary() = default;
220 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800221};
222
Christopher Wileyae589972016-01-29 11:19:23 -0800223struct Cast : public Expression {
224 const Type* type = nullptr;
225 Expression* expression = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226
Christopher Wileyae589972016-01-29 11:19:23 -0800227 Cast() = default;
228 Cast(const Type* type, Expression* expression);
229 virtual ~Cast() = default;
230 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800231};
232
Christopher Wileyae589972016-01-29 11:19:23 -0800233struct VariableDeclaration : public Statement {
234 Variable* lvalue = nullptr;
235 const Type* cast = nullptr;
236 Expression* rvalue = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237
Christopher Wileyae589972016-01-29 11:19:23 -0800238 VariableDeclaration(Variable* lvalue);
239 VariableDeclaration(Variable* lvalue, Expression* rvalue,
240 const Type* cast = NULL);
241 virtual ~VariableDeclaration() = default;
242 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800243};
244
Christopher Wileyae589972016-01-29 11:19:23 -0800245struct IfStatement : public Statement {
246 Expression* expression = nullptr;
247 StatementBlock* statements = new StatementBlock;
248 IfStatement* elseif = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249
Christopher Wileyae589972016-01-29 11:19:23 -0800250 IfStatement() = default;
251 virtual ~IfStatement() = default;
252 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253};
254
Christopher Wileyae589972016-01-29 11:19:23 -0800255struct ReturnStatement : public Statement {
256 Expression* expression;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800257
Christopher Wileyae589972016-01-29 11:19:23 -0800258 ReturnStatement(Expression* expression);
259 virtual ~ReturnStatement() = default;
260 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800261};
262
Christopher Wileyae589972016-01-29 11:19:23 -0800263struct TryStatement : public Statement {
264 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265
Christopher Wileyae589972016-01-29 11:19:23 -0800266 TryStatement() = default;
267 virtual ~TryStatement() = default;
268 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800269};
270
Christopher Wileyae589972016-01-29 11:19:23 -0800271struct CatchStatement : public Statement {
272 StatementBlock* statements;
273 Variable* exception;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800274
Christopher Wileyae589972016-01-29 11:19:23 -0800275 CatchStatement(Variable* exception);
276 virtual ~CatchStatement() = default;
277 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278};
279
Christopher Wileyae589972016-01-29 11:19:23 -0800280struct FinallyStatement : public Statement {
281 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800282
Christopher Wileyae589972016-01-29 11:19:23 -0800283 FinallyStatement() = default;
284 virtual ~FinallyStatement() = default;
285 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800286};
287
Christopher Wileyae589972016-01-29 11:19:23 -0800288struct Case {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800289 std::vector<std::string> cases;
Christopher Wileyae589972016-01-29 11:19:23 -0800290 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800291
Christopher Wileyae589972016-01-29 11:19:23 -0800292 Case() = default;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800293 Case(const std::string& c);
Christopher Wileyae589972016-01-29 11:19:23 -0800294 virtual ~Case() = default;
295 virtual void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296};
297
Christopher Wileyae589972016-01-29 11:19:23 -0800298struct SwitchStatement : public Statement {
299 Expression* expression;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800300 std::vector<Case*> cases;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800301
Christopher Wileyae589972016-01-29 11:19:23 -0800302 SwitchStatement(Expression* expression);
303 virtual ~SwitchStatement() = default;
304 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305};
306
Christopher Wileyae589972016-01-29 11:19:23 -0800307struct Break : public Statement {
308 Break() = default;
309 virtual ~Break() = default;
310 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311};
312
Christopher Wileyae589972016-01-29 11:19:23 -0800313struct Method : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800314 std::string comment;
Christopher Wileyae589972016-01-29 11:19:23 -0800315 int modifiers = 0;
316 const Type* returnType = nullptr; // nullptr means constructor
317 size_t returnTypeDimension = 0;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800318 std::string name;
319 std::vector<Variable*> parameters;
320 std::vector<const Type*> exceptions;
Christopher Wileyae589972016-01-29 11:19:23 -0800321 StatementBlock* statements = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800322
Christopher Wileyae589972016-01-29 11:19:23 -0800323 Method() = default;
324 virtual ~Method() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800325
Christopher Wileyae589972016-01-29 11:19:23 -0800326 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800327};
328
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700329struct IntConstant : public ClassElement {
330 const std::string name;
331 const int value;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800332
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700333 IntConstant(std::string name, int value)
334 : name(name), value(value) {}
335 virtual ~IntConstant() = default;
336
337 void Write(CodeWriter* to) const override;
338};
339
340struct StringConstant : public ClassElement {
341 const std::string name;
342 const std::string value;
343
344 StringConstant(std::string name, std::string value)
345 : name(name), value(value) {}
346 ~StringConstant() override = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800347
Christopher Wileyae589972016-01-29 11:19:23 -0800348 void Write(CodeWriter* to) const override;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800349};
350
Christopher Wileyae589972016-01-29 11:19:23 -0800351struct Class : public ClassElement {
352 enum { CLASS, INTERFACE };
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353
Christopher Wiley12e894a2016-01-29 11:55:07 -0800354 std::string comment;
Christopher Wileyae589972016-01-29 11:19:23 -0800355 int modifiers = 0;
356 int what = CLASS; // CLASS or INTERFACE
357 const Type* type = nullptr;
358 const Type* extends = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800359 std::vector<const Type*> interfaces;
360 std::vector<ClassElement*> elements;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800361
Christopher Wileyae589972016-01-29 11:19:23 -0800362 Class() = default;
363 virtual ~Class() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800364
Christopher Wileyae589972016-01-29 11:19:23 -0800365 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800366};
367
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800368class Document {
369 public:
370 Document(const std::string& comment,
371 const std::string& package,
372 const std::string& original_src,
373 std::unique_ptr<Class> clazz);
Christopher Wileyae589972016-01-29 11:19:23 -0800374 virtual ~Document() = default;
Christopher Wileyae589972016-01-29 11:19:23 -0800375 virtual void Write(CodeWriter* to) const;
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800376
377 private:
378 std::string comment_;
379 std::string package_;
380 std::string original_src_;
381 std::unique_ptr<Class> clazz_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382};
383
Christopher Wileydb154a52015-09-28 16:32:25 -0700384} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700385} // namespace aidl
386} // namespace android
387
Christopher Wileyae589972016-01-29 11:19:23 -0800388#endif // AIDL_AST_JAVA_H_