context.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/lower/context.h"
  5. #include "common/check.h"
  6. #include "common/growing_range.h"
  7. #include "common/vlog.h"
  8. #include "llvm/Transforms/Utils/ModuleUtils.h"
  9. #include "toolchain/lower/file_context.h"
  10. #include "toolchain/sem_ir/inst_namer.h"
  11. namespace Carbon::Lower {
  12. Context::Context(
  13. llvm::LLVMContext& llvm_context,
  14. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs, bool want_debug_info,
  15. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
  16. llvm::StringRef module_name, llvm::raw_ostream* vlog_stream)
  17. : llvm_context_(&llvm_context),
  18. llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
  19. file_system_(std::move(fs)),
  20. di_builder_(*llvm_module_),
  21. di_compile_unit_(
  22. want_debug_info
  23. ? BuildDICompileUnit(module_name, *llvm_module_, di_builder_)
  24. : nullptr),
  25. tree_and_subtrees_getters_(tree_and_subtrees_getters),
  26. vlog_stream_(vlog_stream) {}
  27. auto Context::GetFileContext(const SemIR::File* file,
  28. const SemIR::InstNamer* inst_namer)
  29. -> FileContext& {
  30. auto insert_result = file_contexts_.Insert(file->check_ir_id(), [&] {
  31. auto file_context =
  32. std::make_unique<FileContext>(*this, *file, inst_namer, vlog_stream_);
  33. file_context->PrepareToLower();
  34. return file_context;
  35. });
  36. return *insert_result.value();
  37. }
  38. auto Context::LowerPendingDefinitions() -> void {
  39. // Lower function definitions for generics.
  40. for (auto [file_context, function_id, specific_id] :
  41. GrowingRange(specific_function_definitions_)) {
  42. file_context->BuildFunctionDefinition(function_id, specific_id);
  43. }
  44. }
  45. auto Context::Finalize() && -> std::unique_ptr<llvm::Module> {
  46. LowerPendingDefinitions();
  47. file_contexts_.ForEach(
  48. [](auto, auto& file_context) { file_context->Finalize(); });
  49. return std::move(llvm_module_);
  50. }
  51. auto Context::BuildDICompileUnit(llvm::StringRef module_name,
  52. llvm::Module& llvm_module,
  53. llvm::DIBuilder& di_builder)
  54. -> llvm::DICompileUnit* {
  55. llvm_module.addModuleFlag(llvm::Module::Max, "Dwarf Version", 5);
  56. llvm_module.addModuleFlag(llvm::Module::Warning, "Debug Info Version",
  57. llvm::DEBUG_METADATA_VERSION);
  58. // TODO: Include directory path in the compile_unit_file.
  59. llvm::DIFile* compile_unit_file = di_builder.createFile(module_name, "");
  60. // TODO: Introduce a new language code for Carbon. C works well for now since
  61. // it's something debuggers will already know/have support for at least.
  62. // Probably have to bump to C++ at some point for virtual functions,
  63. // templates, etc.
  64. return di_builder.createCompileUnit(llvm::dwarf::DW_LANG_C, compile_unit_file,
  65. "carbon",
  66. /*isOptimized=*/false, /*Flags=*/"",
  67. /*RV=*/0);
  68. }
  69. auto Context::GetLocForDI(SemIR::AbsoluteNodeId abs_node_id) -> LocForDI {
  70. const auto& tree_and_subtrees =
  71. tree_and_subtrees_getters()[abs_node_id.check_ir_id().index]();
  72. const auto& tokens = tree_and_subtrees.tree().tokens();
  73. if (abs_node_id.node_id().has_value()) {
  74. auto token =
  75. tree_and_subtrees.GetSubtreeTokenRange(abs_node_id.node_id()).begin;
  76. return {.filename = tokens.source().filename(),
  77. .line_number = tokens.GetLineNumber(token),
  78. .column_number = tokens.GetColumnNumber(token)};
  79. } else {
  80. return {.filename = tokens.source().filename(),
  81. .line_number = 0,
  82. .column_number = 0};
  83. }
  84. }
  85. } // namespace Carbon::Lower