declaration.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/Support/Casting.h"
  6. namespace Carbon {
  7. using llvm::cast;
  8. void Declaration::Print(llvm::raw_ostream& out) const {
  9. switch (Tag()) {
  10. case Kind::FunctionDeclaration:
  11. out << cast<FunctionDeclaration>(*this).Definition();
  12. break;
  13. case Kind::ClassDeclaration: {
  14. const ClassDefinition& class_def =
  15. cast<ClassDeclaration>(*this).Definition();
  16. out << "class " << class_def.name << " {\n";
  17. for (Member* m : class_def.members) {
  18. out << *m;
  19. }
  20. out << "}\n";
  21. break;
  22. }
  23. case Kind::ChoiceDeclaration: {
  24. const auto& choice = cast<ChoiceDeclaration>(*this);
  25. out << "choice " << choice.Name() << " {\n";
  26. for (const auto& [name, signature] : choice.Alternatives()) {
  27. out << "alt " << name << " " << *signature << ";\n";
  28. }
  29. out << "}\n";
  30. break;
  31. }
  32. case Kind::VariableDeclaration: {
  33. const auto& var = cast<VariableDeclaration>(*this);
  34. out << "var " << *var.Binding() << " = " << *var.Initializer() << "\n";
  35. break;
  36. }
  37. }
  38. }
  39. } // namespace Carbon