pointer_dereference.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #include "toolchain/check/pointer_dereference.h"
  5. #include "llvm/ADT/STLFunctionalExtras.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/parse/node_ids.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. auto PerformPointerDereference(
  12. Context& context, Parse::AnyPointerDeferenceExprId node_id,
  13. SemIR::InstId base_id,
  14. llvm::function_ref<auto(SemIR::TypeId not_pointer_type_id)->void>
  15. diagnose_not_pointer) -> SemIR::InstId {
  16. // TODO: Once we have a finalized design for a pointer interface, use
  17. //
  18. // HandleUnaryOperator(context, node_id, {"Pointer", "Dereference"});
  19. //
  20. // to convert to a pointer value.
  21. base_id = ConvertToValueExpr(context, base_id);
  22. auto type_id = context.types().GetUnqualifiedType(
  23. context.insts().Get(base_id).type_id());
  24. auto result_type_id = SemIR::ErrorInst::SingletonTypeId;
  25. if (auto pointer_type =
  26. context.types().TryGetAs<SemIR::PointerType>(type_id)) {
  27. result_type_id = pointer_type->pointee_id;
  28. } else if (type_id != SemIR::ErrorInst::SingletonTypeId) {
  29. diagnose_not_pointer(type_id);
  30. }
  31. return context.AddInst<SemIR::Deref>(
  32. node_id, {.type_id = result_type_id, .pointer_id = base_id});
  33. }
  34. } // namespace Carbon::Check