Skip to content

Commit 5611268

Browse files
authored
[CIR] Handle default arguments in ctors (#168649)
This adds the scalar expression visitor needed to handle default arguments being passed to constructors.
1 parent 13e09eb commit 5611268

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
711711
return Visit(e->getSubExpr());
712712
}
713713

714+
mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
715+
CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
716+
return Visit(dae->getExpr());
717+
}
714718
mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
715719
CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
716720
return Visit(die->getExpr());

clang/test/CIR/CodeGen/defaultarg.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,25 @@ void foo() {
3030
// OGCG: %[[TMP0:.*]] = alloca i32
3131
// OGCG: store i32 42, ptr %[[TMP0]]
3232
// OGCG: call void @_Z3barRKi(ptr {{.*}} %[[TMP0]])
33+
34+
struct S
35+
{
36+
S(int n = 2);
37+
};
38+
39+
void test_ctor_defaultarg() {
40+
S s;
41+
}
42+
43+
// CIR: cir.func {{.*}} @_Z20test_ctor_defaultargv()
44+
// CIR: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
45+
// CIR: %[[TWO:.*]] = cir.const #cir.int<2> : !s32i
46+
// CIR: cir.call @_ZN1SC1Ei(%[[S]], %[[TWO]]) : (!cir.ptr<!rec_S>, !s32i) -> ()
47+
48+
// LLVM: define{{.*}} @_Z20test_ctor_defaultargv()
49+
// LLVM: %[[S:.*]] = alloca %struct.S
50+
// LLVM: call void @_ZN1SC1Ei(ptr %[[S]], i32 2)
51+
52+
// OGCG: define{{.*}} @_Z20test_ctor_defaultargv()
53+
// OGCG: %[[S:.*]] = alloca %struct.S
54+
// OGCG: call void @_ZN1SC1Ei(ptr{{.*}} %[[S]], i32 {{.*}} 2)

0 commit comments

Comments
 (0)