TYPE_CONTEXT (base_t) = t;
       DECL_CONTEXT (base_d) = t;
 
+      set_instantiating_module (base_d);
+
       /* If the ABI version is not at least two, and the last
         field was a bit-field, RLI may not be on a byte
         boundary.  In particular, rli_size_unit_so_far might
   DECL_ARTIFICIAL (decl) = 1;
   SET_DECL_SELF_REFERENCE_P (decl);
   set_underlying_type (decl);
+  set_instantiating_module (decl);  
 
   if (processing_template_decl)
     decl = push_template_decl (decl);
 
       && !processing_template_decl)
     deduce_noexcept_on_destructor (decl);
 
+  set_originating_module (decl);
+
   decl = check_explicit_specialization (orig_declarator, decl,
                                        template_count,
                                        2 * funcdef_flag +
       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
     }
 
+  set_originating_module (decl);
+
   if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
     {
       if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
                revert this subsequently if it determines that
                the clones should share a common implementation.  */
            DECL_ABSTRACT_P (decl) = true;
+
+         set_originating_module (decl);
        }
       else if (current_class_type
               && constructor_name_p (unqualified_id, current_class_type))
              ;  /* We already issued a permerror.  */
            else if (decl && DECL_NAME (decl))
              {
+               set_originating_module (decl, true);
+               
                if (initialized)
                  /* Kludge: We need funcdef_flag to be true in do_friend for
                     in-class defaulted functions, but that breaks grokfndecl.
 
       TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
       TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
 
+      set_instantiating_module (d);
+      DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
+
       return t;
     }
 
   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
   DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
 
+  if (modules_p ())
+    {
+      /* Propagate module information from the decl.  */
+      DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
+      if (DECL_LANG_SPECIFIC (decl))
+       {
+         DECL_MODULE_PURVIEW_P (tmpl) = DECL_MODULE_PURVIEW_P (decl);
+         gcc_checking_assert (!DECL_MODULE_IMPORT_P (decl));
+       }
+    }
+
   return tmpl;
 }
 
            = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
        }
 
+      set_instantiating_module (type_decl);
+      /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
+        of export flag.  We want to propagate this because it might
+        be a friend declaration that pushes a new hidden binding.  */
+      DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
+
       if (CLASS_TYPE_P (template_type))
        {
          TREE_PRIVATE (type_decl)
   if (!DECL_DELETED_FN (r))
     DECL_INITIAL (r) = NULL_TREE;
   DECL_CONTEXT (r) = ctx;
+  set_instantiating_module (r);
 
   /* Handle explicit(dependent-expr).  */
   if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
   TREE_TYPE (r) = TREE_TYPE (inner);
   DECL_CONTEXT (r) = DECL_CONTEXT (inner);
 
+  if (modules_p ())
+    {
+      /* Propagate module information from the decl.  */
+      DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
+      if (DECL_LANG_SPECIFIC (inner))
+       {
+         DECL_MODULE_PURVIEW_P (r) = DECL_MODULE_PURVIEW_P (inner);
+         /* If this is a constrained template, the above tsubst of
+            inner can find the unconstrained template, which may have
+            come from an import.  This is ok, because we don't
+            register this instantiation (see below).  */
+         gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
+                              || (TEMPLATE_PARMS_CONSTRAINTS
+                                  (DECL_TEMPLATE_PARMS (t))));
+         DECL_MODULE_IMPORT_P (r) = false;
+       }
+    }
+
   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
 
        SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
        if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
          SET_DECL_RTL (r, NULL);
+       set_instantiating_module (r);
+
        /* The initializer must not be expanded until it is required;
           see [temp.inst].  */
        DECL_INITIAL (r) = NULL_TREE;
   DECL_TI_TEMPLATE (fndecl) = tmpl;
   DECL_TI_ARGS (fndecl) = targ_ptr;
 
+  set_instantiating_module (fndecl);
+
   /* Now we know the specialization, compute access previously
      deferred.  Do no access control for inheriting constructors,
      as we already checked access for the inherited constructor.  */
   DECL_CONTEXT (decl) = current_scope ();
   DECL_INITIAL (decl) = init;
 
+  set_originating_module (decl, false);
+
   /* Push the enclosing template.  */
   return push_template_decl (decl);
 }