llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[CIR] Support pointer-to-data-member binary operators for aggregates

Open amanmaurya92 opened this issue 2 months ago • 0 comments

Overview

Currently in ClangIR codegen, evaluating a pointer-to-data-member binary expression (.* or ->*) where the result is an aggregate type triggers cgm.errorNYI("AggExprEmitter: VisitPointerToDataMemberBinaryOperator") and VisitBinaryOperator in clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp.

In classic Clang CodeGen (clang/lib/CodeGen/CGExprAgg.cpp), this is handled by:

void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
  if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
    VisitPointerToDataMemberBinaryOperator(E);
  else
    CGF.ErrorUnsupported(E, "aggregate binary expression");
}

void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(const BinaryOperator *E) {
  LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
  EmitFinalDestCopy(E->getType(), LV);
}

CIRGenFunction::emitPointerToDataMemberBinaryExpr is already implemented in clang/lib/CIR/CodeGen/CIRGenExpr.cpp. We can hook it up in CIRGenExprAggregate.cpp to resolve the lvalue and emit the final destination copy, matching classic Clang codegen, along with test coverage under clang/test/CIR/CodeGen/.

Minimal Reproducer

struct Inner {
  int x;
  int y;
};

struct Outer {
  Inner inner;
};

Inner test_ptr_to_member(Outer outer, Inner Outer::*p) {
  return outer.*p;
}

Inner test_ptr_to_member_arrow(Outer *outer, Inner Outer::*p) {
  return outer->*p;
}

amanmaurya92 avatar Sep 07 '26 16:09 amanmaurya92