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