member.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 {
  22. public:
  23. enum class Kind { FieldMember };
  24. Member(const Member&) = delete;
  25. Member& operator=(const Member&) = delete;
  26. void Print(llvm::raw_ostream& out) const;
  27. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  28. // Returns the enumerator corresponding to the most-derived type of this
  29. // object.
  30. auto kind() const -> Kind { return kind_; }
  31. auto source_loc() const -> SourceLocation { return source_loc_; }
  32. protected:
  33. // Constructs a Member representing syntax at the given line number.
  34. // `kind` must be the enumerator corresponding to the most-derived type being
  35. // constructed.
  36. Member(Kind kind, SourceLocation source_loc)
  37. : kind_(kind), source_loc_(source_loc) {}
  38. private:
  39. const Kind kind_;
  40. SourceLocation source_loc_;
  41. };
  42. class FieldMember : public Member {
  43. public:
  44. FieldMember(SourceLocation source_loc, Nonnull<const BindingPattern*> binding)
  45. : Member(Kind::FieldMember, source_loc), binding_(binding) {}
  46. static auto classof(const Member* member) -> bool {
  47. return member->kind() == Kind::FieldMember;
  48. }
  49. auto binding() const -> const BindingPattern& { return *binding_; }
  50. private:
  51. // TODO: split this into a non-optional name and a type, initialized by
  52. // a constructor that takes a BindingPattern and handles errors like a
  53. // missing name.
  54. Nonnull<const BindingPattern*> binding_;
  55. };
  56. } // namespace Carbon
  57. #endif // EXECUTABLE_SEMANTICS_AST_MEMBER_H_