blob: f9974947a22caa81c143350dc92c6531d99a226d [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"
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070018#include "code_writer.h"
19
Christopher Wiley12e894a2016-01-29 11:55:07 -080020using std::vector;
21using std::string;
22
Jeongik Cha439f2c42019-02-13 12:38:30 +090023template <class... Ts>
24struct overloaded : Ts... {
25 using Ts::operator()...;
26};
27template <class... Ts>
28overloaded(Ts...)->overloaded<Ts...>;
29
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070030namespace android {
31namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070032namespace java {
Adam Lesinskiffa16862014-01-23 18:17:42 -080033
Jiyong Park176905e2018-07-04 22:29:41 +090034std::string AstNode::ToString() {
35 std::string str;
36 Write(CodeWriter::ForString(&str).get());
37 return str;
38}
39
Christopher Wileyae589972016-01-29 11:19:23 -080040void WriteModifiers(CodeWriter* to, int mod, int mask) {
41 int m = mod & mask;
Adam Lesinskiffa16862014-01-23 18:17:42 -080042
Christopher Wileyae589972016-01-29 11:19:23 -080043 if (m & OVERRIDE) {
44 to->Write("@Override ");
45 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080046
Christopher Wileyae589972016-01-29 11:19:23 -080047 if ((m & SCOPE_MASK) == PUBLIC) {
48 to->Write("public ");
49 } else if ((m & SCOPE_MASK) == PRIVATE) {
50 to->Write("private ");
51 } else if ((m & SCOPE_MASK) == PROTECTED) {
52 to->Write("protected ");
53 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080054
Christopher Wileyae589972016-01-29 11:19:23 -080055 if (m & STATIC) {
56 to->Write("static ");
57 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080058
Christopher Wileyae589972016-01-29 11:19:23 -080059 if (m & FINAL) {
60 to->Write("final ");
61 }
62
63 if (m & ABSTRACT) {
64 to->Write("abstract ");
65 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080066}
67
Steven Moreland48548e02019-09-18 15:10:22 -070068void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<Expression>>& arguments) {
Christopher Wileyae589972016-01-29 11:19:23 -080069 size_t N = arguments.size();
70 for (size_t i = 0; i < N; i++) {
71 arguments[i]->Write(to);
72 if (i != N - 1) {
73 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -080074 }
Christopher Wileyae589972016-01-29 11:19:23 -080075 }
Adam Lesinskiffa16862014-01-23 18:17:42 -080076}
77
Steven Moreland48548e02019-09-18 15:10:22 -070078Field::Field(int m, std::shared_ptr<Variable> v) : ClassElement(), modifiers(m), variable(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -080079
Christopher Wileyae589972016-01-29 11:19:23 -080080void Field::Write(CodeWriter* to) const {
81 if (this->comment.length() != 0) {
82 to->Write("%s\n", this->comment.c_str());
83 }
Jiyong Parka6605ab2018-11-11 14:30:21 +090084 for (const auto& a : this->annotations) {
85 to->Write("%s\n", a.c_str());
86 }
Christopher Wileyae589972016-01-29 11:19:23 -080087 WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
Steven Moreland5635a8a2018-07-03 11:28:20 -070088 this->variable->WriteDeclaration(to);
89
Christopher Wileyae589972016-01-29 11:19:23 -080090 if (this->value.length() != 0) {
91 to->Write(" = %s", this->value.c_str());
92 }
93 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -080094}
95
Christopher Wileyae589972016-01-29 11:19:23 -080096LiteralExpression::LiteralExpression(const string& v) : value(v) {}
97
98void LiteralExpression::Write(CodeWriter* to) const {
99 to->Write("%s", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100}
101
Christopher Wileyae589972016-01-29 11:19:23 -0800102StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103
Christopher Wileyae589972016-01-29 11:19:23 -0800104void StringLiteralExpression::Write(CodeWriter* to) const {
105 to->Write("\"%s\"", this->value.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106}
107
Steven Moreland3dc29d82019-08-21 17:23:11 -0700108Variable::Variable(const string& t, const string& n) : type(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109
Christopher Wileyae589972016-01-29 11:19:23 -0800110void Variable::WriteDeclaration(CodeWriter* to) const {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700111 to->Write("%s %s", this->type.c_str(), this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112}
113
Christopher Wileyae589972016-01-29 11:19:23 -0800114void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115
Steven Moreland48548e02019-09-18 15:10:22 -0700116FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n)
117 : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118
Jeongik Cha439f2c42019-02-13 12:38:30 +0900119FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800120
121void FieldVariable::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900122 visit(
Steven Moreland48548e02019-09-18 15:10:22 -0700123 overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); },
Jeongik Cha439f2c42019-02-13 12:38:30 +0900124 [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
125 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800126 to->Write(".%s", name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800127}
128
Jiyong Park176905e2018-07-04 22:29:41 +0900129LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
130
131void LiteralStatement::Write(CodeWriter* to) const {
132 to->Write("%s", value_.c_str());
133}
134
Christopher Wileyae589972016-01-29 11:19:23 -0800135void StatementBlock::Write(CodeWriter* to) const {
136 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900137 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800138 int N = this->statements.size();
139 for (int i = 0; i < N; i++) {
140 this->statements[i]->Write(to);
141 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900142 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800143 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800144}
145
Steven Moreland48548e02019-09-18 15:10:22 -0700146void StatementBlock::Add(std::shared_ptr<Statement> statement) {
Christopher Wileyae589972016-01-29 11:19:23 -0800147 this->statements.push_back(statement);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148}
149
Steven Moreland48548e02019-09-18 15:10:22 -0700150void StatementBlock::Add(std::shared_ptr<Expression> expression) {
151 this->statements.push_back(std::make_shared<ExpressionStatement>(expression));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152}
153
Steven Moreland48548e02019-09-18 15:10:22 -0700154ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155
Christopher Wileyae589972016-01-29 11:19:23 -0800156void ExpressionStatement::Write(CodeWriter* to) const {
157 this->expression->Write(to);
158 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800159}
160
Steven Moreland48548e02019-09-18 15:10:22 -0700161Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
162 : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800163
Steven Moreland48548e02019-09-18 15:10:22 -0700164Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c)
165 : lvalue(l), rvalue(r), cast(c) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800166
167void Assignment::Write(CodeWriter* to) const {
168 this->lvalue->Write(to);
169 to->Write(" = ");
Jeongik Cha439f2c42019-02-13 12:38:30 +0900170 if (this->cast) {
171 to->Write("(%s)", this->cast->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800172 }
173 this->rvalue->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800174}
175
Christopher Wileyae589972016-01-29 11:19:23 -0800176MethodCall::MethodCall(const string& n) : name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800177
Steven Moreland48548e02019-09-18 15:10:22 -0700178MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args)
179 : name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800180
Steven Moreland48548e02019-09-18 15:10:22 -0700181MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800182
Jeongik Cha439f2c42019-02-13 12:38:30 +0900183MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800184
Steven Moreland48548e02019-09-18 15:10:22 -0700185MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n,
186 const std::vector<std::shared_ptr<Expression>>& args)
187 : receiver(o), name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188
Steven Moreland48548e02019-09-18 15:10:22 -0700189MethodCall::MethodCall(const std::string& t, const string& n,
190 const std::vector<std::shared_ptr<Expression>>& args)
191 : receiver(t), name(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800192
Christopher Wileyae589972016-01-29 11:19:23 -0800193void MethodCall::Write(CodeWriter* to) const {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900194 visit(
Steven Moreland48548e02019-09-18 15:10:22 -0700195 overloaded{[&](std::shared_ptr<Expression> e) {
Jeongik Cha439f2c42019-02-13 12:38:30 +0900196 e->Write(to);
197 to->Write(".");
198 },
199 [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
200 this->receiver);
Christopher Wileyae589972016-01-29 11:19:23 -0800201 to->Write("%s(", this->name.c_str());
202 WriteArgumentList(to, this->arguments);
203 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800204}
205
Steven Moreland48548e02019-09-18 15:10:22 -0700206Comparison::Comparison(std::shared_ptr<Expression> l, const string& o,
207 std::shared_ptr<Expression> r)
Christopher Wileyae589972016-01-29 11:19:23 -0800208 : lvalue(l), op(o), rvalue(r) {}
209
210void Comparison::Write(CodeWriter* to) const {
211 to->Write("(");
212 this->lvalue->Write(to);
213 to->Write("%s", this->op.c_str());
214 this->rvalue->Write(to);
215 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216}
217
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900218NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219
Steven Moreland48548e02019-09-18 15:10:22 -0700220NewExpression::NewExpression(const std::string& n,
221 const std::vector<std::shared_ptr<Expression>>& args)
222 : instantiableName(n), arguments(args) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800223
Christopher Wileyae589972016-01-29 11:19:23 -0800224void NewExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900225 to->Write("new %s(", this->instantiableName.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800226 WriteArgumentList(to, this->arguments);
227 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800228}
229
Steven Moreland48548e02019-09-18 15:10:22 -0700230NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s)
231 : type(t), size(s) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232
Christopher Wileyae589972016-01-29 11:19:23 -0800233void NewArrayExpression::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900234 to->Write("new %s[", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800235 size->Write(to);
236 to->Write("]");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237}
238
Steven Moreland48548e02019-09-18 15:10:22 -0700239Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800240
241void Cast::Write(CodeWriter* to) const {
Jeongik Cha9a7f21f2019-02-13 14:42:47 +0900242 to->Write("((%s)", this->type.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800243 expression->Write(to);
244 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800245}
246
Steven Moreland48548e02019-09-18 15:10:22 -0700247VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
248 : lvalue(l), rvalue(r) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249
Steven Moreland48548e02019-09-18 15:10:22 -0700250VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251
Christopher Wileyae589972016-01-29 11:19:23 -0800252void VariableDeclaration::Write(CodeWriter* to) const {
253 this->lvalue->WriteDeclaration(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700254 if (this->rvalue != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800255 to->Write(" = ");
Christopher Wileyae589972016-01-29 11:19:23 -0800256 this->rvalue->Write(to);
257 }
258 to->Write(";\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259}
260
Christopher Wileyae589972016-01-29 11:19:23 -0800261void IfStatement::Write(CodeWriter* to) const {
Yi Kong894d6ba2018-07-24 11:27:38 -0700262 if (this->expression != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800263 to->Write("if (");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800264 this->expression->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800265 to->Write(") ");
266 }
267 this->statements->Write(to);
Yi Kong894d6ba2018-07-24 11:27:38 -0700268 if (this->elseif != nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800269 to->Write("else ");
270 this->elseif->Write(to);
271 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800272}
273
Steven Moreland48548e02019-09-18 15:10:22 -0700274ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800275
276void ReturnStatement::Write(CodeWriter* to) const {
277 to->Write("return ");
278 this->expression->Write(to);
279 to->Write(";\n");
280}
281
282void TryStatement::Write(CodeWriter* to) const {
283 to->Write("try ");
284 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800285}
286
Christopher Wileyae589972016-01-29 11:19:23 -0800287void FinallyStatement::Write(CodeWriter* to) const {
288 to->Write("finally ");
289 this->statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290}
291
Christopher Wileyae589972016-01-29 11:19:23 -0800292Case::Case(const string& c) { cases.push_back(c); }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800293
Christopher Wileyae589972016-01-29 11:19:23 -0800294void Case::Write(CodeWriter* to) const {
295 int N = this->cases.size();
296 if (N > 0) {
297 for (int i = 0; i < N; i++) {
298 string s = this->cases[i];
299 if (s.length() != 0) {
300 to->Write("case %s:\n", s.c_str());
301 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700302 to->Write("default:\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800303 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304 }
Christopher Wileyae589972016-01-29 11:19:23 -0800305 } else {
306 to->Write("default:\n");
307 }
308 statements->Write(to);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800309}
310
Steven Moreland48548e02019-09-18 15:10:22 -0700311SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
Christopher Wileyae589972016-01-29 11:19:23 -0800312
313void SwitchStatement::Write(CodeWriter* to) const {
314 to->Write("switch (");
315 this->expression->Write(to);
316 to->Write(")\n{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900317 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800318 int N = this->cases.size();
319 for (int i = 0; i < N; i++) {
320 this->cases[i]->Write(to);
321 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900322 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800323 to->Write("}\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324}
325
Christopher Wileyae589972016-01-29 11:19:23 -0800326void Method::Write(CodeWriter* to) const {
327 size_t N, i;
328
329 if (this->comment.length() != 0) {
330 to->Write("%s\n", this->comment.c_str());
331 }
332
Jiyong Parka6605ab2018-11-11 14:30:21 +0900333 for (const auto& a : this->annotations) {
334 to->Write("%s\n", a.c_str());
335 }
336
Christopher Wileyae589972016-01-29 11:19:23 -0800337 WriteModifiers(to, this->modifiers,
338 SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
339
Jeongik Cha6cadc212019-02-12 18:16:03 +0900340 if (this->returnType) {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700341 to->Write("%s ", this->returnType->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800342 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343
Christopher Wileyae589972016-01-29 11:19:23 -0800344 to->Write("%s(", this->name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800345
Christopher Wileyae589972016-01-29 11:19:23 -0800346 N = this->parameters.size();
347 for (i = 0; i < N; i++) {
348 this->parameters[i]->WriteDeclaration(to);
349 if (i != N - 1) {
350 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800351 }
Christopher Wileyae589972016-01-29 11:19:23 -0800352 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353
Christopher Wileyae589972016-01-29 11:19:23 -0800354 to->Write(")");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355
Christopher Wileyae589972016-01-29 11:19:23 -0800356 N = this->exceptions.size();
357 for (i = 0; i < N; i++) {
358 if (i == 0) {
359 to->Write(" throws ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800360 } else {
Christopher Wileyae589972016-01-29 11:19:23 -0800361 to->Write(", ");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800362 }
Jeongik Cha6cadc212019-02-12 18:16:03 +0900363 to->Write("%s", this->exceptions[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800364 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800365
Yi Kong894d6ba2018-07-24 11:27:38 -0700366 if (this->statements == nullptr) {
Christopher Wileyae589972016-01-29 11:19:23 -0800367 to->Write(";\n");
368 } else {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700369 to->Write("\n");
Christopher Wileyae589972016-01-29 11:19:23 -0800370 this->statements->Write(to);
371 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800372}
373
Steven Moreland5557f1c2018-07-02 13:50:23 -0700374void LiteralClassElement::Write(CodeWriter* to) const {
375 to->Write("%s", element.c_str());
376}
377
Christopher Wileyae589972016-01-29 11:19:23 -0800378void Class::Write(CodeWriter* to) const {
379 size_t N, i;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800380
Christopher Wileyae589972016-01-29 11:19:23 -0800381 if (this->comment.length() != 0) {
382 to->Write("%s\n", this->comment.c_str());
383 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900384 for (const auto& a : this->annotations) {
385 to->Write("%s\n", a.c_str());
386 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387
Christopher Wileyae589972016-01-29 11:19:23 -0800388 WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
389
390 if (this->what == Class::CLASS) {
391 to->Write("class ");
392 } else {
393 to->Write("interface ");
394 }
395
Jeongik Chaaabc1442019-02-12 17:44:48 +0900396 string name = this->type;
Christopher Wileyae589972016-01-29 11:19:23 -0800397 size_t pos = name.rfind('.');
398 if (pos != string::npos) {
399 name = name.c_str() + pos + 1;
400 }
401
402 to->Write("%s", name.c_str());
403
Jeongik Chaaabc1442019-02-12 17:44:48 +0900404 if (this->extends) {
405 to->Write(" extends %s", this->extends->c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800406 }
407
408 N = this->interfaces.size();
409 if (N != 0) {
410 if (this->what == Class::CLASS) {
411 to->Write(" implements");
412 } else {
413 to->Write(" extends");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800414 }
Christopher Wileyae589972016-01-29 11:19:23 -0800415 for (i = 0; i < N; i++) {
Jeongik Chaaabc1442019-02-12 17:44:48 +0900416 to->Write(" %s", this->interfaces[i].c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800417 }
418 }
419
420 to->Write("\n");
421 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900422 to->Indent();
Christopher Wileyae589972016-01-29 11:19:23 -0800423
424 N = this->elements.size();
425 for (i = 0; i < N; i++) {
426 this->elements[i]->Write(to);
427 }
428
Jiyong Parka755dc72018-06-29 13:52:24 +0900429 to->Dedent();
Christopher Wileyae589972016-01-29 11:19:23 -0800430 to->Write("}\n");
431}
432
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800433Document::Document(const std::string& comment,
434 const std::string& package,
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800435 std::unique_ptr<Class> clazz)
436 : comment_(comment),
437 package_(package),
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800438 clazz_(std::move(clazz)) {
439}
Christopher Wileyae589972016-01-29 11:19:23 -0800440
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800441void Document::Write(CodeWriter* to) const {
442 if (!comment_.empty()) {
443 to->Write("%s\n", comment_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800444 }
445 to->Write(
446 "/*\n"
447 " * This file is auto-generated. DO NOT MODIFY.\n"
Jiyong Park54fa1e02019-02-08 10:03:20 +0900448 " */\n");
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800449 if (!package_.empty()) {
450 to->Write("package %s;\n", package_.c_str());
Christopher Wileyae589972016-01-29 11:19:23 -0800451 }
452
Christopher Wileyf76b59a2016-01-29 11:32:11 -0800453 if (clazz_) {
454 clazz_->Write(to);
Christopher Wileyae589972016-01-29 11:19:23 -0800455 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800456}
457
Steven Moreland48548e02019-09-18 15:10:22 -0700458std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
459std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
460std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
461std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
462std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
Christopher Wileydb154a52015-09-28 16:32:25 -0700463} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700464} // namespace aidl
465} // namespace android