pointer_dereference.cpp 1.3 KB

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