@@ -2046,9 +2046,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
2046
2046
}
2047
2047
2048
2048
static auto TryResolveTypedInst (ImportRefResolver& resolver,
2049
- SemIR::VtableDecl inst,
2050
- SemIR::ConstantId /* vtable_const_id*/ )
2051
- -> ResolveResult {
2049
+ SemIR::VtableDecl inst) -> ResolveResult {
2052
2050
const auto & import_vtable = resolver.import_vtables ().Get (inst.vtable_id );
2053
2051
auto class_const_id =
2054
2052
GetLocalConstantId (resolver, resolver.import_classes ()
@@ -2117,9 +2115,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
2117
2115
}
2118
2116
2119
2117
static auto TryResolveTypedInst (ImportRefResolver& resolver,
2120
- SemIR::VtablePtr inst,
2121
- SemIR::ConstantId /* vtable_const_id*/ )
2122
- -> ResolveResult {
2118
+ SemIR::VtablePtr inst) -> ResolveResult {
2123
2119
auto specific_data = GetLocalSpecificData (resolver, inst.specific_id );
2124
2120
2125
2121
auto vtable_const_id = GetLocalConstantId (
@@ -2147,6 +2143,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
2147
2143
auto fn_val_id = GetLocalConstantInstId (
2148
2144
resolver,
2149
2145
resolver.import_functions ().Get (inst.function_id ).first_decl_id ());
2146
+
2150
2147
auto specific_data = GetLocalSpecificData (resolver, inst.specific_id );
2151
2148
if (resolver.HasNewWork ()) {
2152
2149
return ResolveResult::Retry ();
@@ -3058,40 +3055,84 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3058
3055
SemIR::InstId inst_id,
3059
3056
SemIR::ConstantId const_id)
3060
3057
-> ResolveResult {
3061
- if (SemIR::IsSingletonInstId (inst_id)) {
3062
- CARBON_CHECK (!const_id.has_value ());
3058
+ // These instruction types are imported across multiple phases to arrive at
3059
+ // their constant value. We can't just import their constant value instruction
3060
+ // directly.
3061
+ auto untyped_inst = resolver.import_insts ().GetWithAttachedType (inst_id);
3062
+ CARBON_KIND_SWITCH (untyped_inst) {
3063
+ case CARBON_KIND (SemIR::AssociatedConstantDecl inst): {
3064
+ return TryResolveTypedInst (resolver, inst, const_id);
3065
+ }
3066
+ case CARBON_KIND (SemIR::ClassDecl inst): {
3067
+ return TryResolveTypedInst (resolver, inst, const_id);
3068
+ }
3069
+ case CARBON_KIND (SemIR::FunctionDecl inst): {
3070
+ return TryResolveTypedInst (resolver, inst, const_id);
3071
+ }
3072
+ case CARBON_KIND (SemIR::ImplDecl inst): {
3073
+ return TryResolveTypedInst (resolver, inst, const_id);
3074
+ }
3075
+ case CARBON_KIND (SemIR::InterfaceDecl inst): {
3076
+ return TryResolveTypedInst (resolver, inst, const_id);
3077
+ }
3078
+ default :
3079
+ break ;
3080
+ }
3081
+
3082
+ // Other instructions are imported in a single phase (once their dependencies
3083
+ // are all imported).
3084
+ CARBON_CHECK (!const_id.has_value ());
3085
+
3086
+ auto inst_constant_id = resolver.import_constant_values ().Get (inst_id);
3087
+ if (!inst_constant_id.is_constant ()) {
3088
+ // TODO: Import of non-constant BindNames happens when importing `let`
3089
+ // declarations.
3090
+ CARBON_CHECK (resolver.import_insts ().Is <SemIR::BindName>(inst_id),
3091
+ " TryResolveInst on non-constant instruction {0}" , inst_id);
3092
+ return ResolveResult::Done (SemIR::ConstantId::NotConstant);
3093
+ }
3094
+
3095
+ // Import the canonical constant value instruction for `inst_id` directly. We
3096
+ // don't try to import the non-canonical `inst_id`.
3097
+ auto constant_inst_id =
3098
+ resolver.import_constant_values ().GetInstId (inst_constant_id);
3099
+ CARBON_DCHECK (resolver.import_constant_values ().GetConstantInstId (
3100
+ constant_inst_id) == constant_inst_id,
3101
+ " Constant value of constant instruction should refer to "
3102
+ " the same instruction" );
3103
+
3104
+ if (SemIR::IsSingletonInstId (constant_inst_id)) {
3063
3105
// Constants for builtins can be directly copied.
3064
- return ResolveResult::Done (resolver.local_constant_values ().Get (inst_id));
3106
+ return ResolveResult::Done (
3107
+ resolver.local_constant_values ().Get (constant_inst_id));
3065
3108
}
3066
3109
3067
- auto untyped_inst = resolver.import_insts ().GetWithAttachedType (inst_id);
3068
- CARBON_KIND_SWITCH (untyped_inst) {
3110
+ auto untyped_constant_inst =
3111
+ resolver.import_insts ().GetWithAttachedType (constant_inst_id);
3112
+ CARBON_KIND_SWITCH (untyped_constant_inst) {
3069
3113
case CARBON_KIND (SemIR::AdaptDecl inst): {
3070
- return TryResolveTypedInst (resolver, inst, inst_id );
3114
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3071
3115
}
3072
3116
case CARBON_KIND (SemIR::AddrPattern inst): {
3073
- return TryResolveTypedInst (resolver, inst, inst_id );
3117
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3074
3118
}
3075
3119
case CARBON_KIND (SemIR::ArrayType inst): {
3076
3120
return TryResolveTypedInst (resolver, inst);
3077
3121
}
3078
- case CARBON_KIND (SemIR::AssociatedConstantDecl inst): {
3079
- return TryResolveTypedInst (resolver, inst, const_id);
3080
- }
3081
3122
case CARBON_KIND (SemIR::AssociatedEntity inst): {
3082
3123
return TryResolveTypedInst (resolver, inst);
3083
3124
}
3084
3125
case CARBON_KIND (SemIR::AssociatedEntityType inst): {
3085
3126
return TryResolveTypedInst (resolver, inst);
3086
3127
}
3087
3128
case CARBON_KIND (SemIR::BaseDecl inst): {
3088
- return TryResolveTypedInst (resolver, inst, inst_id );
3129
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3089
3130
}
3090
3131
case CARBON_KIND (SemIR::BindAlias inst): {
3091
3132
return TryResolveTypedInst (resolver, inst);
3092
3133
}
3093
3134
case CARBON_KIND (SemIR::BindingPattern inst): {
3094
- return TryResolveTypedInst (resolver, inst, inst_id );
3135
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3095
3136
}
3096
3137
case CARBON_KIND (SemIR::BindSymbolicName inst): {
3097
3138
return TryResolveTypedInst (resolver, inst);
@@ -3108,9 +3149,6 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3108
3149
case CARBON_KIND (SemIR::CharLiteralValue inst): {
3109
3150
return TryResolveTypedInst (resolver, inst);
3110
3151
}
3111
- case CARBON_KIND (SemIR::ClassDecl inst): {
3112
- return TryResolveTypedInst (resolver, inst, const_id);
3113
- }
3114
3152
case CARBON_KIND (SemIR::ClassType inst): {
3115
3153
return TryResolveTypedInst (resolver, inst);
3116
3154
}
@@ -3133,7 +3171,7 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3133
3171
return TryResolveTypedInst (resolver, inst);
3134
3172
}
3135
3173
case CARBON_KIND (SemIR::FieldDecl inst): {
3136
- return TryResolveTypedInst (resolver, inst, inst_id );
3174
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3137
3175
}
3138
3176
case CARBON_KIND (SemIR::FloatLiteralValue inst): {
3139
3177
return TryResolveTypedInst (resolver, inst);
@@ -3144,9 +3182,6 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3144
3182
case CARBON_KIND (SemIR::FloatValue inst): {
3145
3183
return TryResolveTypedInst (resolver, inst);
3146
3184
}
3147
- case CARBON_KIND (SemIR::FunctionDecl inst): {
3148
- return TryResolveTypedInst (resolver, inst, const_id);
3149
- }
3150
3185
case CARBON_KIND (SemIR::FunctionType inst): {
3151
3186
return TryResolveTypedInst (resolver, inst);
3152
3187
}
@@ -3159,9 +3194,6 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3159
3194
case CARBON_KIND (SemIR::GenericInterfaceType inst): {
3160
3195
return TryResolveTypedInst (resolver, inst);
3161
3196
}
3162
- case CARBON_KIND (SemIR::ImplDecl inst): {
3163
- return TryResolveTypedInst (resolver, inst, const_id);
3164
- }
3165
3197
case CARBON_KIND (SemIR::LookupImplWitness inst): {
3166
3198
return TryResolveTypedInst (resolver, inst);
3167
3199
}
@@ -3172,13 +3204,10 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3172
3204
return TryResolveTypedInst (resolver, inst);
3173
3205
}
3174
3206
case CARBON_KIND (SemIR::ImplWitnessTable inst): {
3175
- return TryResolveTypedInst (resolver, inst, inst_id );
3207
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3176
3208
}
3177
3209
case CARBON_KIND (SemIR::ImportRefLoaded inst): {
3178
- return TryResolveTypedInst (resolver, inst, inst_id);
3179
- }
3180
- case CARBON_KIND (SemIR::InterfaceDecl inst): {
3181
- return TryResolveTypedInst (resolver, inst, const_id);
3210
+ return TryResolveTypedInst (resolver, inst, constant_inst_id);
3182
3211
}
3183
3212
case CARBON_KIND (SemIR::IntValue inst): {
3184
3213
return TryResolveTypedInst (resolver, inst);
@@ -3190,10 +3219,10 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3190
3219
return TryResolveTypedInst (resolver, inst);
3191
3220
}
3192
3221
case CARBON_KIND (SemIR::Namespace inst): {
3193
- return TryResolveTypedInst (resolver, inst, inst_id );
3222
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3194
3223
}
3195
3224
case CARBON_KIND (SemIR::OutParamPattern inst): {
3196
- return TryResolveTypedInst (resolver, inst, inst_id );
3225
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3197
3226
}
3198
3227
case CARBON_KIND (SemIR::PartialType inst): {
3199
3228
return TryResolveTypedInst (resolver, inst);
@@ -3205,13 +3234,13 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3205
3234
return TryResolveTypedInst (resolver, inst);
3206
3235
}
3207
3236
case CARBON_KIND (SemIR::RefParamPattern inst): {
3208
- return TryResolveTypedInst (resolver, inst, inst_id );
3237
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3209
3238
}
3210
3239
case CARBON_KIND (SemIR::RequireCompleteType inst): {
3211
3240
return TryResolveTypedInst (resolver, inst);
3212
3241
}
3213
3242
case CARBON_KIND (SemIR::ReturnSlotPattern inst): {
3214
- return TryResolveTypedInst (resolver, inst, inst_id );
3243
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3215
3244
}
3216
3245
case CARBON_KIND (SemIR::SpecificFunction inst): {
3217
3246
return TryResolveTypedInst (resolver, inst);
@@ -3229,13 +3258,13 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3229
3258
return TryResolveTypedInst (resolver, inst);
3230
3259
}
3231
3260
case CARBON_KIND (SemIR::SymbolicBindingPattern inst): {
3232
- return TryResolveTypedInst (resolver, inst, inst_id );
3261
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3233
3262
}
3234
3263
case CARBON_KIND (SemIR::TupleAccess inst): {
3235
3264
return TryResolveTypedInst (resolver, inst);
3236
3265
}
3237
3266
case CARBON_KIND (SemIR::TuplePattern inst): {
3238
- return TryResolveTypedInst (resolver, inst, inst_id );
3267
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3239
3268
}
3240
3269
case CARBON_KIND (SemIR::TupleType inst): {
3241
3270
return TryResolveTypedInst (resolver, inst);
@@ -3247,51 +3276,28 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
3247
3276
return TryResolveTypedInst (resolver, inst);
3248
3277
}
3249
3278
case CARBON_KIND (SemIR::ValueParamPattern inst): {
3250
- return TryResolveTypedInst (resolver, inst, inst_id );
3279
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3251
3280
}
3252
3281
case CARBON_KIND (SemIR::VarPattern inst): {
3253
- return TryResolveTypedInst (resolver, inst, inst_id );
3282
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3254
3283
}
3255
3284
case CARBON_KIND (SemIR::VarStorage inst): {
3256
- return TryResolveTypedInst (resolver, inst, inst_id );
3285
+ return TryResolveTypedInst (resolver, inst, constant_inst_id );
3257
3286
}
3258
3287
case CARBON_KIND (SemIR::VtableDecl inst): {
3259
- return TryResolveTypedInst (resolver, inst, const_id );
3288
+ return TryResolveTypedInst (resolver, inst);
3260
3289
}
3261
3290
case CARBON_KIND (SemIR::VtablePtr inst): {
3262
- return TryResolveTypedInst (resolver, inst, const_id);
3263
- }
3264
- default : {
3265
- auto inst_constant_id = resolver.import_constant_values ().Get (inst_id);
3266
- if (!inst_constant_id.is_constant ()) {
3267
- // TODO: Import of non-constant BindNames happens when importing `let`
3268
- // declarations.
3269
- CARBON_CHECK (untyped_inst.Is <SemIR::BindName>(),
3270
- " TryResolveInst on non-constant instruction {0}" ,
3271
- untyped_inst);
3272
- return ResolveResult::Done (SemIR::ConstantId::NotConstant);
3273
- }
3274
-
3275
- // This instruction might have a constant value of a different kind.
3276
- auto constant_inst_id =
3277
- resolver.import_constant_values ().GetInstId (inst_constant_id);
3278
- if (constant_inst_id == inst_id) {
3279
- // Produce a diagnostic to provide a source location with the CHECK
3280
- // failure.
3281
- resolver.local_context ().TODO (
3282
- SemIR::LocId (AddImportIRInst (resolver, inst_id)),
3283
- llvm::formatv (" TryResolveInst on {0}" , untyped_inst.kind ()).str ());
3284
- CARBON_FATAL (" TryResolveInst on unsupported instruction kind {0}" ,
3285
- untyped_inst.kind ());
3286
- }
3287
- // Try to resolve the constant value instead. Note that this can only
3288
- // retry once.
3289
- CARBON_DCHECK (resolver.import_constant_values ().GetConstantInstId (
3290
- constant_inst_id) == constant_inst_id,
3291
- " Constant value of constant instruction should refer to "
3292
- " the same instruction" );
3293
- return TryResolveInstCanonical (resolver, constant_inst_id, const_id);
3291
+ return TryResolveTypedInst (resolver, inst);
3294
3292
}
3293
+ default :
3294
+ // Found a canonical instruction which needs to be resolved, but which is
3295
+ // not yet handled.
3296
+ //
3297
+ // TODO: Could we turn this into a compile-time error?
3298
+ CARBON_FATAL (
3299
+ " Missing case in TryResolveInstCanonical for instruction kind {0}" ,
3300
+ untyped_constant_inst.kind ());
3295
3301
}
3296
3302
}
3297
3303
0 commit comments