Remove SelfParam, add an AddrPattern instead. (#3506)

This is intended to make the representation of a `self` pattern be more
similar to other patterns.
This commit is contained in:
Richard Smith
2023-12-14 20:53:51 +00:00
committed by GitHub
parent de0c02ddae
commit fbb4ecf319
22 changed files with 152 additions and 101 deletions
+21 -9
View File
@@ -73,6 +73,21 @@ auto FileContext::GetGlobal(SemIR::InstId inst_id) -> llvm::Value* {
CARBON_FATAL() << "Missing value: " << inst_id << " " << target;
}
// Returns the parameter for the given instruction from a function parameter
// list.
static auto GetAsParam(const SemIR::File& sem_ir, SemIR::InstId pattern_id)
-> std::pair<SemIR::InstId, SemIR::Param> {
auto pattern = sem_ir.insts().Get(pattern_id);
// Step over `addr`.
if (auto addr = pattern.TryAs<SemIR::AddrPattern>()) {
pattern_id = addr->inner_id;
pattern = sem_ir.insts().Get(pattern_id);
}
return {pattern_id, pattern.As<SemIR::Param>()};
}
auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id)
-> llvm::Function* {
const auto& function = sem_ir().functions().Get(function_id);
@@ -103,7 +118,7 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id)
}
for (auto param_ref_id :
llvm::concat<const SemIR::InstId>(implicit_param_refs, param_refs)) {
auto param_type_id = sem_ir().insts().Get(param_ref_id).type_id();
auto param_type_id = GetAsParam(sem_ir(), param_ref_id).second.type_id;
switch (auto value_rep = SemIR::GetValueRepr(sem_ir(), param_type_id);
value_rep.kind) {
case SemIR::ValueRepr::Unknown:
@@ -148,16 +163,13 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id)
// Set up parameters and the return slot.
for (auto [inst_id, arg] :
llvm::zip_equal(param_inst_ids, llvm_function->args())) {
auto inst = sem_ir().insts().Get(inst_id);
auto name_id = SemIR::NameId::Invalid;
if (inst_id == function.return_slot_id) {
name_id = SemIR::NameId::ReturnSlot;
arg.addAttr(llvm::Attribute::getWithStructRetType(
llvm_context(), GetType(function.return_type_id)));
} else if (inst.Is<SemIR::SelfParam>()) {
name_id = SemIR::NameId::SelfValue;
} else {
name_id = inst.As<SemIR::Param>().name_id;
name_id = GetAsParam(sem_ir(), inst_id).second.name_id;
}
arg.setName(sem_ir().names().GetIRBaseName(name_id));
}
@@ -194,14 +206,14 @@ auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id)
}
for (auto param_ref_id :
llvm::concat<const SemIR::InstId>(implicit_param_refs, param_refs)) {
auto param_type_id = sem_ir().insts().Get(param_ref_id).type_id();
auto [param_id, param] = GetAsParam(sem_ir(), param_ref_id);
auto param_type_id = param.type_id;
if (SemIR::GetValueRepr(sem_ir(), param_type_id).kind ==
SemIR::ValueRepr::None) {
function_lowering.SetLocal(
param_ref_id, llvm::PoisonValue::get(GetType(param_type_id)));
param_id, llvm::PoisonValue::get(GetType(param_type_id)));
} else {
function_lowering.SetLocal(param_ref_id,
llvm_function->getArg(param_index));
function_lowering.SetLocal(param_id, llvm_function->getArg(param_index));
++param_index;
}
}