blob: 1ba723c8d4c16f2d7b62cc001fbddca215c24e51 [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
Christopher Wileyae589972016-01-29 11:19:23 -080029void WriteModifiers(CodeWriter* to, int mod, int mask) {
30 int m = mod & mask;
Adam Lesinskiffa16862014-01-23 18:17:42 -080031
Christopher Wileyae589972016-01-29 11:19:23 -080032 if (m & OVERRIDE) {
33 to->Write("@Override ");
34 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080035
Christopher Wileyae589972016-01-29 11:19:23 -080036 if ((m & SCOPE_MASK) == PUBLIC) {
37 to->Write("public ");
38 } else if ((m & SCOPE_MASK) == PRIVATE) {
39 to->Write("private ");
40 } else if ((m & SCOPE_MASK) == PROTECTED) {
41 to->Write("protected ");
42 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080043
Christopher Wileyae589972016-01-29 11:19:23 -080044 if (m & STATIC) {
45 to->Write("static ");
46 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080047
Christopher Wileyae589972016-01-29 11:19:23 -080048 if (m & FINAL) {
49 to->Write("final ");
50 }
51
52 if (m & ABSTRACT) {
53 to->Write("abstract ");
54 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080055}
56
Christopher Wileyae589972016-01-29 11:19:23 -080057void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) {
58 size_t N = arguments.size();
59 for (size_t i = 0; i < N; i++) {
60 arguments[i]->Write(to);
61 if (i != N - 1) {
62 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080063 }
Christopher Wileyae589972016-01-29 11:19:23 -080064 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080065}
66
Christopher Wileyae589972016-01-29 11:19:23 -080067Field::Field(int m, Variable* v) : ClassElement(), modifiers(m), variable(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080068
Christopher Wileyae589972016-01-29 11:19:23 -080069void Field::Write(CodeWriter* to) const {
70 if (this->comment.length() != 0) {
71 to->Write("%s\n", this->comment.c_str());
72 }
73 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Christopher Wileyd21bfee2016-01-29 15:11:38 -080074 to->Write("%s %s", this->variable->type->JavaType().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -080075 this->variable->name.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -080076 if (this->value.length() != 0) {
77 to->Write(" = %s", this->value.c_str());
78 }
79 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080080}
81
Christopher Wileyae589972016-01-29 11:19:23 -080082LiteralExpression::LiteralExpression(const string& v) : value(v) {}
83
84void LiteralExpression::Write(CodeWriter* to) const {
85 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080086}
87
Christopher Wileyae589972016-01-29 11:19:23 -080088StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080089
Christopher Wileyae589972016-01-29 11:19:23 -080090void StringLiteralExpression::Write(CodeWriter* to) const {
91 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080092}
93
Christopher Wiley8f6816e2015-09-22 17:03:47 -070094Variable::Variable(const Type* t, const string& n)
Christopher Wileyae589972016-01-29 11:19:23 -080095 : type(t), name(n), dimension(0) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080096
Christopher Wiley8f6816e2015-09-22 17:03:47 -070097Variable::Variable(const Type* t, const string& n, int d)
Christopher Wileyae589972016-01-29 11:19:23 -080098 : type(t), name(n), dimension(d) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080099
Christopher Wileyae589972016-01-29 11:19:23 -0800100void Variable::WriteDeclaration(CodeWriter* to) const {
101 string dim;
102 for (int i = 0; i < this->dimension; i++) {
103 dim += "[]";
104 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800105 to->Write("%s%s %s", this->type->JavaType().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106 this->name.c_str());
107}
108
Christopher Wileyae589972016-01-29 11:19:23 -0800109void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110
111FieldVariable::FieldVariable(Expression* o, const string& n)
Christopher Wileyae589972016-01-29 11:19:23 -0800112 : object(o), clazz(NULL), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800113
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700114FieldVariable::FieldVariable(const Type* c, const string& n)
Christopher Wileyae589972016-01-29 11:19:23 -0800115 : object(NULL), clazz(c), name(n) {}
116
117void FieldVariable::Write(CodeWriter* to) const {
118 if (this->object != NULL) {
119 this->object->Write(to);
120 } else if (this->clazz != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800121 to->Write("%s", this->clazz->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800122 }
123 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800124}
125
Christopher Wileyae589972016-01-29 11:19:23 -0800126void StatementBlock::Write(CodeWriter* to) const {
127 to->Write("{\n");
128 int N = this->statements.size();
129 for (int i = 0; i < N; i++) {
130 this->statements[i]->Write(to);
131 }
132 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800133}
134
Christopher Wileyae589972016-01-29 11:19:23 -0800135void StatementBlock::Add(Statement* statement) {
136 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800137}
138
Christopher Wileyae589972016-01-29 11:19:23 -0800139void StatementBlock::Add(Expression* expression) {
140 this->statements.push_back(new ExpressionStatement(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800141}
142
Christopher Wileyae589972016-01-29 11:19:23 -0800143ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800144
Christopher Wileyae589972016-01-29 11:19:23 -0800145void ExpressionStatement::Write(CodeWriter* to) const {
146 this->expression->Write(to);
147 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148}
149
150Assignment::Assignment(Variable* l, Expression* r)
Christopher Wileyae589972016-01-29 11:19:23 -0800151 : lvalue(l), rvalue(r), cast(NULL) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700153Assignment::Assignment(Variable* l, Expression* r, const Type* c)
Christopher Wileyae589972016-01-29 11:19:23 -0800154 : lvalue(l), rvalue(r), cast(c) {}
155
156void Assignment::Write(CodeWriter* to) const {
157 this->lvalue->Write(to);
158 to->Write(" = ");
159 if (this->cast != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800160 to->Write("(%s)", this->cast->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800161 }
162 this->rvalue->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800163}
164
Christopher Wileyae589972016-01-29 11:19:23 -0800165MethodCall::MethodCall(const string& n) : name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800166
Christopher Wileyae589972016-01-29 11:19:23 -0800167MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800168 va_list args;
169 va_start(args, argc);
170 init(argc, args);
171 va_end(args);
172}
173
Christopher Wileyae589972016-01-29 11:19:23 -0800174MethodCall::MethodCall(Expression* o, const string& n) : obj(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800175
Christopher Wileyae589972016-01-29 11:19:23 -0800176MethodCall::MethodCall(const Type* t, const string& n) : clazz(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800177
178MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
Christopher Wileyae589972016-01-29 11:19:23 -0800179 : obj(o), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800180 va_list args;
181 va_start(args, argc);
182 init(argc, args);
183 va_end(args);
184}
185
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700186MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
Christopher Wileyae589972016-01-29 11:19:23 -0800187 : clazz(t), name(n) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188 va_list args;
189 va_start(args, argc);
190 init(argc, args);
191 va_end(args);
192}
193
Christopher Wileyae589972016-01-29 11:19:23 -0800194void MethodCall::init(int n, va_list args) {
195 for (int i = 0; i < n; i++) {
196 Expression* expression = (Expression*)va_arg(args, void*);
197 this->arguments.push_back(expression);
198 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800199}
200
Christopher Wileyae589972016-01-29 11:19:23 -0800201void MethodCall::Write(CodeWriter* to) const {
202 if (this->obj != NULL) {
203 this->obj->Write(to);
204 to->Write(".");
205 } else if (this->clazz != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800206 to->Write("%s.", this->clazz->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800207 }
208 to->Write("%s(", this->name.c_str());
209 WriteArgumentList(to, this->arguments);
210 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800211}
212
213Comparison::Comparison(Expression* l, const string& o, Expression* r)
Christopher Wileyae589972016-01-29 11:19:23 -0800214 : lvalue(l), op(o), rvalue(r) {}
215
216void Comparison::Write(CodeWriter* to) const {
217 to->Write("(");
218 this->lvalue->Write(to);
219 to->Write("%s", this->op.c_str());
220 this->rvalue->Write(to);
221 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222}
223
Christopher Wileyae589972016-01-29 11:19:23 -0800224NewExpression::NewExpression(const Type* t) : type(t) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225
Christopher Wileyae589972016-01-29 11:19:23 -0800226NewExpression::NewExpression(const Type* t, int argc = 0, ...) : type(t) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800227 va_list args;
228 va_start(args, argc);
229 init(argc, args);
230 va_end(args);
231}
232
Christopher Wileyae589972016-01-29 11:19:23 -0800233void NewExpression::init(int n, va_list args) {
234 for (int i = 0; i < n; i++) {
235 Expression* expression = (Expression*)va_arg(args, void*);
236 this->arguments.push_back(expression);
237 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800238}
239
Christopher Wileyae589972016-01-29 11:19:23 -0800240void NewExpression::Write(CodeWriter* to) const {
241 to->Write("new %s(", this->type->InstantiableName().c_str());
242 WriteArgumentList(to, this->arguments);
243 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800244}
245
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700246NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
Christopher Wileyae589972016-01-29 11:19:23 -0800247 : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800248
Christopher Wileyae589972016-01-29 11:19:23 -0800249void NewArrayExpression::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800250 to->Write("new %s[", this->type->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800251 size->Write(to);
252 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253}
254
Adam Lesinskiffa16862014-01-23 18:17:42 -0800255Ternary::Ternary(Expression* a, Expression* b, Expression* c)
Christopher Wileyae589972016-01-29 11:19:23 -0800256 : condition(a), ifpart(b), elsepart(c) {}
257
258void Ternary::Write(CodeWriter* to) const {
259 to->Write("((");
260 this->condition->Write(to);
261 to->Write(")?(");
262 this->ifpart->Write(to);
263 to->Write("):(");
264 this->elsepart->Write(to);
265 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800266}
267
Christopher Wileyae589972016-01-29 11:19:23 -0800268Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {}
269
270void Cast::Write(CodeWriter* to) const {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800271 to->Write("((%s)", this->type->JavaType().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
Christopher Wileyae589972016-01-29 11:19:23 -0800276VariableDeclaration::VariableDeclaration(Variable* l, Expression* r,
277 const Type* c)
278 : lvalue(l), cast(c), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800279
Christopher Wileyae589972016-01-29 11:19:23 -0800280VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281
Christopher Wileyae589972016-01-29 11:19:23 -0800282void VariableDeclaration::Write(CodeWriter* to) const {
283 this->lvalue->WriteDeclaration(to);
284 if (this->rvalue != NULL) {
285 to->Write(" = ");
286 if (this->cast != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800287 to->Write("(%s)", this->cast->JavaType().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800288 }
Christopher Wileyae589972016-01-29 11:19:23 -0800289 this->rvalue->Write(to);
290 }
291 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800292}
293
Christopher Wileyae589972016-01-29 11:19:23 -0800294void IfStatement::Write(CodeWriter* to) const {
295 if (this->expression != NULL) {
296 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800297 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800298 to->Write(") ");
299 }
300 this->statements->Write(to);
301 if (this->elseif != NULL) {
302 to->Write("else ");
303 this->elseif->Write(to);
304 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305}
306
Christopher Wileyae589972016-01-29 11:19:23 -0800307ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
308
309void ReturnStatement::Write(CodeWriter* to) const {
310 to->Write("return ");
311 this->expression->Write(to);
312 to->Write(";\n");
313}
314
315void TryStatement::Write(CodeWriter* to) const {
316 to->Write("try ");
317 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800318}
319
320CatchStatement::CatchStatement(Variable* e)
Christopher Wileyae589972016-01-29 11:19:23 -0800321 : statements(new StatementBlock), exception(e) {}
322
323void CatchStatement::Write(CodeWriter* to) const {
324 to->Write("catch ");
325 if (this->exception != NULL) {
326 to->Write("(");
327 this->exception->WriteDeclaration(to);
328 to->Write(") ");
329 }
330 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800331}
332
Christopher Wileyae589972016-01-29 11:19:23 -0800333void FinallyStatement::Write(CodeWriter* to) const {
334 to->Write("finally ");
335 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336}
337
Christopher Wileyae589972016-01-29 11:19:23 -0800338Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800339
Christopher Wileyae589972016-01-29 11:19:23 -0800340void Case::Write(CodeWriter* to) const {
341 int N = this->cases.size();
342 if (N > 0) {
343 for (int i = 0; i < N; i++) {
344 string s = this->cases[i];
345 if (s.length() != 0) {
346 to->Write("case %s:\n", s.c_str());
347 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700348 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800349 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350 }
Christopher Wileyae589972016-01-29 11:19:23 -0800351 } else {
352 to->Write("default:\n");
353 }
354 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355}
356
Christopher Wileyae589972016-01-29 11:19:23 -0800357SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
358
359void SwitchStatement::Write(CodeWriter* to) const {
360 to->Write("switch (");
361 this->expression->Write(to);
362 to->Write(")\n{\n");
363 int N = this->cases.size();
364 for (int i = 0; i < N; i++) {
365 this->cases[i]->Write(to);
366 }
367 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800368}
369
Christopher Wileyae589972016-01-29 11:19:23 -0800370void Break::Write(CodeWriter* to) const { to->Write("break;\n"); }
371
372void Method::Write(CodeWriter* to) const {
373 size_t N, i;
374
375 if (this->comment.length() != 0) {
376 to->Write("%s\n", this->comment.c_str());
377 }
378
379 WriteModifiers(to, this->modifiers,
380 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
381
382 if (this->returnType != NULL) {
383 string dim;
384 for (i = 0; i < this->returnTypeDimension; i++) {
385 dim += "[]";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800386 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800387 to->Write("%s%s ", this->returnType->JavaType().c_str(), dim.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800388 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389
Christopher Wileyae589972016-01-29 11:19:23 -0800390 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800391
Christopher Wileyae589972016-01-29 11:19:23 -0800392 N = this->parameters.size();
393 for (i = 0; i < N; i++) {
394 this->parameters[i]->WriteDeclaration(to);
395 if (i != N - 1) {
396 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800397 }
Christopher Wileyae589972016-01-29 11:19:23 -0800398 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800399
Christopher Wileyae589972016-01-29 11:19:23 -0800400 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800401
Christopher Wileyae589972016-01-29 11:19:23 -0800402 N = this->exceptions.size();
403 for (i = 0; i < N; i++) {
404 if (i == 0) {
405 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800406 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800407 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800408 }
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800409 to->Write("%s", this->exceptions[i]->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800410 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800411
Christopher Wileyae589972016-01-29 11:19:23 -0800412 if (this->statements == NULL) {
413 to->Write(";\n");
414 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700415 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800416 this->statements->Write(to);
417 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800418}
419
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700420void IntConstant::Write(CodeWriter* to) const {
Christopher Wileyae589972016-01-29 11:19:23 -0800421 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
422 to->Write("int %s = %d;\n", name.c_str(), value);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800423}
424
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700425void StringConstant::Write(CodeWriter* to) const {
426 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
427 to->Write("String %s = %s;\n", name.c_str(), value.c_str());
428}
429
Christopher Wileyae589972016-01-29 11:19:23 -0800430void Class::Write(CodeWriter* to) const {
431 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800432
Christopher Wileyae589972016-01-29 11:19:23 -0800433 if (this->comment.length() != 0) {
434 to->Write("%s\n", this->comment.c_str());
435 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800436
Christopher Wileyae589972016-01-29 11:19:23 -0800437 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
438
439 if (this->what == Class::CLASS) {
440 to->Write("class ");
441 } else {
442 to->Write("interface ");
443 }
444
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800445 string name = this->type->JavaType();
Christopher Wileyae589972016-01-29 11:19:23 -0800446 size_t pos = name.rfind('.');
447 if (pos != string::npos) {
448 name = name.c_str() + pos + 1;
449 }
450
451 to->Write("%s", name.c_str());
452
453 if (this->extends != NULL) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800454 to->Write(" extends %s", this->extends->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800455 }
456
457 N = this->interfaces.size();
458 if (N != 0) {
459 if (this->what == Class::CLASS) {
460 to->Write(" implements");
461 } else {
462 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800463 }
Christopher Wileyae589972016-01-29 11:19:23 -0800464 for (i = 0; i < N; i++) {
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800465 to->Write(" %s", this->interfaces[i]->JavaType().c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800466 }
467 }
468
469 to->Write("\n");
470 to->Write("{\n");
471
472 N = this->elements.size();
473 for (i = 0; i < N; i++) {
474 this->elements[i]->Write(to);
475 }
476
477 to->Write("}\n");
478}
479
480static string escape_backslashes(const string& str) {
481 string result;
482 const size_t I = str.length();
483 for (size_t i = 0; i < I; i++) {
484 char c = str[i];
485 if (c == '\\') {
486 result += "\\\\";
487 } else {
488 result += c;
489 }
490 }
491 return result;
492}
493
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800494Document::Document(const std::string& comment,
495 const std::string& package,
496 const std::string& original_src,
497 std::unique_ptr<Class> clazz)
498 : comment_(comment),
499 package_(package),
500 original_src_(original_src),
501 clazz_(std::move(clazz)) {
502}
Christopher Wileyae589972016-01-29 11:19:23 -0800503
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800504void Document::Write(CodeWriter* to) const {
505 if (!comment_.empty()) {
506 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800507 }
508 to->Write(
509 "/*\n"
510 " * This file is auto-generated. DO NOT MODIFY.\n"
511 " * Original file: %s\n"
512 " */\n",
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800513 escape_backslashes(original_src_).c_str());
514 if (!package_.empty()) {
515 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800516 }
517
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800518 if (clazz_) {
519 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800520 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800521}
522
Christopher Wileydb154a52015-09-28 16:32:25 -0700523} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700524} // namespace aidl
525} // namespace android