blob: b69452c7732a74750fc53d3b4e95900200c92686 [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
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700103Variable::Variable(const Type* t, const string& n)
Christopher Wileyae589972016-01-29 11:19:23 -0800104 : type(t), name(n), dimension(0) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800105
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700106Variable::Variable(const Type* t, const string& n, int d)
Christopher Wileyae589972016-01-29 11:19:23 -0800107 : type(t), name(n), dimension(d) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800108
Christopher Wileyae589972016-01-29 11:19:23 -0800109void Variable::WriteDeclaration(CodeWriter* to) const {
110 string dim;
111 for (int i = 0; i < this->dimension; i++) {
112 dim += "[]";
113 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800114 to->Write("%s%s %s", this->type->JavaType().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115 this->name.c_str());
116}
117
Christopher Wileyae589972016-01-29 11:19:23 -0800118void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800119
120FieldVariable::FieldVariable(Expression* o, const string& n)
Yi Kong894d6ba2018-07-24 11:27:38 -0700121 : object(o), clazz(nullptr), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700123FieldVariable::FieldVariable(const Type* c, const string& n)
Yi Kong894d6ba2018-07-24 11:27:38 -0700124 : object(nullptr), clazz(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800125
126void FieldVariable::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700127 if (this->object != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800128 this->object->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700129 } else if (this->clazz != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800130 to->Write("%s", this->clazz->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800131 }
132 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
Christopher Wileyae589972016-01-29 11:19:23 -0800152void StatementBlock::Add(Statement* statement) {
153 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800154}
155
Christopher Wileyae589972016-01-29 11:19:23 -0800156void StatementBlock::Add(Expression* expression) {
157 this->statements.push_back(new ExpressionStatement(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800158}
159
Christopher Wileyae589972016-01-29 11:19:23 -0800160ExpressionStatement::ExpressionStatement(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
167Assignment::Assignment(Variable* l, Expression* r)
Yi Kong894d6ba2018-07-24 11:27:38 -0700168 : lvalue(l), rvalue(r), cast(nullptr) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800169
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700170Assignment::Assignment(Variable* l, Expression* r, const Type* c)
Christopher Wileyae589972016-01-29 11:19:23 -0800171 : lvalue(l), rvalue(r), cast(c) {}
172
173void Assignment::Write(CodeWriter* to) const {
174 this->lvalue->Write(to);
175 to->Write(" = ");
Yi Kong894d6ba2018-07-24 11:27:38 -0700176 if (this->cast != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800177 to->Write("(%s)", this->cast->JavaType().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
Christopher Wileyae589972016-01-29 11:19:23 -0800184MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800185 va_list args;
186 va_start(args, argc);
187 init(argc, args);
188 va_end(args);
189}
190
Christopher Wileyae589972016-01-29 11:19:23 -0800191MethodCall::MethodCall(Expression* o, const string& n) : obj(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800192
Christopher Wileyae589972016-01-29 11:19:23 -0800193MethodCall::MethodCall(const Type* t, const string& n) : clazz(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800194
195MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
Christopher Wileyae589972016-01-29 11:19:23 -0800196 : obj(o), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800197 va_list args;
198 va_start(args, argc);
199 init(argc, args);
200 va_end(args);
201}
202
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700203MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
Christopher Wileyae589972016-01-29 11:19:23 -0800204 : clazz(t), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800205 va_list args;
206 va_start(args, argc);
207 init(argc, args);
208 va_end(args);
209}
210
Christopher Wileyae589972016-01-29 11:19:23 -0800211void MethodCall::init(int n, va_list args) {
212 for (int i = 0; i < n; i++) {
213 Expression* expression = (Expression*)va_arg(args, void*);
214 this->arguments.push_back(expression);
215 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216}
217
Christopher Wileyae589972016-01-29 11:19:23 -0800218void MethodCall::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700219 if (this->obj != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800220 this->obj->Write(to);
221 to->Write(".");
Yi Kong894d6ba2018-07-24 11:27:38 -0700222 } else if (this->clazz != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800223 to->Write("%s.", this->clazz->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800224 }
225 to->Write("%s(", this->name.c_str());
226 WriteArgumentList(to, this->arguments);
227 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800228}
229
230Comparison::Comparison(Expression* l, const string& o, Expression* r)
Christopher Wileyae589972016-01-29 11:19:23 -0800231 : lvalue(l), op(o), rvalue(r) {}
232
233void Comparison::Write(CodeWriter* to) const {
234 to->Write("(");
235 this->lvalue->Write(to);
236 to->Write("%s", this->op.c_str());
237 this->rvalue->Write(to);
238 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239}
240
Christopher Wileyae589972016-01-29 11:19:23 -0800241NewExpression::NewExpression(const Type* t) : type(t) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800242
Christopher Wileyae589972016-01-29 11:19:23 -0800243NewExpression::NewExpression(const Type* t, int argc = 0, ...) : type(t) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800244 va_list args;
245 va_start(args, argc);
246 init(argc, args);
247 va_end(args);
248}
249
Christopher Wileyae589972016-01-29 11:19:23 -0800250void NewExpression::init(int n, va_list args) {
251 for (int i = 0; i < n; i++) {
252 Expression* expression = (Expression*)va_arg(args, void*);
253 this->arguments.push_back(expression);
254 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255}
256
Christopher Wileyae589972016-01-29 11:19:23 -0800257void NewExpression::Write(CodeWriter* to) const {
258 to->Write("new %s(", this->type->InstantiableName().c_str());
259 WriteArgumentList(to, this->arguments);
260 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800261}
262
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700263NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
Christopher Wileyae589972016-01-29 11:19:23 -0800264 : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265
Christopher Wileyae589972016-01-29 11:19:23 -0800266void NewArrayExpression::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800267 to->Write("new %s[", this->type->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800268 size->Write(to);
269 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800270}
271
Adam Lesinskiffa16862014-01-23 18:17:42 -0800272Ternary::Ternary(Expression* a, Expression* b, Expression* c)
Christopher Wileyae589972016-01-29 11:19:23 -0800273 : condition(a), ifpart(b), elsepart(c) {}
274
275void Ternary::Write(CodeWriter* to) const {
276 to->Write("((");
277 this->condition->Write(to);
278 to->Write(")?(");
279 this->ifpart->Write(to);
280 to->Write("):(");
281 this->elsepart->Write(to);
282 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800283}
284
Christopher Wileyae589972016-01-29 11:19:23 -0800285Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {}
286
287void Cast::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800288 to->Write("((%s)", this->type->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800289 expression->Write(to);
290 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800291}
292
Christopher Wileyae589972016-01-29 11:19:23 -0800293VariableDeclaration::VariableDeclaration(Variable* l, Expression* r,
294 const Type* c)
295 : lvalue(l), cast(c), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296
Christopher Wileyae589972016-01-29 11:19:23 -0800297VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800298
Christopher Wileyae589972016-01-29 11:19:23 -0800299void VariableDeclaration::Write(CodeWriter* to) const {
300 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700301 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800302 to->Write(" = ");
Yi Kong894d6ba2018-07-24 11:27:38 -0700303 if (this->cast != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800304 to->Write("(%s)", this->cast->JavaType().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305 }
Christopher Wileyae589972016-01-29 11:19:23 -0800306 this->rvalue->Write(to);
307 }
308 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800309}
310
Christopher Wileyae589972016-01-29 11:19:23 -0800311void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700312 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800313 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800314 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800315 to->Write(") ");
316 }
317 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700318 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800319 to->Write("else ");
320 this->elseif->Write(to);
321 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800322}
323
Christopher Wileyae589972016-01-29 11:19:23 -0800324ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
325
326void ReturnStatement::Write(CodeWriter* to) const {
327 to->Write("return ");
328 this->expression->Write(to);
329 to->Write(";\n");
330}
331
332void TryStatement::Write(CodeWriter* to) const {
333 to->Write("try ");
334 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800335}
336
337CatchStatement::CatchStatement(Variable* e)
Christopher Wileyae589972016-01-29 11:19:23 -0800338 : statements(new StatementBlock), exception(e) {}
339
340void CatchStatement::Write(CodeWriter* to) const {
341 to->Write("catch ");
Yi Kong894d6ba2018-07-24 11:27:38 -0700342 if (this->exception != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800343 to->Write("(");
344 this->exception->WriteDeclaration(to);
345 to->Write(") ");
346 }
347 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348}
349
Christopher Wileyae589972016-01-29 11:19:23 -0800350void FinallyStatement::Write(CodeWriter* to) const {
351 to->Write("finally ");
352 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353}
354
Christopher Wileyae589972016-01-29 11:19:23 -0800355Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800356
Christopher Wileyae589972016-01-29 11:19:23 -0800357void Case::Write(CodeWriter* to) const {
358 int N = this->cases.size();
359 if (N > 0) {
360 for (int i = 0; i < N; i++) {
361 string s = this->cases[i];
362 if (s.length() != 0) {
363 to->Write("case %s:\n", s.c_str());
364 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700365 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800366 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367 }
Christopher Wileyae589972016-01-29 11:19:23 -0800368 } else {
369 to->Write("default:\n");
370 }
371 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800372}
373
Christopher Wileyae589972016-01-29 11:19:23 -0800374SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
375
376void SwitchStatement::Write(CodeWriter* to) const {
377 to->Write("switch (");
378 this->expression->Write(to);
379 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900380 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800381 int N = this->cases.size();
382 for (int i = 0; i < N; i++) {
383 this->cases[i]->Write(to);
384 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900385 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800386 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387}
388
Christopher Wileyae589972016-01-29 11:19:23 -0800389void Break::Write(CodeWriter* to) const { to->Write("break;\n"); }
390
391void Method::Write(CodeWriter* to) const {
392 size_t N, i;
393
394 if (this->comment.length() != 0) {
395 to->Write("%s\n", this->comment.c_str());
396 }
397
Jiyong Parka6605ab2018-11-11 14:30:21 +0900398 for (const auto& a : this->annotations) {
399 to->Write("%s\n", a.c_str());
400 }
401
Christopher Wileyae589972016-01-29 11:19:23 -0800402 WriteModifiers(to, this->modifiers,
403 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
404
Yi Kong894d6ba2018-07-24 11:27:38 -0700405 if (this->returnType != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800406 string dim;
407 for (i = 0; i < this->returnTypeDimension; i++) {
408 dim += "[]";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800409 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800410 to->Write("%s%s ", this->returnType->JavaType().c_str(), dim.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800411 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800412
Christopher Wileyae589972016-01-29 11:19:23 -0800413 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800414
Christopher Wileyae589972016-01-29 11:19:23 -0800415 N = this->parameters.size();
416 for (i = 0; i < N; i++) {
417 this->parameters[i]->WriteDeclaration(to);
418 if (i != N - 1) {
419 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800420 }
Christopher Wileyae589972016-01-29 11:19:23 -0800421 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800422
Christopher Wileyae589972016-01-29 11:19:23 -0800423 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800424
Christopher Wileyae589972016-01-29 11:19:23 -0800425 N = this->exceptions.size();
426 for (i = 0; i < N; i++) {
427 if (i == 0) {
428 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800429 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800430 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800431 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800432 to->Write("%s", this->exceptions[i]->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800433 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800434
Yi Kong894d6ba2018-07-24 11:27:38 -0700435 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800436 to->Write(";\n");
437 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700438 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800439 this->statements->Write(to);
440 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800441}
442
Steven Moreland5557f1c2018-07-02 13:50:23 -0700443void LiteralClassElement::Write(CodeWriter* to) const {
444 to->Write("%s", element.c_str());
445}
446
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700447void IntConstant::Write(CodeWriter* to) const {
Christopher Wileyae589972016-01-29 11:19:23 -0800448 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
Steven Moreland693640b2018-07-19 13:46:27 -0700449 to->Write("int %s = %s;\n", name.c_str(), value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800450}
451
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700452void StringConstant::Write(CodeWriter* to) const {
453 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
454 to->Write("String %s = %s;\n", name.c_str(), value.c_str());
455}
456
Christopher Wileyae589972016-01-29 11:19:23 -0800457void Class::Write(CodeWriter* to) const {
458 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800459
Christopher Wileyae589972016-01-29 11:19:23 -0800460 if (this->comment.length() != 0) {
461 to->Write("%s\n", this->comment.c_str());
462 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900463 for (const auto& a : this->annotations) {
464 to->Write("%s\n", a.c_str());
465 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800466
Christopher Wileyae589972016-01-29 11:19:23 -0800467 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
468
469 if (this->what == Class::CLASS) {
470 to->Write("class ");
471 } else {
472 to->Write("interface ");
473 }
474
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800475 string name = this->type->JavaType();
Christopher Wileyae589972016-01-29 11:19:23 -0800476 size_t pos = name.rfind('.');
477 if (pos != string::npos) {
478 name = name.c_str() + pos + 1;
479 }
480
481 to->Write("%s", name.c_str());
482
Yi Kong894d6ba2018-07-24 11:27:38 -0700483 if (this->extends != nullptr) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800484 to->Write(" extends %s", this->extends->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800485 }
486
487 N = this->interfaces.size();
488 if (N != 0) {
489 if (this->what == Class::CLASS) {
490 to->Write(" implements");
491 } else {
492 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800493 }
Christopher Wileyae589972016-01-29 11:19:23 -0800494 for (i = 0; i < N; i++) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800495 to->Write(" %s", this->interfaces[i]->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800496 }
497 }
498
499 to->Write("\n");
500 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900501 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800502
503 N = this->elements.size();
504 for (i = 0; i < N; i++) {
505 this->elements[i]->Write(to);
506 }
507
Jiyong Parka755dc72018-06-29 13:52:24 +0900508 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800509 to->Write("}\n");
510}
511
512static string escape_backslashes(const string& str) {
513 string result;
514 const size_t I = str.length();
515 for (size_t i = 0; i < I; i++) {
516 char c = str[i];
517 if (c == '\\') {
518 result += "\\\\";
519 } else {
520 result += c;
521 }
522 }
523 return result;
524}
525
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800526Document::Document(const std::string& comment,
527 const std::string& package,
528 const std::string& original_src,
529 std::unique_ptr<Class> clazz)
530 : comment_(comment),
531 package_(package),
532 original_src_(original_src),
533 clazz_(std::move(clazz)) {
534}
Christopher Wileyae589972016-01-29 11:19:23 -0800535
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800536void Document::Write(CodeWriter* to) const {
537 if (!comment_.empty()) {
538 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800539 }
540 to->Write(
541 "/*\n"
542 " * This file is auto-generated. DO NOT MODIFY.\n"
543 " * Original file: %s\n"
544 " */\n",
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800545 escape_backslashes(original_src_).c_str());
546 if (!package_.empty()) {
547 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800548 }
549
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800550 if (clazz_) {
551 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800552 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800553}
554
Christopher Wileydb154a52015-09-28 16:32:25 -0700555} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700556} // namespace aidl
557} // namespace android