call.c (check_dtor_name): Replace abort with gcc_assert or gcc_unreachable.
[gcc.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "output.h"
32 #include "toplev.h"
33
34 /* Friend data structures are described in cp-tree.h. */
35
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
37
38 int
39 is_friend (tree type, tree supplicant)
40 {
41 int declp;
42 tree list;
43 tree context;
44
45 if (supplicant == NULL_TREE || type == NULL_TREE)
46 return 0;
47
48 declp = DECL_P (supplicant);
49
50 if (declp)
51 /* It's a function decl. */
52 {
53 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
54 tree name = DECL_NAME (supplicant);
55
56 for (; list ; list = TREE_CHAIN (list))
57 {
58 if (name == FRIEND_NAME (list))
59 {
60 tree friends = FRIEND_DECLS (list);
61 for (; friends ; friends = TREE_CHAIN (friends))
62 {
63 tree friend = TREE_VALUE (friends);
64
65 if (friend == NULL_TREE)
66 continue;
67
68 if (supplicant == friend)
69 return 1;
70
71 if (is_specialization_of_friend (supplicant, friend))
72 return 1;
73 }
74 break;
75 }
76 }
77 }
78 else
79 /* It's a type. */
80 {
81 /* Nested classes are implicitly friends of their enclosing types, as
82 per core issue 45 (this is a change from the standard). */
83 for (context = supplicant;
84 context && TYPE_P (context);
85 context = TYPE_CONTEXT (context))
86 if (type == context)
87 return 1;
88
89 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
90 for (; list ; list = TREE_CHAIN (list))
91 {
92 tree t = TREE_VALUE (list);
93
94 if (TREE_CODE (t) == TEMPLATE_DECL ?
95 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
96 same_type_p (supplicant, t))
97 return 1;
98 }
99 }
100
101 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
102 context = DECL_CONTEXT (supplicant);
103 else if (! declp)
104 /* Local classes have the same access as the enclosing function. */
105 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
106 else
107 context = NULL_TREE;
108
109 /* A namespace is not friend to anybody. */
110 if (context && TREE_CODE (context) == NAMESPACE_DECL)
111 context = NULL_TREE;
112
113 if (context)
114 return is_friend (type, context);
115
116 return 0;
117 }
118
119 /* Add a new friend to the friends of the aggregate type TYPE.
120 DECL is the FUNCTION_DECL of the friend being added.
121
122 If COMPLAIN is true, warning about duplicate friend is issued.
123 We want to have this diagnostics during parsing but not
124 when a template is being instantiated. */
125
126 void
127 add_friend (tree type, tree decl, bool complain)
128 {
129 tree typedecl;
130 tree list;
131 tree name;
132 tree ctx;
133
134 if (decl == error_mark_node)
135 return;
136
137 typedecl = TYPE_MAIN_DECL (type);
138 list = DECL_FRIENDLIST (typedecl);
139 name = DECL_NAME (decl);
140 type = TREE_TYPE (typedecl);
141
142 while (list)
143 {
144 if (name == FRIEND_NAME (list))
145 {
146 tree friends = FRIEND_DECLS (list);
147 for (; friends ; friends = TREE_CHAIN (friends))
148 {
149 if (decl == TREE_VALUE (friends))
150 {
151 if (complain)
152 warning ("`%D' is already a friend of class `%T'",
153 decl, type);
154 return;
155 }
156 }
157
158 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
159
160 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
161 TREE_VALUE (list));
162 return;
163 }
164 list = TREE_CHAIN (list);
165 }
166
167 ctx = DECL_CONTEXT (decl);
168 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
169 perform_or_defer_access_check (TYPE_BINFO (ctx), decl);
170
171 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
172
173 DECL_FRIENDLIST (typedecl)
174 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
175 DECL_FRIENDLIST (typedecl));
176 if (!uses_template_parms (type))
177 DECL_BEFRIENDING_CLASSES (decl)
178 = tree_cons (NULL_TREE, type,
179 DECL_BEFRIENDING_CLASSES (decl));
180 }
181
182 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
183 been defined, we make all of its member functions friends of
184 TYPE. If not, we make it a pending friend, which can later be added
185 when its definition is seen. If a type is defined, then its TYPE_DECL's
186 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
187 classes that are not defined. If a type has not yet been defined,
188 then the DECL_WAITING_FRIENDS contains a list of types
189 waiting to make it their friend. Note that these two can both
190 be in use at the same time!
191
192 If COMPLAIN is true, warning about duplicate friend is issued.
193 We want to have this diagnostics during parsing but not
194 when a template is being instantiated. */
195
196 void
197 make_friend_class (tree type, tree friend_type, bool complain)
198 {
199 tree classes;
200 int is_template_friend;
201
202 if (! IS_AGGR_TYPE (friend_type))
203 {
204 error ("invalid type `%T' declared `friend'", friend_type);
205 return;
206 }
207
208 if (processing_template_decl > template_class_depth (type))
209 /* If the TYPE is a template then it makes sense for it to be
210 friends with itself; this means that each instantiation is
211 friends with all other instantiations. */
212 {
213 if (CLASS_TYPE_P (friend_type)
214 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
215 && uses_template_parms (friend_type))
216 {
217 /* [temp.friend]
218 Friend declarations shall not declare partial
219 specializations. */
220 error ("partial specialization `%T' declared `friend'",
221 friend_type);
222 return;
223 }
224
225 is_template_friend = 1;
226 }
227 else if (same_type_p (type, friend_type))
228 {
229 if (complain)
230 pedwarn ("class `%T' is implicitly friends with itself",
231 type);
232 return;
233 }
234 else
235 is_template_friend = 0;
236
237 /* [temp.friend]
238
239 A friend of a class or class template can be a function or
240 class template, a specialization of a function template or
241 class template, or an ordinary (nontemplate) function or
242 class. */
243 if (!is_template_friend)
244 ;/* ok */
245 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
246 {
247 /* template <class T> friend typename S<T>::X; */
248 error ("typename type `%#T' declared `friend'", friend_type);
249 return;
250 }
251 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
252 {
253 /* template <class T> friend class T; */
254 error ("template parameter type `%T' declared `friend'", friend_type);
255 return;
256 }
257 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
258 {
259 /* template <class T> friend class A; where A is not a template */
260 error ("`%#T' is not a template", friend_type);
261 return;
262 }
263
264 if (is_template_friend)
265 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
266
267 /* See if it is already a friend. */
268 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
269 classes;
270 classes = TREE_CHAIN (classes))
271 {
272 tree probe = TREE_VALUE (classes);
273
274 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
275 {
276 if (friend_type == probe)
277 {
278 if (complain)
279 warning ("`%D' is already a friend of `%T'",
280 probe, type);
281 break;
282 }
283 }
284 else if (TREE_CODE (probe) != TEMPLATE_DECL)
285 {
286 if (same_type_p (probe, friend_type))
287 {
288 if (complain)
289 warning ("`%T' is already a friend of `%T'",
290 probe, type);
291 break;
292 }
293 }
294 }
295
296 if (!classes)
297 {
298 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
299
300 CLASSTYPE_FRIEND_CLASSES (type)
301 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
302 if (is_template_friend)
303 friend_type = TREE_TYPE (friend_type);
304 if (!uses_template_parms (type))
305 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
306 = tree_cons (NULL_TREE, type,
307 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
308 }
309 }
310
311 /* Main friend processor.
312
313 CTYPE is the class this friend belongs to.
314
315 DECLARATOR is the name of the friend.
316
317 DECL is the FUNCTION_DECL that the friend is.
318
319 FLAGS is just used for `grokclassfn'.
320
321 QUALS say what special qualifies should apply to the object
322 pointed to by `this'. */
323
324 tree
325 do_friend (tree ctype, tree declarator, tree decl,
326 tree attrlist, enum overload_flags flags,
327 cp_cv_quals quals,
328 int funcdef_flag)
329 {
330 /* Every decl that gets here is a friend of something. */
331 DECL_FRIEND_P (decl) = 1;
332
333 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
334 {
335 declarator = TREE_OPERAND (declarator, 0);
336 if (is_overloaded_fn (declarator))
337 declarator = DECL_NAME (get_first_fn (declarator));
338 }
339
340 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
341
342 if (ctype)
343 {
344 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
345 the enclosing class. FRIEND_DEPTH counts the number of template
346 headers used for this friend declaration. TEMPLATE_MEMBER_P is
347 true if a template header in FRIEND_DEPTH is intended for
348 DECLARATOR. For example, the code
349
350 template <class T> struct A {
351 template <class U> struct B {
352 template <class V> template <class W>
353 friend void C<V>::f(W);
354 };
355 };
356
357 will eventually give the following results
358
359 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
360 2. FRIEND_DEPTH equals 2 (for `V' and `W').
361 3. TEMPLATE_MEMBER_P is true (for `W'). */
362
363 int class_template_depth = template_class_depth (current_class_type);
364 int friend_depth = processing_template_decl - class_template_depth;
365 /* We will figure this out later. */
366 bool template_member_p = false;
367
368 tree cname = TYPE_NAME (ctype);
369 if (TREE_CODE (cname) == TYPE_DECL)
370 cname = DECL_NAME (cname);
371
372 /* A method friend. */
373 if (flags == NO_SPECIAL && declarator == cname)
374 DECL_CONSTRUCTOR_P (decl) = 1;
375
376 /* This will set up DECL_ARGUMENTS for us. */
377 grokclassfn (ctype, decl, flags, quals);
378
379 if (friend_depth)
380 {
381 if (!uses_template_parms_level (ctype, class_template_depth
382 + friend_depth))
383 template_member_p = true;
384 }
385
386 /* A nested class may declare a member of an enclosing class
387 to be a friend, so we do lookup here even if CTYPE is in
388 the process of being defined. */
389 if (class_template_depth
390 || COMPLETE_TYPE_P (ctype)
391 || TYPE_BEING_DEFINED (ctype))
392 {
393 if (DECL_TEMPLATE_INFO (decl))
394 /* DECL is a template specialization. No need to
395 build a new TEMPLATE_DECL. */
396 ;
397 else if (class_template_depth)
398 /* We rely on tsubst_friend_function to check the
399 validity of the declaration later. */
400 decl = push_template_decl_real (decl, /*is_friend=*/1);
401 else
402 decl = check_classfn (ctype, decl,
403 template_member_p
404 ? current_template_parms
405 : NULL_TREE);
406
407 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
408 decl = DECL_TI_TEMPLATE (decl);
409
410 if (decl)
411 add_friend (current_class_type, decl, /*complain=*/true);
412 }
413 else
414 error ("member `%D' declared as friend before type `%T' defined",
415 decl, ctype);
416 }
417 /* A global friend.
418 @@ or possibly a friend from a base class ?!? */
419 else if (TREE_CODE (decl) == FUNCTION_DECL)
420 {
421 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
422
423 /* Friends must all go through the overload machinery,
424 even though they may not technically be overloaded.
425
426 Note that because classes all wind up being top-level
427 in their scope, their friend wind up in top-level scope as well. */
428 if (funcdef_flag)
429 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
430
431 if (! DECL_USE_TEMPLATE (decl))
432 {
433 /* We must check whether the decl refers to template
434 arguments before push_template_decl_real adds a
435 reference to the containing template class. */
436 int warn = (warn_nontemplate_friend
437 && ! funcdef_flag && ! is_friend_template
438 && current_template_parms
439 && uses_template_parms (decl));
440
441 if (is_friend_template
442 || template_class_depth (current_class_type) != 0)
443 /* We can't call pushdecl for a template class, since in
444 general, such a declaration depends on template
445 parameters. Instead, we call pushdecl when the class
446 is instantiated. */
447 decl = push_template_decl_real (decl, /*is_friend=*/1);
448 else if (current_function_decl)
449 /* This must be a local class, so pushdecl will be ok, and
450 insert an unqualified friend into the local scope
451 (rather than the containing namespace scope, which the
452 next choice will do). */
453 decl = pushdecl (decl);
454 else
455 {
456 /* We can't use pushdecl, as we might be in a template
457 class specialization, and pushdecl will insert an
458 unqualified friend decl into the template parameter
459 scope, rather than the namespace containing it. */
460 tree ns = decl_namespace_context (decl);
461
462 push_nested_namespace (ns);
463 decl = pushdecl_namespace_level (decl);
464 pop_nested_namespace (ns);
465 }
466
467 if (warn)
468 {
469 static int explained;
470 warning ("friend declaration `%#D' declares a non-template function", decl);
471 if (! explained)
472 {
473 warning ("(if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning");
474 explained = 1;
475 }
476 }
477 }
478
479 if (decl == error_mark_node)
480 return error_mark_node;
481
482 add_friend (current_class_type,
483 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
484 /*complain=*/true);
485 DECL_FRIEND_P (decl) = 1;
486 }
487
488 /* Unfortunately, we have to handle attributes here. Normally we would
489 handle them in start_decl_1, but since this is a friend decl start_decl_1
490 never gets to see it. */
491
492 /* Set attributes here so if duplicate decl, will have proper attributes. */
493 cplus_decl_attributes (&decl, attrlist, 0);
494
495 return decl;
496 }