Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 17 | #include "ast_cpp.h" |
| 18 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 21 | #include <android-base/strings.h> |
| 22 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 23 | #include "code_writer.h" |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 24 | #include "logging.h" |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 25 | |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 26 | using std::string; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 27 | using std::unique_ptr; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 28 | using std::vector; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 29 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace aidl { |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 32 | namespace cpp { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 33 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 34 | std::string AstNode::ToString() { |
| 35 | std::string str; |
| 36 | Write(CodeWriter::ForString(&str).get()); |
| 37 | return str; |
| 38 | } |
| 39 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 40 | LiteralDecl::LiteralDecl(const std::string& expression) : expression_(expression) {} |
| 41 | |
| 42 | void LiteralDecl::Write(CodeWriter* to) const { |
| 43 | to->Write("%s", expression_.c_str()); |
| 44 | } |
| 45 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 46 | ClassDecl::ClassDecl(const std::string& name, const std::string& parent, |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 47 | const std::vector<std::string>& template_params, const std::string& attributes) |
| 48 | : name_(name), parent_(parent), attributes_(attributes), template_params_(template_params) {} |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 49 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 50 | ClassDecl::ClassDecl(const std::string& name, const std::string& parent, |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 51 | const std::vector<std::string>& template_params, |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 52 | std::vector<unique_ptr<Declaration>> public_members, |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 53 | std::vector<unique_ptr<Declaration>> private_members, |
| 54 | const std::string& attributes) |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 55 | : name_(name), |
| 56 | parent_(parent), |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 57 | attributes_(attributes), |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 58 | template_params_(template_params), |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 59 | public_members_(std::move(public_members)), |
| 60 | private_members_(std::move(private_members)) {} |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 61 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 62 | void ClassDecl::Write(CodeWriter* to) const { |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 63 | if (!template_params_.empty()) |
| 64 | to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str()); |
| 65 | |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 66 | to->Write("class"); |
| 67 | if (!attributes_.empty()) { |
| 68 | to->Write(" %s", attributes_.c_str()); |
| 69 | } |
| 70 | to->Write(" %s ", name_.c_str()); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 71 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 72 | if (parent_.length() > 0) to->Write(": public %s ", parent_.c_str()); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 73 | |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 74 | to->Write("{\n"); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 75 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 76 | if (!public_members_.empty()) to->Write("public:\n"); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 77 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 78 | to->Indent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 79 | for (const auto& dec : public_members_) |
| 80 | dec->Write(to); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 81 | to->Dedent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 82 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 83 | if (!private_members_.empty()) to->Write("private:\n"); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 84 | |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 85 | to->Indent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 86 | for (const auto& dec : private_members_) |
| 87 | dec->Write(to); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 88 | to->Dedent(); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 89 | |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 90 | to->Write("}; // class %s\n", name_.c_str()); |
| 91 | } |
| 92 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 93 | void ClassDecl::AddPublic(std::unique_ptr<Declaration> member) { |
| 94 | public_members_.push_back(std::move(member)); |
| 95 | } |
| 96 | |
| 97 | void ClassDecl::AddPrivate(std::unique_ptr<Declaration> member) { |
| 98 | private_members_.push_back(std::move(member)); |
| 99 | } |
| 100 | |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 101 | Enum::EnumField::EnumField(const string& k, const string& v, const string& a) |
| 102 | : key(k), value(v), attribute(a) {} |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 103 | |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 104 | Enum::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 Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 107 | |
| 108 | void Enum::Write(CodeWriter* to) const { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 109 | to->Write("enum "); |
| 110 | if (is_class_) { |
| 111 | to->Write("class "); |
| 112 | } |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 113 | if (!attributes_.empty()) { |
| 114 | to->Write("%s ", attributes_.c_str()); |
| 115 | } |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 116 | if (underlying_type_.empty()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 117 | to->Write("%s {\n", enum_name_.c_str()); |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 118 | } else { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 119 | to->Write("%s : %s {\n", enum_name_.c_str(), underlying_type_.c_str()); |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 120 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 121 | to->Indent(); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 122 | for (const auto& field : fields_) { |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 123 | to->Write("%s", field.key.c_str()); |
| 124 | if (!field.attribute.empty()) { |
| 125 | to->Write(" %s", field.attribute.c_str()); |
Jooyung Han | 14af79b | 2020-10-12 03:30:41 +0900 | [diff] [blame] | 126 | } |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 127 | if (!field.value.empty()) { |
| 128 | to->Write(" = %s", field.value.c_str()); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 129 | } |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 130 | to->Write(",\n"); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 131 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 132 | to->Dedent(); |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 133 | to->Write("};\n"); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 136 | void Enum::AddValue(const string& key, const string& value, const string& attribute) { |
| 137 | fields_.emplace_back(key, value, attribute); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 140 | ArgList::ArgList(const std::string& single_argument) |
| 141 | : ArgList(vector<string>{single_argument}) {} |
| 142 | |
Christopher Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 143 | ArgList::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 | |
| 149 | ArgList::ArgList(std::vector<std::unique_ptr<AstNode>> arg_list) |
| 150 | : arguments_(std::move(arg_list)) {} |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 151 | |
Chih-Hung Hsieh | f5cbb68 | 2018-09-25 13:43:32 -0700 | [diff] [blame] | 152 | ArgList::ArgList(ArgList&& arg_list) noexcept : arguments_(std::move(arg_list.arguments_)) {} |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 153 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 154 | void 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 Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 160 | s->Write(to); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 161 | } |
| 162 | to->Write(")"); |
| 163 | } |
| 164 | |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 165 | ConstructorDecl::ConstructorDecl( |
| 166 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 167 | ArgList&& arg_list) |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 168 | : ConstructorDecl(name, std::move(arg_list), 0u) {} |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 169 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 170 | ConstructorDecl::ConstructorDecl( |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 171 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 172 | ArgList&& arg_list, |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 173 | uint32_t modifiers) |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 174 | : name_(name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 175 | arguments_(std::move(arg_list)), |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 176 | modifiers_(modifiers) {} |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 177 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 178 | void ConstructorDecl::Write(CodeWriter* to) const { |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 179 | if (modifiers_ & Modifiers::IS_VIRTUAL) |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 180 | to->Write("virtual "); |
| 181 | |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 182 | if (modifiers_ & Modifiers::IS_EXPLICIT) |
| 183 | to->Write("explicit "); |
| 184 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 185 | to->Write("%s", name_.c_str()); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 186 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 187 | arguments_.Write(to); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 188 | |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 189 | if (modifiers_ & Modifiers::IS_DEFAULT) |
Christopher Wiley | f094d58 | 2015-10-08 15:50:15 -0700 | [diff] [blame] | 190 | to->Write(" = default"); |
| 191 | |
| 192 | to->Write(";\n"); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 195 | MacroDecl::MacroDecl(const std::string& name, ArgList&& arg_list) |
| 196 | : name_(name), |
| 197 | arguments_(std::move(arg_list)) {} |
| 198 | |
| 199 | void MacroDecl::Write(CodeWriter* to) const { |
| 200 | to->Write("%s", name_.c_str()); |
| 201 | arguments_.Write(to); |
| 202 | to->Write("\n"); |
| 203 | } |
| 204 | |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 205 | MethodDecl::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 Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 208 | |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 209 | MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list, |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 210 | uint32_t modifiers, const std::string& attributes) |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 211 | : return_type_(return_type), |
| 212 | name_(name), |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 213 | attributes_(attributes), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 214 | arguments_(std::move(arg_list)), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 215 | is_const_(modifiers & IS_CONST), |
| 216 | is_virtual_(modifiers & IS_VIRTUAL), |
| 217 | is_override_(modifiers & IS_OVERRIDE), |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 218 | is_pure_virtual_(modifiers & IS_PURE_VIRTUAL), |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 219 | is_static_(modifiers & IS_STATIC), |
| 220 | is_final_(modifiers & IS_FINAL) {} |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 221 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 222 | void MethodDecl::Write(CodeWriter* to) const { |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 223 | if (is_virtual_) |
| 224 | to->Write("virtual "); |
| 225 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 226 | if (is_static_) |
| 227 | to->Write("static "); |
| 228 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 229 | to->Write("%s %s", return_type_.c_str(), name_.c_str()); |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 230 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 231 | arguments_.Write(to); |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 232 | |
| 233 | if (is_const_) |
| 234 | to->Write(" const"); |
| 235 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 236 | if (is_override_) |
| 237 | to->Write(" override"); |
| 238 | |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 239 | if (is_final_) to->Write(" final"); |
| 240 | |
Jooyung Han | ea571f8 | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 241 | if (!attributes_.empty()) to->Write(" %s", attributes_.c_str()); |
| 242 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 243 | if (is_pure_virtual_) |
| 244 | to->Write(" = 0"); |
| 245 | |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 246 | to->Write(";\n"); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 249 | void StatementBlock::AddStatement(unique_ptr<AstNode> statement) { |
| 250 | statements_.push_back(std::move(statement)); |
| 251 | } |
| 252 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 253 | void StatementBlock::AddStatement(AstNode* statement) { |
| 254 | statements_.emplace_back(statement); |
| 255 | } |
| 256 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 257 | void StatementBlock::AddLiteral(const std::string& expression_str, |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 258 | bool add_semicolon) { |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 259 | 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 Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void StatementBlock::Write(CodeWriter* to) const { |
| 268 | to->Write("{\n"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 269 | to->Indent(); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 270 | for (const auto& statement : statements_) { |
| 271 | statement->Write(to); |
| 272 | } |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 273 | to->Dedent(); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 274 | to->Write("}\n"); |
| 275 | } |
| 276 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 277 | ConstructorImpl::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 Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 284 | StatementBlock* ConstructorImpl::GetStatementBlock() { |
| 285 | return &body_; |
| 286 | } |
| 287 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 288 | void 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 Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 306 | MethodImpl::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 Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 309 | : return_type_(return_type), |
| 310 | method_name_(method_name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 311 | arguments_(std::move(arg_list)), |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 312 | is_const_method_(is_const_method), |
| 313 | template_params_(template_params) { |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 314 | if (!class_name.empty()) { |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 315 | 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 Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 323 | StatementBlock* MethodImpl::GetStatementBlock() { |
| 324 | return &statements_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void MethodImpl::Write(CodeWriter* to) const { |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 328 | if (!template_params_.empty()) |
| 329 | to->Write("template <typename %s>\n", base::Join(template_params_, ", typename ").c_str()); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 330 | to->Write("%s %s", return_type_.c_str(), method_name_.c_str()); |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 331 | arguments_.Write(to); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 332 | to->Write("%s ", (is_const_method_) ? " const" : ""); |
| 333 | statements_.Write(to); |
| 334 | } |
| 335 | |
| 336 | SwitchStatement::SwitchStatement(const std::string& expression) |
| 337 | : switch_expression_(expression) {} |
| 338 | |
| 339 | StatementBlock* 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 Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 342 | AIDL_ERROR(value_expression) << "Duplicate switch case labels"; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 343 | 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 | |
| 351 | void 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 Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 367 | |
| 368 | Assignment::Assignment(const std::string& left, const std::string& right) |
| 369 | : Assignment(left, new LiteralExpression{right}) {} |
| 370 | |
| 371 | Assignment::Assignment(const std::string& left, AstNode* right) |
| 372 | : lhs_(left), |
| 373 | rhs_(right) {} |
| 374 | |
| 375 | void Assignment::Write(CodeWriter* to) const { |
| 376 | to->Write("%s = ", lhs_.c_str()); |
| 377 | rhs_->Write(to); |
| 378 | to->Write(";\n"); |
| 379 | } |
| 380 | |
| 381 | MethodCall::MethodCall(const std::string& method_name, |
| 382 | const std::string& single_argument) |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 383 | : MethodCall(method_name, ArgList{single_argument}) {} |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 384 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 385 | MethodCall::MethodCall(const std::string& method_name, |
| 386 | ArgList&& arg_list) |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 387 | : method_name_(method_name), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 388 | arguments_{std::move(arg_list)} {} |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 389 | |
| 390 | void MethodCall::Write(CodeWriter* to) const { |
| 391 | to->Write("%s", method_name_.c_str()); |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 392 | arguments_.Write(to); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 395 | IfStatement::IfStatement(AstNode* expression, bool invert_expression) |
| 396 | : expression_(expression), |
| 397 | invert_expression_(invert_expression) {} |
| 398 | |
| 399 | void IfStatement::Write(CodeWriter* to) const { |
Christopher Wiley | 864bc09 | 2015-11-10 11:45:23 -0800 | [diff] [blame] | 400 | to->Write("if (%s", (invert_expression_) ? "!(" : ""); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 401 | 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 Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 411 | Statement::Statement(unique_ptr<AstNode> expression) |
| 412 | : expression_(std::move(expression)) {} |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 413 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 414 | Statement::Statement(AstNode* expression) : expression_(expression) {} |
| 415 | |
| 416 | Statement::Statement(const string& expression) |
| 417 | : expression_(new LiteralExpression(expression)) {} |
| 418 | |
| 419 | void Statement::Write(CodeWriter* to) const { |
| 420 | expression_->Write(to); |
| 421 | to->Write(";\n"); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Christopher Wiley | d55db28 | 2015-10-20 18:16:47 -0700 | [diff] [blame] | 424 | Comparison::Comparison(AstNode* lhs, const string& comparison, AstNode* rhs) |
| 425 | : left_(lhs), |
| 426 | right_(rhs), |
| 427 | operator_(comparison) {} |
| 428 | |
| 429 | void 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 Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 437 | LiteralExpression::LiteralExpression(const std::string& expression) |
| 438 | : expression_(expression) {} |
| 439 | |
| 440 | void LiteralExpression::Write(CodeWriter* to) const { |
| 441 | to->Write("%s", expression_.c_str()); |
| 442 | } |
| 443 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 444 | CppNamespace::CppNamespace(const std::string& name, |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 445 | std::vector<unique_ptr<Declaration>> declarations) |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 446 | : declarations_(std::move(declarations)), |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 447 | name_(name) {} |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 448 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 449 | CppNamespace::CppNamespace(const std::string& name, |
| 450 | unique_ptr<Declaration> declaration) |
| 451 | : name_(name) { |
| 452 | declarations_.push_back(std::move(declaration)); |
| 453 | } |
| 454 | CppNamespace::CppNamespace(const std::string& name) |
| 455 | : name_(name) {} |
| 456 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 457 | void CppNamespace::Write(CodeWriter* to) const { |
| 458 | to->Write("namespace %s {\n\n", name_.c_str()); |
| 459 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 460 | for (const auto& dec : declarations_) { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 461 | dec->Write(to); |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 462 | to->Write("\n"); |
| 463 | } |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 464 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 465 | to->Write("} // namespace %s\n", name_.c_str()); |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 468 | Document::Document(const std::vector<std::string>& include_list, |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 469 | std::vector<unique_ptr<Declaration>> declarations) |
| 470 | : include_list_(include_list), declarations_(std::move(declarations)) {} |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 471 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 472 | void Document::Write(CodeWriter* to) const { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 473 | for (const auto& include : include_list_) { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 474 | to->Write("#include <%s>\n", include.c_str()); |
| 475 | } |
| 476 | to->Write("\n"); |
| 477 | |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 478 | for (const auto& declaration : declarations_) { |
| 479 | declaration->Write(to); |
| 480 | } |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Devin Moore | 7aaa9cb | 2020-08-13 14:53:01 -0700 | [diff] [blame] | 483 | CppHeader::CppHeader(const std::vector<std::string>& include_list, |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 484 | std::vector<std::unique_ptr<Declaration>> declarations) |
Devin Moore | 7aaa9cb | 2020-08-13 14:53:01 -0700 | [diff] [blame] | 485 | : Document(include_list, std::move(declarations)) {} |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 486 | |
| 487 | void CppHeader::Write(CodeWriter* to) const { |
Devin Moore | 7aaa9cb | 2020-08-13 14:53:01 -0700 | [diff] [blame] | 488 | to->Write("#pragma once\n\n"); |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 489 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 490 | Document::Write(to); |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 493 | CppSource::CppSource(const std::vector<std::string>& include_list, |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 494 | std::vector<std::unique_ptr<Declaration>> declarations) |
| 495 | : Document(include_list, std::move(declarations)) {} |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 496 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 497 | } // namespace cpp |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 498 | } // namespace aidl |
| 499 | } // namespace android |