Christopher Wiley | da69599 | 2015-10-05 11:31:41 -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 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 17 | #include "ast_cpp.h" |
| 18 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 21 | #include "code_writer.h" |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 22 | #include "logging.h" |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 23 | |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 24 | using std::string; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 25 | using std::unique_ptr; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 26 | using std::vector; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 27 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace aidl { |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 30 | namespace cpp { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 31 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame^] | 32 | LiteralDecl::LiteralDecl(const std::string& expression) : expression_(expression) {} |
| 33 | |
| 34 | void LiteralDecl::Write(CodeWriter* to) const { |
| 35 | to->Write("%s", expression_.c_str()); |
| 36 | } |
| 37 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 38 | ClassDecl::ClassDecl(const std::string& name, const std::string& parent) |
| 39 | : name_(name), |
| 40 | parent_(parent) {} |
| 41 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 42 | ClassDecl::ClassDecl(const std::string& name, const std::string& parent, |
| 43 | std::vector<unique_ptr<Declaration>> public_members, |
| 44 | std::vector<unique_ptr<Declaration>> private_members) |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 45 | : name_(name), |
| 46 | parent_(parent), |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 47 | public_members_(std::move(public_members)), |
| 48 | private_members_(std::move(private_members)) {} |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 49 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 50 | void ClassDecl::Write(CodeWriter* to) const { |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 51 | to->Write("class %s ", name_.c_str()); |
| 52 | |
| 53 | if (parent_.length() > 0) |
| 54 | to->Write(": public %s ", parent_.c_str()); |
| 55 | |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 56 | to->Write("{\n"); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 57 | |
| 58 | if (!public_members_.empty()) |
| 59 | to->Write("public:\n"); |
| 60 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 61 | to->Indent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 62 | for (const auto& dec : public_members_) |
| 63 | dec->Write(to); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 64 | to->Dedent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 65 | |
| 66 | if (!private_members_.empty()) |
| 67 | to->Write("private:\n"); |
| 68 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 69 | to->Indent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 70 | for (const auto& dec : private_members_) |
| 71 | dec->Write(to); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 72 | to->Dedent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 73 | |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 74 | to->Write("}; // class %s\n", name_.c_str()); |
| 75 | } |
| 76 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 77 | void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) { |
| 78 | public_members_.push_back(std::move(member)); |
| 79 | } |
| 80 | |
| 81 | void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) { |
| 82 | private_members_.push_back(std::move(member)); |
| 83 | } |
| 84 | |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 85 | Enum::EnumField::EnumField(const string& k, const string& v) |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 86 | : key(k), |
| 87 | value(v) {} |
| 88 | |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 89 | Enum::Enum(const string& name, const string& base_type) |
| 90 | : enum_name_(name), underlying_type_(base_type) {} |
| 91 | |
| 92 | Enum::Enum(const string& name) : Enum(name, "") {} |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 93 | |
| 94 | void Enum::Write(CodeWriter* to) const { |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 95 | if (underlying_type_.empty()) { |
| 96 | to->Write("enum %s {\n", enum_name_.c_str()); |
| 97 | } else { |
| 98 | to->Write("enum %s : %s {\n", enum_name_.c_str(), underlying_type_.c_str()); |
| 99 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 100 | to->Indent(); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 101 | for (const auto& field : fields_) { |
| 102 | if (field.value.empty()) { |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 103 | to->Write("%s,\n", field.key.c_str()); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 104 | } else { |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 105 | to->Write("%s = %s,\n", field.key.c_str(), field.value.c_str()); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 108 | to->Dedent(); |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 109 | to->Write("};\n"); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void Enum::AddValue(const string& key, const string& value) { |
| 113 | fields_.emplace_back(key, value); |
| 114 | } |
| 115 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 116 | ArgList::ArgList(const std::string& single_argument) |
| 117 | : ArgList(vector<string>{single_argument}) {} |
| 118 | |
Christopher Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 119 | ArgList::ArgList(const std::vector<std::string>& arg_list) { |
| 120 | for (const auto& s : arg_list) { |
| 121 | arguments_.emplace_back(new LiteralExpression(s)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list) |
| 126 | : arguments_(std::move(arg_list)) {} |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 127 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 128 | ArgList::ArgList(ArgList&& arg_list) |
| 129 | : arguments_(std::move(arg_list.arguments_)) {} |
| 130 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 131 | void ArgList::Write(CodeWriter* to) const { |
| 132 | to->Write("("); |
| 133 | bool is_first = true; |
| 134 | for (const auto& s : arguments_) { |
| 135 | if (!is_first) { to->Write(", "); } |
| 136 | is_first = false; |
Christopher Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 137 | s->Write(to); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 138 | } |
| 139 | to->Write(")"); |
| 140 | } |
| 141 | |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 142 | ConstructorDecl::ConstructorDecl( |
| 143 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 144 | ArgList&& arg_list) |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 145 | : ConstructorDecl(name, std::move(arg_list), 0u) {} |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 146 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 147 | ConstructorDecl::ConstructorDecl( |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 148 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 149 | ArgList&& arg_list, |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 150 | uint32_t modifiers) |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 151 | : name_(name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 152 | arguments_(std::move(arg_list)), |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 153 | modifiers_(modifiers) {} |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 154 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 155 | void ConstructorDecl::Write(CodeWriter* to) const { |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 156 | if (modifiers_ & Modifiers::IS_VIRTUAL) |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 157 | to->Write("virtual "); |
| 158 | |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 159 | if (modifiers_ & Modifiers::IS_EXPLICIT) |
| 160 | to->Write("explicit "); |
| 161 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 162 | to->Write("%s", name_.c_str()); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 163 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 164 | arguments_.Write(to); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 165 | |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 166 | if (modifiers_ & Modifiers::IS_DEFAULT) |
Christopher Wiley | f094d58 | 2015-10-08 15:50:15 -0700 | [diff] [blame] | 167 | to->Write(" = default"); |
| 168 | |
| 169 | to->Write(";\n"); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 172 | MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list) |
| 173 | : name_(name), |
| 174 | arguments_(std::move(arg_list)) {} |
| 175 | |
| 176 | void MacroDecl::Write(CodeWriter* to) const { |
| 177 | to->Write("%s", name_.c_str()); |
| 178 | arguments_.Write(to); |
| 179 | to->Write("\n"); |
| 180 | } |
| 181 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 182 | MethodDecl::MethodDecl(const std::string& return_type, |
| 183 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 184 | ArgList&& arg_list) |
| 185 | : MethodDecl(return_type, name, std::move(arg_list), 0u) {} |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 186 | |
| 187 | MethodDecl::MethodDecl(const std::string& return_type, |
| 188 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 189 | ArgList&& arg_list, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 190 | uint32_t modifiers) |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 191 | : return_type_(return_type), |
| 192 | name_(name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 193 | arguments_(std::move(arg_list)), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 194 | is_const_(modifiers & IS_CONST), |
| 195 | is_virtual_(modifiers & IS_VIRTUAL), |
| 196 | is_override_(modifiers & IS_OVERRIDE), |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 197 | is_pure_virtual_(modifiers & IS_PURE_VIRTUAL), |
| 198 | is_static_(modifiers & IS_STATIC) {} |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 199 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 200 | void MethodDecl::Write(CodeWriter* to) const { |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 201 | if (is_virtual_) |
| 202 | to->Write("virtual "); |
| 203 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 204 | if (is_static_) |
| 205 | to->Write("static "); |
| 206 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 207 | to->Write("%s %s", return_type_.c_str(), name_.c_str()); |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 208 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 209 | arguments_.Write(to); |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 210 | |
| 211 | if (is_const_) |
| 212 | to->Write(" const"); |
| 213 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 214 | if (is_override_) |
| 215 | to->Write(" override"); |
| 216 | |
| 217 | if (is_pure_virtual_) |
| 218 | to->Write(" = 0"); |
| 219 | |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 220 | to->Write(";\n"); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 223 | void StatementBlock::AddStatement(unique_ptr<AstNode> statement) { |
| 224 | statements_.push_back(std::move(statement)); |
| 225 | } |
| 226 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 227 | void StatementBlock::AddStatement(AstNode* statement) { |
| 228 | statements_.emplace_back(statement); |
| 229 | } |
| 230 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 231 | void StatementBlock::AddLiteral(const std::string& expression_str, |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 232 | bool add_semicolon) { |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 233 | if (add_semicolon) { |
| 234 | statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str))); |
| 235 | } else { |
| 236 | statements_.push_back(unique_ptr<AstNode>( |
| 237 | new LiteralExpression(expression_str))); |
| 238 | } |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void StatementBlock::Write(CodeWriter* to) const { |
| 242 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 243 | to->Indent(); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 244 | for (const auto& statement : statements_) { |
| 245 | statement->Write(to); |
| 246 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 247 | to->Dedent(); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 248 | to->Write("}\n"); |
| 249 | } |
| 250 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 251 | ConstructorImpl::ConstructorImpl(const string& class_name, |
| 252 | ArgList&& arg_list, |
| 253 | const vector<string>& initializer_list) |
| 254 | : class_name_(class_name), |
| 255 | arguments_(std::move(arg_list)), |
| 256 | initializer_list_(initializer_list) {} |
| 257 | |
| 258 | void ConstructorImpl::Write(CodeWriter* to) const { |
| 259 | to->Write("%s::%s", class_name_.c_str(), class_name_.c_str()); |
| 260 | arguments_.Write(to); |
| 261 | to->Write("\n"); |
| 262 | |
| 263 | bool is_first = true; |
| 264 | for (const string& i : initializer_list_) { |
| 265 | if (is_first) { |
| 266 | to->Write(" : %s", i.c_str()); |
| 267 | } else { |
| 268 | to->Write(",\n %s", i.c_str()); |
| 269 | } |
| 270 | is_first = false; |
| 271 | } |
| 272 | |
| 273 | body_.Write(to); |
| 274 | } |
| 275 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 276 | MethodImpl::MethodImpl(const string& return_type, |
| 277 | const string& class_name, |
| 278 | const string& method_name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 279 | ArgList&& arg_list, |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 280 | bool is_const_method) |
| 281 | : return_type_(return_type), |
| 282 | method_name_(method_name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 283 | arguments_(std::move(arg_list)), |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 284 | is_const_method_(is_const_method) { |
| 285 | if (!class_name.empty()) { |
| 286 | method_name_ = class_name + "::" + method_name; |
| 287 | } |
| 288 | } |
| 289 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 290 | StatementBlock* MethodImpl::GetStatementBlock() { |
| 291 | return &statements_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void MethodImpl::Write(CodeWriter* to) const { |
| 295 | to->Write("%s %s", return_type_.c_str(), method_name_.c_str()); |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 296 | arguments_.Write(to); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 297 | to->Write("%s ", (is_const_method_) ? " const" : ""); |
| 298 | statements_.Write(to); |
| 299 | } |
| 300 | |
| 301 | SwitchStatement::SwitchStatement(const std::string& expression) |
| 302 | : switch_expression_(expression) {} |
| 303 | |
| 304 | StatementBlock* SwitchStatement::AddCase(const string& value_expression) { |
| 305 | auto it = std::find(case_values_.begin(), case_values_.end(), value_expression); |
| 306 | if (it != case_values_.end()) { |
| 307 | LOG(ERROR) << "internal error: duplicate switch case labels"; |
| 308 | return nullptr; |
| 309 | } |
| 310 | StatementBlock* ret = new StatementBlock(); |
| 311 | case_values_.push_back(value_expression); |
| 312 | case_logic_.push_back(unique_ptr<StatementBlock>{ret}); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | void SwitchStatement::Write(CodeWriter* to) const { |
| 317 | to->Write("switch (%s) {\n", switch_expression_.c_str()); |
| 318 | for (size_t i = 0; i < case_values_.size(); ++i) { |
| 319 | const string& case_value = case_values_[i]; |
| 320 | const unique_ptr<StatementBlock>& statements = case_logic_[i]; |
| 321 | if (case_value.empty()) { |
| 322 | to->Write("default:\n"); |
| 323 | } else { |
| 324 | to->Write("case %s:\n", case_value.c_str()); |
| 325 | } |
| 326 | statements->Write(to); |
| 327 | to->Write("break;\n"); |
| 328 | } |
| 329 | to->Write("}\n"); |
| 330 | } |
| 331 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 332 | |
| 333 | Assignment::Assignment(const std::string& left, const std::string& right) |
| 334 | : Assignment(left, new LiteralExpression{right}) {} |
| 335 | |
| 336 | Assignment::Assignment(const std::string& left, AstNode* right) |
| 337 | : lhs_(left), |
| 338 | rhs_(right) {} |
| 339 | |
| 340 | void Assignment::Write(CodeWriter* to) const { |
| 341 | to->Write("%s = ", lhs_.c_str()); |
| 342 | rhs_->Write(to); |
| 343 | to->Write(";\n"); |
| 344 | } |
| 345 | |
| 346 | MethodCall::MethodCall(const std::string& method_name, |
| 347 | const std::string& single_argument) |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 348 | : MethodCall(method_name, ArgList{single_argument}) {} |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 349 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 350 | MethodCall::MethodCall(const std::string& method_name, |
| 351 | ArgList&& arg_list) |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 352 | : method_name_(method_name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 353 | arguments_{std::move(arg_list)} {} |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 354 | |
| 355 | void MethodCall::Write(CodeWriter* to) const { |
| 356 | to->Write("%s", method_name_.c_str()); |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 357 | arguments_.Write(to); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 360 | IfStatement::IfStatement(AstNode* expression, bool invert_expression) |
| 361 | : expression_(expression), |
| 362 | invert_expression_(invert_expression) {} |
| 363 | |
| 364 | void IfStatement::Write(CodeWriter* to) const { |
Christopher Wiley | 864bc09 | 2015-11-10 11:45:23 -0800 | [diff] [blame] | 365 | to->Write("if (%s", (invert_expression_) ? "!(" : ""); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 366 | expression_->Write(to); |
| 367 | to->Write(")%s ", (invert_expression_) ? ")" : ""); |
| 368 | on_true_.Write(to); |
| 369 | |
| 370 | if (!on_false_.Empty()) { |
| 371 | to->Write("else "); |
| 372 | on_false_.Write(to); |
| 373 | } |
| 374 | } |
| 375 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 376 | Statement::Statement(unique_ptr<AstNode> expression) |
| 377 | : expression_(std::move(expression)) {} |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 378 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 379 | Statement::Statement(AstNode* expression) : expression_(expression) {} |
| 380 | |
| 381 | Statement::Statement(const string& expression) |
| 382 | : expression_(new LiteralExpression(expression)) {} |
| 383 | |
| 384 | void Statement::Write(CodeWriter* to) const { |
| 385 | expression_->Write(to); |
| 386 | to->Write(";\n"); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Christopher Wiley | d55db28 | 2015-10-20 18:16:47 -0700 | [diff] [blame] | 389 | Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs) |
| 390 | : left_(lhs), |
| 391 | right_(rhs), |
| 392 | operator_(comparison) {} |
| 393 | |
| 394 | void Comparison::Write(CodeWriter* to) const { |
| 395 | to->Write("(("); |
| 396 | left_->Write(to); |
| 397 | to->Write(") %s (", operator_.c_str()); |
| 398 | right_->Write(to); |
| 399 | to->Write("))"); |
| 400 | } |
| 401 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 402 | LiteralExpression::LiteralExpression(const std::string& expression) |
| 403 | : expression_(expression) {} |
| 404 | |
| 405 | void LiteralExpression::Write(CodeWriter* to) const { |
| 406 | to->Write("%s", expression_.c_str()); |
| 407 | } |
| 408 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 409 | CppNamespace::CppNamespace(const std::string& name, |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 410 | std::vector<unique_ptr<Declaration>> declarations) |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 411 | : declarations_(std::move(declarations)), |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 412 | name_(name) {} |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 413 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 414 | CppNamespace::CppNamespace(const std::string& name, |
| 415 | unique_ptr<Declaration> declaration) |
| 416 | : name_(name) { |
| 417 | declarations_.push_back(std::move(declaration)); |
| 418 | } |
| 419 | CppNamespace::CppNamespace(const std::string& name) |
| 420 | : name_(name) {} |
| 421 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 422 | void CppNamespace::Write(CodeWriter* to) const { |
| 423 | to->Write("namespace %s {\n\n", name_.c_str()); |
| 424 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 425 | for (const auto& dec : declarations_) { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 426 | dec->Write(to); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 427 | to->Write("\n"); |
| 428 | } |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 429 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 430 | to->Write("} // namespace %s\n", name_.c_str()); |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 433 | Document::Document(const std::vector<std::string>& include_list, |
| 434 | unique_ptr<CppNamespace> a_namespace) |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 435 | : include_list_(include_list), |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 436 | namespace_(std::move(a_namespace)) {} |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 437 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 438 | void Document::Write(CodeWriter* to) const { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 439 | for (const auto& include : include_list_) { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 440 | to->Write("#include <%s>\n", include.c_str()); |
| 441 | } |
| 442 | to->Write("\n"); |
| 443 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 444 | namespace_->Write(to); |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 447 | CppHeader::CppHeader(const std::string& include_guard, |
| 448 | const std::vector<std::string>& include_list, |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 449 | unique_ptr<CppNamespace> a_namespace) |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 450 | : Document(include_list, std::move(a_namespace)), |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 451 | include_guard_(include_guard) {} |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 452 | |
| 453 | void CppHeader::Write(CodeWriter* to) const { |
| 454 | to->Write("#ifndef %s\n", include_guard_.c_str()); |
| 455 | to->Write("#define %s\n\n", include_guard_.c_str()); |
| 456 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 457 | Document::Write(to); |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 458 | to->Write("\n"); |
| 459 | |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 460 | to->Write("#endif // %s\n", include_guard_.c_str()); |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 463 | CppSource::CppSource(const std::vector<std::string>& include_list, |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 464 | unique_ptr<CppNamespace> a_namespace) |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 465 | : Document(include_list, std::move(a_namespace)) {} |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 466 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 467 | } // namespace cpp |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 468 | } // namespace aidl |
| 469 | } // namespace android |