This reworks namespace serializing to avoid some issues I ran into
when working on 99170.  In modules, (non-anonymous) namespaces are
strange beasts, that always have external linkage, but may have
module-specific visibility.  I still don't get the latter 100%
correct, but this is in the right direction.

        PR c++/99344
        gcc/cp/
        * module.cc (trees_out::decl_node): Small refactor.
        (depset::hash::add_binding_entity): Return true on meeting an
        import.  Set namespace's import here.
        (module_state:write_namespaces): Inform of purview too.
        (module_state:read_namespaces): Adjust.
        * name-lookup.c (implicitly_export_namespace): Delete.
        (do_pushdecl): Don't call it.
        (push_namespace): Likewise, set purview.
        (add_imported_namespace): Reorder parms.
        * name-lookup.h (add_imported_namespace): Alter param ordering.
        gcc/testsuite/
        * g++.dg/modules/namespace-2_a.C
        * g++.dg/modules/pr99344_a.C
        * g++.dg/modules/pr99344_b.C

--
Nathan Sidwell
diff --git c/gcc/cp/module.cc w/gcc/cp/module.cc
index 369a02604fe..31172824fdd 100644
--- c/gcc/cp/module.cc
+++ w/gcc/cp/module.cc
@@ -2377,8 +2377,10 @@ public:
   }
 
 public:
+  /* This class-member is defined here, but the class was imported.  */
   bool is_member () const
   {
+    gcc_checking_assert (get_entity_kind () == EK_DECL);
     return get_flag_bit<DB_IS_MEMBER_BIT> ();
   }
 public:
@@ -8613,12 +8615,13 @@ trees_out::decl_node (tree decl, walk_kind ref)
   else if (TREE_CODE (ctx) != FUNCTION_DECL
 	   || TREE_CODE (decl) == TEMPLATE_DECL
 	   || (dep_hash->sneakoscope && DECL_IMPLICIT_TYPEDEF_P (decl))
-	   || (DECL_LANG_SPECIFIC (decl)
-	       && DECL_MODULE_IMPORT_P (decl)))
-    dep = dep_hash->add_dependency (decl,
-				    TREE_CODE (decl) == NAMESPACE_DECL
-				    && !DECL_NAMESPACE_ALIAS (decl)
-				    ? depset::EK_NAMESPACE : depset::EK_DECL);
+	   || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)))
+    {
+      auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
+		   && !DECL_NAMESPACE_ALIAS (decl)
+		   ? depset::EK_NAMESPACE : depset::EK_DECL);
+      dep = dep_hash->add_dependency (decl, kind);
+    }
 
   if (!dep)
     {
@@ -12751,12 +12754,14 @@ struct add_binding_data
   bool met_namespace;
 };
 
+/* Return true if we are, or contain something that is exported.  */
+
 bool
 depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
 {
   auto data = static_cast <add_binding_data *> (data_);
 
-  if (TREE_CODE (decl) != NAMESPACE_DECL || DECL_NAMESPACE_ALIAS (decl))
+  if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
     {
       tree inner = decl;
 
@@ -12811,7 +12816,7 @@ depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
 		    d->clear_hidden_binding ();
 		  if (flags & WMB_Export)
 		    OVL_EXPORT_P (d->get_entity ()) = true;
-		  return false;
+		  return bool (flags & WMB_Export);
 		}
 	    }
 	}
@@ -12857,30 +12862,37 @@ depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
       /* Binding and contents are mutually dependent.  */
       dep->deps.safe_push (data->binding);
 
-      return true;
+      return (flags & WMB_Using
+	      ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
     }
   else if (DECL_NAME (decl) && !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)
-	  || DECL_MODULE_EXPORT_P (decl))
+      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));
+	  DECL_MODULE_EXPORT_P (decl) = true;
+	}
+
+      if (DECL_MODULE_PURVIEW_P (decl))
 	{
 	  data->hash->make_dependency (decl, depset::EK_NAMESPACE);
-	  return true;
+
+	  return DECL_MODULE_EXPORT_P (decl);
 	}
     }
 
   return false;
 }
 
