Эх сурвалжийг харах

Drop "Lowered" from various LoweringContext APIs. (#2859)

As I've worked on this more, it's just felt verbosely redundant -- lowered should be the default assumption, needing no further explanation. This does mean there's `context.GetType()` and `context.semantics_ir().GetType()`, but I feel like that's still reasonably clear on reading.
Jon Ross-Perkins 2 жил өмнө
parent
commit
db5629024e

+ 5 - 6
toolchain/lowering/lowering_context.cpp

@@ -19,15 +19,15 @@ LoweringContext::LoweringContext(llvm::LLVMContext& llvm_context,
       builder_(llvm_context),
       semantics_ir_(&semantics_ir),
       vlog_stream_(vlog_stream),
-      lowered_nodes_(semantics_ir_->nodes_size(), nullptr),
-      lowered_callables_(semantics_ir_->callables_size(), nullptr) {
+      nodes_(semantics_ir_->nodes_size(), nullptr),
+      callables_(semantics_ir_->callables_size(), nullptr) {
   CARBON_CHECK(!semantics_ir.has_errors())
       << "Generating LLVM IR from invalid SemanticsIR is unsupported.";
 
   auto types = semantics_ir_->types();
-  lowered_types_.resize_for_overwrite(types.size());
+  types_.resize_for_overwrite(types.size());
   for (int i = 0; i < static_cast<int>(types.size()); ++i) {
-    lowered_types_[i] = BuildLoweredNodeAsType(types[i]);
+    types_[i] = BuildType(types[i]);
   }
 }
 
@@ -60,8 +60,7 @@ auto LoweringContext::LowerBlock(SemanticsNodeBlockId block_id) -> void {
   }
 }
 
-auto LoweringContext::BuildLoweredNodeAsType(SemanticsNodeId node_id)
-    -> llvm::Type* {
+auto LoweringContext::BuildType(SemanticsNodeId node_id) -> llvm::Type* {
   switch (node_id.index) {
     case SemanticsBuiltinKind::EmptyTupleType.AsInt():
       // Represent empty types as empty structs.

+ 23 - 30
toolchain/lowering/lowering_context.h

@@ -25,42 +25,35 @@ class LoweringContext {
   // the main execution loop.
   auto Run() -> std::unique_ptr<llvm::Module>;
 
-  auto HasLoweredNode(SemanticsNodeId node_id) -> bool {
-    return lowered_nodes_[node_id.index];
-  }
-
-  // Returns a lowered type for the given type_id.
-  auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
-    // Neither TypeType nor InvalidType should be passed in.
-    CARBON_CHECK(type_id.index >= 0) << type_id;
-    return lowered_types_[type_id.index];
-  }
-
   // Returns a value for the given node.
-  auto GetLoweredNodeAsValue(SemanticsNodeId node_id) -> llvm::Value* {
-    CARBON_CHECK(lowered_nodes_[node_id.index]) << node_id;
-    return lowered_nodes_[node_id.index];
+  auto GetNode(SemanticsNodeId node_id) -> llvm::Value* {
+    CARBON_CHECK(nodes_[node_id.index]) << node_id;
+    return nodes_[node_id.index];
   }
 
   // Sets the value for the given node.
-  auto SetLoweredNodeAsValue(SemanticsNodeId node_id, llvm::Value* value) {
-    CARBON_CHECK(!lowered_nodes_[node_id.index]) << node_id;
-    lowered_nodes_[node_id.index] = value;
+  auto SetNode(SemanticsNodeId node_id, llvm::Value* value) {
+    CARBON_CHECK(!nodes_[node_id.index]) << node_id;
+    nodes_[node_id.index] = value;
   }
 
   // Gets a callable's function.
-  auto GetLoweredCallable(SemanticsCallableId callable_id) -> llvm::Function* {
-    CARBON_CHECK(lowered_callables_[callable_id.index] != nullptr)
-        << callable_id;
-    return lowered_callables_[callable_id.index];
+  auto GetCallable(SemanticsCallableId callable_id) -> llvm::Function* {
+    CARBON_CHECK(callables_[callable_id.index] != nullptr) << callable_id;
+    return callables_[callable_id.index];
   }
 
   // Sets a callable's function.
-  auto SetLoweredCallable(SemanticsCallableId callable_id,
-                          llvm::Function* function) {
-    CARBON_CHECK(lowered_callables_[callable_id.index] == nullptr)
-        << callable_id;
-    lowered_callables_[callable_id.index] = function;
+  auto SetCallable(SemanticsCallableId callable_id, llvm::Function* function) {
+    CARBON_CHECK(callables_[callable_id.index] == nullptr) << callable_id;
+    callables_[callable_id.index] = function;
+  }
+
+  // Returns a lowered type for the given type_id.
+  auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
+    // Neither TypeType nor InvalidType should be passed in.
+    CARBON_CHECK(type_id.index >= 0) << type_id;
+    return types_[type_id.index];
   }
 
   auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
@@ -78,7 +71,7 @@ class LoweringContext {
 
   // Builds the type for the given node, which should then be cached by the
   // caller.
-  auto BuildLoweredNodeAsType(SemanticsNodeId node_id) -> llvm::Type*;
+  auto BuildType(SemanticsNodeId node_id) -> llvm::Type*;
 
   // State for building the LLVM IR.
   llvm::LLVMContext* llvm_context_;
@@ -100,14 +93,14 @@ class LoweringContext {
   // execution because while expressions will have entries, statements won't.
   // TODO: This is transitioning to only track global and local values, rather
   // than one large map for all nodes.
-  llvm::SmallVector<llvm::Value*> lowered_nodes_;
+  llvm::SmallVector<llvm::Value*> nodes_;
 
   // Maps callables to lowered functions. Semantics treats callables as the
   // canonical form of a function, so lowering needs to do the same.
-  llvm::SmallVector<llvm::Function*> lowered_callables_;
+  llvm::SmallVector<llvm::Function*> callables_;
 
   // Provides lowered versions of types.
-  llvm::SmallVector<llvm::Type*> lowered_types_;
+  llvm::SmallVector<llvm::Type*> types_;
 };
 
 // Declare handlers for each SemanticsIR node.

+ 16 - 19
toolchain/lowering/lowering_handle.cpp

@@ -21,8 +21,8 @@ auto LoweringHandleCrossReference(LoweringContext& /*context*/,
 auto LoweringHandleAssign(LoweringContext& context, SemanticsNodeId /*node_id*/,
                           SemanticsNode node) -> void {
   auto [storage_id, value_id] = node.GetAsAssign();
-  context.builder().CreateStore(context.GetLoweredNodeAsValue(value_id),
-                                context.GetLoweredNodeAsValue(storage_id));
+  context.builder().CreateStore(context.GetNode(value_id),
+                                context.GetNode(storage_id));
 }
 
 auto LoweringHandleBinaryOperatorAdd(LoweringContext& /*context*/,
@@ -46,14 +46,14 @@ auto LoweringHandleBuiltin(LoweringContext& /*context*/,
 auto LoweringHandleCall(LoweringContext& context, SemanticsNodeId node_id,
                         SemanticsNode node) -> void {
   auto [refs_id, callable_id] = node.GetAsCall();
-  auto* function = context.GetLoweredCallable(callable_id);
+  auto* function = context.GetCallable(callable_id);
   std::vector<llvm::Value*> args;
   for (auto ref_id : context.semantics_ir().GetNodeBlock(refs_id)) {
-    args.push_back(context.GetLoweredNodeAsValue(ref_id));
+    args.push_back(context.GetNode(ref_id));
   }
   auto* value =
       context.builder().CreateCall(function, args, function->getName());
-  context.SetLoweredNodeAsValue(node_id, value);
+  context.SetNode(node_id, value);
 }
 
 auto LoweringHandleCodeBlock(LoweringContext& /*context*/,
@@ -86,7 +86,7 @@ auto LoweringHandleFunctionDeclaration(LoweringContext& context,
   auto* function = llvm::Function::Create(
       function_type, llvm::Function::ExternalLinkage,
       context.semantics_ir().GetString(name_id), context.llvm_module());
-  context.SetLoweredCallable(callable_id, function);
+  context.SetCallable(callable_id, function);
 
   // Set parameter names.
   for (int i = 0; i < static_cast<int>(param_refs.size()); ++i) {
@@ -121,7 +121,7 @@ auto LoweringHandleIntegerLiteral(LoweringContext& context,
   // TODO: This won't offer correct semantics, but seems close enough for now.
   llvm::Value* v =
       llvm::ConstantInt::get(context.builder().getInt32Ty(), i.getSExtValue());
-  context.SetLoweredNodeAsValue(node_id, v);
+  context.SetNode(node_id, v);
 }
 
 auto LoweringHandleRealLiteral(LoweringContext& context,
@@ -134,9 +134,8 @@ auto LoweringHandleRealLiteral(LoweringContext& context,
       real.mantissa.getSExtValue() *
       std::pow((real.is_decimal ? 10 : 2), real.exponent.getSExtValue());
   llvm::APFloat llvm_val(val);
-  context.SetLoweredNodeAsValue(
-      node_id,
-      llvm::ConstantFP::get(context.builder().getDoubleTy(), llvm_val));
+  context.SetNode(node_id, llvm::ConstantFP::get(
+                               context.builder().getDoubleTy(), llvm_val));
 }
 
 auto LoweringHandleReturn(LoweringContext& context, SemanticsNodeId /*node_id*/,
@@ -148,7 +147,7 @@ auto LoweringHandleReturnExpression(LoweringContext& context,
                                     SemanticsNodeId /*node_id*/,
                                     SemanticsNode node) -> void {
   SemanticsNodeId expr_id = node.GetAsReturnExpression();
-  context.builder().CreateRet(context.GetLoweredNodeAsValue(expr_id));
+  context.builder().CreateRet(context.GetNode(expr_id));
 }
 
 auto LoweringHandleStringLiteral(LoweringContext& /*context*/,
@@ -175,9 +174,8 @@ auto LoweringHandleStructMemberAccess(LoweringContext& context,
           .GetAsStructTypeField());
 
   auto* gep = context.builder().CreateStructGEP(
-      llvm_type, context.GetLoweredNodeAsValue(struct_id), member_index.index,
-      member_name);
-  context.SetLoweredNodeAsValue(node_id, gep);
+      llvm_type, context.GetNode(struct_id), member_index.index, member_name);
+  context.SetNode(node_id, gep);
 }
 
 auto LoweringHandleStructType(LoweringContext& /*context*/,
@@ -198,7 +196,7 @@ auto LoweringHandleStructValue(LoweringContext& context,
   auto* llvm_type = context.GetType(node.type_id());
   auto* alloca = context.builder().CreateAlloca(
       llvm_type, /*ArraySize=*/nullptr, "StructLiteralValue");
-  context.SetLoweredNodeAsValue(node_id, alloca);
+  context.SetNode(node_id, alloca);
 
   auto refs = context.semantics_ir().GetNodeBlock(node.GetAsStructValue());
   // Get type information for member names.
@@ -211,15 +209,14 @@ auto LoweringHandleStructValue(LoweringContext& context,
         context.semantics_ir().GetNode(type_refs[i]).GetAsStructTypeField());
     auto* gep =
         context.builder().CreateStructGEP(llvm_type, alloca, i, member_name);
-    context.builder().CreateStore(context.GetLoweredNodeAsValue(refs[i]), gep);
+    context.builder().CreateStore(context.GetNode(refs[i]), gep);
   }
 }
 
 auto LoweringHandleStubReference(LoweringContext& context,
                                  SemanticsNodeId node_id, SemanticsNode node)
     -> void {
-  context.SetLoweredNodeAsValue(
-      node_id, context.GetLoweredNodeAsValue(node.GetAsStubReference()));
+  context.SetNode(node_id, context.GetNode(node.GetAsStubReference()));
 }
 
 auto LoweringHandleVarStorage(LoweringContext& context, SemanticsNodeId node_id,
@@ -232,7 +229,7 @@ auto LoweringHandleVarStorage(LoweringContext& context, SemanticsNodeId node_id,
   // storage?
   auto* alloca = context.builder().CreateAlloca(context.GetType(node.type_id()),
                                                 /*ArraySize=*/nullptr, "var");
-  context.SetLoweredNodeAsValue(node_id, alloca);
+  context.SetNode(node_id, alloca);
 }
 
 }  // namespace Carbon