declaration.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "executable_semantics/ast/declaration.h"
  5. #include "llvm/ADT/StringExtras.h"
  6. #include "llvm/Support/Casting.h"
  7. namespace Carbon {
  8. using llvm::cast;
  9. Declaration::~Declaration() = default;
  10. void Declaration::Print(llvm::raw_ostream& out) const {
  11. switch (kind()) {
  12. case DeclarationKind::InterfaceDeclaration: {
  13. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  14. out << "interface " << iface_decl.name() << " {\n";
  15. for (Nonnull<Declaration*> m : iface_decl.members()) {
  16. out << *m;
  17. }
  18. out << "}\n";
  19. break;
  20. }
  21. case DeclarationKind::ImplDeclaration: {
  22. const auto& impl_decl = cast<ImplDeclaration>(*this);
  23. switch (impl_decl.kind()) {
  24. case ImplKind::InternalImpl:
  25. break;
  26. case ImplKind::ExternalImpl:
  27. out << "external ";
  28. break;
  29. }
  30. out << "impl " << *impl_decl.impl_type() << " as "
  31. << impl_decl.interface() << " {\n";
  32. for (Nonnull<Declaration*> m : impl_decl.members()) {
  33. out << *m;
  34. }
  35. out << "}\n";
  36. break;
  37. }
  38. case DeclarationKind::FunctionDeclaration:
  39. cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
  40. break;
  41. case DeclarationKind::ClassDeclaration: {
  42. const auto& class_decl = cast<ClassDeclaration>(*this);
  43. out << "class " << class_decl.name() << " {\n";
  44. for (Nonnull<Declaration*> m : class_decl.members()) {
  45. out << *m;
  46. }
  47. out << "}\n";
  48. break;
  49. }
  50. case DeclarationKind::ChoiceDeclaration: {
  51. const auto& choice = cast<ChoiceDeclaration>(*this);
  52. out << "choice " << choice.name() << " {\n";
  53. for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
  54. out << *alt << ";\n";
  55. }
  56. out << "}\n";
  57. break;
  58. }
  59. case DeclarationKind::VariableDeclaration: {
  60. const auto& var = cast<VariableDeclaration>(*this);
  61. out << "var " << var.binding();
  62. if (var.has_initializer()) {
  63. out << " = " << var.initializer();
  64. }
  65. out << ";\n";
  66. break;
  67. }
  68. }
  69. }
  70. auto GetName(const Declaration& declaration) -> std::optional<std::string> {
  71. switch (declaration.kind()) {
  72. case DeclarationKind::FunctionDeclaration:
  73. return cast<FunctionDeclaration>(declaration).name();
  74. case DeclarationKind::ClassDeclaration:
  75. return cast<ClassDeclaration>(declaration).name();
  76. case DeclarationKind::ChoiceDeclaration:
  77. return cast<ChoiceDeclaration>(declaration).name();
  78. case DeclarationKind::InterfaceDeclaration:
  79. return cast<InterfaceDeclaration>(declaration).name();
  80. case DeclarationKind::VariableDeclaration:
  81. return cast<VariableDeclaration>(declaration).binding().name();
  82. case DeclarationKind::ImplDeclaration:
  83. return std::nullopt;
  84. }
  85. }
  86. void GenericBinding::Print(llvm::raw_ostream& out) const {
  87. out << name() << ":! " << type();
  88. }
  89. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  90. switch (kind_) {
  91. case ReturnKind::Omitted:
  92. return;
  93. case ReturnKind::Auto:
  94. out << "-> auto";
  95. return;
  96. case ReturnKind::Expression:
  97. CHECK(type_expression_.has_value());
  98. out << "-> " << **type_expression_;
  99. return;
  100. }
  101. }
  102. // Look for the `me` parameter in the `deduced_parameters_`
  103. // and put it in the `me_pattern_`.
  104. void FunctionDeclaration::ResolveDeducedAndReceiver(
  105. const std::vector<Nonnull<AstNode*>>& deduced_params) {
  106. for (Nonnull<AstNode*> param : deduced_params) {
  107. switch (param->kind()) {
  108. case AstNodeKind::GenericBinding:
  109. deduced_parameters_.push_back(&cast<GenericBinding>(*param));
  110. break;
  111. case AstNodeKind::BindingPattern: {
  112. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
  113. if (me_pattern_.has_value() || bp->name() != "me") {
  114. FATAL_COMPILATION_ERROR(source_loc())
  115. << "illegal binding pattern in implicit parameter list";
  116. }
  117. me_pattern_ = bp;
  118. break;
  119. }
  120. default:
  121. FATAL_COMPILATION_ERROR(source_loc())
  122. << "illegal AST node in implicit parameter list";
  123. }
  124. }
  125. }
  126. void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  127. out << "fn " << name_ << " ";
  128. if (!deduced_parameters_.empty()) {
  129. out << "[";
  130. llvm::ListSeparator sep;
  131. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  132. out << sep << *deduced;
  133. }
  134. out << "]";
  135. }
  136. out << *param_pattern_ << return_term_;
  137. if (body_) {
  138. out << " {\n";
  139. (*body_)->PrintDepth(depth, out);
  140. out << "\n}\n";
  141. } else {
  142. out << ";\n";
  143. }
  144. }
  145. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  146. out << "alt " << name() << " " << signature();
  147. }
  148. } // namespace Carbon