declaration.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <utility>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "executable_semantics/ast/ast_node.h"
  11. #include "executable_semantics/ast/pattern.h"
  12. #include "executable_semantics/ast/return_term.h"
  13. #include "executable_semantics/ast/source_location.h"
  14. #include "executable_semantics/ast/statement.h"
  15. #include "executable_semantics/ast/static_scope.h"
  16. #include "executable_semantics/ast/value_category.h"
  17. #include "executable_semantics/common/nonnull.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. // Abstract base class of all AST nodes representing patterns.
  22. //
  23. // Declaration and its derived classes support LLVM-style RTTI, including
  24. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  25. // class derived from Declaration must provide a `classof` operation, and
  26. // every concrete derived class must have a corresponding enumerator
  27. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  28. // details.
  29. class Declaration : public AstNode {
  30. public:
  31. ~Declaration() override = 0;
  32. Declaration(const Declaration&) = delete;
  33. auto operator=(const Declaration&) -> Declaration& = delete;
  34. void Print(llvm::raw_ostream& out) const override;
  35. static auto classof(const AstNode* node) -> bool {
  36. return InheritsFromDeclaration(node->kind());
  37. }
  38. // Returns the enumerator corresponding to the most-derived type of this
  39. // object.
  40. auto kind() const -> DeclarationKind {
  41. return static_cast<DeclarationKind>(root_kind());
  42. }
  43. // The static type of the declared entity. Cannot be called before
  44. // typechecking.
  45. auto static_type() const -> const Value& { return **static_type_; }
  46. // Sets the static type of the declared entity. Can only be called once,
  47. // during typechecking.
  48. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  49. // Returns whether the static type has been set. Should only be called
  50. // during typechecking: before typechecking it's guaranteed to be false,
  51. // and after typechecking it's guaranteed to be true.
  52. auto has_static_type() const -> bool { return static_type_.has_value(); }
  53. protected:
  54. // Constructs a Declaration representing syntax at the given line number.
  55. // `kind` must be the enumerator corresponding to the most-derived type being
  56. // constructed.
  57. Declaration(AstNodeKind kind, SourceLocation source_loc)
  58. : AstNode(kind, source_loc) {}
  59. private:
  60. std::optional<Nonnull<const Value*>> static_type_;
  61. };
  62. // TODO: expand the kinds of things that can be deduced parameters.
  63. // For now, only generic parameters are supported.
  64. class GenericBinding : public AstNode {
  65. public:
  66. using ImplementsCarbonNamedEntity = void;
  67. GenericBinding(SourceLocation source_loc, std::string name,
  68. Nonnull<Expression*> type)
  69. : AstNode(AstNodeKind::GenericBinding, source_loc),
  70. name_(std::move(name)),
  71. type_(type) {}
  72. void Print(llvm::raw_ostream& out) const override;
  73. static auto classof(const AstNode* node) -> bool {
  74. return InheritsFromGenericBinding(node->kind());
  75. }
  76. auto name() const -> const std::string& { return name_; }
  77. auto type() const -> const Expression& { return *type_; }
  78. auto type() -> Expression& { return *type_; }
  79. // The static type of the binding. Cannot be called before typechecking.
  80. auto static_type() const -> const Value& { return **static_type_; }
  81. // Sets the static type of the binding. Can only be called once, during
  82. // typechecking.
  83. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  84. // Returns whether the static type has been set. Should only be called
  85. // during typechecking: before typechecking it's guaranteed to be false,
  86. // and after typechecking it's guaranteed to be true.
  87. auto has_static_type() const -> bool { return static_type_.has_value(); }
  88. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  89. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  90. return constant_value_;
  91. }
  92. // Sets the value returned by constant_value(). Can only be called once,
  93. // during typechecking.
  94. void set_constant_value(Nonnull<const Value*> value) {
  95. CHECK(!constant_value_.has_value());
  96. constant_value_ = value;
  97. }
  98. private:
  99. std::string name_;
  100. Nonnull<Expression*> type_;
  101. std::optional<Nonnull<const Value*>> static_type_;
  102. std::optional<Nonnull<const Value*>> constant_value_;
  103. };
  104. class FunctionDeclaration : public Declaration {
  105. public:
  106. using ImplementsCarbonNamedEntity = void;
  107. FunctionDeclaration(SourceLocation source_loc, std::string name,
  108. std::vector<Nonnull<AstNode*>> deduced_params,
  109. std::optional<Nonnull<BindingPattern*>> me_pattern,
  110. Nonnull<TuplePattern*> param_pattern,
  111. ReturnTerm return_term,
  112. std::optional<Nonnull<Block*>> body)
  113. : Declaration(AstNodeKind::FunctionDeclaration, source_loc),
  114. name_(std::move(name)),
  115. me_pattern_(me_pattern),
  116. param_pattern_(param_pattern),
  117. return_term_(return_term),
  118. body_(body) {
  119. ResolveDeducedAndReceiver(deduced_params);
  120. }
  121. static auto classof(const AstNode* node) -> bool {
  122. return InheritsFromFunctionDeclaration(node->kind());
  123. }
  124. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  125. auto name() const -> const std::string& { return name_; }
  126. auto deduced_parameters() const
  127. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  128. return deduced_parameters_;
  129. }
  130. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  131. return deduced_parameters_;
  132. }
  133. auto me_pattern() const -> const BindingPattern& { return **me_pattern_; }
  134. auto me_pattern() -> BindingPattern& { return **me_pattern_; }
  135. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  136. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  137. auto return_term() const -> const ReturnTerm& { return return_term_; }
  138. auto return_term() -> ReturnTerm& { return return_term_; }
  139. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  140. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  141. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  142. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  143. return constant_value_;
  144. }
  145. // Sets the value returned by constant_value(). Can only be called once,
  146. // during typechecking.
  147. void set_constant_value(Nonnull<const Value*> value) {
  148. CHECK(!constant_value_.has_value());
  149. constant_value_ = value;
  150. }
  151. bool is_method() const { return me_pattern_.has_value(); }
  152. private:
  153. void ResolveDeducedAndReceiver(const std::vector<Nonnull<AstNode*>>&);
  154. std::string name_;
  155. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  156. std::optional<Nonnull<BindingPattern*>> me_pattern_;
  157. Nonnull<TuplePattern*> param_pattern_;
  158. ReturnTerm return_term_;
  159. std::optional<Nonnull<Block*>> body_;
  160. std::optional<Nonnull<const Value*>> constant_value_;
  161. };
  162. class ClassDeclaration : public Declaration {
  163. public:
  164. using ImplementsCarbonNamedEntity = void;
  165. ClassDeclaration(SourceLocation source_loc, std::string name,
  166. std::vector<Nonnull<Declaration*>> members)
  167. : Declaration(AstNodeKind::ClassDeclaration, source_loc),
  168. name_(std::move(name)),
  169. members_(std::move(members)) {}
  170. static auto classof(const AstNode* node) -> bool {
  171. return InheritsFromClassDeclaration(node->kind());
  172. }
  173. auto name() const -> const std::string& { return name_; }
  174. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  175. return members_;
  176. }
  177. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  178. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  179. return constant_value_;
  180. }
  181. // Sets the value returned by constant_value(). Can only be called once,
  182. // during typechecking.
  183. void set_constant_value(Nonnull<const Value*> value) {
  184. CHECK(!constant_value_.has_value());
  185. constant_value_ = value;
  186. }
  187. private:
  188. std::string name_;
  189. std::vector<Nonnull<Declaration*>> members_;
  190. std::optional<Nonnull<const Value*>> constant_value_;
  191. };
  192. class AlternativeSignature : public AstNode {
  193. public:
  194. AlternativeSignature(SourceLocation source_loc, std::string name,
  195. Nonnull<Expression*> signature)
  196. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  197. name_(std::move(name)),
  198. signature_(signature) {}
  199. void Print(llvm::raw_ostream& out) const override;
  200. static auto classof(const AstNode* node) -> bool {
  201. return InheritsFromAlternativeSignature(node->kind());
  202. }
  203. auto name() const -> const std::string& { return name_; }
  204. auto signature() const -> const Expression& { return *signature_; }
  205. auto signature() -> Expression& { return *signature_; }
  206. private:
  207. std::string name_;
  208. Nonnull<Expression*> signature_;
  209. };
  210. class ChoiceDeclaration : public Declaration {
  211. public:
  212. using ImplementsCarbonNamedEntity = void;
  213. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  214. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  215. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  216. name_(std::move(name)),
  217. alternatives_(std::move(alternatives)) {}
  218. static auto classof(const AstNode* node) -> bool {
  219. return InheritsFromChoiceDeclaration(node->kind());
  220. }
  221. auto name() const -> const std::string& { return name_; }
  222. auto alternatives() const
  223. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  224. return alternatives_;
  225. }
  226. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  227. return alternatives_;
  228. }
  229. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  230. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  231. return constant_value_;
  232. }
  233. // Sets the value returned by constant_value(). Can only be called once,
  234. // during typechecking.
  235. void set_constant_value(Nonnull<const Value*> value) {
  236. CHECK(!constant_value_.has_value());
  237. constant_value_ = value;
  238. }
  239. private:
  240. std::string name_;
  241. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  242. std::optional<Nonnull<const Value*>> constant_value_;
  243. };
  244. // Global variable definition implements the Declaration concept.
  245. class VariableDeclaration : public Declaration {
  246. public:
  247. VariableDeclaration(SourceLocation source_loc,
  248. Nonnull<BindingPattern*> binding,
  249. std::optional<Nonnull<Expression*>> initializer)
  250. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  251. binding_(binding),
  252. initializer_(initializer) {}
  253. static auto classof(const AstNode* node) -> bool {
  254. return InheritsFromVariableDeclaration(node->kind());
  255. }
  256. auto binding() const -> const BindingPattern& { return *binding_; }
  257. auto binding() -> BindingPattern& { return *binding_; }
  258. auto initializer() const -> const Expression& { return **initializer_; }
  259. auto initializer() -> Expression& { return **initializer_; }
  260. bool has_initializer() const { return initializer_.has_value(); }
  261. private:
  262. // TODO: split this into a non-optional name and a type, initialized by
  263. // a constructor that takes a BindingPattern and handles errors like a
  264. // missing name.
  265. Nonnull<BindingPattern*> binding_;
  266. std::optional<Nonnull<Expression*>> initializer_;
  267. };
  268. } // namespace Carbon
  269. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_