[clang-tools-extra] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

2023-09-25 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

I guess this ends up producing a CK_NoOp CastExpr?  That's probably okay, but 
can we amend the documentation for CK_NoOp to give this as an example?

https://github.com/llvm/llvm-project/pull/65918
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

2023-09-21 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/65918

>From cfedbe3fb2f2f331b3f9ee1f4f3e2fcd348797e2 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Mon, 11 Sep 2023 09:15:41 +0800
Subject: [PATCH 1/5] [Sema] add cast from IncompleteArrayType to
 ConstantArrayType in TryReferenceListInitialization

Fixed: https://github.com/llvm/llvm-project/issues/62945
c++20 supports "Permit conversions to arrays of unknown bound".
This need additional cast from IncompleteArrayType to ConstantArrayType
in TryReferenceListInitialization
---
 clang/lib/Sema/SemaInit.cpp   | 9 +
 clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp | 9 +
 2 files changed, 18 insertions(+)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 93f05e2e47285e4..966d35226eec748 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema ,
   if (T1Quals.hasAddressSpace())
 Sequence.AddQualificationConversionStep(
 cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+  else if (S.getLangOpts().CPlusPlus20 &&
+   isa(T1->getUnqualifiedDesugaredType()) &&
+   DestType->isRValueReferenceType()) {
+// [dcl.init.list] p3.10
+// unless T is “reference to array of unknown bound of U”, in which 
case
+// the type of the prvalue is the type of x in the declaration U x[] H,
+// where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);
+  }
 } else
   Sequence.SetFailed(
   
InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
diff --git a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp 
b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
index 78f35a024a54014..a29f4d720c1de4e 100644
--- a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
+++ b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
@@ -23,4 +23,13 @@ auto (int ()[1]) {
 
   return r2;
 }
+
+// CHECK-LABEL: @_ZN3One3fooEi
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret void
+void foo(int a) {
+  auto f = [](int(&&)[]) {};
+  f({a});
+}
+
 } // namespace One

>From 0d149d1e07783d2cf7b869623c6c055b9184ce8e Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 14 Sep 2023 00:28:28 +0800
Subject: [PATCH 2/5] Update clang/lib/Sema/SemaInit.cpp

