blob: 7af94acec94405ed9b366f2d8affc3d76855b27a [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
Steven Moreland5557f1c2018-07-02 13:50:23 -070032LiteralDecl::LiteralDecl(const std::string& expression) : expression_(expression) {}
33
34void LiteralDecl::Write(CodeWriter* to) const {
35 to->Write("%s", expression_.c_str());
36}
37
Christopher Wiley0c732db2015-09-29 14:36:44 -070038ClassDecl::ClassDecl(const std::string& name, const std::string& parent)
39 : name_(name),
40 parent_(parent) {}
41
Christopher Wileyf944e792015-09-29 10:00:46 -070042ClassDecl::ClassDecl(const std::string& name, const std::string& parent,
43 std::vector<unique_ptr<Declaration>> public_members,
44 std::vector<unique_ptr<Declaration>> private_members)
Casey Dahlin60a49162015-09-17 14:23:10 -070045 : name_(name),
46 parent_(parent),
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070047 public_members_(std::move(public_members)),
48 private_members_(std::move(private_members)) {}
Casey Dahlin60a49162015-09-17 14:23:10 -070049
Christopher Wileyf944e792015-09-29 10:00:46 -070050void ClassDecl::Write(CodeWriter* to) const {
Casey Dahlin60a49162015-09-17 14:23:10 -070051 to->Write("class %s ", name_.c_str());
52
53 if (parent_.length() > 0)
54 to->Write(": public %s ", parent_.c_str());
55
Casey Dahlin88924d62015-09-17 16:28:24 -070056 to->Write("{\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070057
58 if (!public_members_.empty())
59 to->Write("public:\n");
60
Jiyong Parka755dc72018-06-29 13:52:24 +090061 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070062 for (const auto& dec : public_members_)
63 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090064 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070065
66 if (!private_members_.empty())
67 to->Write("private:\n");
68
Jiyong Parka755dc72018-06-29 13:52:24 +090069 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070070 for (const auto& dec : private_members_)
71 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090072 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070073
Casey Dahlin88924d62015-09-17 16:28:24 -070074 to->Write("}; // class %s\n", name_.c_str());
75}
76
Christopher Wiley0c732db2015-09-29 14:36:44 -070077void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) {
78 public_members_.push_back(std::move(member));
79}
80
81void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) {
82 private_members_.push_back(std::move(member));
83}
84
Casey Dahlind40e2fe2015-11-24 14:06:52 -080085Enum::EnumField::EnumField(const string& k, const string& v)
Christopher Wileya7a5c102015-09-29 16:26:52 -070086 : key(k),
87 value(v) {}
88
Christopher Wileyfd7dc032016-02-02 17:58:39 -080089Enum::Enum(const string& name, const string& base_type)
90 : enum_name_(name), underlying_type_(base_type) {}
91
92Enum::Enum(const string& name) : Enum(name, "") {}
Christopher Wileya7a5c102015-09-29 16:26:52 -070093
94void Enum::Write(CodeWriter* to) const {
Christopher Wileyfd7dc032016-02-02 17:58:39 -080095 if (underlying_type_.empty()) {
96 to->Write("enum %s {\n", enum_name_.c_str());
97 } else {
98 to->Write("enum %s : %s {\n", enum_name_.c_str(), underlying_type_.c_str());
99 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900100 to->Indent();
Christopher Wileya7a5c102015-09-29 16:26:52 -0700101 for (const auto& field : fields_) {
102 if (field.value.empty()) {
Jiyong Parka755dc72018-06-29 13:52:24 +0900103 to->Write("%s,\n", field.key.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700104 } else {
Jiyong Parka755dc72018-06-29 13:52:24 +0900105 to->Write("%s = %s,\n", field.key.c_str(), field.value.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700106 }
107 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900108 to->Dedent();
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700109 to->Write("};\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700110}
111
112void Enum::AddValue(const string& key, const string& value) {
113 fields_.emplace_back(key, value);
114}
115
Christopher Wiley23285262015-10-09 15:06:14 -0700116ArgList::ArgList(const std::string& single_argument)
117 : ArgList(vector<string>{single_argument}) {}
118
Christopher Wileyf02facf2015-11-12 08:54:08 -0800119ArgList::ArgList(const std::vector<std::string>& arg_list) {
120 for (const auto& s : arg_list) {
121 arguments_.emplace_back(new LiteralExpression(s));
122 }
123}
124
125ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list)
126 : arguments_(std::move(arg_list)) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700127
Christopher Wileyade4b452015-10-10 11:06:03 -0700128ArgList::ArgList(ArgList&& arg_list)
129 : arguments_(std::move(arg_list.arguments_)) {}
130
Christopher Wiley23285262015-10-09 15:06:14 -0700131void ArgList::Write(CodeWriter* to) const {
132 to->Write("(");
133 bool is_first = true;
134 for (const auto& s : arguments_) {
135 if (!is_first) { to->Write(", "); }
136 is_first = false;
Christopher Wileyf02facf2015-11-12 08:54:08 -0800137 s->Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700138 }
139 to->Write(")");
140}
141
Christopher Wileya7a5c102015-09-29 16:26:52 -0700142ConstructorDecl::ConstructorDecl(
143 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700144 ArgList&& arg_list)
Christopher Wileyb23149d2015-10-14 13:52:21 -0700145 : ConstructorDecl(name, std::move(arg_list), 0u) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700146
Christopher Wileyf944e792015-09-29 10:00:46 -0700147ConstructorDecl::ConstructorDecl(
Casey Dahlina834dd42015-09-23 11:52:15 -0700148 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700149 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700150 uint32_t modifiers)
Casey Dahlina834dd42015-09-23 11:52:15 -0700151 : name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700152 arguments_(std::move(arg_list)),
Christopher Wileyb23149d2015-10-14 13:52:21 -0700153 modifiers_(modifiers) {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700154
Christopher Wileyf944e792015-09-29 10:00:46 -0700155void ConstructorDecl::Write(CodeWriter* to) const {
Christopher Wileyb23149d2015-10-14 13:52:21 -0700156 if (modifiers_ & Modifiers::IS_VIRTUAL)
Casey Dahlina834dd42015-09-23 11:52:15 -0700157 to->Write("virtual ");
158
Christopher Wileyb23149d2015-10-14 13:52:21 -0700159 if (modifiers_ & Modifiers::IS_EXPLICIT)
160 to->Write("explicit ");
161
Christopher Wileyade4b452015-10-10 11:06:03 -0700162 to->Write("%s", name_.c_str());
Casey Dahlina834dd42015-09-23 11:52:15 -0700163
Christopher Wileyade4b452015-10-10 11:06:03 -0700164 arguments_.Write(to);
Casey Dahlina834dd42015-09-23 11:52:15 -0700165
Christopher Wileyb23149d2015-10-14 13:52:21 -0700166 if (modifiers_ & Modifiers::IS_DEFAULT)
Christopher Wileyf094d582015-10-08 15:50:15 -0700167 to->Write(" = default");
168
169 to->Write(";\n");
Casey Dahlina834dd42015-09-23 11:52:15 -0700170}
171
Christopher Wiley11a9d792016-02-24 17:20:33 -0800172MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list)
173 : name_(name),
174 arguments_(std::move(arg_list)) {}
175
176void MacroDecl::Write(CodeWriter* to) const {
177 to->Write("%s", name_.c_str());
178 arguments_.Write(to);
179 to->Write("\n");
180}
181
Christopher Wileyf944e792015-09-29 10:00:46 -0700182MethodDecl::MethodDecl(const std::string& return_type,
183 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700184 ArgList&& arg_list)
185 : MethodDecl(return_type, name, std::move(arg_list), 0u) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -0700186
187MethodDecl::MethodDecl(const std::string& return_type,
188 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700189 ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700190 uint32_t modifiers)
Casey Dahlin88924d62015-09-17 16:28:24 -0700191 : return_type_(return_type),
192 name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700193 arguments_(std::move(arg_list)),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700194 is_const_(modifiers & IS_CONST),
195 is_virtual_(modifiers & IS_VIRTUAL),
196 is_override_(modifiers & IS_OVERRIDE),
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700197 is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
198 is_static_(modifiers & IS_STATIC) {}
Casey Dahlin88924d62015-09-17 16:28:24 -0700199
Christopher Wileyf944e792015-09-29 10:00:46 -0700200void MethodDecl::Write(CodeWriter* to) const {
Casey Dahlin88924d62015-09-17 16:28:24 -0700201 if (is_virtual_)
202 to->Write("virtual ");
203
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700204 if (is_static_)
205 to->Write("static ");
206
Christopher Wileyda695992015-10-05 11:31:41 -0700207 to->Write("%s %s", return_type_.c_str(), name_.c_str());
Casey Dahlin88924d62015-09-17 16:28:24 -0700208
Christopher Wileyade4b452015-10-10 11:06:03 -0700209 arguments_.Write(to);
Casey Dahlin88924d62015-09-17 16:28:24 -0700210
211 if (is_const_)
212 to->Write(" const");
213
Christopher Wiley0c732db2015-09-29 14:36:44 -0700214 if (is_override_)
215 to->Write(" override");
216
217 if (is_pure_virtual_)
218 to->Write(" = 0");
219
Casey Dahlin88924d62015-09-17 16:28:24 -0700220 to->Write(";\n");
Casey Dahlin60a49162015-09-17 14:23:10 -0700221}
222
Christopher Wileyda695992015-10-05 11:31:41 -0700223void StatementBlock::AddStatement(unique_ptr<AstNode> statement) {
224 statements_.push_back(std::move(statement));
225}
226
Christopher Wiley23285262015-10-09 15:06:14 -0700227void StatementBlock::AddStatement(AstNode* statement) {
228 statements_.emplace_back(statement);
229}
230
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700231void StatementBlock::AddLiteral(const std::string& expression_str,
Christopher Wileyda695992015-10-05 11:31:41 -0700232 bool add_semicolon) {
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700233 if (add_semicolon) {
234 statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str)));
235 } else {
236 statements_.push_back(unique_ptr<AstNode>(
237 new LiteralExpression(expression_str)));
238 }
Christopher Wileyda695992015-10-05 11:31:41 -0700239}
240
241void StatementBlock::Write(CodeWriter* to) const {
242 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900243 to->Indent();
Christopher Wileyda695992015-10-05 11:31:41 -0700244 for (const auto& statement : statements_) {
245 statement->Write(to);
246 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900247 to->Dedent();
Christopher Wileyda695992015-10-05 11:31:41 -0700248 to->Write("}\n");
249}
250
Christopher Wileyf9688b02015-10-08 17:17:50 -0700251ConstructorImpl::ConstructorImpl(const string& class_name,
252 ArgList&& arg_list,
253 const vector<string>& initializer_list)
254 : class_name_(class_name),
255 arguments_(std::move(arg_list)),
256 initializer_list_(initializer_list) {}
257
258void ConstructorImpl::Write(CodeWriter* to) const {
259 to->Write("%s::%s", class_name_.c_str(), class_name_.c_str());
260 arguments_.Write(to);
261 to->Write("\n");
262
263 bool is_first = true;
264 for (const string& i : initializer_list_) {
265 if (is_first) {
266 to->Write(" : %s", i.c_str());
267 } else {
268 to->Write(",\n %s", i.c_str());
269 }
270 is_first = false;
271 }
272
273 body_.Write(to);
274}
275
Christopher Wileyda695992015-10-05 11:31:41 -0700276MethodImpl::MethodImpl(const string& return_type,
277 const string& class_name,
278 const string& method_name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700279 ArgList&& arg_list,
Christopher Wileyda695992015-10-05 11:31:41 -0700280 bool is_const_method)
281 : return_type_(return_type),
282 method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700283 arguments_(std::move(arg_list)),
Christopher Wileyda695992015-10-05 11:31:41 -0700284 is_const_method_(is_const_method) {
285 if (!class_name.empty()) {
286 method_name_ = class_name + "::" + method_name;
287 }
288}
289
Christopher Wileyf9688b02015-10-08 17:17:50 -0700290StatementBlock* MethodImpl::GetStatementBlock() {
291 return &statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700292}
293
294void MethodImpl::Write(CodeWriter* to) const {
295 to->Write("%s %s", return_type_.c_str(), method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700296 arguments_.Write(to);
Christopher Wileyda695992015-10-05 11:31:41 -0700297 to->Write("%s ", (is_const_method_) ? " const" : "");
298 statements_.Write(to);
299}
300
301SwitchStatement::SwitchStatement(const std::string& expression)
302 : switch_expression_(expression) {}
303
304StatementBlock* SwitchStatement::AddCase(const string& value_expression) {
305 auto it = std::find(case_values_.begin(), case_values_.end(), value_expression);
306 if (it != case_values_.end()) {
307 LOG(ERROR) << "internal error: duplicate switch case labels";
308 return nullptr;
309 }
310 StatementBlock* ret = new StatementBlock();
311 case_values_.push_back(value_expression);
312 case_logic_.push_back(unique_ptr<StatementBlock>{ret});
313 return ret;
314}
315
316void SwitchStatement::Write(CodeWriter* to) const {
317 to->Write("switch (%s) {\n", switch_expression_.c_str());
318 for (size_t i = 0; i < case_values_.size(); ++i) {
319 const string& case_value = case_values_[i];
320 const unique_ptr<StatementBlock>& statements = case_logic_[i];
321 if (case_value.empty()) {
322 to->Write("default:\n");
323 } else {
324 to->Write("case %s:\n", case_value.c_str());
325 }
326 statements->Write(to);
327 to->Write("break;\n");
328 }
329 to->Write("}\n");
330}
331
Christopher Wiley23285262015-10-09 15:06:14 -0700332
333Assignment::Assignment(const std::string& left, const std::string& right)
334 : Assignment(left, new LiteralExpression{right}) {}
335
336Assignment::Assignment(const std::string& left, AstNode* right)
337 : lhs_(left),
338 rhs_(right) {}
339
340void Assignment::Write(CodeWriter* to) const {
341 to->Write("%s = ", lhs_.c_str());
342 rhs_->Write(to);
343 to->Write(";\n");
344}
345
346MethodCall::MethodCall(const std::string& method_name,
347 const std::string& single_argument)
Christopher Wileyade4b452015-10-10 11:06:03 -0700348 : MethodCall(method_name, ArgList{single_argument}) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700349
Christopher Wileyade4b452015-10-10 11:06:03 -0700350MethodCall::MethodCall(const std::string& method_name,
351 ArgList&& arg_list)
Christopher Wiley23285262015-10-09 15:06:14 -0700352 : method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700353 arguments_{std::move(arg_list)} {}
Christopher Wiley23285262015-10-09 15:06:14 -0700354
355void MethodCall::Write(CodeWriter* to) const {
356 to->Write("%s", method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700357 arguments_.Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700358}
359
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700360IfStatement::IfStatement(AstNode* expression, bool invert_expression)
361 : expression_(expression),
362 invert_expression_(invert_expression) {}
363
364void IfStatement::Write(CodeWriter* to) const {
Christopher Wiley864bc092015-11-10 11:45:23 -0800365 to->Write("if (%s", (invert_expression_) ? "!(" : "");
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700366 expression_->Write(to);
367 to->Write(")%s ", (invert_expression_) ? ")" : "");
368 on_true_.Write(to);
369
370 if (!on_false_.Empty()) {
371 to->Write("else ");
372 on_false_.Write(to);
373 }
374}
375
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700376Statement::Statement(unique_ptr<AstNode> expression)
377 : expression_(std::move(expression)) {}
Christopher Wileyda695992015-10-05 11:31:41 -0700378
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700379Statement::Statement(AstNode* expression) : expression_(expression) {}
380
381Statement::Statement(const string& expression)
382 : expression_(new LiteralExpression(expression)) {}
383
384void Statement::Write(CodeWriter* to) const {
385 expression_->Write(to);
386 to->Write(";\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700387}
388
Christopher Wileyd55db282015-10-20 18:16:47 -0700389Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs)
390 : left_(lhs),
391 right_(rhs),
392 operator_(comparison) {}
393
394void Comparison::Write(CodeWriter* to) const {
395 to->Write("((");
396 left_->Write(to);
397 to->Write(") %s (", operator_.c_str());
398 right_->Write(to);
399 to->Write("))");
400}
401
Christopher Wiley23285262015-10-09 15:06:14 -0700402LiteralExpression::LiteralExpression(const std::string& expression)
403 : expression_(expression) {}
404
405void LiteralExpression::Write(CodeWriter* to) const {
406 to->Write("%s", expression_.c_str());
407}
408
Casey Dahlin34b86102015-09-16 16:03:06 -0700409CppNamespace::CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700410 std::vector<unique_ptr<Declaration>> declarations)
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700411 : declarations_(std::move(declarations)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700412 name_(name) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700413
Christopher Wiley0c732db2015-09-29 14:36:44 -0700414CppNamespace::CppNamespace(const std::string& name,
415 unique_ptr<Declaration> declaration)
416 : name_(name) {
417 declarations_.push_back(std::move(declaration));
418}
419CppNamespace::CppNamespace(const std::string& name)
420 : name_(name) {}
421
Casey Dahlin34b86102015-09-16 16:03:06 -0700422void CppNamespace::Write(CodeWriter* to) const {
423 to->Write("namespace %s {\n\n", name_.c_str());
424
Casey Dahlin60a49162015-09-17 14:23:10 -0700425 for (const auto& dec : declarations_) {
Casey Dahlin34b86102015-09-16 16:03:06 -0700426 dec->Write(to);
Casey Dahlin60a49162015-09-17 14:23:10 -0700427 to->Write("\n");
428 }
Casey Dahlin34b86102015-09-16 16:03:06 -0700429
Casey Dahlin60a49162015-09-17 14:23:10 -0700430 to->Write("} // namespace %s\n", name_.c_str());
Casey Dahlin34b86102015-09-16 16:03:06 -0700431}
432
Christopher Wileyf944e792015-09-29 10:00:46 -0700433Document::Document(const std::vector<std::string>& include_list,
434 unique_ptr<CppNamespace> a_namespace)
Casey Dahlin34b86102015-09-16 16:03:06 -0700435 : include_list_(include_list),
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700436 namespace_(std::move(a_namespace)) {}
Casey Dahlin34b86102015-09-16 16:03:06 -0700437
Christopher Wileyf944e792015-09-29 10:00:46 -0700438void Document::Write(CodeWriter* to) const {
Casey Dahlin34b86102015-09-16 16:03:06 -0700439 for (const auto& include : include_list_) {
Christopher Wileyf600a552015-09-12 14:07:44 -0700440 to->Write("#include <%s>\n", include.c_str());
441 }
442 to->Write("\n");
443
Casey Dahlin34b86102015-09-16 16:03:06 -0700444 namespace_->Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700445}
446
Christopher Wileyf600a552015-09-12 14:07:44 -0700447CppHeader::CppHeader(const std::string& include_guard,
448 const std::vector<std::string>& include_list,
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700449 unique_ptr<CppNamespace> a_namespace)
Christopher Wileyf944e792015-09-29 10:00:46 -0700450 : Document(include_list, std::move(a_namespace)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700451 include_guard_(include_guard) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700452
453void CppHeader::Write(CodeWriter* to) const {
454 to->Write("#ifndef %s\n", include_guard_.c_str());
455 to->Write("#define %s\n\n", include_guard_.c_str());
456
Christopher Wileyf944e792015-09-29 10:00:46 -0700457 Document::Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700458 to->Write("\n");
459
Christopher Wiley11a9d792016-02-24 17:20:33 -0800460 to->Write("#endif // %s\n", include_guard_.c_str());
Christopher Wileyf600a552015-09-12 14:07:44 -0700461}
462
Casey Dahlin34b86102015-09-16 16:03:06 -0700463CppSource::CppSource(const std::vector<std::string>& include_list,
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700464 unique_ptr<CppNamespace> a_namespace)
Christopher Wileyf944e792015-09-29 10:00:46 -0700465 : Document(include_list, std::move(a_namespace)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700466
Christopher Wileyf944e792015-09-29 10:00:46 -0700467} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700468} // namespace aidl
469} // namespace android