declaration.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, std::string name,
  33. const Expression* type,
  34. const Expression* initializer)
  35. -> const Declaration {
  36. Declaration d;
  37. d.value = VariableDeclaration({.source_location = source_location,
  38. .name = std::move(name),
  39. .type = type,
  40. .initializer = initializer});
  41. return d;
  42. }
  43. auto Declaration::GetFunctionDeclaration() const -> const FunctionDeclaration& {
  44. return std::get<FunctionDeclaration>(value);
  45. }
  46. auto Declaration::GetStructDeclaration() const -> const StructDeclaration& {
  47. return std::get<StructDeclaration>(value);
  48. }
  49. auto Declaration::GetChoiceDeclaration() const -> const ChoiceDeclaration& {
  50. return std::get<ChoiceDeclaration>(value);
  51. }
  52. auto Declaration::GetVariableDeclaration() const -> const VariableDeclaration& {
  53. return std::get<VariableDeclaration>(value);
  54. }
  55. void Declaration::Print(llvm::raw_ostream& out) const {
  56. switch (tag()) {
  57. case DeclarationKind::FunctionDeclaration:
  58. out << GetFunctionDeclaration().definition;
  59. break;
  60. case DeclarationKind::StructDeclaration: {
  61. const StructDefinition& struct_def = GetStructDeclaration().definition;
  62. out << "struct " << struct_def.name << " {\n";
  63. for (Member* m : struct_def.members) {
  64. out << *m;
  65. }
  66. out << "}\n";
  67. break;
  68. }
  69. case DeclarationKind::ChoiceDeclaration: {
  70. const auto& choice = GetChoiceDeclaration();
  71. out << "choice " << choice.name << " {\n";
  72. for (const auto& [name, signature] : choice.alternatives) {
  73. out << "alt " << name << " " << *signature << ";\n";
  74. }
  75. out << "}\n";
  76. break;
  77. }
  78. case DeclarationKind::VariableDeclaration: {
  79. const auto& var = GetVariableDeclaration();
  80. out << "var " << *var.type << " : " << var.name << " = "
  81. << *var.initializer << "\n";
  82. break;
  83. }
  84. }
  85. }
  86. } // namespace Carbon