Files
carbon-lang/toolchain/check/pointer_dereference.cpp
T
Richard SmithandJon Ross-Perkins d6ec885eb3 Track the type as written in BaseDecl and AdaptDecl. (#4564)
Represent the type as an `InstId` rather than as a `TypeId` to preserve
how it was written and better support tracking its value in a generic.
Add accessors to `Class` to get the base and adapted type to reduce code
duplication, and add `TypeStore::GetObjectRepr` to make it easier to map
from a type to its possibly-adapted object representation type. In
passing, also move `GetIntTypeInfo` and `GetUnqualifiedType` into
`TypeStore`.

This fixes specifics of generic adapters to properly look at the
specific adapted type, and also fixes importing of adapters.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2024-11-27 22:26:30 +00:00

40 lines
1.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/check/pointer_dereference.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "toolchain/check/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/parse/node_ids.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
auto PerformPointerDereference(
Context& context, Parse::AnyPointerDeferenceExprId node_id,
SemIR::InstId base_id,
llvm::function_ref<auto(SemIR::TypeId not_pointer_type_id)->void>
diagnose_not_pointer) -> SemIR::InstId {
// TODO: Once we have a finalized design for a pointer interface, use
//
// HandleUnaryOperator(context, node_id, {"Pointer", "Dereference"});
//
// to convert to a pointer value.
base_id = ConvertToValueExpr(context, base_id);
auto type_id = context.types().GetUnqualifiedType(
context.insts().Get(base_id).type_id());
auto result_type_id = SemIR::TypeId::Error;
if (auto pointer_type =
context.types().TryGetAs<SemIR::PointerType>(type_id)) {
result_type_id = pointer_type->pointee_id;
} else if (type_id != SemIR::TypeId::Error) {
diagnose_not_pointer(type_id);
}
return context.AddInst<SemIR::Deref>(
node_id, {.type_id = result_type_id, .pointer_id = base_id});
}
} // namespace Carbon::Check