blob: c347d303a88bdacf4b04bd7072542adbf65d1e7b [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
22namespace android {
23namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070024namespace java {
Adam Lesinskiffa16862014-01-23 18:17:42 -080025
26void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070027WriteModifiers(CodeWriter* to, int mod, int mask)
Adam Lesinskiffa16862014-01-23 18:17:42 -080028{
29 int m = mod & mask;
30
31 if (m & OVERRIDE) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070032 to->Write("@Override ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080033 }
34
35 if ((m & SCOPE_MASK) == PUBLIC) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070036 to->Write("public ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080037 }
38 else if ((m & SCOPE_MASK) == PRIVATE) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070039 to->Write("private ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080040 }
41 else if ((m & SCOPE_MASK) == PROTECTED) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070042 to->Write("protected ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080043 }
44
45 if (m & STATIC) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070046 to->Write("static ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080047 }
48
49 if (m & FINAL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070050 to->Write("final ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080051 }
52
53 if (m & ABSTRACT) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070054 to->Write("abstract ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080055 }
56}
57
58void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070059WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments)
Adam Lesinskiffa16862014-01-23 18:17:42 -080060{
61 size_t N = arguments.size();
62 for (size_t i=0; i<N; i++) {
63 arguments[i]->Write(to);
64 if (i != N-1) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070065 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080066 }
67 }
68}
69
Adam Lesinskiffa16862014-01-23 18:17:42 -080070Field::Field(int m, Variable* v)
71 :ClassElement(),
72 modifiers(m),
73 variable(v)
74{
75}
76
Adam Lesinskiffa16862014-01-23 18:17:42 -080077void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070078Field::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -080079{
80 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070081 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080082 }
83 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070084 to->Write("%s %s", this->variable->type->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -080085 this->variable->name.c_str());
86 if (this->value.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070087 to->Write(" = %s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080088 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070089 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080090}
91
Adam Lesinskiffa16862014-01-23 18:17:42 -080092LiteralExpression::LiteralExpression(const string& v)
93 :value(v)
94{
95}
96
Adam Lesinskiffa16862014-01-23 18:17:42 -080097void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070098LiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -080099{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700100 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800101}
102
103StringLiteralExpression::StringLiteralExpression(const string& v)
104 :value(v)
105{
106}
107
Adam Lesinskiffa16862014-01-23 18:17:42 -0800108void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700109StringLiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700111 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112}
113
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700114Variable::Variable(const Type* t, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115 :type(t),
116 name(n),
117 dimension(0)
118{
119}
120
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700121Variable::Variable(const Type* t, const string& n, int d)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122 :type(t),
123 name(n),
124 dimension(d)
125{
126}
127
Adam Lesinskiffa16862014-01-23 18:17:42 -0800128void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700129Variable::WriteDeclaration(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800130{
131 string dim;
132 for (int i=0; i<this->dimension; i++) {
133 dim += "[]";
134 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700135 to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800136 this->name.c_str());
137}
138
139void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700140Variable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800141{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700142 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800143}
144
145FieldVariable::FieldVariable(Expression* o, const string& n)
146 :object(o),
147 clazz(NULL),
148 name(n)
149{
150}
151
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700152FieldVariable::FieldVariable(const Type* c, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153 :object(NULL),
154 clazz(c),
155 name(n)
156{
157}
158
Adam Lesinskiffa16862014-01-23 18:17:42 -0800159void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700160FieldVariable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800161{
162 if (this->object != NULL) {
163 this->object->Write(to);
164 }
165 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700166 to->Write("%s", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800167 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700168 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800169}
170
Adam Lesinskiffa16862014-01-23 18:17:42 -0800171void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700172StatementBlock::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800173{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700174 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800175 int N = this->statements.size();
176 for (int i=0; i<N; i++) {
177 this->statements[i]->Write(to);
178 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700179 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800180}
181
182void
183StatementBlock::Add(Statement* statement)
184{
185 this->statements.push_back(statement);
186}
187
188void
189StatementBlock::Add(Expression* expression)
190{
191 this->statements.push_back(new ExpressionStatement(expression));
192}
193
194ExpressionStatement::ExpressionStatement(Expression* e)
195 :expression(e)
196{
197}
198
Adam Lesinskiffa16862014-01-23 18:17:42 -0800199void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700200ExpressionStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800201{
202 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700203 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800204}
205
206Assignment::Assignment(Variable* l, Expression* r)
207 :lvalue(l),
208 rvalue(r),
209 cast(NULL)
210{
211}
212
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700213Assignment::Assignment(Variable* l, Expression* r, const Type* c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800214 :lvalue(l),
215 rvalue(r),
216 cast(c)
217{
218}
219
Adam Lesinskiffa16862014-01-23 18:17:42 -0800220void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700221Assignment::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222{
223 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700224 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700226 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800227 }
228 this->rvalue->Write(to);
229}
230
231MethodCall::MethodCall(const string& n)
Christopher Wileydf03d972015-09-29 11:10:47 -0700232 : name(n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233{
234}
235
236MethodCall::MethodCall(const string& n, int argc = 0, ...)
Christopher Wileydf03d972015-09-29 11:10:47 -0700237 :name(n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800238{
239 va_list args;
240 va_start(args, argc);
241 init(argc, args);
242 va_end(args);
243}
244
245MethodCall::MethodCall(Expression* o, const string& n)
246 :obj(o),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247 name(n)
248{
249}
250
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700251MethodCall::MethodCall(const Type* t, const string& n)
Christopher Wileydf03d972015-09-29 11:10:47 -0700252 :clazz(t),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253 name(n)
254{
255}
256
257MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
258 :obj(o),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259 name(n)
260{
261 va_list args;
262 va_start(args, argc);
263 init(argc, args);
264 va_end(args);
265}
266
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700267MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
Christopher Wileydf03d972015-09-29 11:10:47 -0700268 :clazz(t),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800269 name(n)
270{
271 va_list args;
272 va_start(args, argc);
273 init(argc, args);
274 va_end(args);
275}
276
Adam Lesinskiffa16862014-01-23 18:17:42 -0800277void
278MethodCall::init(int n, va_list args)
279{
280 for (int i=0; i<n; i++) {
281 Expression* expression = (Expression*)va_arg(args, void*);
282 this->arguments.push_back(expression);
283 }
284}
285
286void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700287MethodCall::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800288{
289 if (this->obj != NULL) {
290 this->obj->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700291 to->Write(".");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800292 }
293 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700294 to->Write("%s.", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800295 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700296 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800297 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700298 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299}
300
301Comparison::Comparison(Expression* l, const string& o, Expression* r)
302 :lvalue(l),
303 op(o),
304 rvalue(r)
305{
306}
307
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700309Comparison::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800310{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700311 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800312 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700313 to->Write("%s", this->op.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800314 this->rvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700315 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800316}
317
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700318NewExpression::NewExpression(const Type* t)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800319 :type(t)
320{
321}
322
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700323NewExpression::NewExpression(const Type* t, int argc = 0, ...)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324 :type(t)
325{
326 va_list args;
327 va_start(args, argc);
328 init(argc, args);
329 va_end(args);
330}
331
Adam Lesinskiffa16862014-01-23 18:17:42 -0800332void
333NewExpression::init(int n, va_list args)
334{
335 for (int i=0; i<n; i++) {
336 Expression* expression = (Expression*)va_arg(args, void*);
337 this->arguments.push_back(expression);
338 }
339}
340
341void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700342NewExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700344 to->Write("new %s(", this->type->InstantiableName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800345 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700346 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800347}
348
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700349NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350 :type(t),
351 size(s)
352{
353}
354
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700356NewArrayExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700358 to->Write("new %s[", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800359 size->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700360 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800361}
362
Adam Lesinskiffa16862014-01-23 18:17:42 -0800363Ternary::Ternary(Expression* a, Expression* b, Expression* c)
364 :condition(a),
365 ifpart(b),
366 elsepart(c)
367{
368}
369
Adam Lesinskiffa16862014-01-23 18:17:42 -0800370void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700371Ternary::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800372{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700373 to->Write("((");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800374 this->condition->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700375 to->Write(")?(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800376 this->ifpart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700377 to->Write("):(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800378 this->elsepart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700379 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800380}
381
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700382Cast::Cast(const Type* t, Expression* e)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383 :type(t),
384 expression(e)
385{
386}
387
Adam Lesinskiffa16862014-01-23 18:17:42 -0800388void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700389Cast::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800390{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700391 to->Write("((%s)", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800392 expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700393 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800394}
395
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700396VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, const Type* c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800397 :lvalue(l),
398 cast(c),
399 rvalue(r)
400{
401}
402
403VariableDeclaration::VariableDeclaration(Variable* l)
Christopher Wileydf03d972015-09-29 11:10:47 -0700404 :lvalue(l)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800405{
406}
407
408void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700409VariableDeclaration::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800410{
411 this->lvalue->WriteDeclaration(to);
412 if (this->rvalue != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700413 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800414 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700415 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800416 }
417 this->rvalue->Write(to);
418 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700419 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800420}
421
Adam Lesinskiffa16862014-01-23 18:17:42 -0800422void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700423IfStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800424{
425 if (this->expression != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700426 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800427 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700428 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800429 }
430 this->statements->Write(to);
431 if (this->elseif != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700432 to->Write("else ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800433 this->elseif->Write(to);
434 }
435}
436
437ReturnStatement::ReturnStatement(Expression* e)
438 :expression(e)
439{
440}
441
Adam Lesinskiffa16862014-01-23 18:17:42 -0800442void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700443ReturnStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800444{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700445 to->Write("return ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800446 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700447 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800448}
449
Adam Lesinskiffa16862014-01-23 18:17:42 -0800450void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700451TryStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800452{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700453 to->Write("try ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800454 this->statements->Write(to);
455}
456
457CatchStatement::CatchStatement(Variable* e)
458 :statements(new StatementBlock),
459 exception(e)
460{
461}
462
Adam Lesinskiffa16862014-01-23 18:17:42 -0800463void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700464CatchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800465{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700466 to->Write("catch ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800467 if (this->exception != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700468 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800469 this->exception->WriteDeclaration(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700470 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800471 }
472 this->statements->Write(to);
473}
474
Adam Lesinskiffa16862014-01-23 18:17:42 -0800475void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700476FinallyStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800477{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700478 to->Write("finally ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800479 this->statements->Write(to);
480}
481
Adam Lesinskiffa16862014-01-23 18:17:42 -0800482Case::Case(const string& c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800483{
484 cases.push_back(c);
485}
486
Adam Lesinskiffa16862014-01-23 18:17:42 -0800487void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700488Case::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800489{
490 int N = this->cases.size();
491 if (N > 0) {
492 for (int i=0; i<N; i++) {
493 string s = this->cases[i];
494 if (s.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700495 to->Write("case %s:\n", s.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800496 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700497 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800498 }
499 }
500 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700501 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800502 }
503 statements->Write(to);
504}
505
506SwitchStatement::SwitchStatement(Expression* e)
507 :expression(e)
508{
509}
510
Adam Lesinskiffa16862014-01-23 18:17:42 -0800511void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700512SwitchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800513{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700514 to->Write("switch (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800515 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700516 to->Write(")\n{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800517 int N = this->cases.size();
518 for (int i=0; i<N; i++) {
519 this->cases[i]->Write(to);
520 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700521 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800522}
523
Adam Lesinskiffa16862014-01-23 18:17:42 -0800524void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700525Break::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800526{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700527 to->Write("break;\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800528}
529
Adam Lesinskiffa16862014-01-23 18:17:42 -0800530void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700531Method::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800532{
533 size_t N, i;
534
535 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700536 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800537 }
538
539 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
540
541 if (this->returnType != NULL) {
542 string dim;
543 for (i=0; i<this->returnTypeDimension; i++) {
544 dim += "[]";
545 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700546 to->Write("%s%s ", this->returnType->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800547 dim.c_str());
548 }
549
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700550 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800551
552 N = this->parameters.size();
553 for (i=0; i<N; i++) {
554 this->parameters[i]->WriteDeclaration(to);
555 if (i != N-1) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700556 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800557 }
558 }
559
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700560 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800561
562 N = this->exceptions.size();
563 for (i=0; i<N; i++) {
564 if (i == 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700565 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800566 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700567 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800568 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700569 to->Write("%s", this->exceptions[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800570 }
571
572 if (this->statements == NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700573 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800574 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700575 to->Write("\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800576 this->statements->Write(to);
577 }
578}
579
Adam Lesinskiffa16862014-01-23 18:17:42 -0800580void
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800581Constant::Write(CodeWriter* to) const
582{
583 WriteModifiers(to, STATIC | FINAL | PUBLIC, ALL_MODIFIERS);
584 to->Write("int %s = %d;\n", name.c_str(), value);
585}
586
587void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700588Class::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800589{
590 size_t N, i;
591
592 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700593 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800594 }
595
596 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
597
598 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700599 to->Write("class ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800600 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700601 to->Write("interface ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800602 }
603
604 string name = this->type->Name();
605 size_t pos = name.rfind('.');
606 if (pos != string::npos) {
607 name = name.c_str() + pos + 1;
608 }
609
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700610 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800611
612 if (this->extends != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700613 to->Write(" extends %s", this->extends->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800614 }
615
616 N = this->interfaces.size();
617 if (N != 0) {
618 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700619 to->Write(" implements");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800620 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700621 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800622 }
623 for (i=0; i<N; i++) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700624 to->Write(" %s", this->interfaces[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800625 }
626 }
627
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700628 to->Write("\n");
629 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800630
631 N = this->elements.size();
632 for (i=0; i<N; i++) {
633 this->elements[i]->Write(to);
634 }
635
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700636 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800637
638}
639
Adam Lesinskiffa16862014-01-23 18:17:42 -0800640static string
641escape_backslashes(const string& str)
642{
643 string result;
644 const size_t I=str.length();
645 for (size_t i=0; i<I; i++) {
646 char c = str[i];
647 if (c == '\\') {
648 result += "\\\\";
649 } else {
650 result += c;
651 }
652 }
653 return result;
654}
655
656void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700657Document::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800658{
659 size_t N, i;
660
661 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700662 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800663 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700664 to->Write("/*\n"
Adam Lesinskiffa16862014-01-23 18:17:42 -0800665 " * This file is auto-generated. DO NOT MODIFY.\n"
666 " * Original file: %s\n"
667 " */\n", escape_backslashes(this->originalSrc).c_str());
668 if (this->package.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700669 to->Write("package %s;\n", this->package.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800670 }
671
672 N = this->classes.size();
673 for (i=0; i<N; i++) {
674 Class* c = this->classes[i];
675 c->Write(to);
676 }
677}
678
Christopher Wileydb154a52015-09-28 16:32:25 -0700679} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700680} // namespace aidl
681} // namespace android