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 (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);
105 if (TREE_CODE (t) == TEMPLATE_DECL ?
106 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
107 comptypes (supplicant, t, 1))
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));
120 /* A namespace is not friend to anybody. */
121 if (context && TREE_CODE (context) == NAMESPACE_DECL)
125 return is_friend (type, context);
130 /* Add a new friend to the friends of the aggregate type TYPE.
131 DECL is the FUNCTION_DECL of the friend being added. */
134 add_friend (type, decl)
137 tree typedecl = TYPE_MAIN_DECL (type);
138 tree list = DECL_FRIENDLIST (typedecl);
139 tree name = DECL_NAME (decl);
143 if (name == TREE_PURPOSE (list))
145 tree friends = TREE_VALUE (list);
146 for (; friends ; friends = TREE_CHAIN (friends))
148 if (decl == TREE_VALUE (friends))
150 cp_warning ("`%D' is already a friend of class `%T'",
152 cp_warning_at ("previous friend declaration of `%D'",
153 TREE_VALUE (friends));
157 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
161 list = TREE_CHAIN (list);
163 DECL_FRIENDLIST (typedecl)
164 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
165 DECL_FRIENDLIST (typedecl));
166 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
168 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
169 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
170 if (parmtypes && TREE_CHAIN (parmtypes))
172 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
173 if (TREE_CODE (parmtype) == REFERENCE_TYPE
174 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
175 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
180 /* Declare that every member function NAME in FRIEND_TYPE
181 (which may be NULL_TREE) is a friend of type TYPE. */
184 add_friends (type, name, friend_type)
185 tree type, name, friend_type;
187 tree typedecl = TYPE_MAIN_DECL (type);
188 tree list = DECL_FRIENDLIST (typedecl);
192 if (name == TREE_PURPOSE (list))
194 tree friends = TREE_VALUE (list);
195 while (friends && TREE_PURPOSE (friends) != friend_type)
196 friends = TREE_CHAIN (friends);
200 warning ("method `%s::%s' is already a friend of class",
201 TYPE_NAME_STRING (friend_type),
202 IDENTIFIER_POINTER (name));
204 warning ("function `%s' is already a friend of class `%s'",
205 IDENTIFIER_POINTER (name),
206 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
209 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
213 list = TREE_CHAIN (list);
215 DECL_FRIENDLIST (typedecl)
217 build_tree_list (friend_type, NULL_TREE),
218 DECL_FRIENDLIST (typedecl));
219 if (! strncmp (IDENTIFIER_POINTER (name),
220 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
221 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
223 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
224 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
228 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
229 been defined, we make all of its member functions friends of
230 TYPE. If not, we make it a pending friend, which can later be added
231 when its definition is seen. If a type is defined, then its TYPE_DECL's
232 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
233 classes that are not defined. If a type has not yet been defined,
234 then the DECL_WAITING_FRIENDS contains a list of types
235 waiting to make it their friend. Note that these two can both
236 be in use at the same time! */
239 make_friend_class (type, friend_type)
240 tree type, friend_type;
243 int is_template_friend;
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 (processing_template_decl > template_class_depth (type))
257 /* If the TYPE is a template then it makes sense for it to be
258 friends with itself; this means that each instantiation is
259 friends with all other instantiations. */
260 is_template_friend = 1;
261 else if (comptypes (type, friend_type, 1))
263 pedwarn ("class `%s' is implicitly friends with itself",
264 TYPE_NAME_STRING (type));
268 is_template_friend = 0;
270 GNU_xref_hier (type, friend_type, 0, 0, 1);
272 if (is_template_friend)
273 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
275 classes = CLASSTYPE_FRIEND_CLASSES (type);
277 /* Stop if we find the same type on the list. */
278 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
279 friend_type == TREE_VALUE (classes) :
280 comptypes (TREE_VALUE (classes), friend_type, 1)))
281 classes = TREE_CHAIN (classes);
283 cp_warning ("`%T' is already a friend of `%T'",
284 TREE_VALUE (classes), type);
287 CLASSTYPE_FRIEND_CLASSES (type)
288 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
292 /* Main friend processor. This is large, and for modularity purposes,
293 has been removed from grokdeclarator. It returns `void_type_node'
294 to indicate that something happened, though a FIELD_DECL is
297 CTYPE is the class this friend belongs to.
299 DECLARATOR is the name of the friend.
301 DECL is the FUNCTION_DECL that the friend is.
303 In case we are parsing a friend which is part of an inline
304 definition, we will need to store PARM_DECL chain that comes
305 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
307 FLAGS is just used for `grokclassfn'.
309 QUALS say what special qualifies should apply to the object
310 pointed to by `this'. */
313 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
314 tree ctype, declarator, decl, parmdecls;
315 enum overload_flags flags;
319 int is_friend_template = 0;
321 /* Every decl that gets here is a friend of something. */
322 DECL_FRIEND_P (decl) = 1;
324 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
326 declarator = TREE_OPERAND (declarator, 0);
327 if (TREE_CODE (declarator) == LOOKUP_EXPR)
328 declarator = TREE_OPERAND (declarator, 0);
329 if (is_overloaded_fn (declarator))
330 declarator = DECL_NAME (get_first_fn (declarator));
333 if (TREE_CODE (decl) == FUNCTION_DECL)
334 is_friend_template = processing_template_decl >
335 template_class_depth (current_class_type);
339 tree cname = TYPE_NAME (ctype);
340 if (TREE_CODE (cname) == TYPE_DECL)
341 cname = DECL_NAME (cname);
343 /* A method friend. */
344 if (TREE_CODE (decl) == FUNCTION_DECL)
346 if (flags == NO_SPECIAL && ctype && declarator == cname)
347 DECL_CONSTRUCTOR_P (decl) = 1;
349 /* This will set up DECL_ARGUMENTS for us. */
350 grokclassfn (ctype, cname, decl, flags, quals);
352 if (is_friend_template)
353 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
355 if (TYPE_SIZE (ctype) != 0 && template_class_depth (ctype) == 0)
356 decl = check_classfn (ctype, decl);
358 /* TYPE_BEING_DEFINED is a hack for nested classes having
359 member functions of the enclosing class as friends. Will
360 go away as parsing of classes gets rewritten. */
361 if (TREE_TYPE (decl) != error_mark_node)
363 if (TYPE_BEING_DEFINED (ctype) ||
364 TYPE_SIZE (ctype) || template_class_depth (ctype) > 0)
365 add_friend (current_class_type, decl);
367 cp_error ("member `%D' declared as friend before type `%T' defined",
373 /* Possibly a bunch of method friends. */
375 /* Get the class they belong to. */
376 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
377 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
380 add_friends (current_class_type, declarator, ctype);
382 cp_error ("method `%D' is not a member of class `%T'",
384 decl = void_type_node;
387 else if (TREE_CODE (decl) == FUNCTION_DECL
388 && (MAIN_NAME_P (declarator)
389 || (IDENTIFIER_LENGTH (declarator) > 10
390 && IDENTIFIER_POINTER (declarator)[0] == '_'
391 && IDENTIFIER_POINTER (declarator)[1] == '_'
392 && strncmp (IDENTIFIER_POINTER (declarator)+2,
393 "builtin_", 8) == 0)))
395 /* raw "main", and builtin functions never gets overloaded,
396 but they can become friends. */
397 add_friend (current_class_type, decl);
398 DECL_FRIEND_P (decl) = 1;
399 decl = void_type_node;
402 @@ or possibly a friend from a base class ?!? */
403 else if (TREE_CODE (decl) == FUNCTION_DECL)
405 /* Friends must all go through the overload machinery,
406 even though they may not technically be overloaded.
408 Note that because classes all wind up being top-level
409 in their scope, their friend wind up in top-level scope as well. */
410 DECL_ASSEMBLER_NAME (decl)
411 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
412 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
413 DECL_ARGUMENTS (decl) = parmdecls;
415 DECL_CLASS_CONTEXT (decl) = current_class_type;
417 if (! DECL_USE_TEMPLATE (decl))
419 /* We can call pushdecl here, because the TREE_CHAIN of this
420 FUNCTION_DECL is not needed for other purposes. Don't do this
421 for a template instantiation. */
422 if (!is_friend_template)
424 /* However, we don't call pushdecl() for a friend
425 function of a template class, since in general,
426 such a declaration depends on template
427 parameters. Instead, we call pushdecl when the
428 class is instantiated. */
429 if (template_class_depth (current_class_type) == 0)
430 decl = pushdecl (decl);
433 decl = push_template_decl (decl);
435 if (! 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)");
451 make_decl_rtl (decl, NULL_PTR, 1);
452 add_friend (current_class_type,
453 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
454 DECL_FRIEND_P (decl) = 1;
458 /* @@ Should be able to ingest later definitions of this function
460 tree decl = lookup_name_nonclass (declarator);
461 if (decl == NULL_TREE)
463 cp_warning ("implicitly declaring `%T' as struct", declarator);
464 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
465 decl = TYPE_MAIN_DECL (decl);
468 /* Allow abbreviated declarations of overloaded functions,
469 but not if those functions are really class names. */
470 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
472 cp_warning ("`friend %T' archaic, use `friend class %T' instead",
473 declarator, declarator);
474 decl = TREE_TYPE (TREE_PURPOSE (decl));
477 if (TREE_CODE (decl) == TREE_LIST)
478 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
480 make_friend_class (current_class_type, TREE_TYPE (decl));
481 decl = void_type_node;