declaration.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #include "explorer/ast/declaration.h"
  5. #include "explorer/ast/value.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. #include "llvm/Support/Casting.h"
  8. namespace Carbon {
  9. using llvm::cast;
  10. Declaration::~Declaration() = default;
  11. void Declaration::Print(llvm::raw_ostream& out) const {
  12. switch (kind()) {
  13. case DeclarationKind::NamespaceDeclaration:
  14. PrintID(out);
  15. out << ";";
  16. break;
  17. case DeclarationKind::InterfaceDeclaration:
  18. case DeclarationKind::ConstraintDeclaration: {
  19. const auto& iface_decl = cast<ConstraintTypeDeclaration>(*this);
  20. PrintID(out);
  21. out << " {\n";
  22. for (Nonnull<Declaration*> m : iface_decl.members()) {
  23. out << *m;
  24. }
  25. out << "}\n";
  26. break;
  27. }
  28. case DeclarationKind::ImplDeclaration: {
  29. const auto& impl_decl = cast<ImplDeclaration>(*this);
  30. PrintID(out);
  31. out << " {\n";
  32. for (Nonnull<Declaration*> m : impl_decl.members()) {
  33. out << *m;
  34. }
  35. out << "}\n";
  36. break;
  37. }
  38. case DeclarationKind::MatchFirstDeclaration: {
  39. const auto& match_first_decl = cast<MatchFirstDeclaration>(*this);
  40. PrintID(out);
  41. out << " {\n";
  42. for (Nonnull<const ImplDeclaration*> m :
  43. match_first_decl.impl_declarations()) {
  44. out << *m;
  45. }
  46. out << "}\n";
  47. break;
  48. }
  49. case DeclarationKind::FunctionDeclaration:
  50. cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
  51. break;
  52. case DeclarationKind::DestructorDeclaration:
  53. cast<DestructorDeclaration>(*this).PrintDepth(-1, out);
  54. break;
  55. case DeclarationKind::ClassDeclaration: {
  56. const auto& class_decl = cast<ClassDeclaration>(*this);
  57. PrintID(out);
  58. if (class_decl.type_params().has_value()) {
  59. out << **class_decl.type_params();
  60. }
  61. out << " {\n";
  62. for (Nonnull<Declaration*> m : class_decl.members()) {
  63. out << *m;
  64. }
  65. out << "}\n";
  66. break;
  67. }
  68. case DeclarationKind::MixinDeclaration: {
  69. const auto& mixin_decl = cast<MixinDeclaration>(*this);
  70. PrintID(out);
  71. out << "{\n";
  72. for (Nonnull<Declaration*> m : mixin_decl.members()) {
  73. out << *m;
  74. }
  75. out << "}\n";
  76. break;
  77. }
  78. case DeclarationKind::MixDeclaration: {
  79. const auto& mix_decl = cast<MixDeclaration>(*this);
  80. PrintID(out);
  81. out << mix_decl.mixin() << ";";
  82. break;
  83. }
  84. case DeclarationKind::ChoiceDeclaration: {
  85. const auto& choice = cast<ChoiceDeclaration>(*this);
  86. PrintID(out);
  87. out << " {\n";
  88. for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
  89. out << *alt << ";\n";
  90. }
  91. out << "}\n";
  92. break;
  93. }
  94. case DeclarationKind::VariableDeclaration: {
  95. const auto& var = cast<VariableDeclaration>(*this);
  96. PrintID(out);
  97. if (var.has_initializer()) {
  98. out << " = " << var.initializer();
  99. }
  100. out << ";\n";
  101. break;
  102. }
  103. case DeclarationKind::InterfaceExtendDeclaration:
  104. case DeclarationKind::InterfaceRequireDeclaration:
  105. case DeclarationKind::AssociatedConstantDeclaration: {
  106. PrintID(out);
  107. out << ";\n";
  108. break;
  109. }
  110. case DeclarationKind::SelfDeclaration: {
  111. out << "Self";
  112. break;
  113. }
  114. case DeclarationKind::AliasDeclaration: {
  115. const auto& alias = cast<AliasDeclaration>(*this);
  116. PrintID(out);
  117. out << " = " << alias.target() << ";\n";
  118. break;
  119. }
  120. case DeclarationKind::ExtendBaseDeclaration: {
  121. PrintID(out);
  122. out << ";\n";
  123. break;
  124. }
  125. }
  126. }
  127. void Declaration::PrintID(llvm::raw_ostream& out) const {
  128. switch (kind()) {
  129. case DeclarationKind::NamespaceDeclaration:
  130. out << "namespace " << cast<NamespaceDeclaration>(*this).name();
  131. break;
  132. case DeclarationKind::InterfaceDeclaration: {
  133. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  134. out << "interface " << iface_decl.name();
  135. break;
  136. }
  137. case DeclarationKind::ConstraintDeclaration: {
  138. const auto& constraint_decl = cast<ConstraintDeclaration>(*this);
  139. out << "constraint " << constraint_decl.name();
  140. break;
  141. }
  142. case DeclarationKind::ImplDeclaration: {
  143. const auto& impl_decl = cast<ImplDeclaration>(*this);
  144. switch (impl_decl.kind()) {
  145. case ImplKind::InternalImpl:
  146. out << "extend ";
  147. break;
  148. case ImplKind::ExternalImpl:
  149. break;
  150. }
  151. out << "impl ";
  152. if (!impl_decl.deduced_parameters().empty()) {
  153. out << "forall [";
  154. llvm::ListSeparator sep;
  155. for (const auto* param : impl_decl.deduced_parameters()) {
  156. out << sep << *param;
  157. }
  158. out << "] ";
  159. }
  160. if (impl_decl.kind() != ImplKind::InternalImpl) {
  161. out << *impl_decl.impl_type() << " ";
  162. }
  163. out << "as " << impl_decl.interface();
  164. break;
  165. }
  166. case DeclarationKind::MatchFirstDeclaration:
  167. out << "match_first";
  168. break;
  169. case DeclarationKind::FunctionDeclaration:
  170. out << "fn " << cast<FunctionDeclaration>(*this).name();
  171. break;
  172. case DeclarationKind::DestructorDeclaration:
  173. out << *GetName(*this);
  174. break;
  175. case DeclarationKind::ClassDeclaration: {
  176. const auto& class_decl = cast<ClassDeclaration>(*this);
  177. out << "class " << class_decl.name();
  178. break;
  179. }
  180. case DeclarationKind::MixinDeclaration: {
  181. const auto& mixin_decl = cast<MixinDeclaration>(*this);
  182. out << "__mixin " << mixin_decl.name();
  183. if (mixin_decl.self()->type().kind() != ExpressionKind::TypeTypeLiteral) {
  184. out << " for " << mixin_decl.self()->type();
  185. }
  186. break;
  187. }
  188. case DeclarationKind::MixDeclaration: {
  189. out << "__mix ";
  190. break;
  191. }
  192. case DeclarationKind::ChoiceDeclaration: {
  193. const auto& choice = cast<ChoiceDeclaration>(*this);
  194. out << "choice " << choice.name();
  195. break;
  196. }
  197. case DeclarationKind::VariableDeclaration: {
  198. const auto& var = cast<VariableDeclaration>(*this);
  199. out << "var " << var.binding();
  200. break;
  201. }
  202. case DeclarationKind::InterfaceExtendDeclaration: {
  203. const auto& extend = cast<InterfaceExtendDeclaration>(*this);
  204. out << "extend " << *extend.base();
  205. break;
  206. }
  207. case DeclarationKind::InterfaceRequireDeclaration: {
  208. const auto& impl = cast<InterfaceRequireDeclaration>(*this);
  209. out << "require " << *impl.impl_type() << " impls " << *impl.constraint();
  210. break;
  211. }
  212. case DeclarationKind::AssociatedConstantDeclaration: {
  213. const auto& let = cast<AssociatedConstantDeclaration>(*this);
  214. out << "let " << let.binding();
  215. break;
  216. }
  217. case DeclarationKind::SelfDeclaration: {
  218. out << "Self";
  219. break;
  220. }
  221. case DeclarationKind::AliasDeclaration: {
  222. const auto& alias = cast<AliasDeclaration>(*this);
  223. out << "alias " << alias.name();
  224. break;
  225. }
  226. case DeclarationKind::ExtendBaseDeclaration: {
  227. const auto& extend = cast<ExtendBaseDeclaration>(*this);
  228. out << "extend base: " << *extend.base_class();
  229. break;
  230. }
  231. }
  232. }
  233. void DeclaredName::Print(llvm::raw_ostream& out) const {
  234. for (const auto& [loc, name] : qualifiers()) {
  235. out << name << ".";
  236. }
  237. out << inner_name();
  238. }
  239. auto GetName(const Declaration& declaration)
  240. -> std::optional<std::string_view> {
  241. switch (declaration.kind()) {
  242. case DeclarationKind::NamespaceDeclaration:
  243. return cast<NamespaceDeclaration>(declaration).name().inner_name();
  244. case DeclarationKind::FunctionDeclaration:
  245. return cast<FunctionDeclaration>(declaration).name().inner_name();
  246. case DeclarationKind::DestructorDeclaration:
  247. return "destructor";
  248. case DeclarationKind::ClassDeclaration:
  249. return cast<ClassDeclaration>(declaration).name().inner_name();
  250. case DeclarationKind::MixinDeclaration: {
  251. return cast<MixinDeclaration>(declaration).name().inner_name();
  252. }
  253. case DeclarationKind::MixDeclaration: {
  254. return std::nullopt;
  255. }
  256. case DeclarationKind::ChoiceDeclaration:
  257. return cast<ChoiceDeclaration>(declaration).name().inner_name();
  258. case DeclarationKind::InterfaceDeclaration:
  259. case DeclarationKind::ConstraintDeclaration:
  260. return cast<ConstraintTypeDeclaration>(declaration).name().inner_name();
  261. case DeclarationKind::VariableDeclaration:
  262. return cast<VariableDeclaration>(declaration).binding().name();
  263. case DeclarationKind::AssociatedConstantDeclaration:
  264. return cast<AssociatedConstantDeclaration>(declaration).binding().name();
  265. case DeclarationKind::InterfaceExtendDeclaration:
  266. case DeclarationKind::InterfaceRequireDeclaration:
  267. case DeclarationKind::ImplDeclaration:
  268. case DeclarationKind::MatchFirstDeclaration:
  269. return std::nullopt;
  270. case DeclarationKind::SelfDeclaration:
  271. return SelfDeclaration::name();
  272. case DeclarationKind::AliasDeclaration: {
  273. return cast<AliasDeclaration>(declaration).name().inner_name();
  274. }
  275. case DeclarationKind::ExtendBaseDeclaration: {
  276. return "extend base";
  277. }
  278. }
  279. }
  280. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  281. switch (kind_) {
  282. case ReturnKind::Omitted:
  283. return;
  284. case ReturnKind::Auto:
  285. out << "-> auto";
  286. return;
  287. case ReturnKind::Expression:
  288. CARBON_CHECK(type_expression_.has_value());
  289. out << "-> " << **type_expression_;
  290. return;
  291. }
  292. }
  293. namespace {
  294. // The deduced parameters of a function declaration.
  295. struct DeducedParameters {
  296. // The `self` parameter, if any.
  297. std::optional<Nonnull<Pattern*>> self_pattern;
  298. // All other deduced parameters.
  299. std::vector<Nonnull<GenericBinding*>> resolved_params;
  300. };
  301. // Split the `self` pattern (if any) out of `deduced_params`.
  302. auto SplitDeducedParameters(
  303. SourceLocation source_loc,
  304. const std::vector<Nonnull<AstNode*>>& deduced_params)
  305. -> ErrorOr<DeducedParameters> {
  306. DeducedParameters result;
  307. for (Nonnull<AstNode*> param : deduced_params) {
  308. switch (param->kind()) {
  309. case AstNodeKind::GenericBinding:
  310. result.resolved_params.push_back(&cast<GenericBinding>(*param));
  311. break;
  312. case AstNodeKind::BindingPattern: {
  313. Nonnull<BindingPattern*> binding = &cast<BindingPattern>(*param);
  314. if (binding->name() != "self") {
  315. return ProgramError(source_loc)
  316. << "illegal binding pattern in implicit parameter list";
  317. }
  318. if (result.self_pattern.has_value()) {
  319. return ProgramError(source_loc)
  320. << "parameter list cannot contain more than one `self` "
  321. "parameter";
  322. }
  323. result.self_pattern = binding;
  324. break;
  325. }
  326. case AstNodeKind::AddrPattern: {
  327. Nonnull<AddrPattern*> addr_pattern = &cast<AddrPattern>(*param);
  328. Nonnull<BindingPattern*> binding =
  329. &cast<BindingPattern>(addr_pattern->binding());
  330. if (binding->name() != "self") {
  331. return ProgramError(source_loc)
  332. << "illegal binding pattern in implicit parameter list";
  333. }
  334. if (result.self_pattern.has_value()) {
  335. return ProgramError(source_loc)
  336. << "parameter list cannot contain more than one `self` "
  337. "parameter";
  338. }
  339. result.self_pattern = addr_pattern;
  340. break;
  341. }
  342. default:
  343. return ProgramError(source_loc)
  344. << "illegal AST node in implicit parameter list";
  345. }
  346. }
  347. return result;
  348. }
  349. } // namespace
  350. auto DestructorDeclaration::CreateDestructor(
  351. Nonnull<Arena*> arena, SourceLocation source_loc,
  352. std::vector<Nonnull<AstNode*>> deduced_params,
  353. Nonnull<TuplePattern*> param_pattern, ReturnTerm return_term,
  354. std::optional<Nonnull<Block*>> body, VirtualOverride virt_override)
  355. -> ErrorOr<Nonnull<DestructorDeclaration*>> {
  356. DeducedParameters split_params;
  357. CARBON_ASSIGN_OR_RETURN(split_params,
  358. SplitDeducedParameters(source_loc, deduced_params));
  359. return arena->New<DestructorDeclaration>(
  360. source_loc, std::move(split_params.resolved_params),
  361. split_params.self_pattern, param_pattern, return_term, body,
  362. virt_override);
  363. }
  364. auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
  365. SourceLocation source_loc, DeclaredName name,
  366. std::vector<Nonnull<AstNode*>> deduced_params,
  367. Nonnull<TuplePattern*> param_pattern,
  368. ReturnTerm return_term,
  369. std::optional<Nonnull<Block*>> body,
  370. VirtualOverride virt_override)
  371. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  372. DeducedParameters split_params;
  373. CARBON_ASSIGN_OR_RETURN(split_params,
  374. SplitDeducedParameters(source_loc, deduced_params));
  375. return arena->New<FunctionDeclaration>(
  376. source_loc, std::move(name), std::move(split_params.resolved_params),
  377. split_params.self_pattern, param_pattern, return_term, body,
  378. virt_override);
  379. }
  380. void CallableDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  381. auto name = GetName(*this);
  382. CARBON_CHECK(name) << "Unexpected missing name for `" << *this << "`.";
  383. out << "fn " << *name << " ";
  384. if (!deduced_parameters_.empty()) {
  385. out << "[";
  386. llvm::ListSeparator sep;
  387. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  388. out << sep << *deduced;
  389. }
  390. out << "]";
  391. }
  392. out << *param_pattern_ << return_term_;
  393. if (body_) {
  394. out << " {\n";
  395. (*body_)->PrintDepth(depth, out);
  396. out << "\n}\n";
  397. } else {
  398. out << ";\n";
  399. }
  400. }
  401. ClassDeclaration::ClassDeclaration(CloneContext& context,
  402. const ClassDeclaration& other)
  403. : Declaration(context, other),
  404. name_(other.name_),
  405. extensibility_(other.extensibility_),
  406. self_decl_(context.Clone(other.self_decl_)),
  407. type_params_(context.Clone(other.type_params_)),
  408. members_(context.Clone(other.members_)),
  409. base_type_(context.Clone(other.base_type_)) {}
  410. ExtendBaseDeclaration::ExtendBaseDeclaration(CloneContext& context,
  411. const ExtendBaseDeclaration& other)
  412. : Declaration(context, other),
  413. base_class_(context.Clone(other.base_class_)) {}
  414. ConstraintTypeDeclaration::ConstraintTypeDeclaration(
  415. CloneContext& context, const ConstraintTypeDeclaration& other)
  416. : Declaration(context, other),
  417. name_(other.name_),
  418. params_(context.Clone(other.params_)),
  419. self_type_(context.Clone(other.self_type_)),
  420. self_(context.Clone(other.self_)),
  421. members_(context.Clone(other.members_)),
  422. constraint_type_(context.Clone(other.constraint_type_)) {}
  423. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  424. ImplKind kind, Nonnull<Expression*> impl_type,
  425. Nonnull<Expression*> interface,
  426. std::vector<Nonnull<AstNode*>> deduced_params,
  427. std::vector<Nonnull<Declaration*>> members)
  428. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  429. std::vector<Nonnull<GenericBinding*>> resolved_params;
  430. for (Nonnull<AstNode*> param : deduced_params) {
  431. switch (param->kind()) {
  432. case AstNodeKind::GenericBinding:
  433. resolved_params.push_back(&cast<GenericBinding>(*param));
  434. break;
  435. default:
  436. return ProgramError(source_loc)
  437. << "illegal AST node in implicit parameter list of impl";
  438. }
  439. }
  440. Nonnull<SelfDeclaration*> self_decl =
  441. arena->New<SelfDeclaration>(impl_type->source_loc());
  442. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  443. interface, resolved_params, members);
  444. }
  445. ImplDeclaration::ImplDeclaration(CloneContext& context,
  446. const ImplDeclaration& other)
  447. : Declaration(context, other),
  448. kind_(other.kind_),
  449. deduced_parameters_(context.Clone(other.deduced_parameters_)),
  450. impl_type_(context.Clone(other.impl_type_)),
  451. self_decl_(context.Clone(other.self_decl_)),
  452. interface_(context.Clone(other.interface_)),
  453. constraint_type_(context.Clone(other.constraint_type_)),
  454. members_(context.Clone(other.members_)),
  455. impl_bindings_(context.Remap(other.impl_bindings_)),
  456. match_first_(context.Remap(other.match_first_)) {}
  457. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  458. out << "alt " << name();
  459. if (auto params = parameters()) {
  460. out << **params;
  461. }
  462. }
  463. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  464. out << name();
  465. }
  466. auto ChoiceDeclaration::FindAlternative(std::string_view name) const
  467. -> std::optional<const AlternativeSignature*> {
  468. for (const auto* alt : alternatives()) {
  469. if (alt->name() == name) {
  470. return alt;
  471. }
  472. }
  473. return std::nullopt;
  474. }
  475. MixDeclaration::MixDeclaration(CloneContext& context,
  476. const MixDeclaration& other)
  477. : Declaration(context, other),
  478. mixin_(context.Clone(other.mixin_)),
  479. mixin_value_(context.Clone(other.mixin_value_)) {}
  480. } // namespace Carbon