#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "rtl.h"
#include "expr.h"
#include "cp-tree.h"
#include "flags.h"
#include "output.h"
#include "toplev.h"
int
is_friend (tree type, tree supplicant)
{
int declp;
tree list;
tree context;
if (supplicant == NULL_TREE || type == NULL_TREE)
return 0;
declp = DECL_P (supplicant);
if (declp)
{
tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
tree name = DECL_NAME (supplicant);
for (; list ; list = TREE_CHAIN (list))
{
if (name == FRIEND_NAME (list))
{
tree friends = FRIEND_DECLS (list);
for (; friends ; friends = TREE_CHAIN (friends))
{
tree friend = TREE_VALUE (friends);
if (friend == NULL_TREE)
continue;
if (supplicant == friend)
return 1;
if (is_specialization_of_friend (supplicant, friend))
return 1;
}
break;
}
}
}
else
{
for (context = supplicant;
context && TYPE_P (context);
context = TYPE_CONTEXT (context))
if (type == context)
return 1;
list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
for (; list ; list = TREE_CHAIN (list))
{
tree t = TREE_VALUE (list);
if (TREE_CODE (t) == TEMPLATE_DECL ?
is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
same_type_p (supplicant, t))
return 1;
}
}
if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
context = DECL_CONTEXT (supplicant);
else if (! declp)
context = decl_function_context (TYPE_MAIN_DECL (supplicant));
else
context = NULL_TREE;
if (context && TREE_CODE (context) == NAMESPACE_DECL)
context = NULL_TREE;
if (context)
return is_friend (type, context);
return 0;
}
void
add_friend (tree type, tree decl, bool complain)
{
tree typedecl;
tree list;
tree name;
tree ctx;
if (decl == error_mark_node)
return;
typedecl = TYPE_MAIN_DECL (type);
list = DECL_FRIENDLIST (typedecl);
name = DECL_NAME (decl);
type = TREE_TYPE (typedecl);
while (list)
{
if (name == FRIEND_NAME (list))
{
tree friends = FRIEND_DECLS (list);
for (; friends ; friends = TREE_CHAIN (friends))
{
if (decl == TREE_VALUE (friends))
{
if (complain)
warning ("%qD is already a friend of class %qT",
decl, type);
return;
}
}
maybe_add_class_template_decl_list (type, decl, 1);
TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
TREE_VALUE (list));
return;
}
list = TREE_CHAIN (list);
}
ctx = DECL_CONTEXT (decl);
if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
perform_or_defer_access_check (TYPE_BINFO (ctx), decl);
maybe_add_class_template_decl_list (type, decl, 1);
DECL_FRIENDLIST (typedecl)
= tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
DECL_FRIENDLIST (typedecl));
if (!uses_template_parms (type))
DECL_BEFRIENDING_CLASSES (decl)
= tree_cons (NULL_TREE, type,
DECL_BEFRIENDING_CLASSES (decl));
}
void
make_friend_class (tree type, tree friend_type, bool complain)
{
tree classes;
int class_template_depth = template_class_depth (type);
int friend_depth = processing_template_decl - class_template_depth;
if (! IS_AGGR_TYPE (friend_type))
{
error ("invalid type %qT declared %<friend%>", friend_type);
return;
}
if (friend_depth)
{
if (CLASS_TYPE_P (friend_type)
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
&& uses_template_parms (friend_type))
{
error ("partial specialization %qT declared %<friend%>",
friend_type);
return;
}
}
else if (same_type_p (type, friend_type))
{
if (complain)
pedwarn ("class %qT is implicitly friends with itself",
type);
return;
}
if (!friend_depth)
;
else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
{
if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
== TEMPLATE_ID_EXPR)
{
error ("partial specialization %qT declared %<friend%>",
friend_type);
return;
}
else
{
bool template_member_p = false;
tree ctype = TYPE_CONTEXT (friend_type);
tree name = TYPE_IDENTIFIER (friend_type);
tree decl;
if (!uses_template_parms_level (ctype, class_template_depth
+ friend_depth))
template_member_p = true;
if (class_template_depth)
{
if (template_member_p)
friend_type
= make_unbound_class_template (ctype,
name,
current_template_parms,
tf_error);
else
friend_type
= make_typename_type (ctype, name, class_type, tf_error);
}
else
{
decl = lookup_member (ctype, name, 0, true);
if (!decl)
{
error ("%qT is not a member of %qT", name, ctype);
return;
}
if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
{
error ("%qT is not a member class template of %qT",
name, ctype);
cp_error_at ("%qD declared here", decl);
return;
}
if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
|| !CLASS_TYPE_P (TREE_TYPE (decl))))
{
error ("%qT is not a nested class of %qT",
name, ctype);
cp_error_at ("%qD declared here", decl);
return;
}
friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
}
}
}
else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
{
error ("template parameter type %qT declared %<friend%>", friend_type);
return;
}
else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
{
error ("%q#T is not a template", friend_type);
return;
}
else
friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
if (friend_type == error_mark_node)
return;
for (classes = CLASSTYPE_FRIEND_CLASSES (type);
classes;
classes = TREE_CHAIN (classes))
{
tree probe = TREE_VALUE (classes);
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
{
if (friend_type == probe)
{
if (complain)
warning ("%qD is already a friend of %qT", probe, type);
break;
}
}
else if (TREE_CODE (probe) != TEMPLATE_DECL)
{
if (same_type_p (probe, friend_type))
{
if (complain)
warning ("%qT is already a friend of %qT", probe, type);
break;
}
}
}
if (!classes)
{
maybe_add_class_template_decl_list (type, friend_type, 1);
CLASSTYPE_FRIEND_CLASSES (type)
= tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
friend_type = TREE_TYPE (friend_type);
if (!uses_template_parms (type))
CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
= tree_cons (NULL_TREE, type,
CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
}
}
tree
do_friend (tree ctype, tree declarator, tree decl,
tree attrlist, enum overload_flags flags,
cp_cv_quals quals,
int funcdef_flag)
{
DECL_FRIEND_P (decl) = 1;
if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
{
declarator = TREE_OPERAND (declarator, 0);
if (is_overloaded_fn (declarator))
declarator = DECL_NAME (get_first_fn (declarator));
}
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
if (ctype)
{
int class_template_depth = template_class_depth (current_class_type);
int friend_depth = processing_template_decl - class_template_depth;
bool template_member_p = false;
tree cname = TYPE_NAME (ctype);
if (TREE_CODE (cname) == TYPE_DECL)
cname = DECL_NAME (cname);
if (flags == NO_SPECIAL && declarator == cname)
DECL_CONSTRUCTOR_P (decl) = 1;
grokclassfn (ctype, decl, flags, quals);
if (friend_depth)
{
if (!uses_template_parms_level (ctype, class_template_depth
+ friend_depth))
template_member_p = true;
}
if (class_template_depth
|| COMPLETE_TYPE_P (ctype)
|| TYPE_BEING_DEFINED (ctype))
{
if (DECL_TEMPLATE_INFO (decl))
;
else if (class_template_depth)
decl = push_template_decl_real (decl, 1);
else
decl = check_classfn (ctype, decl,
template_member_p
? current_template_parms
: NULL_TREE);
if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
decl = DECL_TI_TEMPLATE (decl);
if (decl)
add_friend (current_class_type, decl, true);
}
else
error ("member %qD declared as friend before type %qT defined",
decl, ctype);
}
else if (TREE_CODE (decl) == FUNCTION_DECL)
{
int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
if (funcdef_flag)
SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
if (! DECL_USE_TEMPLATE (decl))
{
int warn = (warn_nontemplate_friend
&& ! funcdef_flag && ! is_friend_template
&& current_template_parms
&& uses_template_parms (decl));
if (is_friend_template
|| template_class_depth (current_class_type) != 0)
decl = push_template_decl_real (decl, 1);
else if (current_function_decl)
decl = pushdecl (decl);
else
{
tree ns = decl_namespace_context (decl);
push_nested_namespace (ns);
decl = pushdecl_namespace_level (decl);
pop_nested_namespace (ns);
}
if (warn)
{
static int explained;
warning ("friend declaration %q#D declares a non-template "
"function", decl);
if (! explained)
{
warning ("(if this is not what you intended, make sure "
"the function template has already been declared "
"and add <> after the function name here) "
"-Wno-non-template-friend disables this warning");
explained = 1;
}
}
}
if (decl == error_mark_node)
return error_mark_node;
add_friend (current_class_type,
is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
true);
DECL_FRIEND_P (decl) = 1;
}
cplus_decl_attributes (&decl, attrlist, 0);
return decl;
}