|
2 | 2 | // Exceptions. See /LICENSE for license information. |
3 | 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
4 | 4 |
|
| 5 | +#include "toolchain/base/kind_switch.h" |
5 | 6 | #include "toolchain/check/context.h" |
| 7 | +#include "toolchain/check/convert.h" |
6 | 8 | #include "toolchain/check/handle.h" |
| 9 | +#include "toolchain/check/modifiers.h" |
| 10 | +#include "toolchain/check/name_lookup.h" |
| 11 | +#include "toolchain/check/subst.h" |
| 12 | +#include "toolchain/check/type_completion.h" |
7 | 13 | #include "toolchain/parse/node_ids.h" |
| 14 | +#include "toolchain/sem_ir/named_constraint.h" |
| 15 | +#include "toolchain/sem_ir/type_iterator.h" |
| 16 | +#include "toolchain/sem_ir/typed_insts.h" |
8 | 17 |
|
9 | 18 | namespace Carbon::Check { |
10 | 19 |
|
11 | 20 | auto HandleParseNode(Context& context, Parse::RequireIntroducerId node_id) |
12 | 21 | -> bool { |
13 | | - return context.TODO(node_id, "require"); |
| 22 | + // Create an instruction block to hold the instructions created for the type |
| 23 | + // and constraint. |
| 24 | + context.inst_block_stack().Push(); |
| 25 | + |
| 26 | + // Optional modifiers follow. |
| 27 | + context.decl_introducer_state_stack().Push<Lex::TokenKind::Require>(); |
| 28 | + |
| 29 | + auto scope_id = context.scope_stack().PeekNameScopeId(); |
| 30 | + auto scope_inst_id = context.name_scopes().Get(scope_id).inst_id(); |
| 31 | + auto scope_inst = context.insts().Get(scope_inst_id); |
| 32 | + if (!scope_inst.Is<SemIR::InterfaceDecl>() && |
| 33 | + !scope_inst.Is<SemIR::NamedConstraintDecl>()) { |
| 34 | + CARBON_DIAGNOSTIC( |
| 35 | + RequireInWrongScope, Error, |
| 36 | + "`require` can only be used in an `interface` or `constraint`"); |
| 37 | + context.emitter().Emit(node_id, RequireInWrongScope); |
| 38 | + scope_inst_id = SemIR::ErrorInst::InstId; |
| 39 | + } |
| 40 | + |
| 41 | + context.node_stack().Push(node_id, scope_inst_id); |
| 42 | + return true; |
14 | 43 | } |
15 | 44 |
|
16 | 45 | auto HandleParseNode(Context& context, Parse::RequireDefaultSelfImplsId node_id) |
17 | 46 | -> bool { |
18 | | - return context.TODO(node_id, "require"); |
| 47 | + auto scope_inst_id = |
| 48 | + context.node_stack().Peek<Parse::NodeKind::RequireIntroducer>(); |
| 49 | + if (scope_inst_id == SemIR::ErrorInst::InstId) { |
| 50 | + context.node_stack().Push(node_id, SemIR::ErrorInst::TypeInstId); |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + auto scope_id = context.scope_stack().PeekNameScopeId(); |
| 55 | + auto lookup_result = |
| 56 | + LookupNameInExactScope(context, node_id, SemIR::NameId::SelfType, |
| 57 | + scope_id, context.name_scopes().Get(scope_id), |
| 58 | + /*is_being_declared=*/false); |
| 59 | + CARBON_CHECK(lookup_result.is_found()); |
| 60 | + |
| 61 | + auto self_inst_id = lookup_result.target_inst_id(); |
| 62 | + auto self_type_id = context.insts().Get(self_inst_id).type_id(); |
| 63 | + CARBON_CHECK(context.types().Is<SemIR::FacetType>(self_type_id)); |
| 64 | + |
| 65 | + auto self_facet_as_type = AddTypeInst<SemIR::FacetAccessType>( |
| 66 | + context, node_id, |
| 67 | + {.type_id = SemIR::TypeType::TypeId, |
| 68 | + .facet_value_inst_id = self_inst_id}); |
| 69 | + context.node_stack().Push(node_id, self_facet_as_type); |
| 70 | + return true; |
19 | 71 | } |
20 | 72 |
|
21 | 73 | auto HandleParseNode(Context& context, Parse::RequireTypeImplsId node_id) |
22 | 74 | -> bool { |
23 | | - return context.TODO(node_id, "require"); |
| 75 | + auto [self_node_id, self_inst_id] = context.node_stack().PopExprWithNodeId(); |
| 76 | + auto self_type = ExprAsType(context, self_node_id, self_inst_id); |
| 77 | + context.node_stack().Push(node_id, self_type.inst_id); |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +static auto TypeStructureReferencesSelf( |
| 82 | + Context& context, SemIR::TypeInstId inst_id, |
| 83 | + const SemIR::IdentifiedFacetType& identified_facet_type) -> bool { |
| 84 | + if (inst_id == SemIR::ErrorInst::TypeInstId) { |
| 85 | + // Don't generate more diagnostics. |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + auto find_self = [&](SemIR::TypeIterator& type_iter) -> bool { |
| 90 | + while (true) { |
| 91 | + auto step = type_iter.Next(); |
| 92 | + if (step.Is<SemIR::TypeIterator::Step::Done>()) { |
| 93 | + break; |
| 94 | + } |
| 95 | + CARBON_KIND_SWITCH(step.any) { |
| 96 | + case CARBON_KIND(SemIR::TypeIterator::Step::Error _): { |
| 97 | + // Don't generate more diagnostics. |
| 98 | + return true; |
| 99 | + } |
| 100 | + case CARBON_KIND(SemIR::TypeIterator::Step::SymbolicBinding bind): { |
| 101 | + if (context.entity_names().Get(bind.entity_name_id).name_id == |
| 102 | + SemIR::NameId::SelfType) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + break; |
| 106 | + } |
| 107 | + default: |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | + }; |
| 113 | + |
| 114 | + { |
| 115 | + SemIR::TypeIterator type_iter(&context.sem_ir()); |
| 116 | + type_iter.Add(context.constant_values().GetConstantTypeInstId(inst_id)); |
| 117 | + if (find_self(type_iter)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (identified_facet_type.required_interfaces().empty()) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + for (auto specific_interface : identified_facet_type.required_interfaces()) { |
| 127 | + SemIR::TypeIterator type_iter(&context.sem_ir()); |
| 128 | + type_iter.Add(specific_interface); |
| 129 | + if (!find_self(type_iter)) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return true; |
24 | 135 | } |
25 | 136 |
|
26 | 137 | auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { |
27 | | - return context.TODO(node_id, "require"); |
| 138 | + auto [constraint_node_id, constraint_inst_id] = |
| 139 | + context.node_stack().PopExprWithNodeId(); |
| 140 | + auto [self_node_id, self_inst_id] = |
| 141 | + context.node_stack().PopWithNodeId<Parse::NodeCategory::RequireImpls>(); |
| 142 | + |
| 143 | + [[maybe_unused]] auto decl_block_id = context.inst_block_stack().Pop(); |
| 144 | + |
| 145 | + // Process modifiers. |
| 146 | + auto introducer = |
| 147 | + context.decl_introducer_state_stack().Pop<Lex::TokenKind::Require>(); |
| 148 | + LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend); |
| 149 | + |
| 150 | + auto scope_inst_id = |
| 151 | + context.node_stack().Pop<Parse::NodeKind::RequireIntroducer>(); |
| 152 | + |
| 153 | + auto constraint_constant_value_inst_id = |
| 154 | + context.constant_values().GetConstantInstId(constraint_inst_id); |
| 155 | + auto constraint_facet_type = context.insts().TryGetAs<SemIR::FacetType>( |
| 156 | + constraint_constant_value_inst_id); |
| 157 | + if (!constraint_facet_type) { |
| 158 | + if (constraint_constant_value_inst_id != SemIR::ErrorInst::InstId) { |
| 159 | + CARBON_DIAGNOSTIC( |
| 160 | + RequireImplsMissingFacetType, Error, |
| 161 | + "`require` declaration constrained by a non-facet type; " |
| 162 | + "expected an `interface` or `constraint` name after `impls`"); |
| 163 | + context.emitter().Emit(constraint_node_id, RequireImplsMissingFacetType); |
| 164 | + } |
| 165 | + // Can't continue without a constraint to use. |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + auto identified_facet_type_id = |
| 170 | + RequireIdentifiedFacetType(context, *constraint_facet_type); |
| 171 | + const auto& identified = |
| 172 | + context.identified_facet_types().Get(identified_facet_type_id); |
| 173 | + |
| 174 | + if (!TypeStructureReferencesSelf(context, self_inst_id, identified)) { |
| 175 | + CARBON_DIAGNOSTIC(RequireImplsMissingSelf, Error, |
| 176 | + "no `Self` reference found in `require` declaration; " |
| 177 | + "`Self` must appear in the self-type or as a generic " |
| 178 | + "parameter for each `interface` or `constraint`"); |
| 179 | + context.emitter().Emit(node_id, RequireImplsMissingSelf); |
| 180 | + return true; |
| 181 | + } |
| 182 | + |
| 183 | + if (scope_inst_id == SemIR::ErrorInst::InstId) { |
| 184 | + // `require` is in the wrong scope. |
| 185 | + return true; |
| 186 | + } |
| 187 | + |
| 188 | + if (identified.required_interfaces().empty()) { |
| 189 | + // A `require T impls type` adds no actual constraints. |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + // TODO: Add the `require` constraint to the InterfaceDecl or ConstraintDecl |
| 194 | + // from `scope_inst_id`. |
| 195 | + |
| 196 | + return true; |
28 | 197 | } |
29 | 198 |
|
30 | 199 | } // namespace Carbon::Check |
0 commit comments