blob: c97a9070e26db0adf5ccca9e43704f678c690d8b [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 }
79 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Steven Moreland5635a8a2018-07-03 11:28:20 -070080 this->variable->WriteDeclaration(to);
81
Christopher Wileyae589972016-01-29 11:19:23 -080082 if (this->value.length() != 0) {
83 to->Write(" = %s", this->value.c_str());
84 }
85 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080086}
87
Christopher Wileyae589972016-01-29 11:19:23 -080088LiteralExpression::LiteralExpression(const string& v) : value(v) {}
89
90void LiteralExpression::Write(CodeWriter* to) const {
91 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080092}
93
Christopher Wileyae589972016-01-29 11:19:23 -080094StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080095
Christopher Wileyae589972016-01-29 11:19:23 -080096void StringLiteralExpression::Write(CodeWriter* to) const {
97 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080098}
99
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700100Variable::Variable(const Type* t, const string& n)
Christopher Wileyae589972016-01-29 11:19:23 -0800101 : type(t), name(n), dimension(0) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800102
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700103Variable::Variable(const Type* t, const string& n, int d)
Christopher Wileyae589972016-01-29 11:19:23 -0800104 : type(t), name(n), dimension(d) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800105
Christopher Wileyae589972016-01-29 11:19:23 -0800106void Variable::WriteDeclaration(CodeWriter* to) const {
107 string dim;
108 for (int i = 0; i < this->dimension; i++) {
109 dim += "[]";
110 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800111 to->Write("%s%s %s", this->type->JavaType().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112 this->name.c_str());
113}
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)
Christopher Wileyae589972016-01-29 11:19:23 -0800118 : object(o), clazz(NULL), 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)
Christopher Wileyae589972016-01-29 11:19:23 -0800121 : object(NULL), clazz(c), name(n) {}
122
123void FieldVariable::Write(CodeWriter* to) const {
124 if (this->object != NULL) {
125 this->object->Write(to);
126 } else if (this->clazz != NULL) {
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)
Christopher Wileyae589972016-01-29 11:19:23 -0800165 : lvalue(l), rvalue(r), cast(NULL) {}
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(" = ");
173 if (this->cast != NULL) {
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 {
216 if (this->obj != NULL) {
217 this->obj->Write(to);
218 to->Write(".");
219 } else if (this->clazz != NULL) {
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
Adam Lesinskiffa16862014-01-23 18:17:42 -0800269Ternary::Ternary(Expression* a, Expression* b, Expression* c)
Christopher Wileyae589972016-01-29 11:19:23 -0800270 : condition(a), ifpart(b), elsepart(c) {}
271
272void Ternary::Write(CodeWriter* to) const {
273 to->Write("((");
274 this->condition->Write(to);
275 to->Write(")?(");
276 this->ifpart->Write(to);
277 to->Write("):(");
278 this->elsepart->Write(to);
279 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800280}
281
Christopher Wileyae589972016-01-29 11:19:23 -0800282Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {}
283
284void Cast::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800285 to->Write("((%s)", this->type->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800286 expression->Write(to);
287 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800288}
289
Christopher Wileyae589972016-01-29 11:19:23 -0800290VariableDeclaration::VariableDeclaration(Variable* l, Expression* r,
291 const Type* c)
292 : lvalue(l), cast(c), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800293
Christopher Wileyae589972016-01-29 11:19:23 -0800294VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800295
Christopher Wileyae589972016-01-29 11:19:23 -0800296void VariableDeclaration::Write(CodeWriter* to) const {
297 this->lvalue->WriteDeclaration(to);
298 if (this->rvalue != NULL) {
299 to->Write(" = ");
300 if (this->cast != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800301 to->Write("(%s)", this->cast->JavaType().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800302 }
Christopher Wileyae589972016-01-29 11:19:23 -0800303 this->rvalue->Write(to);
304 }
305 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306}
307
Christopher Wileyae589972016-01-29 11:19:23 -0800308void IfStatement::Write(CodeWriter* to) const {
309 if (this->expression != NULL) {
310 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800312 to->Write(") ");
313 }
314 this->statements->Write(to);
315 if (this->elseif != NULL) {
316 to->Write("else ");
317 this->elseif->Write(to);
318 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800319}
320
Christopher Wileyae589972016-01-29 11:19:23 -0800321ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
322
323void ReturnStatement::Write(CodeWriter* to) const {
324 to->Write("return ");
325 this->expression->Write(to);
326 to->Write(";\n");
327}
328
329void TryStatement::Write(CodeWriter* to) const {
330 to->Write("try ");
331 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800332}
333
334CatchStatement::CatchStatement(Variable* e)
Christopher Wileyae589972016-01-29 11:19:23 -0800335 : statements(new StatementBlock), exception(e) {}
336
337void CatchStatement::Write(CodeWriter* to) const {
338 to->Write("catch ");
339 if (this->exception != NULL) {
340 to->Write("(");
341 this->exception->WriteDeclaration(to);
342 to->Write(") ");
343 }
344 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800345}
346
Christopher Wileyae589972016-01-29 11:19:23 -0800347void FinallyStatement::Write(CodeWriter* to) const {
348 to->Write("finally ");
349 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350}
351
Christopher Wileyae589972016-01-29 11:19:23 -0800352Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353
Christopher Wileyae589972016-01-29 11:19:23 -0800354void Case::Write(CodeWriter* to) const {
355 int N = this->cases.size();
356 if (N > 0) {
357 for (int i = 0; i < N; i++) {
358 string s = this->cases[i];
359 if (s.length() != 0) {
360 to->Write("case %s:\n", s.c_str());
361 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700362 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800363 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800364 }
Christopher Wileyae589972016-01-29 11:19:23 -0800365 } else {
366 to->Write("default:\n");
367 }
368 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800369}
370
Christopher Wileyae589972016-01-29 11:19:23 -0800371SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
372
373void SwitchStatement::Write(CodeWriter* to) const {
374 to->Write("switch (");
375 this->expression->Write(to);
376 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900377 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800378 int N = this->cases.size();
379 for (int i = 0; i < N; i++) {
380 this->cases[i]->Write(to);
381 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900382 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800383 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800384}
385
Christopher Wileyae589972016-01-29 11:19:23 -0800386void Break::Write(CodeWriter* to) const { to->Write("break;\n"); }
387
388void Method::Write(CodeWriter* to) const {
389 size_t N, i;
390
391 if (this->comment.length() != 0) {
392 to->Write("%s\n", this->comment.c_str());
393 }
394
395 WriteModifiers(to, this->modifiers,
396 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
397
398 if (this->returnType != NULL) {
399 string dim;
400 for (i = 0; i < this->returnTypeDimension; i++) {
401 dim += "[]";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800402 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800403 to->Write("%s%s ", this->returnType->JavaType().c_str(), dim.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800404 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800405
Christopher Wileyae589972016-01-29 11:19:23 -0800406 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800407
Christopher Wileyae589972016-01-29 11:19:23 -0800408 N = this->parameters.size();
409 for (i = 0; i < N; i++) {
410 this->parameters[i]->WriteDeclaration(to);
411 if (i != N - 1) {
412 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800413 }
Christopher Wileyae589972016-01-29 11:19:23 -0800414 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800415
Christopher Wileyae589972016-01-29 11:19:23 -0800416 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800417
Christopher Wileyae589972016-01-29 11:19:23 -0800418 N = this->exceptions.size();
419 for (i = 0; i < N; i++) {
420 if (i == 0) {
421 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800422 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800423 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800424 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800425 to->Write("%s", this->exceptions[i]->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800426 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800427
Christopher Wileyae589972016-01-29 11:19:23 -0800428 if (this->statements == NULL) {
429 to->Write(";\n");
430 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700431 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800432 this->statements->Write(to);
433 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800434}
435
Steven Moreland5557f1c2018-07-02 13:50:23 -0700436void LiteralClassElement::Write(CodeWriter* to) const {
437 to->Write("%s", element.c_str());
438}
439
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700440void IntConstant::Write(CodeWriter* to) const {
Christopher Wileyae589972016-01-29 11:19:23 -0800441 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
442 to->Write("int %s = %d;\n", name.c_str(), value);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800443}
444
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700445void StringConstant::Write(CodeWriter* to) const {
446 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
447 to->Write("String %s = %s;\n", name.c_str(), value.c_str());
448}
449
Christopher Wileyae589972016-01-29 11:19:23 -0800450void Class::Write(CodeWriter* to) const {
451 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800452
Christopher Wileyae589972016-01-29 11:19:23 -0800453 if (this->comment.length() != 0) {
454 to->Write("%s\n", this->comment.c_str());
455 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800456
Christopher Wileyae589972016-01-29 11:19:23 -0800457 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
458
459 if (this->what == Class::CLASS) {
460 to->Write("class ");
461 } else {
462 to->Write("interface ");
463 }
464
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800465 string name = this->type->JavaType();
Christopher Wileyae589972016-01-29 11:19:23 -0800466 size_t pos = name.rfind('.');
467 if (pos != string::npos) {
468 name = name.c_str() + pos + 1;
469 }
470
471 to->Write("%s", name.c_str());
472
473 if (this->extends != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800474 to->Write(" extends %s", this->extends->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800475 }
476
477 N = this->interfaces.size();
478 if (N != 0) {
479 if (this->what == Class::CLASS) {
480 to->Write(" implements");
481 } else {
482 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800483 }
Christopher Wileyae589972016-01-29 11:19:23 -0800484 for (i = 0; i < N; i++) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800485 to->Write(" %s", this->interfaces[i]->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800486 }
487 }
488
489 to->Write("\n");
490 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900491 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800492
493 N = this->elements.size();
494 for (i = 0; i < N; i++) {
495 this->elements[i]->Write(to);
496 }
497
Jiyong Parka755dc72018-06-29 13:52:24 +0900498 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800499 to->Write("}\n");
500}
501
502static string escape_backslashes(const string& str) {
503 string result;
504 const size_t I = str.length();
505 for (size_t i = 0; i < I; i++) {
506 char c = str[i];
507 if (c == '\\') {
508 result += "\\\\";
509 } else {
510 result += c;
511 }
512 }
513 return result;
514}
515
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800516Document::Document(const std::string& comment,
517 const std::string& package,
518 const std::string& original_src,
519 std::unique_ptr<Class> clazz)
520 : comment_(comment),
521 package_(package),
522 original_src_(original_src),
523 clazz_(std::move(clazz)) {
524}
Christopher Wileyae589972016-01-29 11:19:23 -0800525
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800526void Document::Write(CodeWriter* to) const {
527 if (!comment_.empty()) {
528 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800529 }
530 to->Write(
531 "/*\n"
532 " * This file is auto-generated. DO NOT MODIFY.\n"
533 " * Original file: %s\n"
534 " */\n",
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800535 escape_backslashes(original_src_).c_str());
536 if (!package_.empty()) {
537 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800538 }
539
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800540 if (clazz_) {
541 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800542 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800543}
544
Christopher Wileydb154a52015-09-28 16:32:25 -0700545} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700546} // namespace aidl
547} // namespace android