1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000 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)
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. */
31 /* Friend data structures are described in cp-tree.h. */
33 /* Returns non-zero if SUPPLICANT is a friend of TYPE. */
36 is_friend (type
, supplicant
)
37 tree type
, supplicant
;
43 if (supplicant
== NULL_TREE
|| type
== NULL_TREE
)
46 declp
= DECL_P (supplicant
);
49 /* It's a function decl. */
51 tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
52 tree name
= DECL_NAME (supplicant
);
54 for (; list
; list
= TREE_CHAIN (list
))
56 if (name
== FRIEND_NAME (list
))
58 tree friends
= FRIEND_DECLS (list
);
59 for (; friends
; friends
= TREE_CHAIN (friends
))
61 if (TREE_VALUE (friends
) == NULL_TREE
)
64 if (supplicant
== TREE_VALUE (friends
))
67 /* Temporarily, we are more lenient to deal with
68 nested friend functions, for which there can be
69 more than one FUNCTION_DECL, despite being the
70 same function. When that's fixed, this bit can
72 if (DECL_FUNCTION_MEMBER_P (supplicant
)
73 && same_type_p (TREE_TYPE (supplicant
),
74 TREE_TYPE (TREE_VALUE (friends
))))
77 if (TREE_CODE (TREE_VALUE (friends
)) == TEMPLATE_DECL
78 && is_specialization_of (supplicant
,
79 TREE_VALUE (friends
)))
89 /* Nested classes are implicitly friends of their enclosing types, as
90 per core issue 45 (this is a change from the standard). */
91 for (context
= supplicant
;
92 context
&& TYPE_P (context
);
93 context
= TYPE_CONTEXT (context
))
97 list
= CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type
)));
98 for (; list
; list
= TREE_CHAIN (list
))
100 tree t
= TREE_VALUE (list
);
102 if (TREE_CODE (t
) == TEMPLATE_DECL
?
103 is_specialization_of (TYPE_MAIN_DECL (supplicant
), t
) :
104 same_type_p (supplicant
, t
))
109 if (declp
&& DECL_FUNCTION_MEMBER_P (supplicant
))
110 context
= DECL_CONTEXT (supplicant
);
112 /* Local classes have the same access as the enclosing function. */
113 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
117 /* A namespace is not friend to anybody. */
118 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
122 return is_friend (type
, context
);
127 /* Add a new friend to the friends of the aggregate type TYPE.
128 DECL is the FUNCTION_DECL of the friend being added. */
131 add_friend (type
, decl
)
138 if (decl
== error_mark_node
)
141 typedecl
= TYPE_MAIN_DECL (type
);
142 list
= DECL_FRIENDLIST (typedecl
);
143 name
= DECL_NAME (decl
);
144 type
= TREE_TYPE (typedecl
);
148 if (name
== FRIEND_NAME (list
))
150 tree friends
= FRIEND_DECLS (list
);
151 for (; friends
; friends
= TREE_CHAIN (friends
))
153 if (decl
== TREE_VALUE (friends
))
155 cp_warning ("`%D' is already a friend of class `%T'",
157 cp_warning_at ("previous friend declaration of `%D'",
158 TREE_VALUE (friends
));
162 TREE_VALUE (list
) = tree_cons (error_mark_node
, decl
,
166 list
= TREE_CHAIN (list
);
169 DECL_FRIENDLIST (typedecl
)
170 = tree_cons (DECL_NAME (decl
), build_tree_list (error_mark_node
, decl
),
171 DECL_FRIENDLIST (typedecl
));
172 if (!uses_template_parms (type
))
173 DECL_BEFRIENDING_CLASSES (decl
)
174 = tree_cons (NULL_TREE
, type
,
175 DECL_BEFRIENDING_CLASSES (decl
));
178 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
179 been defined, we make all of its member functions friends of
180 TYPE. If not, we make it a pending friend, which can later be added
181 when its definition is seen. If a type is defined, then its TYPE_DECL's
182 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
183 classes that are not defined. If a type has not yet been defined,
184 then the DECL_WAITING_FRIENDS contains a list of types
185 waiting to make it their friend. Note that these two can both
186 be in use at the same time! */
189 make_friend_class (type
, friend_type
)
190 tree type
, friend_type
;
193 int is_template_friend
;
195 if (! IS_AGGR_TYPE (friend_type
))
197 cp_error ("invalid type `%T' declared `friend'", friend_type
);
201 if (CLASS_TYPE_P (friend_type
)
202 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
203 && uses_template_parms (friend_type
))
207 Friend declarations shall not declare partial
209 cp_error ("partial specialization `%T' declared `friend'",
214 if (processing_template_decl
> template_class_depth (type
))
215 /* If the TYPE is a template then it makes sense for it to be
216 friends with itself; this means that each instantiation is
217 friends with all other instantiations. */
218 is_template_friend
= 1;
219 else if (same_type_p (type
, friend_type
))
221 cp_pedwarn ("class `%T' is implicitly friends with itself",
226 is_template_friend
= 0;
228 if (is_template_friend
229 && (TREE_CODE (friend_type
) == TYPENAME_TYPE
230 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
))
234 A friend of a class or class template can be a function or
235 class template, a specialization of a function template or
236 class template, or an ordinary (nontemplate) function or
239 But, we're looking at something like:
241 template <class T> friend typename S<T>::X;
245 template <class T> friend class T;
247 which isn't any of these. */
248 if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
249 cp_error ("typename type `%T' declared `friend'",
252 cp_error ("template parameter type `%T' declared `friend'",
257 GNU_xref_hier (type
, friend_type
, 0, 0, 1);
259 if (is_template_friend
)
260 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
262 classes
= CLASSTYPE_FRIEND_CLASSES (type
);
264 /* Stop if we find the same type on the list. */
265 && !(TREE_CODE (TREE_VALUE (classes
)) == TEMPLATE_DECL
?
266 friend_type
== TREE_VALUE (classes
) :
267 same_type_p (TREE_VALUE (classes
), friend_type
)))
268 classes
= TREE_CHAIN (classes
);
270 cp_warning ("`%T' is already a friend of `%T'",
271 TREE_VALUE (classes
), type
);
274 CLASSTYPE_FRIEND_CLASSES (type
)
275 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
276 if (is_template_friend
)
277 friend_type
= TREE_TYPE (friend_type
);
278 if (!uses_template_parms (type
))
279 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
280 = tree_cons (NULL_TREE
, type
,
281 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
285 /* Main friend processor. This is large, and for modularity purposes,
286 has been removed from grokdeclarator. It returns `void_type_node'
287 to indicate that something happened, though a FIELD_DECL is
290 CTYPE is the class this friend belongs to.
292 DECLARATOR is the name of the friend.
294 DECL is the FUNCTION_DECL that the friend is.
296 In case we are parsing a friend which is part of an inline
297 definition, we will need to store PARM_DECL chain that comes
298 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
300 FLAGS is just used for `grokclassfn'.
302 QUALS say what special qualifies should apply to the object
303 pointed to by `this'. */
306 do_friend (ctype
, declarator
, decl
, parmdecls
, attrlist
,
307 flags
, quals
, funcdef_flag
)
308 tree ctype
, declarator
, decl
, parmdecls
, attrlist
;
309 enum overload_flags flags
;
313 int is_friend_template
= 0;
314 tree prefix_attributes
, attributes
;
316 /* Every decl that gets here is a friend of something. */
317 DECL_FRIEND_P (decl
) = 1;
319 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
321 declarator
= TREE_OPERAND (declarator
, 0);
322 if (TREE_CODE (declarator
) == LOOKUP_EXPR
)
323 declarator
= TREE_OPERAND (declarator
, 0);
324 if (is_overloaded_fn (declarator
))
325 declarator
= DECL_NAME (get_first_fn (declarator
));
328 if (TREE_CODE (decl
) != FUNCTION_DECL
)
329 my_friendly_abort (990513);
331 is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
335 tree cname
= TYPE_NAME (ctype
);
336 if (TREE_CODE (cname
) == TYPE_DECL
)
337 cname
= DECL_NAME (cname
);
339 /* A method friend. */
340 if (flags
== NO_SPECIAL
&& ctype
&& declarator
== cname
)
341 DECL_CONSTRUCTOR_P (decl
) = 1;
343 /* This will set up DECL_ARGUMENTS for us. */
344 grokclassfn (ctype
, decl
, flags
, quals
);
346 if (is_friend_template
)
347 decl
= DECL_TI_TEMPLATE (push_template_decl (decl
));
348 else if (template_class_depth (current_class_type
))
349 decl
= push_template_decl_real (decl
, /*is_friend=*/1);
351 /* We can't do lookup in a type that involves template
352 parameters. Instead, we rely on tsubst_friend_function
353 to check the validity of the declaration later. */
354 if (processing_template_decl
)
355 add_friend (current_class_type
, decl
);
356 /* A nested class may declare a member of an enclosing class
357 to be a friend, so we do lookup here even if CTYPE is in
358 the process of being defined. */
359 else if (COMPLETE_TYPE_P (ctype
) || TYPE_BEING_DEFINED (ctype
))
361 decl
= check_classfn (ctype
, decl
);
364 add_friend (current_class_type
, decl
);
367 cp_error ("member `%D' declared as friend before type `%T' defined",
371 @@ or possibly a friend from a base class ?!? */
372 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
374 /* Friends must all go through the overload machinery,
375 even though they may not technically be overloaded.
377 Note that because classes all wind up being top-level
378 in their scope, their friend wind up in top-level scope as well. */
379 DECL_ARGUMENTS (decl
) = parmdecls
;
381 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
383 if (! DECL_USE_TEMPLATE (decl
))
385 /* We can call pushdecl here, because the TREE_CHAIN of this
386 FUNCTION_DECL is not needed for other purposes. Don't do
387 this for a template instantiation. However, we don't
388 call pushdecl() for a friend function of a template
389 class, since in general, such a declaration depends on
390 template parameters. Instead, we call pushdecl when the
391 class is instantiated. */
392 if (!is_friend_template
393 && template_class_depth (current_class_type
) == 0)
394 decl
= pushdecl (decl
);
396 decl
= push_template_decl_real (decl
, /*is_friend=*/1);
398 if (warn_nontemplate_friend
399 && ! funcdef_flag
&& ! is_friend_template
400 && current_template_parms
&& uses_template_parms (decl
))
402 static int explained
;
403 cp_warning ("friend declaration `%#D' declares a non-template function", decl
);
406 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.");
412 make_decl_rtl (decl
, NULL_PTR
, 1);
413 add_friend (current_class_type
,
414 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
);
415 DECL_FRIEND_P (decl
) = 1;
418 /* Unfortunately, we have to handle attributes here. Normally we would
419 handle them in start_decl_1, but since this is a friend decl start_decl_1
420 never gets to see it. */
424 attributes
= TREE_PURPOSE (attrlist
);
425 prefix_attributes
= TREE_VALUE (attrlist
);
429 attributes
= NULL_TREE
;
430 prefix_attributes
= NULL_TREE
;
433 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
434 SET_DEFAULT_DECL_ATTRIBUTES (decl
, attributes
);
437 /* Set attributes here so if duplicate decl, will have proper attributes. */
438 cplus_decl_attributes (decl
, attributes
, prefix_attributes
);