1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 static void add_friend PROTO((tree, tree));
31 static void add_friends PROTO((tree, tree, tree));
33 /* Friend data structures are described in cp-tree.h. */
35 /* Returns non-zero if SUPPLICANT is a friend of TYPE. */
38 is_friend (type, supplicant)
39 tree type, supplicant;
45 if (supplicant == NULL_TREE || type == NULL_TREE)
48 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
51 /* It's a function decl. */
53 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
54 tree name = DECL_NAME (supplicant);
57 if (DECL_FUNCTION_MEMBER_P (supplicant))
58 ctype = DECL_CLASS_CONTEXT (supplicant);
62 for (; list ; list = TREE_CHAIN (list))
64 if (name == FRIEND_NAME (list))
66 tree friends = FRIEND_DECLS (list);
67 for (; friends ; friends = TREE_CHAIN (friends))
69 if (same_type_p (ctype, TREE_PURPOSE (friends)))
72 if (TREE_VALUE (friends) == NULL_TREE)
75 if (supplicant == TREE_VALUE (friends))
78 /* With -fguiding-decls we are more lenient about
79 friendship. This is bogus in general since two
80 specializations of a template with non-type
81 template parameters may have the same type, but
84 Temporarily, we are also more lenient to deal
85 with nested friend functions, for which there can
86 be more than one FUNCTION_DECL, despite being the
87 same function. When that's fixed, the
88 FUNCTION_MEMBER_P bit can go. */
89 if ((flag_guiding_decls
90 || DECL_FUNCTION_MEMBER_P (supplicant))
91 && same_type_p (TREE_TYPE (supplicant),
92 TREE_TYPE (TREE_VALUE (friends))))
95 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
96 && is_specialization_of (supplicant,
97 TREE_VALUE (friends)))
107 if (type == supplicant)
110 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
111 for (; list ; list = TREE_CHAIN (list))
113 tree t = TREE_VALUE (list);
115 if (TREE_CODE (t) == TEMPLATE_DECL ?
116 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
117 same_type_p (supplicant, t))
122 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
123 context = DECL_CLASS_CONTEXT (supplicant);
125 /* Local classes have the same access as the enclosing function. */
126 context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
130 /* A namespace is not friend to anybody. */
131 if (context && TREE_CODE (context) == NAMESPACE_DECL)
135 return is_friend (type, context);
140 /* Add a new friend to the friends of the aggregate type TYPE.
141 DECL is the FUNCTION_DECL of the friend being added. */
144 add_friend (type, decl)
147 tree typedecl = TYPE_MAIN_DECL (type);
148 tree list = DECL_FRIENDLIST (typedecl);
149 tree name = DECL_NAME (decl);
151 type = TREE_TYPE (typedecl);
155 if (name == FRIEND_NAME (list))
157 tree friends = FRIEND_DECLS (list);
158 for (; friends ; friends = TREE_CHAIN (friends))
160 if (decl == TREE_VALUE (friends))
162 cp_warning ("`%D' is already a friend of class `%T'",
164 cp_warning_at ("previous friend declaration of `%D'",
165 TREE_VALUE (friends));
169 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
173 list = TREE_CHAIN (list);
176 DECL_FRIENDLIST (typedecl)
177 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
178 DECL_FRIENDLIST (typedecl));
179 DECL_BEFRIENDING_CLASSES (decl)
180 = tree_cons (NULL_TREE, type,
181 DECL_BEFRIENDING_CLASSES (decl));
184 /* Declare that every member function NAME in FRIEND_TYPE
185 (which may be NULL_TREE) is a friend of type TYPE. */
188 add_friends (type, name, friend_type)
189 tree type, name, friend_type;
191 tree typedecl = TYPE_MAIN_DECL (type);
192 tree list = DECL_FRIENDLIST (typedecl);
196 if (name == FRIEND_NAME (list))
198 tree friends = FRIEND_DECLS (list);
199 while (friends && TREE_PURPOSE (friends) != friend_type)
200 friends = TREE_CHAIN (friends);
204 warning ("method `%s::%s' is already a friend of class",
205 TYPE_NAME_STRING (friend_type),
206 IDENTIFIER_POINTER (name));
208 warning ("function `%s' is already a friend of class `%s'",
209 IDENTIFIER_POINTER (name),
210 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
213 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
217 list = TREE_CHAIN (list);
219 DECL_FRIENDLIST (typedecl)
221 build_tree_list (friend_type, NULL_TREE),
222 DECL_FRIENDLIST (typedecl));
225 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
226 been defined, we make all of its member functions friends of
227 TYPE. If not, we make it a pending friend, which can later be added
228 when its definition is seen. If a type is defined, then its TYPE_DECL's
229 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
230 classes that are not defined. If a type has not yet been defined,
231 then the DECL_WAITING_FRIENDS contains a list of types
232 waiting to make it their friend. Note that these two can both
233 be in use at the same time! */
236 make_friend_class (type, friend_type)
237 tree type, friend_type;
240 int is_template_friend;
242 if (IS_SIGNATURE (type))
244 error ("`friend' declaration in signature definition");
247 if (IS_SIGNATURE (friend_type) || ! IS_AGGR_TYPE (friend_type))
249 cp_error ("invalid type `%T' declared `friend'", friend_type);
253 if (CLASS_TYPE_P (friend_type)
254 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
255 && uses_template_parms (friend_type))
259 Friend declarations shall not declare partial
261 cp_error ("partial specialization `%T' declared `friend'",
266 if (processing_template_decl > template_class_depth (type))
267 /* If the TYPE is a template then it makes sense for it to be
268 friends with itself; this means that each instantiation is
269 friends with all other instantiations. */
270 is_template_friend = 1;
271 else if (same_type_p (type, friend_type))
273 pedwarn ("class `%s' is implicitly friends with itself",
274 TYPE_NAME_STRING (type));
278 is_template_friend = 0;
280 GNU_xref_hier (type, friend_type, 0, 0, 1);
282 if (is_template_friend)
283 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
285 classes = CLASSTYPE_FRIEND_CLASSES (type);
287 /* Stop if we find the same type on the list. */
288 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
289 friend_type == TREE_VALUE (classes) :
290 same_type_p (TREE_VALUE (classes), friend_type)))
291 classes = TREE_CHAIN (classes);
293 cp_warning ("`%T' is already a friend of `%T'",
294 TREE_VALUE (classes), type);
297 CLASSTYPE_FRIEND_CLASSES (type)
298 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
299 if (is_template_friend)
300 friend_type = TREE_TYPE (friend_type);
301 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
302 = tree_cons (NULL_TREE, type,
303 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
307 /* Main friend processor. This is large, and for modularity purposes,
308 has been removed from grokdeclarator. It returns `void_type_node'
309 to indicate that something happened, though a FIELD_DECL is
312 CTYPE is the class this friend belongs to.
314 DECLARATOR is the name of the friend.
316 DECL is the FUNCTION_DECL that the friend is.
318 In case we are parsing a friend which is part of an inline
319 definition, we will need to store PARM_DECL chain that comes
320 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
322 FLAGS is just used for `grokclassfn'.
324 QUALS say what special qualifies should apply to the object
325 pointed to by `this'. */
328 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
329 tree ctype, declarator, decl, parmdecls;
330 enum overload_flags flags;
334 int is_friend_template = 0;
336 /* Every decl that gets here is a friend of something. */
337 DECL_FRIEND_P (decl) = 1;
339 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
341 declarator = TREE_OPERAND (declarator, 0);
342 if (TREE_CODE (declarator) == LOOKUP_EXPR)
343 declarator = TREE_OPERAND (declarator, 0);
344 if (is_overloaded_fn (declarator))
345 declarator = DECL_NAME (get_first_fn (declarator));
348 if (TREE_CODE (decl) == FUNCTION_DECL)
349 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
353 tree cname = TYPE_NAME (ctype);
354 if (TREE_CODE (cname) == TYPE_DECL)
355 cname = DECL_NAME (cname);
357 /* A method friend. */
358 if (TREE_CODE (decl) == FUNCTION_DECL)
360 if (flags == NO_SPECIAL && ctype && declarator == cname)
361 DECL_CONSTRUCTOR_P (decl) = 1;
363 /* This will set up DECL_ARGUMENTS for us. */
364 grokclassfn (ctype, decl, flags, quals);
366 if (is_friend_template)
367 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
368 else if (template_class_depth (current_class_type))
369 decl = push_template_decl_real (decl, /*is_friend=*/1);
371 /* We can't do lookup in a type that involves template
372 parameters. Instead, we rely on tsubst_friend_function
373 to check the validity of the declaration later. */
374 if (uses_template_parms (ctype))
375 add_friend (current_class_type, decl);
376 /* A nested class may declare a member of an enclosing class
377 to be a friend, so we do lookup here even if CTYPE is in
378 the process of being defined. */
379 else if (TYPE_SIZE (ctype) != 0 || TYPE_BEING_DEFINED (ctype))
381 decl = check_classfn (ctype, decl);
384 add_friend (current_class_type, decl);
387 cp_error ("member `%D' declared as friend before type `%T' defined",
392 /* Possibly a bunch of method friends. */
394 /* Get the class they belong to. */
395 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
396 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
399 add_friends (current_class_type, declarator, ctype);
401 cp_error ("method `%D' is not a member of class `%T'",
403 decl = void_type_node;
407 @@ or possibly a friend from a base class ?!? */
408 else if (TREE_CODE (decl) == FUNCTION_DECL)
410 /* Friends must all go through the overload machinery,
411 even though they may not technically be overloaded.
413 Note that because classes all wind up being top-level
414 in their scope, their friend wind up in top-level scope as well. */
415 DECL_ARGUMENTS (decl) = parmdecls;
417 DECL_CLASS_CONTEXT (decl) = current_class_type;
419 if (! DECL_USE_TEMPLATE (decl))
421 /* We can call pushdecl here, because the TREE_CHAIN of this
422 FUNCTION_DECL is not needed for other purposes. Don't do
423 this for a template instantiation. However, we don't
424 call pushdecl() for a friend function of a template
425 class, since in general, such a declaration depends on
426 template parameters. Instead, we call pushdecl when the
427 class is instantiated. */
428 if (!is_friend_template
429 && template_class_depth (current_class_type) == 0)
430 decl = pushdecl (decl);
432 decl = push_template_decl_real (decl, /*is_friend=*/1);
434 if (warn_nontemplate_friend
435 && ! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
436 && current_template_parms && uses_template_parms (decl))
438 static int explained;
439 cp_warning ("friend declaration `%#D'", decl);
440 warning (" declares a non-template function");
443 warning (" (if this is not what you intended, make sure");
444 warning (" the function template has already been declared,");
445 warning (" and add <> after the function name here)");
446 warning (" -Wno-non-template-friend disables this warning.");
452 make_decl_rtl (decl, NULL_PTR, 1);
453 add_friend (current_class_type,
454 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
455 DECL_FRIEND_P (decl) = 1;
459 /* @@ Should be able to ingest later definitions of this function
461 tree decl = lookup_name_nonclass (declarator);
462 if (decl == NULL_TREE)
464 cp_warning ("implicitly declaring `%T' as struct", declarator);
465 decl = xref_tag (record_type_node, declarator, 1);
466 decl = TYPE_MAIN_DECL (decl);
469 /* Allow abbreviated declarations of overloaded functions,
470 but not if those functions are really class names. */
471 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
473 cp_warning ("`friend %T' archaic, use `friend class %T' instead",
474 declarator, declarator);
475 decl = TREE_TYPE (TREE_PURPOSE (decl));
478 if (TREE_CODE (decl) == TREE_LIST)
479 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
481 make_friend_class (current_class_type, TREE_TYPE (decl));
482 decl = void_type_node;