-/* Recursively find all the namespace bindings of NS.
-   Add a depset for every binding that contains an export or
-   module-linkage entity.  Add a defining depset for every such decl
-   that we need to write a definition.  Such defining depsets depend
-   on the binding depset.  Returns true if we contain something
-   explicitly exported.  */
+/* Recursively find all the namespace bindings of NS.  Add a depset
+   for every binding that contains an export or module-linkage entity.
+   Add a defining depset for every such decl that we need to write a
+   definition.  Such defining depsets depend on the binding depset.
+   Returns true if we contain something exported.  */
 
 bool
 depset::hash::add_namespace_entities (tree ns, bitmap partitions)
@@ -15088,36 +15100,29 @@ 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));
 
-      bool export_p = DECL_MODULE_EXPORT_P (ns);
-      bool inline_p = DECL_NAMESPACE_INLINE_P (ns);
-      bool public_p = TREE_PUBLIC (ns);
-
-      /* We should only be naming public namespaces, or our own
-	 private ones.  Internal linkage ones never get to be written
-	 out -- because that means something erroneously referred to a
-	 member.  However, Davis Herring's paper probably changes that
-	 by permitting them to be written out, but then an error if on
-	 touches them.  (Certain cases cannot be detected until that
-	 point.)  */ 
-      gcc_checking_assert (public_p || !DECL_MODULE_IMPORT_P (ns));
       unsigned flags = 0;
-      if (export_p)
+      if (TREE_PUBLIC (ns))
 	flags |= 1;
-      if (inline_p)
+      if (DECL_NAMESPACE_INLINE_P (ns))
 	flags |= 2;
-      if (public_p)
+      if (DECL_MODULE_PURVIEW_P (ns))
 	flags |= 4;
-      dump () && dump ("Writing namespace:%u %N%s%s%s",
-		       b->cluster, ns, export_p ? ", export" : "",
-		       public_p ? ", public" : "",
-		       inline_p ? ", inline" : "");
+      if (DECL_MODULE_EXPORT_P (ns))
+	flags |= 8;
+
+      dump () && dump ("Writing namespace:%u %N%s%s%s%s",
+		       b->cluster, ns,
+		       flags & 1 ? ", public" : "", 
+		       flags & 2 ? ", inline" : "",
+		       flags & 4 ? ", purview" : "",
+		       flags & 8 ? ", export" : "");
       sec.u (b->cluster);
       sec.u (to->name (DECL_NAME (ns)));
       write_namespace (sec, b->deps[0]);
 
-      /* Don't use bools, because this can be near the end of the
-	 section, and it won't save anything anyway.  */
       sec.u (flags);
       write_location (sec, DECL_SOURCE_LOCATION (ns));
     }
@@ -15151,26 +15156,40 @@ module_state::read_namespaces (unsigned num)
       unsigned flags = sec.u ();
       location_t src_loc = read_location (sec);
 
-      if (entity_index >= entity_num || !parent)
+      if (entity_index >= entity_num
+	  || !parent
+	  || (flags & 0xc) == 0x8)
 	sec.set_overrun ();
       if (sec.get_overrun ())
 	break;
 
       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
-      bool public_p = flags & 4;
-      bool inline_p = flags & 2;
-      bool export_p = flags & 1;
-
-      dump () && dump ("Read namespace:%u %P%s%s%s",
-		       entity_index, parent, id, export_p ? ", export" : "",
-		       public_p ? ", public" : "",
-		       inline_p ? ", inline" : "");
-      bool visible_p = (export_p
-			|| (public_p && (is_partition () || is_module ())));
-      tree inner = add_imported_namespace (parent, id, mod,
-					   src_loc, visible_p, inline_p);
-      if (export_p && is_partition ())
-	DECL_MODULE_EXPORT_P (inner) = true;
+
+      dump () && dump ("Read namespace:%u %P%s%s%s%s",
+		       entity_index, parent, id,
+		       flags & 1 ? ", public" : "", 
+		       flags & 2 ? ", inline" : "",
+		       flags & 4 ? ", purview" : "",
+		       flags & 8 ? ", export" : "");
+      bool visible_p = ((flags & 8)
+			|| ((flags & 1)
+			    && (flags & 4)
+			    && (is_partition () || is_module ())));
+      tree inner = add_imported_namespace (parent, id, src_loc, mod,
+					   bool (flags & 2), visible_p);
+      if (!inner)
+	{
+	  sec.set_overrun ();
+	  break;
+	}
+
+      if (is_partition ())
+	{
+	  if (flags & 4)
+	    DECL_MODULE_PURVIEW_P (inner) = true;
+	  if (flags & 8)
+	    DECL_MODULE_EXPORT_P (inner) = true;
+	}
 
       /* Install the namespace.  */
       (*entity_ary)[entity_lwm + entity_index] = inner;
