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 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 68 | void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<Expression>>& arguments) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 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 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 78 | Field::Field(int m, std::shared_ptr<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 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 116 | FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n) |
| 117 | : receiver(o), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 118 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 119 | FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 120 | |
| 121 | void FieldVariable::Write(CodeWriter* to) const { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 122 | visit( |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 123 | overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); }, |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 124 | [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}}, |
| 125 | this->receiver); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 126 | to->Write(".%s", name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 129 | LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {} |
| 130 | |
| 131 | void LiteralStatement::Write(CodeWriter* to) const { |
| 132 | to->Write("%s", value_.c_str()); |
| 133 | } |
| 134 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 135 | void StatementBlock::Write(CodeWriter* to) const { |
| 136 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 137 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 138 | int N = this->statements.size(); |
| 139 | for (int i = 0; i < N; i++) { |
| 140 | this->statements[i]->Write(to); |
| 141 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 142 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 143 | to->Write("}\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 146 | void StatementBlock::Add(std::shared_ptr<Statement> statement) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 147 | this->statements.push_back(statement); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 150 | void StatementBlock::Add(std::shared_ptr<Expression> expression) { |
| 151 | this->statements.push_back(std::make_shared<ExpressionStatement>(expression)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 154 | ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 155 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 156 | void ExpressionStatement::Write(CodeWriter* to) const { |
| 157 | this->expression->Write(to); |
| 158 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 161 | Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r) |
| 162 | : lvalue(l), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 163 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 164 | Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c) |
| 165 | : lvalue(l), rvalue(r), cast(c) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 166 | |
| 167 | void Assignment::Write(CodeWriter* to) const { |
| 168 | this->lvalue->Write(to); |
| 169 | to->Write(" = "); |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 170 | if (this->cast) { |
| 171 | to->Write("(%s)", this->cast->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 172 | } |
| 173 | this->rvalue->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 176 | MethodCall::MethodCall(const string& n) : name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 177 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 178 | MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args) |
| 179 | : name(n), arguments(args) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 180 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 181 | MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 182 | |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 183 | 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] | 184 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 185 | MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n, |
| 186 | const std::vector<std::shared_ptr<Expression>>& args) |
| 187 | : receiver(o), name(n), arguments(args) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 188 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 189 | MethodCall::MethodCall(const std::string& t, const string& n, |
| 190 | const std::vector<std::shared_ptr<Expression>>& args) |
| 191 | : receiver(t), name(n), arguments(args) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 192 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 193 | void MethodCall::Write(CodeWriter* to) const { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 194 | visit( |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 195 | overloaded{[&](std::shared_ptr<Expression> e) { |
Jeongik Cha | 439f2c4 | 2019-02-13 12:38:30 +0900 | [diff] [blame] | 196 | e->Write(to); |
| 197 | to->Write("."); |
| 198 | }, |
| 199 | [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}}, |
| 200 | this->receiver); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 201 | to->Write("%s(", this->name.c_str()); |
| 202 | WriteArgumentList(to, this->arguments); |
| 203 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 206 | Comparison::Comparison(std::shared_ptr<Expression> l, const string& o, |
| 207 | std::shared_ptr<Expression> r) |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 208 | : lvalue(l), op(o), rvalue(r) {} |
| 209 | |
| 210 | void Comparison::Write(CodeWriter* to) const { |
| 211 | to->Write("("); |
| 212 | this->lvalue->Write(to); |
| 213 | to->Write("%s", this->op.c_str()); |
| 214 | this->rvalue->Write(to); |
| 215 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 218 | NewExpression::NewExpression(const std::string& n) : instantiableName(n) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 219 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 220 | NewExpression::NewExpression(const std::string& n, |
| 221 | const std::vector<std::shared_ptr<Expression>>& args) |
| 222 | : instantiableName(n), arguments(args) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 223 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 224 | void NewExpression::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 225 | to->Write("new %s(", this->instantiableName.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 226 | WriteArgumentList(to, this->arguments); |
| 227 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 230 | NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s) |
| 231 | : type(t), size(s) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 232 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 233 | void NewArrayExpression::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 234 | to->Write("new %s[", this->type.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 235 | size->Write(to); |
| 236 | to->Write("]"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 239 | Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 240 | |
| 241 | void Cast::Write(CodeWriter* to) const { |
Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 242 | to->Write("((%s)", this->type.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 243 | expression->Write(to); |
| 244 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 247 | VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r) |
| 248 | : lvalue(l), rvalue(r) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 249 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 250 | VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 251 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 252 | void VariableDeclaration::Write(CodeWriter* to) const { |
| 253 | this->lvalue->WriteDeclaration(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 254 | if (this->rvalue != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 255 | to->Write(" = "); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 256 | this->rvalue->Write(to); |
| 257 | } |
| 258 | to->Write(";\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 261 | void IfStatement::Write(CodeWriter* to) const { |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 262 | if (this->expression != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 263 | to->Write("if ("); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 264 | this->expression->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 265 | to->Write(") "); |
| 266 | } |
| 267 | this->statements->Write(to); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 268 | if (this->elseif != nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 269 | to->Write("else "); |
| 270 | this->elseif->Write(to); |
| 271 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 274 | ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 275 | |
| 276 | void ReturnStatement::Write(CodeWriter* to) const { |
| 277 | to->Write("return "); |
| 278 | this->expression->Write(to); |
| 279 | to->Write(";\n"); |
| 280 | } |
| 281 | |
| 282 | void TryStatement::Write(CodeWriter* to) const { |
| 283 | to->Write("try "); |
| 284 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 287 | void FinallyStatement::Write(CodeWriter* to) const { |
| 288 | to->Write("finally "); |
| 289 | this->statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 292 | Case::Case(const string& c) { cases.push_back(c); } |
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 | void Case::Write(CodeWriter* to) const { |
| 295 | int N = this->cases.size(); |
| 296 | if (N > 0) { |
| 297 | for (int i = 0; i < N; i++) { |
| 298 | string s = this->cases[i]; |
| 299 | if (s.length() != 0) { |
| 300 | to->Write("case %s:\n", s.c_str()); |
| 301 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 302 | to->Write("default:\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 303 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 304 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 305 | } else { |
| 306 | to->Write("default:\n"); |
| 307 | } |
| 308 | statements->Write(to); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 311 | SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {} |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 312 | |
| 313 | void SwitchStatement::Write(CodeWriter* to) const { |
| 314 | to->Write("switch ("); |
| 315 | this->expression->Write(to); |
| 316 | to->Write(")\n{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 317 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 318 | int N = this->cases.size(); |
| 319 | for (int i = 0; i < N; i++) { |
| 320 | this->cases[i]->Write(to); |
| 321 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 322 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 323 | to->Write("}\n"); |
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 | void Method::Write(CodeWriter* to) const { |
| 327 | size_t N, i; |
| 328 | |
| 329 | if (this->comment.length() != 0) { |
| 330 | to->Write("%s\n", this->comment.c_str()); |
| 331 | } |
| 332 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 333 | for (const auto& a : this->annotations) { |
| 334 | to->Write("%s\n", a.c_str()); |
| 335 | } |
| 336 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 337 | WriteModifiers(to, this->modifiers, |
| 338 | SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE); |
| 339 | |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 340 | if (this->returnType) { |
Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 341 | to->Write("%s ", this->returnType->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 342 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 343 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 344 | to->Write("%s(", this->name.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 345 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 346 | N = this->parameters.size(); |
| 347 | for (i = 0; i < N; i++) { |
| 348 | this->parameters[i]->WriteDeclaration(to); |
| 349 | if (i != N - 1) { |
| 350 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 351 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 352 | } |
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 | to->Write(")"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 355 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 356 | N = this->exceptions.size(); |
| 357 | for (i = 0; i < N; i++) { |
| 358 | if (i == 0) { |
| 359 | to->Write(" throws "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 360 | } else { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 361 | to->Write(", "); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 362 | } |
Jeongik Cha | 6cadc21 | 2019-02-12 18:16:03 +0900 | [diff] [blame] | 363 | to->Write("%s", this->exceptions[i].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 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 366 | if (this->statements == nullptr) { |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 367 | to->Write(";\n"); |
| 368 | } else { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 369 | to->Write("\n"); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 370 | this->statements->Write(to); |
| 371 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 374 | void LiteralClassElement::Write(CodeWriter* to) const { |
| 375 | to->Write("%s", element.c_str()); |
| 376 | } |
| 377 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 378 | void Class::Write(CodeWriter* to) const { |
| 379 | size_t N, i; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 380 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 381 | if (this->comment.length() != 0) { |
| 382 | to->Write("%s\n", this->comment.c_str()); |
| 383 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 384 | for (const auto& a : this->annotations) { |
| 385 | to->Write("%s\n", a.c_str()); |
| 386 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 387 | |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 388 | WriteModifiers(to, this->modifiers, ALL_MODIFIERS); |
| 389 | |
| 390 | if (this->what == Class::CLASS) { |
| 391 | to->Write("class "); |
| 392 | } else { |
| 393 | to->Write("interface "); |
| 394 | } |
| 395 | |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 396 | string name = this->type; |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 397 | size_t pos = name.rfind('.'); |
| 398 | if (pos != string::npos) { |
| 399 | name = name.c_str() + pos + 1; |
| 400 | } |
| 401 | |
| 402 | to->Write("%s", name.c_str()); |
| 403 | |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 404 | if (this->extends) { |
| 405 | to->Write(" extends %s", this->extends->c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | N = this->interfaces.size(); |
| 409 | if (N != 0) { |
| 410 | if (this->what == Class::CLASS) { |
| 411 | to->Write(" implements"); |
| 412 | } else { |
| 413 | to->Write(" extends"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 414 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 415 | for (i = 0; i < N; i++) { |
Jeongik Cha | aabc144 | 2019-02-12 17:44:48 +0900 | [diff] [blame] | 416 | to->Write(" %s", this->interfaces[i].c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | to->Write("\n"); |
| 421 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 422 | to->Indent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 423 | |
| 424 | N = this->elements.size(); |
| 425 | for (i = 0; i < N; i++) { |
| 426 | this->elements[i]->Write(to); |
| 427 | } |
| 428 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 429 | to->Dedent(); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 430 | to->Write("}\n"); |
| 431 | } |
| 432 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 433 | Document::Document(const std::string& comment, |
| 434 | const std::string& package, |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 435 | std::unique_ptr<Class> clazz) |
| 436 | : comment_(comment), |
| 437 | package_(package), |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 438 | clazz_(std::move(clazz)) { |
| 439 | } |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 440 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 441 | void Document::Write(CodeWriter* to) const { |
| 442 | if (!comment_.empty()) { |
| 443 | to->Write("%s\n", comment_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 444 | } |
| 445 | to->Write( |
| 446 | "/*\n" |
| 447 | " * This file is auto-generated. DO NOT MODIFY.\n" |
Jiyong Park | 54fa1e0 | 2019-02-08 10:03:20 +0900 | [diff] [blame] | 448 | " */\n"); |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 449 | if (!package_.empty()) { |
| 450 | to->Write("package %s;\n", package_.c_str()); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 453 | if (clazz_) { |
| 454 | clazz_->Write(to); |
Christopher Wiley | ae58997 | 2016-01-29 11:19:23 -0800 | [diff] [blame] | 455 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 458 | std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null"); |
| 459 | std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this"); |
| 460 | std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super"); |
| 461 | std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true"); |
| 462 | std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false"); |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 463 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 464 | } // namespace aidl |
| 465 | } // namespace android |