Co-authored-by: Mariya Podchishchaeva 
---
 clang/lib/Sema/SemaInit.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 966d35226eec748..d8a035b60dcb6e5 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4535,7 +4535,7 @@ static void TryReferenceListInitialization(Sema ,
   else if (S.getLangOpts().CPlusPlus20 &&
isa(T1->getUnqualifiedDesugaredType()) &&
DestType->isRValueReferenceType()) {
-// [dcl.init.list] p3.10
+// C++20 [dcl.init.list]p3.10:
 // unless T is “reference to array of unknown bound of U”, in which 
case
 // the type of the prvalue is the type of x in the declaration U x[] H,
 // where H is the initializer list.

>From fecee704039f5c9cb964a646f0d5f27edd6101f6 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Sep 2023 09:46:52 +0800
Subject: [PATCH 3/5] fix

---
 clang/lib/Sema/SemaInit.cpp | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index d8a035b60dcb6e5..da006e98e81b99a 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -18,6 +18,7 @@
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/CharInfo.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/Specifiers.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
@@ -4527,20 +4528,22 @@ static void TryReferenceListInitialization(Sema ,
   if (Sequence) {
 if (DestType->isRValueReferenceType() ||
 (T1Quals.hasConst() && !T1Quals.hasVolatile())) {
+  if (S.getLangOpts().CPlusPlus20 &&
+  isa(T1->getUnqualifiedDesugaredType()) &&
+  DestType->isRValueReferenceType()) {
+// C++20 [dcl.init.list]p3.10:
+// List-initialization of an object or reference of type T is defined 
as
+// follows:
+// ..., unless T is “reference to array of unknown bound of U”, in 
which
+// case the type of the prvalue is the type of x in the declaration U
+// x[] H, where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, clang::VK_PRValue);
+  }
   Sequence.AddReferenceBindingStep(cv1T1IgnoreAS,
/*BindingTemporary=*/true);
   if (T1Quals.hasAddressSpace())
 

[clang-tools-extra] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

2023-09-18 Thread Mariya Podchishchaeva via cfe-commits


@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema ,
   if (T1Quals.hasAddressSpace())
 Sequence.AddQualificationConversionStep(
 cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+  else if (S.getLangOpts().CPlusPlus20 &&
+   isa(T1->getUnqualifiedDesugaredType()) &&
+   DestType->isRValueReferenceType()) {
+// [dcl.init.list] p3.10
+// unless T is “reference to array of unknown bound of U”, in which 
case
+// the type of the prvalue is the type of x in the declaration U x[] H,
+// where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);

Fznamznon wrote:

@HerrCai0907 I see what you mean and what the patch accomplishes. But my 
confusion was, why in order to do that `AddQualificationConversionStep` is 
called? IMO this is just to convert cv-qualifiers. The goal is to convert one 
array type to another, right?

https://github.com/llvm/llvm-project/pull/65918
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

2023-09-14 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/65918:

>From cfedbe3fb2f2f331b3f9ee1f4f3e2fcd348797e2 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Mon, 11 Sep 2023 09:15:41 +0800
Subject: [PATCH 1/4] [Sema] add cast from IncompleteArrayType to
 ConstantArrayType in TryReferenceListInitialization

Fixed: https://github.com/llvm/llvm-project/issues/62945
c++20 supports "Permit conversions to arrays of unknown bound".
This need additional cast from IncompleteArrayType to ConstantArrayType
in TryReferenceListInitialization
---
 clang/lib/Sema/SemaInit.cpp   | 9 +
 clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp | 9 +
 2 files changed, 18 insertions(+)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 93f05e2e47285e4..966d35226eec748 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema ,
   if (T1Quals.hasAddressSpace())
 Sequence.AddQualificationConversionStep(
 cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+  else if (S.getLangOpts().CPlusPlus20 &&
+   isa(T1->getUnqualifiedDesugaredType()) &&
+   DestType->isRValueReferenceType()) {
+// [dcl.init.list] p3.10
+// unless T is “reference to array of unknown bound of U”, in which 
case
+// the type of the prvalue is the type of x in the declaration U x[] H,
+// where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);
+  }
 } else
   Sequence.SetFailed(
   
InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
diff --git a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp 
b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
index 78f35a024a54014..a29f4d720c1de4e 100644
--- a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
+++ b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
@@ -23,4 +23,13 @@ auto (int ()[1]) {
 
   return r2;
 }
+
+// CHECK-LABEL: @_ZN3One3fooEi
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret void
+void foo(int a) {
+  auto f = [](int(&&)[]) {};
+  f({a});
+}
+
 } // namespace One

>From 0d149d1e07783d2cf7b869623c6c055b9184ce8e Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 14 Sep 2023 00:28:28 +0800
Subject: [PATCH 2/4] Update clang/lib/Sema/SemaInit.cpp

Co-authored-by: Mariya Podchishchaeva 
---
 clang/lib/Sema/SemaInit.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 966d35226eec748..d8a035b60dcb6e5 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4535,7 +4535,7 @@ static void TryReferenceListInitialization(Sema ,
   else if (S.getLangOpts().CPlusPlus20 &&
isa(T1->getUnqualifiedDesugaredType()) &&
DestType->isRValueReferenceType()) {
-// [dcl.init.list] p3.10
+// C++20 [dcl.init.list]p3.10:
 // unless T is “reference to array of unknown bound of U”, in which 
case
 // the type of the prvalue is the type of x in the declaration U x[] H,
 // where H is the initializer list.

>From fecee704039f5c9cb964a646f0d5f27edd6101f6 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Sep 2023 09:46:52 +0800
Subject: [PATCH 3/4] fix

---
 clang/lib/Sema/SemaInit.cpp | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index d8a035b60dcb6e5..da006e98e81b99a 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -18,6 +18,7 @@
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/CharInfo.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/Specifiers.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
@@ -4527,20 +4528,22 @@ static void TryReferenceListInitialization(Sema ,
   if (Sequence) {
 if (DestType->isRValueReferenceType() ||
 (T1Quals.hasConst() && !T1Quals.hasVolatile())) {
+  if (S.getLangOpts().CPlusPlus20 &&
+  isa(T1->getUnqualifiedDesugaredType()) &&
+  DestType->isRValueReferenceType()) {
+// C++20 [dcl.init.list]p3.10:
+// List-initialization of an object or reference of type T is defined 
as
+// follows:
+// ..., unless T is “reference to array of unknown bound of U”, in 
which
+// case the type of the prvalue is the type of x in the declaration U
+// x[] H, where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, clang::VK_PRValue);
+  }
   Sequence.AddReferenceBindingStep(cv1T1IgnoreAS,
/*BindingTemporary=*/true);
   if (T1Quals.hasAddressSpace())
 

[clang-tools-extra] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

2023-09-14 Thread Congcong Cai via cfe-commits


@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema ,
   if (T1Quals.hasAddressSpace())
 Sequence.AddQualificationConversionStep(
 cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+  else if (S.getLangOpts().CPlusPlus20 &&
+   isa(T1->getUnqualifiedDesugaredType()) &&
+   DestType->isRValueReferenceType()) {
+// [dcl.init.list] p3.10
+// unless T is “reference to array of unknown bound of U”, in which 
case
+// the type of the prvalue is the type of x in the declaration U x[] H,
+// where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);

HerrCai0907 wrote:

I have changed sequence in the latest commit.
The reason to qualification conversion is that in cpp 20, spec. requires 
support cast from `initializer list` to `reference to array of unknown bound`.

for example: 
```
void foo(int a) {
  auto f = [](int(&&)[]) {};
  f({a});
}
```

`{a}` should be casted from int[1] to int[].


ast will look like:
```
  `-CXXOperatorCallExpr 0x12b904d18  'void':'void' '()'
|-ImplicitCastExpr 0x12b904c58  'void (*)(int (&&)[]) 
const' 
| `-DeclRefExpr 0x12b904bd8  'void (int (&&)[]) const' 
lvalue CXXMethod 0x12b904008 'operator()' 'void (int (&&)[]) const'
|-ImplicitCastExpr 0x12b904c70  'const (lambda at 
ID/test.cpp:2:12)' lvalue 
| `-DeclRefExpr 0x12b904b10  '(lambda at 
ID/test.cpp:2:12)':'(lambda at ID/test.cpp:2:12)' lvalue Var 0x12b8ee210 'f' 
'(lambda at ID/test.cpp:2:12)':'(lambda at ID/test.cpp:2:12)'
`-MaterializeTemporaryExpr 0x12b904d00  'int[]':'int[]' 
xvalue
  `-ImplicitCastExpr 0x12b904ce8  'int[]':'int[]' 
`-InitListExpr 0x12b904c88  'int[1]'
  `-ImplicitCastExpr 0x12b904cc8  'int' 
`-DeclRefExpr 0x12b904b30  'int' lvalue ParmVar 
0x12b8edfb8 'a' 'int'
```


https://github.com/llvm/llvm-project/pull/65918
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

2023-09-14 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/65918:

>From cfedbe3fb2f2f331b3f9ee1f4f3e2fcd348797e2 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Mon, 11 Sep 2023 09:15:41 +0800
Subject: [PATCH 1/3] [Sema] add cast from IncompleteArrayType to
 ConstantArrayType in TryReferenceListInitialization

Fixed: https://github.com/llvm/llvm-project/issues/62945
c++20 supports "Permit conversions to arrays of unknown bound".
This need additional cast from IncompleteArrayType to ConstantArrayType
in TryReferenceListInitialization
---
 clang/lib/Sema/SemaInit.cpp   | 9 +
 clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp | 9 +
 2 files changed, 18 insertions(+)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 93f05e2e47285e4..966d35226eec748 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema ,
   if (T1Quals.hasAddressSpace())
 Sequence.AddQualificationConversionStep(
 cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+  else if (S.getLangOpts().CPlusPlus20 &&
+   isa(T1->getUnqualifiedDesugaredType()) &&
+   DestType->isRValueReferenceType()) {
+// [dcl.init.list] p3.10
+// unless T is “reference to array of unknown bound of U”, in which 
case
+// the type of the prvalue is the type of x in the declaration U x[] H,
+// where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);
+  }
 } else
   Sequence.SetFailed(
   
InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
diff --git a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp 
b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
index 78f35a024a54014..a29f4d720c1de4e 100644
--- a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
+++ b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
@@ -23,4 +23,13 @@ auto (int ()[1]) {
 
   return r2;
 }
+
+// CHECK-LABEL: @_ZN3One3fooEi
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret void
+void foo(int a) {
+  auto f = [](int(&&)[]) {};
+  f({a});
+}
+
 } // namespace One

>From 0d149d1e07783d2cf7b869623c6c055b9184ce8e Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 14 Sep 2023 00:28:28 +0800
Subject: [PATCH 2/3] Update clang/lib/Sema/SemaInit.cpp

Co-authored-by: Mariya Podchishchaeva 
---
 clang/lib/Sema/SemaInit.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 966d35226eec748..d8a035b60dcb6e5 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4535,7 +4535,7 @@ static void TryReferenceListInitialization(Sema ,
   else if (S.getLangOpts().CPlusPlus20 &&
isa(T1->getUnqualifiedDesugaredType()) &&
DestType->isRValueReferenceType()) {
-// [dcl.init.list] p3.10
+// C++20 [dcl.init.list]p3.10:
 // unless T is “reference to array of unknown bound of U”, in which 
case
 // the type of the prvalue is the type of x in the declaration U x[] H,
 // where H is the initializer list.

>From fecee704039f5c9cb964a646f0d5f27edd6101f6 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Sep 2023 09:46:52 +0800
Subject: [PATCH 3/3] fix

---
 clang/lib/Sema/SemaInit.cpp | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index d8a035b60dcb6e5..da006e98e81b99a 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -18,6 +18,7 @@
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/CharInfo.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/Specifiers.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
@@ -4527,20 +4528,22 @@ static void TryReferenceListInitialization(Sema ,
   if (Sequence) {
 if (DestType->isRValueReferenceType() ||
 (T1Quals.hasConst() && !T1Quals.hasVolatile())) {
+  if (S.getLangOpts().CPlusPlus20 &&
+  isa(T1->getUnqualifiedDesugaredType()) &&
+  DestType->isRValueReferenceType()) {
+// C++20 [dcl.init.list]p3.10:
+// List-initialization of an object or reference of type T is defined 
as
+// follows:
+// ..., unless T is “reference to array of unknown bound of U”, in 
which
+// case the type of the prvalue is the type of x in the declaration U
+// x[] H, where H is the initializer list.
+Sequence.AddQualificationConversionStep(cv1T1, clang::VK_PRValue);
+  }
   Sequence.AddReferenceBindingStep(cv1T1IgnoreAS,
/*BindingTemporary=*/true);
   if (T1Quals.hasAddressSpace())