diff mbox series

[v3,3/5] c++/modules: Support unnamed namespaces in header units

Message ID 670928a6.170a0220.2beec7.848e@mx.google.com
State New
Headers show
Series c++/modules: Implement P1815 "Translation-unit-local entities" | expand

Commit Message

Nathaniel Shead Oct. 11, 2024, 1:31 p.m. UTC
Added the rationale comment for the 'make_namespace_finish' changes,
changed commit message to mention 'unnamed' rather than 'anonymous'
namespaces.

-- >8 --

A header unit may contain unnamed namespaces, and those declarations
are exported (as with any declaration in a header unit).  This patch
ensures that such declarations are correctly handled.

The change to 'make_namespace_finish' is required so that if an unnamed
namespace is first seen by an import it is correctly handled within
'add_imported_namespace'.  I don't see any particular reason why
handling of unnamed namespaces here had to be handled separately outside
that function since these are the only two callers.

gcc/cp/ChangeLog:

	* module.cc (depset::hash::add_binding_entity): Also walk
	unnamed namespaces.
	(module_state::write_namespaces): Adjust assertion.
	* name-lookup.cc (push_namespace): Move anon using-directive
	handling to...
	(make_namespace_finish): ...here.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/internal-9_a.H: New test.
	* g++.dg/modules/internal-9_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
---
 gcc/cp/module.cc                            |  7 +++--
 gcc/cp/name-lookup.cc                       | 10 +++----
 gcc/testsuite/g++.dg/modules/internal-9_a.H | 28 ++++++++++++++++++++
 gcc/testsuite/g++.dg/modules/internal-9_b.C | 29 +++++++++++++++++++++
 4 files changed, 65 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/internal-9_a.H
 create mode 100644 gcc/testsuite/g++.dg/modules/internal-9_b.C
diff mbox series

Patch

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 4b26dc5d367..f75e211e0e1 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -13723,15 +13723,15 @@  depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
       return (flags & WMB_Using
 	      ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
     }
-  else if (DECL_NAME (decl) && !data->met_namespace)
+  else if (!data->met_namespace)
     {
       /* Namespace, walk exactly once.  */
-      gcc_checking_assert (TREE_PUBLIC (decl));
       data->met_namespace = true;
       if (data->hash->add_namespace_entities (decl, data->partitions))
 	{
 	  /* It contains an exported thing, so it is exported.  */
 	  gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
+	  gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
 	  DECL_MODULE_EXPORT_P (decl) = true;
 	}
 
@@ -16126,8 +16126,7 @@  module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
       tree ns = b->get_entity ();
 
       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
-      /* P1815 may have something to say about this.  */
-      gcc_checking_assert (TREE_PUBLIC (ns));
+      gcc_checking_assert (TREE_PUBLIC (ns) || header_module_p ());
 
       unsigned flags = 0;
       if (TREE_PUBLIC (ns))
diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 609bd6e8c9b..22391110fd7 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -9102,6 +9102,11 @@  make_namespace_finish (tree ns, tree *slot, bool from_import = false)
 
   if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
     emit_debug_info_using_namespace (ctx, ns, true);
+
+  /* An unnamed namespace implicitly has a using-directive inserted so
+     that its contents are usable in the surrounding context.  */
+  if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns))
+    add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns);
 }
 
 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE,
@@ -9238,11 +9243,6 @@  push_namespace (tree name, bool make_inline)
 	      gcc_checking_assert (slot);
 	    }
 	  make_namespace_finish (ns, slot);
-
-	  /* Add the anon using-directive here, we don't do it in
-	     make_namespace_finish.  */
-	  if (!DECL_NAMESPACE_INLINE_P (ns) && !name)
-	    add_using_namespace (current_binding_level->using_directives, ns);
 	}
     }
 
diff --git a/gcc/testsuite/g++.dg/modules/internal-9_a.H b/gcc/testsuite/g++.dg/modules/internal-9_a.H
new file mode 100644
index 00000000000..57fe60bb3c0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/internal-9_a.H
@@ -0,0 +1,28 @@ 
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+static int x = 123;
+static void f() {}
+template <typename T> static void t() {}
+
+namespace {
+  int y = 456;
+  void g() {};
+  template <typename T> void u() {}
+
+  namespace ns { int in_ns = 456; }
+
+  struct A {};
+  template <typename T> struct B {};
+
+  enum E { X };
+  enum class F { Y };
+
+  template <typename T> using U = int;
+
+#if __cplusplus >= 202002L
+  template <typename T> concept C = true;
+#endif
+}
+
+namespace ns2 = ns;
diff --git a/gcc/testsuite/g++.dg/modules/internal-9_b.C b/gcc/testsuite/g++.dg/modules/internal-9_b.C
new file mode 100644
index 00000000000..5f8eeac2340
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/internal-9_b.C
@@ -0,0 +1,29 @@ 
+// { dg-additional-options "-fmodules-ts" }
+
+import "internal-9_a.H";
+
+int main() {
+  auto x2 = x;
+  f();
+  t<int>();
+
+  auto y2 = y;
+  g();
+  u<int>();
+
+  int val1 = ns::in_ns;
+
+  A a;
+  B<int> b;
+
+  E e = X;
+  F f = F::Y;
+
+  U<int> temp;
+
+#if __cplusplus >= 202002L
+  static_assert(C<int>);
+#endif
+
+  int val2 = ns2::in_ns;
+}