+
+ if (use_template)
+ {
+ /* If the specialization doesn't specify visibility, use the
+ visibility from the template. */
+ tree tinfo = (TREE_CODE (decl) == TYPE_DECL
+ ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
+ : DECL_TEMPLATE_INFO (decl));
+ tree args = TI_ARGS (tinfo);
+
+ if (args != error_mark_node)
+ {
+ int depth = TMPL_ARGS_DEPTH (args);
+ tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
+
+ if (!DECL_VISIBILITY_SPECIFIED (decl))
+ {
+ DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
+ DECL_VISIBILITY_SPECIFIED (decl)
+ = DECL_VISIBILITY_SPECIFIED (pattern);
+ }
+
+ /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
+ if (args && depth > template_class_depth (class_type))
+ /* Limit visibility based on its template arguments. */
+ constrain_visibility_for_template (decl, args);
+ }
+ }
+
+ if (class_type)
+ determine_visibility_from_class (decl, class_type);
+
+ if (decl_anon_ns_mem_p (decl))
+ /* Names in an anonymous namespace get internal linkage.
+ This might change once we implement export. */
+ constrain_visibility (decl, VISIBILITY_ANON);
+ else if (TREE_CODE (decl) != TYPE_DECL)
+ {
+ /* Propagate anonymity from type to decl. */
+ int tvis = type_visibility (TREE_TYPE (decl));
+ if (tvis == VISIBILITY_ANON
+ || ! DECL_VISIBILITY_SPECIFIED (decl))
+ constrain_visibility (decl, tvis);
+ }
+}
+
+/* By default, static data members and function members receive
+ the visibility of their containing class. */
+
+static void
+determine_visibility_from_class (tree decl, tree class_type)
+{
+ if (DECL_VISIBILITY_SPECIFIED (decl))
+ return;
+
+ if (visibility_options.inlines_hidden
+ /* Don't do this for inline templates; specializations might not be
+ inline, and we don't want them to inherit the hidden
+ visibility. We'll set it here for all inline instantiations. */
+ && !processing_template_decl
+ && TREE_CODE (decl) == FUNCTION_DECL
+ && DECL_DECLARED_INLINE_P (decl)
+ && (! DECL_LANG_SPECIFIC (decl)
+ || ! DECL_EXPLICIT_INSTANTIATION (decl)))
+ DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
+ else
+ {
+ /* Default to the class visibility. */
+ DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
+ DECL_VISIBILITY_SPECIFIED (decl)
+ = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
+ }
+
+ /* Give the target a chance to override the visibility associated
+ with DECL. */
+ if (TREE_CODE (decl) == VAR_DECL
+ && (DECL_TINFO_P (decl)
+ || (DECL_VTABLE_OR_VTT_P (decl)
+ /* Construction virtual tables are not exported because
+ they cannot be referred to from other object files;
+ their name is not standardized by the ABI. */
+ && !DECL_CONSTRUCTION_VTABLE_P (decl)))
+ && TREE_PUBLIC (decl)
+ && !DECL_REALLY_EXTERN (decl)
+ && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
+ targetm.cxx.determine_class_data_visibility (decl);
+}
+
+/* Constrain the visibility of a class TYPE based on the visibility of its
+ field types. Warn if any fields require lesser visibility. */
+
+void
+constrain_class_visibility (tree type)
+{
+ tree binfo;
+ tree t;
+ int i;
+
+ int vis = type_visibility (type);
+
+ if (vis == VISIBILITY_ANON
+ || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
+ return;
+
+ /* Don't warn about visibility if the class has explicit visibility. */
+ if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
+ vis = VISIBILITY_INTERNAL;
+
+ for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
+ if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
+ {
+ tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
+ int subvis = type_visibility (ftype);
+
+ if (subvis == VISIBILITY_ANON)
+ {
+ if (!in_main_input_context ())
+ warning (0, "\
+%qT has a field %qD whose type uses the anonymous namespace",
+ type, t);
+ }
+ else if (IS_AGGR_TYPE (ftype)
+ && vis < VISIBILITY_HIDDEN
+ && subvis >= VISIBILITY_HIDDEN)
+ warning (OPT_Wattributes, "\
+%qT declared with greater visibility than the type of its field %qD",
+ type, t);
+ }
+
+ binfo = TYPE_BINFO (type);
+ for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
+ {
+ int subvis = type_visibility (TREE_TYPE (t));
+
+ if (subvis == VISIBILITY_ANON)
+ {
+ if (!in_main_input_context())
+ warning (0, "\
+%qT has a base %qT whose type uses the anonymous namespace",
+ type, TREE_TYPE (t));
+ }
+ else if (vis < VISIBILITY_HIDDEN
+ && subvis >= VISIBILITY_HIDDEN)
+ warning (OPT_Wattributes, "\
+%qT declared with greater visibility than its base %qT",
+ type, TREE_TYPE (t));
+ }