blob: 8604bc2d30f081be891fb6cf35b197252f4dff5f [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
Christopher Wileyae589972016-01-29 11:19:23 -080040void WriteModifiers(CodeWriter* to, int mod, int mask) {
41 int m = mod & mask;
Adam Lesinskiffa16862014-01-23 18:17:42 -080042
Christopher Wileyae589972016-01-29 11:19:23 -080043 if (m & OVERRIDE) {
44 to->Write("@Override ");
45 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080046
Christopher Wileyae589972016-01-29 11:19:23 -080047 if ((m & SCOPE_MASK) == PUBLIC) {
48 to->Write("public ");
49 } else if ((m & SCOPE_MASK) == PRIVATE) {
50 to->Write("private ");
51 } else if ((m & SCOPE_MASK) == PROTECTED) {
52 to->Write("protected ");
53 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080054
Christopher Wileyae589972016-01-29 11:19:23 -080055 if (m & STATIC) {
56 to->Write("static ");
57 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080058
Christopher Wileyae589972016-01-29 11:19:23 -080059 if (m & FINAL) {
60 to->Write("final ");
61 }
62
63 if (m & ABSTRACT) {
64 to->Write("abstract ");
65 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080066}
67
Steven Moreland48548e02019-09-18 15:10:22 -070068void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<Expression>>& arguments) {
Christopher Wileyae589972016-01-29 11:19:23 -080069 size_t N = arguments.size();
70 for (size_t i = 0; i < N; i++) {
71 arguments[i]->Write(to);
72 if (i != N - 1) {
73 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080074 }
Christopher Wileyae589972016-01-29 11:19:23 -080075 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080076}
77
Steven Moreland48548e02019-09-18 15:10:22 -070078Field::Field(int m, std::shared_ptr<Variable> v) : ClassElement(), modifiers(m), variable(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080079
Christopher Wileyae589972016-01-29 11:19:23 -080080void Field::Write(CodeWriter* to) const {
81 if (this->comment.length() != 0) {
82 to->Write("%s\n", this->comment.c_str());
83 }
Jiyong Parka6605ab2018-11-11 14:30:21 +090084 for (const auto& a : this->annotations) {
85 to->Write("%s\n", a.c_str());
86 }
Christopher Wileyae589972016-01-29 11:19:23 -080087 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Steven Moreland5635a8a2018-07-03 11:28:20 -070088 this->variable->WriteDeclaration(to);
89
Christopher Wileyae589972016-01-29 11:19:23 -080090 if (this->value.length() != 0) {
91 to->Write(" = %s", this->value.c_str());
92 }
93 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080094}
95
Christopher Wileyae589972016-01-29 11:19:23 -080096LiteralExpression::LiteralExpression(const string& v) : value(v) {}
97
98void LiteralExpression::Write(CodeWriter* to) const {
99 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100}
101
Christopher Wileyae589972016-01-29 11:19:23 -0800102StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103
Christopher Wileyae589972016-01-29 11:19:23 -0800104void StringLiteralExpression::Write(CodeWriter* to) const {
105 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106}
107
Steven Moreland3dc29d82019-08-21 17:23:11 -0700108Variable::Variable(const string& t, const string& n) : type(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109
Christopher Wileyae589972016-01-29 11:19:23 -0800110void Variable::WriteDeclaration(CodeWriter* to) const {
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900111 for (const auto& a : this->annotations) {
112 to->Write("%s ", a.c_str());
113 }
Steven Moreland3dc29d82019-08-21 17:23:11 -0700114 to->Write("%s %s", this->type.c_str(), this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115}
116
Christopher Wileyae589972016-01-29 11:19:23 -0800117void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118
Steven Moreland48548e02019-09-18 15:10:22 -0700119FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n)
120 : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121
Jeongik Cha439f2c42019-02-13 12:38:30 +0900122FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800123
124void FieldVariable::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900125 visit(
Steven Moreland48548e02019-09-18 15:10:22 -0700126 overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); },
Jeongik Cha439f2c42019-02-13 12:38:30 +0900127 [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
128 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800129 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800130}
131
Jiyong Park176905e2018-07-04 22:29:41 +0900132LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
133
134void LiteralStatement::Write(CodeWriter* to) const {
135 to->Write("%s", value_.c_str());
136}
137
Christopher Wileyae589972016-01-29 11:19:23 -0800138void StatementBlock::Write(CodeWriter* to) const {
139 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900140 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800141 int N = this->statements.size();
142 for (int i = 0; i < N; i++) {
143 this->statements[i]->Write(to);
144 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900145 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800146 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147}
148
Steven Moreland48548e02019-09-18 15:10:22 -0700149void StatementBlock::Add(std::shared_ptr<Statement> statement) {
Christopher Wileyae589972016-01-29 11:19:23 -0800150 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151}
152
Steven Moreland48548e02019-09-18 15:10:22 -0700153void StatementBlock::Add(std::shared_ptr<Expression> expression) {
154 this->statements.push_back(std::make_shared<ExpressionStatement>(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155}
156
Steven Moreland48548e02019-09-18 15:10:22 -0700157ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800158
Christopher Wileyae589972016-01-29 11:19:23 -0800159void ExpressionStatement::Write(CodeWriter* to) const {
160 this->expression->Write(to);
161 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800162}
163
Steven Moreland48548e02019-09-18 15:10:22 -0700164Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
165 : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800166
Steven Moreland48548e02019-09-18 15:10:22 -0700167Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c)
168 : lvalue(l), rvalue(r), cast(c) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800169
170void Assignment::Write(CodeWriter* to) const {
171 this->lvalue->Write(to);
172 to->Write(" = ");
Jeongik Cha439f2c42019-02-13 12:38:30 +0900173 if (this->cast) {
174 to->Write("(%s)", this->cast->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800175 }
176 this->rvalue->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800177}
178
Christopher Wileyae589972016-01-29 11:19:23 -0800179MethodCall::MethodCall(const string& n) : name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800180
Steven Moreland48548e02019-09-18 15:10:22 -0700181MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args)
182 : name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183
Steven Moreland48548e02019-09-18 15:10:22 -0700184MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800185
Jeongik Cha439f2c42019-02-13 12:38:30 +0900186MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800187
Steven Moreland48548e02019-09-18 15:10:22 -0700188MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n,
189 const std::vector<std::shared_ptr<Expression>>& args)
190 : receiver(o), name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800191
Steven Moreland48548e02019-09-18 15:10:22 -0700192MethodCall::MethodCall(const std::string& t, const string& n,
193 const std::vector<std::shared_ptr<Expression>>& args)
194 : receiver(t), name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800195
Christopher Wileyae589972016-01-29 11:19:23 -0800196void MethodCall::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900197 visit(
Steven Moreland48548e02019-09-18 15:10:22 -0700198 overloaded{[&](std::shared_ptr<Expression> e) {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900199 e->Write(to);
200 to->Write(".");
201 },
202 [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
203 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800204 to->Write("%s(", this->name.c_str());
205 WriteArgumentList(to, this->arguments);
206 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800207}
208
Steven Moreland48548e02019-09-18 15:10:22 -0700209Comparison::Comparison(std::shared_ptr<Expression> l, const string& o,
210 std::shared_ptr<Expression> r)
Christopher Wileyae589972016-01-29 11:19:23 -0800211 : lvalue(l), op(o), rvalue(r) {}
212
213void Comparison::Write(CodeWriter* to) const {
214 to->Write("(");
215 this->lvalue->Write(to);
216 to->Write("%s", this->op.c_str());
217 this->rvalue->Write(to);
218 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219}
220
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900221NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222
Steven Moreland48548e02019-09-18 15:10:22 -0700223NewExpression::NewExpression(const std::string& n,
224 const std::vector<std::shared_ptr<Expression>>& args)
225 : instantiableName(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226
Christopher Wileyae589972016-01-29 11:19:23 -0800227void NewExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900228 to->Write("new %s(", this->instantiableName.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800229 WriteArgumentList(to, this->arguments);
230 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800231}
232
Steven Moreland48548e02019-09-18 15:10:22 -0700233NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s)
234 : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800235
Christopher Wileyae589972016-01-29 11:19:23 -0800236void NewArrayExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900237 to->Write("new %s[", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800238 size->Write(to);
239 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800240}
241
Steven Moreland48548e02019-09-18 15:10:22 -0700242Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800243
244void Cast::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900245 to->Write("((%s)", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800246 expression->Write(to);
247 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800248}
249
Steven Moreland48548e02019-09-18 15:10:22 -0700250VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
251 : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252
Steven Moreland48548e02019-09-18 15:10:22 -0700253VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800254
Christopher Wileyae589972016-01-29 11:19:23 -0800255void VariableDeclaration::Write(CodeWriter* to) const {
256 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700257 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800258 to->Write(" = ");
Christopher Wileyae589972016-01-29 11:19:23 -0800259 this->rvalue->Write(to);
260 }
261 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800262}
263
Christopher Wileyae589972016-01-29 11:19:23 -0800264void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700265 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800266 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800267 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800268 to->Write(") ");
269 }
270 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700271 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800272 to->Write("else ");
273 this->elseif->Write(to);
274 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800275}
276
Steven Moreland48548e02019-09-18 15:10:22 -0700277ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800278
279void ReturnStatement::Write(CodeWriter* to) const {
280 to->Write("return ");
281 this->expression->Write(to);
282 to->Write(";\n");
283}
284
285void TryStatement::Write(CodeWriter* to) const {
286 to->Write("try ");
287 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800288}
289
Christopher Wileyae589972016-01-29 11:19:23 -0800290void FinallyStatement::Write(CodeWriter* to) const {
291 to->Write("finally ");
292 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800293}
294
Christopher Wileyae589972016-01-29 11:19:23 -0800295Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296
Christopher Wileyae589972016-01-29 11:19:23 -0800297void Case::Write(CodeWriter* to) const {
298 int N = this->cases.size();
299 if (N > 0) {
300 for (int i = 0; i < N; i++) {
301 string s = this->cases[i];
302 if (s.length() != 0) {
303 to->Write("case %s:\n", s.c_str());
304 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700305 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800306 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800307 }
Christopher Wileyae589972016-01-29 11:19:23 -0800308 } else {
309 to->Write("default:\n");
310 }
311 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800312}
313
Steven Moreland48548e02019-09-18 15:10:22 -0700314SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800315
316void SwitchStatement::Write(CodeWriter* to) const {
317 to->Write("switch (");
318 this->expression->Write(to);
319 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900320 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800321 int N = this->cases.size();
322 for (int i = 0; i < N; i++) {
323 this->cases[i]->Write(to);
324 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900325 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800326 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800327}
328
Christopher Wileyae589972016-01-29 11:19:23 -0800329void Method::Write(CodeWriter* to) const {
330 size_t N, i;
331
332 if (this->comment.length() != 0) {
333 to->Write("%s\n", this->comment.c_str());
334 }
335
Jiyong Parka6605ab2018-11-11 14:30:21 +0900336 for (const auto& a : this->annotations) {
337 to->Write("%s\n", a.c_str());
338 }
339
Christopher Wileyae589972016-01-29 11:19:23 -0800340 WriteModifiers(to, this->modifiers,
341 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
342
Jeongik Cha6cadc212019-02-12 18:16:03 +0900343 if (this->returnType) {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700344 to->Write("%s ", this->returnType->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800345 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800346
Christopher Wileyae589972016-01-29 11:19:23 -0800347 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348
Christopher Wileyae589972016-01-29 11:19:23 -0800349 N = this->parameters.size();
350 for (i = 0; i < N; i++) {
351 this->parameters[i]->WriteDeclaration(to);
352 if (i != N - 1) {
353 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800354 }
Christopher Wileyae589972016-01-29 11:19:23 -0800355 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800356
Christopher Wileyae589972016-01-29 11:19:23 -0800357 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800358
Christopher Wileyae589972016-01-29 11:19:23 -0800359 N = this->exceptions.size();
360 for (i = 0; i < N; i++) {
361 if (i == 0) {
362 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800363 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800364 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800365 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900366 to->Write("%s", this->exceptions[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800367 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800368
Yi Kong894d6ba2018-07-24 11:27:38 -0700369 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800370 to->Write(";\n");
371 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700372 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800373 this->statements->Write(to);
374 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375}
376
Steven Moreland5557f1c2018-07-02 13:50:23 -0700377void LiteralClassElement::Write(CodeWriter* to) const {
378 to->Write("%s", element.c_str());
379}
380
Christopher Wileyae589972016-01-29 11:19:23 -0800381void Class::Write(CodeWriter* to) const {
382 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383
Christopher Wileyae589972016-01-29 11:19:23 -0800384 if (this->comment.length() != 0) {
385 to->Write("%s\n", this->comment.c_str());
386 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900387 for (const auto& a : this->annotations) {
388 to->Write("%s\n", a.c_str());
389 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800390
Christopher Wileyae589972016-01-29 11:19:23 -0800391 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
392
393 if (this->what == Class::CLASS) {
394 to->Write("class ");
395 } else {
396 to->Write("interface ");
397 }
398
Jeongik Chaaabc1442019-02-12 17:44:48 +0900399 string name = this->type;
Christopher Wileyae589972016-01-29 11:19:23 -0800400 size_t pos = name.rfind('.');
401 if (pos != string::npos) {
402 name = name.c_str() + pos + 1;
403 }
404
405 to->Write("%s", name.c_str());
406
Jeongik Chaaabc1442019-02-12 17:44:48 +0900407 if (this->extends) {
408 to->Write(" extends %s", this->extends->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800409 }
410
411 N = this->interfaces.size();
412 if (N != 0) {
413 if (this->what == Class::CLASS) {
414 to->Write(" implements");
415 } else {
416 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800417 }
Christopher Wileyae589972016-01-29 11:19:23 -0800418 for (i = 0; i < N; i++) {
Jeongik Chaaabc1442019-02-12 17:44:48 +0900419 to->Write(" %s", this->interfaces[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800420 }
421 }
422
423 to->Write("\n");
424 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900425 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800426
427 N = this->elements.size();
428 for (i = 0; i < N; i++) {
429 this->elements[i]->Write(to);
430 }
431
Jiyong Parka755dc72018-06-29 13:52:24 +0900432 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800433 to->Write("}\n");
434}
435
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800436Document::Document(const std::string& comment,
437 const std::string& package,
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800438 std::unique_ptr<Class> clazz)
439 : comment_(comment),
440 package_(package),
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800441 clazz_(std::move(clazz)) {
442}
Christopher Wileyae589972016-01-29 11:19:23 -0800443
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800444void Document::Write(CodeWriter* to) const {
445 if (!comment_.empty()) {
446 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800447 }
448 to->Write(
449 "/*\n"
450 " * This file is auto-generated. DO NOT MODIFY.\n"
Jiyong Park54fa1e02019-02-08 10:03:20 +0900451 " */\n");
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800452 if (!package_.empty()) {
453 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800454 }
455
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800456 if (clazz_) {
457 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800458 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800459}
460
Steven Moreland48548e02019-09-18 15:10:22 -0700461std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
462std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
463std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
464std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
465std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
Christopher Wileydb154a52015-09-28 16:32:25 -0700466} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700467} // namespace aidl
468} // namespace android