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