1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 if (TREE_VALUE (friends) == NULL_TREE)
66 if (supplicant == TREE_VALUE (friends))
69 /* Temporarily, we are more lenient to deal with
70 nested friend functions, for which there can be
71 more than one FUNCTION_DECL, despite being the
72 same function. When that's fixed, this bit can
74 if (DECL_FUNCTION_MEMBER_P (supplicant)
75 && same_type_p (TREE_TYPE (supplicant),
76 TREE_TYPE (TREE_VALUE (friends))))
79 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
80 && is_specialization_of (supplicant,
81 TREE_VALUE (friends)))
91 /* Nested classes are implicitly friends of their enclosing types, as
92 per core issue 45 (this is a change from the standard). */
93 for (context = supplicant;
94 context && TYPE_P (context);
95 context = TYPE_CONTEXT (context))
99 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
100 for (; list ; list = TREE_CHAIN (list))
102 tree t = TREE_VALUE (list);
104 if (TREE_CODE (t) == TEMPLATE_DECL ?
105 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
106 same_type_p (supplicant, t))
111 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
112 context = DECL_CONTEXT (supplicant);
114 /* Local classes have the same access as the enclosing function. */
115 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
119 /* A namespace is not friend to anybody. */
120 if (context && TREE_CODE (context) == NAMESPACE_DECL)
124 return is_friend (type, context);
129 /* Add a new friend to the friends of the aggregate type TYPE.
130 DECL is the FUNCTION_DECL of the friend being added.
132 If COMPLAIN is true, warning about duplicate friend is issued.
133 We want to have this diagnostics during parsing but not
134 when a template is being instantiated. */
137 add_friend (tree type, tree decl, bool complain)
143 if (decl == error_mark_node)
146 typedecl = TYPE_MAIN_DECL (type);
147 list = DECL_FRIENDLIST (typedecl);
148 name = DECL_NAME (decl);
149 type = TREE_TYPE (typedecl);
153 if (name == FRIEND_NAME (list))
155 tree friends = FRIEND_DECLS (list);
156 for (; friends ; friends = TREE_CHAIN (friends))
158 if (decl == TREE_VALUE (friends))
161 warning ("`%D' is already a friend of class `%T'",
167 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
169 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
173 list = TREE_CHAIN (list);
176 if (DECL_CLASS_SCOPE_P (decl))
177 perform_or_defer_access_check (TYPE_BINFO (DECL_CONTEXT (decl)), decl);
179 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
181 DECL_FRIENDLIST (typedecl)
182 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
183 DECL_FRIENDLIST (typedecl));
184 if (!uses_template_parms (type))
185 DECL_BEFRIENDING_CLASSES (decl)
186 = tree_cons (NULL_TREE, type,
187 DECL_BEFRIENDING_CLASSES (decl));
190 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
191 been defined, we make all of its member functions friends of
192 TYPE. If not, we make it a pending friend, which can later be added
193 when its definition is seen. If a type is defined, then its TYPE_DECL's
194 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
195 classes that are not defined. If a type has not yet been defined,
196 then the DECL_WAITING_FRIENDS contains a list of types
197 waiting to make it their friend. Note that these two can both
198 be in use at the same time!
200 If COMPLAIN is true, warning about duplicate friend is issued.
201 We want to have this diagnostics during parsing but not
202 when a template is being instantiated. */
205 make_friend_class (tree type, tree friend_type, bool complain)
208 int is_template_friend;
210 if (! IS_AGGR_TYPE (friend_type))
212 error ("invalid type `%T' declared `friend'", friend_type);
216 if (processing_template_decl > template_class_depth (type))
217 /* If the TYPE is a template then it makes sense for it to be
218 friends with itself; this means that each instantiation is
219 friends with all other instantiations. */
221 if (CLASS_TYPE_P (friend_type)
222 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
223 && uses_template_parms (friend_type))
226 Friend declarations shall not declare partial
228 error ("partial specialization `%T' declared `friend'",
233 is_template_friend = 1;
235 else if (same_type_p (type, friend_type))
238 pedwarn ("class `%T' is implicitly friends with itself",
243 is_template_friend = 0;
247 A friend of a class or class template can be a function or
248 class template, a specialization of a function template or
249 class template, or an ordinary (nontemplate) function or
251 if (!is_template_friend)
253 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
255 /* template <class T> friend typename S<T>::X; */
256 error ("typename type `%#T' declared `friend'", friend_type);
259 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
261 /* template <class T> friend class T; */
262 error ("template parameter type `%T' declared `friend'", friend_type);
265 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
267 /* template <class T> friend class A; where A is not a template */
268 error ("`%#T' is not a template", friend_type);
272 if (is_template_friend)
273 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
275 /* See if it is already a friend. */
276 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
278 classes = TREE_CHAIN (classes))
280 tree probe = TREE_VALUE (classes);
282 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
284 if (friend_type == probe)
287 warning ("`%D' is already a friend of `%T'",
292 else if (TREE_CODE (probe) != TEMPLATE_DECL)
294 if (same_type_p (probe, friend_type))
297 warning ("`%T' is already a friend of `%T'",
306 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
308 CLASSTYPE_FRIEND_CLASSES (type)
309 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
310 if (is_template_friend)
311 friend_type = TREE_TYPE (friend_type);
312 if (!uses_template_parms (type))
313 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
314 = tree_cons (NULL_TREE, type,
315 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
319 /* Main friend processor.
321 CTYPE is the class this friend belongs to.
323 DECLARATOR is the name of the friend.
325 DECL is the FUNCTION_DECL that the friend is.
327 In case we are parsing a friend which is part of an inline
328 definition, we will need to store PARM_DECL chain that comes
329 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
331 FLAGS is just used for `grokclassfn'.
333 QUALS say what special qualifies should apply to the object
334 pointed to by `this'. */
337 do_friend (tree ctype, tree declarator, tree decl, tree parmdecls,
338 tree attrlist, enum overload_flags flags, tree quals,
341 int is_friend_template = 0;
343 /* Every decl that gets here is a friend of something. */
344 DECL_FRIEND_P (decl) = 1;
346 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
348 declarator = TREE_OPERAND (declarator, 0);
349 if (is_overloaded_fn (declarator))
350 declarator = DECL_NAME (get_first_fn (declarator));
353 if (TREE_CODE (decl) != FUNCTION_DECL)
356 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
360 tree cname = TYPE_NAME (ctype);
361 if (TREE_CODE (cname) == TYPE_DECL)
362 cname = DECL_NAME (cname);
364 /* A method friend. */
365 if (flags == NO_SPECIAL && ctype && declarator == cname)
366 DECL_CONSTRUCTOR_P (decl) = 1;
368 /* This will set up DECL_ARGUMENTS for us. */
369 grokclassfn (ctype, decl, flags, quals);
371 if (is_friend_template)
372 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
373 else if (DECL_TEMPLATE_INFO (decl))
375 else if (template_class_depth (current_class_type))
376 decl = push_template_decl_real (decl, /*is_friend=*/1);
378 /* We can't do lookup in a type that involves template
379 parameters. Instead, we rely on tsubst_friend_function
380 to check the validity of the declaration later. */
381 if (processing_template_decl)
382 add_friend (current_class_type, decl, /*complain=*/true);
383 /* A nested class may declare a member of an enclosing class
384 to be a friend, so we do lookup here even if CTYPE is in
385 the process of being defined. */
386 else if (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
388 decl = check_classfn (ctype, decl);
391 add_friend (current_class_type, decl, /*complain=*/true);
394 error ("member `%D' declared as friend before type `%T' defined",
398 @@ or possibly a friend from a base class ?!? */
399 else if (TREE_CODE (decl) == FUNCTION_DECL)
401 /* Friends must all go through the overload machinery,
402 even though they may not technically be overloaded.
404 Note that because classes all wind up being top-level
405 in their scope, their friend wind up in top-level scope as well. */
406 DECL_ARGUMENTS (decl) = parmdecls;
408 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
410 if (! DECL_USE_TEMPLATE (decl))
412 /* We must check whether the decl refers to template
413 arguments before push_template_decl_real adds a
414 reference to the containing template class. */
415 int warn = (warn_nontemplate_friend
416 && ! funcdef_flag && ! is_friend_template
417 && current_template_parms
418 && uses_template_parms (decl));
420 if (is_friend_template
421 || template_class_depth (current_class_type) != 0)
422 /* We can't call pushdecl for a template class, since in
423 general, such a declaration depends on template
424 parameters. Instead, we call pushdecl when the class
426 decl = push_template_decl_real (decl, /*is_friend=*/1);
427 else if (current_function_decl)
428 /* This must be a local class, so pushdecl will be ok, and
429 insert an unqualified friend into the local scope
430 (rather than the containing namespace scope, which the
431 next choice will do). */
432 decl = pushdecl (decl);
435 /* We can't use pushdecl, as we might be in a template
436 class specialization, and pushdecl will insert an
437 unqualified friend decl into the template parameter
438 scope, rather than the namespace containing it. */
439 tree ns = decl_namespace_context (decl);
441 push_nested_namespace (ns);
442 decl = pushdecl_namespace_level (decl);
443 pop_nested_namespace (ns);
448 static int explained;
449 warning ("friend declaration `%#D' declares a non-template function", decl);
452 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");
458 if (decl == error_mark_node)
459 return error_mark_node;
461 add_friend (current_class_type,
462 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
464 DECL_FRIEND_P (decl) = 1;
467 /* Unfortunately, we have to handle attributes here. Normally we would
468 handle them in start_decl_1, but since this is a friend decl start_decl_1
469 never gets to see it. */
471 /* Set attributes here so if duplicate decl, will have proper attributes. */
472 cplus_decl_attributes (&decl, attrlist, 0);