declaration.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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::FunctionDeclaration:
  13. cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
  14. break;
  15. case DeclarationKind::ClassDeclaration: {
  16. const auto& class_decl = cast<ClassDeclaration>(*this);
  17. out << "class " << class_decl.name() << " {\n";
  18. for (Nonnull<Declaration*> m : class_decl.members()) {
  19. out << *m;
  20. }
  21. out << "}\n";
  22. break;
  23. }
  24. case DeclarationKind::ChoiceDeclaration: {
  25. const auto& choice = cast<ChoiceDeclaration>(*this);
  26. out << "choice " << choice.name() << " {\n";
  27. for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
  28. out << *alt << ";\n";
  29. }
  30. out << "}\n";
  31. break;
  32. }
  33. case DeclarationKind::VariableDeclaration: {
  34. const auto& var = cast<VariableDeclaration>(*this);
  35. out << "var " << var.binding();
  36. if (var.has_initializer()) {
  37. out << " = " << var.initializer();
  38. }
  39. out << ";\n";
  40. break;
  41. }
  42. }
  43. }
  44. void GenericBinding::Print(llvm::raw_ostream& out) const {
  45. out << name() << ":! " << type();
  46. }
  47. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  48. switch (kind_) {
  49. case ReturnKind::Omitted:
  50. return;
  51. case ReturnKind::Auto:
  52. out << "-> auto";
  53. return;
  54. case ReturnKind::Expression:
  55. out << "-> " << **type_expression_;
  56. return;
  57. }
  58. }
  59. // Look for the `me` parameter in the `deduced_parameters_`
  60. // and put it in the `me_pattern_`.
  61. void FunctionDeclaration::ResolveDeducedAndReceiver(
  62. const std::vector<Nonnull<AstNode*>>& deduced_params) {
  63. for (Nonnull<AstNode*> param : deduced_params) {
  64. switch (param->kind()) {
  65. case AstNodeKind::GenericBinding:
  66. deduced_parameters_.push_back(&cast<GenericBinding>(*param));
  67. break;
  68. case AstNodeKind::BindingPattern: {
  69. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
  70. if (me_pattern_.has_value() || bp->name() != "me") {
  71. FATAL_COMPILATION_ERROR(source_loc())
  72. << "illegal binding pattern in implicit parameter list";
  73. }
  74. me_pattern_ = bp;
  75. break;
  76. }
  77. default:
  78. FATAL_COMPILATION_ERROR(source_loc())
  79. << "illegal AST node in implicit parameter list";
  80. }
  81. }
  82. }
  83. void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  84. out << "fn " << name_ << " ";
  85. if (!deduced_parameters_.empty()) {
  86. out << "[";
  87. llvm::ListSeparator sep;
  88. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  89. out << sep << *deduced;
  90. }
  91. out << "]";
  92. }
  93. out << *param_pattern_ << return_term_;
  94. if (body_) {
  95. out << " {\n";
  96. (*body_)->PrintDepth(depth, out);
  97. out << "\n}\n";
  98. } else {
  99. out << ";\n";
  100. }
  101. }
  102. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  103. out << "alt " << name() << " " << signature();
  104. }
  105. } // namespace Carbon