blob: d38319c46cf8e0e3ce13528f632782e04c918ba7 [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
Jeongik Chadc77c1b2019-02-12 16:13:25 +0900108Variable::Variable(const string& t, const string& n) : type(t), name(n), dimension(0) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109
Jeongik Chadc77c1b2019-02-12 16:13:25 +0900110Variable::Variable(const string& t, const string& n, int d) : type(t), name(n), dimension(d) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800111
Christopher Wileyae589972016-01-29 11:19:23 -0800112void Variable::WriteDeclaration(CodeWriter* to) const {
113 string dim;
114 for (int i = 0; i < this->dimension; i++) {
115 dim += "[]";
116 }
Jeongik Chadc77c1b2019-02-12 16:13:25 +0900117 to->Write("%s%s %s", this->type.c_str(), dim.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
Jeongik Cha439f2c42019-02-13 12:38:30 +0900122FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800123
Jeongik Cha439f2c42019-02-13 12:38:30 +0900124FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800125
126void FieldVariable::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900127 visit(
128 overloaded{[&](Expression* e) { e->Write(to); },
129 [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
130 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800131 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132}
133
Jiyong Park176905e2018-07-04 22:29:41 +0900134LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
135
136void LiteralStatement::Write(CodeWriter* to) const {
137 to->Write("%s", value_.c_str());
138}
139
Christopher Wileyae589972016-01-29 11:19:23 -0800140void StatementBlock::Write(CodeWriter* to) const {
141 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900142 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800143 int N = this->statements.size();
144 for (int i = 0; i < N; i++) {
145 this->statements[i]->Write(to);
146 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900147 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800148 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149}
150
Christopher Wileyae589972016-01-29 11:19:23 -0800151void StatementBlock::Add(Statement* statement) {
152 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153}
154
Christopher Wileyae589972016-01-29 11:19:23 -0800155void StatementBlock::Add(Expression* expression) {
156 this->statements.push_back(new ExpressionStatement(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800157}
158
Christopher Wileyae589972016-01-29 11:19:23 -0800159ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800160
Christopher Wileyae589972016-01-29 11:19:23 -0800161void ExpressionStatement::Write(CodeWriter* to) const {
162 this->expression->Write(to);
163 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800164}
165
Jeongik Cha439f2c42019-02-13 12:38:30 +0900166Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800167
Jeongik Cha439f2c42019-02-13 12:38:30 +0900168Assignment::Assignment(Variable* l, Expression* r, string c) : 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
Christopher Wileyae589972016-01-29 11:19:23 -0800181MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800182 va_list args;
183 va_start(args, argc);
184 init(argc, args);
185 va_end(args);
186}
187
Jeongik Cha439f2c42019-02-13 12:38:30 +0900188MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800189
Jeongik Cha439f2c42019-02-13 12:38:30 +0900190MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800191
Jeongik Cha439f2c42019-02-13 12:38:30 +0900192MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800193 va_list args;
194 va_start(args, argc);
195 init(argc, args);
196 va_end(args);
197}
198
Jeongik Cha439f2c42019-02-13 12:38:30 +0900199MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...)
200 : receiver(t), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800201 va_list args;
202 va_start(args, argc);
203 init(argc, args);
204 va_end(args);
205}
206
Christopher Wileyae589972016-01-29 11:19:23 -0800207void MethodCall::init(int n, va_list args) {
208 for (int i = 0; i < n; i++) {
209 Expression* expression = (Expression*)va_arg(args, void*);
210 this->arguments.push_back(expression);
211 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800212}
213
Christopher Wileyae589972016-01-29 11:19:23 -0800214void MethodCall::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900215 visit(
216 overloaded{[&](Expression* e) {
217 e->Write(to);
218 to->Write(".");
219 },
220 [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
221 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800222 to->Write("%s(", this->name.c_str());
223 WriteArgumentList(to, this->arguments);
224 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225}
226
227Comparison::Comparison(Expression* l, const string& o, Expression* r)
Christopher Wileyae589972016-01-29 11:19:23 -0800228 : lvalue(l), op(o), rvalue(r) {}
229
230void Comparison::Write(CodeWriter* to) const {
231 to->Write("(");
232 this->lvalue->Write(to);
233 to->Write("%s", this->op.c_str());
234 this->rvalue->Write(to);
235 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236}
237
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900238NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900240NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800241 va_list args;
242 va_start(args, argc);
243 init(argc, args);
244 va_end(args);
245}
246
Christopher Wileyae589972016-01-29 11:19:23 -0800247void NewExpression::init(int n, va_list args) {
248 for (int i = 0; i < n; i++) {
249 Expression* expression = (Expression*)va_arg(args, void*);
250 this->arguments.push_back(expression);
251 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252}
253
Christopher Wileyae589972016-01-29 11:19:23 -0800254void NewExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900255 to->Write("new %s(", this->instantiableName.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800256 WriteArgumentList(to, this->arguments);
257 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800258}
259
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900260NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800261
Christopher Wileyae589972016-01-29 11:19:23 -0800262void NewArrayExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900263 to->Write("new %s[", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800264 size->Write(to);
265 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800266}
267
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900268Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800269
270void Cast::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900271 to->Write("((%s)", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800272 expression->Write(to);
273 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800274}
275
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900276VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800277
Christopher Wileyae589972016-01-29 11:19:23 -0800278VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800279
Christopher Wileyae589972016-01-29 11:19:23 -0800280void VariableDeclaration::Write(CodeWriter* to) const {
281 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700282 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800283 to->Write(" = ");
Christopher Wileyae589972016-01-29 11:19:23 -0800284 this->rvalue->Write(to);
285 }
286 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800287}
288
Christopher Wileyae589972016-01-29 11:19:23 -0800289void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700290 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800291 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800292 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800293 to->Write(") ");
294 }
295 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700296 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800297 to->Write("else ");
298 this->elseif->Write(to);
299 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300}
301
Christopher Wileyae589972016-01-29 11:19:23 -0800302ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
303
304void ReturnStatement::Write(CodeWriter* to) const {
305 to->Write("return ");
306 this->expression->Write(to);
307 to->Write(";\n");
308}
309
310void TryStatement::Write(CodeWriter* to) const {
311 to->Write("try ");
312 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800313}
314
Christopher Wileyae589972016-01-29 11:19:23 -0800315void FinallyStatement::Write(CodeWriter* to) const {
316 to->Write("finally ");
317 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800318}
319
Christopher Wileyae589972016-01-29 11:19:23 -0800320Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800321
Christopher Wileyae589972016-01-29 11:19:23 -0800322void Case::Write(CodeWriter* to) const {
323 int N = this->cases.size();
324 if (N > 0) {
325 for (int i = 0; i < N; i++) {
326 string s = this->cases[i];
327 if (s.length() != 0) {
328 to->Write("case %s:\n", s.c_str());
329 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700330 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800331 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800332 }
Christopher Wileyae589972016-01-29 11:19:23 -0800333 } else {
334 to->Write("default:\n");
335 }
336 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800337}
338
Christopher Wileyae589972016-01-29 11:19:23 -0800339SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
340
341void SwitchStatement::Write(CodeWriter* to) const {
342 to->Write("switch (");
343 this->expression->Write(to);
344 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900345 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800346 int N = this->cases.size();
347 for (int i = 0; i < N; i++) {
348 this->cases[i]->Write(to);
349 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900350 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800351 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800352}
353
Christopher Wileyae589972016-01-29 11:19:23 -0800354void Method::Write(CodeWriter* to) const {
355 size_t N, i;
356
357 if (this->comment.length() != 0) {
358 to->Write("%s\n", this->comment.c_str());
359 }
360
Jiyong Parka6605ab2018-11-11 14:30:21 +0900361 for (const auto& a : this->annotations) {
362 to->Write("%s\n", a.c_str());
363 }
364
Christopher Wileyae589972016-01-29 11:19:23 -0800365 WriteModifiers(to, this->modifiers,
366 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
367
Jeongik Cha6cadc212019-02-12 18:16:03 +0900368 if (this->returnType) {
Christopher Wileyae589972016-01-29 11:19:23 -0800369 string dim;
370 for (i = 0; i < this->returnTypeDimension; i++) {
371 dim += "[]";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800372 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900373 to->Write("%s%s ", this->returnType->c_str(), dim.c_str());
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("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800377
Christopher Wileyae589972016-01-29 11:19:23 -0800378 N = this->parameters.size();
379 for (i = 0; i < N; i++) {
380 this->parameters[i]->WriteDeclaration(to);
381 if (i != N - 1) {
382 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383 }
Christopher Wileyae589972016-01-29 11:19:23 -0800384 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800385
Christopher Wileyae589972016-01-29 11:19:23 -0800386 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387
Christopher Wileyae589972016-01-29 11:19:23 -0800388 N = this->exceptions.size();
389 for (i = 0; i < N; i++) {
390 if (i == 0) {
391 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800392 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800393 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800394 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900395 to->Write("%s", this->exceptions[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800396 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800397
Yi Kong894d6ba2018-07-24 11:27:38 -0700398 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800399 to->Write(";\n");
400 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700401 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800402 this->statements->Write(to);
403 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800404}
405
Steven Moreland5557f1c2018-07-02 13:50:23 -0700406void LiteralClassElement::Write(CodeWriter* to) const {
407 to->Write("%s", element.c_str());
408}
409
Christopher Wileyae589972016-01-29 11:19:23 -0800410void Class::Write(CodeWriter* to) const {
411 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800412
Christopher Wileyae589972016-01-29 11:19:23 -0800413 if (this->comment.length() != 0) {
414 to->Write("%s\n", this->comment.c_str());
415 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900416 for (const auto& a : this->annotations) {
417 to->Write("%s\n", a.c_str());
418 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800419
Christopher Wileyae589972016-01-29 11:19:23 -0800420 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
421
422 if (this->what == Class::CLASS) {
423 to->Write("class ");
424 } else {
425 to->Write("interface ");
426 }
427
Jeongik Chaaabc1442019-02-12 17:44:48 +0900428 string name = this->type;
Christopher Wileyae589972016-01-29 11:19:23 -0800429 size_t pos = name.rfind('.');
430 if (pos != string::npos) {
431 name = name.c_str() + pos + 1;
432 }
433
434 to->Write("%s", name.c_str());
435
Jeongik Chaaabc1442019-02-12 17:44:48 +0900436 if (this->extends) {
437 to->Write(" extends %s", this->extends->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800438 }
439
440 N = this->interfaces.size();
441 if (N != 0) {
442 if (this->what == Class::CLASS) {
443 to->Write(" implements");
444 } else {
445 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800446 }
Christopher Wileyae589972016-01-29 11:19:23 -0800447 for (i = 0; i < N; i++) {
Jeongik Chaaabc1442019-02-12 17:44:48 +0900448 to->Write(" %s", this->interfaces[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800449 }
450 }
451
452 to->Write("\n");
453 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900454 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800455
456 N = this->elements.size();
457 for (i = 0; i < N; i++) {
458 this->elements[i]->Write(to);
459 }
460
Jiyong Parka755dc72018-06-29 13:52:24 +0900461 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800462 to->Write("}\n");
463}
464
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800465Document::Document(const std::string& comment,
466 const std::string& package,
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800467 std::unique_ptr<Class> clazz)
468 : comment_(comment),
469 package_(package),
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800470 clazz_(std::move(clazz)) {
471}
Christopher Wileyae589972016-01-29 11:19:23 -0800472
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800473void Document::Write(CodeWriter* to) const {
474 if (!comment_.empty()) {
475 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800476 }
477 to->Write(
478 "/*\n"
479 " * This file is auto-generated. DO NOT MODIFY.\n"
Jiyong Park54fa1e02019-02-08 10:03:20 +0900480 " */\n");
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800481 if (!package_.empty()) {
482 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800483 }
484
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800485 if (clazz_) {
486 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800487 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800488}
489
Christopher Wileydb154a52015-09-28 16:32:25 -0700490} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700491} // namespace aidl
492} // namespace android