Christopher Wiley | 038485e | 2015-09-12 11:14:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 18 | #include "code_writer.h" |
| 19 | |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 20 | using std::vector; |
| 21 | using std::string; |
| 22 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 23 | template <class... Ts> |
| 24 | struct overloaded : Ts... { |
| 25 | using Ts::operator()...; |
| 26 | }; |
| 27 | template <class... Ts> |
| 28 | overloaded(Ts...)->overloaded<Ts...>; |
| 29 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace aidl { |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 32 | namespace java { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 33 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 34 | std::string AstNode::ToString() { |
| 35 | std::string str; |
| 36 | Write(CodeWriter::ForString(&str).get()); |
| 37 | return str; |
| 38 | } |
| 39 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 40 | void WriteModifiers(CodeWriter* to, int mod, int mask) { |
| 41 | int m = mod & mask; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 42 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 43 | if (m & OVERRIDE) { |
| 44 | to->Write("@Override "); |
| 45 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 46 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 47 | 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 Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 54 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 55 | if (m & STATIC) { |
| 56 | to->Write("static "); |
| 57 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 58 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 59 | if (m & FINAL) { |
| 60 | to->Write("final "); |
| 61 | } |
| 62 | |
| 63 | if (m & ABSTRACT) { |
| 64 | to->Write("abstract "); |
| 65 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 68 | void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) { |
| 69 | 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 Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 74 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 75 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 78 | Field::Field(int m, Variable* v) : ClassElement(), modifiers(m), variable(v) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 79 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 80 | void Field::Write(CodeWriter* to) const { |
| 81 | if (this->comment.length() != 0) { |
| 82 | to->Write("%s\n", this->comment.c_str()); |
| 83 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 84 | for (const auto& a : this->annotations) { |
| 85 | to->Write("%s\n", a.c_str()); |
| 86 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 87 | WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE); |
Steven Moreland | 5635a8a | 2018-07-03 11:28:20 -0700 | [diff] [blame] | 88 | this->variable->WriteDeclaration(to); |
| 89 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 90 | if (this->value.length() != 0) { |
| 91 | to->Write(" = %s", this->value.c_str()); |
| 92 | } |
| 93 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 96 | LiteralExpression::LiteralExpression(const string& v) : value(v) {} |
| 97 | |
| 98 | void LiteralExpression::Write(CodeWriter* to) const { |
| 99 | to->Write("%s", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 102 | StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 103 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 104 | void StringLiteralExpression::Write(CodeWriter* to) const { |
| 105 | to->Write("\"%s\"", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Jeongik Cha | dc77c1b | 2019-02-12 16:13:25 +0900 | [diff] [blame] | 108 | Variable::Variable(const string& t, const string& n) : type(t), name(n), dimension(0) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 109 | |
Jeongik Cha | dc77c1b | 2019-02-12 16:13:25 +0900 | [diff] [blame] | 110 | Variable::Variable(const string& t, const string& n, int d) : type(t), name(n), dimension(d) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 111 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 112 | void Variable::WriteDeclaration(CodeWriter* to) const { |
| 113 | string dim; |
| 114 | for (int i = 0; i < this->dimension; i++) { |
| 115 | dim += "[]"; |
| 116 | } |
Jeongik Cha | dc77c1b | 2019-02-12 16:13:25 +0900 | [diff] [blame] | 117 | to->Write("%s%s %s", this->type.c_str(), dim.c_str(), this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 120 | void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 121 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 122 | FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 123 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 124 | FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 125 | |
| 126 | void FieldVariable::Write(CodeWriter* to) const { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 127 | visit( |
| 128 | overloaded{[&](Expression* e) { e->Write(to); }, |
| 129 | [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}}, |
| 130 | this->receiver); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 131 | to->Write(".%s", name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 134 | LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {} |
| 135 | |
| 136 | void LiteralStatement::Write(CodeWriter* to) const { |
| 137 | to->Write("%s", value_.c_str()); |
| 138 | } |
| 139 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 140 | void StatementBlock::Write(CodeWriter* to) const { |
| 141 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 142 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 143 | int N = this->statements.size(); |
| 144 | for (int i = 0; i < N; i++) { |
| 145 | this->statements[i]->Write(to); |
| 146 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 147 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 148 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 151 | void StatementBlock::Add(Statement* statement) { |
| 152 | this->statements.push_back(statement); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 155 | void StatementBlock::Add(Expression* expression) { |
| 156 | this->statements.push_back(new ExpressionStatement(expression)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 159 | ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 160 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 161 | void ExpressionStatement::Write(CodeWriter* to) const { |
| 162 | this->expression->Write(to); |
| 163 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 166 | Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 167 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 168 | Assignment::Assignment(Variable* l, Expression* r, string c) : lvalue(l), rvalue(r), cast(c) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 169 | |
| 170 | void Assignment::Write(CodeWriter* to) const { |
| 171 | this->lvalue->Write(to); |
| 172 | to->Write(" = "); |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 173 | if (this->cast) { |
| 174 | to->Write("(%s)", this->cast->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 175 | } |
| 176 | this->rvalue->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 179 | MethodCall::MethodCall(const string& n) : name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 180 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 181 | MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 182 | va_list args; |
| 183 | va_start(args, argc); |
| 184 | init(argc, args); |
| 185 | va_end(args); |
| 186 | } |
| 187 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 188 | MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 189 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 190 | MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 191 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 192 | MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 193 | va_list args; |
| 194 | va_start(args, argc); |
| 195 | init(argc, args); |
| 196 | va_end(args); |
| 197 | } |
| 198 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 199 | MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...) |
| 200 | : receiver(t), name(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 201 | va_list args; |
| 202 | va_start(args, argc); |
| 203 | init(argc, args); |
| 204 | va_end(args); |
| 205 | } |
| 206 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 207 | void MethodCall::init(int n, va_list args) { |
| 208 | for (int i = 0; i < n; i++) { |
| 209 | Expression* expression = (Expression*)va_arg(args, void*); |
| 210 | this->arguments.push_back(expression); |
| 211 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 214 | void MethodCall::Write(CodeWriter* to) const { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 215 | visit( |
| 216 | overloaded{[&](Expression* e) { |
| 217 | e->Write(to); |
| 218 | to->Write("."); |
| 219 | }, |
| 220 | [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}}, |
| 221 | this->receiver); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 222 | to->Write("%s(", this->name.c_str()); |
| 223 | WriteArgumentList(to, this->arguments); |
| 224 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | Comparison::Comparison(Expression* l, const string& o, Expression* r) |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 228 | : lvalue(l), op(o), rvalue(r) {} |
| 229 | |
| 230 | void Comparison::Write(CodeWriter* to) const { |
| 231 | to->Write("("); |
| 232 | this->lvalue->Write(to); |
| 233 | to->Write("%s", this->op.c_str()); |
| 234 | this->rvalue->Write(to); |
| 235 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 238 | NewExpression::NewExpression(const std::string& n) : instantiableName(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 239 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 240 | NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 241 | va_list args; |
| 242 | va_start(args, argc); |
| 243 | init(argc, args); |
| 244 | va_end(args); |
| 245 | } |
| 246 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 247 | void NewExpression::init(int n, va_list args) { |
| 248 | for (int i = 0; i < n; i++) { |
| 249 | Expression* expression = (Expression*)va_arg(args, void*); |
| 250 | this->arguments.push_back(expression); |
| 251 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 254 | void NewExpression::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 255 | to->Write("new %s(", this->instantiableName.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 256 | WriteArgumentList(to, this->arguments); |
| 257 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 260 | NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 261 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 262 | void NewArrayExpression::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 263 | to->Write("new %s[", this->type.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 264 | size->Write(to); |
| 265 | to->Write("]"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 268 | Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 269 | |
| 270 | void Cast::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 271 | to->Write("((%s)", this->type.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 272 | expression->Write(to); |
| 273 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 276 | VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 277 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 278 | VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 279 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 280 | void VariableDeclaration::Write(CodeWriter* to) const { |
| 281 | this->lvalue->WriteDeclaration(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 282 | if (this->rvalue != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 283 | to->Write(" = "); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 284 | this->rvalue->Write(to); |
| 285 | } |
| 286 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 289 | void IfStatement::Write(CodeWriter* to) const { |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 290 | if (this->expression != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 291 | to->Write("if ("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 292 | this->expression->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 293 | to->Write(") "); |
| 294 | } |
| 295 | this->statements->Write(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 296 | if (this->elseif != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 297 | to->Write("else "); |
| 298 | this->elseif->Write(to); |
| 299 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 302 | ReturnStatement::ReturnStatement(Expression* e) : expression(e) {} |
| 303 | |
| 304 | void ReturnStatement::Write(CodeWriter* to) const { |
| 305 | to->Write("return "); |
| 306 | this->expression->Write(to); |
| 307 | to->Write(";\n"); |
| 308 | } |
| 309 | |
| 310 | void TryStatement::Write(CodeWriter* to) const { |
| 311 | to->Write("try "); |
| 312 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 315 | void FinallyStatement::Write(CodeWriter* to) const { |
| 316 | to->Write("finally "); |
| 317 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 320 | Case::Case(const string& c) { cases.push_back(c); } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 321 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 322 | void Case::Write(CodeWriter* to) const { |
| 323 | int N = this->cases.size(); |
| 324 | if (N > 0) { |
| 325 | for (int i = 0; i < N; i++) { |
| 326 | string s = this->cases[i]; |
| 327 | if (s.length() != 0) { |
| 328 | to->Write("case %s:\n", s.c_str()); |
| 329 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 330 | to->Write("default:\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 331 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 332 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 333 | } else { |
| 334 | to->Write("default:\n"); |
| 335 | } |
| 336 | statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 339 | SwitchStatement::SwitchStatement(Expression* e) : expression(e) {} |
| 340 | |
| 341 | void SwitchStatement::Write(CodeWriter* to) const { |
| 342 | to->Write("switch ("); |
| 343 | this->expression->Write(to); |
| 344 | to->Write(")\n{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 345 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 346 | int N = this->cases.size(); |
| 347 | for (int i = 0; i < N; i++) { |
| 348 | this->cases[i]->Write(to); |
| 349 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 350 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 351 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 354 | void Method::Write(CodeWriter* to) const { |
| 355 | size_t N, i; |
| 356 | |
| 357 | if (this->comment.length() != 0) { |
| 358 | to->Write("%s\n", this->comment.c_str()); |
| 359 | } |
| 360 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 361 | for (const auto& a : this->annotations) { |
| 362 | to->Write("%s\n", a.c_str()); |
| 363 | } |
| 364 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 365 | WriteModifiers(to, this->modifiers, |
| 366 | SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE); |
| 367 | |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 368 | if (this->returnType) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 369 | string dim; |
| 370 | for (i = 0; i < this->returnTypeDimension; i++) { |
| 371 | dim += "[]"; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 372 | } |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 373 | to->Write("%s%s ", this->returnType->c_str(), dim.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 374 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 375 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 376 | to->Write("%s(", this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 377 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 378 | N = this->parameters.size(); |
| 379 | for (i = 0; i < N; i++) { |
| 380 | this->parameters[i]->WriteDeclaration(to); |
| 381 | if (i != N - 1) { |
| 382 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 383 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 384 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 385 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 386 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 387 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 388 | N = this->exceptions.size(); |
| 389 | for (i = 0; i < N; i++) { |
| 390 | if (i == 0) { |
| 391 | to->Write(" throws "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 392 | } else { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 393 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 394 | } |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 395 | to->Write("%s", this->exceptions[i].c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 396 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 397 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 398 | if (this->statements == nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 399 | to->Write(";\n"); |
| 400 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 401 | to->Write("\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 402 | this->statements->Write(to); |
| 403 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 406 | void LiteralClassElement::Write(CodeWriter* to) const { |
| 407 | to->Write("%s", element.c_str()); |
| 408 | } |
| 409 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 410 | void Class::Write(CodeWriter* to) const { |
| 411 | size_t N, i; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 412 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 413 | if (this->comment.length() != 0) { |
| 414 | to->Write("%s\n", this->comment.c_str()); |
| 415 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 416 | for (const auto& a : this->annotations) { |
| 417 | to->Write("%s\n", a.c_str()); |
| 418 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 419 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 420 | WriteModifiers(to, this->modifiers, ALL_MODIFIERS); |
| 421 | |
| 422 | if (this->what == Class::CLASS) { |
| 423 | to->Write("class "); |
| 424 | } else { |
| 425 | to->Write("interface "); |
| 426 | } |
| 427 | |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 428 | string name = this->type; |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 429 | size_t pos = name.rfind('.'); |
| 430 | if (pos != string::npos) { |
| 431 | name = name.c_str() + pos + 1; |
| 432 | } |
| 433 | |
| 434 | to->Write("%s", name.c_str()); |
| 435 | |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 436 | if (this->extends) { |
| 437 | to->Write(" extends %s", this->extends->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | N = this->interfaces.size(); |
| 441 | if (N != 0) { |
| 442 | if (this->what == Class::CLASS) { |
| 443 | to->Write(" implements"); |
| 444 | } else { |
| 445 | to->Write(" extends"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 446 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 447 | for (i = 0; i < N; i++) { |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 448 | to->Write(" %s", this->interfaces[i].c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | to->Write("\n"); |
| 453 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 454 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 455 | |
| 456 | N = this->elements.size(); |
| 457 | for (i = 0; i < N; i++) { |
| 458 | this->elements[i]->Write(to); |
| 459 | } |
| 460 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 461 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 462 | to->Write("}\n"); |
| 463 | } |
| 464 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 465 | Document::Document(const std::string& comment, |
| 466 | const std::string& package, |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 467 | std::unique_ptr<Class> clazz) |
| 468 | : comment_(comment), |
| 469 | package_(package), |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 470 | clazz_(std::move(clazz)) { |
| 471 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 472 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 473 | void Document::Write(CodeWriter* to) const { |
| 474 | if (!comment_.empty()) { |
| 475 | to->Write("%s\n", comment_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 476 | } |
| 477 | to->Write( |
| 478 | "/*\n" |
| 479 | " * This file is auto-generated. DO NOT MODIFY.\n" |
Jiyong Park | 54fa1e0 | 2019-02-08 10:03:20 +0900 | [diff] [blame^] | 480 | " */\n"); |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 481 | if (!package_.empty()) { |
| 482 | to->Write("package %s;\n", package_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 485 | if (clazz_) { |
| 486 | clazz_->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 487 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 490 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 491 | } // namespace aidl |
| 492 | } // namespace android |