call.c (enforce_access): Assert we get a binfo.
[gcc.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (type, supplicant)
40 tree type, supplicant;
41 {
42 int declp;
43 register tree list;
44 tree context;
45
46 if (supplicant == NULL_TREE || type == NULL_TREE)
47 return 0;
48
49 declp = DECL_P (supplicant);
50
51 if (declp)
52 /* It's a function decl. */
53 {
54 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
55 tree name = DECL_NAME (supplicant);
56
57 for (; list ; list = TREE_CHAIN (list))
58 {
59 if (name == FRIEND_NAME (list))
60 {
61 tree friends = FRIEND_DECLS (list);
62 for (; friends ; friends = TREE_CHAIN (friends))
63 {
64 if (TREE_VALUE (friends) == NULL_TREE)
65 continue;
66
67 if (supplicant == TREE_VALUE (friends))
68 return 1;
69
70 /* Temporarily, we are more lenient to deal with
71 nested friend functions, for which there can be
72 more than one FUNCTION_DECL, despite being the
73 same function. When that's fixed, this bit can
74 go. */
75 if (DECL_FUNCTION_MEMBER_P (supplicant)
76 && same_type_p (TREE_TYPE (supplicant),
77 TREE_TYPE (TREE_VALUE (friends))))
78 return 1;
79
80 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
81 && is_specialization_of (supplicant,
82 TREE_VALUE (friends)))
83 return 1;
84 }
85 break;
86 }
87 }
88 }
89 else
90 /* It's a type. */
91 {
92 /* Nested classes are implicitly friends of their enclosing types, as
93 per core issue 45 (this is a change from the standard). */
94 for (context = supplicant;
95 context && TYPE_P (context);
96 context = TYPE_CONTEXT (context))
97 if (type == context)
98 return 1;
99
100 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
101 for (; list ; list = TREE_CHAIN (list))
102 {
103 tree t = TREE_VALUE (list);
104
105 if (TREE_CODE (t) == TEMPLATE_DECL ?
106 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
107 same_type_p (supplicant, t))
108 return 1;
109 }
110 }
111
112 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
113 context = DECL_CONTEXT (supplicant);
114 else if (! declp)
115 /* Local classes have the same access as the enclosing function. */
116 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
117 else
118 context = NULL_TREE;
119
120 /* A namespace is not friend to anybody. */
121 if (context && TREE_CODE (context) == NAMESPACE_DECL)
122 context = NULL_TREE;
123
124 if (context)
125 return is_friend (type, context);
126
127 return 0;
128 }
129
130 /* Add a new friend to the friends of the aggregate type TYPE.
131 DECL is the FUNCTION_DECL of the friend being added. */
132
133 void
134 add_friend (type, decl)
135 tree type, decl;
136 {
137 tree typedecl;
138 tree list;
139 tree name;
140
141 if (decl == error_mark_node)
142 return;
143
144 typedecl = TYPE_MAIN_DECL (type);
145 list = DECL_FRIENDLIST (typedecl);
146 name = DECL_NAME (decl);
147 type = TREE_TYPE (typedecl);
148
149 while (list)
150 {
151 if (name == FRIEND_NAME (list))
152 {
153 tree friends = FRIEND_DECLS (list);
154 for (; friends ; friends = TREE_CHAIN (friends))
155 {
156 if (decl == TREE_VALUE (friends))
157 {
158 warning ("`%D' is already a friend of class `%T'",
159 decl, type);
160 cp_warning_at ("previous friend declaration of `%D'",
161 TREE_VALUE (friends));
162 return;
163 }
164 }
165
166 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
167
168 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
169 TREE_VALUE (list));
170 return;
171 }
172 list = TREE_CHAIN (list);
173 }
174
175 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
176
177 DECL_FRIENDLIST (typedecl)
178 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
179 DECL_FRIENDLIST (typedecl));
180 if (!uses_template_parms (type))
181 DECL_BEFRIENDING_CLASSES (decl)
182 = tree_cons (NULL_TREE, type,
183 DECL_BEFRIENDING_CLASSES (decl));
184 }
185
186 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
187 been defined, we make all of its member functions friends of
188 TYPE. If not, we make it a pending friend, which can later be added
189 when its definition is seen. If a type is defined, then its TYPE_DECL's
190 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
191 classes that are not defined. If a type has not yet been defined,
192 then the DECL_WAITING_FRIENDS contains a list of types
193 waiting to make it their friend. Note that these two can both
194 be in use at the same time! */
195
196 void
197 make_friend_class (type, friend_type)
198 tree type, friend_type;
199 {
200 tree classes;
201 int is_template_friend;
202
203 if (! IS_AGGR_TYPE (friend_type))
204 {
205 error ("invalid type `%T' declared `friend'", friend_type);
206 return;
207 }
208
209 if (processing_template_decl > template_class_depth (type))
210 /* If the TYPE is a template then it makes sense for it to be
211 friends with itself; this means that each instantiation is
212 friends with all other instantiations. */
213 {
214 if (CLASS_TYPE_P (friend_type)
215 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
216 && uses_template_parms (friend_type))
217 {
218 /* [temp.friend]
219 Friend declarations shall not declare partial
220 specializations. */
221 error ("partial specialization `%T' declared `friend'",
222 friend_type);
223 return;
224 }
225
226 is_template_friend = 1;
227 }
228 else if (same_type_p (type, friend_type))
229 {
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 warning ("`%D' is already a friend of `%T'",
279 probe, type);
280 break;
281 }
282 }
283 else if (TREE_CODE (probe) != TEMPLATE_DECL)
284 {
285 if (same_type_p (probe, friend_type))
286 {
287 warning ("`%T' is already a friend of `%T'",
288 probe, type);
289 break;
290 }
291 }
292 }
293
294 if (!classes)
295 {
296 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
297
298 CLASSTYPE_FRIEND_CLASSES (type)
299 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
300 if (is_template_friend)
301 friend_type = TREE_TYPE (friend_type);
302 if (!uses_template_parms (type))
303 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
304 = tree_cons (NULL_TREE, type,
305 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
306 }
307 }
308
309 /* Main friend processor. This is large, and for modularity purposes,
310 has been removed from grokdeclarator. It returns `void_type_node'
311 to indicate that something happened, though a FIELD_DECL is
312 not returned.
313
314 CTYPE is the class this friend belongs to.
315
316 DECLARATOR is the name of the friend.
317
318 DECL is the FUNCTION_DECL that the friend is.
319
320 In case we are parsing a friend which is part of an inline
321 definition, we will need to store PARM_DECL chain that comes
322 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
323
324 FLAGS is just used for `grokclassfn'.
325
326 QUALS say what special qualifies should apply to the object
327 pointed to by `this'. */
328
329 tree
330 do_friend (ctype, declarator, decl, parmdecls, attrlist,
331 flags, quals, funcdef_flag)
332 tree ctype, declarator, decl, parmdecls, attrlist;
333 enum overload_flags flags;
334 tree quals;
335 int funcdef_flag;
336 {
337 int is_friend_template = 0;
338
339 /* Every decl that gets here is a friend of something. */
340 DECL_FRIEND_P (decl) = 1;
341
342 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
343 {
344 declarator = TREE_OPERAND (declarator, 0);
345 if (TREE_CODE (declarator) == LOOKUP_EXPR)
346 declarator = TREE_OPERAND (declarator, 0);
347 if (is_overloaded_fn (declarator))
348 declarator = DECL_NAME (get_first_fn (declarator));
349 }
350
351 if (TREE_CODE (decl) != FUNCTION_DECL)
352 abort ();
353
354 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
355
356 if (ctype)
357 {
358 tree cname = TYPE_NAME (ctype);
359 if (TREE_CODE (cname) == TYPE_DECL)
360 cname = DECL_NAME (cname);
361
362 /* A method friend. */
363 if (flags == NO_SPECIAL && ctype && declarator == cname)
364 DECL_CONSTRUCTOR_P (decl) = 1;
365
366 /* This will set up DECL_ARGUMENTS for us. */
367 grokclassfn (ctype, decl, flags, quals);
368
369 if (is_friend_template)
370 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
371 else if (template_class_depth (current_class_type))
372 decl = push_template_decl_real (decl, /*is_friend=*/1);
373
374 /* We can't do lookup in a type that involves template
375 parameters. Instead, we rely on tsubst_friend_function
376 to check the validity of the declaration later. */
377 if (processing_template_decl)
378 add_friend (current_class_type, decl);
379 /* A nested class may declare a member of an enclosing class
380 to be a friend, so we do lookup here even if CTYPE is in
381 the process of being defined. */
382 else if (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
383 {
384 decl = check_classfn (ctype, decl);
385
386 if (decl)
387 add_friend (current_class_type, decl);
388 }
389 else
390 error ("member `%D' declared as friend before type `%T' defined",
391 decl, ctype);
392 }
393 /* A global friend.
394 @@ or possibly a friend from a base class ?!? */
395 else if (TREE_CODE (decl) == FUNCTION_DECL)
396 {
397 /* Friends must all go through the overload machinery,
398 even though they may not technically be overloaded.
399
400 Note that because classes all wind up being top-level
401 in their scope, their friend wind up in top-level scope as well. */
402 DECL_ARGUMENTS (decl) = parmdecls;
403 if (funcdef_flag)
404 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
405
406 if (! DECL_USE_TEMPLATE (decl))
407 {
408 /* We must check whether the decl refers to template
409 arguments before push_template_decl_real adds a
410 reference to the containing template class. */
411 int warn = (warn_nontemplate_friend
412 && ! funcdef_flag && ! is_friend_template
413 && current_template_parms
414 && uses_template_parms (decl));
415
416 if (is_friend_template
417 || template_class_depth (current_class_type) != 0)
418 /* We can't call pushdecl for a template class, since in
419 general, such a declaration depends on template
420 parameters. Instead, we call pushdecl when the class
421 is instantiated. */
422 decl = push_template_decl_real (decl, /*is_friend=*/1);
423 else if (current_function_decl)
424 /* This must be a local class, so pushdecl will be ok, and
425 insert an unqualified friend into the local scope
426 (rather than the containing namespace scope, which the
427 next choice will do). */
428 decl = pushdecl (decl);
429 else
430 {
431 /* We can't use pushdecl, as we might be in a template
432 class specialization, and pushdecl will insert an
433 unqualified friend decl into the template parameter
434 scope, rather than the namespace containing it. */
435 tree ns = decl_namespace_context (decl);
436
437 push_nested_namespace (ns);
438 decl = pushdecl_namespace_level (decl);
439 pop_nested_namespace (ns);
440 }
441
442 if (warn)
443 {
444 static int explained;
445 warning ("friend declaration `%#D' declares a non-template function", decl);
446 if (! explained)
447 {
448 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");
449 explained = 1;
450 }
451 }
452 }
453
454 add_friend (current_class_type,
455 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
456 DECL_FRIEND_P (decl) = 1;
457 }
458
459 /* Unfortunately, we have to handle attributes here. Normally we would
460 handle them in start_decl_1, but since this is a friend decl start_decl_1
461 never gets to see it. */
462
463 /* Set attributes here so if duplicate decl, will have proper attributes. */
464 cplus_decl_attributes (&decl, attrlist, 0);
465
466 return decl;
467 }