declaration.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. namespace Carbon {
  6. auto Declaration::MakeFunctionDeclaration(FunctionDefinition definition)
  7. -> const Declaration {
  8. Declaration d;
  9. d.value = FunctionDeclaration({.definition = definition});
  10. return d;
  11. }
  12. auto Declaration::MakeStructDeclaration(int line_num, std::string name,
  13. std::list<Member*> members)
  14. -> const Declaration {
  15. Declaration d;
  16. d.value = StructDeclaration(
  17. {.definition = StructDefinition({.line_num = line_num,
  18. .name = std::move(name),
  19. .members = std::move(members)})});
  20. return d;
  21. }
  22. auto Declaration::MakeChoiceDeclaration(
  23. int line_num, std::string name,
  24. std::list<std::pair<std::string, const Expression*>> alternatives)
  25. -> const Declaration {
  26. Declaration d;
  27. d.value = ChoiceDeclaration({.line_num = line_num,
  28. .name = std::move(name),
  29. .alternatives = std::move(alternatives)});
  30. return d;
  31. }
  32. auto Declaration::MakeVariableDeclaration(int source_location,
  33. const BindingPattern* binding,
  34. const Expression* initializer)
  35. -> const Declaration {
  36. Declaration d;
  37. d.value = VariableDeclaration({.source_location = source_location,
  38. .binding = binding,
  39. .initializer = initializer});
  40. return d;
  41. }
  42. auto Declaration::GetFunctionDeclaration() const -> const FunctionDeclaration& {
  43. return std::get<FunctionDeclaration>(value);
  44. }
  45. auto Declaration::GetStructDeclaration() const -> const StructDeclaration& {
  46. return std::get<StructDeclaration>(value);
  47. }
  48. auto Declaration::GetChoiceDeclaration() const -> const ChoiceDeclaration& {
  49. return std::get<ChoiceDeclaration>(value);
  50. }
  51. auto Declaration::GetVariableDeclaration() const -> const VariableDeclaration& {
  52. return std::get<VariableDeclaration>(value);
  53. }
  54. void Declaration::Print(llvm::raw_ostream& out) const {
  55. switch (tag()) {
  56. case DeclarationKind::FunctionDeclaration:
  57. out << GetFunctionDeclaration().definition;
  58. break;
  59. case DeclarationKind::StructDeclaration: {
  60. const StructDefinition& struct_def = GetStructDeclaration().definition;
  61. out << "struct " << struct_def.name << " {\n";
  62. for (Member* m : struct_def.members) {
  63. out << *m;
  64. }
  65. out << "}\n";
  66. break;
  67. }
  68. case DeclarationKind::ChoiceDeclaration: {
  69. const auto& choice = GetChoiceDeclaration();
  70. out << "choice " << choice.name << " {\n";
  71. for (const auto& [name, signature] : choice.alternatives) {
  72. out << "alt " << name << " " << *signature << ";\n";
  73. }
  74. out << "}\n";
  75. break;
  76. }
  77. case DeclarationKind::VariableDeclaration: {
  78. const auto& var = GetVariableDeclaration();
  79. out << "var " << *var.binding << " = " << *var.initializer << "\n";
  80. break;
  81. }
  82. }
  83. }
  84. } // namespace Carbon