diff --git c/gcc/cp/name-lookup.c w/gcc/cp/name-lookup.c
index 8e3038cb7ad..66c35a1c16d 100644
--- c/gcc/cp/name-lookup.c
+++ w/gcc/cp/name-lookup.c
@@ -3490,18 +3490,6 @@ push_local_extern_decl_alias (tree decl)
   DECL_LOCAL_DECL_ALIAS (decl) = alias;
 }
 
-/* NS needs to be exported, mark it and all its parents as exported.  */
-
-static void
-implicitly_export_namespace (tree ns)
-{
-  while (!DECL_MODULE_EXPORT_P (ns))
-    {
-      DECL_MODULE_EXPORT_P (ns) = true;
-      ns = CP_DECL_CONTEXT (ns);
-    }
-}
-
 /* DECL is a global or module-purview entity.  If it has non-internal
    linkage, and we have a module vector, record it in the appropriate
    slot.  We have already checked for duplicates.  */
@@ -3741,10 +3729,6 @@ do_pushdecl (tree decl, bool hiding)
 	    decl = update_binding (NULL, binding, mslot, old,
 				   match, hiding);
 
-	    if (match == decl && DECL_MODULE_EXPORT_P (decl)
-		&& !DECL_MODULE_EXPORT_P (level->this_entity))
-	      implicitly_export_namespace (level->this_entity);
-
 	    return decl;
 	  }
 
@@ -3839,16 +3823,9 @@ do_pushdecl (tree decl, bool hiding)
 	    }
 
 	  if (level->kind == sk_namespace
-	      && TREE_PUBLIC (level->this_entity))
-	    {
-	      if (TREE_CODE (decl) != CONST_DECL
-		  && DECL_MODULE_EXPORT_P (decl)
-		  && !DECL_MODULE_EXPORT_P (level->this_entity))
-		implicitly_export_namespace (level->this_entity);
-
-	      if (!not_module_p ())
-		maybe_record_mergeable_decl (slot, name, decl);
-	    }
+	      && TREE_PUBLIC (level->this_entity)
+	      && !not_module_p ())
+	    maybe_record_mergeable_decl (slot, name, decl);
 	}
     }
   else
@@ -3936,7 +3913,8 @@ lookup_class_binding (tree klass, tree name)
 
 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
    for all decls of the current module.  When partitions are involved,
