Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -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 | |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 17 | #pragma once |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 18 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 19 | #include <memory> |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace aidl { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 25 | class CodeWriter; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 26 | } // namespace aidl |
| 27 | } // namespace android |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 28 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace aidl { |
| 31 | namespace cpp { |
| 32 | |
| 33 | class AstNode { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 34 | public: |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 35 | AstNode() = default; |
| 36 | virtual ~AstNode() = default; |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 37 | |
| 38 | // All ast nodes are non-copyable and non-movable |
| 39 | AstNode(const AstNode&) = delete; |
| 40 | AstNode(AstNode&&) = delete; |
| 41 | AstNode& operator=(const AstNode&) = delete; |
| 42 | AstNode& operator=(AstNode&&) = delete; |
| 43 | |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 44 | virtual void Write(CodeWriter* to) const = 0; |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 45 | std::string ToString(); |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 46 | }; // class AstNode |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 47 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 48 | class Declaration : public AstNode { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 49 | public: |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 50 | Declaration() = default; |
| 51 | virtual ~Declaration() = default; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 52 | }; // class Declaration |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 53 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 54 | class LiteralDecl : public Declaration { |
| 55 | public: |
| 56 | explicit LiteralDecl(const std::string& expression); |
| 57 | ~LiteralDecl() = default; |
| 58 | void Write(CodeWriter* to) const override; |
| 59 | |
| 60 | private: |
| 61 | const std::string expression_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 62 | }; // class LiteralDecl |
| 63 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 64 | class ClassDecl : public Declaration { |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 65 | public: |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 66 | ClassDecl(const std::string& name, const std::string& parent, |
| 67 | const std::vector<std::string>& template_params); |
| 68 | ClassDecl(const std::string& name, const std::string& parent, |
| 69 | const std::vector<std::string>& template_params, |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 70 | std::vector<std::unique_ptr<Declaration>> public_members, |
| 71 | std::vector<std::unique_ptr<Declaration>> private_members); |
| 72 | virtual ~ClassDecl() = default; |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 73 | |
| 74 | void Write(CodeWriter* to) const override; |
| 75 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 76 | void AddPublic(std::unique_ptr<Declaration> member); |
| 77 | void AddPrivate(std::unique_ptr<Declaration> member); |
| 78 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 79 | private: |
| 80 | std::string name_; |
| 81 | std::string parent_; |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 82 | std::vector<std::string> template_params_; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 83 | std::vector<std::unique_ptr<Declaration>> public_members_; |
| 84 | std::vector<std::unique_ptr<Declaration>> private_members_; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 85 | }; // class ClassDecl |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 86 | |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 87 | class Enum : public Declaration { |
| 88 | public: |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 89 | Enum(const std::string& name, const std::string& base_type, bool is_class); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 90 | virtual ~Enum() = default; |
| 91 | |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 92 | bool HasValues() const { return !fields_.empty(); } |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 93 | void Write(CodeWriter* to) const override; |
| 94 | |
Jooyung Han | 14af79b | 2020-10-12 03:30:41 +0900 | [diff] [blame] | 95 | void AddValue(const std::string& key, const std::string& value, const std::string& comment = ""); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | struct EnumField { |
Jooyung Han | 14af79b | 2020-10-12 03:30:41 +0900 | [diff] [blame] | 99 | EnumField(const std::string& k, const std::string& v, const std::string& c); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 100 | const std::string key; |
| 101 | const std::string value; |
Jooyung Han | 14af79b | 2020-10-12 03:30:41 +0900 | [diff] [blame] | 102 | const std::string comment; |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | std::string enum_name_; |
Christopher Wiley | fd7dc03 | 2016-02-02 17:58:39 -0800 | [diff] [blame] | 106 | std::string underlying_type_; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 107 | bool is_class_; |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 108 | std::vector<EnumField> fields_; |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 109 | }; // class Enum |
| 110 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 111 | class ArgList : public AstNode { |
| 112 | public: |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 113 | ArgList() = default; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 114 | explicit ArgList(const std::string& single_argument); |
| 115 | explicit ArgList(const std::vector<std::string>& arg_list); |
Christopher Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 116 | explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list); |
Chih-Hung Hsieh | f5cbb68 | 2018-09-25 13:43:32 -0700 | [diff] [blame] | 117 | ArgList(ArgList&& arg_list) noexcept; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 118 | virtual ~ArgList() = default; |
| 119 | |
| 120 | void Write(CodeWriter* to) const override; |
| 121 | |
| 122 | private: |
Christopher Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 123 | std::vector<std::unique_ptr<AstNode>> arguments_; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 124 | }; // class ArgList |
| 125 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 126 | class ConstructorDecl : public Declaration { |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 127 | public: |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 128 | enum Modifiers { |
| 129 | IS_VIRTUAL = 1 << 0, |
| 130 | IS_DEFAULT = 1 << 1, |
| 131 | IS_EXPLICIT = 1 << 2, |
| 132 | }; |
| 133 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 134 | ConstructorDecl(const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 135 | ArgList&& arg_list); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 136 | ConstructorDecl(const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 137 | ArgList&& arg_list, |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 138 | uint32_t modifiers); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 139 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 140 | virtual ~ConstructorDecl() = default; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 141 | |
| 142 | void Write(CodeWriter* to) const override; |
| 143 | |
| 144 | private: |
| 145 | const std::string name_; |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 146 | const ArgList arguments_; |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 147 | const uint32_t modifiers_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 148 | }; // class ConstructorDecl |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 149 | |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 150 | class MacroDecl : public Declaration { |
| 151 | public: |
| 152 | MacroDecl(const std::string& name, ArgList&& arg_list); |
| 153 | virtual ~MacroDecl() = default; |
| 154 | |
| 155 | void Write(CodeWriter* to) const override; |
| 156 | |
| 157 | private: |
| 158 | const std::string name_; |
| 159 | const ArgList arguments_; |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 160 | }; // class MacroDecl |
| 161 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 162 | class MethodDecl : public Declaration { |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 163 | public: |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 164 | enum Modifiers { |
| 165 | IS_CONST = 1 << 0, |
| 166 | IS_VIRTUAL = 1 << 1, |
| 167 | IS_OVERRIDE = 1 << 2, |
| 168 | IS_PURE_VIRTUAL = 1 << 3, |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 169 | IS_STATIC = 1 << 4, |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 170 | IS_FINAL = 1 << 5, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | MethodDecl(const std::string& return_type, |
| 174 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 175 | ArgList&& arg_list); |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 176 | MethodDecl(const std::string& return_type, |
| 177 | const std::string& name, |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 178 | ArgList&& arg_list, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 179 | uint32_t modifiers); |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 180 | virtual ~MethodDecl() = default; |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 181 | |
| 182 | void Write(CodeWriter* to) const override; |
| 183 | |
| 184 | private: |
| 185 | const std::string return_type_; |
| 186 | const std::string name_; |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 187 | const ArgList arguments_; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 188 | bool is_const_ = false; |
| 189 | bool is_virtual_ = false; |
| 190 | bool is_override_ = false; |
| 191 | bool is_pure_virtual_ = false; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 192 | bool is_static_ = true; |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 193 | bool is_final_ = false; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 194 | }; // class MethodDecl |
| 195 | |
| 196 | class StatementBlock : public Declaration { |
| 197 | public: |
| 198 | StatementBlock() = default; |
| 199 | virtual ~StatementBlock() = default; |
| 200 | |
| 201 | void AddStatement(std::unique_ptr<AstNode> statement); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 202 | void AddStatement(AstNode* statement); // Takes ownership |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 203 | void AddLiteral(const std::string& expression, bool add_semicolon = true); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 204 | bool Empty() const { return statements_.empty(); } |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 205 | |
| 206 | void Write(CodeWriter* to) const override; |
| 207 | |
| 208 | private: |
| 209 | std::vector<std::unique_ptr<AstNode>> statements_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 210 | }; // class StatementBlock |
| 211 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 212 | class ConstructorImpl : public Declaration { |
| 213 | public: |
| 214 | ConstructorImpl(const std::string& class_name, |
| 215 | ArgList&& arg_list, |
| 216 | const std::vector<std::string>& initializer_list); |
| 217 | virtual ~ConstructorImpl() = default; |
| 218 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 219 | // ConstructorImpl retains ownership of the statement block. |
| 220 | StatementBlock* GetStatementBlock(); |
| 221 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 222 | void Write(CodeWriter* to) const override; |
| 223 | |
| 224 | private: |
| 225 | std::string class_name_; |
| 226 | ArgList arguments_; |
| 227 | std::vector<std::string> initializer_list_; |
| 228 | StatementBlock body_; |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 229 | }; // class ConstructorImpl |
| 230 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 231 | class MethodImpl : public Declaration { |
| 232 | public: |
| 233 | // Passing an empty class name causes the method to be declared as a normal |
| 234 | // function (ie. no ClassName:: qualifier). |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 235 | MethodImpl(const std::string& return_type, const std::string& class_name, |
| 236 | const std::string& method_name, const std::vector<std::string>& template_params, |
| 237 | ArgList&& arg_list, bool is_const_method = false); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 238 | virtual ~MethodImpl() = default; |
| 239 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 240 | // MethodImpl retains ownership of the statement block. |
| 241 | StatementBlock* GetStatementBlock(); |
| 242 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 243 | void Write(CodeWriter* to) const override; |
| 244 | |
| 245 | private: |
| 246 | std::string return_type_; |
| 247 | std::string method_name_; |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 248 | const ArgList arguments_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 249 | StatementBlock statements_; |
| 250 | bool is_const_method_ = false; |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 251 | std::vector<std::string> template_params_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 252 | }; // class MethodImpl |
| 253 | |
| 254 | class SwitchStatement : public AstNode { |
| 255 | public: |
| 256 | explicit SwitchStatement(const std::string& expression); |
| 257 | virtual ~SwitchStatement() = default; |
| 258 | |
| 259 | // Add a case statement and return a pointer code block corresponding |
| 260 | // to the case. The switch statement will add a break statement |
| 261 | // after the code block by default to prevent accidental fall-through. |
| 262 | // Returns nullptr on duplicate value expressions (by strcmp, not value |
| 263 | // equivalence). |
| 264 | StatementBlock* AddCase(const std::string& value_expression); |
| 265 | void Write(CodeWriter* to) const override; |
| 266 | |
| 267 | private: |
| 268 | const std::string switch_expression_; |
| 269 | std::vector<std::string> case_values_; |
| 270 | std::vector<std::unique_ptr<StatementBlock>> case_logic_; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 271 | }; // class SwitchStatement |
| 272 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 273 | class Assignment : public AstNode { |
| 274 | public: |
| 275 | Assignment(const std::string& left, const std::string& right); |
| 276 | Assignment(const std::string& left, AstNode* right); |
| 277 | ~Assignment() = default; |
| 278 | void Write(CodeWriter* to) const override; |
| 279 | |
| 280 | private: |
| 281 | const std::string lhs_; |
| 282 | std::unique_ptr<AstNode> rhs_; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 283 | }; // class Assignment |
| 284 | |
| 285 | class MethodCall : public AstNode { |
| 286 | public: |
| 287 | MethodCall(const std::string& method_name, |
| 288 | const std::string& single_argument); |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 289 | MethodCall(const std::string& method_name, ArgList&& arg_list); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 290 | ~MethodCall() = default; |
| 291 | void Write(CodeWriter* to) const override; |
| 292 | |
| 293 | private: |
| 294 | const std::string method_name_; |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 295 | const ArgList arguments_; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 296 | }; // class MethodCall |
| 297 | |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 298 | class IfStatement : public AstNode { |
| 299 | public: |
Chih-Hung Hsieh | 156a57f | 2016-06-02 16:17:28 -0700 | [diff] [blame] | 300 | explicit IfStatement(AstNode* expression, |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 301 | bool invert_expression = false); |
| 302 | virtual ~IfStatement() = default; |
| 303 | StatementBlock* OnTrue() { return &on_true_; } |
| 304 | StatementBlock* OnFalse() { return &on_false_; } |
| 305 | void Write(CodeWriter* to) const override; |
| 306 | |
| 307 | private: |
| 308 | std::unique_ptr<AstNode> expression_; |
| 309 | bool invert_expression_ = false; |
| 310 | StatementBlock on_true_; |
| 311 | StatementBlock on_false_; |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 312 | }; // class IfStatement |
| 313 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 314 | class Statement : public AstNode { |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 315 | public: |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 316 | explicit Statement(std::unique_ptr<AstNode> expression); |
| 317 | explicit Statement(AstNode* expression); // Takes possession. |
| 318 | explicit Statement(const std::string& expression); |
| 319 | ~Statement() = default; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 320 | void Write(CodeWriter* to) const override; |
| 321 | |
| 322 | private: |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 323 | std::unique_ptr<AstNode> expression_; |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 324 | }; // class Statement |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 325 | |
Christopher Wiley | d55db28 | 2015-10-20 18:16:47 -0700 | [diff] [blame] | 326 | class Comparison : public AstNode { |
| 327 | public: |
| 328 | Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs); |
| 329 | ~Comparison() = default; |
| 330 | void Write(CodeWriter* to) const override; |
| 331 | |
| 332 | private: |
| 333 | std::unique_ptr<AstNode> left_; |
| 334 | std::unique_ptr<AstNode> right_; |
| 335 | const std::string operator_; |
Christopher Wiley | d55db28 | 2015-10-20 18:16:47 -0700 | [diff] [blame] | 336 | }; // class Comparison |
| 337 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 338 | class LiteralExpression : public AstNode { |
| 339 | public: |
| 340 | explicit LiteralExpression(const std::string& expression); |
| 341 | ~LiteralExpression() = default; |
| 342 | void Write(CodeWriter* to) const override; |
| 343 | |
| 344 | private: |
| 345 | const std::string expression_; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 346 | }; // class LiteralExpression |
| 347 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 348 | class CppNamespace : public Declaration { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 349 | public: |
| 350 | CppNamespace(const std::string& name, |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 351 | std::vector<std::unique_ptr<Declaration>> declarations); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 352 | CppNamespace(const std::string& name, |
| 353 | std::unique_ptr<Declaration> declaration); |
Chih-Hung Hsieh | 156a57f | 2016-06-02 16:17:28 -0700 | [diff] [blame] | 354 | explicit CppNamespace(const std::string& name); |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 355 | virtual ~CppNamespace() = default; |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 356 | |
| 357 | void Write(CodeWriter* to) const override; |
| 358 | |
| 359 | private: |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 360 | std::vector<std::unique_ptr<Declaration>> declarations_; |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 361 | std::string name_; |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 362 | }; // class CppNamespace |
| 363 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 364 | class Document : public AstNode { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 365 | public: |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 366 | Document(const std::vector<std::string>& include_list, |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 367 | std::vector<std::unique_ptr<Declaration>> declarations); |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 368 | |
| 369 | void Write(CodeWriter* to) const override; |
| 370 | |
| 371 | private: |
| 372 | std::vector<std::string> include_list_; |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 373 | std::vector<std::unique_ptr<Declaration>> declarations_; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 374 | }; // class Document |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 375 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 376 | class CppHeader final : public Document { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 377 | public: |
Devin Moore | 7aaa9cb | 2020-08-13 14:53:01 -0700 | [diff] [blame] | 378 | CppHeader(const std::vector<std::string>& include_list, |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 379 | std::vector<std::unique_ptr<Declaration>> declarations); |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 380 | void Write(CodeWriter* to) const override; |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 381 | }; // class CppHeader |
| 382 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 383 | class CppSource final : public Document { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 384 | public: |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 385 | CppSource(const std::vector<std::string>& include_list, |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 386 | std::vector<std::unique_ptr<Declaration>> declarations); |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 387 | }; // class CppSource |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 388 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 389 | } // namespace cpp |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 390 | } // namespace aidl |
| 391 | } // namespace android |