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 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 79 | for (const auto& a : this->annotations) { |
| 80 | to->Write("%s\n", a.c_str()); |
| 81 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 82 | WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE); |
Steven Moreland | 5635a8a | 2018-07-03 11:28:20 -0700 | [diff] [blame] | 83 | this->variable->WriteDeclaration(to); |
| 84 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 85 | if (this->value.length() != 0) { |
| 86 | to->Write(" = %s", this->value.c_str()); |
| 87 | } |
| 88 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 91 | LiteralExpression::LiteralExpression(const string& v) : value(v) {} |
| 92 | |
| 93 | void LiteralExpression::Write(CodeWriter* to) const { |
| 94 | to->Write("%s", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 97 | StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 98 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 99 | void StringLiteralExpression::Write(CodeWriter* to) const { |
| 100 | to->Write("\"%s\"", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jeongik Cha | dc77c1b | 2019-02-12 16:13:25 +0900 | [diff] [blame^] | 103 | Variable::Variable(const string& t, const string& n) : type(t), name(n), dimension(0) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 104 | |
Jeongik Cha | dc77c1b | 2019-02-12 16:13:25 +0900 | [diff] [blame^] | 105 | Variable::Variable(const string& t, const string& n, int d) : type(t), name(n), dimension(d) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 106 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 107 | void Variable::WriteDeclaration(CodeWriter* to) const { |
| 108 | string dim; |
| 109 | for (int i = 0; i < this->dimension; i++) { |
| 110 | dim += "[]"; |
| 111 | } |
Jeongik Cha | dc77c1b | 2019-02-12 16:13:25 +0900 | [diff] [blame^] | 112 | to->Write("%s%s %s", this->type.c_str(), dim.c_str(), this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 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) |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 118 | : object(o), clazz(nullptr), 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) |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 121 | : object(nullptr), clazz(c), name(n) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 122 | |
| 123 | void FieldVariable::Write(CodeWriter* to) const { |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 124 | if (this->object != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 125 | this->object->Write(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 126 | } else if (this->clazz != nullptr) { |
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) |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 165 | : lvalue(l), rvalue(r), cast(nullptr) {} |
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(" = "); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 173 | if (this->cast != nullptr) { |
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 { |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 216 | if (this->obj != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 217 | this->obj->Write(to); |
| 218 | to->Write("."); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 219 | } else if (this->clazz != nullptr) { |
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 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 269 | Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {} |
| 270 | |
| 271 | void Cast::Write(CodeWriter* to) const { |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 272 | to->Write("((%s)", this->type->JavaType().c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 273 | expression->Write(to); |
| 274 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 277 | VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, |
| 278 | const Type* c) |
| 279 | : lvalue(l), cast(c), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 280 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 281 | VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 282 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 283 | void VariableDeclaration::Write(CodeWriter* to) const { |
| 284 | this->lvalue->WriteDeclaration(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 285 | if (this->rvalue != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 286 | to->Write(" = "); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 287 | if (this->cast != nullptr) { |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 288 | to->Write("(%s)", this->cast->JavaType().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 289 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 290 | this->rvalue->Write(to); |
| 291 | } |
| 292 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 295 | void IfStatement::Write(CodeWriter* to) const { |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 296 | if (this->expression != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 297 | to->Write("if ("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 298 | this->expression->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 299 | to->Write(") "); |
| 300 | } |
| 301 | this->statements->Write(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 302 | if (this->elseif != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 303 | to->Write("else "); |
| 304 | this->elseif->Write(to); |
| 305 | } |
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 | ReturnStatement::ReturnStatement(Expression* e) : expression(e) {} |
| 309 | |
| 310 | void ReturnStatement::Write(CodeWriter* to) const { |
| 311 | to->Write("return "); |
| 312 | this->expression->Write(to); |
| 313 | to->Write(";\n"); |
| 314 | } |
| 315 | |
| 316 | void TryStatement::Write(CodeWriter* to) const { |
| 317 | to->Write("try "); |
| 318 | this->statements->Write(to); |
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 | void FinallyStatement::Write(CodeWriter* to) const { |
| 322 | to->Write("finally "); |
| 323 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 326 | Case::Case(const string& c) { cases.push_back(c); } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 327 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 328 | void Case::Write(CodeWriter* to) const { |
| 329 | int N = this->cases.size(); |
| 330 | if (N > 0) { |
| 331 | for (int i = 0; i < N; i++) { |
| 332 | string s = this->cases[i]; |
| 333 | if (s.length() != 0) { |
| 334 | to->Write("case %s:\n", s.c_str()); |
| 335 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 336 | to->Write("default:\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 337 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 338 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 339 | } else { |
| 340 | to->Write("default:\n"); |
| 341 | } |
| 342 | statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 345 | SwitchStatement::SwitchStatement(Expression* e) : expression(e) {} |
| 346 | |
| 347 | void SwitchStatement::Write(CodeWriter* to) const { |
| 348 | to->Write("switch ("); |
| 349 | this->expression->Write(to); |
| 350 | to->Write(")\n{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 351 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 352 | int N = this->cases.size(); |
| 353 | for (int i = 0; i < N; i++) { |
| 354 | this->cases[i]->Write(to); |
| 355 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 356 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 357 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 360 | void Method::Write(CodeWriter* to) const { |
| 361 | size_t N, i; |
| 362 | |
| 363 | if (this->comment.length() != 0) { |
| 364 | to->Write("%s\n", this->comment.c_str()); |
| 365 | } |
| 366 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 367 | for (const auto& a : this->annotations) { |
| 368 | to->Write("%s\n", a.c_str()); |
| 369 | } |
| 370 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 371 | WriteModifiers(to, this->modifiers, |
| 372 | SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE); |
| 373 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 374 | if (this->returnType != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 375 | string dim; |
| 376 | for (i = 0; i < this->returnTypeDimension; i++) { |
| 377 | dim += "[]"; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 378 | } |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 379 | to->Write("%s%s ", this->returnType->JavaType().c_str(), dim.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 380 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 381 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 382 | to->Write("%s(", this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 383 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 384 | N = this->parameters.size(); |
| 385 | for (i = 0; i < N; i++) { |
| 386 | this->parameters[i]->WriteDeclaration(to); |
| 387 | if (i != N - 1) { |
| 388 | to->Write(", "); |
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 | } |
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 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 393 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 394 | N = this->exceptions.size(); |
| 395 | for (i = 0; i < N; i++) { |
| 396 | if (i == 0) { |
| 397 | to->Write(" throws "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 398 | } else { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 399 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 400 | } |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 401 | to->Write("%s", this->exceptions[i]->JavaType().c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 402 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 403 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 404 | if (this->statements == nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 405 | to->Write(";\n"); |
| 406 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 407 | to->Write("\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 408 | this->statements->Write(to); |
| 409 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 412 | void LiteralClassElement::Write(CodeWriter* to) const { |
| 413 | to->Write("%s", element.c_str()); |
| 414 | } |
| 415 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 416 | void Class::Write(CodeWriter* to) const { |
| 417 | size_t N, i; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 418 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 419 | if (this->comment.length() != 0) { |
| 420 | to->Write("%s\n", this->comment.c_str()); |
| 421 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 422 | for (const auto& a : this->annotations) { |
| 423 | to->Write("%s\n", a.c_str()); |
| 424 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 425 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 426 | WriteModifiers(to, this->modifiers, ALL_MODIFIERS); |
| 427 | |
| 428 | if (this->what == Class::CLASS) { |
| 429 | to->Write("class "); |
| 430 | } else { |
| 431 | to->Write("interface "); |
| 432 | } |
| 433 | |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 434 | string name = this->type->JavaType(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 435 | size_t pos = name.rfind('.'); |
| 436 | if (pos != string::npos) { |
| 437 | name = name.c_str() + pos + 1; |
| 438 | } |
| 439 | |
| 440 | to->Write("%s", name.c_str()); |
| 441 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 442 | if (this->extends != nullptr) { |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 443 | to->Write(" extends %s", this->extends->JavaType().c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | N = this->interfaces.size(); |
| 447 | if (N != 0) { |
| 448 | if (this->what == Class::CLASS) { |
| 449 | to->Write(" implements"); |
| 450 | } else { |
| 451 | to->Write(" extends"); |
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 | for (i = 0; i < N; i++) { |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 454 | to->Write(" %s", this->interfaces[i]->JavaType().c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | to->Write("\n"); |
| 459 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 460 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 461 | |
| 462 | N = this->elements.size(); |
| 463 | for (i = 0; i < N; i++) { |
| 464 | this->elements[i]->Write(to); |
| 465 | } |
| 466 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 467 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 468 | to->Write("}\n"); |
| 469 | } |
| 470 | |
| 471 | static string escape_backslashes(const string& str) { |
| 472 | string result; |
| 473 | const size_t I = str.length(); |
| 474 | for (size_t i = 0; i < I; i++) { |
| 475 | char c = str[i]; |
| 476 | if (c == '\\') { |
| 477 | result += "\\\\"; |
| 478 | } else { |
| 479 | result += c; |
| 480 | } |
| 481 | } |
| 482 | return result; |
| 483 | } |
| 484 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 485 | Document::Document(const std::string& comment, |
| 486 | const std::string& package, |
| 487 | const std::string& original_src, |
| 488 | std::unique_ptr<Class> clazz) |
| 489 | : comment_(comment), |
| 490 | package_(package), |
| 491 | original_src_(original_src), |
| 492 | clazz_(std::move(clazz)) { |
| 493 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 494 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 495 | void Document::Write(CodeWriter* to) const { |
| 496 | if (!comment_.empty()) { |
| 497 | to->Write("%s\n", comment_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 498 | } |
| 499 | to->Write( |
| 500 | "/*\n" |
| 501 | " * This file is auto-generated. DO NOT MODIFY.\n" |
| 502 | " * Original file: %s\n" |
| 503 | " */\n", |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 504 | escape_backslashes(original_src_).c_str()); |
| 505 | if (!package_.empty()) { |
| 506 | to->Write("package %s;\n", package_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 509 | if (clazz_) { |
| 510 | clazz_->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 511 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 514 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 515 | } // namespace aidl |
| 516 | } // namespace android |