file_context.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #ifndef CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
  6. #include "clang/Basic/CodeGenOptions.h"
  7. #include "clang/CodeGen/ModuleBuilder.h"
  8. #include "clang/Lex/PreprocessorOptions.h"
  9. #include "llvm/IR/Constants.h"
  10. #include "llvm/IR/DIBuilder.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Module.h"
  13. #include "toolchain/parse/tree_and_subtrees.h"
  14. #include "toolchain/sem_ir/file.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/inst_namer.h"
  17. namespace Carbon::Lower {
  18. // Context and shared functionality for lowering handlers.
  19. class FileContext {
  20. public:
  21. // Location information for use with DebugInfo. The line_number and
  22. // column_number are >= 0, with 0 as unknown, so that they can be passed
  23. // directly to DebugInfo.
  24. struct LocForDI {
  25. llvm::StringRef filename;
  26. int32_t line_number;
  27. int32_t column_number;
  28. };
  29. explicit FileContext(
  30. llvm::LLVMContext& llvm_context,
  31. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  32. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  33. tree_and_subtrees_getters_for_debug_info,
  34. llvm::StringRef module_name, const SemIR::File& sem_ir,
  35. clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
  36. llvm::raw_ostream* vlog_stream);
  37. // Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
  38. // the main execution loop.
  39. auto Run() -> std::unique_ptr<llvm::Module>;
  40. // Create the DICompileUnit metadata for this compilation.
  41. auto BuildDICompileUnit(llvm::StringRef module_name,
  42. llvm::Module& llvm_module,
  43. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  44. // Creates the Clang `CodeGenerator` to generate LLVM module from imported C++
  45. // code. Returns null when not importing C++.
  46. auto CreateCppCodeGenerator() -> std::unique_ptr<clang::CodeGenerator>;
  47. // Gets a callable's function. Returns nullptr for a builtin.
  48. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  49. return functions_[function_id.index];
  50. }
  51. // Gets a or creates callable's function. Returns nullptr for a builtin.
  52. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  53. SemIR::SpecificId specific_id) -> llvm::Function*;
  54. // Returns a lowered type for the given type_id.
  55. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  56. CARBON_CHECK(type_id.has_value(), "Should not be called with `None`");
  57. CARBON_CHECK(type_id.is_concrete(), "Lowering symbolic type {0}: {1}",
  58. type_id, sem_ir().types().GetAsInst(type_id));
  59. CARBON_CHECK(types_[type_id.index], "Missing type {0}: {1}", type_id,
  60. sem_ir().types().GetAsInst(type_id));
  61. return types_[type_id.index];
  62. }
  63. // Returns location information for use with DebugInfo.
  64. auto GetLocForDI(SemIR::InstId inst_id) -> LocForDI;
  65. // Returns a lowered value to use for a value of type `type`.
  66. auto GetTypeAsValue() -> llvm::Constant* {
  67. return llvm::ConstantStruct::get(GetTypeType());
  68. }
  69. // Returns a lowered value to use for a value of int literal type.
  70. auto GetIntLiteralAsValue() -> llvm::Constant* {
  71. // TODO: Consider adding a named struct type for integer literals.
  72. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  73. }
  74. // Returns a global value for the given instruction.
  75. auto GetGlobal(SemIR::InstId inst_id, SemIR::SpecificId specific_id)
  76. -> llvm::Value*;
  77. // Returns the empty LLVM struct type used to represent the type `type`.
  78. auto GetTypeType() -> llvm::StructType* {
  79. if (!type_type_) {
  80. // `type` is lowered to an empty LLVM StructType.
  81. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  82. }
  83. return type_type_;
  84. }
  85. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  86. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  87. auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
  88. auto cpp_ast() -> clang::ASTUnit* { return cpp_ast_; }
  89. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  90. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  91. return global_variables_;
  92. }
  93. auto printf_int_format_string() -> llvm::Value* {
  94. return printf_int_format_string_;
  95. }
  96. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  97. CARBON_CHECK(!printf_int_format_string_,
  98. "PrintInt formatting string already generated");
  99. printf_int_format_string_ = printf_int_format_string;
  100. }
  101. struct FunctionTypeInfo {
  102. llvm::FunctionType* type;
  103. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  104. llvm::Type* return_type = nullptr;
  105. SemIR::InstId return_param_id = SemIR::InstId::None;
  106. };
  107. // Retrieve various features of the function's type useful for constructing
  108. // the `llvm::Type` for the `llvm::Function`. If any part of the type can't be
  109. // manifest (eg: incomplete return or parameter types), then the result is as
  110. // if the type was `void()`.
  111. auto BuildFunctionTypeInfo(const SemIR::Function& function,
  112. SemIR::SpecificId specific_id) -> FunctionTypeInfo;
  113. // Builds the global for the given instruction, which should then be cached by
  114. // the caller.
  115. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  116. -> llvm::GlobalVariable*;
  117. private:
  118. // Builds the declaration for the given function, which should then be cached
  119. // by the caller.
  120. auto BuildFunctionDecl(SemIR::FunctionId function_id,
  121. SemIR::SpecificId specific_id =
  122. SemIR::SpecificId::None) -> llvm::Function*;
  123. // Builds the definition for the given function. If the function is only a
  124. // declaration with no definition, does nothing. If this is a generic it'll
  125. // only be lowered if the specific_id is specified. During this lowering of
  126. // a generic, more generic functions may be added for lowering.
  127. auto BuildFunctionDefinition(
  128. SemIR::FunctionId function_id,
  129. SemIR::SpecificId specific_id = SemIR::SpecificId::None) -> void;
  130. // Builds a functions body. Common functionality for all functions.
  131. auto BuildFunctionBody(
  132. SemIR::FunctionId function_id, const SemIR::Function& function,
  133. llvm::Function* llvm_function,
  134. SemIR::SpecificId specific_id = SemIR::SpecificId::None) -> void;
  135. // Build the DISubprogram metadata for the given function.
  136. auto BuildDISubprogram(const SemIR::Function& function,
  137. const llvm::Function* llvm_function)
  138. -> llvm::DISubprogram*;
  139. // Builds the type for the given instruction, which should then be cached by
  140. // the caller.
  141. auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
  142. auto BuildVtable(const SemIR::Class& class_info) -> llvm::GlobalVariable*;
  143. // State for building the LLVM IR.
  144. llvm::LLVMContext* llvm_context_;
  145. std::unique_ptr<llvm::Module> llvm_module_;
  146. // The filesystem for source code.
  147. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  148. // State for building the LLVM IR debug info metadata.
  149. llvm::DIBuilder di_builder_;
  150. // The DICompileUnit, if any - null implies debug info is not being emitted.
  151. llvm::DICompileUnit* di_compile_unit_;
  152. // The trees are only provided when debug info should be emitted.
  153. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  154. tree_and_subtrees_getters_for_debug_info_;
  155. // The input SemIR.
  156. const SemIR::File* const sem_ir_;
  157. // A mutable Clang AST is necessary for lowering since using the AST in lower
  158. // modifies it.
  159. clang::ASTUnit* cpp_ast_;
  160. // The options used to create the Clang Code Generator.
  161. clang::HeaderSearchOptions cpp_header_search_options_;
  162. clang::PreprocessorOptions cpp_preprocessor_options_;
  163. clang::CodeGenOptions cpp_code_gen_options_;
  164. // The Clang `CodeGenerator` to generate LLVM module from imported C++
  165. // code. Should be initialized using `CreateCppCodeGenerator()`. Can be null
  166. // if no C++ code is imported.
  167. std::unique_ptr<clang::CodeGenerator> cpp_code_generator_;
  168. // The instruction namer, if given.
  169. const SemIR::InstNamer* const inst_namer_;
  170. // The optional vlog stream.
  171. llvm::raw_ostream* vlog_stream_;
  172. // Maps callables to lowered functions. SemIR treats callables as the
  173. // canonical form of a function, so lowering needs to do the same.
  174. // Vector indexes correspond to `FunctionId` indexes. We resize this directly
  175. // to the correct size.
  176. llvm::SmallVector<llvm::Function*, 0> functions_;
  177. // Maps specific callables to lowered functions. Vector indexes correspond to
  178. // `SpecificId` indexes. We resize this directly to the correct size.
  179. llvm::SmallVector<llvm::Function*, 0> specific_functions_;
  180. // Maps which specific functions are generics that need to have their
  181. // definitions lowered after the lowering of other definitions.
  182. // This list may grow while lowering generic definitions from this list.
  183. // The list uses the `SpecificId` to index into specific_functions_.
  184. llvm::SmallVector<std::pair<SemIR::FunctionId, SemIR::SpecificId>, 10>
  185. specific_function_definitions_;
  186. // Provides lowered versions of types.
  187. // Vector indexes correspond to `TypeId` indexes for non-symbolic types. We
  188. // resize this directly to the (often large) correct size.
  189. llvm::SmallVector<llvm::Type*, 0> types_;
  190. // Lowered version of the builtin type `type`.
  191. llvm::StructType* type_type_ = nullptr;
  192. // Maps constants to their lowered values.
  193. // Vector indexes correspond to `InstId` indexes for constant instructions. We
  194. // resize this directly to the (often large) correct size.
  195. llvm::SmallVector<llvm::Constant*, 0> constants_;
  196. // Maps global variables to their lowered variant.
  197. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  198. // Global format string for `printf.int.format` used by the PrintInt builtin.
  199. llvm::Value* printf_int_format_string_ = nullptr;
  200. };
  201. } // namespace Carbon::Lower
  202. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_