declaration.cpp 3.5 KB

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