member.h 2.2 KB

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