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. */
29 static void add_friend PROTO((tree, tree));
30 static void add_friends PROTO((tree, tree, tree));
32 /* Friend data structures are described in cp-tree.h. */
35 is_friend (type, supplicant)
36 tree type, supplicant;
42 if (supplicant == NULL_TREE || type == NULL_TREE)
45 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
48 /* It's a function decl. */
50 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
51 tree name = DECL_NAME (supplicant);
54 if (DECL_FUNCTION_MEMBER_P (supplicant))
55 ctype = DECL_CLASS_CONTEXT (supplicant);
59 for (; list ; list = TREE_CHAIN (list))
61 if (name == TREE_PURPOSE (list))
63 tree friends = TREE_VALUE (list);
64 for (; friends ; friends = TREE_CHAIN (friends))
66 if (ctype == TREE_PURPOSE (friends))
69 if (TREE_VALUE (friends) == NULL_TREE)
72 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL)
74 if (is_specialization_of (supplicant,
75 TREE_VALUE (friends)))
81 /* FIXME: The use of comptypes here is bogus, since
82 two specializations of a template with non-type
83 parameters may have the same type, but be
85 if (comptypes (TREE_TYPE (supplicant),
86 TREE_TYPE (TREE_VALUE (friends)), 1))
96 if (type == supplicant)
99 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
100 for (; list ; list = TREE_CHAIN (list))
102 tree t = TREE_VALUE (list);
105 || (CLASSTYPE_IS_TEMPLATE (t)
106 && is_specialization_of (TYPE_MAIN_DECL (supplicant),
107 CLASSTYPE_TI_TEMPLATE (t))))
112 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
113 context = DECL_CLASS_CONTEXT (supplicant);
115 /* Local classes have the same access as the enclosing function. */
116 context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
121 return is_friend (type, context);
126 /* Add a new friend to the friends of the aggregate type TYPE.
127 DECL is the FUNCTION_DECL of the friend being added. */
130 add_friend (type, decl)
133 tree typedecl = TYPE_MAIN_DECL (type);
134 tree list = DECL_FRIENDLIST (typedecl);
135 tree name = DECL_NAME (decl);
139 if (name == TREE_PURPOSE (list))
141 tree friends = TREE_VALUE (list);
142 for (; friends ; friends = TREE_CHAIN (friends))
144 if (decl == TREE_VALUE (friends))
146 cp_warning ("`%D' is already a friend of class `%T'",
148 cp_warning_at ("previous friend declaration of `%D'",
149 TREE_VALUE (friends));
153 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
157 list = TREE_CHAIN (list);
159 DECL_FRIENDLIST (typedecl)
160 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
161 DECL_FRIENDLIST (typedecl));
162 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
164 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
165 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
166 if (parmtypes && TREE_CHAIN (parmtypes))
168 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
169 if (TREE_CODE (parmtype) == REFERENCE_TYPE
170 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
171 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
176 /* Declare that every member function NAME in FRIEND_TYPE
177 (which may be NULL_TREE) is a friend of type TYPE. */
180 add_friends (type, name, friend_type)
181 tree type, name, friend_type;
183 tree typedecl = TYPE_MAIN_DECL (type);
184 tree list = DECL_FRIENDLIST (typedecl);
188 if (name == TREE_PURPOSE (list))
190 tree friends = TREE_VALUE (list);
191 while (friends && TREE_PURPOSE (friends) != friend_type)
192 friends = TREE_CHAIN (friends);
196 warning ("method `%s::%s' is already a friend of class",
197 TYPE_NAME_STRING (friend_type),
198 IDENTIFIER_POINTER (name));
200 warning ("function `%s' is already a friend of class `%s'",
201 IDENTIFIER_POINTER (name),
202 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
205 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
209 list = TREE_CHAIN (list);
211 DECL_FRIENDLIST (typedecl)
213 build_tree_list (friend_type, NULL_TREE),
214 DECL_FRIENDLIST (typedecl));
215 if (! strncmp (IDENTIFIER_POINTER (name),
216 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
217 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
219 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
220 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
224 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
225 been defined, we make all of its member functions friends of
226 TYPE. If not, we make it a pending friend, which can later be added
227 when its definition is seen. If a type is defined, then its TYPE_DECL's
228 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
229 classes that are not defined. If a type has not yet been defined,
230 then the DECL_WAITING_FRIENDS contains a list of types
231 waiting to make it their friend. Note that these two can both
232 be in use at the same time! */
235 make_friend_class (type, friend_type)
236 tree type, friend_type;
240 if (IS_SIGNATURE (type))
242 error ("`friend' declaration in signature definition");
245 if (IS_SIGNATURE (friend_type))
247 error ("signature type `%s' declared `friend'",
248 IDENTIFIER_POINTER (TYPE_IDENTIFIER (friend_type)));
251 /* If the TYPE is a template then it makes sense for it to be
252 friends with itself; this means that each instantiation is
253 friends with all other instantiations. */
254 if (type == friend_type && !CLASSTYPE_IS_TEMPLATE (type))
256 pedwarn ("class `%s' is implicitly friends with itself",
257 TYPE_NAME_STRING (type));
261 GNU_xref_hier (TYPE_NAME_STRING (type),
262 TYPE_NAME_STRING (friend_type), 0, 0, 1);
264 classes = CLASSTYPE_FRIEND_CLASSES (type);
265 while (classes && TREE_VALUE (classes) != friend_type)
266 classes = TREE_CHAIN (classes);
268 warning ("class `%s' is already friends with class `%s'",
269 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
272 CLASSTYPE_FRIEND_CLASSES (type)
273 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
277 /* Main friend processor. This is large, and for modularity purposes,
278 has been removed from grokdeclarator. It returns `void_type_node'
279 to indicate that something happened, though a FIELD_DECL is
282 CTYPE is the class this friend belongs to.
284 DECLARATOR is the name of the friend.
286 DECL is the FUNCTION_DECL that the friend is.
288 In case we are parsing a friend which is part of an inline
289 definition, we will need to store PARM_DECL chain that comes
290 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
292 FLAGS is just used for `grokclassfn'.
294 QUALS say what special qualifies should apply to the object
295 pointed to by `this'. */
298 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
299 tree ctype, declarator, decl, parmdecls;
300 enum overload_flags flags;
304 int is_friend_template = 0;
306 /* Every decl that gets here is a friend of something. */
307 DECL_FRIEND_P (decl) = 1;
309 if (TREE_CODE (decl) == FUNCTION_DECL)
310 is_friend_template = processing_template_decl >
311 template_class_depth (current_class_type);
315 tree cname = TYPE_NAME (ctype);
316 if (TREE_CODE (cname) == TYPE_DECL)
317 cname = DECL_NAME (cname);
319 /* A method friend. */
320 if (TREE_CODE (decl) == FUNCTION_DECL)
322 if (flags == NO_SPECIAL && ctype && declarator == cname)
323 DECL_CONSTRUCTOR_P (decl) = 1;
325 /* This will set up DECL_ARGUMENTS for us. */
326 grokclassfn (ctype, cname, decl, flags, quals);
328 if (is_friend_template)
329 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
331 if (TYPE_SIZE (ctype) != 0
332 && template_class_depth (ctype) == 0)
333 decl = check_classfn (ctype, decl);
335 if (TREE_TYPE (decl) != error_mark_node)
337 if (TYPE_SIZE (ctype) || template_class_depth (ctype) > 0)
338 add_friend (current_class_type, decl);
340 cp_error ("member `%D' declared as friend before type `%T' defined",
346 /* Possibly a bunch of method friends. */
348 /* Get the class they belong to. */
349 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
350 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
353 add_friends (current_class_type, declarator, ctype);
355 error ("method `%s' is not a member of class `%s'",
356 IDENTIFIER_POINTER (declarator),
357 IDENTIFIER_POINTER (cname));
358 decl = void_type_node;
361 else if (TREE_CODE (decl) == FUNCTION_DECL
362 && ((IDENTIFIER_LENGTH (declarator) == 4
363 && IDENTIFIER_POINTER (declarator)[0] == 'm'
364 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
365 || (IDENTIFIER_LENGTH (declarator) > 10
366 && IDENTIFIER_POINTER (declarator)[0] == '_'
367 && IDENTIFIER_POINTER (declarator)[1] == '_'
368 && strncmp (IDENTIFIER_POINTER (declarator)+2,
369 "builtin_", 8) == 0)))
371 /* raw "main", and builtin functions never gets overloaded,
372 but they can become friends. */
373 add_friend (current_class_type, decl);
374 DECL_FRIEND_P (decl) = 1;
375 decl = void_type_node;
378 @@ or possibly a friend from a base class ?!? */
379 else if (TREE_CODE (decl) == FUNCTION_DECL)
381 /* Friends must all go through the overload machinery,
382 even though they may not technically be overloaded.
384 Note that because classes all wind up being top-level
385 in their scope, their friend wind up in top-level scope as well. */
386 DECL_ASSEMBLER_NAME (decl)
387 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
388 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
389 DECL_ARGUMENTS (decl) = parmdecls;
391 DECL_CLASS_CONTEXT (decl) = current_class_type;
393 if (! DECL_USE_TEMPLATE (decl))
395 /* We can call pushdecl here, because the TREE_CHAIN of this
396 FUNCTION_DECL is not needed for other purposes. Don't do this
397 for a template instantiation. */
398 if (!is_friend_template)
400 /* However, we don't call pushdecl() for a friend
401 function of a template class, since in general,
402 such a declaration depends on template
403 parameters. Instead, we call pushdecl when the
404 class is instantiated. */
405 if (template_class_depth (current_class_type) == 0)
406 decl = pushdecl (decl);
409 decl = push_template_decl (decl);
411 if (! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
412 && current_template_parms && uses_template_parms (decl))
414 static int explained;
415 cp_warning ("friend declaration `%#D'", decl);
416 warning (" declares a non-template function");
419 warning (" unless you compile with -fguiding-decls");
420 warning (" or add <> after the function name");
426 make_decl_rtl (decl, NULL_PTR, 1);
427 add_friend (current_class_type,
428 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
429 DECL_FRIEND_P (decl) = 1;
433 /* @@ Should be able to ingest later definitions of this function
435 tree decl = lookup_name_nonclass (declarator);
436 if (decl == NULL_TREE)
438 warning ("implicitly declaring `%s' as struct",
439 IDENTIFIER_POINTER (declarator));
440 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
441 decl = TYPE_MAIN_DECL (decl);
444 /* Allow abbreviated declarations of overloaded functions,
445 but not if those functions are really class names. */
446 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
448 warning ("`friend %s' archaic, use `friend class %s' instead",
449 IDENTIFIER_POINTER (declarator),
450 IDENTIFIER_POINTER (declarator));
451 decl = TREE_TYPE (TREE_PURPOSE (decl));
454 if (TREE_CODE (decl) == TREE_LIST)
455 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
457 make_friend_class (current_class_type, TREE_TYPE (decl));
458 decl = void_type_node;