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" |
| 18 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 19 | #include "code_writer.h" |
Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame] | 20 | #include "type_java.h" |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace aidl { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 24 | |
| 25 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 26 | WriteModifiers(CodeWriter* to, int mod, int mask) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 27 | { |
| 28 | int m = mod & mask; |
| 29 | |
| 30 | if (m & OVERRIDE) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 31 | to->Write("@Override "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | if ((m & SCOPE_MASK) == PUBLIC) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 35 | to->Write("public "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 36 | } |
| 37 | else if ((m & SCOPE_MASK) == PRIVATE) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 38 | to->Write("private "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 39 | } |
| 40 | else if ((m & SCOPE_MASK) == PROTECTED) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 41 | to->Write("protected "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | if (m & STATIC) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 45 | to->Write("static "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | if (m & FINAL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 49 | to->Write("final "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if (m & ABSTRACT) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 53 | to->Write("abstract "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 58 | WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 59 | { |
| 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 64 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | ClassElement::ClassElement() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | ClassElement::~ClassElement() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | Field::Field() |
| 78 | :ClassElement(), |
| 79 | modifiers(0), |
| 80 | variable(NULL) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | Field::Field(int m, Variable* v) |
| 85 | :ClassElement(), |
| 86 | modifiers(m), |
| 87 | variable(v) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | Field::~Field() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | Field::GatherTypes(set<Type*>* types) const |
| 97 | { |
| 98 | types->insert(this->variable->type); |
| 99 | } |
| 100 | |
| 101 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 102 | Field::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 103 | { |
| 104 | if (this->comment.length() != 0) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 105 | to->Write("%s\n", this->comment.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 106 | } |
| 107 | WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 108 | to->Write("%s %s", this->variable->type->QualifiedName().c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 109 | this->variable->name.c_str()); |
| 110 | if (this->value.length() != 0) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 111 | to->Write(" = %s", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 112 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 113 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | Expression::~Expression() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | LiteralExpression::LiteralExpression(const string& v) |
| 121 | :value(v) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | LiteralExpression::~LiteralExpression() |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 130 | LiteralExpression::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 131 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 132 | to->Write("%s", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | StringLiteralExpression::StringLiteralExpression(const string& v) |
| 136 | :value(v) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | StringLiteralExpression::~StringLiteralExpression() |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 145 | StringLiteralExpression::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 146 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 147 | to->Write("\"%s\"", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | Variable::Variable() |
| 151 | :type(NULL), |
| 152 | name(), |
| 153 | dimension(0) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | Variable::Variable(Type* t, const string& n) |
| 158 | :type(t), |
| 159 | name(n), |
| 160 | dimension(0) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | Variable::Variable(Type* t, const string& n, int d) |
| 165 | :type(t), |
| 166 | name(n), |
| 167 | dimension(d) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | Variable::~Variable() |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | Variable::GatherTypes(set<Type*>* types) const |
| 177 | { |
| 178 | types->insert(this->type); |
| 179 | } |
| 180 | |
| 181 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 182 | Variable::WriteDeclaration(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 183 | { |
| 184 | string dim; |
| 185 | for (int i=0; i<this->dimension; i++) { |
| 186 | dim += "[]"; |
| 187 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 188 | to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 189 | this->name.c_str()); |
| 190 | } |
| 191 | |
| 192 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 193 | Variable::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 194 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 195 | to->Write("%s", name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | FieldVariable::FieldVariable(Expression* o, const string& n) |
| 199 | :object(o), |
| 200 | clazz(NULL), |
| 201 | name(n) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | FieldVariable::FieldVariable(Type* c, const string& n) |
| 206 | :object(NULL), |
| 207 | clazz(c), |
| 208 | name(n) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | FieldVariable::~FieldVariable() |
| 213 | { |
| 214 | } |
| 215 | |
| 216 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 217 | FieldVariable::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 218 | { |
| 219 | if (this->object != NULL) { |
| 220 | this->object->Write(to); |
| 221 | } |
| 222 | else if (this->clazz != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 223 | to->Write("%s", this->clazz->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 224 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 225 | to->Write(".%s", name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | Statement::~Statement() |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | StatementBlock::StatementBlock() |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | StatementBlock::~StatementBlock() |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 242 | StatementBlock::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 243 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 244 | to->Write("{\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 245 | int N = this->statements.size(); |
| 246 | for (int i=0; i<N; i++) { |
| 247 | this->statements[i]->Write(to); |
| 248 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 249 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
| 253 | StatementBlock::Add(Statement* statement) |
| 254 | { |
| 255 | this->statements.push_back(statement); |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | StatementBlock::Add(Expression* expression) |
| 260 | { |
| 261 | this->statements.push_back(new ExpressionStatement(expression)); |
| 262 | } |
| 263 | |
| 264 | ExpressionStatement::ExpressionStatement(Expression* e) |
| 265 | :expression(e) |
| 266 | { |
| 267 | } |
| 268 | |
| 269 | ExpressionStatement::~ExpressionStatement() |
| 270 | { |
| 271 | } |
| 272 | |
| 273 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 274 | ExpressionStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 275 | { |
| 276 | this->expression->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 277 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | Assignment::Assignment(Variable* l, Expression* r) |
| 281 | :lvalue(l), |
| 282 | rvalue(r), |
| 283 | cast(NULL) |
| 284 | { |
| 285 | } |
| 286 | |
Christopher Wiley | 3637a4d | 2015-09-22 15:37:59 -0700 | [diff] [blame^] | 287 | Assignment::Assignment(Variable* l, Expression* r, const Type* c) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 288 | :lvalue(l), |
| 289 | rvalue(r), |
| 290 | cast(c) |
| 291 | { |
| 292 | } |
| 293 | |
| 294 | Assignment::~Assignment() |
| 295 | { |
| 296 | } |
| 297 | |
| 298 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 299 | Assignment::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 300 | { |
| 301 | this->lvalue->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 302 | to->Write(" = "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 303 | if (this->cast != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 304 | to->Write("(%s)", this->cast->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 305 | } |
| 306 | this->rvalue->Write(to); |
| 307 | } |
| 308 | |
| 309 | MethodCall::MethodCall(const string& n) |
| 310 | :obj(NULL), |
| 311 | clazz(NULL), |
| 312 | name(n) |
| 313 | { |
| 314 | } |
| 315 | |
| 316 | MethodCall::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 | |
| 327 | MethodCall::MethodCall(Expression* o, const string& n) |
| 328 | :obj(o), |
| 329 | clazz(NULL), |
| 330 | name(n) |
| 331 | { |
| 332 | } |
| 333 | |
| 334 | MethodCall::MethodCall(Type* t, const string& n) |
| 335 | :obj(NULL), |
| 336 | clazz(t), |
| 337 | name(n) |
| 338 | { |
| 339 | } |
| 340 | |
| 341 | MethodCall::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 | |
| 352 | MethodCall::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 | |
| 363 | MethodCall::~MethodCall() |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | MethodCall::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 | |
| 376 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 377 | MethodCall::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 378 | { |
| 379 | if (this->obj != NULL) { |
| 380 | this->obj->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 381 | to->Write("."); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 382 | } |
| 383 | else if (this->clazz != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 384 | to->Write("%s.", this->clazz->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 385 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 386 | to->Write("%s(", this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 387 | WriteArgumentList(to, this->arguments); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 388 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | Comparison::Comparison(Expression* l, const string& o, Expression* r) |
| 392 | :lvalue(l), |
| 393 | op(o), |
| 394 | rvalue(r) |
| 395 | { |
| 396 | } |
| 397 | |
| 398 | Comparison::~Comparison() |
| 399 | { |
| 400 | } |
| 401 | |
| 402 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 403 | Comparison::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 404 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 405 | to->Write("("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 406 | this->lvalue->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 407 | to->Write("%s", this->op.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 408 | this->rvalue->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 409 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | NewExpression::NewExpression(Type* t) |
| 413 | :type(t) |
| 414 | { |
| 415 | } |
| 416 | |
| 417 | NewExpression::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 | |
| 426 | NewExpression::~NewExpression() |
| 427 | { |
| 428 | } |
| 429 | |
| 430 | void |
| 431 | NewExpression::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 | |
| 439 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 440 | NewExpression::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 441 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 442 | to->Write("new %s(", this->type->InstantiableName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 443 | WriteArgumentList(to, this->arguments); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 444 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | NewArrayExpression::NewArrayExpression(Type* t, Expression* s) |
| 448 | :type(t), |
| 449 | size(s) |
| 450 | { |
| 451 | } |
| 452 | |
| 453 | NewArrayExpression::~NewArrayExpression() |
| 454 | { |
| 455 | } |
| 456 | |
| 457 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 458 | NewArrayExpression::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 459 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 460 | to->Write("new %s[", this->type->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 461 | size->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 462 | to->Write("]"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | Ternary::Ternary() |
| 466 | :condition(NULL), |
| 467 | ifpart(NULL), |
| 468 | elsepart(NULL) |
| 469 | { |
| 470 | } |
| 471 | |
| 472 | Ternary::Ternary(Expression* a, Expression* b, Expression* c) |
| 473 | :condition(a), |
| 474 | ifpart(b), |
| 475 | elsepart(c) |
| 476 | { |
| 477 | } |
| 478 | |
| 479 | Ternary::~Ternary() |
| 480 | { |
| 481 | } |
| 482 | |
| 483 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 484 | Ternary::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 485 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 486 | to->Write("(("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 487 | this->condition->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 488 | to->Write(")?("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 489 | this->ifpart->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 490 | to->Write("):("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 491 | this->elsepart->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 492 | to->Write("))"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | Cast::Cast() |
| 496 | :type(NULL), |
| 497 | expression(NULL) |
| 498 | { |
| 499 | } |
| 500 | |
| 501 | Cast::Cast(Type* t, Expression* e) |
| 502 | :type(t), |
| 503 | expression(e) |
| 504 | { |
| 505 | } |
| 506 | |
| 507 | Cast::~Cast() |
| 508 | { |
| 509 | } |
| 510 | |
| 511 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 512 | Cast::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 513 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 514 | to->Write("((%s)", this->type->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 515 | expression->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 516 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, Type* c) |
| 520 | :lvalue(l), |
| 521 | cast(c), |
| 522 | rvalue(r) |
| 523 | { |
| 524 | } |
| 525 | |
| 526 | VariableDeclaration::VariableDeclaration(Variable* l) |
| 527 | :lvalue(l), |
| 528 | cast(NULL), |
| 529 | rvalue(NULL) |
| 530 | { |
| 531 | } |
| 532 | |
| 533 | VariableDeclaration::~VariableDeclaration() |
| 534 | { |
| 535 | } |
| 536 | |
| 537 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 538 | VariableDeclaration::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 539 | { |
| 540 | this->lvalue->WriteDeclaration(to); |
| 541 | if (this->rvalue != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 542 | to->Write(" = "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 543 | if (this->cast != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 544 | to->Write("(%s)", this->cast->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 545 | } |
| 546 | this->rvalue->Write(to); |
| 547 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 548 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | IfStatement::IfStatement() |
| 552 | :expression(NULL), |
| 553 | statements(new StatementBlock), |
| 554 | elseif(NULL) |
| 555 | { |
| 556 | } |
| 557 | |
| 558 | IfStatement::~IfStatement() |
| 559 | { |
| 560 | } |
| 561 | |
| 562 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 563 | IfStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 564 | { |
| 565 | if (this->expression != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 566 | to->Write("if ("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 567 | this->expression->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 568 | to->Write(") "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 569 | } |
| 570 | this->statements->Write(to); |
| 571 | if (this->elseif != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 572 | to->Write("else "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 573 | this->elseif->Write(to); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | ReturnStatement::ReturnStatement(Expression* e) |
| 578 | :expression(e) |
| 579 | { |
| 580 | } |
| 581 | |
| 582 | ReturnStatement::~ReturnStatement() |
| 583 | { |
| 584 | } |
| 585 | |
| 586 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 587 | ReturnStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 588 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 589 | to->Write("return "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 590 | this->expression->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 591 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | TryStatement::TryStatement() |
| 595 | :statements(new StatementBlock) |
| 596 | { |
| 597 | } |
| 598 | |
| 599 | TryStatement::~TryStatement() |
| 600 | { |
| 601 | } |
| 602 | |
| 603 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 604 | TryStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 605 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 606 | to->Write("try "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 607 | this->statements->Write(to); |
| 608 | } |
| 609 | |
| 610 | CatchStatement::CatchStatement(Variable* e) |
| 611 | :statements(new StatementBlock), |
| 612 | exception(e) |
| 613 | { |
| 614 | } |
| 615 | |
| 616 | CatchStatement::~CatchStatement() |
| 617 | { |
| 618 | } |
| 619 | |
| 620 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 621 | CatchStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 622 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 623 | to->Write("catch "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 624 | if (this->exception != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 625 | to->Write("("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 626 | this->exception->WriteDeclaration(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 627 | to->Write(") "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 628 | } |
| 629 | this->statements->Write(to); |
| 630 | } |
| 631 | |
| 632 | FinallyStatement::FinallyStatement() |
| 633 | :statements(new StatementBlock) |
| 634 | { |
| 635 | } |
| 636 | |
| 637 | FinallyStatement::~FinallyStatement() |
| 638 | { |
| 639 | } |
| 640 | |
| 641 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 642 | FinallyStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 643 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 644 | to->Write("finally "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 645 | this->statements->Write(to); |
| 646 | } |
| 647 | |
| 648 | Case::Case() |
| 649 | :statements(new StatementBlock) |
| 650 | { |
| 651 | } |
| 652 | |
| 653 | Case::Case(const string& c) |
| 654 | :statements(new StatementBlock) |
| 655 | { |
| 656 | cases.push_back(c); |
| 657 | } |
| 658 | |
| 659 | Case::~Case() |
| 660 | { |
| 661 | } |
| 662 | |
| 663 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 664 | Case::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 665 | { |
| 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 671 | to->Write("case %s:\n", s.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 672 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 673 | to->Write("default:\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 677 | to->Write("default:\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 678 | } |
| 679 | statements->Write(to); |
| 680 | } |
| 681 | |
| 682 | SwitchStatement::SwitchStatement(Expression* e) |
| 683 | :expression(e) |
| 684 | { |
| 685 | } |
| 686 | |
| 687 | SwitchStatement::~SwitchStatement() |
| 688 | { |
| 689 | } |
| 690 | |
| 691 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 692 | SwitchStatement::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 693 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 694 | to->Write("switch ("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 695 | this->expression->Write(to); |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 696 | to->Write(")\n{\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 697 | int N = this->cases.size(); |
| 698 | for (int i=0; i<N; i++) { |
| 699 | this->cases[i]->Write(to); |
| 700 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 701 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | Break::Break() |
| 705 | { |
| 706 | } |
| 707 | |
| 708 | Break::~Break() |
| 709 | { |
| 710 | } |
| 711 | |
| 712 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 713 | Break::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 714 | { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 715 | to->Write("break;\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | Method::Method() |
| 719 | :ClassElement(), |
| 720 | modifiers(0), |
| 721 | returnType(NULL), // (NULL means constructor) |
| 722 | returnTypeDimension(0), |
| 723 | statements(NULL) |
| 724 | { |
| 725 | } |
| 726 | |
| 727 | Method::~Method() |
| 728 | { |
| 729 | } |
| 730 | |
| 731 | void |
| 732 | Method::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 | |
| 751 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 752 | Method::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 753 | { |
| 754 | size_t N, i; |
| 755 | |
| 756 | if (this->comment.length() != 0) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 757 | to->Write("%s\n", this->comment.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 758 | } |
| 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 767 | to->Write("%s%s ", this->returnType->QualifiedName().c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 768 | dim.c_str()); |
| 769 | } |
| 770 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 771 | to->Write("%s(", this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 772 | |
| 773 | N = this->parameters.size(); |
| 774 | for (i=0; i<N; i++) { |
| 775 | this->parameters[i]->WriteDeclaration(to); |
| 776 | if (i != N-1) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 777 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 781 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 782 | |
| 783 | N = this->exceptions.size(); |
| 784 | for (i=0; i<N; i++) { |
| 785 | if (i == 0) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 786 | to->Write(" throws "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 787 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 788 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 789 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 790 | to->Write("%s", this->exceptions[i]->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (this->statements == NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 794 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 795 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 796 | to->Write("\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 797 | this->statements->Write(to); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | Class::Class() |
| 802 | :modifiers(0), |
| 803 | what(CLASS), |
| 804 | type(NULL), |
| 805 | extends(NULL) |
| 806 | { |
| 807 | } |
| 808 | |
| 809 | Class::~Class() |
| 810 | { |
| 811 | } |
| 812 | |
| 813 | void |
| 814 | Class::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 | |
| 834 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 835 | Class::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 836 | { |
| 837 | size_t N, i; |
| 838 | |
| 839 | if (this->comment.length() != 0) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 840 | to->Write("%s\n", this->comment.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | WriteModifiers(to, this->modifiers, ALL_MODIFIERS); |
| 844 | |
| 845 | if (this->what == Class::CLASS) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 846 | to->Write("class "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 847 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 848 | to->Write("interface "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 849 | } |
| 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 857 | to->Write("%s", name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 858 | |
| 859 | if (this->extends != NULL) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 860 | to->Write(" extends %s", this->extends->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | N = this->interfaces.size(); |
| 864 | if (N != 0) { |
| 865 | if (this->what == Class::CLASS) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 866 | to->Write(" implements"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 867 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 868 | to->Write(" extends"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 869 | } |
| 870 | for (i=0; i<N; i++) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 871 | to->Write(" %s", this->interfaces[i]->QualifiedName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 875 | to->Write("\n"); |
| 876 | to->Write("{\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 877 | |
| 878 | N = this->elements.size(); |
| 879 | for (i=0; i<N; i++) { |
| 880 | this->elements[i]->Write(to); |
| 881 | } |
| 882 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 883 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 884 | |
| 885 | } |
| 886 | |
| 887 | Document::Document() |
| 888 | { |
| 889 | } |
| 890 | |
| 891 | Document::~Document() |
| 892 | { |
| 893 | } |
| 894 | |
| 895 | static string |
| 896 | escape_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 | |
| 911 | void |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 912 | Document::Write(CodeWriter* to) const |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 913 | { |
| 914 | size_t N, i; |
| 915 | |
| 916 | if (this->comment.length() != 0) { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 917 | to->Write("%s\n", this->comment.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 918 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 919 | to->Write("/*\n" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 920 | " * 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 924 | to->Write("package %s;\n", this->package.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 925 | } |
| 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 Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 934 | } // namespace aidl |
| 935 | } // namespace android |