blob: 39af24ca1ca04a8d9bd54e3d94f72a443503917b [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,
Jooyung Hanea571f82021-01-05 19:13:17 +090047 const std::vector<std::string>& template_params, const std::string& attributes)
48 : name_(name), parent_(parent), attributes_(attributes), 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,
Jooyung Hanea571f82021-01-05 19:13:17 +090053 std::vector<unique_ptr<Declaration>> private_members,
54 const std::string& attributes)
Casey Dahlin60a49162015-09-17 14:23:10 -070055 : name_(name),
56 parent_(parent),
Jooyung Hanea571f82021-01-05 19:13:17 +090057 attributes_(attributes),
Devin Moore53fc99c2020-08-12 08:07:52 -070058 template_params_(template_params),
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070059 public_members_(std::move(public_members)),
60 private_members_(std::move(private_members)) {}
Casey Dahlin60a49162015-09-17 14:23:10 -070061
Christopher Wileyf944e792015-09-29 10:00:46 -070062void ClassDecl::Write(CodeWriter* to) const {
Devin Moore53fc99c2020-08-12 08:07:52 -070063 if (!template_params_.empty())
64 to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str());
65
Jooyung Hanea571f82021-01-05 19:13:17 +090066 to->Write("class");
67 if (!attributes_.empty()) {
68 to->Write(" %s", attributes_.c_str());
69 }
70 to->Write(" %s ", name_.c_str());
Casey Dahlin60a49162015-09-17 14:23:10 -070071
Devin Moore53fc99c2020-08-12 08:07:52 -070072 if (parent_.length() > 0) to->Write(": public %s ", parent_.c_str());
Casey Dahlin60a49162015-09-17 14:23:10 -070073
Casey Dahlin88924d62015-09-17 16:28:24 -070074 to->Write("{\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070075
Devin Moore53fc99c2020-08-12 08:07:52 -070076 if (!public_members_.empty()) to->Write("public:\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070077
Jiyong Parka755dc72018-06-29 13:52:24 +090078 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070079 for (const auto& dec : public_members_)
80 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090081 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070082
Devin Moore53fc99c2020-08-12 08:07:52 -070083 if (!private_members_.empty()) to->Write("private:\n");
Casey Dahlin60a49162015-09-17 14:23:10 -070084
Jiyong Parka755dc72018-06-29 13:52:24 +090085 to->Indent();
Casey Dahlin60a49162015-09-17 14:23:10 -070086 for (const auto& dec : private_members_)
87 dec->Write(to);
Jiyong Parka755dc72018-06-29 13:52:24 +090088 to->Dedent();
Casey Dahlin60a49162015-09-17 14:23:10 -070089
Casey Dahlin88924d62015-09-17 16:28:24 -070090 to->Write("}; // class %s\n", name_.c_str());
91}
92
Christopher Wiley0c732db2015-09-29 14:36:44 -070093void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) {
94 public_members_.push_back(std::move(member));
95}
96
97void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) {
98 private_members_.push_back(std::move(member));
99}
100
Jooyung Hanea571f82021-01-05 19:13:17 +0900101Enum::EnumField::EnumField(const string& k, const string& v, const string& a)
102 : key(k), value(v), attribute(a) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700103
Jooyung Hanea571f82021-01-05 19:13:17 +0900104Enum::Enum(const string& name, const string& base_type, bool is_class,
105 const std::string& attributes)
106 : enum_name_(name), underlying_type_(base_type), attributes_(attributes), is_class_(is_class) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700107
108void Enum::Write(CodeWriter* to) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700109 to->Write("enum ");
110 if (is_class_) {
111 to->Write("class ");
112 }
Jooyung Hanea571f82021-01-05 19:13:17 +0900113 if (!attributes_.empty()) {
114 to->Write("%s ", attributes_.c_str());
115 }
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800116 if (underlying_type_.empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700117 to->Write("%s {\n", enum_name_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800118 } else {
Daniel Norman85aed542019-08-21 12:01:14 -0700119 to->Write("%s : %s {\n", enum_name_.c_str(), underlying_type_.c_str());
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800120 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900121 to->Indent();
Christopher Wileya7a5c102015-09-29 16:26:52 -0700122 for (const auto& field : fields_) {
Jooyung Hanea571f82021-01-05 19:13:17 +0900123 to->Write("%s", field.key.c_str());
124 if (!field.attribute.empty()) {
125 to->Write(" %s", field.attribute.c_str());
Jooyung Han14af79b2020-10-12 03:30:41 +0900126 }
Jooyung Hanea571f82021-01-05 19:13:17 +0900127 if (!field.value.empty()) {
128 to->Write(" = %s", field.value.c_str());
Christopher Wileya7a5c102015-09-29 16:26:52 -0700129 }
Jooyung Hanea571f82021-01-05 19:13:17 +0900130 to->Write(",\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700131 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900132 to->Dedent();
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700133 to->Write("};\n");
Christopher Wileya7a5c102015-09-29 16:26:52 -0700134}
135
Jooyung Hanea571f82021-01-05 19:13:17 +0900136void Enum::AddValue(const string& key, const string& value, const string& attribute) {
137 fields_.emplace_back(key, value, attribute);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700138}
139
Christopher Wiley23285262015-10-09 15:06:14 -0700140ArgList::ArgList(const std::string& single_argument)
141 : ArgList(vector<string>{single_argument}) {}
142
Christopher Wileyf02facf2015-11-12 08:54:08 -0800143ArgList::ArgList(const std::vector<std::string>& arg_list) {
144 for (const auto& s : arg_list) {
145 arguments_.emplace_back(new LiteralExpression(s));
146 }
147}
148
149ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list)
150 : arguments_(std::move(arg_list)) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700151
Chih-Hung Hsiehf5cbb682018-09-25 13:43:32 -0700152ArgList::ArgList(ArgList&& arg_list) noexcept : arguments_(std::move(arg_list.arguments_)) {}
Christopher Wileyade4b452015-10-10 11:06:03 -0700153
Christopher Wiley23285262015-10-09 15:06:14 -0700154void ArgList::Write(CodeWriter* to) const {
155 to->Write("(");
156 bool is_first = true;
157 for (const auto& s : arguments_) {
158 if (!is_first) { to->Write(", "); }
159 is_first = false;
Christopher Wileyf02facf2015-11-12 08:54:08 -0800160 s->Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700161 }
162 to->Write(")");
163}
164
Christopher Wileya7a5c102015-09-29 16:26:52 -0700165ConstructorDecl::ConstructorDecl(
166 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700167 ArgList&& arg_list)
Christopher Wileyb23149d2015-10-14 13:52:21 -0700168 : ConstructorDecl(name, std::move(arg_list), 0u) {}
Christopher Wileya7a5c102015-09-29 16:26:52 -0700169
Christopher Wileyf944e792015-09-29 10:00:46 -0700170ConstructorDecl::ConstructorDecl(
Casey Dahlina834dd42015-09-23 11:52:15 -0700171 const std::string& name,
Christopher Wileyade4b452015-10-10 11:06:03 -0700172 ArgList&& arg_list,
Christopher Wileyb23149d2015-10-14 13:52:21 -0700173 uint32_t modifiers)
Casey Dahlina834dd42015-09-23 11:52:15 -0700174 : name_(name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700175 arguments_(std::move(arg_list)),
Christopher Wileyb23149d2015-10-14 13:52:21 -0700176 modifiers_(modifiers) {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700177
Christopher Wileyf944e792015-09-29 10:00:46 -0700178void ConstructorDecl::Write(CodeWriter* to) const {
Christopher Wileyb23149d2015-10-14 13:52:21 -0700179 if (modifiers_ & Modifiers::IS_VIRTUAL)
Casey Dahlina834dd42015-09-23 11:52:15 -0700180 to->Write("virtual ");
181
Christopher Wileyb23149d2015-10-14 13:52:21 -0700182 if (modifiers_ & Modifiers::IS_EXPLICIT)
183 to->Write("explicit ");
184
Christopher Wileyade4b452015-10-10 11:06:03 -0700185 to->Write("%s", name_.c_str());
Casey Dahlina834dd42015-09-23 11:52:15 -0700186
Christopher Wileyade4b452015-10-10 11:06:03 -0700187 arguments_.Write(to);
Casey Dahlina834dd42015-09-23 11:52:15 -0700188
Christopher Wileyb23149d2015-10-14 13:52:21 -0700189 if (modifiers_ & Modifiers::IS_DEFAULT)
Christopher Wileyf094d582015-10-08 15:50:15 -0700190 to->Write(" = default");
191
192 to->Write(";\n");
Casey Dahlina834dd42015-09-23 11:52:15 -0700193}
194
Christopher Wiley11a9d792016-02-24 17:20:33 -0800195MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list)
196 : name_(name),
197 arguments_(std::move(arg_list)) {}
198
199void MacroDecl::Write(CodeWriter* to) const {
200 to->Write("%s", name_.c_str());
201 arguments_.Write(to);
202 to->Write("\n");
203}
204
Jooyung Hanea571f82021-01-05 19:13:17 +0900205MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
206 const std::string& attributes)
207 : MethodDecl(return_type, name, std::move(arg_list), 0u, attributes) {}
Christopher Wiley0c732db2015-09-29 14:36:44 -0700208
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900209MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
Jooyung Hanea571f82021-01-05 19:13:17 +0900210 uint32_t modifiers, const std::string& attributes)
Casey Dahlin88924d62015-09-17 16:28:24 -0700211 : return_type_(return_type),
212 name_(name),
Jooyung Hanea571f82021-01-05 19:13:17 +0900213 attributes_(attributes),
Christopher Wileyade4b452015-10-10 11:06:03 -0700214 arguments_(std::move(arg_list)),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700215 is_const_(modifiers & IS_CONST),
216 is_virtual_(modifiers & IS_VIRTUAL),
217 is_override_(modifiers & IS_OVERRIDE),
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700218 is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900219 is_static_(modifiers & IS_STATIC),
220 is_final_(modifiers & IS_FINAL) {}
Casey Dahlin88924d62015-09-17 16:28:24 -0700221
Christopher Wileyf944e792015-09-29 10:00:46 -0700222void MethodDecl::Write(CodeWriter* to) const {
Casey Dahlin88924d62015-09-17 16:28:24 -0700223 if (is_virtual_)
224 to->Write("virtual ");
225
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700226 if (is_static_)
227 to->Write("static ");
228
Christopher Wileyda695992015-10-05 11:31:41 -0700229 to->Write("%s %s", return_type_.c_str(), name_.c_str());
Casey Dahlin88924d62015-09-17 16:28:24 -0700230
Christopher Wileyade4b452015-10-10 11:06:03 -0700231 arguments_.Write(to);
Casey Dahlin88924d62015-09-17 16:28:24 -0700232
233 if (is_const_)
234 to->Write(" const");
235
Christopher Wiley0c732db2015-09-29 14:36:44 -0700236 if (is_override_)
237 to->Write(" override");
238
Jeongik Chaa2ada0c2018-11-17 15:11:45 +0900239 if (is_final_) to->Write(" final");
240
Jooyung Hanea571f82021-01-05 19:13:17 +0900241 if (!attributes_.empty()) to->Write(" %s", attributes_.c_str());
242
Christopher Wiley0c732db2015-09-29 14:36:44 -0700243 if (is_pure_virtual_)
244 to->Write(" = 0");
245
Casey Dahlin88924d62015-09-17 16:28:24 -0700246 to->Write(";\n");
Casey Dahlin60a49162015-09-17 14:23:10 -0700247}
248
Christopher Wileyda695992015-10-05 11:31:41 -0700249void StatementBlock::AddStatement(unique_ptr<AstNode> statement) {
250 statements_.push_back(std::move(statement));
251}
252
Christopher Wiley23285262015-10-09 15:06:14 -0700253void StatementBlock::AddStatement(AstNode* statement) {
254 statements_.emplace_back(statement);
255}
256
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700257void StatementBlock::AddLiteral(const std::string& expression_str,
Christopher Wileyda695992015-10-05 11:31:41 -0700258 bool add_semicolon) {
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700259 if (add_semicolon) {
260 statements_.push_back(unique_ptr<AstNode>(new Statement(expression_str)));
261 } else {
262 statements_.push_back(unique_ptr<AstNode>(
263 new LiteralExpression(expression_str)));
264 }
Christopher Wileyda695992015-10-05 11:31:41 -0700265}
266
267void StatementBlock::Write(CodeWriter* to) const {
268 to->Write("{\n");
Jiyong Parka755dc72018-06-29 13:52:24 +0900269 to->Indent();
Christopher Wileyda695992015-10-05 11:31:41 -0700270 for (const auto& statement : statements_) {
271 statement->Write(to);
272 }
Jiyong Parka755dc72018-06-29 13:52:24 +0900273 to->Dedent();
Christopher Wileyda695992015-10-05 11:31:41 -0700274 to->Write("}\n");
275}
276
Christopher Wileyf9688b02015-10-08 17:17:50 -0700277ConstructorImpl::ConstructorImpl(const string& class_name,
278 ArgList&& arg_list,
279 const vector<string>& initializer_list)
280 : class_name_(class_name),
281 arguments_(std::move(arg_list)),
282 initializer_list_(initializer_list) {}
283
Steven Morelanda57d0a62019-07-30 09:41:14 -0700284StatementBlock* ConstructorImpl::GetStatementBlock() {
285 return &body_;
286}
287
Christopher Wileyf9688b02015-10-08 17:17:50 -0700288void ConstructorImpl::Write(CodeWriter* to) const {
289 to->Write("%s::%s", class_name_.c_str(), class_name_.c_str());
290 arguments_.Write(to);
291 to->Write("\n");
292
293 bool is_first = true;
294 for (const string& i : initializer_list_) {
295 if (is_first) {
296 to->Write(" : %s", i.c_str());
297 } else {
298 to->Write(",\n %s", i.c_str());
299 }
300 is_first = false;
301 }
302
303 body_.Write(to);
304}
305
Devin Moore53fc99c2020-08-12 08:07:52 -0700306MethodImpl::MethodImpl(const string& return_type, const string& class_name,
307 const string& method_name, const std::vector<std::string>& template_params,
308 ArgList&& arg_list, bool is_const_method)
Christopher Wileyda695992015-10-05 11:31:41 -0700309 : return_type_(return_type),
310 method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700311 arguments_(std::move(arg_list)),
Devin Moore53fc99c2020-08-12 08:07:52 -0700312 is_const_method_(is_const_method),
313 template_params_(template_params) {
Christopher Wileyda695992015-10-05 11:31:41 -0700314 if (!class_name.empty()) {
Devin Moore53fc99c2020-08-12 08:07:52 -0700315 if (!template_params.empty()) {
316 method_name_ = class_name + "<" + base::Join(template_params, ",") + ">::" + method_name;
317 } else {
318 method_name_ = class_name + "::" + method_name;
319 }
Christopher Wileyda695992015-10-05 11:31:41 -0700320 }
321}
322
Christopher Wileyf9688b02015-10-08 17:17:50 -0700323StatementBlock* MethodImpl::GetStatementBlock() {
324 return &statements_;
Christopher Wileyda695992015-10-05 11:31:41 -0700325}
326
327void MethodImpl::Write(CodeWriter* to) const {
Devin Moore53fc99c2020-08-12 08:07:52 -0700328 if (!template_params_.empty())
329 to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str());
Christopher Wileyda695992015-10-05 11:31:41 -0700330 to->Write("%s %s", return_type_.c_str(), method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700331 arguments_.Write(to);
Christopher Wileyda695992015-10-05 11:31:41 -0700332 to->Write("%s ", (is_const_method_) ? " const" : "");
333 statements_.Write(to);
334}
335
336SwitchStatement::SwitchStatement(const std::string& expression)
337 : switch_expression_(expression) {}
338
339StatementBlock* SwitchStatement::AddCase(const string& value_expression) {
340 auto it = std::find(case_values_.begin(), case_values_.end(), value_expression);
341 if (it != case_values_.end()) {
Steven Moreland21780812020-09-11 01:29:45 +0000342 AIDL_ERROR(value_expression) << "Duplicate switch case labels";
Christopher Wileyda695992015-10-05 11:31:41 -0700343 return nullptr;
344 }
345 StatementBlock* ret = new StatementBlock();
346 case_values_.push_back(value_expression);
347 case_logic_.push_back(unique_ptr<StatementBlock>{ret});
348 return ret;
349}
350
351void SwitchStatement::Write(CodeWriter* to) const {
352 to->Write("switch (%s) {\n", switch_expression_.c_str());
353 for (size_t i = 0; i < case_values_.size(); ++i) {
354 const string& case_value = case_values_[i];
355 const unique_ptr<StatementBlock>& statements = case_logic_[i];
356 if (case_value.empty()) {
357 to->Write("default:\n");
358 } else {
359 to->Write("case %s:\n", case_value.c_str());
360 }
361 statements->Write(to);
362 to->Write("break;\n");
363 }
364 to->Write("}\n");
365}
366
Christopher Wiley23285262015-10-09 15:06:14 -0700367
368Assignment::Assignment(const std::string& left, const std::string& right)
369 : Assignment(left, new LiteralExpression{right}) {}
370
371Assignment::Assignment(const std::string& left, AstNode* right)
372 : lhs_(left),
373 rhs_(right) {}
374
375void Assignment::Write(CodeWriter* to) const {
376 to->Write("%s = ", lhs_.c_str());
377 rhs_->Write(to);
378 to->Write(";\n");
379}
380
381MethodCall::MethodCall(const std::string& method_name,
382 const std::string& single_argument)
Christopher Wileyade4b452015-10-10 11:06:03 -0700383 : MethodCall(method_name, ArgList{single_argument}) {}
Christopher Wiley23285262015-10-09 15:06:14 -0700384
Christopher Wileyade4b452015-10-10 11:06:03 -0700385MethodCall::MethodCall(const std::string& method_name,
386 ArgList&& arg_list)
Christopher Wiley23285262015-10-09 15:06:14 -0700387 : method_name_(method_name),
Christopher Wileyade4b452015-10-10 11:06:03 -0700388 arguments_{std::move(arg_list)} {}
Christopher Wiley23285262015-10-09 15:06:14 -0700389
390void MethodCall::Write(CodeWriter* to) const {
391 to->Write("%s", method_name_.c_str());
Christopher Wileyade4b452015-10-10 11:06:03 -0700392 arguments_.Write(to);
Christopher Wiley23285262015-10-09 15:06:14 -0700393}
394
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700395IfStatement::IfStatement(AstNode* expression, bool invert_expression)
396 : expression_(expression),
397 invert_expression_(invert_expression) {}
398
399void IfStatement::Write(CodeWriter* to) const {
Christopher Wiley864bc092015-11-10 11:45:23 -0800400 to->Write("if (%s", (invert_expression_) ? "!(" : "");
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700401 expression_->Write(to);
402 to->Write(")%s ", (invert_expression_) ? ")" : "");
403 on_true_.Write(to);
404
405 if (!on_false_.Empty()) {
406 to->Write("else ");
407 on_false_.Write(to);
408 }
409}
410
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700411Statement::Statement(unique_ptr<AstNode> expression)
412 : expression_(std::move(expression)) {}
Christopher Wileyda695992015-10-05 11:31:41 -0700413
Christopher Wiley3c5d28d2015-10-21 09:53:46 -0700414Statement::Statement(AstNode* expression) : expression_(expression) {}
415
416Statement::Statement(const string& expression)
417 : expression_(new LiteralExpression(expression)) {}
418
419void Statement::Write(CodeWriter* to) const {
420 expression_->Write(to);
421 to->Write(";\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700422}
423
Christopher Wileyd55db282015-10-20 18:16:47 -0700424Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs)
425 : left_(lhs),
426 right_(rhs),
427 operator_(comparison) {}
428
429void Comparison::Write(CodeWriter* to) const {
430 to->Write("((");
431 left_->Write(to);
432 to->Write(") %s (", operator_.c_str());
433 right_->Write(to);
434 to->Write("))");
435}
436
Christopher Wiley23285262015-10-09 15:06:14 -0700437LiteralExpression::LiteralExpression(const std::string& expression)
438 : expression_(expression) {}
439
440void LiteralExpression::Write(CodeWriter* to) const {
441 to->Write("%s", expression_.c_str());
442}
443
Casey Dahlin34b86102015-09-16 16:03:06 -0700444CppNamespace::CppNamespace(const std::string& name,
Christopher Wileyf944e792015-09-29 10:00:46 -0700445 std::vector<unique_ptr<Declaration>> declarations)
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700446 : declarations_(std::move(declarations)),
Casey Dahlin34b86102015-09-16 16:03:06 -0700447 name_(name) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700448
Christopher Wiley0c732db2015-09-29 14:36:44 -0700449CppNamespace::CppNamespace(const std::string& name,
450 unique_ptr<Declaration> declaration)
451 : name_(name) {
452 declarations_.push_back(std::move(declaration));
453}
454CppNamespace::CppNamespace(const std::string& name)
455 : name_(name) {}
456
Casey Dahlin34b86102015-09-16 16:03:06 -0700457void CppNamespace::Write(CodeWriter* to) const {
458 to->Write("namespace %s {\n\n", name_.c_str());
459
Casey Dahlin60a49162015-09-17 14:23:10 -0700460 for (const auto& dec : declarations_) {
Casey Dahlin34b86102015-09-16 16:03:06 -0700461 dec->Write(to);
Casey Dahlin60a49162015-09-17 14:23:10 -0700462 to->Write("\n");
463 }
Casey Dahlin34b86102015-09-16 16:03:06 -0700464
Casey Dahlin60a49162015-09-17 14:23:10 -0700465 to->Write("} // namespace %s\n", name_.c_str());
Casey Dahlin34b86102015-09-16 16:03:06 -0700466}
467
Christopher Wileyf944e792015-09-29 10:00:46 -0700468Document::Document(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700469 std::vector<unique_ptr<Declaration>> declarations)
470 : include_list_(include_list), declarations_(std::move(declarations)) {}
Casey Dahlin34b86102015-09-16 16:03:06 -0700471
Christopher Wileyf944e792015-09-29 10:00:46 -0700472void Document::Write(CodeWriter* to) const {
Casey Dahlin34b86102015-09-16 16:03:06 -0700473 for (const auto& include : include_list_) {
Christopher Wileyf600a552015-09-12 14:07:44 -0700474 to->Write("#include <%s>\n", include.c_str());
475 }
476 to->Write("\n");
477
Steven Morelandf3da0892018-10-05 14:52:01 -0700478 for (const auto& declaration : declarations_) {
479 declaration->Write(to);
480 }
Christopher Wileyf600a552015-09-12 14:07:44 -0700481}
482
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700483CppHeader::CppHeader(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700484 std::vector<std::unique_ptr<Declaration>> declarations)
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700485 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700486
487void CppHeader::Write(CodeWriter* to) const {
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700488 to->Write("#pragma once\n\n");
Christopher Wileyf600a552015-09-12 14:07:44 -0700489
Christopher Wileyf944e792015-09-29 10:00:46 -0700490 Document::Write(to);
Christopher Wileyf600a552015-09-12 14:07:44 -0700491}
492
Casey Dahlin34b86102015-09-16 16:03:06 -0700493CppSource::CppSource(const std::vector<std::string>& include_list,
Steven Morelandf3da0892018-10-05 14:52:01 -0700494 std::vector<std::unique_ptr<Declaration>> declarations)
495 : Document(include_list, std::move(declarations)) {}
Christopher Wileyf600a552015-09-12 14:07:44 -0700496
Christopher Wileyf944e792015-09-29 10:00:46 -0700497} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700498} // namespace aidl
499} // namespace android