lowering_context.h 2.8 KB

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