* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cp / friend.c
blob3ecd3177195a509a4f4a1140e1cb092a91c72520
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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. */
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"
34 /* Friend data structures are described in cp-tree.h. */
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
38 int
39 is_friend (type, supplicant)
40 tree type, supplicant;
42 int declp;
43 register tree list;
44 tree context;
46 if (supplicant == NULL_TREE || type == NULL_TREE)
47 return 0;
49 declp = DECL_P (supplicant);
51 if (declp)
52 /* It's a function decl. */
54 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
55 tree name = DECL_NAME (supplicant);
57 for (; list ; list = TREE_CHAIN (list))
59 if (name == FRIEND_NAME (list))
61 tree friends = FRIEND_DECLS (list);
62 for (; friends ; friends = TREE_CHAIN (friends))
64 if (TREE_VALUE (friends) == NULL_TREE)
65 continue;
67 if (supplicant == TREE_VALUE (friends))
68 return 1;
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;
80 if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
81 && is_specialization_of (supplicant,
82 TREE_VALUE (friends)))
83 return 1;
85 break;
89 else
90 /* It's a type. */
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;
100 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
101 for (; list ; list = TREE_CHAIN (list))
103 tree t = TREE_VALUE (list);
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;
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;
120 /* A namespace is not friend to anybody. */
121 if (context && TREE_CODE (context) == NAMESPACE_DECL)
122 context = NULL_TREE;
124 if (context)
125 return is_friend (type, context);
127 return 0;
130 /* Add a new friend to the friends of the aggregate type TYPE.
131 DECL is the FUNCTION_DECL of the friend being added. */
133 void
134 add_friend (type, decl)
135 tree type, decl;
137 tree typedecl;
138 tree list;
139 tree name;
141 if (decl == error_mark_node)
142 return;
144 typedecl = TYPE_MAIN_DECL (type);
145 list = DECL_FRIENDLIST (typedecl);
146 name = DECL_NAME (decl);
147 type = TREE_TYPE (typedecl);
149 while (list)
151 if (name == FRIEND_NAME (list))
153 tree friends = FRIEND_DECLS (list);
154 for (; friends ; friends = TREE_CHAIN (friends))
156 if (decl == TREE_VALUE (friends))
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;
166 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
168 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
169 TREE_VALUE (list));
170 return;
172 list = TREE_CHAIN (list);
175 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
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));
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! */
196 void
197 make_friend_class (type, friend_type)
198 tree type, friend_type;
200 tree classes;
201 int is_template_friend;
203 if (! IS_AGGR_TYPE (friend_type))
205 error ("invalid type `%T' declared `friend'", friend_type);
206 return;
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. */
214 if (CLASS_TYPE_P (friend_type)
215 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
216 && uses_template_parms (friend_type))
218 /* [temp.friend]
219 Friend declarations shall not declare partial
220 specializations. */
221 error ("partial specialization `%T' declared `friend'",
222 friend_type);
223 return;
226 is_template_friend = 1;
228 else if (same_type_p (type, friend_type))
230 pedwarn ("class `%T' is implicitly friends with itself",
231 type);
232 return;
234 else
235 is_template_friend = 0;
237 /* [temp.friend]
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)
247 /* template <class T> friend typename S<T>::X; */
248 error ("typename type `%#T' declared `friend'", friend_type);
249 return;
251 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
253 /* template <class T> friend class T; */
254 error ("template parameter type `%T' declared `friend'", friend_type);
255 return;
257 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
259 /* template <class T> friend class A; where A is not a template */
260 error ("`%#T' is not a template", friend_type);
261 return;
264 if (is_template_friend)
265 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
267 classes = CLASSTYPE_FRIEND_CLASSES (type);
268 while (classes
269 /* Stop if we find the same type on the list. */
270 && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
271 friend_type == TREE_VALUE (classes) :
272 same_type_p (TREE_VALUE (classes), friend_type)))
273 classes = TREE_CHAIN (classes);
274 if (classes)
275 warning ("`%T' is already a friend of `%T'",
276 TREE_VALUE (classes), type);
277 else
279 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
281 CLASSTYPE_FRIEND_CLASSES (type)
282 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
283 if (is_template_friend)
284 friend_type = TREE_TYPE (friend_type);
285 if (!uses_template_parms (type))
286 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
287 = tree_cons (NULL_TREE, type,
288 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
292 /* Main friend processor. This is large, and for modularity purposes,
293 has been removed from grokdeclarator. It returns `void_type_node'
294 to indicate that something happened, though a FIELD_DECL is
295 not returned.
297 CTYPE is the class this friend belongs to.
299 DECLARATOR is the name of the friend.
301 DECL is the FUNCTION_DECL that the friend is.
303 In case we are parsing a friend which is part of an inline
304 definition, we will need to store PARM_DECL chain that comes
305 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
307 FLAGS is just used for `grokclassfn'.
309 QUALS say what special qualifies should apply to the object
310 pointed to by `this'. */
312 tree
313 do_friend (ctype, declarator, decl, parmdecls, attrlist,
314 flags, quals, funcdef_flag)
315 tree ctype, declarator, decl, parmdecls, attrlist;
316 enum overload_flags flags;
317 tree quals;
318 int funcdef_flag;
320 int is_friend_template = 0;
322 /* Every decl that gets here is a friend of something. */
323 DECL_FRIEND_P (decl) = 1;
325 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
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));
334 if (TREE_CODE (decl) != FUNCTION_DECL)
335 abort ();
337 is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
339 if (ctype)
341 tree cname = TYPE_NAME (ctype);
342 if (TREE_CODE (cname) == TYPE_DECL)
343 cname = DECL_NAME (cname);
345 /* A method friend. */
346 if (flags == NO_SPECIAL && ctype && declarator == cname)
347 DECL_CONSTRUCTOR_P (decl) = 1;
349 /* This will set up DECL_ARGUMENTS for us. */
350 grokclassfn (ctype, decl, flags, quals);
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);
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 (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
367 decl = check_classfn (ctype, decl);
369 if (decl)
370 add_friend (current_class_type, decl);
372 else
373 error ("member `%D' declared as friend before type `%T' defined",
374 decl, ctype);
376 /* A global friend.
377 @@ or possibly a friend from a base class ?!? */
378 else if (TREE_CODE (decl) == FUNCTION_DECL)
380 /* Friends must all go through the overload machinery,
381 even though they may not technically be overloaded.
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);
389 if (! DECL_USE_TEMPLATE (decl))
391 /* We must check whether the decl refers to template
392 arguments before push_template_decl_real adds a
393 reference to the containing template class. */
394 int warn = (warn_nontemplate_friend
395 && ! funcdef_flag && ! is_friend_template
396 && current_template_parms
397 && uses_template_parms (decl));
399 if (is_friend_template
400 || template_class_depth (current_class_type) != 0)
401 /* We can't call pushdecl for a template class, since in
402 general, such a declaration depends on template
403 parameters. Instead, we call pushdecl when the class
404 is instantiated. */
405 decl = push_template_decl_real (decl, /*is_friend=*/1);
406 else if (current_function_decl)
407 /* This must be a local class, so pushdecl will be ok, and
408 insert an unqualified friend into the local scope
409 (rather than the containing namespace scope, which the
410 next choice will do). */
411 decl = pushdecl (decl);
412 else
414 /* We can't use pushdecl, as we might be in a template
415 class specialization, and pushdecl will insert an
416 unqualified friend decl into the template parameter
417 scope, rather than the namespace containing it. */
418 tree ns = decl_namespace_context (decl);
420 push_nested_namespace (ns);
421 decl = pushdecl_namespace_level (decl);
422 pop_nested_namespace (ns);
425 if (warn)
427 static int explained;
428 warning ("friend declaration `%#D' declares a non-template function", decl);
429 if (! explained)
431 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");
432 explained = 1;
437 add_friend (current_class_type,
438 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
439 DECL_FRIEND_P (decl) = 1;
442 /* Unfortunately, we have to handle attributes here. Normally we would
443 handle them in start_decl_1, but since this is a friend decl start_decl_1
444 never gets to see it. */
446 /* Set attributes here so if duplicate decl, will have proper attributes. */
447 cplus_decl_attributes (&decl, attrlist, 0);
449 return decl;