lowering_context.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_LOWERING_LOWERING_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWERING_LOWERING_CONTEXT_H_
  6. #include "llvm/IR/Constants.h"
  7. #include "llvm/IR/LLVMContext.h"
  8. #include "llvm/IR/Module.h"
  9. #include "toolchain/semantics/semantics_ir.h"
  10. #include "toolchain/semantics/semantics_node.h"
  11. namespace Carbon {
  12. // Context and shared functionality for lowering handlers.
  13. class LoweringContext {
  14. public:
  15. explicit LoweringContext(llvm::LLVMContext& llvm_context,
  16. llvm::StringRef module_name,
  17. const SemanticsIR& semantics_ir,
  18. llvm::raw_ostream* vlog_stream);
  19. // Lowers the SemanticsIR to LLVM IR. Should only be called once, and handles
  20. // the main execution loop.
  21. auto Run() -> std::unique_ptr<llvm::Module>;
  22. // Gets a callable's function.
  23. auto GetFunction(SemanticsFunctionId function_id) -> llvm::Function* {
  24. CARBON_CHECK(functions_[function_id.index] != nullptr) << function_id;
  25. return functions_[function_id.index];
  26. }
  27. // Returns a lowered type for the given type_id.
  28. auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
  29. // InvalidType should not be passed in.
  30. if (type_id == SemanticsTypeId::TypeType) {
  31. return GetTypeType();
  32. }
  33. CARBON_CHECK(type_id.index >= 0) << type_id;
  34. return types_[type_id.index];
  35. }
  36. // Returns a lowered value to use for a value of type `type`.
  37. auto GetTypeAsValue() -> llvm::Value* {
  38. return llvm::ConstantStruct::get(GetTypeType());
  39. }
  40. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  41. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  42. auto semantics_ir() -> const SemanticsIR& { return *semantics_ir_; }
  43. private:
  44. // Builds the declaration for the given function, which should then be cached
  45. // by the caller.
  46. auto BuildFunctionDeclaration(SemanticsFunctionId function_id)
  47. -> llvm::Function*;
  48. // Builds the definition for the given function. If the function is only a
  49. // declaration with no definition, does nothing.
  50. auto BuildFunctionDefinition(SemanticsFunctionId function_id) -> void;
  51. // Builds the type for the given node, which should then be cached by the
  52. // caller.
  53. auto BuildType(SemanticsNodeId node_id) -> llvm::Type*;
  54. // Returns the empty LLVM struct type used to represent the type `type`.
  55. auto GetTypeType() -> llvm::StructType* {
  56. if (!type_type_) {
  57. // `type` is lowered to an empty LLVM StructType.
  58. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  59. }
  60. return type_type_;
  61. }
  62. // State for building the LLVM IR.
  63. llvm::LLVMContext* llvm_context_;
  64. std::unique_ptr<llvm::Module> llvm_module_;
  65. // The input Semantics IR.
  66. const SemanticsIR* const semantics_ir_;
  67. // The optional vlog stream.
  68. llvm::raw_ostream* vlog_stream_;
  69. // Maps callables to lowered functions. Semantics treats callables as the
  70. // canonical form of a function, so lowering needs to do the same.
  71. llvm::SmallVector<llvm::Function*> functions_;
  72. // Provides lowered versions of types.
  73. llvm::SmallVector<llvm::Type*> types_;
  74. // Lowered version of the builtin type `type`.
  75. llvm::StructType* type_type_ = nullptr;
  76. };
  77. } // namespace Carbon
  78. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_CONTEXT_H_