ast_cpp.h revision f9688b04c9e34063e8b2dffab4827e02f4f104fa
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 17#ifndef AIDL_AST_CPP_H_ 18#define AIDL_AST_CPP_H_ 19 20#include <memory> 21#include <string> 22#include <vector> 23 24#include <base/macros.h> 25 26namespace android { 27namespace aidl { 28class CodeWriter; 29} // namespace aidl 30} // namespace android 31 32namespace android { 33namespace aidl { 34namespace cpp { 35 36class AstNode { 37 public: 38 AstNode() = default; 39 virtual ~AstNode() = default; 40 virtual void Write(CodeWriter* to) const = 0; 41}; // class AstNode 42 43class Declaration : public AstNode { 44 public: 45 Declaration() = default; 46 virtual ~Declaration() = default; 47 48 private: 49 DISALLOW_COPY_AND_ASSIGN(Declaration); 50}; // class Declaration 51 52class ClassDecl : public Declaration { 53 public: 54 ClassDecl(const std::string& name, 55 const std::string& parent); 56 ClassDecl(const std::string& name, 57 const std::string& parent, 58 std::vector<std::unique_ptr<Declaration>> public_members, 59 std::vector<std::unique_ptr<Declaration>> private_members); 60 virtual ~ClassDecl() = default; 61 62 void Write(CodeWriter* to) const override; 63 64 void AddPublic(std::unique_ptr<Declaration> member); 65 void AddPrivate(std::unique_ptr<Declaration> member); 66 67 private: 68 std::string name_; 69 std::string parent_; 70 std::vector<std::unique_ptr<Declaration>> public_members_; 71 std::vector<std::unique_ptr<Declaration>> private_members_; 72 73 DISALLOW_COPY_AND_ASSIGN(ClassDecl); 74}; // class ClassDecl 75 76class Enum : public Declaration { 77 public: 78 explicit Enum(const std::string& name); 79 virtual ~Enum() = default; 80 81 void Write(CodeWriter* to) const override; 82 83 void AddValue(const std::string& key, const std::string& value); 84 85 private: 86 struct EnumField { 87 EnumField(const std::string& k, const std::string& v); 88 const std::string key; 89 const std::string value; 90 }; 91 92 std::string enum_name_; 93 std::vector<EnumField> fields_; 94 95 DISALLOW_COPY_AND_ASSIGN(Enum); 96}; // class Enum 97 98class ArgList : public AstNode { 99 public: 100 ArgList() = default; 101 explicit ArgList(const std::string& single_argument); 102 explicit ArgList(const std::vector<std::string>& arg_list); 103 ArgList(ArgList&& arg_list); 104 virtual ~ArgList() = default; 105 106 void Write(CodeWriter* to) const override; 107 108 private: 109 std::vector<std::string> arguments_; 110 111 DISALLOW_COPY_AND_ASSIGN(ArgList); 112}; // class ArgList 113 114class ConstructorDecl : public Declaration { 115 public: 116 ConstructorDecl(const std::string& name, 117 ArgList&& arg_list); 118 ConstructorDecl(const std::string& name, 119 ArgList&& arg_list, 120 bool is_virtual, 121 bool is_default); 122 123 virtual ~ConstructorDecl() = default; 124 125 void Write(CodeWriter* to) const override; 126 127 private: 128 const std::string name_; 129 const ArgList arguments_; 130 bool is_virtual_ = false; 131 bool is_default_ = false; 132 133 DISALLOW_COPY_AND_ASSIGN(ConstructorDecl); 134}; // class ConstructorDecl 135 136class MethodDecl : public Declaration { 137 public: 138 enum Modifiers { 139 IS_CONST = 1 << 0, 140 IS_VIRTUAL = 1 << 1, 141 IS_OVERRIDE = 1 << 2, 142 IS_PURE_VIRTUAL = 1 << 3, 143 }; 144 145 MethodDecl(const std::string& return_type, 146 const std::string& name, 147 ArgList&& arg_list); 148 MethodDecl(const std::string& return_type, 149 const std::string& name, 150 ArgList&& arg_list, 151 uint32_t modifiers); 152 virtual ~MethodDecl() = default; 153 154 void Write(CodeWriter* to) const override; 155 156 private: 157 const std::string return_type_; 158 const std::string name_; 159 const ArgList arguments_; 160 bool is_const_ = false; 161 bool is_virtual_ = false; 162 bool is_override_ = false; 163 bool is_pure_virtual_ = false; 164 165 DISALLOW_COPY_AND_ASSIGN(MethodDecl); 166}; // class MethodDecl 167 168class StatementBlock : public Declaration { 169 public: 170 StatementBlock() = default; 171 virtual ~StatementBlock() = default; 172 173 void AddStatement(std::unique_ptr<AstNode> statement); 174 void AddStatement(AstNode* statement); // Takes ownership 175 void AddLiteral(const std::string& expression, bool add_semicolon = true); 176 177 void Write(CodeWriter* to) const override; 178 179 private: 180 std::vector<std::unique_ptr<AstNode>> statements_; 181 182 DISALLOW_COPY_AND_ASSIGN(StatementBlock); 183}; // class StatementBlock 184 185class ConstructorImpl : public Declaration { 186 public: 187 ConstructorImpl(const std::string& class_name, 188 ArgList&& arg_list, 189 const std::vector<std::string>& initializer_list); 190 virtual ~ConstructorImpl() = default; 191 192 void Write(CodeWriter* to) const override; 193 194 private: 195 std::string class_name_; 196 ArgList arguments_; 197 std::vector<std::string> initializer_list_; 198 StatementBlock body_; 199 200 DISALLOW_COPY_AND_ASSIGN(ConstructorImpl); 201}; // class ConstructorImpl 202 203class MethodImpl : public Declaration { 204 public: 205 // Passing an empty class name causes the method to be declared as a normal 206 // function (ie. no ClassName:: qualifier). 207 MethodImpl(const std::string& return_type, 208 const std::string& class_name, 209 const std::string& method_name, 210 ArgList&& arg_list, 211 bool is_const_method = false); 212 virtual ~MethodImpl() = default; 213 214 // MethodImpl retains ownership of the statement block. 215 StatementBlock* GetStatementBlock(); 216 217 void Write(CodeWriter* to) const override; 218 219 private: 220 std::string return_type_; 221 std::string method_name_; 222 const ArgList arguments_; 223 StatementBlock statements_; 224 bool is_const_method_ = false; 225 226 DISALLOW_COPY_AND_ASSIGN(MethodImpl); 227}; // class MethodImpl 228 229class SwitchStatement : public AstNode { 230 public: 231 explicit SwitchStatement(const std::string& expression); 232 virtual ~SwitchStatement() = default; 233 234 // Add a case statement and return a pointer code block corresponding 235 // to the case. The switch statement will add a break statement 236 // after the code block by default to prevent accidental fall-through. 237 // Returns nullptr on duplicate value expressions (by strcmp, not value 238 // equivalence). 239 StatementBlock* AddCase(const std::string& value_expression); 240 void Write(CodeWriter* to) const override; 241 242 private: 243 const std::string switch_expression_; 244 std::vector<std::string> case_values_; 245 std::vector<std::unique_ptr<StatementBlock>> case_logic_; 246 247 DISALLOW_COPY_AND_ASSIGN(SwitchStatement); 248}; // class SwitchStatement 249 250class Assignment : public AstNode { 251 public: 252 Assignment(const std::string& left, const std::string& right); 253 Assignment(const std::string& left, AstNode* right); 254 ~Assignment() = default; 255 void Write(CodeWriter* to) const override; 256 257 private: 258 const std::string lhs_; 259 std::unique_ptr<AstNode> rhs_; 260 261 DISALLOW_COPY_AND_ASSIGN(Assignment); 262}; // class Assignment 263 264class MethodCall : public AstNode { 265 public: 266 MethodCall(const std::string& method_name, 267 const std::string& single_argument); 268 MethodCall(const std::string& method_name, ArgList&& arg_list); 269 ~MethodCall() = default; 270 void Write(CodeWriter* to) const override; 271 272 private: 273 const std::string method_name_; 274 const ArgList arguments_; 275 276 DISALLOW_COPY_AND_ASSIGN(MethodCall); 277}; // class MethodCall 278 279class LiteralStatement : public AstNode { 280 public: 281 LiteralStatement(const std::string& expression, bool use_semicolon = true); 282 ~LiteralStatement() = default; 283 void Write(CodeWriter* to) const override; 284 285 private: 286 const std::string expression_; 287 bool use_semicolon_; 288 289 DISALLOW_COPY_AND_ASSIGN(LiteralStatement); 290}; // class LiteralStatement 291 292class LiteralExpression : public AstNode { 293 public: 294 explicit LiteralExpression(const std::string& expression); 295 ~LiteralExpression() = default; 296 void Write(CodeWriter* to) const override; 297 298 private: 299 const std::string expression_; 300 301 DISALLOW_COPY_AND_ASSIGN(LiteralExpression); 302}; // class LiteralExpression 303 304class CppNamespace : public Declaration { 305 public: 306 CppNamespace(const std::string& name, 307 std::vector<std::unique_ptr<Declaration>> declarations); 308 CppNamespace(const std::string& name, 309 std::unique_ptr<Declaration> declaration); 310 CppNamespace(const std::string& name); 311 virtual ~CppNamespace() = default; 312 313 void Write(CodeWriter* to) const override; 314 315 private: 316 std::vector<std::unique_ptr<Declaration>> declarations_; 317 std::string name_; 318 319 DISALLOW_COPY_AND_ASSIGN(CppNamespace); 320}; // class CppNamespace 321 322class Document : public AstNode { 323 public: 324 Document(const std::vector<std::string>& include_list, 325 std::unique_ptr<CppNamespace> a_namespace); 326 327 void Write(CodeWriter* to) const override; 328 329 private: 330 std::vector<std::string> include_list_; 331 std::unique_ptr<CppNamespace> namespace_; 332 333 DISALLOW_COPY_AND_ASSIGN(Document); 334}; // class Document 335 336class CppHeader final : public Document { 337 public: 338 CppHeader(const std::string& include_guard, 339 const std::vector<std::string>& include_list, 340 std::unique_ptr<CppNamespace> a_namespace); 341 void Write(CodeWriter* to) const override; 342 343 private: 344 const std::string include_guard_; 345 346 DISALLOW_COPY_AND_ASSIGN(CppHeader); 347}; // class CppHeader 348 349class CppSource final : public Document { 350 public: 351 CppSource(const std::vector<std::string>& include_list, 352 std::unique_ptr<CppNamespace> a_namespace); 353 354 private: 355 DISALLOW_COPY_AND_ASSIGN(CppSource); 356}; // class CppSource 357 358} // namespace cpp 359} // namespace aidl 360} // namespace android 361 362#endif // AIDL_AST_CPP_H_ 363