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