call.c (build_object_call): Compress consecutive calls to cp_error.
[gcc.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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. */
20
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"
29
30 /* Friend data structures are described in cp-tree.h. */
31
32 /* Returns non-zero if SUPPLICANT is a friend of TYPE. */
33
34 int
35 is_friend (type, supplicant)
36 tree type, supplicant;
37 {
38 int declp;
39 register tree list;
40 tree context;
41
42 if (supplicant == NULL_TREE || type == NULL_TREE)
43 return 0;
44
45 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
46
47 if (declp)
48 /* It's a function decl. */
49 {
50 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
51 tree name = DECL_NAME (supplicant);
52
53 for (; list ; list = TREE_CHAIN (list))
54 {
55 if (name == FRIEND_NAME (list))
56 {
57 tree friends = FRIEND_DECLS (list);
58 for (; friends ; friends = TREE_CHAIN (friends))
59 {
60 if (TREE_VALUE (friends) == NULL_TREE)
61 continue;
62
63 if (supplicant == TREE_VALUE (friends))
64 return 1;
65
66 /* With -fguiding-decls we are more lenient about
67 friendship. This is bogus in general since two
68 specializations of a template with non-type
69 template parameters may have the same type, but
70 be different.
71
72 Temporarily, we are also more lenient to deal
73 with nested friend functions, for which there can
74 be more than one FUNCTION_DECL, despite being the
75 same function. When that's fixed, the
76 FUNCTION_MEMBER_P bit can go. */
77 if ((flag_guiding_decls
78 || DECL_FUNCTION_MEMBER_P (supplicant))
79 && same_type_p (TREE_TYPE (supplicant),
80 TREE_TYPE (TREE_VALUE (friends))))
81 return 1;
82
83 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
84 && is_specialization_of (supplicant,
85 TREE_VALUE (friends)))
86 return 1;
87 }
88 break;
89 }
90 }
91 }
92 else
93 /* It's a type. */
94 {
95 /* Nested classes are implicitly friends of their enclosing types, as
96 per core issue 45 (this is a change from the standard). */
97 for (context = supplicant;
98 context && TYPE_P (context);
99 context = TYPE_CONTEXT (context))
100 if (type == context)
101 return 1;
102
103 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
104 for (; list ; list = TREE_CHAIN (list))
105 {
106 tree t = TREE_VALUE (list);
107
108 if (TREE_CODE (t) == TEMPLATE_DECL ?
109 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
110 same_type_p (supplicant, t))
111 return 1;
112 }
113 }
114
115 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
116 context = DECL_CONTEXT (supplicant);
117 else if (! declp)
118 /* Local classes have the same access as the enclosing function. */
119 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
120 else
121 context = NULL_TREE;
122
123 /* A namespace is not friend to anybody. */
124 if (context && TREE_CODE (context) == NAMESPACE_DECL)
125 context = NULL_TREE;
126
127 if (context)
128 return is_friend (type, context);
129
130 return 0;
131 }
132
133 /* Add a new friend to the friends of the aggregate type TYPE.
134 DECL is the FUNCTION_DECL of the friend being added. */
135
136 void
137 add_friend (type, decl)
138 tree type, decl;
139 {
140 tree typedecl;
141 tree list;
142 tree name;
143
144 if (decl == error_mark_node)
145 return;
146
147 typedecl = TYPE_MAIN_DECL (type);
148 list = DECL_FRIENDLIST (typedecl);
149 name = DECL_NAME (decl);
150 type = TREE_TYPE (typedecl);
151
152 while (list)
153 {
154 if (name == FRIEND_NAME (list))
155 {
156 tree friends = FRIEND_DECLS (list);
157 for (; friends ; friends = TREE_CHAIN (friends))
158 {
159 if (decl == TREE_VALUE (friends))
160 {
161 cp_warning ("`%D' is already a friend of class `%T'",
162 decl, type);
163 cp_warning_at ("previous friend declaration of `%D'",
164 TREE_VALUE (friends));
165 return;
166 }
167 }
168 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
169 TREE_VALUE (list));
170 return;
171 }
172 list = TREE_CHAIN (list);
173 }
174
175 DECL_FRIENDLIST (typedecl)
176 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
177 DECL_FRIENDLIST (typedecl));
178 if (!uses_template_parms (type))
179 DECL_BEFRIENDING_CLASSES (decl)
180 = tree_cons (NULL_TREE, type,
181 DECL_BEFRIENDING_CLASSES (decl));
182 }
183
184 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
185 been defined, we make all of its member functions friends of
186 TYPE. If not, we make it a pending friend, which can later be added
187 when its definition is seen. If a type is defined, then its TYPE_DECL's
188 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
189 classes that are not defined. If a type has not yet been defined,
190 then the DECL_WAITING_FRIENDS contains a list of types
191 waiting to make it their friend. Note that these two can both
192 be in use at the same time! */
193
194 void
195 make_friend_class (type, friend_type)
196 tree type, friend_type;
197 {
198 tree classes;
199 int is_template_friend;
200
201 if (! IS_AGGR_TYPE (friend_type))
202 {
203 cp_error ("invalid type `%T' declared `friend'", friend_type);
204 return;
205 }
206
207 if (CLASS_TYPE_P (friend_type)
208 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
209 && uses_template_parms (friend_type))
210 {
211 /* [temp.friend]
212
213 Friend declarations shall not declare partial
214 specializations. */
215 cp_error ("partial specialization `%T' declared `friend'",
216 friend_type);
217 return;
218 }
219
220 if (processing_template_decl > template_class_depth (type))
221 /* If the TYPE is a template then it makes sense for it to be
222 friends with itself; this means that each instantiation is
223 friends with all other instantiations. */
224 is_template_friend = 1;
225 else if (same_type_p (type, friend_type))
226 {
227 pedwarn ("class `%s' is implicitly friends with itself",
228 TYPE_NAME_STRING (type));
229 return;
230 }
231 else
232 is_template_friend = 0;
233
234 if (is_template_friend
235 && (TREE_CODE (friend_type) == TYPENAME_TYPE
236 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM))
237 {
238 /* [temp.friend]
239
240 A friend of a class or class template can be a function or
241 class template, a specialization of a function template or
242 class template, or an ordinary (nontemplate) function or
243 class.
244
245 But, we're looking at something like:
246
247 template <class T> friend typename S<T>::X;
248
249 or:
250
251 template <class T> friend class T;
252
253 which isn't any of these. */
254 if (TREE_CODE (friend_type) == TYPENAME_TYPE)
255 cp_error ("typename type `%T' declared `friend'",
256 friend_type);
257 else
258 cp_error ("template parameter type `%T' declared `friend'",
259 friend_type);
260 return;
261 }
262
263 GNU_xref_hier (type, friend_type, 0, 0, 1);
264
265 if (is_template_friend)
266 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
267
268 classes = CLASSTYPE_FRIEND_CLASSES (type);
269 while (classes
270 /* Stop if we find the same type on the list. */
271 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
272 friend_type == TREE_VALUE (classes) :
273 same_type_p (TREE_VALUE (classes), friend_type)))
274 classes = TREE_CHAIN (classes);
275 if (classes)
276 cp_warning ("`%T' is already a friend of `%T'",
277 TREE_VALUE (classes), type);
278 else
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 tree prefix_attributes, attributes;
321
322 /* Every decl that gets here is a friend of something. */
323 DECL_FRIEND_P (decl) = 1;
324
325 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
326 {
327 declarator = TREE_OPERAND (declarator, 0);
328 if (TREE_CODE (declarator) == LOOKUP_EXPR)
329 declarator = TREE_OPERAND (declarator, 0);
330 if (is_overloaded_fn (declarator))
331 declarator = DECL_NAME (get_first_fn (declarator));
332 }
333
334 if (TREE_CODE (decl) != FUNCTION_DECL)
335 my_friendly_abort (990513);
336
337 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
338
339 if (ctype)
340 {
341 tree cname = TYPE_NAME (ctype);
342 if (TREE_CODE (cname) == TYPE_DECL)
343 cname = DECL_NAME (cname);
344
345 /* A method friend. */
346 if (flags == NO_SPECIAL && ctype && declarator == cname)
347 DECL_CONSTRUCTOR_P (decl) = 1;
348
349 /* This will set up DECL_ARGUMENTS for us. */
350 grokclassfn (ctype, decl, flags, quals);
351
352 if (is_friend_template)
353 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
354 else if (template_class_depth (current_class_type))
355 decl = push_template_decl_real (decl, /*is_friend=*/1);
356
357 /* We can't do lookup in a type that involves template
358 parameters. Instead, we rely on tsubst_friend_function
359 to check the validity of the declaration later. */
360 if (processing_template_decl)
361 add_friend (current_class_type, decl);
362 /* A nested class may declare a member of an enclosing class
363 to be a friend, so we do lookup here even if CTYPE is in
364 the process of being defined. */
365 else if (TYPE_SIZE (ctype) != 0 || TYPE_BEING_DEFINED (ctype))
366 {
367 decl = check_classfn (ctype, decl);
368
369 if (decl)
370 add_friend (current_class_type, decl);
371 }
372 else
373 cp_error ("member `%D' declared as friend before type `%T' defined",
374 decl, ctype);
375 }
376 /* A global friend.
377 @@ or possibly a friend from a base class ?!? */
378 else if (TREE_CODE (decl) == FUNCTION_DECL)
379 {
380 /* Friends must all go through the overload machinery,
381 even though they may not technically be overloaded.
382
383 Note that because classes all wind up being top-level
384 in their scope, their friend wind up in top-level scope as well. */
385 DECL_ARGUMENTS (decl) = parmdecls;
386 if (funcdef_flag)
387 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
388
389 if (! DECL_USE_TEMPLATE (decl))
390 {
391 /* We can call pushdecl here, because the TREE_CHAIN of this
392 FUNCTION_DECL is not needed for other purposes. Don't do
393 this for a template instantiation. However, we don't
394 call pushdecl() for a friend function of a template
395 class, since in general, such a declaration depends on
396 template parameters. Instead, we call pushdecl when the
397 class is instantiated. */
398 if (!is_friend_template
399 && template_class_depth (current_class_type) == 0)
400 decl = pushdecl (decl);
401 else
402 decl = push_template_decl_real (decl, /*is_friend=*/1);
403
404 if (warn_nontemplate_friend
405 && ! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
406 && current_template_parms && uses_template_parms (decl))
407 {
408 static int explained;
409 cp_warning ("friend declaration `%#D'", decl);
410 warning (" declares a non-template function");
411 if (! explained)
412 {
413 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.");
414 explained = 1;
415 }
416 }
417 }
418
419 make_decl_rtl (decl, NULL_PTR, 1);
420 add_friend (current_class_type,
421 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
422 DECL_FRIEND_P (decl) = 1;
423 }
424
425 /* Unfortunately, we have to handle attributes here. Normally we would
426 handle them in start_decl_1, but since this is a friend decl start_decl_1
427 never gets to see it. */
428
429 if (attrlist)
430 {
431 attributes = TREE_PURPOSE (attrlist);
432 prefix_attributes = TREE_VALUE (attrlist);
433 }
434 else
435 {
436 attributes = NULL_TREE;
437 prefix_attributes = NULL_TREE;
438 }
439
440 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
441 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
442 #endif
443
444 /* Set attributes here so if duplicate decl, will have proper attributes. */
445 cplus_decl_attributes (decl, attributes, prefix_attributes);
446
447 return decl;
448 }