declaration.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_DECLARATION_H_
  5. #define EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
  6. #include <string>
  7. #include <vector>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/ast/class_definition.h"
  10. #include "executable_semantics/ast/function_definition.h"
  11. #include "executable_semantics/ast/member.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/ast/source_location.h"
  14. #include "executable_semantics/common/ptr.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace Carbon {
  17. // Abstract base class of all AST nodes representing patterns.
  18. //
  19. // Declaration and its derived classes support LLVM-style RTTI, including
  20. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  21. // class derived from Declaration must provide a `classof` operation, and
  22. // every concrete derived class must have a corresponding enumerator
  23. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  24. // details.
  25. class Declaration {
  26. public:
  27. enum class Kind {
  28. FunctionDeclaration,
  29. ClassDeclaration,
  30. ChoiceDeclaration,
  31. VariableDeclaration,
  32. };
  33. Declaration(const Member&) = delete;
  34. Declaration& operator=(const Member&) = delete;
  35. // Returns the enumerator corresponding to the most-derived type of this
  36. // object.
  37. auto Tag() const -> Kind { return tag; }
  38. auto SourceLoc() const -> SourceLocation { return loc; }
  39. void Print(llvm::raw_ostream& out) const;
  40. protected:
  41. // Constructs a Declaration representing syntax at the given line number.
  42. // `tag` must be the enumerator corresponding to the most-derived type being
  43. // constructed.
  44. Declaration(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
  45. private:
  46. const Kind tag;
  47. SourceLocation loc;
  48. };
  49. class FunctionDeclaration : public Declaration {
  50. public:
  51. FunctionDeclaration(Ptr<const FunctionDefinition> definition)
  52. : Declaration(Kind::FunctionDeclaration, definition->source_location),
  53. definition(definition) {}
  54. static auto classof(const Declaration* decl) -> bool {
  55. return decl->Tag() == Kind::FunctionDeclaration;
  56. }
  57. auto Definition() const -> const FunctionDefinition& { return *definition; }
  58. private:
  59. Ptr<const FunctionDefinition> definition;
  60. };
  61. class ClassDeclaration : public Declaration {
  62. public:
  63. ClassDeclaration(SourceLocation loc, std::string name,
  64. std::vector<Ptr<Member>> members)
  65. : Declaration(Kind::ClassDeclaration, loc),
  66. definition({.loc = loc,
  67. .name = std::move(name),
  68. .members = std::move(members)}) {}
  69. static auto classof(const Declaration* decl) -> bool {
  70. return decl->Tag() == Kind::ClassDeclaration;
  71. }
  72. auto Definition() const -> const ClassDefinition& { return definition; }
  73. private:
  74. ClassDefinition definition;
  75. };
  76. class ChoiceDeclaration : public Declaration {
  77. public:
  78. ChoiceDeclaration(
  79. SourceLocation loc, std::string name,
  80. std::vector<std::pair<std::string, Ptr<const Expression>>> alternatives)
  81. : Declaration(Kind::ChoiceDeclaration, loc),
  82. name(std::move(name)),
  83. alternatives(std::move(alternatives)) {}
  84. static auto classof(const Declaration* decl) -> bool {
  85. return decl->Tag() == Kind::ChoiceDeclaration;
  86. }
  87. auto Name() const -> const std::string& { return name; }
  88. auto Alternatives() const
  89. -> const std::vector<std::pair<std::string, Ptr<const Expression>>>& {
  90. return alternatives;
  91. }
  92. private:
  93. std::string name;
  94. std::vector<std::pair<std::string, Ptr<const Expression>>> alternatives;
  95. };
  96. // Global variable definition implements the Declaration concept.
  97. class VariableDeclaration : public Declaration {
  98. public:
  99. VariableDeclaration(SourceLocation loc, Ptr<const BindingPattern> binding,
  100. Ptr<const Expression> initializer)
  101. : Declaration(Kind::VariableDeclaration, loc),
  102. binding(binding),
  103. initializer(initializer) {}
  104. static auto classof(const Declaration* decl) -> bool {
  105. return decl->Tag() == Kind::VariableDeclaration;
  106. }
  107. auto Binding() const -> Ptr<const BindingPattern> { return binding; }
  108. auto Initializer() const -> Ptr<const Expression> { return initializer; }
  109. private:
  110. // TODO: split this into a non-optional name and a type, initialized by
  111. // a constructor that takes a BindingPattern and handles errors like a
  112. // missing name.
  113. Ptr<const BindingPattern> binding;
  114. Ptr<const Expression> initializer;
  115. };
  116. } // namespace Carbon
  117. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_