clang_decl.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "toolchain/sem_ir/clang_decl.h"
  5. #include "clang/AST/DeclBase.h"
  6. #include "clang/AST/TextNodeDumper.h"
  7. #include "common/ostream.h"
  8. #include "common/raw_string_ostream.h"
  9. namespace Carbon::SemIR {
  10. auto ClangDeclKey::Print(llvm::raw_ostream& out) const -> void {
  11. RawStringOstream decl_stream;
  12. auto policy = decl->getASTContext().getPrintingPolicy();
  13. policy.TerseOutput = true;
  14. if (isa<clang::TranslationUnitDecl>(decl)) {
  15. decl_stream << "<translation unit>";
  16. } else {
  17. decl->print(decl_stream, policy);
  18. }
  19. if (signature.num_params != -1) {
  20. out << "{decl: \"" << FormatEscaped(decl_stream.TakeStr()) << "\", kind: ";
  21. switch (signature.kind) {
  22. case ClangDeclKey::Signature::Normal:
  23. out << "normal";
  24. break;
  25. case ClangDeclKey::Signature::TuplePattern:
  26. out << "tuple";
  27. break;
  28. }
  29. out << ", num_params: " << signature.num_params << "}";
  30. } else {
  31. out << "\"" << FormatEscaped(decl_stream.TakeStr()) << "\"";
  32. }
  33. }
  34. auto ClangDecl::Print(llvm::raw_ostream& out) const -> void {
  35. out << "{key: " << key << ", inst_id: " << inst_id << "}";
  36. }
  37. ClangDeclStore::ClangDeclStore(CheckIRId check_ir_id) : values_(check_ir_id) {}
  38. auto ClangDeclStore::Add(ClangDecl value) -> ClangDeclId {
  39. auto id = values_.Add(value);
  40. inst_id_to_clang_decl_id_.Insert(value.inst_id, id);
  41. return id;
  42. }
  43. auto ClangDeclStore::Lookup(ClangDeclKey key) const -> ClangDeclId {
  44. return values_.Lookup(key);
  45. }
  46. auto ClangDeclStore::Lookup(InstId inst_id) const -> ClangDeclId {
  47. if (auto result = inst_id_to_clang_decl_id_.Lookup(inst_id)) {
  48. return result.value();
  49. }
  50. return ClangDeclId::None;
  51. }
  52. auto ClangDeclStore::OutputYaml() const -> Yaml::OutputMapping {
  53. return values_.OutputYaml();
  54. }
  55. auto ClangDeclStore::CollectMemUsage(MemUsage& mem_usage,
  56. llvm::StringRef label) const -> void {
  57. values_.CollectMemUsage(mem_usage, label);
  58. mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_id_to_clang_decl_id_"),
  59. inst_id_to_clang_decl_id_);
  60. }
  61. } // namespace Carbon::SemIR