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" |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 18 | #include "code_writer.h" |
| 19 | |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 20 | using std::vector; |
| 21 | using std::string; |
| 22 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 23 | template <class... Ts> |
| 24 | struct overloaded : Ts... { |
| 25 | using Ts::operator()...; |
| 26 | }; |
| 27 | template <class... Ts> |
| 28 | overloaded(Ts...)->overloaded<Ts...>; |
| 29 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace aidl { |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 32 | namespace java { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 33 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 34 | std::string AstNode::ToString() { |
| 35 | std::string str; |
| 36 | Write(CodeWriter::ForString(&str).get()); |
| 37 | return str; |
| 38 | } |
| 39 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 40 | void WriteModifiers(CodeWriter* to, int mod, int mask) { |
| 41 | int m = mod & mask; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 42 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 43 | if (m & OVERRIDE) { |
| 44 | to->Write("@Override "); |
| 45 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 46 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 47 | if ((m & SCOPE_MASK) == PUBLIC) { |
| 48 | to->Write("public "); |
| 49 | } else if ((m & SCOPE_MASK) == PRIVATE) { |
| 50 | to->Write("private "); |
| 51 | } else if ((m & SCOPE_MASK) == PROTECTED) { |
| 52 | to->Write("protected "); |
| 53 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 54 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 55 | if (m & STATIC) { |
| 56 | to->Write("static "); |
| 57 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 58 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 59 | if (m & FINAL) { |
| 60 | to->Write("final "); |
| 61 | } |
| 62 | |
| 63 | if (m & ABSTRACT) { |
| 64 | to->Write("abstract "); |
| 65 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 68 | void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) { |
| 69 | size_t N = arguments.size(); |
| 70 | for (size_t i = 0; i < N; i++) { |
| 71 | arguments[i]->Write(to); |
| 72 | if (i != N - 1) { |
| 73 | to->Write(", "); |
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 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 78 | Field::Field(int m, Variable* v) : ClassElement(), modifiers(m), variable(v) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 79 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 80 | void Field::Write(CodeWriter* to) const { |
| 81 | if (this->comment.length() != 0) { |
| 82 | to->Write("%s\n", this->comment.c_str()); |
| 83 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 84 | for (const auto& a : this->annotations) { |
| 85 | to->Write("%s\n", a.c_str()); |
| 86 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 87 | WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE); |
Steven Moreland | 5635a8a | 2018-07-03 11:28:20 -0700 | [diff] [blame] | 88 | this->variable->WriteDeclaration(to); |
| 89 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 90 | if (this->value.length() != 0) { |
| 91 | to->Write(" = %s", this->value.c_str()); |
| 92 | } |
| 93 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 96 | LiteralExpression::LiteralExpression(const string& v) : value(v) {} |
| 97 | |
| 98 | void LiteralExpression::Write(CodeWriter* to) const { |
| 99 | to->Write("%s", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 102 | StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 103 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 104 | void StringLiteralExpression::Write(CodeWriter* to) const { |
| 105 | to->Write("\"%s\"", this->value.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 108 | Variable::Variable(const string& t, const string& n) : type(t), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 109 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 110 | void Variable::WriteDeclaration(CodeWriter* to) const { |
Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 111 | to->Write("%s %s", this->type.c_str(), this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 114 | void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 115 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 116 | FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 117 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 118 | FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 119 | |
| 120 | void FieldVariable::Write(CodeWriter* to) const { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 121 | visit( |
| 122 | overloaded{[&](Expression* e) { e->Write(to); }, |
| 123 | [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}}, |
| 124 | this->receiver); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 125 | to->Write(".%s", name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 128 | LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {} |
| 129 | |
| 130 | void LiteralStatement::Write(CodeWriter* to) const { |
| 131 | to->Write("%s", value_.c_str()); |
| 132 | } |
| 133 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 134 | void StatementBlock::Write(CodeWriter* to) const { |
| 135 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 136 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 137 | int N = this->statements.size(); |
| 138 | for (int i = 0; i < N; i++) { |
| 139 | this->statements[i]->Write(to); |
| 140 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 141 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 142 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 145 | void StatementBlock::Add(Statement* statement) { |
| 146 | this->statements.push_back(statement); |
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(Expression* expression) { |
| 150 | this->statements.push_back(new ExpressionStatement(expression)); |
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 | ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 154 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 155 | void ExpressionStatement::Write(CodeWriter* to) const { |
| 156 | this->expression->Write(to); |
| 157 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 160 | Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 161 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 162 | Assignment::Assignment(Variable* l, Expression* r, string c) : lvalue(l), rvalue(r), cast(c) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 163 | |
| 164 | void Assignment::Write(CodeWriter* to) const { |
| 165 | this->lvalue->Write(to); |
| 166 | to->Write(" = "); |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 167 | if (this->cast) { |
| 168 | to->Write("(%s)", this->cast->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 169 | } |
| 170 | this->rvalue->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 173 | MethodCall::MethodCall(const string& n) : name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 174 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 175 | MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 176 | va_list args; |
| 177 | va_start(args, argc); |
| 178 | init(argc, args); |
| 179 | va_end(args); |
| 180 | } |
| 181 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 182 | MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 183 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 184 | MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 185 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 186 | MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 187 | va_list args; |
| 188 | va_start(args, argc); |
| 189 | init(argc, args); |
| 190 | va_end(args); |
| 191 | } |
| 192 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 193 | MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...) |
| 194 | : receiver(t), name(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 195 | va_list args; |
| 196 | va_start(args, argc); |
| 197 | init(argc, args); |
| 198 | va_end(args); |
| 199 | } |
| 200 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 201 | void MethodCall::init(int n, va_list args) { |
| 202 | for (int i = 0; i < n; i++) { |
| 203 | Expression* expression = (Expression*)va_arg(args, void*); |
| 204 | this->arguments.push_back(expression); |
| 205 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 208 | void MethodCall::Write(CodeWriter* to) const { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 209 | visit( |
| 210 | overloaded{[&](Expression* e) { |
| 211 | e->Write(to); |
| 212 | to->Write("."); |
| 213 | }, |
| 214 | [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}}, |
| 215 | this->receiver); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 216 | to->Write("%s(", this->name.c_str()); |
| 217 | WriteArgumentList(to, this->arguments); |
| 218 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | Comparison::Comparison(Expression* l, const string& o, Expression* r) |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 222 | : lvalue(l), op(o), rvalue(r) {} |
| 223 | |
| 224 | void Comparison::Write(CodeWriter* to) const { |
| 225 | to->Write("("); |
| 226 | this->lvalue->Write(to); |
| 227 | to->Write("%s", this->op.c_str()); |
| 228 | this->rvalue->Write(to); |
| 229 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 232 | NewExpression::NewExpression(const std::string& n) : instantiableName(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 233 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 234 | NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 235 | va_list args; |
| 236 | va_start(args, argc); |
| 237 | init(argc, args); |
| 238 | va_end(args); |
| 239 | } |
| 240 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 241 | void NewExpression::init(int n, va_list args) { |
| 242 | for (int i = 0; i < n; i++) { |
| 243 | Expression* expression = (Expression*)va_arg(args, void*); |
| 244 | this->arguments.push_back(expression); |
| 245 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 248 | void NewExpression::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 249 | to->Write("new %s(", this->instantiableName.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 250 | WriteArgumentList(to, this->arguments); |
| 251 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 254 | NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 255 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 256 | void NewArrayExpression::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 257 | to->Write("new %s[", this->type.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 258 | size->Write(to); |
| 259 | to->Write("]"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 262 | Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 263 | |
| 264 | void Cast::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 265 | to->Write("((%s)", this->type.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 266 | expression->Write(to); |
| 267 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 270 | VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 271 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 272 | VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 273 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 274 | void VariableDeclaration::Write(CodeWriter* to) const { |
| 275 | this->lvalue->WriteDeclaration(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 276 | if (this->rvalue != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 277 | to->Write(" = "); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 278 | this->rvalue->Write(to); |
| 279 | } |
| 280 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 283 | void IfStatement::Write(CodeWriter* to) const { |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 284 | if (this->expression != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 285 | to->Write("if ("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 286 | this->expression->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 287 | to->Write(") "); |
| 288 | } |
| 289 | this->statements->Write(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 290 | if (this->elseif != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 291 | to->Write("else "); |
| 292 | this->elseif->Write(to); |
| 293 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 296 | ReturnStatement::ReturnStatement(Expression* e) : expression(e) {} |
| 297 | |
| 298 | void ReturnStatement::Write(CodeWriter* to) const { |
| 299 | to->Write("return "); |
| 300 | this->expression->Write(to); |
| 301 | to->Write(";\n"); |
| 302 | } |
| 303 | |
| 304 | void TryStatement::Write(CodeWriter* to) const { |
| 305 | to->Write("try "); |
| 306 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 309 | void FinallyStatement::Write(CodeWriter* to) const { |
| 310 | to->Write("finally "); |
| 311 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 314 | Case::Case(const string& c) { cases.push_back(c); } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 315 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 316 | void Case::Write(CodeWriter* to) const { |
| 317 | int N = this->cases.size(); |
| 318 | if (N > 0) { |
| 319 | for (int i = 0; i < N; i++) { |
| 320 | string s = this->cases[i]; |
| 321 | if (s.length() != 0) { |
| 322 | to->Write("case %s:\n", s.c_str()); |
| 323 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 324 | to->Write("default:\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 325 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 326 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 327 | } else { |
| 328 | to->Write("default:\n"); |
| 329 | } |
| 330 | 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 | SwitchStatement::SwitchStatement(Expression* e) : expression(e) {} |
| 334 | |
| 335 | void SwitchStatement::Write(CodeWriter* to) const { |
| 336 | to->Write("switch ("); |
| 337 | this->expression->Write(to); |
| 338 | to->Write(")\n{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 339 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 340 | int N = this->cases.size(); |
| 341 | for (int i = 0; i < N; i++) { |
| 342 | this->cases[i]->Write(to); |
| 343 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 344 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 345 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 348 | void Method::Write(CodeWriter* to) const { |
| 349 | size_t N, i; |
| 350 | |
| 351 | if (this->comment.length() != 0) { |
| 352 | to->Write("%s\n", this->comment.c_str()); |
| 353 | } |
| 354 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 355 | for (const auto& a : this->annotations) { |
| 356 | to->Write("%s\n", a.c_str()); |
| 357 | } |
| 358 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 359 | WriteModifiers(to, this->modifiers, |
| 360 | SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE); |
| 361 | |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 362 | if (this->returnType) { |
Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 363 | to->Write("%s ", this->returnType->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 364 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 365 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 366 | to->Write("%s(", this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 367 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 368 | N = this->parameters.size(); |
| 369 | for (i = 0; i < N; i++) { |
| 370 | this->parameters[i]->WriteDeclaration(to); |
| 371 | if (i != N - 1) { |
| 372 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 373 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 374 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 375 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 376 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 377 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 378 | N = this->exceptions.size(); |
| 379 | for (i = 0; i < N; i++) { |
| 380 | if (i == 0) { |
| 381 | to->Write(" throws "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 382 | } else { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 383 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 384 | } |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 385 | to->Write("%s", this->exceptions[i].c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 386 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 387 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 388 | if (this->statements == nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 389 | to->Write(";\n"); |
| 390 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 391 | to->Write("\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 392 | this->statements->Write(to); |
| 393 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 396 | void LiteralClassElement::Write(CodeWriter* to) const { |
| 397 | to->Write("%s", element.c_str()); |
| 398 | } |
| 399 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 400 | void Class::Write(CodeWriter* to) const { |
| 401 | size_t N, i; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 402 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 403 | if (this->comment.length() != 0) { |
| 404 | to->Write("%s\n", this->comment.c_str()); |
| 405 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 406 | for (const auto& a : this->annotations) { |
| 407 | to->Write("%s\n", a.c_str()); |
| 408 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 409 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 410 | WriteModifiers(to, this->modifiers, ALL_MODIFIERS); |
| 411 | |
| 412 | if (this->what == Class::CLASS) { |
| 413 | to->Write("class "); |
| 414 | } else { |
| 415 | to->Write("interface "); |
| 416 | } |
| 417 | |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 418 | string name = this->type; |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 419 | size_t pos = name.rfind('.'); |
| 420 | if (pos != string::npos) { |
| 421 | name = name.c_str() + pos + 1; |
| 422 | } |
| 423 | |
| 424 | to->Write("%s", name.c_str()); |
| 425 | |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 426 | if (this->extends) { |
| 427 | to->Write(" extends %s", this->extends->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | N = this->interfaces.size(); |
| 431 | if (N != 0) { |
| 432 | if (this->what == Class::CLASS) { |
| 433 | to->Write(" implements"); |
| 434 | } else { |
| 435 | to->Write(" extends"); |
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 | for (i = 0; i < N; i++) { |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 438 | to->Write(" %s", this->interfaces[i].c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | to->Write("\n"); |
| 443 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 444 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 445 | |
| 446 | N = this->elements.size(); |
| 447 | for (i = 0; i < N; i++) { |
| 448 | this->elements[i]->Write(to); |
| 449 | } |
| 450 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 451 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 452 | to->Write("}\n"); |
| 453 | } |
| 454 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 455 | Document::Document(const std::string& comment, |
| 456 | const std::string& package, |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 457 | std::unique_ptr<Class> clazz) |
| 458 | : comment_(comment), |
| 459 | package_(package), |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 460 | clazz_(std::move(clazz)) { |
| 461 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 462 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 463 | void Document::Write(CodeWriter* to) const { |
| 464 | if (!comment_.empty()) { |
| 465 | to->Write("%s\n", comment_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 466 | } |
| 467 | to->Write( |
| 468 | "/*\n" |
| 469 | " * This file is auto-generated. DO NOT MODIFY.\n" |
Jiyong Park | 54fa1e0 | 2019-02-08 10:03:20 +0900 | [diff] [blame] | 470 | " */\n"); |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 471 | if (!package_.empty()) { |
| 472 | to->Write("package %s;\n", package_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 475 | if (clazz_) { |
| 476 | clazz_->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 477 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 480 | Expression* NULL_VALUE = new LiteralExpression("null"); |
| 481 | Expression* THIS_VALUE = new LiteralExpression("this"); |
| 482 | Expression* SUPER_VALUE = new LiteralExpression("super"); |
| 483 | Expression* TRUE_VALUE = new LiteralExpression("true"); |
| 484 | Expression* FALSE_VALUE = new LiteralExpression("false"); |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 485 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 486 | } // namespace aidl |
| 487 | } // namespace android |