call.c, [...]: Replace "GNU CC" with "GCC" in the copyright header.
[gcc.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC 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.
10
11 GCC 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.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "expr.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "toplev.h"
32
33 /* Friend data structures are described in cp-tree.h. */
34
35 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
36
37 int
38 is_friend (type, supplicant)
39 tree type, supplicant;
40 {
41 int declp;
42 register 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 if (TREE_VALUE (friends) == NULL_TREE)
64 continue;
65
66 if (supplicant == TREE_VALUE (friends))
67 return 1;
68
69 /* Temporarily, we are more lenient to deal with
70 nested friend functions, for which there can be
71 more than one FUNCTION_DECL, despite being the
72 same function. When that's fixed, this bit can
73 go. */
74 if (DECL_FUNCTION_MEMBER_P (supplicant)
75 && same_type_p (TREE_TYPE (supplicant),
76 TREE_TYPE (TREE_VALUE (friends))))
77 return 1;
78
79 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
80 && is_specialization_of (supplicant,
81 TREE_VALUE (friends)))
82 return 1;
83 }
84 break;
85 }
86 }
87 }
88 else
89 /* It's a type. */
90 {
91 /* Nested classes are implicitly friends of their enclosing types, as
92 per core issue 45 (this is a change from the standard). */
93 for (context = supplicant;
94 context && TYPE_P (context);
95 context = TYPE_CONTEXT (context))
96 if (type == context)
97 return 1;
98
99 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
100 for (; list ; list = TREE_CHAIN (list))
101 {
102 tree t = TREE_VALUE (list);
103
104 if (TREE_CODE (t) == TEMPLATE_DECL ?
105 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
106 same_type_p (supplicant, t))
107 return 1;
108 }
109 }
110
111 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
112 context = DECL_CONTEXT (supplicant);
113 else if (! declp)
114 /* Local classes have the same access as the enclosing function. */
115 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
116 else
117 context = NULL_TREE;
118
119 /* A namespace is not friend to anybody. */
120 if (context && TREE_CODE (context) == NAMESPACE_DECL)
121 context = NULL_TREE;
122
123 if (context)
124 return is_friend (type, context);
125
126 return 0;
127 }
128
129 /* Add a new friend to the friends of the aggregate type TYPE.
130 DECL is the FUNCTION_DECL of the friend being added. */
131
132 void
133 add_friend (type, decl)
134 tree type, decl;
135 {
136 tree typedecl;
137 tree list;
138 tree name;
139
140 if (decl == error_mark_node)
141 return;
142
143 typedecl = TYPE_MAIN_DECL (type);
144 list = DECL_FRIENDLIST (typedecl);
145 name = DECL_NAME (decl);
146 type = TREE_TYPE (typedecl);
147
148 while (list)
149 {
150 if (name == FRIEND_NAME (list))
151 {
152 tree friends = FRIEND_DECLS (list);
153 for (; friends ; friends = TREE_CHAIN (friends))
154 {
155 if (decl == TREE_VALUE (friends))
156 {
157 warning ("`%D' is already a friend of class `%T'",
158 decl, type);
159 cp_warning_at ("previous friend declaration of `%D'",
160 TREE_VALUE (friends));
161 return;
162 }
163 }
164
165 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
166
167 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
168 TREE_VALUE (list));
169 return;
170 }
171 list = TREE_CHAIN (list);
172 }
173
174 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
175
176 DECL_FRIENDLIST (typedecl)
177 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
178 DECL_FRIENDLIST (typedecl));
179 if (!uses_template_parms (type))
180 DECL_BEFRIENDING_CLASSES (decl)
181 = tree_cons (NULL_TREE, type,
182 DECL_BEFRIENDING_CLASSES (decl));
183 }
184
185 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
186 been defined, we make all of its member functions friends of
187 TYPE. If not, we make it a pending friend, which can later be added
188 when its definition is seen. If a type is defined, then its TYPE_DECL's
189 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
190 classes that are not defined. If a type has not yet been defined,
191 then the DECL_WAITING_FRIENDS contains a list of types
192 waiting to make it their friend. Note that these two can both
193 be in use at the same time! */
194
195 void
196 make_friend_class (type, friend_type)
197 tree type, friend_type;
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 pedwarn ("class `%T' is implicitly friends with itself",
230 type);
231 return;
232 }
233 else
234 is_template_friend = 0;
235
236 /* [temp.friend]
237
238 A friend of a class or class template can be a function or
239 class template, a specialization of a function template or
240 class template, or an ordinary (nontemplate) function or
241 class. */
242 if (!is_template_friend)
243 ;/* ok */
244 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
245 {
246 /* template <class T> friend typename S<T>::X; */
247 error ("typename type `%#T' declared `friend'", friend_type);
248 return;
249 }
250 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
251 {
252 /* template <class T> friend class T; */
253 error ("template parameter type `%T' declared `friend'", friend_type);
254 return;
255 }
256 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
257 {
258 /* template <class T> friend class A; where A is not a template */
259 error ("`%#T' is not a template", friend_type);
260 return;
261 }
262
263 if (is_template_friend)
264 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
265
266 classes = CLASSTYPE_FRIEND_CLASSES (type);
267 while (classes
268 /* Stop if we find the same type on the list. */
269 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
270 friend_type == TREE_VALUE (classes) :
271 same_type_p (TREE_VALUE (classes), friend_type)))
272 classes = TREE_CHAIN (classes);
273 if (classes)
274 warning ("`%T' is already a friend of `%T'",
275 TREE_VALUE (classes), type);
276 else
277 {
278 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
279
280 CLASSTYPE_FRIEND_CLASSES (type)
281 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
282 if (is_template_friend)
283 friend_type = TREE_TYPE (friend_type);
284 if (!uses_template_parms (type))
285 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
286 = tree_cons (NULL_TREE, type,
287 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
288 }
289 }
290
291 /* Main friend processor. This is large, and for modularity purposes,
292 has been removed from grokdeclarator. It returns `void_type_node'
293 to indicate that something happened, though a FIELD_DECL is
294 not returned.
295
296 CTYPE is the class this friend belongs to.
297
298 DECLARATOR is the name of the friend.
299
300 DECL is the FUNCTION_DECL that the friend is.
301
302 In case we are parsing a friend which is part of an inline
303 definition, we will need to store PARM_DECL chain that comes
304 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
305
306 FLAGS is just used for `grokclassfn'.
307
308 QUALS say what special qualifies should apply to the object
309 pointed to by `this'. */
310
311 tree
312 do_friend (ctype, declarator, decl, parmdecls, attrlist,
313 flags, quals, funcdef_flag)
314 tree ctype, declarator, decl, parmdecls, attrlist;
315 enum overload_flags flags;
316 tree quals;
317 int funcdef_flag;
318 {
319 int is_friend_template = 0;
320
321 /* Every decl that gets here is a friend of something. */
322 DECL_FRIEND_P (decl) = 1;
323
324 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
325 {
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));
331 }
332
333 if (TREE_CODE (decl) != FUNCTION_DECL)
334 abort ();
335
336 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
337
338 if (ctype)
339 {
340 tree cname = TYPE_NAME (ctype);
341 if (TREE_CODE (cname) == TYPE_DECL)
342 cname = DECL_NAME (cname);
343
344 /* A method friend. */
345 if (flags == NO_SPECIAL && ctype && declarator == cname)
346 DECL_CONSTRUCTOR_P (decl) = 1;
347
348 /* This will set up DECL_ARGUMENTS for us. */
349 grokclassfn (ctype, decl, flags, quals);
350
351 if (is_friend_template)
352 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
353 else if (template_class_depth (current_class_type))
354 decl = push_template_decl_real (decl, /*is_friend=*/1);
355
356 /* We can't do lookup in a type that involves template
357 parameters. Instead, we rely on tsubst_friend_function
358 to check the validity of the declaration later. */
359 if (processing_template_decl)
360 add_friend (current_class_type, decl);
361 /* A nested class may declare a member of an enclosing class
362 to be a friend, so we do lookup here even if CTYPE is in
363 the process of being defined. */
364 else if (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
365 {
366 decl = check_classfn (ctype, decl);
367
368 if (decl)
369 add_friend (current_class_type, decl);
370 }
371 else
372 error ("member `%D' declared as friend before type `%T' defined",
373 decl, ctype);
374 }
375 /* A global friend.
376 @@ or possibly a friend from a base class ?!? */
377 else if (TREE_CODE (decl) == FUNCTION_DECL)
378 {
379 /* Friends must all go through the overload machinery,
380 even though they may not technically be overloaded.
381
382 Note that because classes all wind up being top-level
383 in their scope, their friend wind up in top-level scope as well. */
384 DECL_ARGUMENTS (decl) = parmdecls;
385 if (funcdef_flag)
386 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
387
388 if (! DECL_USE_TEMPLATE (decl))
389 {
390 /* We must check whether the decl refers to template
391 arguments before push_template_decl_real adds a
392 reference to the containing template class. */
393 int warn = (warn_nontemplate_friend
394 && ! funcdef_flag && ! is_friend_template
395 && current_template_parms
396 && uses_template_parms (decl));
397
398 if (is_friend_template
399 || template_class_depth (current_class_type) != 0)
400 /* We can't call pushdecl for a template class, since in
401 general, such a declaration depends on template
402 parameters. Instead, we call pushdecl when the class
403 is instantiated. */
404 decl = push_template_decl_real (decl, /*is_friend=*/1);
405 else if (current_function_decl)
406 /* This must be a local class, so pushdecl will be ok, and
407 insert an unqualified friend into the local scope
408 (rather than the containing namespace scope, which the
409 next choice will do). */
410 decl = pushdecl (decl);
411 else
412 {
413 /* We can't use pushdecl, as we might be in a template
414 class specialization, and pushdecl will insert an
415 unqualified friend decl into the template parameter
416 scope, rather than the namespace containing it. */
417 tree ns = decl_namespace_context (decl);
418
419 push_nested_namespace (ns);
420 decl = pushdecl_namespace_level (decl);
421 pop_nested_namespace (ns);
422 }
423
424 if (warn)
425 {
426 static int explained;
427 warning ("friend declaration `%#D' declares a non-template function", decl);
428 if (! explained)
429 {
430 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");
431 explained = 1;
432 }
433 }
434 }
435
436 add_friend (current_class_type,
437 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
438 DECL_FRIEND_P (decl) = 1;
439 }
440
441 /* Unfortunately, we have to handle attributes here. Normally we would
442 handle them in start_decl_1, but since this is a friend decl start_decl_1
443 never gets to see it. */
444
445 /* Set attributes here so if duplicate decl, will have proper attributes. */
446 cplus_decl_attributes (&decl, attrlist, 0);
447
448 return decl;
449 }