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