blob: 98566fbe44ff9e5dc05158c1025575ca84e045bd [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
Christopher Wiley0c732db2015-09-29 14:36:44 -070044ClassDecl::ClassDecl(const std::string& name, const std::string& parent)
45 : name_(name),
46 parent_(parent) {}
47
Christopher Wileyf944e792015-09-29 10:00:46 -070048ClassDecl::ClassDecl(const std::string& name, const std::string& parent,
49 std::vector<unique_ptr<Declaration>> public_members,
50 std::vector<unique_ptr<Declaration>> private_members)
Casey Dahlin60a49162015-09-17 14:23:10 -070051 : name_(name),
52 parent_(parent),
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070053 public_members_(std::move(public_members)),
54 private_members_(std::move(private_members)) {}
Casey Dahlin60a49162015-09-17 14:23:10 -070055
Christopher Wileyf944e792015-09-29 10:00:46 -070056void ClassDecl::Write(CodeWriter* to) const {
Casey Dahlin60a49162015-09-17 14:23:10 -070057 to->Write("class %s ", name_.c_str());
58
59 if (parent_.length() > 0)
60 to->Write(": public %s ", parent_.c_str());
61
Casey Dahlin88924d62015-09-17 16:28:24 -070062 to->Write("{\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070063
64 if (!public_members_.empty())
65 to->Write("public:\n");
66
Jiyong Parka755dc72018-06-29 13:52:24 +090067 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070068 for (const auto& dec : public_members_)
69 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090070 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070071
72 if (!private_members_.empty())
73 to->Write("private:\n");
74
Jiyong Parka755dc72018-06-29 13:52:24 +090075 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070076 for (const auto& dec : private_members_)
77 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090078 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070079
Casey Dahlin88924d62015-09-17 16:28:24 -070080 to->Write("}; // class %s\n", name_.c_str());
81}
82
Christopher Wiley0c732db2015-09-29 14:36:44 -070083void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) {
84 public_members_.push_back(std::move(member));
85}
86
87void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) {
88 private_members_.push_back(std::move(member));
89}
90
Casey Dahlind40e2fe2015-11-24 14:06:52 -080091Enum::EnumField::EnumField(const string& k, const string& v)
Christopher Wileya7a5c102015-09-29 16:26:52 -070092 : key(k),
93 value(v) {}
94
Daniel Norman85aed542019-08-21 12:01:14 -070095Enum::Enum(const string& name, const string& base_type, bool is_class)
96 : enum_name_(name), underlying_type_(base_type), is_class_(is_class) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -070097
98void Enum::Write(CodeWriter* to) const {
Daniel Norman85aed542019-08-21 12:01:14 -070099 to->Write("enum ");
100 if (is_class_) {
101 to->Write("class ");
102 }
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800103 if (underlying_type_.empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700104 to->Write("%s {\n", enum_name_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800105 } else {
Daniel Norman85aed542019-08-21 12:01:14 -0700106 to->Write("%s : %s {\n", enum_name_.c_str(), underlying_type_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800107 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900108 to->Indent();
Christopher Wileya7a5c102015-09-29 16:26:52 -0700109 for (const auto& field : fields_) {
110 if (field.value.empty()) {
Jiyong Parka755dc72018-06-29 13:52:24 +0900111 to->Write("%s,\n", field.key.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700112 } else {
Jiyong Parka755dc72018-06-29 13:52:24 +0900113 to->Write("%s = %s,\n", field.key.c_str(), field.value.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700114 }
115 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900116 to->Dedent();
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700117 to->Write("};\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700118}
119
120void Enum::AddValue(const string& key, const string& value) {
121 fields_.emplace_back(key, value);
122}
123
Christopher Wiley23285262015-10-09 15:06:14 -0700124ArgList::ArgList(const std::string& single_argument)
125 : ArgList(vector<string>{single_argument}) {}
126
Christopher Wileyf02facf2015-11-12 08:54:08 -0800127ArgList::ArgList(const std::vector<std::string>& arg_list) {
128 for (const auto& s : arg_list) {
129 arguments_.emplace_back(new LiteralExpression(s));
130 }
131}
132
133ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list)
134 : arguments_(std::move(arg_list)) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700135
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700136ArgList::ArgList(ArgList&& arg_list) noexcept : arguments_(std::move(arg_list.arguments_)) {}
Christopher Wileyade4b452015-10-10 11:06:03 -0700137
Christopher Wiley23285262015-10-09 15:06:14 -0700138void ArgList::Write(CodeWriter* to) const {
139 to->Write("(");
140 bool is_first = true;
141 for (const auto& s : arguments_) {
142 if (!is_first) { to->Write(", "); }
143 is_first = false;
Christopher Wileyf02facf2015-11-12 08:54:08 -0800144 s->Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700145 }
146 to->Write(")");
147}
148
Christopher Wileya7a5c102015-09-29 16:26:52 -0700149ConstructorDecl::ConstructorDecl(
150 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700151 ArgList&& arg_list)
Christopher Wileyb23149d2015-10-14 13:52:21 -0700152 : ConstructorDecl(name, std::move(arg_list), 0u) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700153
Christopher Wileyf944e792015-09-29 10:00:46 -0700154ConstructorDecl::ConstructorDecl(
Casey Dahlina834dd42015-09-23 11:52:15 -0700155 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700156 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700157 uint32_t modifiers)
Casey Dahlina834dd42015-09-23 11:52:15 -0700158 : name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700159 arguments_(std::move(arg_list)),
Christopher Wileyb23149d2015-10-14 13:52:21 -0700160 modifiers_(modifiers) {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700161
Christopher Wileyf944e792015-09-29 10:00:46 -0700162void ConstructorDecl::Write(CodeWriter* to) const {
Christopher Wileyb23149d2015-10-14 13:52:21 -0700163 if (modifiers_ & Modifiers::IS_VIRTUAL)
Casey Dahlina834dd42015-09-23 11:52:15 -0700164 to->Write("virtual ");
165
Christopher Wileyb23149d2015-10-14 13:52:21 -0700166 if (modifiers_ & Modifiers::IS_EXPLICIT)
167 to->Write("explicit ");
168
Christopher Wileyade4b452015-10-10 11:06:03 -0700169 to->Write("%s", name_.c_str());
Casey Dahlina834dd42015-09-23 11:52:15 -0700170
Christopher Wileyade4b452015-10-10 11:06:03 -0700171 arguments_.Write(to);
Casey Dahlina834dd42015-09-23 11:52:15 -0700172
Christopher Wileyb23149d2015-10-14 13:52:21 -0700173 if (modifiers_ & Modifiers::IS_DEFAULT)
Christopher Wileyf094d582015-10-08 15:50:15 -0700174 to->Write(" = default");
175
176 to->Write(";\n");
Casey Dahlina834dd42015-09-23 11:52:15 -0700177}
178
Christopher Wiley11a9d792016-02-24 17:20:33 -0800179MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list)
180 : name_(name),
181 arguments_(std::move(arg_list)) {}
182
183void MacroDecl::Write(CodeWriter* to) const {
184 to->Write("%s", name_.c_str());
185 arguments_.Write(to);
186 to->Write("\n");
187}
188
Christopher Wileyf944e792015-09-29 10:00:46 -0700189MethodDecl::MethodDecl(const std::string& return_type,
190 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700191 ArgList&& arg_list)
192 : MethodDecl(return_type, name, std::move(arg_list), 0u) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -0700193
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900194MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700195 uint32_t modifiers)
Casey Dahlin88924d62015-09-17 16:28:24 -0700196 : return_type_(return_type),
197 name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700198 arguments_(std::move(arg_list)),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700199 is_const_(modifiers & IS_CONST),
200 is_virtual_(modifiers & IS_VIRTUAL),
201 is_override_(modifiers & IS_OVERRIDE),
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700202 is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900203 is_static_(modifiers & IS_STATIC),
204 is_final_(modifiers & IS_FINAL) {}
Casey Dahlin88924d62015-09-17 16:28:24 -0700205
Christopher Wileyf944e792015-09-29 10:00:46 -0700206void MethodDecl::Write(CodeWriter* to) const {
Casey Dahlin88924d62015-09-17 16:28:24 -0700207 if (is_virtual_)
208 to->Write("virtual ");
209
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700210 if (is_static_)
211 to->Write("static ");
212
Christopher Wileyda695992015-10-05 11:31:41 -0700213 to->Write("%s %s", return_type_.c_str(), name_.c_str());
Casey Dahlin88924d62015-09-17 16:28:24 -0700214
Christopher Wileyade4b452015-10-10 11:06:03 -0700215 arguments_.Write(to);
Casey Dahlin88924d62015-09-17 16:28:24 -0700216
217 if (is_const_)
218 to->Write(" const");
219
Christopher Wiley0c732db2015-09-29 14:36:44 -0700220 if (is_override_)
221 to->Write(" override");
222
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900223 if (is_final_) to->Write(" final");
224
Christopher Wiley0c732db2015-09-29 14:36:44 -0700225 if (is_pure_virtual_)
226 to->Write(" = 0");
227
Casey Dahlin88924d62015-09-17 16:28:24 -0700228 to->Write(";\n");
Casey Dahlin60a49162015-09-17 14:23:10 -0700229}
230
Christopher Wileyda695992015-10-05 11:31:41 -0700231void StatementBlock::AddStatement(unique_ptr<AstNode> statement) {
232 statements_.push_back(std::move(statement));
233}
234
Christopher Wiley23285262015-10-09 15:06:14 -0700235void StatementBlock::AddStatement(AstNode* statement) {
236 statements_.emplace_back(statement);
237}
238
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700239void StatementBlock::AddLiteral(const std::string& expression_str,
Christopher Wileyda695992015-10-05 11:31:41 -0700240 bool add_semicolon) {
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700241 if (add_semicolon) {
242 statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str)));
243 } else {
244 statements_.push_back(unique_ptr<AstNode>(
245 new LiteralExpression(expression_str)));
246 }
Christopher Wileyda695992015-10-05 11:31:41 -0700247}
248
249void StatementBlock::Write(CodeWriter* to) const {
250 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900251 to->Indent();
Christopher Wileyda695992015-10-05 11:31:41 -0700252 for (const auto& statement : statements_) {
253 statement->Write(to);
254 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900255 to->Dedent();
Christopher Wileyda695992015-10-05 11:31:41 -0700256 to->Write("}\n");
257}
258
Christopher Wileyf9688b02015-10-08 17:17:50 -0700259ConstructorImpl::ConstructorImpl(const string& class_name,
260 ArgList&& arg_list,
261 const vector<string>& initializer_list)
262 : class_name_(class_name),
263 arguments_(std::move(arg_list)),
264 initializer_list_(initializer_list) {}
265
Steven Morelanda57d0a62019-07-30 09:41:14 -0700266StatementBlock* ConstructorImpl::GetStatementBlock() {
267 return &body_;
268}
269
Christopher Wileyf9688b02015-10-08 17:17:50 -0700270void ConstructorImpl::Write(CodeWriter* to) const {
271 to->Write("%s::%s", class_name_.c_str(), class_name_.c_str());
272 arguments_.Write(to);
273 to->Write("\n");
274
275 bool is_first = true;
276 for (const string& i : initializer_list_) {
277 if (is_first) {
278 to->Write(" : %s", i.c_str());
279 } else {
280 to->Write(",\n %s", i.c_str());
281 }
282 is_first = false;
283 }
284
285 body_.Write(to);
286}
287
Christopher Wileyda695992015-10-05 11:31:41 -0700288MethodImpl::MethodImpl(const string& return_type,
289 const string& class_name,
290 const string& method_name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700291 ArgList&& arg_list,
Christopher Wileyda695992015-10-05 11:31:41 -0700292 bool is_const_method)
293 : return_type_(return_type),
294 method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700295 arguments_(std::move(arg_list)),
Christopher Wileyda695992015-10-05 11:31:41 -0700296 is_const_method_(is_const_method) {
297 if (!class_name.empty()) {
298 method_name_ = class_name + "::" + method_name;
299 }
300}
301
Christopher Wileyf9688b02015-10-08 17:17:50 -0700302StatementBlock* MethodImpl::GetStatementBlock() {
303 return &statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700304}
305
306void MethodImpl::Write(CodeWriter* to) const {
307 to->Write("%s %s", return_type_.c_str(), method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700308 arguments_.Write(to);
Christopher Wileyda695992015-10-05 11:31:41 -0700309 to->Write("%s ", (is_const_method_) ? " const" : "");
310 statements_.Write(to);
311}
312
313SwitchStatement::SwitchStatement(const std::string& expression)
314 : switch_expression_(expression) {}
315
316StatementBlock* SwitchStatement::AddCase(const string& value_expression) {
317 auto it = std::find(case_values_.begin(), case_values_.end(), value_expression);
318 if (it != case_values_.end()) {
319 LOG(ERROR) << "internal error: duplicate switch case labels";
320 return nullptr;
321 }
322 StatementBlock* ret = new StatementBlock();
323 case_values_.push_back(value_expression);
324 case_logic_.push_back(unique_ptr<StatementBlock>{ret});
325 return ret;
326}
327
328void SwitchStatement::Write(CodeWriter* to) const {
329 to->Write("switch (%s) {\n", switch_expression_.c_str());
330 for (size_t i = 0; i < case_values_.size(); ++i) {
331 const string& case_value = case_values_[i];
332 const unique_ptr<StatementBlock>& statements = case_logic_[i];
333 if (case_value.empty()) {
334 to->Write("default:\n");
335 } else {
336 to->Write("case %s:\n", case_value.c_str());
337 }
338 statements->Write(to);
339 to->Write("break;\n");
340 }
341 to->Write("}\n");
342}
343
Christopher Wiley23285262015-10-09 15:06:14 -0700344
345Assignment::Assignment(const std::string& left, const std::string& right)
346 : Assignment(left, new LiteralExpression{right}) {}
347
348Assignment::Assignment(const std::string& left, AstNode* right)
349 : lhs_(left),
350 rhs_(right) {}
351
352void Assignment::Write(CodeWriter* to) const {
353 to->Write("%s = ", lhs_.c_str());
354 rhs_->Write(to);
355 to->Write(";\n");
356}
357
358MethodCall::MethodCall(const std::string& method_name,
359 const std::string& single_argument)
Christopher Wileyade4b452015-10-10 11:06:03 -0700360 : MethodCall(method_name, ArgList{single_argument}) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700361
Christopher Wileyade4b452015-10-10 11:06:03 -0700362MethodCall::MethodCall(const std::string& method_name,
363 ArgList&& arg_list)
Christopher Wiley23285262015-10-09 15:06:14 -0700364 : method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700365 arguments_{std::move(arg_list)} {}
Christopher Wiley23285262015-10-09 15:06:14 -0700366
367void MethodCall::Write(CodeWriter* to) const {
368 to->Write("%s", method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700369 arguments_.Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700370}
371
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700372IfStatement::IfStatement(AstNode* expression, bool invert_expression)
373 : expression_(expression),
374 invert_expression_(invert_expression) {}
375
376void IfStatement::Write(CodeWriter* to) const {
Christopher Wiley864bc092015-11-10 11:45:23 -0800377 to->Write("if (%s", (invert_expression_) ? "!(" : "");
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700378 expression_->Write(to);
379 to->Write(")%s ", (invert_expression_) ? ")" : "");
380 on_true_.Write(to);
381
382 if (!on_false_.Empty()) {
383 to->Write("else ");
384 on_false_.Write(to);
385 }
386}
387
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700388Statement::Statement(unique_ptr<AstNode> expression)
389 : expression_(std::move(expression)) {}
Christopher Wileyda695992015-10-05 11:31:41 -0700390
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700391Statement::Statement(AstNode* expression) : expression_(expression) {}
392
393Statement::Statement(const string& expression)
394 : expression_(new LiteralExpression(expression)) {}
395
396void Statement::Write(CodeWriter* to) const {
397 expression_->Write(to);
398 to->Write(";\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700399}
400
Christopher Wileyd55db282015-10-20 18:16:47 -0700401Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs)
402 : left_(lhs),
403 right_(rhs),
404 operator_(comparison) {}
405
406void Comparison::Write(CodeWriter* to) const {
407 to->Write("((");
408 left_->Write(to);
409 to->Write(") %s (", operator_.c_str());
410 right_->Write(to);
411 to->Write("))");
412}
413
Christopher Wiley23285262015-10-09 15:06:14 -0700414LiteralExpression::LiteralExpression(const std::string& expression)
415 : expression_(expression) {}
416
417void LiteralExpression::Write(CodeWriter* to) const {
418 to->Write("%s", expression_.c_str());
419}
420
Casey Dahlin34b86102015-09-16 16:03:06 -0700421CppNamespace::CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700422 std::vector<unique_ptr<Declaration>> declarations)
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700423 : declarations_(std::move(declarations)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700424 name_(name) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700425
Christopher Wiley0c732db2015-09-29 14:36:44 -0700426CppNamespace::CppNamespace(const std::string& name,
427 unique_ptr<Declaration> declaration)
428 : name_(name) {
429 declarations_.push_back(std::move(declaration));
430}
431CppNamespace::CppNamespace(const std::string& name)
432 : name_(name) {}
433
Casey Dahlin34b86102015-09-16 16:03:06 -0700434void CppNamespace::Write(CodeWriter* to) const {
435 to->Write("namespace %s {\n\n", name_.c_str());
436
Casey Dahlin60a49162015-09-17 14:23:10 -0700437 for (const auto& dec : declarations_) {
Casey Dahlin34b86102015-09-16 16:03:06 -0700438 dec->Write(to);
Casey Dahlin60a49162015-09-17 14:23:10 -0700439 to->Write("\n");
440 }
Casey Dahlin34b86102015-09-16 16:03:06 -0700441
Casey Dahlin60a49162015-09-17 14:23:10 -0700442 to->Write("} // namespace %s\n", name_.c_str());
Casey Dahlin34b86102015-09-16 16:03:06 -0700443}
444
Christopher Wileyf944e792015-09-29 10:00:46 -0700445Document::Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700446 std::vector<unique_ptr<Declaration>> declarations)
447 : include_list_(include_list), declarations_(std::move(declarations)) {}
Casey Dahlin34b86102015-09-16 16:03:06 -0700448
Christopher Wileyf944e792015-09-29 10:00:46 -0700449void Document::Write(CodeWriter* to) const {
Casey Dahlin34b86102015-09-16 16:03:06 -0700450 for (const auto& include : include_list_) {
Christopher Wileyf600a552015-09-12 14:07:44 -0700451 to->Write("#include <%s>\n", include.c_str());
452 }
453 to->Write("\n");
454
Steven Morelandf3da0892018-10-05 14:52:01 -0700455 for (const auto& declaration : declarations_) {
456 declaration->Write(to);
457 }
Christopher Wileyf600a552015-09-12 14:07:44 -0700458}
459
Steven Morelandf3da0892018-10-05 14:52:01 -0700460CppHeader::CppHeader(const std::string& include_guard, const std::vector<std::string>& include_list,
461 std::vector<std::unique_ptr<Declaration>> declarations)
462 : Document(include_list, std::move(declarations)), include_guard_(include_guard) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700463
464void CppHeader::Write(CodeWriter* to) const {
465 to->Write("#ifndef %s\n", include_guard_.c_str());
466 to->Write("#define %s\n\n", include_guard_.c_str());
467
Christopher Wileyf944e792015-09-29 10:00:46 -0700468 Document::Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700469 to->Write("\n");
470
Christopher Wiley11a9d792016-02-24 17:20:33 -0800471 to->Write("#endif // %s\n", include_guard_.c_str());
Christopher Wileyf600a552015-09-12 14:07:44 -0700472}
473
Casey Dahlin34b86102015-09-16 16:03:06 -0700474CppSource::CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700475 std::vector<std::unique_ptr<Declaration>> declarations)
476 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700477
Christopher Wileyf944e792015-09-29 10:00:46 -0700478} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700479} // namespace aidl
480} // namespace android