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