blob: ec0ccfda229926429b22a6b0c3cc177f23c75466 [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
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Adam Lesinskiffa16862014-01-23 18:17:42 -080018
Adam Lesinskiffa16862014-01-23 18:17:42 -080019#include <stdarg.h>
20#include <stdio.h>
Jeongik Chaaabc1442019-02-12 17:44:48 +090021#include <memory>
22#include <optional>
Christopher Wileyae589972016-01-29 11:19:23 -080023#include <string>
Jeongik Cha439f2c42019-02-13 12:38:30 +090024#include <variant>
Christopher Wileyae589972016-01-29 11:19:23 -080025#include <vector>
Adam Lesinskiffa16862014-01-23 18:17:42 -080026
Adam Lesinskiffa16862014-01-23 18:17:42 -080027enum {
Christopher Wileyae589972016-01-29 11:19:23 -080028 PACKAGE_PRIVATE = 0x00000000,
29 PUBLIC = 0x00000001,
30 PRIVATE = 0x00000002,
31 PROTECTED = 0x00000003,
32 SCOPE_MASK = 0x00000003,
Adam Lesinskiffa16862014-01-23 18:17:42 -080033
Christopher Wileyae589972016-01-29 11:19:23 -080034 STATIC = 0x00000010,
35 FINAL = 0x00000020,
36 ABSTRACT = 0x00000040,
Adam Lesinskiffa16862014-01-23 18:17:42 -080037
Christopher Wileyae589972016-01-29 11:19:23 -080038 OVERRIDE = 0x00000100,
Adam Lesinskiffa16862014-01-23 18:17:42 -080039
Christopher Wileyae589972016-01-29 11:19:23 -080040 ALL_MODIFIERS = 0xffffffff
Adam Lesinskiffa16862014-01-23 18:17:42 -080041};
42
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070043namespace android {
44namespace aidl {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070045class CodeWriter;
Christopher Wileydb154a52015-09-28 16:32:25 -070046} // namespace aidl
47} // namespace android
48
49namespace android {
50namespace aidl {
51namespace java {
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
Jiyong Park176905e2018-07-04 22:29:41 +090056struct AstNode {
57 AstNode() = default;
58 virtual ~AstNode() = default;
Christopher Wileyae589972016-01-29 11:19:23 -080059 virtual void Write(CodeWriter* to) const = 0;
Jiyong Park176905e2018-07-04 22:29:41 +090060 std::string ToString();
Adam Lesinskiffa16862014-01-23 18:17:42 -080061};
62
Jiyong Park176905e2018-07-04 22:29:41 +090063struct ClassElement : public AstNode {
64 ClassElement() = default;
65 virtual ~ClassElement() = default;
66};
67
68struct Expression : public AstNode {
Christopher Wileyae589972016-01-29 11:19:23 -080069 virtual ~Expression() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080070};
71
Christopher Wileyae589972016-01-29 11:19:23 -080072struct LiteralExpression : public Expression {
Christopher Wiley12e894a2016-01-29 11:55:07 -080073 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -080074
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -070075 explicit LiteralExpression(const std::string& value);
Christopher Wileyae589972016-01-29 11:19:23 -080076 virtual ~LiteralExpression() = default;
77 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080078};
79
80// TODO: also escape the contents. not needed for now
Christopher Wileyae589972016-01-29 11:19:23 -080081struct StringLiteralExpression : public Expression {
Christopher Wiley12e894a2016-01-29 11:55:07 -080082 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -080083
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -070084 explicit StringLiteralExpression(const std::string& value);
Christopher Wileyae589972016-01-29 11:19:23 -080085 virtual ~StringLiteralExpression() = default;
86 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -080087};
88
Christopher Wileyae589972016-01-29 11:19:23 -080089struct Variable : public Expression {
Jeongik Chadc77c1b2019-02-12 16:13:25 +090090 const std::string type;
Christopher Wiley12e894a2016-01-29 11:55:07 -080091 std::string name;
Christopher Wileyae589972016-01-29 11:19:23 -080092 int dimension = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -080093
Christopher Wileyae589972016-01-29 11:19:23 -080094 Variable() = default;
Jeongik Chadc77c1b2019-02-12 16:13:25 +090095 Variable(const std::string& type, const std::string& name);
96 Variable(const std::string& type, const std::string& name, int dimension);
Christopher Wileyae589972016-01-29 11:19:23 -080097 virtual ~Variable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080098
Christopher Wileyae589972016-01-29 11:19:23 -080099 void WriteDeclaration(CodeWriter* to) const;
100 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800101};
102
Christopher Wileyae589972016-01-29 11:19:23 -0800103struct FieldVariable : public Expression {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900104 std::variant<Expression*, std::string> receiver;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800105 std::string name;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106
Christopher Wiley12e894a2016-01-29 11:55:07 -0800107 FieldVariable(Expression* object, const std::string& name);
Jeongik Cha439f2c42019-02-13 12:38:30 +0900108 FieldVariable(const std::string& clazz, const std::string& name);
Christopher Wileyae589972016-01-29 11:19:23 -0800109 virtual ~FieldVariable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110
Christopher Wileyae589972016-01-29 11:19:23 -0800111 void Write(CodeWriter* to) const;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112};
113
Christopher Wileyae589972016-01-29 11:19:23 -0800114struct Field : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800115 std::string comment;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900116 std::vector<std::string> annotations;
Christopher Wileyae589972016-01-29 11:19:23 -0800117 int modifiers = 0;
118 Variable* variable = nullptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800119 std::string value;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800120
Christopher Wileyae589972016-01-29 11:19:23 -0800121 Field() = default;
122 Field(int modifiers, Variable* variable);
123 virtual ~Field() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800124
Christopher Wileyae589972016-01-29 11:19:23 -0800125 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800126};
127
Jiyong Park176905e2018-07-04 22:29:41 +0900128struct Statement : public AstNode {
Christopher Wileyae589972016-01-29 11:19:23 -0800129 virtual ~Statement() = default;
Jiyong Park176905e2018-07-04 22:29:41 +0900130};
131
132struct LiteralStatement : public Statement {
133 public:
134 LiteralStatement(const std::string& value);
135 virtual ~LiteralStatement() = default;
136 void Write(CodeWriter* to) const override;
137
138 private:
139 const std::string value_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800140};
141
Christopher Wileyae589972016-01-29 11:19:23 -0800142struct StatementBlock : public Statement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800143 std::vector<Statement*> statements;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800144
Christopher Wileyae589972016-01-29 11:19:23 -0800145 StatementBlock() = default;
146 virtual ~StatementBlock() = default;
147 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148
Christopher Wileyae589972016-01-29 11:19:23 -0800149 void Add(Statement* statement);
150 void Add(Expression* expression);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151};
152
Christopher Wileyae589972016-01-29 11:19:23 -0800153struct ExpressionStatement : public Statement {
154 Expression* expression;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700156 explicit ExpressionStatement(Expression* expression);
Christopher Wileyae589972016-01-29 11:19:23 -0800157 virtual ~ExpressionStatement() = default;
158 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800159};
160
Christopher Wileyae589972016-01-29 11:19:23 -0800161struct Assignment : public Expression {
162 Variable* lvalue;
163 Expression* rvalue;
Jeongik Cha439f2c42019-02-13 12:38:30 +0900164 std::optional<std::string> cast = std::nullopt;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800165
Christopher Wileyae589972016-01-29 11:19:23 -0800166 Assignment(Variable* lvalue, Expression* rvalue);
Jeongik Cha439f2c42019-02-13 12:38:30 +0900167 Assignment(Variable* lvalue, Expression* rvalue, std::string cast);
Christopher Wileyae589972016-01-29 11:19:23 -0800168 virtual ~Assignment() = default;
169 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800170};
171
Christopher Wileyae589972016-01-29 11:19:23 -0800172struct MethodCall : public Expression {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900173 std::variant<std::monostate, Expression*, std::string> receiver;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800174 std::string name;
175 std::vector<Expression*> arguments;
176 std::vector<std::string> exceptions;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800177
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700178 explicit MethodCall(const std::string& name);
Christopher Wiley12e894a2016-01-29 11:55:07 -0800179 MethodCall(const std::string& name, int argc, ...);
180 MethodCall(Expression* obj, const std::string& name);
Jeongik Cha439f2c42019-02-13 12:38:30 +0900181 MethodCall(const std::string& clazz, const std::string& name);
Christopher Wiley12e894a2016-01-29 11:55:07 -0800182 MethodCall(Expression* obj, const std::string& name, int argc, ...);
Jeongik Cha439f2c42019-02-13 12:38:30 +0900183 MethodCall(const std::string&, const std::string& name, int argc, ...);
Christopher Wileyae589972016-01-29 11:19:23 -0800184 virtual ~MethodCall() = default;
185 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800186
Christopher Wileyae589972016-01-29 11:19:23 -0800187 private:
188 void init(int n, va_list args);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800189};
190
Christopher Wileyae589972016-01-29 11:19:23 -0800191struct Comparison : public Expression {
192 Expression* lvalue;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800193 std::string op;
Christopher Wileyae589972016-01-29 11:19:23 -0800194 Expression* rvalue;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800195
Christopher Wiley12e894a2016-01-29 11:55:07 -0800196 Comparison(Expression* lvalue, const std::string& op, Expression* rvalue);
Christopher Wileyae589972016-01-29 11:19:23 -0800197 virtual ~Comparison() = default;
198 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800199};
200
Christopher Wileyae589972016-01-29 11:19:23 -0800201struct NewExpression : public Expression {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900202 const std::string instantiableName;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800203 std::vector<Expression*> arguments;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800204
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900205 explicit NewExpression(const std::string& name);
206 NewExpression(const std::string& name, int argc, ...);
Christopher Wileyae589972016-01-29 11:19:23 -0800207 virtual ~NewExpression() = default;
208 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800209
Christopher Wileyae589972016-01-29 11:19:23 -0800210 private:
211 void init(int n, va_list args);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800212};
213
Christopher Wileyae589972016-01-29 11:19:23 -0800214struct NewArrayExpression : public Expression {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900215 const std::string type;
Christopher Wileyae589972016-01-29 11:19:23 -0800216 Expression* size;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900218 NewArrayExpression(const std::string& type, Expression* size);
Christopher Wileyae589972016-01-29 11:19:23 -0800219 virtual ~NewArrayExpression() = 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 {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900224 const std::string type;
Christopher Wileyae589972016-01-29 11:19:23 -0800225 Expression* expression = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226
Christopher Wileyae589972016-01-29 11:19:23 -0800227 Cast() = default;
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900228 Cast(const std::string& type, Expression* expression);
Christopher Wileyae589972016-01-29 11:19:23 -0800229 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;
Christopher Wileyae589972016-01-29 11:19:23 -0800235 Expression* rvalue = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700237 explicit VariableDeclaration(Variable* lvalue);
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900238 VariableDeclaration(Variable* lvalue, Expression* rvalue);
Christopher Wileyae589972016-01-29 11:19:23 -0800239 virtual ~VariableDeclaration() = default;
240 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800241};
242
Christopher Wileyae589972016-01-29 11:19:23 -0800243struct IfStatement : public Statement {
244 Expression* expression = nullptr;
245 StatementBlock* statements = new StatementBlock;
246 IfStatement* elseif = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247
Christopher Wileyae589972016-01-29 11:19:23 -0800248 IfStatement() = default;
249 virtual ~IfStatement() = default;
250 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251};
252
Christopher Wileyae589972016-01-29 11:19:23 -0800253struct ReturnStatement : public Statement {
254 Expression* expression;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700256 explicit ReturnStatement(Expression* expression);
Christopher Wileyae589972016-01-29 11:19:23 -0800257 virtual ~ReturnStatement() = default;
258 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259};
260
Christopher Wileyae589972016-01-29 11:19:23 -0800261struct TryStatement : public Statement {
262 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800263
Christopher Wileyae589972016-01-29 11:19:23 -0800264 TryStatement() = default;
265 virtual ~TryStatement() = default;
266 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800267};
268
Christopher Wileyae589972016-01-29 11:19:23 -0800269struct FinallyStatement : public Statement {
270 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800271
Christopher Wileyae589972016-01-29 11:19:23 -0800272 FinallyStatement() = default;
273 virtual ~FinallyStatement() = default;
274 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800275};
276
Jiyong Park176905e2018-07-04 22:29:41 +0900277struct Case : public AstNode {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800278 std::vector<std::string> cases;
Christopher Wileyae589972016-01-29 11:19:23 -0800279 StatementBlock* statements = new StatementBlock;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800280
Christopher Wileyae589972016-01-29 11:19:23 -0800281 Case() = default;
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700282 explicit Case(const std::string& c);
Christopher Wileyae589972016-01-29 11:19:23 -0800283 virtual ~Case() = default;
Jiyong Park176905e2018-07-04 22:29:41 +0900284 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800285};
286
Christopher Wileyae589972016-01-29 11:19:23 -0800287struct SwitchStatement : public Statement {
288 Expression* expression;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800289 std::vector<Case*> cases;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290
Chih-Hung Hsieh156a57f2016-06-02 16:17:28 -0700291 explicit SwitchStatement(Expression* expression);
Christopher Wileyae589972016-01-29 11:19:23 -0800292 virtual ~SwitchStatement() = default;
293 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800294};
295
Christopher Wileyae589972016-01-29 11:19:23 -0800296struct Method : public ClassElement {
Christopher Wiley12e894a2016-01-29 11:55:07 -0800297 std::string comment;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900298 std::vector<std::string> annotations;
Christopher Wileyae589972016-01-29 11:19:23 -0800299 int modifiers = 0;
Jeongik Cha6cadc212019-02-12 18:16:03 +0900300 std::optional<std::string> returnType = std::nullopt; // nullopt means constructor
Christopher Wileyae589972016-01-29 11:19:23 -0800301 size_t returnTypeDimension = 0;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800302 std::string name;
303 std::vector<Variable*> parameters;
Jeongik Cha6cadc212019-02-12 18:16:03 +0900304 std::vector<std::string> exceptions;
Christopher Wileyae589972016-01-29 11:19:23 -0800305 StatementBlock* statements = nullptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306
Christopher Wileyae589972016-01-29 11:19:23 -0800307 Method() = default;
308 virtual ~Method() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800309
Christopher Wileyae589972016-01-29 11:19:23 -0800310 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311};
312
Steven Moreland5557f1c2018-07-02 13:50:23 -0700313struct LiteralClassElement : public ClassElement {
314 std::string element;
315
316 LiteralClassElement(std::string e) : element(e) {}
317 virtual ~LiteralClassElement() = default;
318
319 void Write(CodeWriter* to) const override;
320};
321
Christopher Wileyae589972016-01-29 11:19:23 -0800322struct Class : public ClassElement {
323 enum { CLASS, INTERFACE };
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324
Christopher Wiley12e894a2016-01-29 11:55:07 -0800325 std::string comment;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900326 std::vector<std::string> annotations;
Christopher Wileyae589972016-01-29 11:19:23 -0800327 int modifiers = 0;
328 int what = CLASS; // CLASS or INTERFACE
Jeongik Chaaabc1442019-02-12 17:44:48 +0900329 std::string type;
330 std::optional<std::string> extends = std::nullopt;
331 std::vector<std::string> interfaces;
Christopher Wiley12e894a2016-01-29 11:55:07 -0800332 std::vector<ClassElement*> elements;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800333
Christopher Wileyae589972016-01-29 11:19:23 -0800334 Class() = default;
335 virtual ~Class() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336
Christopher Wileyae589972016-01-29 11:19:23 -0800337 void Write(CodeWriter* to) const override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800338};
339
Jiyong Park176905e2018-07-04 22:29:41 +0900340class Document : public AstNode {
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800341 public:
342 Document(const std::string& comment,
343 const std::string& package,
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800344 std::unique_ptr<Class> clazz);
Christopher Wileyae589972016-01-29 11:19:23 -0800345 virtual ~Document() = default;
Jiyong Park176905e2018-07-04 22:29:41 +0900346 void Write(CodeWriter* to) const override;
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800347
348 private:
349 std::string comment_;
350 std::string package_;
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800351 std::unique_ptr<Class> clazz_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800352};
353
Christopher Wileydb154a52015-09-28 16:32:25 -0700354} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700355} // namespace aidl
356} // namespace android