blob: 0be172cfbdec50b3b525d9374348d81c76b3aa5e [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"
18
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070019#include "code_writer.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070020#include "type_java.h"
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070021
Christopher Wiley12e894a2016-01-29 11:55:07 -080022using std::vector;
23using std::string;
24
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070025namespace android {
26namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070027namespace java {
Adam Lesinskiffa16862014-01-23 18:17:42 -080028
Jiyong Park176905e2018-07-04 22:29:41 +090029std::string AstNode::ToString() {
30 std::string str;
31 Write(CodeWriter::ForString(&str).get());
32 return str;
33}
34
Christopher Wileyae589972016-01-29 11:19:23 -080035void WriteModifiers(CodeWriter* to, int mod, int mask) {
36 int m = mod & mask;
Adam Lesinskiffa16862014-01-23 18:17:42 -080037
Christopher Wileyae589972016-01-29 11:19:23 -080038 if (m & OVERRIDE) {
39 to->Write("@Override ");
40 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080041
Christopher Wileyae589972016-01-29 11:19:23 -080042 if ((m & SCOPE_MASK) == PUBLIC) {
43 to->Write("public ");
44 } else if ((m & SCOPE_MASK) == PRIVATE) {
45 to->Write("private ");
46 } else if ((m & SCOPE_MASK) == PROTECTED) {
47 to->Write("protected ");
48 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080049
Christopher Wileyae589972016-01-29 11:19:23 -080050 if (m & STATIC) {
51 to->Write("static ");
52 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080053
Christopher Wileyae589972016-01-29 11:19:23 -080054 if (m & FINAL) {
55 to->Write("final ");
56 }
57
58 if (m & ABSTRACT) {
59 to->Write("abstract ");
60 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080061}
62
Christopher Wileyae589972016-01-29 11:19:23 -080063void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) {
64 size_t N = arguments.size();
65 for (size_t i = 0; i < N; i++) {
66 arguments[i]->Write(to);
67 if (i != N - 1) {
68 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080069 }
Christopher Wileyae589972016-01-29 11:19:23 -080070 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080071}
72
Christopher Wileyae589972016-01-29 11:19:23 -080073Field::Field(int m, Variable* v) : ClassElement(), modifiers(m), variable(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080074
Christopher Wileyae589972016-01-29 11:19:23 -080075void Field::Write(CodeWriter* to) const {
76 if (this->comment.length() != 0) {
77 to->Write("%s\n", this->comment.c_str());
78 }
Jiyong Parka6605ab2018-11-11 14:30:21 +090079 for (const auto& a : this->annotations) {
80 to->Write("%s\n", a.c_str());
81 }
Christopher Wileyae589972016-01-29 11:19:23 -080082 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Steven Moreland5635a8a2018-07-03 11:28:20 -070083 this->variable->WriteDeclaration(to);
84
Christopher Wileyae589972016-01-29 11:19:23 -080085 if (this->value.length() != 0) {
86 to->Write(" = %s", this->value.c_str());
87 }
88 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080089}
90
Christopher Wileyae589972016-01-29 11:19:23 -080091LiteralExpression::LiteralExpression(const string& v) : value(v) {}
92
93void LiteralExpression::Write(CodeWriter* to) const {
94 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080095}
96
Christopher Wileyae589972016-01-29 11:19:23 -080097StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080098
Christopher Wileyae589972016-01-29 11:19:23 -080099void StringLiteralExpression::Write(CodeWriter* to) const {
100 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800101}
102
Jeongik Chadc77c1b2019-02-12 16:13:25 +0900103Variable::Variable(const string& t, const string& n) : type(t), name(n), dimension(0) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104
Jeongik Chadc77c1b2019-02-12 16:13:25 +0900105Variable::Variable(const string& t, const string& n, int d) : type(t), name(n), dimension(d) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106
Christopher Wileyae589972016-01-29 11:19:23 -0800107void Variable::WriteDeclaration(CodeWriter* to) const {
108 string dim;
109 for (int i = 0; i < this->dimension; i++) {
110 dim += "[]";
111 }
Jeongik Chadc77c1b2019-02-12 16:13:25 +0900112 to->Write("%s%s %s", this->type.c_str(), dim.c_str(), this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800113}
114
Christopher Wileyae589972016-01-29 11:19:23 -0800115void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800116
117FieldVariable::FieldVariable(Expression* o, const string& n)
Yi Kong894d6ba2018-07-24 11:27:38 -0700118 : object(o), clazz(nullptr), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800119
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700120FieldVariable::FieldVariable(const Type* c, const string& n)
Yi Kong894d6ba2018-07-24 11:27:38 -0700121 : object(nullptr), clazz(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800122
123void FieldVariable::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700124 if (this->object != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800125 this->object->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700126 } else if (this->clazz != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800127 to->Write("%s", this->clazz->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800128 }
129 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
Christopher Wileyae589972016-01-29 11:19:23 -0800149void StatementBlock::Add(Statement* statement) {
150 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151}
152
Christopher Wileyae589972016-01-29 11:19:23 -0800153void StatementBlock::Add(Expression* expression) {
154 this->statements.push_back(new ExpressionStatement(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155}
156
Christopher Wileyae589972016-01-29 11:19:23 -0800157ExpressionStatement::ExpressionStatement(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
164Assignment::Assignment(Variable* l, Expression* r)
Yi Kong894d6ba2018-07-24 11:27:38 -0700165 : lvalue(l), rvalue(r), cast(nullptr) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800166
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700167Assignment::Assignment(Variable* l, Expression* r, const Type* c)
Christopher Wileyae589972016-01-29 11:19:23 -0800168 : lvalue(l), rvalue(r), cast(c) {}
169
170void Assignment::Write(CodeWriter* to) const {
171 this->lvalue->Write(to);
172 to->Write(" = ");
Yi Kong894d6ba2018-07-24 11:27:38 -0700173 if (this->cast != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800174 to->Write("(%s)", this->cast->JavaType().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
Christopher Wileyae589972016-01-29 11:19:23 -0800188MethodCall::MethodCall(Expression* o, const string& n) : obj(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800189
Christopher Wileyae589972016-01-29 11:19:23 -0800190MethodCall::MethodCall(const Type* t, const string& n) : clazz(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800191
192MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
Christopher Wileyae589972016-01-29 11:19:23 -0800193 : obj(o), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800194 va_list args;
195 va_start(args, argc);
196 init(argc, args);
197 va_end(args);
198}
199
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700200MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
Christopher Wileyae589972016-01-29 11:19:23 -0800201 : clazz(t), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800202 va_list args;
203 va_start(args, argc);
204 init(argc, args);
205 va_end(args);
206}
207
Christopher Wileyae589972016-01-29 11:19:23 -0800208void MethodCall::init(int n, va_list args) {
209 for (int i = 0; i < n; i++) {
210 Expression* expression = (Expression*)va_arg(args, void*);
211 this->arguments.push_back(expression);
212 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800213}
214
Christopher Wileyae589972016-01-29 11:19:23 -0800215void MethodCall::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700216 if (this->obj != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800217 this->obj->Write(to);
218 to->Write(".");
Yi Kong894d6ba2018-07-24 11:27:38 -0700219 } else if (this->clazz != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800220 to->Write("%s.", this->clazz->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800221 }
222 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
Christopher Wileyae589972016-01-29 11:19:23 -0800238NewExpression::NewExpression(const Type* t) : type(t) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239
Christopher Wileyae589972016-01-29 11:19:23 -0800240NewExpression::NewExpression(const Type* t, int argc = 0, ...) : type(t) {
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 {
255 to->Write("new %s(", this->type->InstantiableName().c_str());
256 WriteArgumentList(to, this->arguments);
257 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800258}
259
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700260NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
Christopher Wileyae589972016-01-29 11:19:23 -0800261 : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800262
Christopher Wileyae589972016-01-29 11:19:23 -0800263void NewArrayExpression::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800264 to->Write("new %s[", this->type->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800265 size->Write(to);
266 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800267}
268
Christopher Wileyae589972016-01-29 11:19:23 -0800269Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {}
270
271void Cast::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800272 to->Write("((%s)", this->type->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800273 expression->Write(to);
274 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800275}
276
Christopher Wileyae589972016-01-29 11:19:23 -0800277VariableDeclaration::VariableDeclaration(Variable* l, Expression* r,
278 const Type* c)
279 : lvalue(l), cast(c), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800280
Christopher Wileyae589972016-01-29 11:19:23 -0800281VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800282
Christopher Wileyae589972016-01-29 11:19:23 -0800283void VariableDeclaration::Write(CodeWriter* to) const {
284 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700285 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800286 to->Write(" = ");
Yi Kong894d6ba2018-07-24 11:27:38 -0700287 if (this->cast != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800288 to->Write("(%s)", this->cast->JavaType().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800289 }
Christopher Wileyae589972016-01-29 11:19:23 -0800290 this->rvalue->Write(to);
291 }
292 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800293}
294
Christopher Wileyae589972016-01-29 11:19:23 -0800295void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700296 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800297 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800298 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800299 to->Write(") ");
300 }
301 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700302 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800303 to->Write("else ");
304 this->elseif->Write(to);
305 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306}
307
Christopher Wileyae589972016-01-29 11:19:23 -0800308ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
309
310void ReturnStatement::Write(CodeWriter* to) const {
311 to->Write("return ");
312 this->expression->Write(to);
313 to->Write(";\n");
314}
315
316void TryStatement::Write(CodeWriter* to) const {
317 to->Write("try ");
318 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800319}
320
Christopher Wileyae589972016-01-29 11:19:23 -0800321void FinallyStatement::Write(CodeWriter* to) const {
322 to->Write("finally ");
323 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324}
325
Christopher Wileyae589972016-01-29 11:19:23 -0800326Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800327
Christopher Wileyae589972016-01-29 11:19:23 -0800328void Case::Write(CodeWriter* to) const {
329 int N = this->cases.size();
330 if (N > 0) {
331 for (int i = 0; i < N; i++) {
332 string s = this->cases[i];
333 if (s.length() != 0) {
334 to->Write("case %s:\n", s.c_str());
335 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700336 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800337 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800338 }
Christopher Wileyae589972016-01-29 11:19:23 -0800339 } else {
340 to->Write("default:\n");
341 }
342 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343}
344
Christopher Wileyae589972016-01-29 11:19:23 -0800345SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
346
347void SwitchStatement::Write(CodeWriter* to) const {
348 to->Write("switch (");
349 this->expression->Write(to);
350 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900351 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800352 int N = this->cases.size();
353 for (int i = 0; i < N; i++) {
354 this->cases[i]->Write(to);
355 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900356 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800357 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800358}
359
Christopher Wileyae589972016-01-29 11:19:23 -0800360void Method::Write(CodeWriter* to) const {
361 size_t N, i;
362
363 if (this->comment.length() != 0) {
364 to->Write("%s\n", this->comment.c_str());
365 }
366
Jiyong Parka6605ab2018-11-11 14:30:21 +0900367 for (const auto& a : this->annotations) {
368 to->Write("%s\n", a.c_str());
369 }
370
Christopher Wileyae589972016-01-29 11:19:23 -0800371 WriteModifiers(to, this->modifiers,
372 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
373
Jeongik Cha6cadc212019-02-12 18:16:03 +0900374 if (this->returnType) {
Christopher Wileyae589972016-01-29 11:19:23 -0800375 string dim;
376 for (i = 0; i < this->returnTypeDimension; i++) {
377 dim += "[]";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800378 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900379 to->Write("%s%s ", this->returnType->c_str(), dim.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800380 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800381
Christopher Wileyae589972016-01-29 11:19:23 -0800382 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383
Christopher Wileyae589972016-01-29 11:19:23 -0800384 N = this->parameters.size();
385 for (i = 0; i < N; i++) {
386 this->parameters[i]->WriteDeclaration(to);
387 if (i != N - 1) {
388 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389 }
Christopher Wileyae589972016-01-29 11:19:23 -0800390 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800391
Christopher Wileyae589972016-01-29 11:19:23 -0800392 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800393
Christopher Wileyae589972016-01-29 11:19:23 -0800394 N = this->exceptions.size();
395 for (i = 0; i < N; i++) {
396 if (i == 0) {
397 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800398 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800399 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800400 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900401 to->Write("%s", this->exceptions[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800402 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800403
Yi Kong894d6ba2018-07-24 11:27:38 -0700404 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800405 to->Write(";\n");
406 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700407 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800408 this->statements->Write(to);
409 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800410}
411
Steven Moreland5557f1c2018-07-02 13:50:23 -0700412void LiteralClassElement::Write(CodeWriter* to) const {
413 to->Write("%s", element.c_str());
414}
415
Christopher Wileyae589972016-01-29 11:19:23 -0800416void Class::Write(CodeWriter* to) const {
417 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800418
Christopher Wileyae589972016-01-29 11:19:23 -0800419 if (this->comment.length() != 0) {
420 to->Write("%s\n", this->comment.c_str());
421 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900422 for (const auto& a : this->annotations) {
423 to->Write("%s\n", a.c_str());
424 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800425
Christopher Wileyae589972016-01-29 11:19:23 -0800426 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
427
428 if (this->what == Class::CLASS) {
429 to->Write("class ");
430 } else {
431 to->Write("interface ");
432 }
433
Jeongik Chaaabc1442019-02-12 17:44:48 +0900434 string name = this->type;
Christopher Wileyae589972016-01-29 11:19:23 -0800435 size_t pos = name.rfind('.');
436 if (pos != string::npos) {
437 name = name.c_str() + pos + 1;
438 }
439
440 to->Write("%s", name.c_str());
441
Jeongik Chaaabc1442019-02-12 17:44:48 +0900442 if (this->extends) {
443 to->Write(" extends %s", this->extends->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800444 }
445
446 N = this->interfaces.size();
447 if (N != 0) {
448 if (this->what == Class::CLASS) {
449 to->Write(" implements");
450 } else {
451 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800452 }
Christopher Wileyae589972016-01-29 11:19:23 -0800453 for (i = 0; i < N; i++) {
Jeongik Chaaabc1442019-02-12 17:44:48 +0900454 to->Write(" %s", this->interfaces[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800455 }
456 }
457
458 to->Write("\n");
459 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900460 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800461
462 N = this->elements.size();
463 for (i = 0; i < N; i++) {
464 this->elements[i]->Write(to);
465 }
466
Jiyong Parka755dc72018-06-29 13:52:24 +0900467 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800468 to->Write("}\n");
469}
470
471static string escape_backslashes(const string& str) {
472 string result;
473 const size_t I = str.length();
474 for (size_t i = 0; i < I; i++) {
475 char c = str[i];
476 if (c == '\\') {
477 result += "\\\\";
478 } else {
479 result += c;
480 }
481 }
482 return result;
483}
484
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800485Document::Document(const std::string& comment,
486 const std::string& package,
487 const std::string& original_src,
488 std::unique_ptr<Class> clazz)
489 : comment_(comment),
490 package_(package),
491 original_src_(original_src),
492 clazz_(std::move(clazz)) {
493}
Christopher Wileyae589972016-01-29 11:19:23 -0800494
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800495void Document::Write(CodeWriter* to) const {
496 if (!comment_.empty()) {
497 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800498 }
499 to->Write(
500 "/*\n"
501 " * This file is auto-generated. DO NOT MODIFY.\n"
502 " * Original file: %s\n"
503 " */\n",
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800504 escape_backslashes(original_src_).c_str());
505 if (!package_.empty()) {
506 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800507 }
508
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800509 if (clazz_) {
510 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800511 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800512}
513
Christopher Wileydb154a52015-09-28 16:32:25 -0700514} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700515} // namespace aidl
516} // namespace android