1 /* Help friends in C++.
2 Copyright (C) 1997 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. */
36 is_friend (type, supplicant)
37 tree type, supplicant;
43 if (supplicant == NULL_TREE || type == NULL_TREE)
46 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
49 /* It's a function decl. */
51 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
52 tree name = DECL_NAME (supplicant);
55 if (DECL_FUNCTION_MEMBER_P (supplicant))
56 ctype = DECL_CLASS_CONTEXT (supplicant);
60 for (; list ; list = TREE_CHAIN (list))
62 if (name == TREE_PURPOSE (list))
64 tree friends = TREE_VALUE (list);
65 for (; friends ; friends = TREE_CHAIN (friends))
67 if (comptypes (ctype, TREE_PURPOSE (friends), 1))
70 if (TREE_VALUE (friends) == NULL_TREE)
73 if (supplicant == TREE_VALUE (friends))
76 /* With -fguiding-decls we are more lenient about
77 friendship. This is bogus in general since two
78 specializations of a template with non-type
79 template parameters may have the same type, but
82 Temporarily, we are also more lenient to deal
83 with nested friend functions, for which there can
84 be more than one FUNCTION_DECL, despite being the
85 same function. When that's fixed, the
86 FUNCTION_MEMBER_P bit can go. */
87 if ((flag_guiding_decls
88 || DECL_FUNCTION_MEMBER_P (supplicant))
89 && comptypes (TREE_TYPE (supplicant),
90 TREE_TYPE (TREE_VALUE (friends)), 1))
93 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
94 && is_specialization_of (supplicant,
95 TREE_VALUE (friends)))
105 if (type == supplicant)
108 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
109 for (; list ; list = TREE_CHAIN (list))
111 tree t = TREE_VALUE (list);
113 if (TREE_CODE (t) == TEMPLATE_DECL ?
114 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
115 comptypes (supplicant, t, 1))
120 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
121 context = DECL_CLASS_CONTEXT (supplicant);
123 /* Local classes have the same access as the enclosing function. */
124 context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
128 /* A namespace is not friend to anybody. */
129 if (context && TREE_CODE (context) == NAMESPACE_DECL)
133 return is_friend (type, context);
138 /* Add a new friend to the friends of the aggregate type TYPE.
139 DECL is the FUNCTION_DECL of the friend being added. */
142 add_friend (type, decl)
145 tree typedecl = TYPE_MAIN_DECL (type);
146 tree list = DECL_FRIENDLIST (typedecl);
147 tree name = DECL_NAME (decl);
151 if (name == TREE_PURPOSE (list))
153 tree friends = TREE_VALUE (list);
154 for (; friends ; friends = TREE_CHAIN (friends))
156 if (decl == TREE_VALUE (friends))
158 cp_warning ("`%D' is already a friend of class `%T'",
160 cp_warning_at ("previous friend declaration of `%D'",
161 TREE_VALUE (friends));
165 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
169 list = TREE_CHAIN (list);
171 DECL_FRIENDLIST (typedecl)
172 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
173 DECL_FRIENDLIST (typedecl));
174 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
176 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
177 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
178 if (parmtypes && TREE_CHAIN (parmtypes))
180 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
181 if (TREE_CODE (parmtype) == REFERENCE_TYPE
182 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
183 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
188 /* Declare that every member function NAME in FRIEND_TYPE
189 (which may be NULL_TREE) is a friend of type TYPE. */
192 add_friends (type, name, friend_type)
193 tree type, name, friend_type;
195 tree typedecl = TYPE_MAIN_DECL (type);
196 tree list = DECL_FRIENDLIST (typedecl);
200 if (name == TREE_PURPOSE (list))
202 tree friends = TREE_VALUE (list);
203 while (friends && TREE_PURPOSE (friends) != friend_type)
204 friends = TREE_CHAIN (friends);
208 warning ("method `%s::%s' is already a friend of class",
209 TYPE_NAME_STRING (friend_type),
210 IDENTIFIER_POINTER (name));
212 warning ("function `%s' is already a friend of class `%s'",
213 IDENTIFIER_POINTER (name),
214 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
217 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
221 list = TREE_CHAIN (list);
223 DECL_FRIENDLIST (typedecl)
225 build_tree_list (friend_type, NULL_TREE),
226 DECL_FRIENDLIST (typedecl));
227 if (! strncmp (IDENTIFIER_POINTER (name),
228 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
229 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
231 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
232 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
236 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
237 been defined, we make all of its member functions friends of
238 TYPE. If not, we make it a pending friend, which can later be added
239 when its definition is seen. If a type is defined, then its TYPE_DECL's
240 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
241 classes that are not defined. If a type has not yet been defined,
242 then the DECL_WAITING_FRIENDS contains a list of types
243 waiting to make it their friend. Note that these two can both
244 be in use at the same time! */
247 make_friend_class (type, friend_type)
248 tree type, friend_type;
251 int is_template_friend;
253 if (IS_SIGNATURE (type))
255 error ("`friend' declaration in signature definition");
258 if (IS_SIGNATURE (friend_type))
260 error ("signature type `%s' declared `friend'",
261 IDENTIFIER_POINTER (TYPE_IDENTIFIER (friend_type)));
265 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type))
269 Friend declarations shall not declare partial
272 Note that CLASSTYPE_TEMPLATE_SPECIALIZATION is not set for
273 full specializations. */
274 cp_error ("partial specialization `%T' declared `friend'",
279 if (processing_template_decl > template_class_depth (type))
280 /* If the TYPE is a template then it makes sense for it to be
281 friends with itself; this means that each instantiation is
282 friends with all other instantiations. */
283 is_template_friend = 1;
284 else if (comptypes (type, friend_type, 1))
286 pedwarn ("class `%s' is implicitly friends with itself",
287 TYPE_NAME_STRING (type));
291 is_template_friend = 0;
293 GNU_xref_hier (type, friend_type, 0, 0, 1);
295 if (is_template_friend)
296 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
298 classes = CLASSTYPE_FRIEND_CLASSES (type);
300 /* Stop if we find the same type on the list. */
301 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
302 friend_type == TREE_VALUE (classes) :
303 comptypes (TREE_VALUE (classes), friend_type, 1)))
304 classes = TREE_CHAIN (classes);
306 cp_warning ("`%T' is already a friend of `%T'",
307 TREE_VALUE (classes), type);
310 CLASSTYPE_FRIEND_CLASSES (type)
311 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
315 /* Main friend processor. This is large, and for modularity purposes,
316 has been removed from grokdeclarator. It returns `void_type_node'
317 to indicate that something happened, though a FIELD_DECL is
320 CTYPE is the class this friend belongs to.
322 DECLARATOR is the name of the friend.
324 DECL is the FUNCTION_DECL that the friend is.
326 In case we are parsing a friend which is part of an inline
327 definition, we will need to store PARM_DECL chain that comes
328 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
330 FLAGS is just used for `grokclassfn'.
332 QUALS say what special qualifies should apply to the object
333 pointed to by `this'. */
336 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
337 tree ctype, declarator, decl, parmdecls;
338 enum overload_flags flags;
342 int is_friend_template = 0;
344 /* Every decl that gets here is a friend of something. */
345 DECL_FRIEND_P (decl) = 1;
347 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
349 declarator = TREE_OPERAND (declarator, 0);
350 if (TREE_CODE (declarator) == LOOKUP_EXPR)
351 declarator = TREE_OPERAND (declarator, 0);
352 if (is_overloaded_fn (declarator))
353 declarator = DECL_NAME (get_first_fn (declarator));
356 if (TREE_CODE (decl) == FUNCTION_DECL)
357 is_friend_template = processing_template_decl >
358 template_class_depth (current_class_type);
362 tree cname = TYPE_NAME (ctype);
363 if (TREE_CODE (cname) == TYPE_DECL)
364 cname = DECL_NAME (cname);
366 /* A method friend. */
367 if (TREE_CODE (decl) == FUNCTION_DECL)
369 if (flags == NO_SPECIAL && ctype && declarator == cname)
370 DECL_CONSTRUCTOR_P (decl) = 1;
372 /* This will set up DECL_ARGUMENTS for us. */
373 grokclassfn (ctype, cname, decl, flags, quals);
375 if (is_friend_template)
376 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
378 if (TYPE_SIZE (ctype) != 0 && template_class_depth (ctype) == 0)
379 decl = check_classfn (ctype, decl);
381 /* TYPE_BEING_DEFINED is a hack for nested classes having
382 member functions of the enclosing class as friends. Will
383 go away as parsing of classes gets rewritten. */
384 if (TREE_TYPE (decl) != error_mark_node)
386 if (TYPE_BEING_DEFINED (ctype) ||
387 TYPE_SIZE (ctype) || template_class_depth (ctype) > 0)
388 add_friend (current_class_type, decl);
390 cp_error ("member `%D' declared as friend before type `%T' defined",
396 /* Possibly a bunch of method friends. */
398 /* Get the class they belong to. */
399 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
400 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
403 add_friends (current_class_type, declarator, ctype);
405 cp_error ("method `%D' is not a member of class `%T'",
407 decl = void_type_node;
410 else if (TREE_CODE (decl) == FUNCTION_DECL
411 && (MAIN_NAME_P (declarator)
412 || (IDENTIFIER_LENGTH (declarator) > 10
413 && IDENTIFIER_POINTER (declarator)[0] == '_'
414 && IDENTIFIER_POINTER (declarator)[1] == '_'
415 && strncmp (IDENTIFIER_POINTER (declarator)+2,
416 "builtin_", 8) == 0)))
418 /* raw "main", and builtin functions never gets overloaded,
419 but they can become friends. */
420 add_friend (current_class_type, decl);
421 DECL_FRIEND_P (decl) = 1;
422 decl = void_type_node;
425 @@ or possibly a friend from a base class ?!? */
426 else if (TREE_CODE (decl) == FUNCTION_DECL)
428 /* Friends must all go through the overload machinery,
429 even though they may not technically be overloaded.
431 Note that because classes all wind up being top-level
432 in their scope, their friend wind up in top-level scope as well. */
433 set_mangled_name_for_decl (decl);
434 DECL_ARGUMENTS (decl) = parmdecls;
436 DECL_CLASS_CONTEXT (decl) = current_class_type;
438 if (! DECL_USE_TEMPLATE (decl))
440 /* We can call pushdecl here, because the TREE_CHAIN of this
441 FUNCTION_DECL is not needed for other purposes. Don't do
442 this for a template instantiation. However, we don't
443 call pushdecl() for a friend function of a template
444 class, since in general, such a declaration depends on
445 template parameters. Instead, we call pushdecl when the
446 class is instantiated. */
447 if (!is_friend_template
448 && template_class_depth (current_class_type) == 0)
449 decl = pushdecl (decl);
451 decl = push_template_decl_real (decl, /*is_friend=*/1);
453 if (! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
454 && current_template_parms && uses_template_parms (decl))
456 static int explained;
457 cp_warning ("friend declaration `%#D'", decl);
458 warning (" declares a non-template function");
461 warning (" (if this is not what you intended, make sure");
462 warning (" the function template has already been declared,");
463 warning (" and add <> after the function name here)");
469 make_decl_rtl (decl, NULL_PTR, 1);
470 add_friend (current_class_type,
471 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
472 DECL_FRIEND_P (decl) = 1;
476 /* @@ Should be able to ingest later definitions of this function
478 tree decl = lookup_name_nonclass (declarator);
479 if (decl == NULL_TREE)
481 cp_warning ("implicitly declaring `%T' as struct", declarator);
482 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
483 decl = TYPE_MAIN_DECL (decl);
486 /* Allow abbreviated declarations of overloaded functions,
487 but not if those functions are really class names. */
488 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
490 cp_warning ("`friend %T' archaic, use `friend class %T' instead",
491 declarator, declarator);
492 decl = TREE_TYPE (TREE_PURPOSE (decl));
495 if (TREE_CODE (decl) == TREE_LIST)
496 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
498 make_friend_class (current_class_type, TREE_TYPE (decl));
499 decl = void_type_node;