OSDN Git Service

PR c++/641, c++/11876
[pf3gnuchains/gcc-fork.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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"
33
34 /* Friend data structures are described in cp-tree.h.  */
35
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE.  */
37
38 int
39 is_friend (tree type, tree 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    If COMPLAIN is true, warning about duplicate friend is issued.
133    We want to have this diagnostics during parsing but not
134    when a template is being instantiated.  */
135
136 void
137 add_friend (tree type, tree decl, bool complain)
138 {
139   tree typedecl;
140   tree list;
141   tree name;
142
143   if (decl == error_mark_node)
144     return;
145
146   typedecl = TYPE_MAIN_DECL (type);
147   list = DECL_FRIENDLIST (typedecl);
148   name = DECL_NAME (decl);
149   type = TREE_TYPE (typedecl);
150
151   while (list)
152     {
153       if (name == FRIEND_NAME (list))
154         {
155           tree friends = FRIEND_DECLS (list);
156           for (; friends ; friends = TREE_CHAIN (friends))
157             {
158               if (decl == TREE_VALUE (friends))
159                 {
160                   if (complain)
161                     {
162                       warning ("`%D' is already a friend of class `%T'",
163                                decl, type);
164                       cp_warning_at ("previous friend declaration of `%D'",
165                                      TREE_VALUE (friends));
166                     }
167                   return;
168                 }
169             }
170
171           maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
172
173           TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
174                                          TREE_VALUE (list));
175           return;
176         }
177       list = TREE_CHAIN (list);
178     }
179
180   if (DECL_CLASS_SCOPE_P (decl))
181     perform_or_defer_access_check (TYPE_BINFO (DECL_CONTEXT (decl)), decl);
182
183   maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
184
185   DECL_FRIENDLIST (typedecl)
186     = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
187                  DECL_FRIENDLIST (typedecl));
188   if (!uses_template_parms (type))
189     DECL_BEFRIENDING_CLASSES (decl) 
190       = tree_cons (NULL_TREE, type,
191                    DECL_BEFRIENDING_CLASSES (decl));
192 }
193
194 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
195    been defined, we make all of its member functions friends of
196    TYPE.  If not, we make it a pending friend, which can later be added
197    when its definition is seen.  If a type is defined, then its TYPE_DECL's
198    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
199    classes that are not defined.  If a type has not yet been defined,
200    then the DECL_WAITING_FRIENDS contains a list of types
201    waiting to make it their friend.  Note that these two can both
202    be in use at the same time!
203
204    If COMPLAIN is true, warning about duplicate friend is issued.
205    We want to have this diagnostics during parsing but not
206    when a template is being instantiated.  */
207
208 void
209 make_friend_class (tree type, tree friend_type, bool complain)
210 {
211   tree classes;
212   int is_template_friend;
213
214   if (! IS_AGGR_TYPE (friend_type))
215     {
216       error ("invalid type `%T' declared `friend'", 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     {
225       if (CLASS_TYPE_P (friend_type)
226           && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
227           && uses_template_parms (friend_type))
228         {
229           /* [temp.friend]
230              Friend declarations shall not declare partial
231              specializations.  */
232           error ("partial specialization `%T' declared `friend'",
233                  friend_type);
234           return;
235         }
236   
237       is_template_friend = 1;
238     }
239   else if (same_type_p (type, friend_type))
240     {
241       if (complain)
242         pedwarn ("class `%T' is implicitly friends with itself",
243                  type);
244       return;
245     }
246   else
247     is_template_friend = 0;
248
249   /* [temp.friend]
250
251      A friend of a class or class template can be a function or
252      class template, a specialization of a function template or
253      class template, or an ordinary (nontemplate) function or
254      class.  */
255   if (!is_template_friend)
256     ;/* ok */
257   else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
258     {
259       /* template <class T> friend typename S<T>::X; */
260       error ("typename type `%#T' declared `friend'", friend_type);
261       return;
262     }
263   else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
264     {
265       /* template <class T> friend class T; */
266       error ("template parameter type `%T' declared `friend'", friend_type);
267       return;
268     }
269   else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
270     {
271       /* template <class T> friend class A; where A is not a template */
272       error ("`%#T' is not a template", friend_type);
273       return;
274     }
275
276   if (is_template_friend)
277     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
278
279   /* See if it is already a friend.  */
280   for (classes = CLASSTYPE_FRIEND_CLASSES (type);
281        classes;
282        classes = TREE_CHAIN (classes))
283     {
284       tree probe = TREE_VALUE (classes);
285
286       if (TREE_CODE (friend_type) == TEMPLATE_DECL)
287         {
288           if (friend_type == probe)
289             {
290               if (complain)
291                 warning ("`%D' is already a friend of `%T'",
292                          probe, type);
293               break;
294             }
295         }
296       else if (TREE_CODE (probe) != TEMPLATE_DECL)
297         {
298           if (same_type_p (probe, friend_type))
299             {
300               if (complain)
301                 warning ("`%T' is already a friend of `%T'",
302                          probe, type);
303               break;
304             }
305         }
306     }
307   
308   if (!classes) 
309     {
310       maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
311
312       CLASSTYPE_FRIEND_CLASSES (type)
313         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
314       if (is_template_friend)
315         friend_type = TREE_TYPE (friend_type);
316       if (!uses_template_parms (type))
317         CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
318           = tree_cons (NULL_TREE, type, 
319                        CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); 
320     }
321 }
322
323 /* Main friend processor. 
324
325    CTYPE is the class this friend belongs to.
326
327    DECLARATOR is the name of the friend.
328
329    DECL is the FUNCTION_DECL that the friend is.
330
331    In case we are parsing a friend which is part of an inline
332    definition, we will need to store PARM_DECL chain that comes
333    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
334
335    FLAGS is just used for `grokclassfn'.
336
337    QUALS say what special qualifies should apply to the object
338    pointed to by `this'.  */
339
340 tree
341 do_friend (tree ctype, tree declarator, tree decl, tree parmdecls,
342            tree attrlist, enum overload_flags flags, tree quals,
343            int funcdef_flag)
344 {
345   int is_friend_template = 0;
346
347   /* Every decl that gets here is a friend of something.  */
348   DECL_FRIEND_P (decl) = 1;
349
350   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
351     {
352       declarator = TREE_OPERAND (declarator, 0);
353       if (is_overloaded_fn (declarator))
354         declarator = DECL_NAME (get_first_fn (declarator));
355     }
356
357   if (TREE_CODE (decl) != FUNCTION_DECL)
358     abort ();
359
360   is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
361
362   if (ctype)
363     {
364       tree cname = TYPE_NAME (ctype);
365       if (TREE_CODE (cname) == TYPE_DECL)
366         cname = DECL_NAME (cname);
367
368       /* A method friend.  */
369       if (flags == NO_SPECIAL && ctype && declarator == cname)
370         DECL_CONSTRUCTOR_P (decl) = 1;
371
372       /* This will set up DECL_ARGUMENTS for us.  */
373       grokclassfn (ctype, decl, flags, quals);
374
375       if (is_friend_template)
376         decl = DECL_TI_TEMPLATE (push_template_decl (decl));
377       else if (DECL_TEMPLATE_INFO (decl))
378         ;
379       else if (template_class_depth (current_class_type))
380         decl = push_template_decl_real (decl, /*is_friend=*/1);
381
382       /* We can't do lookup in a type that involves template
383          parameters.  Instead, we rely on tsubst_friend_function
384          to check the validity of the declaration later.  */
385       if (processing_template_decl)
386         add_friend (current_class_type, decl, /*complain=*/true);
387       /* A nested class may declare a member of an enclosing class
388          to be a friend, so we do lookup here even if CTYPE is in
389          the process of being defined.  */
390       else if (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
391         {
392           decl = check_classfn (ctype, decl);
393
394           if (decl)
395             add_friend (current_class_type, decl, /*complain=*/true);
396         }
397       else
398         error ("member `%D' declared as friend before type `%T' defined",
399                   decl, ctype);
400     }
401   /* A global friend.
402      @@ or possibly a friend from a base class ?!?  */
403   else if (TREE_CODE (decl) == FUNCTION_DECL)
404     {
405       /* Friends must all go through the overload machinery,
406          even though they may not technically be overloaded.
407
408          Note that because classes all wind up being top-level
409          in their scope, their friend wind up in top-level scope as well.  */
410       DECL_ARGUMENTS (decl) = parmdecls;
411       if (funcdef_flag)
412         SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
413
414       if (! DECL_USE_TEMPLATE (decl))
415         {
416           /* We must check whether the decl refers to template
417              arguments before push_template_decl_real adds a
418              reference to the containing template class.  */
419           int warn = (warn_nontemplate_friend
420                       && ! funcdef_flag && ! is_friend_template
421                       && current_template_parms
422                       && uses_template_parms (decl));
423
424           if (is_friend_template
425               || template_class_depth (current_class_type) != 0)
426             /* We can't call pushdecl for a template class, since in
427                general, such a declaration depends on template
428                parameters.  Instead, we call pushdecl when the class
429                is instantiated.  */
430             decl = push_template_decl_real (decl, /*is_friend=*/1); 
431           else if (current_function_decl)
432             /* This must be a local class, so pushdecl will be ok, and
433                insert an unqualified friend into the local scope
434                (rather than the containing namespace scope, which the
435                next choice will do).  */
436             decl = pushdecl (decl);
437           else
438             {
439               /* We can't use pushdecl, as we might be in a template
440                  class specialization, and pushdecl will insert an
441                  unqualified friend decl into the template parameter
442                  scope, rather than the namespace containing it.  */
443               tree ns = decl_namespace_context (decl);
444               
445               push_nested_namespace (ns);
446               decl = pushdecl_namespace_level (decl);
447               pop_nested_namespace (ns);
448             }
449
450           if (warn)
451             {
452               static int explained;
453               warning ("friend declaration `%#D' declares a non-template function", decl);
454               if (! explained)
455                 {
456                   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");
457                   explained = 1;
458                 }
459             }
460         }
461
462       add_friend (current_class_type, 
463                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
464                   /*complain=*/true);
465       DECL_FRIEND_P (decl) = 1;
466     }
467
468   /* Unfortunately, we have to handle attributes here.  Normally we would
469      handle them in start_decl_1, but since this is a friend decl start_decl_1
470      never gets to see it.  */
471
472   /* Set attributes here so if duplicate decl, will have proper attributes.  */
473   cplus_decl_attributes (&decl, attrlist, 0);
474
475   return decl;
476 }