declaration.h 5.2 KB

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