1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
34 /* Friend data structures are described in cp-tree.h. */
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
39 is_friend (tree type
, tree supplicant
)
45 if (supplicant
== NULL_TREE
|| type
== NULL_TREE
)
48 declp
= DECL_P (supplicant
);
51 /* It's a function decl. */
53 tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
54 tree name
= DECL_NAME (supplicant
);
56 for (; list
; list
= TREE_CHAIN (list
))
58 if (name
== FRIEND_NAME (list
))
60 tree friends
= FRIEND_DECLS (list
);
61 for (; friends
; friends
= TREE_CHAIN (friends
))
63 tree
friend = TREE_VALUE (friends
);
65 if (friend == NULL_TREE
)
68 if (supplicant
== friend)
71 if (is_specialization_of_friend (supplicant
, friend))
81 /* Nested classes are implicitly friends of their enclosing types, as
82 per core issue 45 (this is a change from the standard). */
83 for (context
= supplicant
;
84 context
&& TYPE_P (context
);
85 context
= TYPE_CONTEXT (context
))
89 list
= CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type
)));
90 for (; list
; list
= TREE_CHAIN (list
))
92 tree t
= TREE_VALUE (list
);
94 if (TREE_CODE (t
) == TEMPLATE_DECL
?
95 is_specialization_of (TYPE_MAIN_DECL (supplicant
), t
) :
96 same_type_p (supplicant
, t
))
101 if (declp
&& DECL_FUNCTION_MEMBER_P (supplicant
))
102 context
= DECL_CONTEXT (supplicant
);
104 /* Local classes have the same access as the enclosing function. */
105 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
109 /* A namespace is not friend to anybody. */
110 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
114 return is_friend (type
, context
);
119 /* Add a new friend to the friends of the aggregate type TYPE.
120 DECL is the FUNCTION_DECL of the friend being added.
122 If COMPLAIN is true, warning about duplicate friend is issued.
123 We want to have this diagnostics during parsing but not
124 when a template is being instantiated. */
127 add_friend (tree type
, tree decl
, bool complain
)
133 if (decl
== error_mark_node
)
136 typedecl
= TYPE_MAIN_DECL (type
);
137 list
= DECL_FRIENDLIST (typedecl
);
138 name
= DECL_NAME (decl
);
139 type
= TREE_TYPE (typedecl
);
143 if (name
== FRIEND_NAME (list
))
145 tree friends
= FRIEND_DECLS (list
);
146 for (; friends
; friends
= TREE_CHAIN (friends
))
148 if (decl
== TREE_VALUE (friends
))
151 warning ("`%D' is already a friend of class `%T'",
157 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
159 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
163 list
= TREE_CHAIN (list
);
166 if (DECL_CLASS_SCOPE_P (decl
))
167 perform_or_defer_access_check (TYPE_BINFO (DECL_CONTEXT (decl
)), decl
);
169 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
171 DECL_FRIENDLIST (typedecl
)
172 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
173 DECL_FRIENDLIST (typedecl
));
174 if (!uses_template_parms (type
))
175 DECL_BEFRIENDING_CLASSES (decl
)
176 = tree_cons (NULL_TREE
, type
,
177 DECL_BEFRIENDING_CLASSES (decl
));
180 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
181 been defined, we make all of its member functions friends of
182 TYPE. If not, we make it a pending friend, which can later be added
183 when its definition is seen. If a type is defined, then its TYPE_DECL's
184 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
185 classes that are not defined. If a type has not yet been defined,
186 then the DECL_WAITING_FRIENDS contains a list of types
187 waiting to make it their friend. Note that these two can both
188 be in use at the same time!
190 If COMPLAIN is true, warning about duplicate friend is issued.
191 We want to have this diagnostics during parsing but not
192 when a template is being instantiated. */
195 make_friend_class (tree type
, tree friend_type
, bool complain
)
198 int is_template_friend
;
200 if (! IS_AGGR_TYPE (friend_type
))
202 error ("invalid type `%T' declared `friend'", friend_type
);
206 if (processing_template_decl
> template_class_depth (type
))
207 /* If the TYPE is a template then it makes sense for it to be
208 friends with itself; this means that each instantiation is
209 friends with all other instantiations. */
211 if (CLASS_TYPE_P (friend_type
)
212 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
213 && uses_template_parms (friend_type
))
216 Friend declarations shall not declare partial
218 error ("partial specialization `%T' declared `friend'",
223 is_template_friend
= 1;
225 else if (same_type_p (type
, friend_type
))
228 pedwarn ("class `%T' is implicitly friends with itself",
233 is_template_friend
= 0;
237 A friend of a class or class template can be a function or
238 class template, a specialization of a function template or
239 class template, or an ordinary (nontemplate) function or
241 if (!is_template_friend
)
243 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
245 /* template <class T> friend typename S<T>::X; */
246 error ("typename type `%#T' declared `friend'", friend_type
);
249 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
251 /* template <class T> friend class T; */
252 error ("template parameter type `%T' declared `friend'", friend_type
);
255 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
257 /* template <class T> friend class A; where A is not a template */
258 error ("`%#T' is not a template", friend_type
);
262 if (is_template_friend
)
263 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
265 /* See if it is already a friend. */
266 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
268 classes
= TREE_CHAIN (classes
))
270 tree probe
= TREE_VALUE (classes
);
272 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
274 if (friend_type
== probe
)
277 warning ("`%D' is already a friend of `%T'",
282 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
284 if (same_type_p (probe
, friend_type
))
287 warning ("`%T' is already a friend of `%T'",
296 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
298 CLASSTYPE_FRIEND_CLASSES (type
)
299 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
300 if (is_template_friend
)
301 friend_type
= TREE_TYPE (friend_type
);
302 if (!uses_template_parms (type
))
303 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
304 = tree_cons (NULL_TREE
, type
,
305 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
309 /* Main friend processor.
311 CTYPE is the class this friend belongs to.
313 DECLARATOR is the name of the friend.
315 DECL is the FUNCTION_DECL that the friend is.
317 FLAGS is just used for `grokclassfn'.
319 QUALS say what special qualifies should apply to the object
320 pointed to by `this'. */
323 do_friend (tree ctype
, tree declarator
, tree decl
,
324 tree attrlist
, enum overload_flags flags
, tree quals
,
327 /* Every decl that gets here is a friend of something. */
328 DECL_FRIEND_P (decl
) = 1;
330 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
332 declarator
= TREE_OPERAND (declarator
, 0);
333 if (is_overloaded_fn (declarator
))
334 declarator
= DECL_NAME (get_first_fn (declarator
));
337 if (TREE_CODE (decl
) != FUNCTION_DECL
)
342 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
343 the enclosing class. FRIEND_DEPTH counts the number of template
344 headers used for this friend declaration. TEMPLATE_MEMBER_P is
345 true if a template header in FRIEND_DEPTH is intended for
346 DECLARATOR. For example, the code
348 template <class T> struct A {
349 template <class U> struct B {
350 template <class V> template <class W>
351 friend void C<V>::f(W);
355 will eventually give the following results
357 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
358 2. FRIEND_DEPTH equals 2 (for `V' and `W').
359 3. TEMPLATE_MEMBER_P is true (for `W'). */
361 int class_template_depth
= template_class_depth (current_class_type
);
362 int friend_depth
= processing_template_decl
- class_template_depth
;
363 /* We will figure this out later. */
364 bool template_member_p
= false;
366 tree cname
= TYPE_NAME (ctype
);
367 if (TREE_CODE (cname
) == TYPE_DECL
)
368 cname
= DECL_NAME (cname
);
370 /* A method friend. */
371 if (flags
== NO_SPECIAL
&& declarator
== cname
)
372 DECL_CONSTRUCTOR_P (decl
) = 1;
374 /* This will set up DECL_ARGUMENTS for us. */
375 grokclassfn (ctype
, decl
, flags
, quals
);
379 if (!uses_template_parms_level (ctype
, class_template_depth
381 template_member_p
= true;
384 /* A nested class may declare a member of an enclosing class
385 to be a friend, so we do lookup here even if CTYPE is in
386 the process of being defined. */
387 if (class_template_depth
388 || COMPLETE_TYPE_P (ctype
)
389 || TYPE_BEING_DEFINED (ctype
))
391 if (DECL_TEMPLATE_INFO (decl
))
392 /* DECL is a template specialization. No need to
393 build a new TEMPLATE_DECL. */
395 else if (class_template_depth
)
396 /* We rely on tsubst_friend_function to check the
397 validity of the declaration later. */
398 decl
= push_template_decl_real (decl
, /*is_friend=*/1);
400 decl
= check_classfn (ctype
, decl
,
402 ? current_template_parms
405 if (template_member_p
&& decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
406 decl
= DECL_TI_TEMPLATE (decl
);
409 add_friend (current_class_type
, decl
, /*complain=*/true);
412 error ("member `%D' declared as friend before type `%T' defined",
416 @@ or possibly a friend from a base class ?!? */
417 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
419 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
421 /* Friends must all go through the overload machinery,
422 even though they may not technically be overloaded.
424 Note that because classes all wind up being top-level
425 in their scope, their friend wind up in top-level scope as well. */
427 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
429 if (! DECL_USE_TEMPLATE (decl
))
431 /* We must check whether the decl refers to template
432 arguments before push_template_decl_real adds a
433 reference to the containing template class. */
434 int warn
= (warn_nontemplate_friend
435 && ! funcdef_flag
&& ! is_friend_template
436 && current_template_parms
437 && uses_template_parms (decl
));
439 if (is_friend_template
440 || template_class_depth (current_class_type
) != 0)
441 /* We can't call pushdecl for a template class, since in
442 general, such a declaration depends on template
443 parameters. Instead, we call pushdecl when the class
445 decl
= push_template_decl_real (decl
, /*is_friend=*/1);
446 else if (current_function_decl
)
447 /* This must be a local class, so pushdecl will be ok, and
448 insert an unqualified friend into the local scope
449 (rather than the containing namespace scope, which the
450 next choice will do). */
451 decl
= pushdecl (decl
);
454 /* We can't use pushdecl, as we might be in a template
455 class specialization, and pushdecl will insert an
456 unqualified friend decl into the template parameter
457 scope, rather than the namespace containing it. */
458 tree ns
= decl_namespace_context (decl
);
460 push_nested_namespace (ns
);
461 decl
= pushdecl_namespace_level (decl
);
462 pop_nested_namespace (ns
);
467 static int explained
;
468 warning ("friend declaration `%#D' declares a non-template function", decl
);
471 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");
477 if (decl
== error_mark_node
)
478 return error_mark_node
;
480 add_friend (current_class_type
,
481 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
483 DECL_FRIEND_P (decl
) = 1;
486 /* Unfortunately, we have to handle attributes here. Normally we would
487 handle them in start_decl_1, but since this is a friend decl start_decl_1
488 never gets to see it. */
490 /* Set attributes here so if duplicate decl, will have proper attributes. */
491 cplus_decl_attributes (&decl
, attrlist
, 0);