blob: aac691e45bc144c3693cf79e6cedb9a5247b315f [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 Wiley8f6816e2015-09-22 17:03:47 -070078Field::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -080079{
80 types->insert(this->variable->type);
81}
82
83void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070084Field::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -080085{
86 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070087 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080088 }
89 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070090 to->Write("%s %s", this->variable->type->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -080091 this->variable->name.c_str());
92 if (this->value.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070093 to->Write(" = %s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -080094 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070095 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080096}
97
Adam Lesinskiffa16862014-01-23 18:17:42 -080098LiteralExpression::LiteralExpression(const string& v)
99 :value(v)
100{
101}
102
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700104LiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800105{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700106 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800107}
108
109StringLiteralExpression::StringLiteralExpression(const string& v)
110 :value(v)
111{
112}
113
Adam Lesinskiffa16862014-01-23 18:17:42 -0800114void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700115StringLiteralExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800116{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700117 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118}
119
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700120Variable::Variable(const Type* t, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121 :type(t),
122 name(n),
123 dimension(0)
124{
125}
126
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700127Variable::Variable(const Type* t, const string& n, int d)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800128 :type(t),
129 name(n),
130 dimension(d)
131{
132}
133
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700135Variable::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800136{
137 types->insert(this->type);
138}
139
140void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700141Variable::WriteDeclaration(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800142{
143 string dim;
144 for (int i=0; i<this->dimension; i++) {
145 dim += "[]";
146 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700147 to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148 this->name.c_str());
149}
150
151void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700152Variable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700154 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155}
156
157FieldVariable::FieldVariable(Expression* o, const string& n)
158 :object(o),
159 clazz(NULL),
160 name(n)
161{
162}
163
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700164FieldVariable::FieldVariable(const Type* c, const string& n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800165 :object(NULL),
166 clazz(c),
167 name(n)
168{
169}
170
Adam Lesinskiffa16862014-01-23 18:17:42 -0800171void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700172FieldVariable::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800173{
174 if (this->object != NULL) {
175 this->object->Write(to);
176 }
177 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700178 to->Write("%s", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800179 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700180 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800181}
182
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700184StatementBlock::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800185{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700186 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800187 int N = this->statements.size();
188 for (int i=0; i<N; i++) {
189 this->statements[i]->Write(to);
190 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700191 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800192}
193
194void
195StatementBlock::Add(Statement* statement)
196{
197 this->statements.push_back(statement);
198}
199
200void
201StatementBlock::Add(Expression* expression)
202{
203 this->statements.push_back(new ExpressionStatement(expression));
204}
205
206ExpressionStatement::ExpressionStatement(Expression* e)
207 :expression(e)
208{
209}
210
Adam Lesinskiffa16862014-01-23 18:17:42 -0800211void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700212ExpressionStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800213{
214 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700215 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216}
217
218Assignment::Assignment(Variable* l, Expression* r)
219 :lvalue(l),
220 rvalue(r),
221 cast(NULL)
222{
223}
224
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700225Assignment::Assignment(Variable* l, Expression* r, const Type* c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226 :lvalue(l),
227 rvalue(r),
228 cast(c)
229{
230}
231
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700233Assignment::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800234{
235 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700236 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700238 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239 }
240 this->rvalue->Write(to);
241}
242
243MethodCall::MethodCall(const string& n)
Christopher Wileydf03d972015-09-29 11:10:47 -0700244 : name(n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800245{
246}
247
248MethodCall::MethodCall(const string& n, int argc = 0, ...)
Christopher Wileydf03d972015-09-29 11:10:47 -0700249 :name(n)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250{
251 va_list args;
252 va_start(args, argc);
253 init(argc, args);
254 va_end(args);
255}
256
257MethodCall::MethodCall(Expression* o, const string& n)
258 :obj(o),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259 name(n)
260{
261}
262
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700263MethodCall::MethodCall(const Type* t, const string& n)
Christopher Wileydf03d972015-09-29 11:10:47 -0700264 :clazz(t),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265 name(n)
266{
267}
268
269MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
270 :obj(o),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800271 name(n)
272{
273 va_list args;
274 va_start(args, argc);
275 init(argc, args);
276 va_end(args);
277}
278
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700279MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
Christopher Wileydf03d972015-09-29 11:10:47 -0700280 :clazz(t),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281 name(n)
282{
283 va_list args;
284 va_start(args, argc);
285 init(argc, args);
286 va_end(args);
287}
288
Adam Lesinskiffa16862014-01-23 18:17:42 -0800289void
290MethodCall::init(int n, va_list args)
291{
292 for (int i=0; i<n; i++) {
293 Expression* expression = (Expression*)va_arg(args, void*);
294 this->arguments.push_back(expression);
295 }
296}
297
298void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700299MethodCall::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300{
301 if (this->obj != NULL) {
302 this->obj->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700303 to->Write(".");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304 }
305 else if (this->clazz != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700306 to->Write("%s.", this->clazz->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800307 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700308 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800309 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700310 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311}
312
313Comparison::Comparison(Expression* l, const string& o, Expression* r)
314 :lvalue(l),
315 op(o),
316 rvalue(r)
317{
318}
319
Adam Lesinskiffa16862014-01-23 18:17:42 -0800320void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700321Comparison::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800322{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700323 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324 this->lvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700325 to->Write("%s", this->op.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800326 this->rvalue->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700327 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800328}
329
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700330NewExpression::NewExpression(const Type* t)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800331 :type(t)
332{
333}
334
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700335NewExpression::NewExpression(const Type* t, int argc = 0, ...)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336 :type(t)
337{
338 va_list args;
339 va_start(args, argc);
340 init(argc, args);
341 va_end(args);
342}
343
Adam Lesinskiffa16862014-01-23 18:17:42 -0800344void
345NewExpression::init(int n, va_list args)
346{
347 for (int i=0; i<n; i++) {
348 Expression* expression = (Expression*)va_arg(args, void*);
349 this->arguments.push_back(expression);
350 }
351}
352
353void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700354NewExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700356 to->Write("new %s(", this->type->InstantiableName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357 WriteArgumentList(to, this->arguments);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700358 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800359}
360
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700361NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800362 :type(t),
363 size(s)
364{
365}
366
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700368NewArrayExpression::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800369{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700370 to->Write("new %s[", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800371 size->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700372 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800373}
374
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375Ternary::Ternary(Expression* a, Expression* b, Expression* c)
376 :condition(a),
377 ifpart(b),
378 elsepart(c)
379{
380}
381
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700383Ternary::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800384{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700385 to->Write("((");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800386 this->condition->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700387 to->Write(")?(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800388 this->ifpart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700389 to->Write("):(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800390 this->elsepart->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700391 to->Write("))");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800392}
393
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700394Cast::Cast(const Type* t, Expression* e)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800395 :type(t),
396 expression(e)
397{
398}
399
Adam Lesinskiffa16862014-01-23 18:17:42 -0800400void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700401Cast::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800402{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700403 to->Write("((%s)", this->type->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800404 expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700405 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800406}
407
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700408VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, const Type* c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800409 :lvalue(l),
410 cast(c),
411 rvalue(r)
412{
413}
414
415VariableDeclaration::VariableDeclaration(Variable* l)
Christopher Wileydf03d972015-09-29 11:10:47 -0700416 :lvalue(l)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800417{
418}
419
420void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700421VariableDeclaration::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800422{
423 this->lvalue->WriteDeclaration(to);
424 if (this->rvalue != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700425 to->Write(" = ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800426 if (this->cast != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700427 to->Write("(%s)", this->cast->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800428 }
429 this->rvalue->Write(to);
430 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700431 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800432}
433
Adam Lesinskiffa16862014-01-23 18:17:42 -0800434void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700435IfStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800436{
437 if (this->expression != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700438 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800439 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700440 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800441 }
442 this->statements->Write(to);
443 if (this->elseif != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700444 to->Write("else ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800445 this->elseif->Write(to);
446 }
447}
448
449ReturnStatement::ReturnStatement(Expression* e)
450 :expression(e)
451{
452}
453
Adam Lesinskiffa16862014-01-23 18:17:42 -0800454void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700455ReturnStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800456{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700457 to->Write("return ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800458 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700459 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800460}
461
Adam Lesinskiffa16862014-01-23 18:17:42 -0800462void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700463TryStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800464{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700465 to->Write("try ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800466 this->statements->Write(to);
467}
468
469CatchStatement::CatchStatement(Variable* e)
470 :statements(new StatementBlock),
471 exception(e)
472{
473}
474
Adam Lesinskiffa16862014-01-23 18:17:42 -0800475void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700476CatchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800477{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700478 to->Write("catch ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800479 if (this->exception != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700480 to->Write("(");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800481 this->exception->WriteDeclaration(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700482 to->Write(") ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800483 }
484 this->statements->Write(to);
485}
486
Adam Lesinskiffa16862014-01-23 18:17:42 -0800487void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700488FinallyStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800489{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700490 to->Write("finally ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800491 this->statements->Write(to);
492}
493
Adam Lesinskiffa16862014-01-23 18:17:42 -0800494Case::Case(const string& c)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800495{
496 cases.push_back(c);
497}
498
Adam Lesinskiffa16862014-01-23 18:17:42 -0800499void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700500Case::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800501{
502 int N = this->cases.size();
503 if (N > 0) {
504 for (int i=0; i<N; i++) {
505 string s = this->cases[i];
506 if (s.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700507 to->Write("case %s:\n", s.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800508 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700509 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800510 }
511 }
512 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700513 to->Write("default:\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800514 }
515 statements->Write(to);
516}
517
518SwitchStatement::SwitchStatement(Expression* e)
519 :expression(e)
520{
521}
522
Adam Lesinskiffa16862014-01-23 18:17:42 -0800523void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700524SwitchStatement::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800525{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700526 to->Write("switch (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800527 this->expression->Write(to);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700528 to->Write(")\n{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800529 int N = this->cases.size();
530 for (int i=0; i<N; i++) {
531 this->cases[i]->Write(to);
532 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700533 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800534}
535
Adam Lesinskiffa16862014-01-23 18:17:42 -0800536void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700537Break::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800538{
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700539 to->Write("break;\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800540}
541
Adam Lesinskiffa16862014-01-23 18:17:42 -0800542void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700543Method::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800544{
545 size_t N, i;
546
547 if (this->returnType) {
548 types->insert(this->returnType);
549 }
550
551 N = this->parameters.size();
552 for (i=0; i<N; i++) {
553 this->parameters[i]->GatherTypes(types);
554 }
555
556 N = this->exceptions.size();
557 for (i=0; i<N; i++) {
558 types->insert(this->exceptions[i]);
559 }
560}
561
562void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700563Method::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800564{
565 size_t N, i;
566
567 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700568 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800569 }
570
571 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
572
573 if (this->returnType != NULL) {
574 string dim;
575 for (i=0; i<this->returnTypeDimension; i++) {
576 dim += "[]";
577 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700578 to->Write("%s%s ", this->returnType->QualifiedName().c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800579 dim.c_str());
580 }
581
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700582 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800583
584 N = this->parameters.size();
585 for (i=0; i<N; i++) {
586 this->parameters[i]->WriteDeclaration(to);
587 if (i != N-1) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700588 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800589 }
590 }
591
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700592 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800593
594 N = this->exceptions.size();
595 for (i=0; i<N; i++) {
596 if (i == 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700597 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800598 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700599 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800600 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700601 to->Write("%s", this->exceptions[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800602 }
603
604 if (this->statements == NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700605 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800606 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700607 to->Write("\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800608 this->statements->Write(to);
609 }
610}
611
Adam Lesinskiffa16862014-01-23 18:17:42 -0800612void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700613Class::GatherTypes(set<const Type*>* types) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800614{
615 int N, i;
616
617 types->insert(this->type);
618 if (this->extends != NULL) {
619 types->insert(this->extends);
620 }
621
622 N = this->interfaces.size();
623 for (i=0; i<N; i++) {
624 types->insert(this->interfaces[i]);
625 }
626
627 N = this->elements.size();
628 for (i=0; i<N; i++) {
629 this->elements[i]->GatherTypes(types);
630 }
631}
632
633void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700634Class::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800635{
636 size_t N, i;
637
638 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700639 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800640 }
641
642 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
643
644 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700645 to->Write("class ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800646 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700647 to->Write("interface ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800648 }
649
650 string name = this->type->Name();
651 size_t pos = name.rfind('.');
652 if (pos != string::npos) {
653 name = name.c_str() + pos + 1;
654 }
655
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700656 to->Write("%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800657
658 if (this->extends != NULL) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700659 to->Write(" extends %s", this->extends->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800660 }
661
662 N = this->interfaces.size();
663 if (N != 0) {
664 if (this->what == Class::CLASS) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700665 to->Write(" implements");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800666 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700667 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800668 }
669 for (i=0; i<N; i++) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700670 to->Write(" %s", this->interfaces[i]->QualifiedName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800671 }
672 }
673
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700674 to->Write("\n");
675 to->Write("{\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800676
677 N = this->elements.size();
678 for (i=0; i<N; i++) {
679 this->elements[i]->Write(to);
680 }
681
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700682 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800683
684}
685
Adam Lesinskiffa16862014-01-23 18:17:42 -0800686static string
687escape_backslashes(const string& str)
688{
689 string result;
690 const size_t I=str.length();
691 for (size_t i=0; i<I; i++) {
692 char c = str[i];
693 if (c == '\\') {
694 result += "\\\\";
695 } else {
696 result += c;
697 }
698 }
699 return result;
700}
701
702void
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700703Document::Write(CodeWriter* to) const
Adam Lesinskiffa16862014-01-23 18:17:42 -0800704{
705 size_t N, i;
706
707 if (this->comment.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700708 to->Write("%s\n", this->comment.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800709 }
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700710 to->Write("/*\n"
Adam Lesinskiffa16862014-01-23 18:17:42 -0800711 " * This file is auto-generated. DO NOT MODIFY.\n"
712 " * Original file: %s\n"
713 " */\n", escape_backslashes(this->originalSrc).c_str());
714 if (this->package.length() != 0) {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700715 to->Write("package %s;\n", this->package.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800716 }
717
718 N = this->classes.size();
719 for (i=0; i<N; i++) {
720 Class* c = this->classes[i];
721 c->Write(to);
722 }
723}
724
Christopher Wileydb154a52015-09-28 16:32:25 -0700725} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700726} // namespace aidl
727} // namespace android