member.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_AST_MEMBER_H_
  5. #define EXECUTABLE_SEMANTICS_AST_MEMBER_H_
  6. #include <string>
  7. #include "common/ostream.h"
  8. #include "executable_semantics/ast/expression.h"
  9. #include "executable_semantics/ast/pattern.h"
  10. #include "executable_semantics/ast/source_location.h"
  11. #include "llvm/Support/Compiler.h"
  12. namespace Carbon {
  13. // Abstract base class of all AST nodes representing patterns.
  14. //
  15. // Member and its derived classes support LLVM-style RTTI, including
  16. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  17. // class derived from Member must provide a `classof` operation, and
  18. // every concrete derived class must have a corresponding enumerator
  19. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  20. // details.
  21. class Member : public AstNode {
  22. public:
  23. ~Member() override = 0;
  24. Member(const Member&) = delete;
  25. auto operator=(const Member&) -> Member& = delete;
  26. void Print(llvm::raw_ostream& out) const override;
  27. static auto classof(const AstNode* node) -> bool {
  28. return InheritsFromMember(node->kind());
  29. }
  30. // Returns the enumerator corresponding to the most-derived type of this
  31. // object.
  32. auto kind() const -> MemberKind {
  33. return static_cast<MemberKind>(root_kind());
  34. }
  35. protected:
  36. Member(AstNodeKind kind, SourceLocation source_loc)
  37. : AstNode(kind, source_loc) {}
  38. };
  39. class FieldMember : public Member {
  40. public:
  41. FieldMember(SourceLocation source_loc, Nonnull<BindingPattern*> binding)
  42. : Member(AstNodeKind::FieldMember, source_loc), binding_(binding) {
  43. CHECK(binding->name() != AnonymousName);
  44. }
  45. static auto classof(const AstNode* node) -> bool {
  46. return InheritsFromFieldMember(node->kind());
  47. }
  48. auto binding() const -> const BindingPattern& { return *binding_; }
  49. auto binding() -> BindingPattern& { return *binding_; }
  50. private:
  51. Nonnull<BindingPattern*> binding_;
  52. };
  53. } // namespace Carbon
  54. #endif // EXECUTABLE_SEMANTICS_AST_MEMBER_H_