blob: c80414cc8c39dcd82e63c50c5695e3debeaa09de [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
70ClassElement::ClassElement()
71{
72}
73
74ClassElement::~ClassElement()
75{
76}
77
78Field::Field()
79 :ClassElement(),
80 modifiers(0),
81 variable(NULL)
82{
83}
84
85Field::Field(int m, Variable* v)
86 :ClassElement(),
87 modifiers(m),
88 variable(v)
89{
90}
91
92Field::~Field()
93{
94}
95
96void
Christopher Wiley8f6816e2015-09-22 17:03:47 -070097Field::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -080098{
99 types->insert(this->variable->type);
100}
101
102void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700103Field::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104{
105 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700106 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800107 }
108 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700109 to->Write("%s %s", this->variable->type->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110 this->variable->name.c_str());
111 if (this->value.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700112 to->Write(" = %s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800113 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700114 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115}
116
117Expression::~Expression()
118{
119}
120
121LiteralExpression::LiteralExpression(const string& v)
122 :value(v)
123{
124}
125
126LiteralExpression::~LiteralExpression()
127{
128}
129
130void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700131LiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700133 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134}
135
136StringLiteralExpression::StringLiteralExpression(const string& v)
137 :value(v)
138{
139}
140
141StringLiteralExpression::~StringLiteralExpression()
142{
143}
144
145void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700146StringLiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700148 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149}
150
151Variable::Variable()
152 :type(NULL),
153 name(),
154 dimension(0)
155{
156}
157
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700158Variable::Variable(const Type* t, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800159 :type(t),
160 name(n),
161 dimension(0)
162{
163}
164
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700165Variable::Variable(const Type* t, const string& n, int d)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800166 :type(t),
167 name(n),
168 dimension(d)
169{
170}
171
172Variable::~Variable()
173{
174}
175
176void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700177Variable::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800178{
179 types->insert(this->type);
180}
181
182void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700183Variable::WriteDeclaration(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800184{
185 string dim;
186 for (int i=0; i<this->dimension; i++) {
187 dim += "[]";
188 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700189 to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800190 this->name.c_str());
191}
192
193void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700194Variable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800195{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700196 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800197}
198
199FieldVariable::FieldVariable(Expression* o, const string& n)
200 :object(o),
201 clazz(NULL),
202 name(n)
203{
204}
205
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700206FieldVariable::FieldVariable(const Type* c, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800207 :object(NULL),
208 clazz(c),
209 name(n)
210{
211}
212
213FieldVariable::~FieldVariable()
214{
215}
216
217void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700218FieldVariable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219{
220 if (this->object != NULL) {
221 this->object->Write(to);
222 }
223 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700224 to->Write("%s", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700226 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800227}
228
229
230Statement::~Statement()
231{
232}
233
234StatementBlock::StatementBlock()
235{
236}
237
238StatementBlock::~StatementBlock()
239{
240}
241
242void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700243StatementBlock::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800244{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700245 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800246 int N = this->statements.size();
247 for (int i=0; i<N; i++) {
248 this->statements[i]->Write(to);
249 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700250 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251}
252
253void
254StatementBlock::Add(Statement* statement)
255{
256 this->statements.push_back(statement);
257}
258
259void
260StatementBlock::Add(Expression* expression)
261{
262 this->statements.push_back(new ExpressionStatement(expression));
263}
264
265ExpressionStatement::ExpressionStatement(Expression* e)
266 :expression(e)
267{
268}
269
270ExpressionStatement::~ExpressionStatement()
271{
272}
273
274void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700275ExpressionStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800276{
277 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700278 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800279}
280
281Assignment::Assignment(Variable* l, Expression* r)
282 :lvalue(l),
283 rvalue(r),
284 cast(NULL)
285{
286}
287
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700288Assignment::Assignment(Variable* l, Expression* r, const Type* c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800289 :lvalue(l),
290 rvalue(r),
291 cast(c)
292{
293}
294
295Assignment::~Assignment()
296{
297}
298
299void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700300Assignment::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800301{
302 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700303 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700305 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306 }
307 this->rvalue->Write(to);
308}
309
310MethodCall::MethodCall(const string& n)
311 :obj(NULL),
312 clazz(NULL),
313 name(n)
314{
315}
316
317MethodCall::MethodCall(const string& n, int argc = 0, ...)
318 :obj(NULL),
319 clazz(NULL),
320 name(n)
321{
322 va_list args;
323 va_start(args, argc);
324 init(argc, args);
325 va_end(args);
326}
327
328MethodCall::MethodCall(Expression* o, const string& n)
329 :obj(o),
330 clazz(NULL),
331 name(n)
332{
333}
334
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700335MethodCall::MethodCall(const Type* t, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336 :obj(NULL),
337 clazz(t),
338 name(n)
339{
340}
341
342MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
343 :obj(o),
344 clazz(NULL),
345 name(n)
346{
347 va_list args;
348 va_start(args, argc);
349 init(argc, args);
350 va_end(args);
351}
352
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700353MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800354 :obj(NULL),
355 clazz(t),
356 name(n)
357{
358 va_list args;
359 va_start(args, argc);
360 init(argc, args);
361 va_end(args);
362}
363
364MethodCall::~MethodCall()
365{
366}
367
368void
369MethodCall::init(int n, va_list args)
370{
371 for (int i=0; i<n; i++) {
372 Expression* expression = (Expression*)va_arg(args, void*);
373 this->arguments.push_back(expression);
374 }
375}
376
377void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700378MethodCall::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800379{
380 if (this->obj != NULL) {
381 this->obj->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700382 to->Write(".");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383 }
384 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700385 to->Write("%s.", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800386 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700387 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800388 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700389 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800390}
391
392Comparison::Comparison(Expression* l, const string& o, Expression* r)
393 :lvalue(l),
394 op(o),
395 rvalue(r)
396{
397}
398
399Comparison::~Comparison()
400{
401}
402
403void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700404Comparison::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800405{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700406 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800407 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700408 to->Write("%s", this->op.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800409 this->rvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700410 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800411}
412
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700413NewExpression::NewExpression(const Type* t)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800414 :type(t)
415{
416}
417
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700418NewExpression::NewExpression(const Type* t, int argc = 0, ...)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800419 :type(t)
420{
421 va_list args;
422 va_start(args, argc);
423 init(argc, args);
424 va_end(args);
425}
426
427NewExpression::~NewExpression()
428{
429}
430
431void
432NewExpression::init(int n, va_list args)
433{
434 for (int i=0; i<n; i++) {
435 Expression* expression = (Expression*)va_arg(args, void*);
436 this->arguments.push_back(expression);
437 }
438}
439
440void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700441NewExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800442{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700443 to->Write("new %s(", this->type->InstantiableName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800444 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700445 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800446}
447
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700448NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800449 :type(t),
450 size(s)
451{
452}
453
454NewArrayExpression::~NewArrayExpression()
455{
456}
457
458void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700459NewArrayExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800460{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700461 to->Write("new %s[", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800462 size->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700463 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800464}
465
466Ternary::Ternary()
467 :condition(NULL),
468 ifpart(NULL),
469 elsepart(NULL)
470{
471}
472
473Ternary::Ternary(Expression* a, Expression* b, Expression* c)
474 :condition(a),
475 ifpart(b),
476 elsepart(c)
477{
478}
479
480Ternary::~Ternary()
481{
482}
483
484void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700485Ternary::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800486{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700487 to->Write("((");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800488 this->condition->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700489 to->Write(")?(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800490 this->ifpart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700491 to->Write("):(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800492 this->elsepart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700493 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800494}
495
496Cast::Cast()
497 :type(NULL),
498 expression(NULL)
499{
500}
501
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700502Cast::Cast(const Type* t, Expression* e)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800503 :type(t),
504 expression(e)
505{
506}
507
508Cast::~Cast()
509{
510}
511
512void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700513Cast::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800514{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700515 to->Write("((%s)", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800516 expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700517 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800518}
519
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700520VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, const Type* c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800521 :lvalue(l),
522 cast(c),
523 rvalue(r)
524{
525}
526
527VariableDeclaration::VariableDeclaration(Variable* l)
528 :lvalue(l),
529 cast(NULL),
530 rvalue(NULL)
531{
532}
533
534VariableDeclaration::~VariableDeclaration()
535{
536}
537
538void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700539VariableDeclaration::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800540{
541 this->lvalue->WriteDeclaration(to);
542 if (this->rvalue != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700543 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800544 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700545 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800546 }
547 this->rvalue->Write(to);
548 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700549 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800550}
551
552IfStatement::IfStatement()
553 :expression(NULL),
554 statements(new StatementBlock),
555 elseif(NULL)
556{
557}
558
559IfStatement::~IfStatement()
560{
561}
562
563void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700564IfStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800565{
566 if (this->expression != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700567 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800568 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700569 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800570 }
571 this->statements->Write(to);
572 if (this->elseif != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700573 to->Write("else ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800574 this->elseif->Write(to);
575 }
576}
577
578ReturnStatement::ReturnStatement(Expression* e)
579 :expression(e)
580{
581}
582
583ReturnStatement::~ReturnStatement()
584{
585}
586
587void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700588ReturnStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800589{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700590 to->Write("return ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800591 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700592 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800593}
594
595TryStatement::TryStatement()
596 :statements(new StatementBlock)
597{
598}
599
600TryStatement::~TryStatement()
601{
602}
603
604void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700605TryStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800606{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700607 to->Write("try ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800608 this->statements->Write(to);
609}
610
611CatchStatement::CatchStatement(Variable* e)
612 :statements(new StatementBlock),
613 exception(e)
614{
615}
616
617CatchStatement::~CatchStatement()
618{
619}
620
621void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700622CatchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800623{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700624 to->Write("catch ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800625 if (this->exception != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700626 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800627 this->exception->WriteDeclaration(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700628 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800629 }
630 this->statements->Write(to);
631}
632
633FinallyStatement::FinallyStatement()
634 :statements(new StatementBlock)
635{
636}
637
638FinallyStatement::~FinallyStatement()
639{
640}
641
642void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700643FinallyStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800644{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700645 to->Write("finally ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800646 this->statements->Write(to);
647}
648
649Case::Case()
650 :statements(new StatementBlock)
651{
652}
653
654Case::Case(const string& c)
655 :statements(new StatementBlock)
656{
657 cases.push_back(c);
658}
659
660Case::~Case()
661{
662}
663
664void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700665Case::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800666{
667 int N = this->cases.size();
668 if (N > 0) {
669 for (int i=0; i<N; i++) {
670 string s = this->cases[i];
671 if (s.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700672 to->Write("case %s:\n", s.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800673 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700674 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800675 }
676 }
677 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700678 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800679 }
680 statements->Write(to);
681}
682
683SwitchStatement::SwitchStatement(Expression* e)
684 :expression(e)
685{
686}
687
688SwitchStatement::~SwitchStatement()
689{
690}
691
692void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700693SwitchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800694{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700695 to->Write("switch (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800696 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700697 to->Write(")\n{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800698 int N = this->cases.size();
699 for (int i=0; i<N; i++) {
700 this->cases[i]->Write(to);
701 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700702 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800703}
704
705Break::Break()
706{
707}
708
709Break::~Break()
710{
711}
712
713void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700714Break::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800715{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700716 to->Write("break;\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800717}
718
719Method::Method()
720 :ClassElement(),
721 modifiers(0),
722 returnType(NULL), // (NULL means constructor)
723 returnTypeDimension(0),
724 statements(NULL)
725{
726}
727
728Method::~Method()
729{
730}
731
732void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700733Method::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800734{
735 size_t N, i;
736
737 if (this->returnType) {
738 types->insert(this->returnType);
739 }
740
741 N = this->parameters.size();
742 for (i=0; i<N; i++) {
743 this->parameters[i]->GatherTypes(types);
744 }
745
746 N = this->exceptions.size();
747 for (i=0; i<N; i++) {
748 types->insert(this->exceptions[i]);
749 }
750}
751
752void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700753Method::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800754{
755 size_t N, i;
756
757 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700758 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800759 }
760
761 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
762
763 if (this->returnType != NULL) {
764 string dim;
765 for (i=0; i<this->returnTypeDimension; i++) {
766 dim += "[]";
767 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700768 to->Write("%s%s ", this->returnType->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800769 dim.c_str());
770 }
771
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700772 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800773
774 N = this->parameters.size();
775 for (i=0; i<N; i++) {
776 this->parameters[i]->WriteDeclaration(to);
777 if (i != N-1) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700778 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800779 }
780 }
781
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700782 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800783
784 N = this->exceptions.size();
785 for (i=0; i<N; i++) {
786 if (i == 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700787 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800788 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700789 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800790 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700791 to->Write("%s", this->exceptions[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800792 }
793
794 if (this->statements == NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700795 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800796 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700797 to->Write("\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800798 this->statements->Write(to);
799 }
800}
801
802Class::Class()
803 :modifiers(0),
804 what(CLASS),
805 type(NULL),
806 extends(NULL)
807{
808}
809
810Class::~Class()
811{
812}
813
814void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700815Class::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800816{
817 int N, i;
818
819 types->insert(this->type);
820 if (this->extends != NULL) {
821 types->insert(this->extends);
822 }
823
824 N = this->interfaces.size();
825 for (i=0; i<N; i++) {
826 types->insert(this->interfaces[i]);
827 }
828
829 N = this->elements.size();
830 for (i=0; i<N; i++) {
831 this->elements[i]->GatherTypes(types);
832 }
833}
834
835void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700836Class::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800837{
838 size_t N, i;
839
840 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700841 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800842 }
843
844 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
845
846 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700847 to->Write("class ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800848 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700849 to->Write("interface ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800850 }
851
852 string name = this->type->Name();
853 size_t pos = name.rfind('.');
854 if (pos != string::npos) {
855 name = name.c_str() + pos + 1;
856 }
857
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700858 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800859
860 if (this->extends != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700861 to->Write(" extends %s", this->extends->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800862 }
863
864 N = this->interfaces.size();
865 if (N != 0) {
866 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700867 to->Write(" implements");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800868 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700869 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800870 }
871 for (i=0; i<N; i++) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700872 to->Write(" %s", this->interfaces[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800873 }
874 }
875
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700876 to->Write("\n");
877 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800878
879 N = this->elements.size();
880 for (i=0; i<N; i++) {
881 this->elements[i]->Write(to);
882 }
883
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700884 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800885
886}
887
888Document::Document()
889{
890}
891
892Document::~Document()
893{
894}
895
896static string
897escape_backslashes(const string& str)
898{
899 string result;
900 const size_t I=str.length();
901 for (size_t i=0; i<I; i++) {
902 char c = str[i];
903 if (c == '\\') {
904 result += "\\\\";
905 } else {
906 result += c;
907 }
908 }
909 return result;
910}
911
912void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700913Document::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800914{
915 size_t N, i;
916
917 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700918 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800919 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700920 to->Write("/*\n"
Adam Lesinskiffa16862014-01-23 18:17:42 -0800921 " * This file is auto-generated. DO NOT MODIFY.\n"
922 " * Original file: %s\n"
923 " */\n", escape_backslashes(this->originalSrc).c_str());
924 if (this->package.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700925 to->Write("package %s;\n", this->package.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800926 }
927
928 N = this->classes.size();
929 for (i=0; i<N; i++) {
930 Class* c = this->classes[i];
931 c->Write(to);
932 }
933}
934
Christopher Wileydb154a52015-09-28 16:32:25 -0700935} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700936} // namespace aidl
937} // namespace android