blob: b57527b00cd51e02fcaadb3ca39dc5c0da5aaeb4 [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
Jooyung Han535c5e82020-12-29 15:16:59 +090021#include <android-base/strings.h>
22
Christopher Wileyf600a552015-09-12 14:07:44 -070023#include "code_writer.h"
Christopher Wileyda695992015-10-05 11:31:41 -070024#include "logging.h"
Christopher Wileyf600a552015-09-12 14:07:44 -070025
Christopher Wileya7a5c102015-09-29 16:26:52 -070026using std::string;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070027using std::unique_ptr;
Christopher Wileyda695992015-10-05 11:31:41 -070028using std::vector;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070029
Christopher Wileyf600a552015-09-12 14:07:44 -070030namespace android {
31namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070032namespace cpp {
Christopher Wileyf600a552015-09-12 14:07:44 -070033
Jiyong Park176905e2018-07-04 22:29:41 +090034std::string AstNode::ToString() {
35 std::string str;
36 Write(CodeWriter::ForString(&str).get());
37 return str;
38}
39
Steven Moreland5557f1c2018-07-02 13:50:23 -070040LiteralDecl::LiteralDecl(const std::string& expression) : expression_(expression) {}
41
42void LiteralDecl::Write(CodeWriter* to) const {
43 to->Write("%s", expression_.c_str());
44}
45
Devin Moore53fc99c2020-08-12 08:07:52 -070046ClassDecl::ClassDecl(const std::string& name, const std::string& parent,
47 const std::vector<std::string>& template_params)
48 : name_(name), parent_(parent), template_params_(template_params) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -070049
Christopher Wileyf944e792015-09-29 10:00:46 -070050ClassDecl::ClassDecl(const std::string& name, const std::string& parent,
Devin Moore53fc99c2020-08-12 08:07:52 -070051 const std::vector<std::string>& template_params,
Christopher Wileyf944e792015-09-29 10:00:46 -070052 std::vector<unique_ptr<Declaration>> public_members,
53 std::vector<unique_ptr<Declaration>> private_members)
Casey Dahlin60a49162015-09-17 14:23:10 -070054 : name_(name),
55 parent_(parent),
Devin Moore53fc99c2020-08-12 08:07:52 -070056 template_params_(template_params),
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070057 public_members_(std::move(public_members)),
58 private_members_(std::move(private_members)) {}
Casey Dahlin60a49162015-09-17 14:23:10 -070059
Christopher Wileyf944e792015-09-29 10:00:46 -070060void ClassDecl::Write(CodeWriter* to) const {
Devin Moore53fc99c2020-08-12 08:07:52 -070061 if (!template_params_.empty())
62 to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str());
63
Casey Dahlin60a49162015-09-17 14:23:10 -070064 to->Write("class %s ", name_.c_str());
65
Devin Moore53fc99c2020-08-12 08:07:52 -070066 if (parent_.length() > 0) to->Write(": public %s ", parent_.c_str());
Casey Dahlin60a49162015-09-17 14:23:10 -070067
Casey Dahlin88924d62015-09-17 16:28:24 -070068 to->Write("{\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070069
Devin Moore53fc99c2020-08-12 08:07:52 -070070 if (!public_members_.empty()) to->Write("public:\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070071
Jiyong Parka755dc72018-06-29 13:52:24 +090072 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070073 for (const auto& dec : public_members_)
74 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090075 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070076
Devin Moore53fc99c2020-08-12 08:07:52 -070077 if (!private_members_.empty()) to->Write("private:\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070078
Jiyong Parka755dc72018-06-29 13:52:24 +090079 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070080 for (const auto& dec : private_members_)
81 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090082 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070083
Casey Dahlin88924d62015-09-17 16:28:24 -070084 to->Write("}; // class %s\n", name_.c_str());
85}
86
Christopher Wiley0c732db2015-09-29 14:36:44 -070087void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) {
88 public_members_.push_back(std::move(member));
89}
90
91void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) {
92 private_members_.push_back(std::move(member));
93}
94
Jooyung Han14af79b2020-10-12 03:30:41 +090095Enum::EnumField::EnumField(const string& k, const string& v, const string& c)
96 : key(k), value(v), comment(c) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -070097
Daniel Norman85aed542019-08-21 12:01:14 -070098Enum::Enum(const string& name, const string& base_type, bool is_class)
99 : enum_name_(name), underlying_type_(base_type), is_class_(is_class) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700100
101void Enum::Write(CodeWriter* to) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700102 to->Write("enum ");
103 if (is_class_) {
104 to->Write("class ");
105 }
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800106 if (underlying_type_.empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700107 to->Write("%s {\n", enum_name_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800108 } else {
Daniel Norman85aed542019-08-21 12:01:14 -0700109 to->Write("%s : %s {\n", enum_name_.c_str(), underlying_type_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800110 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900111 to->Indent();
Christopher Wileya7a5c102015-09-29 16:26:52 -0700112 for (const auto& field : fields_) {
Jooyung Han14af79b2020-10-12 03:30:41 +0900113 if (!field.comment.empty()) {
114 to->Write("%s\n", field.comment.c_str());
115 }
Christopher Wileya7a5c102015-09-29 16:26:52 -0700116 if (field.value.empty()) {
Jiyong Parka755dc72018-06-29 13:52:24 +0900117 to->Write("%s,\n", field.key.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700118 } else {
Jiyong Parka755dc72018-06-29 13:52:24 +0900119 to->Write("%s = %s,\n", field.key.c_str(), field.value.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700120 }
121 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900122 to->Dedent();
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700123 to->Write("};\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700124}
125
Jooyung Han14af79b2020-10-12 03:30:41 +0900126void Enum::AddValue(const string& key, const string& value, const string& comment) {
127 fields_.emplace_back(key, value, comment);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700128}
129
Christopher Wiley23285262015-10-09 15:06:14 -0700130ArgList::ArgList(const std::string& single_argument)
131 : ArgList(vector<string>{single_argument}) {}
132
Christopher Wileyf02facf2015-11-12 08:54:08 -0800133ArgList::ArgList(const std::vector<std::string>& arg_list) {
134 for (const auto& s : arg_list) {
135 arguments_.emplace_back(new LiteralExpression(s));
136 }
137}
138
139ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list)
140 : arguments_(std::move(arg_list)) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700141
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700142ArgList::ArgList(ArgList&& arg_list) noexcept : arguments_(std::move(arg_list.arguments_)) {}
Christopher Wileyade4b452015-10-10 11:06:03 -0700143
Christopher Wiley23285262015-10-09 15:06:14 -0700144void ArgList::Write(CodeWriter* to) const {
145 to->Write("(");
146 bool is_first = true;
147 for (const auto& s : arguments_) {
148 if (!is_first) { to->Write(", "); }
149 is_first = false;
Christopher Wileyf02facf2015-11-12 08:54:08 -0800150 s->Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700151 }
152 to->Write(")");
153}
154
Christopher Wileya7a5c102015-09-29 16:26:52 -0700155ConstructorDecl::ConstructorDecl(
156 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700157 ArgList&& arg_list)
Christopher Wileyb23149d2015-10-14 13:52:21 -0700158 : ConstructorDecl(name, std::move(arg_list), 0u) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700159
Christopher Wileyf944e792015-09-29 10:00:46 -0700160ConstructorDecl::ConstructorDecl(
Casey Dahlina834dd42015-09-23 11:52:15 -0700161 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700162 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700163 uint32_t modifiers)
Casey Dahlina834dd42015-09-23 11:52:15 -0700164 : name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700165 arguments_(std::move(arg_list)),
Christopher Wileyb23149d2015-10-14 13:52:21 -0700166 modifiers_(modifiers) {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700167
Christopher Wileyf944e792015-09-29 10:00:46 -0700168void ConstructorDecl::Write(CodeWriter* to) const {
Christopher Wileyb23149d2015-10-14 13:52:21 -0700169 if (modifiers_ & Modifiers::IS_VIRTUAL)
Casey Dahlina834dd42015-09-23 11:52:15 -0700170 to->Write("virtual ");
171
Christopher Wileyb23149d2015-10-14 13:52:21 -0700172 if (modifiers_ & Modifiers::IS_EXPLICIT)
173 to->Write("explicit ");
174
Christopher Wileyade4b452015-10-10 11:06:03 -0700175 to->Write("%s", name_.c_str());
Casey Dahlina834dd42015-09-23 11:52:15 -0700176
Christopher Wileyade4b452015-10-10 11:06:03 -0700177 arguments_.Write(to);
Casey Dahlina834dd42015-09-23 11:52:15 -0700178
Christopher Wileyb23149d2015-10-14 13:52:21 -0700179 if (modifiers_ & Modifiers::IS_DEFAULT)
Christopher Wileyf094d582015-10-08 15:50:15 -0700180 to->Write(" = default");
181
182 to->Write(";\n");
Casey Dahlina834dd42015-09-23 11:52:15 -0700183}
184
Christopher Wiley11a9d792016-02-24 17:20:33 -0800185MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list)
186 : name_(name),
187 arguments_(std::move(arg_list)) {}
188
189void MacroDecl::Write(CodeWriter* to) const {
190 to->Write("%s", name_.c_str());
191 arguments_.Write(to);
192 to->Write("\n");
193}
194
Christopher Wileyf944e792015-09-29 10:00:46 -0700195MethodDecl::MethodDecl(const std::string& return_type,
196 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700197 ArgList&& arg_list)
198 : MethodDecl(return_type, name, std::move(arg_list), 0u) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -0700199
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900200MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700201 uint32_t modifiers)
Casey Dahlin88924d62015-09-17 16:28:24 -0700202 : return_type_(return_type),
203 name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700204 arguments_(std::move(arg_list)),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700205 is_const_(modifiers & IS_CONST),
206 is_virtual_(modifiers & IS_VIRTUAL),
207 is_override_(modifiers & IS_OVERRIDE),
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700208 is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900209 is_static_(modifiers & IS_STATIC),
210 is_final_(modifiers & IS_FINAL) {}
Casey Dahlin88924d62015-09-17 16:28:24 -0700211
Christopher Wileyf944e792015-09-29 10:00:46 -0700212void MethodDecl::Write(CodeWriter* to) const {
Casey Dahlin88924d62015-09-17 16:28:24 -0700213 if (is_virtual_)
214 to->Write("virtual ");
215
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700216 if (is_static_)
217 to->Write("static ");
218
Christopher Wileyda695992015-10-05 11:31:41 -0700219 to->Write("%s %s", return_type_.c_str(), name_.c_str());
Casey Dahlin88924d62015-09-17 16:28:24 -0700220
Christopher Wileyade4b452015-10-10 11:06:03 -0700221 arguments_.Write(to);
Casey Dahlin88924d62015-09-17 16:28:24 -0700222
223 if (is_const_)
224 to->Write(" const");
225
Christopher Wiley0c732db2015-09-29 14:36:44 -0700226 if (is_override_)
227 to->Write(" override");
228
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900229 if (is_final_) to->Write(" final");
230
Christopher Wiley0c732db2015-09-29 14:36:44 -0700231 if (is_pure_virtual_)
232 to->Write(" = 0");
233
Casey Dahlin88924d62015-09-17 16:28:24 -0700234 to->Write(";\n");
Casey Dahlin60a49162015-09-17 14:23:10 -0700235}
236
Christopher Wileyda695992015-10-05 11:31:41 -0700237void StatementBlock::AddStatement(unique_ptr<AstNode> statement) {
238 statements_.push_back(std::move(statement));
239}
240
Christopher Wiley23285262015-10-09 15:06:14 -0700241void StatementBlock::AddStatement(AstNode* statement) {
242 statements_.emplace_back(statement);
243}
244
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700245void StatementBlock::AddLiteral(const std::string& expression_str,
Christopher Wileyda695992015-10-05 11:31:41 -0700246 bool add_semicolon) {
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700247 if (add_semicolon) {
248 statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str)));
249 } else {
250 statements_.push_back(unique_ptr<AstNode>(
251 new LiteralExpression(expression_str)));
252 }
Christopher Wileyda695992015-10-05 11:31:41 -0700253}
254
255void StatementBlock::Write(CodeWriter* to) const {
256 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900257 to->Indent();
Christopher Wileyda695992015-10-05 11:31:41 -0700258 for (const auto& statement : statements_) {
259 statement->Write(to);
260 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900261 to->Dedent();
Christopher Wileyda695992015-10-05 11:31:41 -0700262 to->Write("}\n");
263}
264
Christopher Wileyf9688b02015-10-08 17:17:50 -0700265ConstructorImpl::ConstructorImpl(const string& class_name,
266 ArgList&& arg_list,
267 const vector<string>& initializer_list)
268 : class_name_(class_name),
269 arguments_(std::move(arg_list)),
270 initializer_list_(initializer_list) {}
271
Steven Morelanda57d0a62019-07-30 09:41:14 -0700272StatementBlock* ConstructorImpl::GetStatementBlock() {
273 return &body_;
274}
275
Christopher Wileyf9688b02015-10-08 17:17:50 -0700276void ConstructorImpl::Write(CodeWriter* to) const {
277 to->Write("%s::%s", class_name_.c_str(), class_name_.c_str());
278 arguments_.Write(to);
279 to->Write("\n");
280
281 bool is_first = true;
282 for (const string& i : initializer_list_) {
283 if (is_first) {
284 to->Write(" : %s", i.c_str());
285 } else {
286 to->Write(",\n %s", i.c_str());
287 }
288 is_first = false;
289 }
290
291 body_.Write(to);
292}
293
Devin Moore53fc99c2020-08-12 08:07:52 -0700294MethodImpl::MethodImpl(const string& return_type, const string& class_name,
295 const string& method_name, const std::vector<std::string>& template_params,
296 ArgList&& arg_list, bool is_const_method)
Christopher Wileyda695992015-10-05 11:31:41 -0700297 : return_type_(return_type),
298 method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700299 arguments_(std::move(arg_list)),
Devin Moore53fc99c2020-08-12 08:07:52 -0700300 is_const_method_(is_const_method),
301 template_params_(template_params) {
Christopher Wileyda695992015-10-05 11:31:41 -0700302 if (!class_name.empty()) {
Devin Moore53fc99c2020-08-12 08:07:52 -0700303 if (!template_params.empty()) {
304 method_name_ = class_name + "<" + base::Join(template_params, ",") + ">::" + method_name;
305 } else {
306 method_name_ = class_name + "::" + method_name;
307 }
Christopher Wileyda695992015-10-05 11:31:41 -0700308 }
309}
310
Christopher Wileyf9688b02015-10-08 17:17:50 -0700311StatementBlock* MethodImpl::GetStatementBlock() {
312 return &statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700313}
314
315void MethodImpl::Write(CodeWriter* to) const {
Devin Moore53fc99c2020-08-12 08:07:52 -0700316 if (!template_params_.empty())
317 to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str());
Christopher Wileyda695992015-10-05 11:31:41 -0700318 to->Write("%s %s", return_type_.c_str(), method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700319 arguments_.Write(to);
Christopher Wileyda695992015-10-05 11:31:41 -0700320 to->Write("%s ", (is_const_method_) ? " const" : "");
321 statements_.Write(to);
322}
323
324SwitchStatement::SwitchStatement(const std::string& expression)
325 : switch_expression_(expression) {}
326
327StatementBlock* SwitchStatement::AddCase(const string& value_expression) {
328 auto it = std::find(case_values_.begin(), case_values_.end(), value_expression);
329 if (it != case_values_.end()) {
Steven Moreland21780812020-09-11 01:29:45 +0000330 AIDL_ERROR(value_expression) << "Duplicate switch case labels";
Christopher Wileyda695992015-10-05 11:31:41 -0700331 return nullptr;
332 }
333 StatementBlock* ret = new StatementBlock();
334 case_values_.push_back(value_expression);
335 case_logic_.push_back(unique_ptr<StatementBlock>{ret});
336 return ret;
337}
338
339void SwitchStatement::Write(CodeWriter* to) const {
340 to->Write("switch (%s) {\n", switch_expression_.c_str());
341 for (size_t i = 0; i < case_values_.size(); ++i) {
342 const string& case_value = case_values_[i];
343 const unique_ptr<StatementBlock>& statements = case_logic_[i];
344 if (case_value.empty()) {
345 to->Write("default:\n");
346 } else {
347 to->Write("case %s:\n", case_value.c_str());
348 }
349 statements->Write(to);
350 to->Write("break;\n");
351 }
352 to->Write("}\n");
353}
354
Christopher Wiley23285262015-10-09 15:06:14 -0700355
356Assignment::Assignment(const std::string& left, const std::string& right)
357 : Assignment(left, new LiteralExpression{right}) {}
358
359Assignment::Assignment(const std::string& left, AstNode* right)
360 : lhs_(left),
361 rhs_(right) {}
362
363void Assignment::Write(CodeWriter* to) const {
364 to->Write("%s = ", lhs_.c_str());
365 rhs_->Write(to);
366 to->Write(";\n");
367}
368
369MethodCall::MethodCall(const std::string& method_name,
370 const std::string& single_argument)
Christopher Wileyade4b452015-10-10 11:06:03 -0700371 : MethodCall(method_name, ArgList{single_argument}) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700372
Christopher Wileyade4b452015-10-10 11:06:03 -0700373MethodCall::MethodCall(const std::string& method_name,
374 ArgList&& arg_list)
Christopher Wiley23285262015-10-09 15:06:14 -0700375 : method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700376 arguments_{std::move(arg_list)} {}
Christopher Wiley23285262015-10-09 15:06:14 -0700377
378void MethodCall::Write(CodeWriter* to) const {
379 to->Write("%s", method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700380 arguments_.Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700381}
382
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700383IfStatement::IfStatement(AstNode* expression, bool invert_expression)
384 : expression_(expression),
385 invert_expression_(invert_expression) {}
386
387void IfStatement::Write(CodeWriter* to) const {
Christopher Wiley864bc092015-11-10 11:45:23 -0800388 to->Write("if (%s", (invert_expression_) ? "!(" : "");
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700389 expression_->Write(to);
390 to->Write(")%s ", (invert_expression_) ? ")" : "");
391 on_true_.Write(to);
392
393 if (!on_false_.Empty()) {
394 to->Write("else ");
395 on_false_.Write(to);
396 }
397}
398
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700399Statement::Statement(unique_ptr<AstNode> expression)
400 : expression_(std::move(expression)) {}
Christopher Wileyda695992015-10-05 11:31:41 -0700401
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700402Statement::Statement(AstNode* expression) : expression_(expression) {}
403
404Statement::Statement(const string& expression)
405 : expression_(new LiteralExpression(expression)) {}
406
407void Statement::Write(CodeWriter* to) const {
408 expression_->Write(to);
409 to->Write(";\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700410}
411
Christopher Wileyd55db282015-10-20 18:16:47 -0700412Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs)
413 : left_(lhs),
414 right_(rhs),
415 operator_(comparison) {}
416
417void Comparison::Write(CodeWriter* to) const {
418 to->Write("((");
419 left_->Write(to);
420 to->Write(") %s (", operator_.c_str());
421 right_->Write(to);
422 to->Write("))");
423}
424
Christopher Wiley23285262015-10-09 15:06:14 -0700425LiteralExpression::LiteralExpression(const std::string& expression)
426 : expression_(expression) {}
427
428void LiteralExpression::Write(CodeWriter* to) const {
429 to->Write("%s", expression_.c_str());
430}
431
Casey Dahlin34b86102015-09-16 16:03:06 -0700432CppNamespace::CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700433 std::vector<unique_ptr<Declaration>> declarations)
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700434 : declarations_(std::move(declarations)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700435 name_(name) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700436
Christopher Wiley0c732db2015-09-29 14:36:44 -0700437CppNamespace::CppNamespace(const std::string& name,
438 unique_ptr<Declaration> declaration)
439 : name_(name) {
440 declarations_.push_back(std::move(declaration));
441}
442CppNamespace::CppNamespace(const std::string& name)
443 : name_(name) {}
444
Casey Dahlin34b86102015-09-16 16:03:06 -0700445void CppNamespace::Write(CodeWriter* to) const {
446 to->Write("namespace %s {\n\n", name_.c_str());
447
Casey Dahlin60a49162015-09-17 14:23:10 -0700448 for (const auto& dec : declarations_) {
Casey Dahlin34b86102015-09-16 16:03:06 -0700449 dec->Write(to);
Casey Dahlin60a49162015-09-17 14:23:10 -0700450 to->Write("\n");
451 }
Casey Dahlin34b86102015-09-16 16:03:06 -0700452
Casey Dahlin60a49162015-09-17 14:23:10 -0700453 to->Write("} // namespace %s\n", name_.c_str());
Casey Dahlin34b86102015-09-16 16:03:06 -0700454}
455
Christopher Wileyf944e792015-09-29 10:00:46 -0700456Document::Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700457 std::vector<unique_ptr<Declaration>> declarations)
458 : include_list_(include_list), declarations_(std::move(declarations)) {}
Casey Dahlin34b86102015-09-16 16:03:06 -0700459
Christopher Wileyf944e792015-09-29 10:00:46 -0700460void Document::Write(CodeWriter* to) const {
Casey Dahlin34b86102015-09-16 16:03:06 -0700461 for (const auto& include : include_list_) {
Christopher Wileyf600a552015-09-12 14:07:44 -0700462 to->Write("#include <%s>\n", include.c_str());
463 }
464 to->Write("\n");
465
Steven Morelandf3da0892018-10-05 14:52:01 -0700466 for (const auto& declaration : declarations_) {
467 declaration->Write(to);
468 }
Christopher Wileyf600a552015-09-12 14:07:44 -0700469}
470
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700471CppHeader::CppHeader(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700472 std::vector<std::unique_ptr<Declaration>> declarations)
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700473 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700474
475void CppHeader::Write(CodeWriter* to) const {
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700476 to->Write("#pragma once\n\n");
Christopher Wileyf600a552015-09-12 14:07:44 -0700477
Christopher Wileyf944e792015-09-29 10:00:46 -0700478 Document::Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700479}
480
Casey Dahlin34b86102015-09-16 16:03:06 -0700481CppSource::CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700482 std::vector<std::unique_ptr<Declaration>> declarations)
483 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700484
Christopher Wileyf944e792015-09-29 10:00:46 -0700485} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700486} // namespace aidl
487} // namespace android