-   decls might be mentioned more than once.   */
+   decls might be mentioned more than once.   Return the accumulation of
+   CALLBACK results.  */
 
 unsigned
 walk_module_binding (tree binding, bitmap partitions,
@@ -8932,9 +8910,10 @@ push_namespace (tree name, bool make_inline)
     {
       /* A public namespace is exported only if explicitly marked, or
 	 it contains exported entities.  */
-      if (!DECL_MODULE_EXPORT_P (ns) && TREE_PUBLIC (ns)
-	  && module_exporting_p ())
-	implicitly_export_namespace (ns);
+      if (TREE_PUBLIC (ns) && module_exporting_p ())
+	DECL_MODULE_EXPORT_P (ns) = true;
+      if (module_purview_p ())
+	DECL_MODULE_PURVIEW_P (ns) = true;
 
       if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
 	{
@@ -8966,12 +8945,12 @@ pop_namespace (void)
   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
 }
 
-/* An import is defining namespace NAME inside CTX.  Find or create
-   that namespace and add it to the container's binding-vector.  */
+/* An IMPORT is an import that is defining namespace NAME inside CTX.  Find or
+   create that namespace and add it to the container's binding-vector.   */
 
 tree
-add_imported_namespace (tree ctx, tree name, unsigned origin, location_t loc,
-			bool visible_p, bool inline_p)
+add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import, 
+			bool inline_p, bool visible_p)
 {
   // FIXME: Something is not correct about the VISIBLE_P handling.  We
   // need to insert this namespace into
@@ -8980,9 +8959,10 @@ add_imported_namespace (tree ctx, tree name, unsigned origin, location_t loc,
   // (c) Do we need to put it in the CURRENT slot?  This is the
   // confused piece.
 
-  gcc_checking_assert (origin);
   tree *slot = find_namespace_slot (ctx, name, true);
   tree decl = reuse_namespace (slot, ctx, name);
+
+  /* Creating and binding.  */
   if (!decl)
     {
       decl = make_namespace (ctx, name, loc, inline_p);
@@ -9010,7 +8990,7 @@ add_imported_namespace (tree ctx, tree name, unsigned origin, location_t loc,
       tree final = last->slots[jx];
       if (visible_p == !STAT_HACK_P (final)
 	  && MAYBE_STAT_DECL (final) == decl
-	  && last->indices[jx].base + last->indices[jx].span == origin
+	  && last->indices[jx].base + last->indices[jx].span == import
 	  && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
 	      || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
 		  && jx >= BINDING_SLOTS_FIXED)))
@@ -9021,7 +9001,7 @@ add_imported_namespace (tree ctx, tree name, unsigned origin, location_t loc,
     }
 
   /* Append a new slot.  */
-  tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, origin);
+  tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
 
   gcc_assert (!*mslot);
   *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
diff --git c/gcc/cp/name-lookup.h w/gcc/cp/name-lookup.h
index cb75566d1ae..d95472b7545 100644
--- c/gcc/cp/name-lookup.h
+++ w/gcc/cp/name-lookup.h
@@ -504,8 +504,9 @@ enum WMB_Flags
 extern unsigned walk_module_binding (tree binding, bitmap partitions,
 				     bool (*)(tree decl, WMB_Flags, void *data),
 				     void *data);
-extern tree add_imported_namespace (tree ctx, tree name, unsigned module,
-				    location_t, bool visible_p, bool inline_p);
+extern tree add_imported_namespace (tree ctx, tree name, location_t,
+				    unsigned module,
+				    bool inline_p, bool visible_p);
 extern void note_pending_specializations (tree ns, tree name, bool is_header);
 extern void load_pending_specializations (tree ns, tree name);
 extern const char *get_cxx_dialect_name (enum cxx_dialect dialect);
diff --git c/gcc/testsuite/g++.dg/modules/namespace-2_a.C w/gcc/testsuite/g++.dg/modules/namespace-2_a.C
index 18327e205ee..b2c9abb2482 100644
--- c/gcc/testsuite/g++.dg/modules/namespace-2_a.C
+++ w/gcc/testsuite/g++.dg/modules/namespace-2_a.C
@@ -32,9 +32,9 @@ export namespace explicit_export
 }
 
 // { dg-final { scan-lang-dump-not {Writable bindings at '::not_exported'} "module" } }
-// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::implicit_export', export, public} "module" } }
-// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::explicit_export', export, public} "module" } }
-// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::also_not_exported', public} "module" } }
-// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::explicit_export::also_exported', export, public} "module" } }
+// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::also_not_exported', public, purview\n} "module" } }
+// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::implicit_export', public, purview, export\n} "module" } }
+// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::explicit_export', public, purview, export\n} "module" } }
+// { dg-final { scan-lang-dump {Writing namespace:[0-9] '::explicit_export::also_exported', public, purview, export\n} "module" } }
 // { dg-final { scan-lang-dump-not {Writing namespace:[0-9] '::not_exported'} "module" } }
 // { dg-final { scan-lang-dump-not {Writing namespace:[0-9] '::std'} "module" } }
diff --git c/gcc/testsuite/g++.dg/modules/pr99344_a.C w/gcc/testsuite/g++.dg/modules/pr99344_a.C
new file mode 100644
index 00000000000..e0ba1faf54f
--- /dev/null
+++ w/gcc/testsuite/g++.dg/modules/pr99344_a.C
@@ -0,0 +1,23 @@
+// { dg-additional-options {-fmodules-ts -Wno-pedantic} }
+module ;
+       
+# 4 "bug_a.ii" 1
+
+namespace STD::RANGES::INNER
+{
+void Frob ();
+}
+
+struct gnu_char_traits
+{
+  void Frob()
+  {
+    STD::RANGES::INNER::Frob ();
+  }
+};
+
+# 19 "" 2
+
+export  module  hello;
+// { dg-module-cmi hello }
+export void greeter (gnu_char_traits const &name);
diff --git c/gcc/testsuite/g++.dg/modules/pr99344_b.C w/gcc/testsuite/g++.dg/modules/pr99344_b.C
new file mode 100644
index 00000000000..e54ae3d6fc5
--- /dev/null
+++ w/gcc/testsuite/g++.dg/modules/pr99344_b.C
@@ -0,0 +1,2 @@
+// { dg-additional-options -fmodules-ts }
+import  hello;

Reply via email to