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. */
30 /* Friend data structures are described in cp-tree.h. */
32 /* Returns non-zero if SUPPLICANT is a friend of TYPE. */
35 is_friend (type, supplicant)
36 tree type, supplicant;
42 if (supplicant == NULL_TREE || type == NULL_TREE)
45 declp = DECL_P (supplicant);
48 /* It's a function decl. */
50 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
51 tree name = DECL_NAME (supplicant);
53 for (; list ; list = TREE_CHAIN (list))
55 if (name == FRIEND_NAME (list))
57 tree friends = FRIEND_DECLS (list);
58 for (; friends ; friends = TREE_CHAIN (friends))
60 if (TREE_VALUE (friends) == NULL_TREE)
63 if (supplicant == TREE_VALUE (friends))
66 /* Temporarily, we are more lenient to deal with
67 nested friend functions, for which there can be
68 more than one FUNCTION_DECL, despite being the
69 same function. When that's fixed, this bit can
71 if (DECL_FUNCTION_MEMBER_P (supplicant)
72 && same_type_p (TREE_TYPE (supplicant),
73 TREE_TYPE (TREE_VALUE (friends))))
76 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
77 && is_specialization_of (supplicant,
78 TREE_VALUE (friends)))
88 /* Nested classes are implicitly friends of their enclosing types, as
89 per core issue 45 (this is a change from the standard). */
90 for (context = supplicant;
91 context && TYPE_P (context);
92 context = TYPE_CONTEXT (context))
96 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
97 for (; list ; list = TREE_CHAIN (list))
99 tree t = TREE_VALUE (list);
101 if (TREE_CODE (t) == TEMPLATE_DECL ?
102 is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
103 same_type_p (supplicant, t))
108 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
109 context = DECL_CONTEXT (supplicant);
111 /* Local classes have the same access as the enclosing function. */
112 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
116 /* A namespace is not friend to anybody. */
117 if (context && TREE_CODE (context) == NAMESPACE_DECL)
121 return is_friend (type, context);
126 /* Add a new friend to the friends of the aggregate type TYPE.
127 DECL is the FUNCTION_DECL of the friend being added. */
130 add_friend (type, decl)
137 if (decl == error_mark_node)
140 typedecl = TYPE_MAIN_DECL (type);
141 list = DECL_FRIENDLIST (typedecl);
142 name = DECL_NAME (decl);
143 type = TREE_TYPE (typedecl);
147 if (name == FRIEND_NAME (list))
149 tree friends = FRIEND_DECLS (list);
150 for (; friends ; friends = TREE_CHAIN (friends))
152 if (decl == TREE_VALUE (friends))
154 cp_warning ("`%D' is already a friend of class `%T'",
156 cp_warning_at ("previous friend declaration of `%D'",
157 TREE_VALUE (friends));
161 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
165 list = TREE_CHAIN (list);
168 DECL_FRIENDLIST (typedecl)
169 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
170 DECL_FRIENDLIST (typedecl));
171 if (!uses_template_parms (type))
172 DECL_BEFRIENDING_CLASSES (decl)
173 = tree_cons (NULL_TREE, type,
174 DECL_BEFRIENDING_CLASSES (decl));
177 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
178 been defined, we make all of its member functions friends of
179 TYPE. If not, we make it a pending friend, which can later be added
180 when its definition is seen. If a type is defined, then its TYPE_DECL's
181 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
182 classes that are not defined. If a type has not yet been defined,
183 then the DECL_WAITING_FRIENDS contains a list of types
184 waiting to make it their friend. Note that these two can both
185 be in use at the same time! */
188 make_friend_class (type, friend_type)
189 tree type, friend_type;
192 int is_template_friend;
194 if (! IS_AGGR_TYPE (friend_type))
196 cp_error ("invalid type `%T' declared `friend'", friend_type);
200 if (CLASS_TYPE_P (friend_type)
201 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
202 && uses_template_parms (friend_type))
206 Friend declarations shall not declare partial
208 cp_error ("partial specialization `%T' declared `friend'",
213 if (processing_template_decl > template_class_depth (type))
214 /* If the TYPE is a template then it makes sense for it to be
215 friends with itself; this means that each instantiation is
216 friends with all other instantiations. */
217 is_template_friend = 1;
218 else if (same_type_p (type, friend_type))
220 pedwarn ("class `%s' is implicitly friends with itself",
221 TYPE_NAME_STRING (type));
225 is_template_friend = 0;
227 if (is_template_friend
228 && (TREE_CODE (friend_type) == TYPENAME_TYPE
229 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM))
233 A friend of a class or class template can be a function or
234 class template, a specialization of a function template or
235 class template, or an ordinary (nontemplate) function or
238 But, we're looking at something like:
240 template <class T> friend typename S<T>::X;
244 template <class T> friend class T;
246 which isn't any of these. */
247 if (TREE_CODE (friend_type) == TYPENAME_TYPE)
248 cp_error ("typename type `%T' declared `friend'",
251 cp_error ("template parameter type `%T' declared `friend'",
256 GNU_xref_hier (type, friend_type, 0, 0, 1);
258 if (is_template_friend)
259 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
261 classes = CLASSTYPE_FRIEND_CLASSES (type);
263 /* Stop if we find the same type on the list. */
264 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
265 friend_type == TREE_VALUE (classes) :
266 same_type_p (TREE_VALUE (classes), friend_type)))
267 classes = TREE_CHAIN (classes);
269 cp_warning ("`%T' is already a friend of `%T'",
270 TREE_VALUE (classes), type);
273 CLASSTYPE_FRIEND_CLASSES (type)
274 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
275 if (is_template_friend)
276 friend_type = TREE_TYPE (friend_type);
277 if (!uses_template_parms (type))
278 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
279 = tree_cons (NULL_TREE, type,
280 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
284 /* Main friend processor. This is large, and for modularity purposes,
285 has been removed from grokdeclarator. It returns `void_type_node'
286 to indicate that something happened, though a FIELD_DECL is
289 CTYPE is the class this friend belongs to.
291 DECLARATOR is the name of the friend.
293 DECL is the FUNCTION_DECL that the friend is.
295 In case we are parsing a friend which is part of an inline
296 definition, we will need to store PARM_DECL chain that comes
297 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
299 FLAGS is just used for `grokclassfn'.
301 QUALS say what special qualifies should apply to the object
302 pointed to by `this'. */
305 do_friend (ctype, declarator, decl, parmdecls, attrlist,
306 flags, quals, funcdef_flag)
307 tree ctype, declarator, decl, parmdecls, attrlist;
308 enum overload_flags flags;
312 int is_friend_template = 0;
313 tree prefix_attributes, attributes;
315 /* Every decl that gets here is a friend of something. */
316 DECL_FRIEND_P (decl) = 1;
318 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
320 declarator = TREE_OPERAND (declarator, 0);
321 if (TREE_CODE (declarator) == LOOKUP_EXPR)
322 declarator = TREE_OPERAND (declarator, 0);
323 if (is_overloaded_fn (declarator))
324 declarator = DECL_NAME (get_first_fn (declarator));
327 if (TREE_CODE (decl) != FUNCTION_DECL)
328 my_friendly_abort (990513);
330 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
334 tree cname = TYPE_NAME (ctype);
335 if (TREE_CODE (cname) == TYPE_DECL)
336 cname = DECL_NAME (cname);
338 /* A method friend. */
339 if (flags == NO_SPECIAL && ctype && declarator == cname)
340 DECL_CONSTRUCTOR_P (decl) = 1;
342 /* This will set up DECL_ARGUMENTS for us. */
343 grokclassfn (ctype, decl, flags, quals);
345 if (is_friend_template)
346 decl = DECL_TI_TEMPLATE (push_template_decl (decl));
347 else if (template_class_depth (current_class_type))
348 decl = push_template_decl_real (decl, /*is_friend=*/1);
350 /* We can't do lookup in a type that involves template
351 parameters. Instead, we rely on tsubst_friend_function
352 to check the validity of the declaration later. */
353 if (processing_template_decl)
354 add_friend (current_class_type, decl);
355 /* A nested class may declare a member of an enclosing class
356 to be a friend, so we do lookup here even if CTYPE is in
357 the process of being defined. */
358 else if (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
360 decl = check_classfn (ctype, decl);
363 add_friend (current_class_type, decl);
366 cp_error ("member `%D' declared as friend before type `%T' defined",
370 @@ or possibly a friend from a base class ?!? */
371 else if (TREE_CODE (decl) == FUNCTION_DECL)
373 /* Friends must all go through the overload machinery,
374 even though they may not technically be overloaded.
376 Note that because classes all wind up being top-level
377 in their scope, their friend wind up in top-level scope as well. */
378 DECL_ARGUMENTS (decl) = parmdecls;
380 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
382 if (! DECL_USE_TEMPLATE (decl))
384 /* We can call pushdecl here, because the TREE_CHAIN of this
385 FUNCTION_DECL is not needed for other purposes. Don't do
386 this for a template instantiation. However, we don't
387 call pushdecl() for a friend function of a template
388 class, since in general, such a declaration depends on
389 template parameters. Instead, we call pushdecl when the
390 class is instantiated. */
391 if (!is_friend_template
392 && template_class_depth (current_class_type) == 0)
393 decl = pushdecl (decl);
395 decl = push_template_decl_real (decl, /*is_friend=*/1);
397 if (warn_nontemplate_friend
398 && ! funcdef_flag && ! is_friend_template
399 && current_template_parms && uses_template_parms (decl))
401 static int explained;
402 cp_warning ("friend declaration `%#D'", decl);
403 warning (" declares a non-template function");
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);