pointer_dereference.cpp 1.5 KB

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