blob: d5bd83e5e65b5f318750f019c8f1cc0f6835e8da [file] [log] [blame]
Christopher Wileyda695992015-10-05 11:31:41 -07001/*
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 Wileyf600a552015-09-12 14:07:44 -070017#include "ast_cpp.h"
18
Christopher Wileyda695992015-10-05 11:31:41 -070019#include <algorithm>
20
Christopher Wileyf600a552015-09-12 14:07:44 -070021#include "code_writer.h"
Christopher Wileyda695992015-10-05 11:31:41 -070022#include "logging.h"
Christopher Wileyf600a552015-09-12 14:07:44 -070023
Christopher Wileya7a5c102015-09-29 16:26:52 -070024using std::string;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070025using std::unique_ptr;
Christopher Wileyda695992015-10-05 11:31:41 -070026using std::vector;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070027
Christopher Wileyf600a552015-09-12 14:07:44 -070028namespace android {
29namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070030namespace cpp {
Christopher Wileyf600a552015-09-12 14:07:44 -070031
Jiyong Park176905e2018-07-04 22:29:41 +090032std::string AstNode::ToString() {
33 std::string str;
34 Write(CodeWriter::ForString(&str).get());
35 return str;
36}
37
Steven Moreland5557f1c2018-07-02 13:50:23 -070038LiteralDecl::LiteralDecl(const std::string& expression) : expression_(expression) {}
39
40void LiteralDecl::Write(CodeWriter* to) const {
41 to->Write("%s", expression_.c_str());
42}
43
Devin Moore53fc99c2020-08-12 08:07:52 -070044ClassDecl::ClassDecl(const std::string& name, const std::string& parent,
45 const std::vector<std::string>& template_params)
46 : name_(name), parent_(parent), template_params_(template_params) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -070047
Christopher Wileyf944e792015-09-29 10:00:46 -070048ClassDecl::ClassDecl(const std::string& name, const std::string& parent,
Devin Moore53fc99c2020-08-12 08:07:52 -070049 const std::vector<std::string>& template_params,
Christopher Wileyf944e792015-09-29 10:00:46 -070050 std::vector<unique_ptr<Declaration>> public_members,
51 std::vector<unique_ptr<Declaration>> private_members)
Casey Dahlin60a49162015-09-17 14:23:10 -070052 : name_(name),
53 parent_(parent),
Devin Moore53fc99c2020-08-12 08:07:52 -070054 template_params_(template_params),
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070055 public_members_(std::move(public_members)),
56 private_members_(std::move(private_members)) {}
Casey Dahlin60a49162015-09-17 14:23:10 -070057
Christopher Wileyf944e792015-09-29 10:00:46 -070058void ClassDecl::Write(CodeWriter* to) const {
Devin Moore53fc99c2020-08-12 08:07:52 -070059 if (!template_params_.empty())
60 to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str());
61
Casey Dahlin60a49162015-09-17 14:23:10 -070062 to->Write("class %s ", name_.c_str());
63
Devin Moore53fc99c2020-08-12 08:07:52 -070064 if (parent_.length() > 0) to->Write(": public %s ", parent_.c_str());
Casey Dahlin60a49162015-09-17 14:23:10 -070065
Casey Dahlin88924d62015-09-17 16:28:24 -070066 to->Write("{\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070067
Devin Moore53fc99c2020-08-12 08:07:52 -070068 if (!public_members_.empty()) to->Write("public:\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070069
Jiyong Parka755dc72018-06-29 13:52:24 +090070 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070071 for (const auto& dec : public_members_)
72 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090073 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070074
Devin Moore53fc99c2020-08-12 08:07:52 -070075 if (!private_members_.empty()) to->Write("private:\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070076
Jiyong Parka755dc72018-06-29 13:52:24 +090077 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070078 for (const auto& dec : private_members_)
79 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090080 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070081
Casey Dahlin88924d62015-09-17 16:28:24 -070082 to->Write("}; // class %s\n", name_.c_str());
83}
84
Christopher Wiley0c732db2015-09-29 14:36:44 -070085void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) {
86 public_members_.push_back(std::move(member));
87}
88
89void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) {
90 private_members_.push_back(std::move(member));
91}
92
Casey Dahlind40e2fe2015-11-24 14:06:52 -080093Enum::EnumField::EnumField(const string& k, const string& v)
Christopher Wileya7a5c102015-09-29 16:26:52 -070094 : key(k),
95 value(v) {}
96
Daniel Norman85aed542019-08-21 12:01:14 -070097Enum::Enum(const string& name, const string& base_type, bool is_class)
98 : enum_name_(name), underlying_type_(base_type), is_class_(is_class) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -070099
100void Enum::Write(CodeWriter* to) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700101 to->Write("enum ");
102 if (is_class_) {
103 to->Write("class ");
104 }
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800105 if (underlying_type_.empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700106 to->Write("%s {\n", enum_name_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800107 } else {
Daniel Norman85aed542019-08-21 12:01:14 -0700108 to->Write("%s : %s {\n", enum_name_.c_str(), underlying_type_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800109 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900110 to->Indent();
Christopher Wileya7a5c102015-09-29 16:26:52 -0700111 for (const auto& field : fields_) {
112 if (field.value.empty()) {
Jiyong Parka755dc72018-06-29 13:52:24 +0900113 to->Write("%s,\n", field.key.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700114 } else {
Jiyong Parka755dc72018-06-29 13:52:24 +0900115 to->Write("%s = %s,\n", field.key.c_str(), field.value.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700116 }
117 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900118 to->Dedent();
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700119 to->Write("};\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700120}
121
122void Enum::AddValue(const string& key, const string& value) {
123 fields_.emplace_back(key, value);
124}
125
Christopher Wiley23285262015-10-09 15:06:14 -0700126ArgList::ArgList(const std::string& single_argument)
127 : ArgList(vector<string>{single_argument}) {}
128
Christopher Wileyf02facf2015-11-12 08:54:08 -0800129ArgList::ArgList(const std::vector<std::string>& arg_list) {
130 for (const auto& s : arg_list) {
131 arguments_.emplace_back(new LiteralExpression(s));
132 }
133}
134
135ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list)
136 : arguments_(std::move(arg_list)) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700137
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700138ArgList::ArgList(ArgList&& arg_list) noexcept : arguments_(std::move(arg_list.arguments_)) {}
Christopher Wileyade4b452015-10-10 11:06:03 -0700139
Christopher Wiley23285262015-10-09 15:06:14 -0700140void ArgList::Write(CodeWriter* to) const {
141 to->Write("(");
142 bool is_first = true;
143 for (const auto& s : arguments_) {
144 if (!is_first) { to->Write(", "); }
145 is_first = false;
Christopher Wileyf02facf2015-11-12 08:54:08 -0800146 s->Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700147 }
148 to->Write(")");
149}
150
Christopher Wileya7a5c102015-09-29 16:26:52 -0700151ConstructorDecl::ConstructorDecl(
152 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700153 ArgList&& arg_list)
Christopher Wileyb23149d2015-10-14 13:52:21 -0700154 : ConstructorDecl(name, std::move(arg_list), 0u) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700155
Christopher Wileyf944e792015-09-29 10:00:46 -0700156ConstructorDecl::ConstructorDecl(
Casey Dahlina834dd42015-09-23 11:52:15 -0700157 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700158 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700159 uint32_t modifiers)
Casey Dahlina834dd42015-09-23 11:52:15 -0700160 : name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700161 arguments_(std::move(arg_list)),
Christopher Wileyb23149d2015-10-14 13:52:21 -0700162 modifiers_(modifiers) {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700163
Christopher Wileyf944e792015-09-29 10:00:46 -0700164void ConstructorDecl::Write(CodeWriter* to) const {
Christopher Wileyb23149d2015-10-14 13:52:21 -0700165 if (modifiers_ & Modifiers::IS_VIRTUAL)
Casey Dahlina834dd42015-09-23 11:52:15 -0700166 to->Write("virtual ");
167
Christopher Wileyb23149d2015-10-14 13:52:21 -0700168 if (modifiers_ & Modifiers::IS_EXPLICIT)
169 to->Write("explicit ");
170
Christopher Wileyade4b452015-10-10 11:06:03 -0700171 to->Write("%s", name_.c_str());
Casey Dahlina834dd42015-09-23 11:52:15 -0700172
Christopher Wileyade4b452015-10-10 11:06:03 -0700173 arguments_.Write(to);
Casey Dahlina834dd42015-09-23 11:52:15 -0700174
Christopher Wileyb23149d2015-10-14 13:52:21 -0700175 if (modifiers_ & Modifiers::IS_DEFAULT)
Christopher Wileyf094d582015-10-08 15:50:15 -0700176 to->Write(" = default");
177
178 to->Write(";\n");
Casey Dahlina834dd42015-09-23 11:52:15 -0700179}
180
Christopher Wiley11a9d792016-02-24 17:20:33 -0800181MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list)
182 : name_(name),
183 arguments_(std::move(arg_list)) {}
184
185void MacroDecl::Write(CodeWriter* to) const {
186 to->Write("%s", name_.c_str());
187 arguments_.Write(to);
188 to->Write("\n");
189}
190
Christopher Wileyf944e792015-09-29 10:00:46 -0700191MethodDecl::MethodDecl(const std::string& return_type,
192 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700193 ArgList&& arg_list)
194 : MethodDecl(return_type, name, std::move(arg_list), 0u) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -0700195
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900196MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700197 uint32_t modifiers)
Casey Dahlin88924d62015-09-17 16:28:24 -0700198 : return_type_(return_type),
199 name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700200 arguments_(std::move(arg_list)),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700201 is_const_(modifiers & IS_CONST),
202 is_virtual_(modifiers & IS_VIRTUAL),
203 is_override_(modifiers & IS_OVERRIDE),
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700204 is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900205 is_static_(modifiers & IS_STATIC),
206 is_final_(modifiers & IS_FINAL) {}
Casey Dahlin88924d62015-09-17 16:28:24 -0700207
Christopher Wileyf944e792015-09-29 10:00:46 -0700208void MethodDecl::Write(CodeWriter* to) const {
Casey Dahlin88924d62015-09-17 16:28:24 -0700209 if (is_virtual_)
210 to->Write("virtual ");
211
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700212 if (is_static_)
213 to->Write("static ");
214
Christopher Wileyda695992015-10-05 11:31:41 -0700215 to->Write("%s %s", return_type_.c_str(), name_.c_str());
Casey Dahlin88924d62015-09-17 16:28:24 -0700216
Christopher Wileyade4b452015-10-10 11:06:03 -0700217 arguments_.Write(to);
Casey Dahlin88924d62015-09-17 16:28:24 -0700218
219 if (is_const_)
220 to->Write(" const");
221
Christopher Wiley0c732db2015-09-29 14:36:44 -0700222 if (is_override_)
223 to->Write(" override");
224
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900225 if (is_final_) to->Write(" final");
226
Christopher Wiley0c732db2015-09-29 14:36:44 -0700227 if (is_pure_virtual_)
228 to->Write(" = 0");
229
Casey Dahlin88924d62015-09-17 16:28:24 -0700230 to->Write(";\n");
Casey Dahlin60a49162015-09-17 14:23:10 -0700231}
232
Christopher Wileyda695992015-10-05 11:31:41 -0700233void StatementBlock::AddStatement(unique_ptr<AstNode> statement) {
234 statements_.push_back(std::move(statement));
235}
236
Christopher Wiley23285262015-10-09 15:06:14 -0700237void StatementBlock::AddStatement(AstNode* statement) {
238 statements_.emplace_back(statement);
239}
240
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700241void StatementBlock::AddLiteral(const std::string& expression_str,
Christopher Wileyda695992015-10-05 11:31:41 -0700242 bool add_semicolon) {
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700243 if (add_semicolon) {
244 statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str)));
245 } else {
246 statements_.push_back(unique_ptr<AstNode>(
247 new LiteralExpression(expression_str)));
248 }
Christopher Wileyda695992015-10-05 11:31:41 -0700249}
250
251void StatementBlock::Write(CodeWriter* to) const {
252 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900253 to->Indent();
Christopher Wileyda695992015-10-05 11:31:41 -0700254 for (const auto& statement : statements_) {
255 statement->Write(to);
256 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900257 to->Dedent();
Christopher Wileyda695992015-10-05 11:31:41 -0700258 to->Write("}\n");
259}
260
Christopher Wileyf9688b02015-10-08 17:17:50 -0700261ConstructorImpl::ConstructorImpl(const string& class_name,
262 ArgList&& arg_list,
263 const vector<string>& initializer_list)
264 : class_name_(class_name),
265 arguments_(std::move(arg_list)),
266 initializer_list_(initializer_list) {}
267
Steven Morelanda57d0a62019-07-30 09:41:14 -0700268StatementBlock* ConstructorImpl::GetStatementBlock() {
269 return &body_;
270}
271
Christopher Wileyf9688b02015-10-08 17:17:50 -0700272void ConstructorImpl::Write(CodeWriter* to) const {
273 to->Write("%s::%s", class_name_.c_str(), class_name_.c_str());
274 arguments_.Write(to);
275 to->Write("\n");
276
277 bool is_first = true;
278 for (const string& i : initializer_list_) {
279 if (is_first) {
280 to->Write(" : %s", i.c_str());
281 } else {
282 to->Write(",\n %s", i.c_str());
283 }
284 is_first = false;
285 }
286
287 body_.Write(to);
288}
289
Devin Moore53fc99c2020-08-12 08:07:52 -0700290MethodImpl::MethodImpl(const string& return_type, const string& class_name,
291 const string& method_name, const std::vector<std::string>& template_params,
292 ArgList&& arg_list, bool is_const_method)
Christopher Wileyda695992015-10-05 11:31:41 -0700293 : return_type_(return_type),
294 method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700295 arguments_(std::move(arg_list)),
Devin Moore53fc99c2020-08-12 08:07:52 -0700296 is_const_method_(is_const_method),
297 template_params_(template_params) {
Christopher Wileyda695992015-10-05 11:31:41 -0700298 if (!class_name.empty()) {
Devin Moore53fc99c2020-08-12 08:07:52 -0700299 if (!template_params.empty()) {
300 method_name_ = class_name + "<" + base::Join(template_params, ",") + ">::" + method_name;
301 } else {
302 method_name_ = class_name + "::" + method_name;
303 }
Christopher Wileyda695992015-10-05 11:31:41 -0700304 }
305}
306
Christopher Wileyf9688b02015-10-08 17:17:50 -0700307StatementBlock* MethodImpl::GetStatementBlock() {
308 return &statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700309}
310
311void MethodImpl::Write(CodeWriter* to) const {
Devin Moore53fc99c2020-08-12 08:07:52 -0700312 if (!template_params_.empty())
313 to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str());
Christopher Wileyda695992015-10-05 11:31:41 -0700314 to->Write("%s %s", return_type_.c_str(), method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700315 arguments_.Write(to);
Christopher Wileyda695992015-10-05 11:31:41 -0700316 to->Write("%s ", (is_const_method_) ? " const" : "");
317 statements_.Write(to);
318}
319
320SwitchStatement::SwitchStatement(const std::string& expression)
321 : switch_expression_(expression) {}
322
323StatementBlock* SwitchStatement::AddCase(const string& value_expression) {
324 auto it = std::find(case_values_.begin(), case_values_.end(), value_expression);
325 if (it != case_values_.end()) {
326 LOG(ERROR) << "internal error: duplicate switch case labels";
327 return nullptr;
328 }
329 StatementBlock* ret = new StatementBlock();
330 case_values_.push_back(value_expression);
331 case_logic_.push_back(unique_ptr<StatementBlock>{ret});
332 return ret;
333}
334
335void SwitchStatement::Write(CodeWriter* to) const {
336 to->Write("switch (%s) {\n", switch_expression_.c_str());
337 for (size_t i = 0; i < case_values_.size(); ++i) {
338 const string& case_value = case_values_[i];
339 const unique_ptr<StatementBlock>& statements = case_logic_[i];
340 if (case_value.empty()) {
341 to->Write("default:\n");
342 } else {
343 to->Write("case %s:\n", case_value.c_str());
344 }
345 statements->Write(to);
346 to->Write("break;\n");
347 }
348 to->Write("}\n");
349}
350
Christopher Wiley23285262015-10-09 15:06:14 -0700351
352Assignment::Assignment(const std::string& left, const std::string& right)
353 : Assignment(left, new LiteralExpression{right}) {}
354
355Assignment::Assignment(const std::string& left, AstNode* right)
356 : lhs_(left),
357 rhs_(right) {}
358
359void Assignment::Write(CodeWriter* to) const {
360 to->Write("%s = ", lhs_.c_str());
361 rhs_->Write(to);
362 to->Write(";\n");
363}
364
365MethodCall::MethodCall(const std::string& method_name,
366 const std::string& single_argument)
Christopher Wileyade4b452015-10-10 11:06:03 -0700367 : MethodCall(method_name, ArgList{single_argument}) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700368
Christopher Wileyade4b452015-10-10 11:06:03 -0700369MethodCall::MethodCall(const std::string& method_name,
370 ArgList&& arg_list)
Christopher Wiley23285262015-10-09 15:06:14 -0700371 : method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700372 arguments_{std::move(arg_list)} {}
Christopher Wiley23285262015-10-09 15:06:14 -0700373
374void MethodCall::Write(CodeWriter* to) const {
375 to->Write("%s", method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700376 arguments_.Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700377}
378
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700379IfStatement::IfStatement(AstNode* expression, bool invert_expression)
380 : expression_(expression),
381 invert_expression_(invert_expression) {}
382
383void IfStatement::Write(CodeWriter* to) const {
Christopher Wiley864bc092015-11-10 11:45:23 -0800384 to->Write("if (%s", (invert_expression_) ? "!(" : "");
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700385 expression_->Write(to);
386 to->Write(")%s ", (invert_expression_) ? ")" : "");
387 on_true_.Write(to);
388
389 if (!on_false_.Empty()) {
390 to->Write("else ");
391 on_false_.Write(to);
392 }
393}
394
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700395Statement::Statement(unique_ptr<AstNode> expression)
396 : expression_(std::move(expression)) {}
Christopher Wileyda695992015-10-05 11:31:41 -0700397
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700398Statement::Statement(AstNode* expression) : expression_(expression) {}
399
400Statement::Statement(const string& expression)
401 : expression_(new LiteralExpression(expression)) {}
402
403void Statement::Write(CodeWriter* to) const {
404 expression_->Write(to);
405 to->Write(";\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700406}
407
Christopher Wileyd55db282015-10-20 18:16:47 -0700408Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs)
409 : left_(lhs),
410 right_(rhs),
411 operator_(comparison) {}
412
413void Comparison::Write(CodeWriter* to) const {
414 to->Write("((");
415 left_->Write(to);
416 to->Write(") %s (", operator_.c_str());
417 right_->Write(to);
418 to->Write("))");
419}
420
Christopher Wiley23285262015-10-09 15:06:14 -0700421LiteralExpression::LiteralExpression(const std::string& expression)
422 : expression_(expression) {}
423
424void LiteralExpression::Write(CodeWriter* to) const {
425 to->Write("%s", expression_.c_str());
426}
427
Casey Dahlin34b86102015-09-16 16:03:06 -0700428CppNamespace::CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700429 std::vector<unique_ptr<Declaration>> declarations)
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700430 : declarations_(std::move(declarations)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700431 name_(name) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700432
Christopher Wiley0c732db2015-09-29 14:36:44 -0700433CppNamespace::CppNamespace(const std::string& name,
434 unique_ptr<Declaration> declaration)
435 : name_(name) {
436 declarations_.push_back(std::move(declaration));
437}
438CppNamespace::CppNamespace(const std::string& name)
439 : name_(name) {}
440
Casey Dahlin34b86102015-09-16 16:03:06 -0700441void CppNamespace::Write(CodeWriter* to) const {
442 to->Write("namespace %s {\n\n", name_.c_str());
443
Casey Dahlin60a49162015-09-17 14:23:10 -0700444 for (const auto& dec : declarations_) {
Casey Dahlin34b86102015-09-16 16:03:06 -0700445 dec->Write(to);
Casey Dahlin60a49162015-09-17 14:23:10 -0700446 to->Write("\n");
447 }
Casey Dahlin34b86102015-09-16 16:03:06 -0700448
Casey Dahlin60a49162015-09-17 14:23:10 -0700449 to->Write("} // namespace %s\n", name_.c_str());
Casey Dahlin34b86102015-09-16 16:03:06 -0700450}
451
Christopher Wileyf944e792015-09-29 10:00:46 -0700452Document::Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700453 std::vector<unique_ptr<Declaration>> declarations)
454 : include_list_(include_list), declarations_(std::move(declarations)) {}
Casey Dahlin34b86102015-09-16 16:03:06 -0700455
Christopher Wileyf944e792015-09-29 10:00:46 -0700456void Document::Write(CodeWriter* to) const {
Casey Dahlin34b86102015-09-16 16:03:06 -0700457 for (const auto& include : include_list_) {
Christopher Wileyf600a552015-09-12 14:07:44 -0700458 to->Write("#include <%s>\n", include.c_str());
459 }
460 to->Write("\n");
461
Steven Morelandf3da0892018-10-05 14:52:01 -0700462 for (const auto& declaration : declarations_) {
463 declaration->Write(to);
464 }
Christopher Wileyf600a552015-09-12 14:07:44 -0700465}
466
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700467CppHeader::CppHeader(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700468 std::vector<std::unique_ptr<Declaration>> declarations)
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700469 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700470
471void CppHeader::Write(CodeWriter* to) const {
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700472 to->Write("#pragma once\n\n");
Christopher Wileyf600a552015-09-12 14:07:44 -0700473
Christopher Wileyf944e792015-09-29 10:00:46 -0700474 Document::Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700475}
476
Casey Dahlin34b86102015-09-16 16:03:06 -0700477CppSource::CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700478 std::vector<std::unique_ptr<Declaration>> declarations)
479 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700480
Christopher Wileyf944e792015-09-29 10:00:46 -0700481} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700482} // namespace aidl
483} // namespace android