member.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "llvm/Support/Compiler.h"
  11. namespace Carbon {
  12. // Abstract base class of all AST nodes representing patterns.
  13. //
  14. // Member and its derived classes support LLVM-style RTTI, including
  15. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  16. // class derived from Member must provide a `classof` operation, and
  17. // every concrete derived class must have a corresponding enumerator
  18. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  19. // details.
  20. class Member {
  21. public:
  22. enum class Kind { FieldMember };
  23. Member(const Member&) = delete;
  24. Member& operator=(const Member&) = delete;
  25. // Returns the enumerator corresponding to the most-derived type of this
  26. // object.
  27. auto Tag() const -> Kind { return tag; }
  28. auto LineNumber() const -> int { return line_num; }
  29. void Print(llvm::raw_ostream& out) const;
  30. protected:
  31. // Constructs a Member representing syntax at the given line number.
  32. // `tag` must be the enumerator corresponding to the most-derived type being
  33. // constructed.
  34. Member(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
  35. private:
  36. const Kind tag;
  37. int line_num;
  38. };
  39. class FieldMember : public Member {
  40. public:
  41. FieldMember(int line_num, const BindingPattern* binding)
  42. : Member(Kind::FieldMember, line_num), binding(binding) {}
  43. static auto classof(const Member* member) -> bool {
  44. return member->Tag() == Kind::FieldMember;
  45. }
  46. auto Binding() const -> const BindingPattern* { return binding; }
  47. private:
  48. // TODO: split this into a non-optional name and a type, initialized by
  49. // a constructor that takes a BindingPattern and handles errors like a
  50. // missing name.
  51. const BindingPattern* binding;
  52. };
  53. } // namespace Carbon
  54. #endif // EXECUTABLE_SEMANTICS_AST_MEMBER_H_