blob: 5eab218990a1a0d0f114c730838f962e1118bd95 [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
Christopher Wileyae589972016-01-29 11:19:23 -080068void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) {
69 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
Christopher Wileyae589972016-01-29 11:19:23 -080078Field::Field(int m, 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 {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700111 to->Write("%s %s", this->type.c_str(), this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112}
113
Christopher Wileyae589972016-01-29 11:19:23 -0800114void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115
Jeongik Cha439f2c42019-02-13 12:38:30 +0900116FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800117
Jeongik Cha439f2c42019-02-13 12:38:30 +0900118FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800119
120void FieldVariable::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900121 visit(
122 overloaded{[&](Expression* e) { e->Write(to); },
123 [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
124 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800125 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800126}
127
Jiyong Park176905e2018-07-04 22:29:41 +0900128LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
129
130void LiteralStatement::Write(CodeWriter* to) const {
131 to->Write("%s", value_.c_str());
132}
133
Christopher Wileyae589972016-01-29 11:19:23 -0800134void StatementBlock::Write(CodeWriter* to) const {
135 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900136 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800137 int N = this->statements.size();
138 for (int i = 0; i < N; i++) {
139 this->statements[i]->Write(to);
140 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900141 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800142 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800143}
144
Christopher Wileyae589972016-01-29 11:19:23 -0800145void StatementBlock::Add(Statement* statement) {
146 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147}
148
Christopher Wileyae589972016-01-29 11:19:23 -0800149void StatementBlock::Add(Expression* expression) {
150 this->statements.push_back(new ExpressionStatement(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151}
152
Christopher Wileyae589972016-01-29 11:19:23 -0800153ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800154
Christopher Wileyae589972016-01-29 11:19:23 -0800155void ExpressionStatement::Write(CodeWriter* to) const {
156 this->expression->Write(to);
157 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800158}
159
Jeongik Cha439f2c42019-02-13 12:38:30 +0900160Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800161
Jeongik Cha439f2c42019-02-13 12:38:30 +0900162Assignment::Assignment(Variable* l, Expression* r, string c) : lvalue(l), rvalue(r), cast(c) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800163
164void Assignment::Write(CodeWriter* to) const {
165 this->lvalue->Write(to);
166 to->Write(" = ");
Jeongik Cha439f2c42019-02-13 12:38:30 +0900167 if (this->cast) {
168 to->Write("(%s)", this->cast->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800169 }
170 this->rvalue->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800171}
172
Christopher Wileyae589972016-01-29 11:19:23 -0800173MethodCall::MethodCall(const string& n) : name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800174
Christopher Wileyae589972016-01-29 11:19:23 -0800175MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800176 va_list args;
177 va_start(args, argc);
178 init(argc, args);
179 va_end(args);
180}
181
Jeongik Cha439f2c42019-02-13 12:38:30 +0900182MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183
Jeongik Cha439f2c42019-02-13 12:38:30 +0900184MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800185
Jeongik Cha439f2c42019-02-13 12:38:30 +0900186MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800187 va_list args;
188 va_start(args, argc);
189 init(argc, args);
190 va_end(args);
191}
192
Jeongik Cha439f2c42019-02-13 12:38:30 +0900193MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...)
194 : receiver(t), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800195 va_list args;
196 va_start(args, argc);
197 init(argc, args);
198 va_end(args);
199}
200
Christopher Wileyae589972016-01-29 11:19:23 -0800201void MethodCall::init(int n, va_list args) {
202 for (int i = 0; i < n; i++) {
203 Expression* expression = (Expression*)va_arg(args, void*);
204 this->arguments.push_back(expression);
205 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800206}
207
Christopher Wileyae589972016-01-29 11:19:23 -0800208void MethodCall::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900209 visit(
210 overloaded{[&](Expression* e) {
211 e->Write(to);
212 to->Write(".");
213 },
214 [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
215 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800216 to->Write("%s(", this->name.c_str());
217 WriteArgumentList(to, this->arguments);
218 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219}
220
221Comparison::Comparison(Expression* l, const string& o, Expression* r)
Christopher Wileyae589972016-01-29 11:19:23 -0800222 : lvalue(l), op(o), rvalue(r) {}
223
224void Comparison::Write(CodeWriter* to) const {
225 to->Write("(");
226 this->lvalue->Write(to);
227 to->Write("%s", this->op.c_str());
228 this->rvalue->Write(to);
229 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800230}
231
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900232NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900234NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800235 va_list args;
236 va_start(args, argc);
237 init(argc, args);
238 va_end(args);
239}
240
Christopher Wileyae589972016-01-29 11:19:23 -0800241void NewExpression::init(int n, va_list args) {
242 for (int i = 0; i < n; i++) {
243 Expression* expression = (Expression*)va_arg(args, void*);
244 this->arguments.push_back(expression);
245 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800246}
247
Christopher Wileyae589972016-01-29 11:19:23 -0800248void NewExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900249 to->Write("new %s(", this->instantiableName.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800250 WriteArgumentList(to, this->arguments);
251 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252}
253
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900254NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255
Christopher Wileyae589972016-01-29 11:19:23 -0800256void NewArrayExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900257 to->Write("new %s[", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800258 size->Write(to);
259 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800260}
261
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900262Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800263
264void Cast::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900265 to->Write("((%s)", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800266 expression->Write(to);
267 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800268}
269
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900270VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800271
Christopher Wileyae589972016-01-29 11:19:23 -0800272VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800273
Christopher Wileyae589972016-01-29 11:19:23 -0800274void VariableDeclaration::Write(CodeWriter* to) const {
275 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700276 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800277 to->Write(" = ");
Christopher Wileyae589972016-01-29 11:19:23 -0800278 this->rvalue->Write(to);
279 }
280 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281}
282
Christopher Wileyae589972016-01-29 11:19:23 -0800283void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700284 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800285 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800286 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800287 to->Write(") ");
288 }
289 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700290 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800291 to->Write("else ");
292 this->elseif->Write(to);
293 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800294}
295
Christopher Wileyae589972016-01-29 11:19:23 -0800296ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
297
298void ReturnStatement::Write(CodeWriter* to) const {
299 to->Write("return ");
300 this->expression->Write(to);
301 to->Write(";\n");
302}
303
304void TryStatement::Write(CodeWriter* to) const {
305 to->Write("try ");
306 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800307}
308
Christopher Wileyae589972016-01-29 11:19:23 -0800309void FinallyStatement::Write(CodeWriter* to) const {
310 to->Write("finally ");
311 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800312}
313
Christopher Wileyae589972016-01-29 11:19:23 -0800314Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800315
Christopher Wileyae589972016-01-29 11:19:23 -0800316void Case::Write(CodeWriter* to) const {
317 int N = this->cases.size();
318 if (N > 0) {
319 for (int i = 0; i < N; i++) {
320 string s = this->cases[i];
321 if (s.length() != 0) {
322 to->Write("case %s:\n", s.c_str());
323 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700324 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800325 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800326 }
Christopher Wileyae589972016-01-29 11:19:23 -0800327 } else {
328 to->Write("default:\n");
329 }
330 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800331}
332
Christopher Wileyae589972016-01-29 11:19:23 -0800333SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
334
335void SwitchStatement::Write(CodeWriter* to) const {
336 to->Write("switch (");
337 this->expression->Write(to);
338 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900339 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800340 int N = this->cases.size();
341 for (int i = 0; i < N; i++) {
342 this->cases[i]->Write(to);
343 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900344 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800345 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800346}
347
Christopher Wileyae589972016-01-29 11:19:23 -0800348void Method::Write(CodeWriter* to) const {
349 size_t N, i;
350
351 if (this->comment.length() != 0) {
352 to->Write("%s\n", this->comment.c_str());
353 }
354
Jiyong Parka6605ab2018-11-11 14:30:21 +0900355 for (const auto& a : this->annotations) {
356 to->Write("%s\n", a.c_str());
357 }
358
Christopher Wileyae589972016-01-29 11:19:23 -0800359 WriteModifiers(to, this->modifiers,
360 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
361
Jeongik Cha6cadc212019-02-12 18:16:03 +0900362 if (this->returnType) {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700363 to->Write("%s ", this->returnType->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800364 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800365
Christopher Wileyae589972016-01-29 11:19:23 -0800366 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367
Christopher Wileyae589972016-01-29 11:19:23 -0800368 N = this->parameters.size();
369 for (i = 0; i < N; i++) {
370 this->parameters[i]->WriteDeclaration(to);
371 if (i != N - 1) {
372 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800373 }
Christopher Wileyae589972016-01-29 11:19:23 -0800374 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375
Christopher Wileyae589972016-01-29 11:19:23 -0800376 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800377
Christopher Wileyae589972016-01-29 11:19:23 -0800378 N = this->exceptions.size();
379 for (i = 0; i < N; i++) {
380 if (i == 0) {
381 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800383 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800384 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900385 to->Write("%s", this->exceptions[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800386 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387
Yi Kong894d6ba2018-07-24 11:27:38 -0700388 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800389 to->Write(";\n");
390 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700391 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800392 this->statements->Write(to);
393 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800394}
395
Steven Moreland5557f1c2018-07-02 13:50:23 -0700396void LiteralClassElement::Write(CodeWriter* to) const {
397 to->Write("%s", element.c_str());
398}
399
Christopher Wileyae589972016-01-29 11:19:23 -0800400void Class::Write(CodeWriter* to) const {
401 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800402
Christopher Wileyae589972016-01-29 11:19:23 -0800403 if (this->comment.length() != 0) {
404 to->Write("%s\n", this->comment.c_str());
405 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900406 for (const auto& a : this->annotations) {
407 to->Write("%s\n", a.c_str());
408 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800409
Christopher Wileyae589972016-01-29 11:19:23 -0800410 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
411
412 if (this->what == Class::CLASS) {
413 to->Write("class ");
414 } else {
415 to->Write("interface ");
416 }
417
Jeongik Chaaabc1442019-02-12 17:44:48 +0900418 string name = this->type;
Christopher Wileyae589972016-01-29 11:19:23 -0800419 size_t pos = name.rfind('.');
420 if (pos != string::npos) {
421 name = name.c_str() + pos + 1;
422 }
423
424 to->Write("%s", name.c_str());
425
Jeongik Chaaabc1442019-02-12 17:44:48 +0900426 if (this->extends) {
427 to->Write(" extends %s", this->extends->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800428 }
429
430 N = this->interfaces.size();
431 if (N != 0) {
432 if (this->what == Class::CLASS) {
433 to->Write(" implements");
434 } else {
435 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800436 }
Christopher Wileyae589972016-01-29 11:19:23 -0800437 for (i = 0; i < N; i++) {
Jeongik Chaaabc1442019-02-12 17:44:48 +0900438 to->Write(" %s", this->interfaces[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800439 }
440 }
441
442 to->Write("\n");
443 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900444 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800445
446 N = this->elements.size();
447 for (i = 0; i < N; i++) {
448 this->elements[i]->Write(to);
449 }
450
Jiyong Parka755dc72018-06-29 13:52:24 +0900451 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800452 to->Write("}\n");
453}
454
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800455Document::Document(const std::string& comment,
456 const std::string& package,
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800457 std::unique_ptr<Class> clazz)
458 : comment_(comment),
459 package_(package),
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800460 clazz_(std::move(clazz)) {
461}
Christopher Wileyae589972016-01-29 11:19:23 -0800462
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800463void Document::Write(CodeWriter* to) const {
464 if (!comment_.empty()) {
465 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800466 }
467 to->Write(
468 "/*\n"
469 " * This file is auto-generated. DO NOT MODIFY.\n"
Jiyong Park54fa1e02019-02-08 10:03:20 +0900470 " */\n");
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800471 if (!package_.empty()) {
472 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800473 }
474
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800475 if (clazz_) {
476 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800477 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800478}
479
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900480Expression* NULL_VALUE = new LiteralExpression("null");
481Expression* THIS_VALUE = new LiteralExpression("this");
482Expression* SUPER_VALUE = new LiteralExpression("super");
483Expression* TRUE_VALUE = new LiteralExpression("true");
484Expression* FALSE_VALUE = new LiteralExpression("false");
Christopher Wileydb154a52015-09-28 16:32:25 -0700485} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700486} // namespace aidl
487} // namespace android