浏览代码

Use `CARBON_KIND_SWITCH` for inst dispatch in `FunctionContext` and `ConstantContext`. (#3919)

As requested in #3916.
Richard Smith 2 年之前
父节点
当前提交
0ff798ae90
共有 2 个文件被更改,包括 10 次插入8 次删除
  1. 5 4
      toolchain/lower/constant.cpp
  2. 5 4
      toolchain/lower/function_context.cpp

+ 5 - 4
toolchain/lower/constant.cpp

@@ -6,6 +6,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Value.h"
+#include "toolchain/base/kind_switch.h"
 #include "toolchain/lower/file_context.h"
 #include "toolchain/sem_ir/inst.h"
 
@@ -250,12 +251,12 @@ auto LowerConstants(FileContext& file_context,
 
     auto inst = file_context.sem_ir().insts().Get(const_id.inst_id());
     llvm::Constant* value = nullptr;
-    switch (inst.kind()) {
+    CARBON_KIND_SWITCH(inst) {
 #define CARBON_SEM_IR_INST_KIND_CONSTANT_NEVER(...)
 #define CARBON_SEM_IR_INST_KIND_CONSTANT_SYMBOLIC_ONLY(...)
-#define CARBON_SEM_IR_INST_KIND(Name)                                \
-  case SemIR::Name::Kind:                                            \
-    value = Emit##Name##AsConstant(context, inst.As<SemIR::Name>()); \
+#define CARBON_SEM_IR_INST_KIND(Name)                    \
+  case CARBON_KIND(SemIR::Name const_inst):              \
+    value = Emit##Name##AsConstant(context, const_inst); \
     break;
 #include "toolchain/sem_ir/inst_kind.def"
 

+ 5 - 4
toolchain/lower/function_context.cpp

@@ -5,6 +5,7 @@
 #include "toolchain/lower/function_context.h"
 
 #include "common/vlog.h"
+#include "toolchain/base/kind_switch.h"
 #include "toolchain/sem_ir/file.h"
 
 namespace Carbon::Lower {
@@ -60,11 +61,11 @@ auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
   auto inst = sem_ir().insts().Get(inst_id);
   CARBON_VLOG() << "Lowering " << inst_id << ": " << inst << "\n";
   builder_.getInserter().SetCurrentInstId(inst_id);
-  switch (inst.kind()) {
+  CARBON_KIND_SWITCH(inst) {
 #define CARBON_SEM_IR_INST_KIND_CONSTANT_ALWAYS(Name)
-#define CARBON_SEM_IR_INST_KIND(Name)                     \
-  case SemIR::Name::Kind:                                 \
-    Handle##Name(*this, inst_id, inst.As<SemIR::Name>()); \
+#define CARBON_SEM_IR_INST_KIND(Name)         \
+  case CARBON_KIND(SemIR::Name typed_inst):   \
+    Handle##Name(*this, inst_id, typed_inst); \
     break;
 #include "toolchain/sem_ir/inst_kind.def"