blob: e94ebfbce3410740bf4e61671a6b9a5a9bea37a5 [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 {
Adam Lesinskiffa16862014-01-23 18:17:42 -080024
25void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070026WriteModifiers(CodeWriter* to, int mod, int mask)
Adam Lesinskiffa16862014-01-23 18:17:42 -080027{
28 int m = mod & mask;
29
30 if (m & OVERRIDE) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070031 to->Write("@Override ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080032 }
33
34 if ((m & SCOPE_MASK) == PUBLIC) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070035 to->Write("public ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080036 }
37 else if ((m & SCOPE_MASK) == PRIVATE) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070038 to->Write("private ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080039 }
40 else if ((m & SCOPE_MASK) == PROTECTED) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070041 to->Write("protected ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080042 }
43
44 if (m & STATIC) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070045 to->Write("static ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080046 }
47
48 if (m & FINAL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070049 to->Write("final ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080050 }
51
52 if (m & ABSTRACT) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070053 to->Write("abstract ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080054 }
55}
56
57void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070058WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments)
Adam Lesinskiffa16862014-01-23 18:17:42 -080059{
60 size_t N = arguments.size();
61 for (size_t i=0; i<N; i++) {
62 arguments[i]->Write(to);
63 if (i != N-1) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070064 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080065 }
66 }
67}
68
69ClassElement::ClassElement()
70{
71}
72
73ClassElement::~ClassElement()
74{
75}
76
77Field::Field()
78 :ClassElement(),
79 modifiers(0),
80 variable(NULL)
81{
82}
83
84Field::Field(int m, Variable* v)
85 :ClassElement(),
86 modifiers(m),
87 variable(v)
88{
89}
90
91Field::~Field()
92{
93}
94
95void
96Field::GatherTypes(set<Type*>* types) const
97{
98 types->insert(this->variable->type);
99}
100
101void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700102Field::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103{
104 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700105 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106 }
107 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700108 to->Write("%s %s", this->variable->type->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109 this->variable->name.c_str());
110 if (this->value.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700111 to->Write(" = %s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700113 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800114}
115
116Expression::~Expression()
117{
118}
119
120LiteralExpression::LiteralExpression(const string& v)
121 :value(v)
122{
123}
124
125LiteralExpression::~LiteralExpression()
126{
127}
128
129void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700130LiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700132 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800133}
134
135StringLiteralExpression::StringLiteralExpression(const string& v)
136 :value(v)
137{
138}
139
140StringLiteralExpression::~StringLiteralExpression()
141{
142}
143
144void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700145StringLiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800146{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700147 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148}
149
150Variable::Variable()
151 :type(NULL),
152 name(),
153 dimension(0)
154{
155}
156
157Variable::Variable(Type* t, const string& n)
158 :type(t),
159 name(n),
160 dimension(0)
161{
162}
163
164Variable::Variable(Type* t, const string& n, int d)
165 :type(t),
166 name(n),
167 dimension(d)
168{
169}
170
171Variable::~Variable()
172{
173}
174
175void
176Variable::GatherTypes(set<Type*>* types) const
177{
178 types->insert(this->type);
179}
180
181void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700182Variable::WriteDeclaration(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183{
184 string dim;
185 for (int i=0; i<this->dimension; i++) {
186 dim += "[]";
187 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700188 to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800189 this->name.c_str());
190}
191
192void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700193Variable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800194{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700195 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800196}
197
198FieldVariable::FieldVariable(Expression* o, const string& n)
199 :object(o),
200 clazz(NULL),
201 name(n)
202{
203}
204
205FieldVariable::FieldVariable(Type* c, const string& n)
206 :object(NULL),
207 clazz(c),
208 name(n)
209{
210}
211
212FieldVariable::~FieldVariable()
213{
214}
215
216void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700217FieldVariable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800218{
219 if (this->object != NULL) {
220 this->object->Write(to);
221 }
222 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700223 to->Write("%s", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700225 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226}
227
228
229Statement::~Statement()
230{
231}
232
233StatementBlock::StatementBlock()
234{
235}
236
237StatementBlock::~StatementBlock()
238{
239}
240
241void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700242StatementBlock::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800243{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700244 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800245 int N = this->statements.size();
246 for (int i=0; i<N; i++) {
247 this->statements[i]->Write(to);
248 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700249 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250}
251
252void
253StatementBlock::Add(Statement* statement)
254{
255 this->statements.push_back(statement);
256}
257
258void
259StatementBlock::Add(Expression* expression)
260{
261 this->statements.push_back(new ExpressionStatement(expression));
262}
263
264ExpressionStatement::ExpressionStatement(Expression* e)
265 :expression(e)
266{
267}
268
269ExpressionStatement::~ExpressionStatement()
270{
271}
272
273void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700274ExpressionStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800275{
276 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700277 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278}
279
280Assignment::Assignment(Variable* l, Expression* r)
281 :lvalue(l),
282 rvalue(r),
283 cast(NULL)
284{
285}
286
287Assignment::Assignment(Variable* l, Expression* r, Type* c)
288 :lvalue(l),
289 rvalue(r),
290 cast(c)
291{
292}
293
294Assignment::~Assignment()
295{
296}
297
298void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700299Assignment::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300{
301 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700302 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700304 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305 }
306 this->rvalue->Write(to);
307}
308
309MethodCall::MethodCall(const string& n)
310 :obj(NULL),
311 clazz(NULL),
312 name(n)
313{
314}
315
316MethodCall::MethodCall(const string& n, int argc = 0, ...)
317 :obj(NULL),
318 clazz(NULL),
319 name(n)
320{
321 va_list args;
322 va_start(args, argc);
323 init(argc, args);
324 va_end(args);
325}
326
327MethodCall::MethodCall(Expression* o, const string& n)
328 :obj(o),
329 clazz(NULL),
330 name(n)
331{
332}
333
334MethodCall::MethodCall(Type* t, const string& n)
335 :obj(NULL),
336 clazz(t),
337 name(n)
338{
339}
340
341MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
342 :obj(o),
343 clazz(NULL),
344 name(n)
345{
346 va_list args;
347 va_start(args, argc);
348 init(argc, args);
349 va_end(args);
350}
351
352MethodCall::MethodCall(Type* t, const string& n, int argc = 0, ...)
353 :obj(NULL),
354 clazz(t),
355 name(n)
356{
357 va_list args;
358 va_start(args, argc);
359 init(argc, args);
360 va_end(args);
361}
362
363MethodCall::~MethodCall()
364{
365}
366
367void
368MethodCall::init(int n, va_list args)
369{
370 for (int i=0; i<n; i++) {
371 Expression* expression = (Expression*)va_arg(args, void*);
372 this->arguments.push_back(expression);
373 }
374}
375
376void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700377MethodCall::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800378{
379 if (this->obj != NULL) {
380 this->obj->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700381 to->Write(".");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382 }
383 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700384 to->Write("%s.", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800385 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700386 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700388 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389}
390
391Comparison::Comparison(Expression* l, const string& o, Expression* r)
392 :lvalue(l),
393 op(o),
394 rvalue(r)
395{
396}
397
398Comparison::~Comparison()
399{
400}
401
402void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700403Comparison::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800404{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700405 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800406 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700407 to->Write("%s", this->op.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800408 this->rvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700409 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800410}
411
412NewExpression::NewExpression(Type* t)
413 :type(t)
414{
415}
416
417NewExpression::NewExpression(Type* t, int argc = 0, ...)
418 :type(t)
419{
420 va_list args;
421 va_start(args, argc);
422 init(argc, args);
423 va_end(args);
424}
425
426NewExpression::~NewExpression()
427{
428}
429
430void
431NewExpression::init(int n, va_list args)
432{
433 for (int i=0; i<n; i++) {
434 Expression* expression = (Expression*)va_arg(args, void*);
435 this->arguments.push_back(expression);
436 }
437}
438
439void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700440NewExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800441{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700442 to->Write("new %s(", this->type->InstantiableName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800443 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700444 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800445}
446
447NewArrayExpression::NewArrayExpression(Type* t, Expression* s)
448 :type(t),
449 size(s)
450{
451}
452
453NewArrayExpression::~NewArrayExpression()
454{
455}
456
457void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700458NewArrayExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800459{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700460 to->Write("new %s[", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800461 size->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700462 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800463}
464
465Ternary::Ternary()
466 :condition(NULL),
467 ifpart(NULL),
468 elsepart(NULL)
469{
470}
471
472Ternary::Ternary(Expression* a, Expression* b, Expression* c)
473 :condition(a),
474 ifpart(b),
475 elsepart(c)
476{
477}
478
479Ternary::~Ternary()
480{
481}
482
483void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700484Ternary::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800485{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700486 to->Write("((");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800487 this->condition->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700488 to->Write(")?(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800489 this->ifpart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700490 to->Write("):(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800491 this->elsepart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700492 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800493}
494
495Cast::Cast()
496 :type(NULL),
497 expression(NULL)
498{
499}
500
501Cast::Cast(Type* t, Expression* e)
502 :type(t),
503 expression(e)
504{
505}
506
507Cast::~Cast()
508{
509}
510
511void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700512Cast::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800513{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700514 to->Write("((%s)", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800515 expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700516 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800517}
518
519VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, Type* c)
520 :lvalue(l),
521 cast(c),
522 rvalue(r)
523{
524}
525
526VariableDeclaration::VariableDeclaration(Variable* l)
527 :lvalue(l),
528 cast(NULL),
529 rvalue(NULL)
530{
531}
532
533VariableDeclaration::~VariableDeclaration()
534{
535}
536
537void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700538VariableDeclaration::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800539{
540 this->lvalue->WriteDeclaration(to);
541 if (this->rvalue != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700542 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800543 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700544 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800545 }
546 this->rvalue->Write(to);
547 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700548 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800549}
550
551IfStatement::IfStatement()
552 :expression(NULL),
553 statements(new StatementBlock),
554 elseif(NULL)
555{
556}
557
558IfStatement::~IfStatement()
559{
560}
561
562void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700563IfStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800564{
565 if (this->expression != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700566 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800567 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700568 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800569 }
570 this->statements->Write(to);
571 if (this->elseif != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700572 to->Write("else ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800573 this->elseif->Write(to);
574 }
575}
576
577ReturnStatement::ReturnStatement(Expression* e)
578 :expression(e)
579{
580}
581
582ReturnStatement::~ReturnStatement()
583{
584}
585
586void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700587ReturnStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800588{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700589 to->Write("return ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800590 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700591 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800592}
593
594TryStatement::TryStatement()
595 :statements(new StatementBlock)
596{
597}
598
599TryStatement::~TryStatement()
600{
601}
602
603void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700604TryStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800605{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700606 to->Write("try ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800607 this->statements->Write(to);
608}
609
610CatchStatement::CatchStatement(Variable* e)
611 :statements(new StatementBlock),
612 exception(e)
613{
614}
615
616CatchStatement::~CatchStatement()
617{
618}
619
620void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700621CatchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800622{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700623 to->Write("catch ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800624 if (this->exception != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700625 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800626 this->exception->WriteDeclaration(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700627 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800628 }
629 this->statements->Write(to);
630}
631
632FinallyStatement::FinallyStatement()
633 :statements(new StatementBlock)
634{
635}
636
637FinallyStatement::~FinallyStatement()
638{
639}
640
641void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700642FinallyStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800643{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700644 to->Write("finally ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800645 this->statements->Write(to);
646}
647
648Case::Case()
649 :statements(new StatementBlock)
650{
651}
652
653Case::Case(const string& c)
654 :statements(new StatementBlock)
655{
656 cases.push_back(c);
657}
658
659Case::~Case()
660{
661}
662
663void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700664Case::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800665{
666 int N = this->cases.size();
667 if (N > 0) {
668 for (int i=0; i<N; i++) {
669 string s = this->cases[i];
670 if (s.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700671 to->Write("case %s:\n", s.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800672 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700673 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800674 }
675 }
676 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700677 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800678 }
679 statements->Write(to);
680}
681
682SwitchStatement::SwitchStatement(Expression* e)
683 :expression(e)
684{
685}
686
687SwitchStatement::~SwitchStatement()
688{
689}
690
691void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700692SwitchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800693{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700694 to->Write("switch (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800695 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700696 to->Write(")\n{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800697 int N = this->cases.size();
698 for (int i=0; i<N; i++) {
699 this->cases[i]->Write(to);
700 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700701 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800702}
703
704Break::Break()
705{
706}
707
708Break::~Break()
709{
710}
711
712void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700713Break::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800714{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700715 to->Write("break;\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800716}
717
718Method::Method()
719 :ClassElement(),
720 modifiers(0),
721 returnType(NULL), // (NULL means constructor)
722 returnTypeDimension(0),
723 statements(NULL)
724{
725}
726
727Method::~Method()
728{
729}
730
731void
732Method::GatherTypes(set<Type*>* types) const
733{
734 size_t N, i;
735
736 if (this->returnType) {
737 types->insert(this->returnType);
738 }
739
740 N = this->parameters.size();
741 for (i=0; i<N; i++) {
742 this->parameters[i]->GatherTypes(types);
743 }
744
745 N = this->exceptions.size();
746 for (i=0; i<N; i++) {
747 types->insert(this->exceptions[i]);
748 }
749}
750
751void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700752Method::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800753{
754 size_t N, i;
755
756 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700757 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800758 }
759
760 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
761
762 if (this->returnType != NULL) {
763 string dim;
764 for (i=0; i<this->returnTypeDimension; i++) {
765 dim += "[]";
766 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700767 to->Write("%s%s ", this->returnType->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800768 dim.c_str());
769 }
770
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700771 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800772
773 N = this->parameters.size();
774 for (i=0; i<N; i++) {
775 this->parameters[i]->WriteDeclaration(to);
776 if (i != N-1) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700777 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800778 }
779 }
780
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700781 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800782
783 N = this->exceptions.size();
784 for (i=0; i<N; i++) {
785 if (i == 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700786 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800787 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700788 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800789 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700790 to->Write("%s", this->exceptions[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800791 }
792
793 if (this->statements == NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700794 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800795 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700796 to->Write("\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800797 this->statements->Write(to);
798 }
799}
800
801Class::Class()
802 :modifiers(0),
803 what(CLASS),
804 type(NULL),
805 extends(NULL)
806{
807}
808
809Class::~Class()
810{
811}
812
813void
814Class::GatherTypes(set<Type*>* types) const
815{
816 int N, i;
817
818 types->insert(this->type);
819 if (this->extends != NULL) {
820 types->insert(this->extends);
821 }
822
823 N = this->interfaces.size();
824 for (i=0; i<N; i++) {
825 types->insert(this->interfaces[i]);
826 }
827
828 N = this->elements.size();
829 for (i=0; i<N; i++) {
830 this->elements[i]->GatherTypes(types);
831 }
832}
833
834void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700835Class::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800836{
837 size_t N, i;
838
839 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700840 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800841 }
842
843 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
844
845 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700846 to->Write("class ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800847 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700848 to->Write("interface ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800849 }
850
851 string name = this->type->Name();
852 size_t pos = name.rfind('.');
853 if (pos != string::npos) {
854 name = name.c_str() + pos + 1;
855 }
856
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700857 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800858
859 if (this->extends != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700860 to->Write(" extends %s", this->extends->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800861 }
862
863 N = this->interfaces.size();
864 if (N != 0) {
865 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700866 to->Write(" implements");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800867 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700868 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800869 }
870 for (i=0; i<N; i++) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700871 to->Write(" %s", this->interfaces[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800872 }
873 }
874
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700875 to->Write("\n");
876 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800877
878 N = this->elements.size();
879 for (i=0; i<N; i++) {
880 this->elements[i]->Write(to);
881 }
882
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700883 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800884
885}
886
887Document::Document()
888{
889}
890
891Document::~Document()
892{
893}
894
895static string
896escape_backslashes(const string& str)
897{
898 string result;
899 const size_t I=str.length();
900 for (size_t i=0; i<I; i++) {
901 char c = str[i];
902 if (c == '\\') {
903 result += "\\\\";
904 } else {
905 result += c;
906 }
907 }
908 return result;
909}
910
911void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700912Document::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800913{
914 size_t N, i;
915
916 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700917 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800918 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700919 to->Write("/*\n"
Adam Lesinskiffa16862014-01-23 18:17:42 -0800920 " * This file is auto-generated. DO NOT MODIFY.\n"
921 " * Original file: %s\n"
922 " */\n", escape_backslashes(this->originalSrc).c_str());
923 if (this->package.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700924 to->Write("package %s;\n", this->package.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800925 }
926
927 N = this->classes.size();
928 for (i=0; i<N; i++) {
929 Class* c = this->classes[i];
930 c->Write(to);
931 }
932}
933
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700934} // namespace aidl
935} // namespace android