declaration.h 14 KB

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