]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: visibility wrt template and ptrmem targs [PR70413]
authorPatrick Palka <ppalka@redhat.com>
Thu, 21 Dec 2023 18:53:43 +0000 (13:53 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 21 Dec 2023 18:53:43 +0000 (13:53 -0500)
When constraining the visibility of an instantiation, we weren't
properly considering the visibility of PTRMEM_CST and TEMPLATE_DECL
template arguments.

This patch fixes this.  It turns out we don't maintain the relevant
visibility flags for alias templates (e.g. TREE_PUBLIC is never set),
so continue to ignore alias template template arguments for now.

PR c++/70413
PR c++/107906

gcc/cp/ChangeLog:

* decl2.cc (min_vis_expr_r): Handle PTRMEM_CST and TEMPLATE_DECL
other than those for alias templates.

gcc/testsuite/ChangeLog:

* g++.dg/template/linkage2.C: New test.
* g++.dg/template/linkage3.C: New test.
* g++.dg/template/linkage4.C: New test.
* g++.dg/template/linkage4a.C: New test.

gcc/cp/decl2.cc
gcc/testsuite/g++.dg/template/linkage2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/linkage3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/linkage4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/linkage4a.C [new file with mode: 0644]

index 0850d3f5bce5d0db3f60f375c3da93d71b4587e3..4777ceb8af7a13d706a80af33d260f7854a7dc15 100644 (file)
@@ -2655,7 +2655,10 @@ min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
   int *vis_p = (int *)data;
   int tpvis = VISIBILITY_DEFAULT;
 
-  switch (TREE_CODE (*tp))
+  tree t = *tp;
+  if (TREE_CODE (t) == PTRMEM_CST)
+    t = PTRMEM_CST_MEMBER (t);
+  switch (TREE_CODE (t))
     {
     case CAST_EXPR:
     case IMPLICIT_CONV_EXPR:
@@ -2666,15 +2669,26 @@ min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
     case NEW_EXPR:
     case CONSTRUCTOR:
     case LAMBDA_EXPR:
-      tpvis = type_visibility (TREE_TYPE (*tp));
+      tpvis = type_visibility (TREE_TYPE (t));
       break;
 
+    case TEMPLATE_DECL:
+      if (DECL_ALIAS_TEMPLATE_P (t))
+       /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for
+          alias templates so we can't trust it here (PR107906).  */
+       break;
+      t = DECL_TEMPLATE_RESULT (t);
+      /* Fall through.  */
     case VAR_DECL:
     case FUNCTION_DECL:
-      if (! TREE_PUBLIC (*tp))
+      if (! TREE_PUBLIC (t))
        tpvis = VISIBILITY_ANON;
       else
-       tpvis = DECL_VISIBILITY (*tp);
+       tpvis = DECL_VISIBILITY (t);
+      break;
+
+    case FIELD_DECL:
+      tpvis = type_visibility (DECL_CONTEXT (t));
       break;
 
     default:
diff --git a/gcc/testsuite/g++.dg/template/linkage2.C b/gcc/testsuite/g++.dg/template/linkage2.C
new file mode 100644 (file)
index 0000000..08fb693
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/70413
+// { dg-do compile { target c++11 } }
+// { dg-final { scan-assembler-not "(weak|glob)\[^\n\]*_Z" } }
+
+namespace {
+  template<class> struct A;
+}
+
+template<template<class> class Q> void f() { }
+
+int main() {
+  f<A>();
+}
diff --git a/gcc/testsuite/g++.dg/template/linkage3.C b/gcc/testsuite/g++.dg/template/linkage3.C
new file mode 100644 (file)
index 0000000..257aab3
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/70413
+// { dg-final { scan-assembler-not "(weak|glob)\[^\n\]*_Z" } }
+
+namespace {
+  struct A {
+    void f();
+    int m;
+  };
+}
+
+template<void(A::*)()> void g() { }
+template<int A::*> void h() { }
+
+int main() {
+  g<&A::f>();
+  h<&A::m>();
+}
diff --git a/gcc/testsuite/g++.dg/template/linkage4.C b/gcc/testsuite/g++.dg/template/linkage4.C
new file mode 100644 (file)
index 0000000..03630ee
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/107906
+// { dg-do compile { target c++11 } }
+// { dg-final { scan-assembler-not "(weak|glob)\[^\n\]*_Z" { xfail *-*-* } } }
+
+namespace {
+  template<class> using X = int;
+  struct A {
+    template<class> using X = int;
+  };
+}
+template<template<class> class Q> void f() { }
+
+int main() {
+  f<X>();
+  f<A::X>();
+}
diff --git a/gcc/testsuite/g++.dg/template/linkage4a.C b/gcc/testsuite/g++.dg/template/linkage4a.C
new file mode 100644 (file)
index 0000000..f1934fd
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-do compile { target c++11 } }
+// { dg-final { scan-assembler "(weak|glob)\[^\n\]*_Z1fI1XEvv" } }
+// { dg-final { scan-assembler "(weak|glob)\[^\n\]*_Z1fIN1A1XEEvv" } }
+
+template<class> using X = int;
+struct A {
+  template<class> using X = int;
+};
+template<template<class> class Q> void f() { }
+
+int main() {
+  f<X>();
+  f<A::X>();
+}