1999-10-04 Mark Mitchell <mark@codesourcery.com>
[official-gcc.git] / gcc / cp / friend.c
blob410eaf5c82bfc9492cc4258c9072510437b857a0
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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "tree.h"
24 #include "rtl.h"
25 #include "cp-tree.h"
26 #include "flags.h"
27 #include "output.h"
28 #include "toplev.h"
30 /* Friend data structures are described in cp-tree.h. */
32 /* Returns non-zero if SUPPLICANT is a friend of TYPE. */
34 int
35 is_friend (type, supplicant)
36 tree type, supplicant;
38 int declp;
39 register tree list;
40 tree context;
42 if (supplicant == NULL_TREE || type == NULL_TREE)
43 return 0;
45 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
47 if (declp)
48 /* It's a function decl. */
50 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
51 tree name = DECL_NAME (supplicant);
52 tree ctype;
54 if (DECL_FUNCTION_MEMBER_P (supplicant))
55 ctype = DECL_CLASS_CONTEXT (supplicant);
56 else
57 ctype = NULL_TREE;
59 for (; list ; list = TREE_CHAIN (list))
61 if (name == FRIEND_NAME (list))
63 tree friends = FRIEND_DECLS (list);
64 for (; friends ; friends = TREE_CHAIN (friends))
66 if (same_type_p (ctype, TREE_PURPOSE (friends)))
67 return 1;
69 if (TREE_VALUE (friends) == NULL_TREE)
70 continue;
72 if (supplicant == TREE_VALUE (friends))
73 return 1;
75 /* With -fguiding-decls we are more lenient about
76 friendship. This is bogus in general since two
77 specializations of a template with non-type
78 template parameters may have the same type, but
79 be different.
81 Temporarily, we are also more lenient to deal
82 with nested friend functions, for which there can
83 be more than one FUNCTION_DECL, despite being the
84 same function. When that's fixed, the
85 FUNCTION_MEMBER_P bit can go. */
86 if ((flag_guiding_decls
87 || DECL_FUNCTION_MEMBER_P (supplicant))
88 && same_type_p (TREE_TYPE (supplicant),
89 TREE_TYPE (TREE_VALUE (friends))))
90 return 1;
92 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
93 && is_specialization_of (supplicant,
94 TREE_VALUE (friends)))
95 return 1;
97 break;
101 else
102 /* It's a type. */
104 if (type == supplicant)
105 return 1;
107 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
108 for (; list ; list = TREE_CHAIN (list))
110 tree t = TREE_VALUE (list);
112 if (TREE_CODE (t) == TEMPLATE_DECL ?
113 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
114 same_type_p (supplicant, t))
115 return 1;
119 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
120 context = DECL_CLASS_CONTEXT (supplicant);
121 else if (! declp)
122 /* Local classes have the same access as the enclosing function. */
123 context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
124 else
125 context = NULL_TREE;
127 /* A namespace is not friend to anybody. */
128 if (context && TREE_CODE (context) == NAMESPACE_DECL)
129 context = NULL_TREE;
131 if (context)
132 return is_friend (type, context);
134 return 0;
137 /* Add a new friend to the friends of the aggregate type TYPE.
138 DECL is the FUNCTION_DECL of the friend being added. */
140 void
141 add_friend (type, decl)
142 tree type, decl;
144 tree typedecl;
145 tree list;
146 tree name;
148 if (decl == error_mark_node)
149 return;
151 typedecl = TYPE_MAIN_DECL (type);
152 list = DECL_FRIENDLIST (typedecl);
153 name = DECL_NAME (decl);
154 type = TREE_TYPE (typedecl);
156 while (list)
158 if (name == FRIEND_NAME (list))
160 tree friends = FRIEND_DECLS (list);
161 for (; friends ; friends = TREE_CHAIN (friends))
163 if (decl == TREE_VALUE (friends))
165 cp_warning ("`%D' is already a friend of class `%T'",
166 decl, type);
167 cp_warning_at ("previous friend declaration of `%D'",
168 TREE_VALUE (friends));
169 return;
172 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
173 TREE_VALUE (list));
174 return;
176 list = TREE_CHAIN (list);
179 DECL_FRIENDLIST (typedecl)
180 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
181 DECL_FRIENDLIST (typedecl));
182 if (!uses_template_parms (type))
183 DECL_BEFRIENDING_CLASSES (decl)
184 = tree_cons (NULL_TREE, type,
185 DECL_BEFRIENDING_CLASSES (decl));
188 /* Declare that every member function NAME in FRIEND_TYPE
189 (which may be NULL_TREE) is a friend of type TYPE. */
191 void
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);
198 while (list)
200 if (name == FRIEND_NAME (list))
202 tree friends = FRIEND_DECLS (list);
203 while (friends && TREE_PURPOSE (friends) != friend_type)
204 friends = TREE_CHAIN (friends);
205 if (friends)
207 if (friend_type)
208 warning ("method `%s::%s' is already a friend of class",
209 TYPE_NAME_STRING (friend_type),
210 IDENTIFIER_POINTER (name));
211 else
212 warning ("function `%s' is already a friend of class `%s'",
213 IDENTIFIER_POINTER (name),
214 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
216 else
217 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
218 TREE_VALUE (list));
219 return;
221 list = TREE_CHAIN (list);
223 DECL_FRIENDLIST (typedecl)
224 = tree_cons (name,
225 build_tree_list (friend_type, NULL_TREE),
226 DECL_FRIENDLIST (typedecl));
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! */
239 void
240 make_friend_class (type, friend_type)
241 tree type, friend_type;
243 tree classes;
244 int is_template_friend;
246 if (! IS_AGGR_TYPE (friend_type))
248 cp_error ("invalid type `%T' declared `friend'", friend_type);
249 return;
252 if (CLASS_TYPE_P (friend_type)
253 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
254 && uses_template_parms (friend_type))
256 /* [temp.friend]
258 Friend declarations shall not declare partial
259 specializations. */
260 cp_error ("partial specialization `%T' declared `friend'",
261 friend_type);
262 return;
265 if (processing_template_decl > template_class_depth (type))
266 /* If the TYPE is a template then it makes sense for it to be
267 friends with itself; this means that each instantiation is
268 friends with all other instantiations. */
269 is_template_friend = 1;
270 else if (same_type_p (type, friend_type))
272 pedwarn ("class `%s' is implicitly friends with itself",
273 TYPE_NAME_STRING (type));
274 return;
276 else
277 is_template_friend = 0;
279 if (is_template_friend
280 && TREE_CODE (friend_type) == TYPENAME_TYPE)
282 /* [temp.friend]
284 A friend of a class or class template can be a function or
285 class template, a specialization of a function template or
286 class template, or an ordinary (nontemplate) function or
287 class.
289 But, we're looking at something like:
291 template <class T> friend typename S<T>::X;
293 which isn't any of these. */
294 cp_error ("typename type `%T' declared `friend'",
295 friend_type);
296 return;
299 GNU_xref_hier (type, friend_type, 0, 0, 1);
301 if (is_template_friend)
302 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
304 classes = CLASSTYPE_FRIEND_CLASSES (type);
305 while (classes
306 /* Stop if we find the same type on the list. */
307 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
308 friend_type == TREE_VALUE (classes) :
309 same_type_p (TREE_VALUE (classes), friend_type)))
310 classes = TREE_CHAIN (classes);
311 if (classes)
312 cp_warning ("`%T' is already a friend of `%T'",
313 TREE_VALUE (classes), type);
314 else
316 CLASSTYPE_FRIEND_CLASSES (type)
317 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
318 if (is_template_friend)
319 friend_type = TREE_TYPE (friend_type);
320 if (!uses_template_parms (type))
321 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
322 = tree_cons (NULL_TREE, type,
323 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
327 /* Main friend processor. This is large, and for modularity purposes,
328 has been removed from grokdeclarator. It returns `void_type_node'
329 to indicate that something happened, though a FIELD_DECL is
330 not returned.
332 CTYPE is the class this friend belongs to.
334 DECLARATOR is the name of the friend.
336 DECL is the FUNCTION_DECL that the friend is.
338 In case we are parsing a friend which is part of an inline
339 definition, we will need to store PARM_DECL chain that comes
340 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
342 FLAGS is just used for `grokclassfn'.
344 QUALS say what special qualifies should apply to the object
345 pointed to by `this'. */
347 tree
348 do_friend (ctype, declarator, decl, parmdecls, attrlist,
349 flags, quals, funcdef_flag)
350 tree ctype, declarator, decl, parmdecls, attrlist;
351 enum overload_flags flags;
352 tree quals;
353 int funcdef_flag;
355 int is_friend_template = 0;
356 tree prefix_attributes, attributes;
358 /* Every decl that gets here is a friend of something. */
359 DECL_FRIEND_P (decl) = 1;
361 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
363 declarator = TREE_OPERAND (declarator, 0);
364 if (TREE_CODE (declarator) == LOOKUP_EXPR)
365 declarator = TREE_OPERAND (declarator, 0);
366 if (is_overloaded_fn (declarator))
367 declarator = DECL_NAME (get_first_fn (declarator));
370 if (TREE_CODE (decl) != FUNCTION_DECL)
371 my_friendly_abort (990513);
373 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
375 if (ctype)
377 tree cname = TYPE_NAME (ctype);
378 if (TREE_CODE (cname) == TYPE_DECL)
379 cname = DECL_NAME (cname);
381 /* A method friend. */
382 if (flags == NO_SPECIAL && ctype && declarator == cname)
383 DECL_CONSTRUCTOR_P (decl) = 1;
385 /* This will set up DECL_ARGUMENTS for us. */
386 grokclassfn (ctype, decl, flags, quals);
388 if (is_friend_template)
389 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
390 else if (template_class_depth (current_class_type))
391 decl = push_template_decl_real (decl, /*is_friend=*/1);
393 /* We can't do lookup in a type that involves template
394 parameters. Instead, we rely on tsubst_friend_function
395 to check the validity of the declaration later. */
396 if (uses_template_parms (ctype))
397 add_friend (current_class_type, decl);
398 /* A nested class may declare a member of an enclosing class
399 to be a friend, so we do lookup here even if CTYPE is in
400 the process of being defined. */
401 else if (TYPE_SIZE (ctype) != 0 || TYPE_BEING_DEFINED (ctype))
403 decl = check_classfn (ctype, decl);
405 if (decl)
406 add_friend (current_class_type, decl);
408 else
409 cp_error ("member `%D' declared as friend before type `%T' defined",
410 decl, ctype);
412 /* A global friend.
413 @@ or possibly a friend from a base class ?!? */
414 else if (TREE_CODE (decl) == FUNCTION_DECL)
416 /* Friends must all go through the overload machinery,
417 even though they may not technically be overloaded.
419 Note that because classes all wind up being top-level
420 in their scope, their friend wind up in top-level scope as well. */
421 DECL_ARGUMENTS (decl) = parmdecls;
422 if (funcdef_flag)
423 DECL_CLASS_CONTEXT (decl) = current_class_type;
425 if (! DECL_USE_TEMPLATE (decl))
427 /* We can call pushdecl here, because the TREE_CHAIN of this
428 FUNCTION_DECL is not needed for other purposes. Don't do
429 this for a template instantiation. However, we don't
430 call pushdecl() for a friend function of a template
431 class, since in general, such a declaration depends on
432 template parameters. Instead, we call pushdecl when the
433 class is instantiated. */
434 if (!is_friend_template
435 && template_class_depth (current_class_type) == 0)
436 decl = pushdecl (decl);
437 else
438 decl = push_template_decl_real (decl, /*is_friend=*/1);
440 if (warn_nontemplate_friend
441 && ! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
442 && current_template_parms && uses_template_parms (decl))
444 static int explained;
445 cp_warning ("friend declaration `%#D'", decl);
446 warning (" declares a non-template function");
447 if (! explained)
449 warning (" (if this is not what you intended, make sure");
450 warning (" the function template has already been declared,");
451 warning (" and add <> after the function name here)");
452 warning (" -Wno-non-template-friend disables this warning.");
453 explained = 1;
458 make_decl_rtl (decl, NULL_PTR, 1);
459 add_friend (current_class_type,
460 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
461 DECL_FRIEND_P (decl) = 1;
464 /* Unfortunately, we have to handle attributes here. Normally we would
465 handle them in start_decl_1, but since this is a friend decl start_decl_1
466 never gets to see it. */
468 if (attrlist)
470 attributes = TREE_PURPOSE (attrlist);
471 prefix_attributes = TREE_VALUE (attrlist);
473 else
475 attributes = NULL_TREE;
476 prefix_attributes = NULL_TREE;
479 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
480 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
481 #endif
483 /* Set attributes here so if duplicate decl, will have proper attributes. */
484 cplus_decl_attributes (decl, attributes, prefix_attributes);
486 return decl;