blob: e5ed6d8ac1886854b86d7f9f882318c47428b9ce [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
Christopher Wileyfd7dc032016-02-02 17:58:39 -080095Enum::Enum(const string& name, const string& base_type)
96 : enum_name_(name), underlying_type_(base_type) {}
97
98Enum::Enum(const string& name) : Enum(name, "") {}
Christopher Wileya7a5c102015-09-29 16:26:52 -070099
100void Enum::Write(CodeWriter* to) const {
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800101 if (underlying_type_.empty()) {
102 to->Write("enum %s {\n", enum_name_.c_str());
103 } else {
104 to->Write("enum %s : %s {\n", enum_name_.c_str(), underlying_type_.c_str());
105 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900106 to->Indent();
Christopher Wileya7a5c102015-09-29 16:26:52 -0700107 for (const auto& field : fields_) {
108 if (field.value.empty()) {
Jiyong Parka755dc72018-06-29 13:52:24 +0900109 to->Write("%s,\n", field.key.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700110 } else {
Jiyong Parka755dc72018-06-29 13:52:24 +0900111 to->Write("%s = %s,\n", field.key.c_str(), field.value.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700112 }
113 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900114 to->Dedent();
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700115 to->Write("};\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700116}
117
118void Enum::AddValue(const string& key, const string& value) {
119 fields_.emplace_back(key, value);
120}
121
Christopher Wiley23285262015-10-09 15:06:14 -0700122ArgList::ArgList(const std::string& single_argument)
123 : ArgList(vector<string>{single_argument}) {}
124
Christopher Wileyf02facf2015-11-12 08:54:08 -0800125ArgList::ArgList(const std::vector<std::string>& arg_list) {
126 for (const auto& s : arg_list) {
127 arguments_.emplace_back(new LiteralExpression(s));
128 }
129}
130
131ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list)
132 : arguments_(std::move(arg_list)) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700133
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700134ArgList::ArgList(ArgList&& arg_list) noexcept : arguments_(std::move(arg_list.arguments_)) {}
Christopher Wileyade4b452015-10-10 11:06:03 -0700135
Christopher Wiley23285262015-10-09 15:06:14 -0700136void ArgList::Write(CodeWriter* to) const {
137 to->Write("(");
138 bool is_first = true;
139 for (const auto& s : arguments_) {
140 if (!is_first) { to->Write(", "); }
141 is_first = false;
Christopher Wileyf02facf2015-11-12 08:54:08 -0800142 s->Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700143 }
144 to->Write(")");
145}
146
Christopher Wileya7a5c102015-09-29 16:26:52 -0700147ConstructorDecl::ConstructorDecl(
148 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700149 ArgList&& arg_list)
Christopher Wileyb23149d2015-10-14 13:52:21 -0700150 : ConstructorDecl(name, std::move(arg_list), 0u) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700151
Christopher Wileyf944e792015-09-29 10:00:46 -0700152ConstructorDecl::ConstructorDecl(
Casey Dahlina834dd42015-09-23 11:52:15 -0700153 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700154 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700155 uint32_t modifiers)
Casey Dahlina834dd42015-09-23 11:52:15 -0700156 : name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700157 arguments_(std::move(arg_list)),
Christopher Wileyb23149d2015-10-14 13:52:21 -0700158 modifiers_(modifiers) {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700159
Christopher Wileyf944e792015-09-29 10:00:46 -0700160void ConstructorDecl::Write(CodeWriter* to) const {
Christopher Wileyb23149d2015-10-14 13:52:21 -0700161 if (modifiers_ & Modifiers::IS_VIRTUAL)
Casey Dahlina834dd42015-09-23 11:52:15 -0700162 to->Write("virtual ");
163
Christopher Wileyb23149d2015-10-14 13:52:21 -0700164 if (modifiers_ & Modifiers::IS_EXPLICIT)
165 to->Write("explicit ");
166
Christopher Wileyade4b452015-10-10 11:06:03 -0700167 to->Write("%s", name_.c_str());
Casey Dahlina834dd42015-09-23 11:52:15 -0700168
Christopher Wileyade4b452015-10-10 11:06:03 -0700169 arguments_.Write(to);
Casey Dahlina834dd42015-09-23 11:52:15 -0700170
Christopher Wileyb23149d2015-10-14 13:52:21 -0700171 if (modifiers_ & Modifiers::IS_DEFAULT)
Christopher Wileyf094d582015-10-08 15:50:15 -0700172 to->Write(" = default");
173
174 to->Write(";\n");
Casey Dahlina834dd42015-09-23 11:52:15 -0700175}
176
Christopher Wiley11a9d792016-02-24 17:20:33 -0800177MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list)
178 : name_(name),
179 arguments_(std::move(arg_list)) {}
180
181void MacroDecl::Write(CodeWriter* to) const {
182 to->Write("%s", name_.c_str());
183 arguments_.Write(to);
184 to->Write("\n");
185}
186
Christopher Wileyf944e792015-09-29 10:00:46 -0700187MethodDecl::MethodDecl(const std::string& return_type,
188 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700189 ArgList&& arg_list)
190 : MethodDecl(return_type, name, std::move(arg_list), 0u) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -0700191
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900192MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700193 uint32_t modifiers)
Casey Dahlin88924d62015-09-17 16:28:24 -0700194 : return_type_(return_type),
195 name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700196 arguments_(std::move(arg_list)),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700197 is_const_(modifiers & IS_CONST),
198 is_virtual_(modifiers & IS_VIRTUAL),
199 is_override_(modifiers & IS_OVERRIDE),
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700200 is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900201 is_static_(modifiers & IS_STATIC),
202 is_final_(modifiers & IS_FINAL) {}
Casey Dahlin88924d62015-09-17 16:28:24 -0700203
Christopher Wileyf944e792015-09-29 10:00:46 -0700204void MethodDecl::Write(CodeWriter* to) const {
Casey Dahlin88924d62015-09-17 16:28:24 -0700205 if (is_virtual_)
206 to->Write("virtual ");
207
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700208 if (is_static_)
209 to->Write("static ");
210
Christopher Wileyda695992015-10-05 11:31:41 -0700211 to->Write("%s %s", return_type_.c_str(), name_.c_str());
Casey Dahlin88924d62015-09-17 16:28:24 -0700212
Christopher Wileyade4b452015-10-10 11:06:03 -0700213 arguments_.Write(to);
Casey Dahlin88924d62015-09-17 16:28:24 -0700214
215 if (is_const_)
216 to->Write(" const");
217
Christopher Wiley0c732db2015-09-29 14:36:44 -0700218 if (is_override_)
219 to->Write(" override");
220
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900221 if (is_final_) to->Write(" final");
222
Christopher Wiley0c732db2015-09-29 14:36:44 -0700223 if (is_pure_virtual_)
224 to->Write(" = 0");
225
Casey Dahlin88924d62015-09-17 16:28:24 -0700226 to->Write(";\n");
Casey Dahlin60a49162015-09-17 14:23:10 -0700227}
228
Christopher Wileyda695992015-10-05 11:31:41 -0700229void StatementBlock::AddStatement(unique_ptr<AstNode> statement) {
230 statements_.push_back(std::move(statement));
231}
232
Christopher Wiley23285262015-10-09 15:06:14 -0700233void StatementBlock::AddStatement(AstNode* statement) {
234 statements_.emplace_back(statement);
235}
236
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700237void StatementBlock::AddLiteral(const std::string& expression_str,
Christopher Wileyda695992015-10-05 11:31:41 -0700238 bool add_semicolon) {
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700239 if (add_semicolon) {
240 statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str)));
241 } else {
242 statements_.push_back(unique_ptr<AstNode>(
243 new LiteralExpression(expression_str)));
244 }
Christopher Wileyda695992015-10-05 11:31:41 -0700245}
246
247void StatementBlock::Write(CodeWriter* to) const {
248 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900249 to->Indent();
Christopher Wileyda695992015-10-05 11:31:41 -0700250 for (const auto& statement : statements_) {
251 statement->Write(to);
252 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900253 to->Dedent();
Christopher Wileyda695992015-10-05 11:31:41 -0700254 to->Write("}\n");
255}
256
Christopher Wileyf9688b02015-10-08 17:17:50 -0700257ConstructorImpl::ConstructorImpl(const string& class_name,
258 ArgList&& arg_list,
259 const vector<string>& initializer_list)
260 : class_name_(class_name),
261 arguments_(std::move(arg_list)),
262 initializer_list_(initializer_list) {}
263
264void ConstructorImpl::Write(CodeWriter* to) const {
265 to->Write("%s::%s", class_name_.c_str(), class_name_.c_str());
266 arguments_.Write(to);
267 to->Write("\n");
268
269 bool is_first = true;
270 for (const string& i : initializer_list_) {
271 if (is_first) {
272 to->Write(" : %s", i.c_str());
273 } else {
274 to->Write(",\n %s", i.c_str());
275 }
276 is_first = false;
277 }
278
279 body_.Write(to);
280}
281
Christopher Wileyda695992015-10-05 11:31:41 -0700282MethodImpl::MethodImpl(const string& return_type,
283 const string& class_name,
284 const string& method_name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700285 ArgList&& arg_list,
Christopher Wileyda695992015-10-05 11:31:41 -0700286 bool is_const_method)
287 : return_type_(return_type),
288 method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700289 arguments_(std::move(arg_list)),
Christopher Wileyda695992015-10-05 11:31:41 -0700290 is_const_method_(is_const_method) {
291 if (!class_name.empty()) {
292 method_name_ = class_name + "::" + method_name;
293 }
294}
295
Christopher Wileyf9688b02015-10-08 17:17:50 -0700296StatementBlock* MethodImpl::GetStatementBlock() {
297 return &statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700298}
299
300void MethodImpl::Write(CodeWriter* to) const {
301 to->Write("%s %s", return_type_.c_str(), method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700302 arguments_.Write(to);
Christopher Wileyda695992015-10-05 11:31:41 -0700303 to->Write("%s ", (is_const_method_) ? " const" : "");
304 statements_.Write(to);
305}
306
307SwitchStatement::SwitchStatement(const std::string& expression)
308 : switch_expression_(expression) {}
309
310StatementBlock* SwitchStatement::AddCase(const string& value_expression) {
311 auto it = std::find(case_values_.begin(), case_values_.end(), value_expression);
312 if (it != case_values_.end()) {
313 LOG(ERROR) << "internal error: duplicate switch case labels";
314 return nullptr;
315 }
316 StatementBlock* ret = new StatementBlock();
317 case_values_.push_back(value_expression);
318 case_logic_.push_back(unique_ptr<StatementBlock>{ret});
319 return ret;
320}
321
322void SwitchStatement::Write(CodeWriter* to) const {
323 to->Write("switch (%s) {\n", switch_expression_.c_str());
324 for (size_t i = 0; i < case_values_.size(); ++i) {
325 const string& case_value = case_values_[i];
326 const unique_ptr<StatementBlock>& statements = case_logic_[i];
327 if (case_value.empty()) {
328 to->Write("default:\n");
329 } else {
330 to->Write("case %s:\n", case_value.c_str());
331 }
332 statements->Write(to);
333 to->Write("break;\n");
334 }
335 to->Write("}\n");
336}
337
Christopher Wiley23285262015-10-09 15:06:14 -0700338
339Assignment::Assignment(const std::string& left, const std::string& right)
340 : Assignment(left, new LiteralExpression{right}) {}
341
342Assignment::Assignment(const std::string& left, AstNode* right)
343 : lhs_(left),
344 rhs_(right) {}
345
346void Assignment::Write(CodeWriter* to) const {
347 to->Write("%s = ", lhs_.c_str());
348 rhs_->Write(to);
349 to->Write(";\n");
350}
351
352MethodCall::MethodCall(const std::string& method_name,
353 const std::string& single_argument)
Christopher Wileyade4b452015-10-10 11:06:03 -0700354 : MethodCall(method_name, ArgList{single_argument}) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700355
Christopher Wileyade4b452015-10-10 11:06:03 -0700356MethodCall::MethodCall(const std::string& method_name,
357 ArgList&& arg_list)
Christopher Wiley23285262015-10-09 15:06:14 -0700358 : method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700359 arguments_{std::move(arg_list)} {}
Christopher Wiley23285262015-10-09 15:06:14 -0700360
361void MethodCall::Write(CodeWriter* to) const {
362 to->Write("%s", method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700363 arguments_.Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700364}
365
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700366IfStatement::IfStatement(AstNode* expression, bool invert_expression)
367 : expression_(expression),
368 invert_expression_(invert_expression) {}
369
370void IfStatement::Write(CodeWriter* to) const {
Christopher Wiley864bc092015-11-10 11:45:23 -0800371 to->Write("if (%s", (invert_expression_) ? "!(" : "");
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700372 expression_->Write(to);
373 to->Write(")%s ", (invert_expression_) ? ")" : "");
374 on_true_.Write(to);
375
376 if (!on_false_.Empty()) {
377 to->Write("else ");
378 on_false_.Write(to);
379 }
380}
381
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700382Statement::Statement(unique_ptr<AstNode> expression)
383 : expression_(std::move(expression)) {}
Christopher Wileyda695992015-10-05 11:31:41 -0700384
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700385Statement::Statement(AstNode* expression) : expression_(expression) {}
386
387Statement::Statement(const string& expression)
388 : expression_(new LiteralExpression(expression)) {}
389
390void Statement::Write(CodeWriter* to) const {
391 expression_->Write(to);
392 to->Write(";\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700393}
394
Christopher Wileyd55db282015-10-20 18:16:47 -0700395Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs)
396 : left_(lhs),
397 right_(rhs),
398 operator_(comparison) {}
399
400void Comparison::Write(CodeWriter* to) const {
401 to->Write("((");
402 left_->Write(to);
403 to->Write(") %s (", operator_.c_str());
404 right_->Write(to);
405 to->Write("))");
406}
407
Christopher Wiley23285262015-10-09 15:06:14 -0700408LiteralExpression::LiteralExpression(const std::string& expression)
409 : expression_(expression) {}
410
411void LiteralExpression::Write(CodeWriter* to) const {
412 to->Write("%s", expression_.c_str());
413}
414
Casey Dahlin34b86102015-09-16 16:03:06 -0700415CppNamespace::CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700416 std::vector<unique_ptr<Declaration>> declarations)
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700417 : declarations_(std::move(declarations)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700418 name_(name) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700419
Christopher Wiley0c732db2015-09-29 14:36:44 -0700420CppNamespace::CppNamespace(const std::string& name,
421 unique_ptr<Declaration> declaration)
422 : name_(name) {
423 declarations_.push_back(std::move(declaration));
424}
425CppNamespace::CppNamespace(const std::string& name)
426 : name_(name) {}
427
Casey Dahlin34b86102015-09-16 16:03:06 -0700428void CppNamespace::Write(CodeWriter* to) const {
429 to->Write("namespace %s {\n\n", name_.c_str());
430
Casey Dahlin60a49162015-09-17 14:23:10 -0700431 for (const auto& dec : declarations_) {
Casey Dahlin34b86102015-09-16 16:03:06 -0700432 dec->Write(to);
Casey Dahlin60a49162015-09-17 14:23:10 -0700433 to->Write("\n");
434 }
Casey Dahlin34b86102015-09-16 16:03:06 -0700435
Casey Dahlin60a49162015-09-17 14:23:10 -0700436 to->Write("} // namespace %s\n", name_.c_str());
Casey Dahlin34b86102015-09-16 16:03:06 -0700437}
438
Christopher Wileyf944e792015-09-29 10:00:46 -0700439Document::Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700440 std::vector<unique_ptr<Declaration>> declarations)
441 : include_list_(include_list), declarations_(std::move(declarations)) {}
Casey Dahlin34b86102015-09-16 16:03:06 -0700442
Christopher Wileyf944e792015-09-29 10:00:46 -0700443void Document::Write(CodeWriter* to) const {
Casey Dahlin34b86102015-09-16 16:03:06 -0700444 for (const auto& include : include_list_) {
Christopher Wileyf600a552015-09-12 14:07:44 -0700445 to->Write("#include <%s>\n", include.c_str());
446 }
447 to->Write("\n");
448
Steven Morelandf3da0892018-10-05 14:52:01 -0700449 for (const auto& declaration : declarations_) {
450 declaration->Write(to);
451 }
Christopher Wileyf600a552015-09-12 14:07:44 -0700452}
453
Steven Morelandf3da0892018-10-05 14:52:01 -0700454CppHeader::CppHeader(const std::string& include_guard, const std::vector<std::string>& include_list,
455 std::vector<std::unique_ptr<Declaration>> declarations)
456 : Document(include_list, std::move(declarations)), include_guard_(include_guard) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700457
458void CppHeader::Write(CodeWriter* to) const {
459 to->Write("#ifndef %s\n", include_guard_.c_str());
460 to->Write("#define %s\n\n", include_guard_.c_str());
461
Christopher Wileyf944e792015-09-29 10:00:46 -0700462 Document::Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700463 to->Write("\n");
464
Christopher Wiley11a9d792016-02-24 17:20:33 -0800465 to->Write("#endif // %s\n", include_guard_.c_str());
Christopher Wileyf600a552015-09-12 14:07:44 -0700466}
467
Casey Dahlin34b86102015-09-16 16:03:06 -0700468CppSource::CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700469 std::vector<std::unique_ptr<Declaration>> declarations)
470 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700471
Christopher Wileyf944e792015-09-29 10:00:46 -0700472} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700473} // namespace aidl
474} // namespace android