|
| 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 | + |
| 5 | +#include "toolchain/check/implicit_type_impls.h" |
| 6 | + |
| 7 | +#include "toolchain/check/convert.h" |
| 8 | +#include "toolchain/check/generic.h" |
| 9 | +#include "toolchain/check/impl.h" |
| 10 | +#include "toolchain/check/inst.h" |
| 11 | +#include "toolchain/check/name_lookup.h" |
| 12 | +#include "toolchain/check/name_ref.h" |
| 13 | +#include "toolchain/check/pattern.h" |
| 14 | +#include "toolchain/check/pattern_match.h" |
| 15 | +#include "toolchain/check/type.h" |
| 16 | +#include "toolchain/sem_ir/ids.h" |
| 17 | + |
| 18 | +namespace Carbon::Check { |
| 19 | + |
| 20 | +// Produces an `impl <self_type_id> as <interface_id>` declaration. The caller |
| 21 | +// should inspect the resulting `impl` to ensure it's incomplete before |
| 22 | +// proceeding to define it. |
| 23 | +static auto TryDeclareImpl(Context& context, SemIR::LocId loc_id, |
| 24 | + SemIR::NameScopeId parent_scope_id, |
| 25 | + SemIR::TypeId self_type_id, |
| 26 | + SemIR::InstId interface_id) |
| 27 | + -> std::pair<SemIR::ImplId, SemIR::InstId> { |
| 28 | + auto impl_decl_id = AddPlaceholderInst( |
| 29 | + context, |
| 30 | + SemIR::LocIdAndInst::UncheckedLoc( |
| 31 | + loc_id, SemIR::ImplDecl{.impl_id = SemIR::ImplId::None, |
| 32 | + .decl_block_id = SemIR::InstBlockId::Empty})); |
| 33 | + |
| 34 | + auto self_id = context.types().GetInstId(self_type_id); |
| 35 | + auto constraint_id = ExprAsType(context, loc_id, interface_id).inst_id; |
| 36 | + |
| 37 | + SemIR::Impl impl = { |
| 38 | + { |
| 39 | + .name_id = SemIR::NameId::None, |
| 40 | + .parent_scope_id = parent_scope_id, |
| 41 | + .generic_id = SemIR::GenericId::None, |
| 42 | + .first_param_node_id = Parse::NodeId::None, |
| 43 | + .last_param_node_id = Parse::NodeId::None, |
| 44 | + .pattern_block_id = SemIR::InstBlockId::None, |
| 45 | + .implicit_param_patterns_id = SemIR::InstBlockId::None, |
| 46 | + .param_patterns_id = SemIR::InstBlockId::None, |
| 47 | + .is_extern = false, |
| 48 | + .extern_library_id = SemIR::LibraryNameId::None, |
| 49 | + .non_owning_decl_id = SemIR::InstId::None, |
| 50 | + .first_owning_decl_id = impl_decl_id, |
| 51 | + }, |
| 52 | + { |
| 53 | + .self_id = self_id, |
| 54 | + .constraint_id = constraint_id, |
| 55 | + .interface = |
| 56 | + CheckConstraintIsInterface(context, impl_decl_id, constraint_id), |
| 57 | + .is_final = true, |
| 58 | + }}; |
| 59 | + |
| 60 | + StartGenericDecl(context); |
| 61 | + return StartImplDecl(context, loc_id, |
| 62 | + /*implicit_params_loc_id=*/SemIR::LocId::None, impl, |
| 63 | + /*is_definition=*/true, /*extend_impl=*/std::nullopt); |
| 64 | +} |
| 65 | + |
| 66 | +// Constructs the implicit params for the `Op` function. Returns the block and |
| 67 | +// the `self` pattern. |
| 68 | +static auto MakeImplicitParams(Context& context, SemIR::LocId loc_id) |
| 69 | + -> std::pair<SemIR::InstBlockId, SemIR::InstId> { |
| 70 | + BeginSubpattern(context); |
| 71 | + |
| 72 | + auto result = LookupUnqualifiedName(context, loc_id, SemIR::NameId::SelfType); |
| 73 | + auto self_id = |
| 74 | + BuildNameRef(context, loc_id, SemIR::NameId::SelfType, |
| 75 | + result.scope_result.target_inst_id(), result.specific_id); |
| 76 | + auto self_type_expr = ExprAsType(context, loc_id, self_id); |
| 77 | + |
| 78 | + SemIR::ExprRegionId type_expr_region_id = |
| 79 | + EndSubpatternAsExpr(context, self_type_expr.inst_id); |
| 80 | + |
| 81 | + auto self_pattern_id = AddAddrSelfParamPattern( |
| 82 | + context, loc_id, type_expr_region_id, self_type_expr.inst_id); |
| 83 | + |
| 84 | + auto implicit_param_patterns_id = |
| 85 | + context.inst_blocks().Add({self_pattern_id}); |
| 86 | + return {implicit_param_patterns_id, self_pattern_id}; |
| 87 | +} |
| 88 | + |
| 89 | +// Defines the `Op` function for the `impl`. |
| 90 | +static auto DeclareImplOpFunction(Context& context, SemIR::LocId loc_id, |
| 91 | + const SemIR::Impl& impl) |
| 92 | + -> std::pair<SemIR::FunctionId, SemIR::InstId> { |
| 93 | + StartGenericDecl(context); |
| 94 | + |
| 95 | + auto name_id = SemIR::NameId::ForIdentifier(context.identifiers().Add("Op")); |
| 96 | + |
| 97 | + context.inst_block_stack().Push(); |
| 98 | + |
| 99 | + context.pattern_block_stack().Push(); |
| 100 | + auto [implicit_param_patterns_id, self_pattern_id] = |
| 101 | + MakeImplicitParams(context, loc_id); |
| 102 | + constexpr auto NoRegularParams = SemIR::InstBlockId::Empty; |
| 103 | + constexpr auto NoReturnSlot = SemIR::InstId::None; |
| 104 | + auto pattern_block_id = context.pattern_block_stack().Pop(); |
| 105 | + |
| 106 | + // Perform callee-side pattern matching to rebuild the parameter list. |
| 107 | + auto call_params_id = CalleePatternMatch(context, implicit_param_patterns_id, |
| 108 | + NoRegularParams, NoReturnSlot); |
| 109 | + auto decl_block_id = context.inst_block_stack().Pop(); |
| 110 | + |
| 111 | + // Create the `FunctionDecl` instruction. |
| 112 | + SemIR::FunctionDecl function_decl = {SemIR::TypeId::None, |
| 113 | + SemIR::FunctionId::None, decl_block_id}; |
| 114 | + auto decl_id = AddPlaceholderInst( |
| 115 | + context, SemIR::LocIdAndInst::UncheckedLoc(loc_id, function_decl)); |
| 116 | + auto generic_id = BuildGenericDecl(context, decl_id); |
| 117 | + |
| 118 | + // Create the `Function` object. |
| 119 | + function_decl.function_id = context.functions().Add(SemIR::Function{ |
| 120 | + { |
| 121 | + .name_id = name_id, |
| 122 | + .parent_scope_id = impl.scope_id, |
| 123 | + .generic_id = generic_id, |
| 124 | + .first_param_node_id = Parse::NodeId::None, |
| 125 | + .last_param_node_id = Parse::NodeId::None, |
| 126 | + .pattern_block_id = pattern_block_id, |
| 127 | + .implicit_param_patterns_id = implicit_param_patterns_id, |
| 128 | + .param_patterns_id = NoRegularParams, |
| 129 | + .is_extern = false, |
| 130 | + .extern_library_id = SemIR::LibraryNameId::None, |
| 131 | + .non_owning_decl_id = SemIR::InstId::None, |
| 132 | + .first_owning_decl_id = decl_id, |
| 133 | + }, |
| 134 | + { |
| 135 | + .call_params_id = call_params_id, |
| 136 | + .return_slot_pattern_id = NoReturnSlot, |
| 137 | + .virtual_modifier = SemIR::FunctionFields::VirtualModifier::None, |
| 138 | + .virtual_index = -1, |
| 139 | + .self_param_id = self_pattern_id, |
| 140 | + }}); |
| 141 | + function_decl.type_id = |
| 142 | + GetFunctionType(context, function_decl.function_id, |
| 143 | + context.scope_stack().PeekSpecificId()); |
| 144 | + ReplaceInstBeforeConstantUse(context, decl_id, function_decl); |
| 145 | + context.name_scopes().AddRequiredName(impl.scope_id, name_id, decl_id); |
| 146 | + |
| 147 | + return {function_decl.function_id, decl_id}; |
| 148 | +} |
| 149 | + |
| 150 | +auto MakeClassDestroyImpl(Context& context, SemIR::ClassId class_id) -> void { |
| 151 | + if (!context.gen_implicit_type_impls()) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + // Identify the type and interface for implementation. |
| 156 | + auto& class_info = context.classes().Get(class_id); |
| 157 | + auto loc_id = context.insts().GetLocIdForDesugaring( |
| 158 | + SemIR::LocId(class_info.latest_decl_id())); |
| 159 | + |
| 160 | + auto destroy_id = LookupNameInCore(context, loc_id, "Destroy"); |
| 161 | + if (destroy_id == SemIR::ErrorInst::InstId) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // Declare the `impl`. |
| 166 | + auto [impl_id, impl_decl_id] = |
| 167 | + TryDeclareImpl(context, loc_id, class_info.scope_id, |
| 168 | + class_info.self_type_id, destroy_id); |
| 169 | + auto& impl = context.impls().Get(impl_id); |
| 170 | + if (impl.is_complete()) { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + // Define the `impl`. |
| 175 | + impl.definition_id = impl_decl_id; |
| 176 | + impl.scope_id = context.name_scopes().Add(impl_decl_id, SemIR::NameId::None, |
| 177 | + class_info.scope_id); |
| 178 | + |
| 179 | + context.scope_stack().PushForEntity( |
| 180 | + impl_decl_id, impl.scope_id, |
| 181 | + context.generics().GetSelfSpecific(impl.generic_id)); |
| 182 | + StartGenericDefinition(context, impl.generic_id); |
| 183 | + context.inst_block_stack().Push(); |
| 184 | + |
| 185 | + // Declare the `Op` function. |
| 186 | + auto [fn_id, fn_decl_id] = DeclareImplOpFunction(context, loc_id, impl); |
| 187 | + |
| 188 | + // Define the `Op` function. |
| 189 | + // TODO: Add an actual definition. |
| 190 | + context.scope_stack().PushForFunctionBody(fn_decl_id); |
| 191 | + auto& function = context.functions().Get(fn_id); |
| 192 | + function.SetBuiltinFunction(SemIR::BuiltinFunctionKind::NoOp); |
| 193 | + StartGenericDefinition(context, function.generic_id); |
| 194 | + FinishGenericDefinition(context, function.generic_id); |
| 195 | + context.scope_stack().Pop(); |
| 196 | + |
| 197 | + // Close the `impl` definition. |
| 198 | + FinishImplWitness(context, impl_id); |
| 199 | + impl.defined = true; |
| 200 | + FinishGenericDefinition(context, impl.generic_id); |
| 201 | + context.scope_stack().Pop(); |
| 202 | + impl.body_block_id = context.inst_block_stack().Pop(); |
| 203 | +} |
| 204 | + |
| 205 | +} // namespace Carbon::Check |
0 commit comments