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 (ctype == TREE_PURPOSE (friends))
70 if (TREE_VALUE (friends) == NULL_TREE)
73 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL)
75 if (is_specialization_of (supplicant,
76 TREE_VALUE (friends)))
82 /* FIXME: The use of comptypes here is bogus, since
83 two specializations of a template with non-type
84 parameters may have the same type, but be
86 if (comptypes (TREE_TYPE (supplicant),
87 TREE_TYPE (TREE_VALUE (friends)), 1))
97 if (type == supplicant)
100 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
101 for (; list ; list = TREE_CHAIN (list))
103 tree t = TREE_VALUE (list);
106 || (CLASSTYPE_IS_TEMPLATE (t)
107 && is_specialization_of (TYPE_MAIN_DECL (supplicant),
108 CLASSTYPE_TI_TEMPLATE (t))))
113 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
114 context = DECL_CLASS_CONTEXT (supplicant);
116 /* Local classes have the same access as the enclosing function. */
117 context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
121 /* A namespace is not friend to anybody. */
122 if (context && TREE_CODE (context) == NAMESPACE_DECL)
126 return is_friend (type, context);
131 /* Add a new friend to the friends of the aggregate type TYPE.
132 DECL is the FUNCTION_DECL of the friend being added. */
135 add_friend (type, decl)
138 tree typedecl = TYPE_MAIN_DECL (type);
139 tree list = DECL_FRIENDLIST (typedecl);
140 tree name = DECL_NAME (decl);
144 if (name == TREE_PURPOSE (list))
146 tree friends = TREE_VALUE (list);
147 for (; friends ; friends = TREE_CHAIN (friends))
149 if (decl == TREE_VALUE (friends))
151 cp_warning ("`%D' is already a friend of class `%T'",
153 cp_warning_at ("previous friend declaration of `%D'",
154 TREE_VALUE (friends));
158 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
162 list = TREE_CHAIN (list);
164 DECL_FRIENDLIST (typedecl)
165 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
166 DECL_FRIENDLIST (typedecl));
167 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
169 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
170 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
171 if (parmtypes && TREE_CHAIN (parmtypes))
173 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
174 if (TREE_CODE (parmtype) == REFERENCE_TYPE
175 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
176 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
181 /* Declare that every member function NAME in FRIEND_TYPE
182 (which may be NULL_TREE) is a friend of type TYPE. */
185 add_friends (type, name, friend_type)
186 tree type, name, friend_type;
188 tree typedecl = TYPE_MAIN_DECL (type);
189 tree list = DECL_FRIENDLIST (typedecl);
193 if (name == TREE_PURPOSE (list))
195 tree friends = TREE_VALUE (list);
196 while (friends && TREE_PURPOSE (friends) != friend_type)
197 friends = TREE_CHAIN (friends);
201 warning ("method `%s::%s' is already a friend of class",
202 TYPE_NAME_STRING (friend_type),
203 IDENTIFIER_POINTER (name));
205 warning ("function `%s' is already a friend of class `%s'",
206 IDENTIFIER_POINTER (name),
207 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
210 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
214 list = TREE_CHAIN (list);
216 DECL_FRIENDLIST (typedecl)
218 build_tree_list (friend_type, NULL_TREE),
219 DECL_FRIENDLIST (typedecl));
220 if (! strncmp (IDENTIFIER_POINTER (name),
221 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
222 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
224 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
225 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
229 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
230 been defined, we make all of its member functions friends of
231 TYPE. If not, we make it a pending friend, which can later be added
232 when its definition is seen. If a type is defined, then its TYPE_DECL's
233 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
234 classes that are not defined. If a type has not yet been defined,
235 then the DECL_WAITING_FRIENDS contains a list of types
236 waiting to make it their friend. Note that these two can both
237 be in use at the same time! */
240 make_friend_class (type, friend_type)
241 tree type, friend_type;
245 if (IS_SIGNATURE (type))
247 error ("`friend' declaration in signature definition");
250 if (IS_SIGNATURE (friend_type))
252 error ("signature type `%s' declared `friend'",
253 IDENTIFIER_POINTER (TYPE_IDENTIFIER (friend_type)));
256 /* If the TYPE is a template then it makes sense for it to be
257 friends with itself; this means that each instantiation is
258 friends with all other instantiations. */
259 if (type == friend_type && !CLASSTYPE_IS_TEMPLATE (type))
261 pedwarn ("class `%s' is implicitly friends with itself",
262 TYPE_NAME_STRING (type));
266 GNU_xref_hier (TYPE_NAME_STRING (type),
267 TYPE_NAME_STRING (friend_type), 0, 0, 1);
269 classes = CLASSTYPE_FRIEND_CLASSES (type);
270 while (classes && TREE_VALUE (classes) != friend_type)
271 classes = TREE_CHAIN (classes);
273 warning ("class `%s' is already friends with class `%s'",
274 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
277 CLASSTYPE_FRIEND_CLASSES (type)
278 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
282 /* Main friend processor. This is large, and for modularity purposes,
283 has been removed from grokdeclarator. It returns `void_type_node'
284 to indicate that something happened, though a FIELD_DECL is
287 CTYPE is the class this friend belongs to.
289 DECLARATOR is the name of the friend.
291 DECL is the FUNCTION_DECL that the friend is.
293 In case we are parsing a friend which is part of an inline
294 definition, we will need to store PARM_DECL chain that comes
295 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
297 FLAGS is just used for `grokclassfn'.
299 QUALS say what special qualifies should apply to the object
300 pointed to by `this'. */
303 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
304 tree ctype, declarator, decl, parmdecls;
305 enum overload_flags flags;
309 int is_friend_template = 0;
311 /* Every decl that gets here is a friend of something. */
312 DECL_FRIEND_P (decl) = 1;
314 if (TREE_CODE (decl) == FUNCTION_DECL)
315 is_friend_template = processing_template_decl >
316 template_class_depth (current_class_type);
320 tree cname = TYPE_NAME (ctype);
321 if (TREE_CODE (cname) == TYPE_DECL)
322 cname = DECL_NAME (cname);
324 /* A method friend. */
325 if (TREE_CODE (decl) == FUNCTION_DECL)
327 if (flags == NO_SPECIAL && ctype && declarator == cname)
328 DECL_CONSTRUCTOR_P (decl) = 1;
330 /* This will set up DECL_ARGUMENTS for us. */
331 grokclassfn (ctype, cname, decl, flags, quals);
333 if (is_friend_template)
334 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
336 if (TYPE_SIZE (ctype) != 0
337 && template_class_depth (ctype) == 0)
338 decl = check_classfn (ctype, decl);
340 if (TREE_TYPE (decl) != error_mark_node)
342 if (TYPE_SIZE (ctype) || template_class_depth (ctype) > 0)
343 add_friend (current_class_type, decl);
345 cp_error ("member `%D' declared as friend before type `%T' defined",
351 /* Possibly a bunch of method friends. */
353 /* Get the class they belong to. */
354 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
355 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
358 add_friends (current_class_type, declarator, ctype);
360 error ("method `%s' is not a member of class `%s'",
361 IDENTIFIER_POINTER (declarator),
362 IDENTIFIER_POINTER (cname));
363 decl = void_type_node;
366 else if (TREE_CODE (decl) == FUNCTION_DECL
367 && (MAIN_NAME_P (declarator)
368 || (IDENTIFIER_LENGTH (declarator) > 10
369 && IDENTIFIER_POINTER (declarator)[0] == '_'
370 && IDENTIFIER_POINTER (declarator)[1] == '_'
371 && strncmp (IDENTIFIER_POINTER (declarator)+2,
372 "builtin_", 8) == 0)))
374 /* raw "main", and builtin functions never gets overloaded,
375 but they can become friends. */
376 add_friend (current_class_type, decl);
377 DECL_FRIEND_P (decl) = 1;
378 decl = void_type_node;
381 @@ or possibly a friend from a base class ?!? */
382 else if (TREE_CODE (decl) == FUNCTION_DECL)
384 /* Friends must all go through the overload machinery,
385 even though they may not technically be overloaded.
387 Note that because classes all wind up being top-level
388 in their scope, their friend wind up in top-level scope as well. */
389 DECL_ASSEMBLER_NAME (decl)
390 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
391 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
392 DECL_ARGUMENTS (decl) = parmdecls;
394 DECL_CLASS_CONTEXT (decl) = current_class_type;
396 if (! DECL_USE_TEMPLATE (decl))
398 /* We can call pushdecl here, because the TREE_CHAIN of this
399 FUNCTION_DECL is not needed for other purposes. Don't do this
400 for a template instantiation. */
401 if (!is_friend_template)
403 /* However, we don't call pushdecl() for a friend
404 function of a template class, since in general,
405 such a declaration depends on template
406 parameters. Instead, we call pushdecl when the
407 class is instantiated. */
408 if (template_class_depth (current_class_type) == 0)
409 decl = pushdecl (decl);
412 decl = push_template_decl (decl);
414 if (! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
415 && current_template_parms && uses_template_parms (decl))
417 static int explained;
418 cp_warning ("friend declaration `%#D'", decl);
419 warning (" declares a non-template function");
422 warning (" unless you compile with -fguiding-decls");
423 warning (" or add <> after the function name");
429 make_decl_rtl (decl, NULL_PTR, 1);
430 add_friend (current_class_type,
431 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
432 DECL_FRIEND_P (decl) = 1;
436 /* @@ Should be able to ingest later definitions of this function
438 tree decl = lookup_name_nonclass (declarator);
439 if (decl == NULL_TREE)
441 warning ("implicitly declaring `%s' as struct",
442 IDENTIFIER_POINTER (declarator));
443 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
444 decl = TYPE_MAIN_DECL (decl);
447 /* Allow abbreviated declarations of overloaded functions,
448 but not if those functions are really class names. */
449 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
451 warning ("`friend %s' archaic, use `friend class %s' instead",
452 IDENTIFIER_POINTER (declarator),
453 IDENTIFIER_POINTER (declarator));
454 decl = TREE_TYPE (TREE_PURPOSE (decl));
457 if (TREE_CODE (decl) == TREE_LIST)
458 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
460 make_friend_class (current_class_type, TREE_TYPE (decl));
461 decl = void_type_node;