blob: 066624ede7c9fe289781d2b4adebb4da927b410c [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#include "ast_java.h"
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070018#include "code_writer.h"
19
Christopher Wiley12e894a2016-01-29 11:55:07 -080020using std::vector;
21using std::string;
22
Jeongik Cha439f2c42019-02-13 12:38:30 +090023template <class... Ts>
24struct overloaded : Ts... {
25 using Ts::operator()...;
26};
27template <class... Ts>
28overloaded(Ts...)->overloaded<Ts...>;
29
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070030namespace android {
31namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070032namespace java {
Adam Lesinskiffa16862014-01-23 18:17:42 -080033
Jiyong Park176905e2018-07-04 22:29:41 +090034std::string AstNode::ToString() {
35 std::string str;
36 Write(CodeWriter::ForString(&str).get());
37 return str;
38}
39
Jooyung Hanbd9db442021-01-14 01:45:55 +090040void WriteComment(CodeWriter* to, const std::string& comment) {
41 to->Write("%s", comment.c_str());
42 if (!comment.empty() && comment.back() != '\n') to->Write("\n");
43}
44
Christopher Wileyae589972016-01-29 11:19:23 -080045void WriteModifiers(CodeWriter* to, int mod, int mask) {
46 int m = mod & mask;
Adam Lesinskiffa16862014-01-23 18:17:42 -080047
Christopher Wileyae589972016-01-29 11:19:23 -080048 if (m & OVERRIDE) {
49 to->Write("@Override ");
50 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080051
Christopher Wileyae589972016-01-29 11:19:23 -080052 if ((m & SCOPE_MASK) == PUBLIC) {
53 to->Write("public ");
54 } else if ((m & SCOPE_MASK) == PRIVATE) {
55 to->Write("private ");
56 } else if ((m & SCOPE_MASK) == PROTECTED) {
57 to->Write("protected ");
58 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Christopher Wileyae589972016-01-29 11:19:23 -080060 if (m & STATIC) {
61 to->Write("static ");
62 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080063
Christopher Wileyae589972016-01-29 11:19:23 -080064 if (m & FINAL) {
65 to->Write("final ");
66 }
67
68 if (m & ABSTRACT) {
69 to->Write("abstract ");
70 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080071}
72
Steven Moreland48548e02019-09-18 15:10:22 -070073void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<Expression>>& arguments) {
Christopher Wileyae589972016-01-29 11:19:23 -080074 size_t N = arguments.size();
75 for (size_t i = 0; i < N; i++) {
76 arguments[i]->Write(to);
77 if (i != N - 1) {
78 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080079 }
Christopher Wileyae589972016-01-29 11:19:23 -080080 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080081}
82
Steven Moreland48548e02019-09-18 15:10:22 -070083Field::Field(int m, std::shared_ptr<Variable> v) : ClassElement(), modifiers(m), variable(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080084
Christopher Wileyae589972016-01-29 11:19:23 -080085void Field::Write(CodeWriter* to) const {
Jooyung Hanbd9db442021-01-14 01:45:55 +090086 WriteComment(to, comment);
Jiyong Parka6605ab2018-11-11 14:30:21 +090087 for (const auto& a : this->annotations) {
88 to->Write("%s\n", a.c_str());
89 }
Christopher Wileyae589972016-01-29 11:19:23 -080090 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Steven Moreland5635a8a2018-07-03 11:28:20 -070091 this->variable->WriteDeclaration(to);
92
Christopher Wileyae589972016-01-29 11:19:23 -080093 if (this->value.length() != 0) {
94 to->Write(" = %s", this->value.c_str());
95 }
96 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080097}
98
Christopher Wileyae589972016-01-29 11:19:23 -080099LiteralExpression::LiteralExpression(const string& v) : value(v) {}
100
101void LiteralExpression::Write(CodeWriter* to) const {
102 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103}
104
Christopher Wileyae589972016-01-29 11:19:23 -0800105StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106
Christopher Wileyae589972016-01-29 11:19:23 -0800107void StringLiteralExpression::Write(CodeWriter* to) const {
108 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109}
110
Steven Moreland3dc29d82019-08-21 17:23:11 -0700111Variable::Variable(const string& t, const string& n) : type(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112
Christopher Wileyae589972016-01-29 11:19:23 -0800113void Variable::WriteDeclaration(CodeWriter* to) const {
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900114 for (const auto& a : this->annotations) {
115 to->Write("%s ", a.c_str());
116 }
Steven Moreland3dc29d82019-08-21 17:23:11 -0700117 to->Write("%s %s", this->type.c_str(), this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118}
119
Christopher Wileyae589972016-01-29 11:19:23 -0800120void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121
Steven Moreland48548e02019-09-18 15:10:22 -0700122FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n)
123 : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800124
Jeongik Cha439f2c42019-02-13 12:38:30 +0900125FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800126
127void FieldVariable::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900128 visit(
Steven Moreland48548e02019-09-18 15:10:22 -0700129 overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); },
Jeongik Cha439f2c42019-02-13 12:38:30 +0900130 [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
131 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800132 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800133}
134
Jiyong Park176905e2018-07-04 22:29:41 +0900135LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
136
137void LiteralStatement::Write(CodeWriter* to) const {
138 to->Write("%s", value_.c_str());
139}
140
Christopher Wileyae589972016-01-29 11:19:23 -0800141void StatementBlock::Write(CodeWriter* to) const {
142 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900143 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800144 int N = this->statements.size();
145 for (int i = 0; i < N; i++) {
146 this->statements[i]->Write(to);
147 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900148 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800149 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800150}
151
Steven Moreland48548e02019-09-18 15:10:22 -0700152void StatementBlock::Add(std::shared_ptr<Statement> statement) {
Christopher Wileyae589972016-01-29 11:19:23 -0800153 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800154}
155
Steven Moreland48548e02019-09-18 15:10:22 -0700156void StatementBlock::Add(std::shared_ptr<Expression> expression) {
157 this->statements.push_back(std::make_shared<ExpressionStatement>(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800158}
159
Steven Moreland48548e02019-09-18 15:10:22 -0700160ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800161
Christopher Wileyae589972016-01-29 11:19:23 -0800162void ExpressionStatement::Write(CodeWriter* to) const {
163 this->expression->Write(to);
164 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800165}
166
Steven Moreland48548e02019-09-18 15:10:22 -0700167Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
168 : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800169
Steven Moreland48548e02019-09-18 15:10:22 -0700170Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c)
171 : lvalue(l), rvalue(r), cast(c) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800172
173void Assignment::Write(CodeWriter* to) const {
174 this->lvalue->Write(to);
175 to->Write(" = ");
Jeongik Cha439f2c42019-02-13 12:38:30 +0900176 if (this->cast) {
177 to->Write("(%s)", this->cast->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800178 }
179 this->rvalue->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800180}
181
Christopher Wileyae589972016-01-29 11:19:23 -0800182MethodCall::MethodCall(const string& n) : name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183
Steven Moreland48548e02019-09-18 15:10:22 -0700184MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args)
185 : name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800186
Steven Moreland48548e02019-09-18 15:10:22 -0700187MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188
Jeongik Cha439f2c42019-02-13 12:38:30 +0900189MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800190
Steven Moreland48548e02019-09-18 15:10:22 -0700191MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n,
192 const std::vector<std::shared_ptr<Expression>>& args)
193 : receiver(o), name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800194
Steven Moreland48548e02019-09-18 15:10:22 -0700195MethodCall::MethodCall(const std::string& t, const string& n,
196 const std::vector<std::shared_ptr<Expression>>& args)
197 : receiver(t), name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800198
Christopher Wileyae589972016-01-29 11:19:23 -0800199void MethodCall::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900200 visit(
Steven Moreland48548e02019-09-18 15:10:22 -0700201 overloaded{[&](std::shared_ptr<Expression> e) {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900202 e->Write(to);
203 to->Write(".");
204 },
205 [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
206 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800207 to->Write("%s(", this->name.c_str());
208 WriteArgumentList(to, this->arguments);
209 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800210}
211
Steven Moreland48548e02019-09-18 15:10:22 -0700212Comparison::Comparison(std::shared_ptr<Expression> l, const string& o,
213 std::shared_ptr<Expression> r)
Christopher Wileyae589972016-01-29 11:19:23 -0800214 : lvalue(l), op(o), rvalue(r) {}
215
216void Comparison::Write(CodeWriter* to) const {
217 to->Write("(");
218 this->lvalue->Write(to);
219 to->Write("%s", this->op.c_str());
220 this->rvalue->Write(to);
221 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222}
223
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900224NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225
Steven Moreland48548e02019-09-18 15:10:22 -0700226NewExpression::NewExpression(const std::string& n,
227 const std::vector<std::shared_ptr<Expression>>& args)
228 : instantiableName(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800229
Christopher Wileyae589972016-01-29 11:19:23 -0800230void NewExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900231 to->Write("new %s(", this->instantiableName.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800232 WriteArgumentList(to, this->arguments);
233 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800234}
235
Steven Moreland48548e02019-09-18 15:10:22 -0700236NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s)
237 : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800238
Christopher Wileyae589972016-01-29 11:19:23 -0800239void NewArrayExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900240 to->Write("new %s[", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800241 size->Write(to);
242 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800243}
244
Steven Moreland48548e02019-09-18 15:10:22 -0700245Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800246
247void Cast::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900248 to->Write("((%s)", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800249 expression->Write(to);
250 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251}
252
Steven Moreland48548e02019-09-18 15:10:22 -0700253VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
254 : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255
Steven Moreland48548e02019-09-18 15:10:22 -0700256VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800257
Christopher Wileyae589972016-01-29 11:19:23 -0800258void VariableDeclaration::Write(CodeWriter* to) const {
259 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700260 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800261 to->Write(" = ");
Christopher Wileyae589972016-01-29 11:19:23 -0800262 this->rvalue->Write(to);
263 }
264 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265}
266
Christopher Wileyae589972016-01-29 11:19:23 -0800267void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700268 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800269 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800270 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800271 to->Write(") ");
272 }
273 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700274 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800275 to->Write("else ");
276 this->elseif->Write(to);
277 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278}
279
Steven Moreland48548e02019-09-18 15:10:22 -0700280ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800281
282void ReturnStatement::Write(CodeWriter* to) const {
283 to->Write("return ");
284 this->expression->Write(to);
285 to->Write(";\n");
286}
287
288void TryStatement::Write(CodeWriter* to) const {
289 to->Write("try ");
290 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800291}
292
Christopher Wileyae589972016-01-29 11:19:23 -0800293void FinallyStatement::Write(CodeWriter* to) const {
294 to->Write("finally ");
295 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296}
297
Christopher Wileyae589972016-01-29 11:19:23 -0800298Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299
Christopher Wileyae589972016-01-29 11:19:23 -0800300void Case::Write(CodeWriter* to) const {
301 int N = this->cases.size();
302 if (N > 0) {
303 for (int i = 0; i < N; i++) {
304 string s = this->cases[i];
305 if (s.length() != 0) {
306 to->Write("case %s:\n", s.c_str());
307 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700308 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800309 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800310 }
Christopher Wileyae589972016-01-29 11:19:23 -0800311 } else {
312 to->Write("default:\n");
313 }
314 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800315}
316
Steven Moreland48548e02019-09-18 15:10:22 -0700317SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800318
319void SwitchStatement::Write(CodeWriter* to) const {
320 to->Write("switch (");
321 this->expression->Write(to);
322 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900323 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800324 int N = this->cases.size();
325 for (int i = 0; i < N; i++) {
326 this->cases[i]->Write(to);
327 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900328 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800329 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800330}
331
Christopher Wileyae589972016-01-29 11:19:23 -0800332void Method::Write(CodeWriter* to) const {
333 size_t N, i;
334
Jooyung Hanbd9db442021-01-14 01:45:55 +0900335 WriteComment(to, comment);
Christopher Wileyae589972016-01-29 11:19:23 -0800336
Jiyong Parka6605ab2018-11-11 14:30:21 +0900337 for (const auto& a : this->annotations) {
338 to->Write("%s\n", a.c_str());
339 }
340
Christopher Wileyae589972016-01-29 11:19:23 -0800341 WriteModifiers(to, this->modifiers,
342 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
343
Jeongik Cha6cadc212019-02-12 18:16:03 +0900344 if (this->returnType) {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700345 to->Write("%s ", this->returnType->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800346 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800347
Christopher Wileyae589972016-01-29 11:19:23 -0800348 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800349
Christopher Wileyae589972016-01-29 11:19:23 -0800350 N = this->parameters.size();
351 for (i = 0; i < N; i++) {
352 this->parameters[i]->WriteDeclaration(to);
353 if (i != N - 1) {
354 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355 }
Christopher Wileyae589972016-01-29 11:19:23 -0800356 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357
Christopher Wileyae589972016-01-29 11:19:23 -0800358 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800359
Christopher Wileyae589972016-01-29 11:19:23 -0800360 N = this->exceptions.size();
361 for (i = 0; i < N; i++) {
362 if (i == 0) {
363 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800364 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800365 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800366 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900367 to->Write("%s", this->exceptions[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800368 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800369
Yi Kong894d6ba2018-07-24 11:27:38 -0700370 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800371 to->Write(";\n");
372 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700373 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800374 this->statements->Write(to);
375 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800376}
377
Steven Moreland5557f1c2018-07-02 13:50:23 -0700378void LiteralClassElement::Write(CodeWriter* to) const {
379 to->Write("%s", element.c_str());
380}
381
Christopher Wileyae589972016-01-29 11:19:23 -0800382void Class::Write(CodeWriter* to) const {
383 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800384
Jooyung Hanbd9db442021-01-14 01:45:55 +0900385 WriteComment(to, comment);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900386 for (const auto& a : this->annotations) {
387 to->Write("%s\n", a.c_str());
388 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389
Christopher Wileyae589972016-01-29 11:19:23 -0800390 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
391
392 if (this->what == Class::CLASS) {
393 to->Write("class ");
394 } else {
395 to->Write("interface ");
396 }
397
Jeongik Chaaabc1442019-02-12 17:44:48 +0900398 string name = this->type;
Christopher Wileyae589972016-01-29 11:19:23 -0800399 size_t pos = name.rfind('.');
400 if (pos != string::npos) {
401 name = name.c_str() + pos + 1;
402 }
403
404 to->Write("%s", name.c_str());
405
Jeongik Chaaabc1442019-02-12 17:44:48 +0900406 if (this->extends) {
407 to->Write(" extends %s", this->extends->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800408 }
409
410 N = this->interfaces.size();
411 if (N != 0) {
412 if (this->what == Class::CLASS) {
413 to->Write(" implements");
414 } else {
415 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800416 }
Christopher Wileyae589972016-01-29 11:19:23 -0800417 for (i = 0; i < N; i++) {
Jeongik Chaaabc1442019-02-12 17:44:48 +0900418 to->Write(" %s", this->interfaces[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800419 }
420 }
421
422 to->Write("\n");
423 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900424 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800425
426 N = this->elements.size();
427 for (i = 0; i < N; i++) {
428 this->elements[i]->Write(to);
429 }
430
Jiyong Parka755dc72018-06-29 13:52:24 +0900431 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800432 to->Write("}\n");
433}
434
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800435Document::Document(const std::string& comment,
436 const std::string& package,
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800437 std::unique_ptr<Class> clazz)
438 : comment_(comment),
439 package_(package),
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800440 clazz_(std::move(clazz)) {
441}
Christopher Wileyae589972016-01-29 11:19:23 -0800442
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800443void Document::Write(CodeWriter* to) const {
444 if (!comment_.empty()) {
445 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800446 }
447 to->Write(
448 "/*\n"
449 " * This file is auto-generated. DO NOT MODIFY.\n"
Jiyong Park54fa1e02019-02-08 10:03:20 +0900450 " */\n");
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800451 if (!package_.empty()) {
452 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800453 }
454
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800455 if (clazz_) {
456 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800457 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800458}
459
Steven Moreland48548e02019-09-18 15:10:22 -0700460std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
461std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
462std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
463std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
464std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
Christopher Wileydb154a52015-09-28 16:32:25 -0700465} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700466} // namespace aidl
467} // namespace android