lowering_function_context.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_FUNCTION_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
  6. #include "llvm/IR/IRBuilder.h"
  7. #include "llvm/IR/LLVMContext.h"
  8. #include "llvm/IR/Module.h"
  9. #include "toolchain/lowering/lowering_context.h"
  10. #include "toolchain/semantics/semantics_ir.h"
  11. #include "toolchain/semantics/semantics_node.h"
  12. namespace Carbon {
  13. // Context and shared functionality for lowering handlers that produce an
  14. // `llvm::Function` definition.
  15. class LoweringFunctionContext {
  16. public:
  17. explicit LoweringFunctionContext(LoweringContext& lowering_context,
  18. llvm::Function* function);
  19. // Returns a local (versus global) value for the given node.
  20. auto GetLocal(SemanticsNodeId node_id) -> llvm::Value* {
  21. auto it = locals_.find(node_id);
  22. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  23. return it->second;
  24. }
  25. // Returns a local (versus global) value for the given node in loaded state.
  26. // Loads will only be inserted on an as-needed basis.
  27. auto GetLocalLoaded(SemanticsNodeId node_id) -> llvm::Value*;
  28. // Sets the value for the given node.
  29. auto SetLocal(SemanticsNodeId node_id, llvm::Value* value) {
  30. bool added = locals_.insert({node_id, value}).second;
  31. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  32. }
  33. // Gets a callable's function.
  34. auto GetFunction(SemanticsFunctionId function_id) -> llvm::Function* {
  35. return lowering_context_->GetFunction(function_id);
  36. }
  37. // Returns a lowered type for the given type_id.
  38. auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
  39. return lowering_context_->GetType(type_id);
  40. }
  41. auto llvm_context() -> llvm::LLVMContext& {
  42. return lowering_context_->llvm_context();
  43. }
  44. auto llvm_module() -> llvm::Module& {
  45. return lowering_context_->llvm_module();
  46. }
  47. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  48. auto semantics_ir() -> const SemanticsIR& {
  49. return lowering_context_->semantics_ir();
  50. }
  51. private:
  52. // Context for the overall lowering process.
  53. LoweringContext* lowering_context_;
  54. // The IR function we're generating.
  55. llvm::Function* function_;
  56. llvm::IRBuilder<> builder_;
  57. // Maps a function's SemanticsIR nodes to lowered values.
  58. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  59. // every block.
  60. llvm::DenseMap<SemanticsNodeId, llvm::Value*> locals_;
  61. };
  62. // Declare handlers for each SemanticsIR node.
  63. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  64. auto LoweringHandle##Name(LoweringFunctionContext& context, \
  65. SemanticsNodeId node_id, SemanticsNode node) \
  66. ->void;
  67. #include "toolchain/semantics/semantics_node_kind.def"
  68. } // namespace Carbon
  69. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_