Merge branch 'vendor/BZIP'
[dragonfly.git] / contrib / gcc-4.1 / gcc / cp / friend.c
1 /* Help friends in C++.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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   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                   tree friend = TREE_VALUE (friends);
64
65                   if (friend == NULL_TREE)
66                     continue;
67
68                   if (supplicant == friend)
69                     return 1;
70
71                   if (is_specialization_of_friend (supplicant, friend))
72                     return 1;
73                 }
74               break;
75             }
76         }
77     }
78   else
79     /* It's a type.  */
80     {
81       if (same_type_p (supplicant, type))
82         return 1;
83
84       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
85       for (; list ; list = TREE_CHAIN (list))
86         {
87           tree t = TREE_VALUE (list);
88
89           if (TREE_CODE (t) == TEMPLATE_DECL ?
90               is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
91               same_type_p (supplicant, t))
92             return 1;
93         }
94     }
95
96   if (declp)
97     {
98       if (DECL_FUNCTION_MEMBER_P (supplicant))
99         context = DECL_CONTEXT (supplicant);
100       else
101         context = NULL_TREE;
102     }
103   else
104     {
105       if (TYPE_CONTEXT (supplicant)
106           && TYPE_P (TYPE_CONTEXT (supplicant)))
107         /* Nested classes get the same access as their enclosing types, as
108            per DR 45 (this is a change from the standard).  */
109         context = TYPE_CONTEXT (supplicant);
110       else
111         /* Local classes have the same access as the enclosing function.  */
112         context = decl_function_context (TYPE_MAIN_DECL (supplicant));
113     }
114
115   /* A namespace is not friend to anybody.  */
116   if (context && TREE_CODE (context) == NAMESPACE_DECL)
117     context = NULL_TREE;
118
119   if (context)
120     return is_friend (type, context);
121
122   return 0;
123 }
124
125 /* Add a new friend to the friends of the aggregate type TYPE.
126    DECL is the FUNCTION_DECL of the friend being added.
127
128    If COMPLAIN is true, warning about duplicate friend is issued.
129    We want to have this diagnostics during parsing but not
130    when a template is being instantiated.  */
131
132 void
133 add_friend (tree type, tree decl, bool complain)
134 {
135   tree typedecl;
136   tree list;
137   tree name;
138   tree ctx;
139
140   if (decl == error_mark_node)
141     return;
142
143   typedecl = TYPE_MAIN_DECL (type);
144   list = DECL_FRIENDLIST (typedecl);
145   name = DECL_NAME (decl);
146   type = TREE_TYPE (typedecl);
147
148   while (list)
149     {
150       if (name == FRIEND_NAME (list))
151         {
152           tree friends = FRIEND_DECLS (list);
153           for (; friends ; friends = TREE_CHAIN (friends))
154             {
155               if (decl == TREE_VALUE (friends))
156                 {
157                   if (complain)
158                     warning (0, "%qD is already a friend of class %qT",
159                              decl, type);
160                   return;
161                 }
162             }
163
164           maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
165
166           TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
167                                          TREE_VALUE (list));
168           return;
169         }
170       list = TREE_CHAIN (list);
171     }
172
173   ctx = DECL_CONTEXT (decl);
174   if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
175     perform_or_defer_access_check (TYPE_BINFO (ctx), decl);
176
177   maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
178
179   DECL_FRIENDLIST (typedecl)
180     = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
181                  DECL_FRIENDLIST (typedecl));
182   if (!uses_template_parms (type))
183     DECL_BEFRIENDING_CLASSES (decl)
184       = tree_cons (NULL_TREE, type,
185                    DECL_BEFRIENDING_CLASSES (decl));
186 }
187
188 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
189    been defined, we make all of its member functions friends of
190    TYPE.  If not, we make it a pending friend, which can later be added
191    when its definition is seen.  If a type is defined, then its TYPE_DECL's
192    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
193    classes that are not defined.  If a type has not yet been defined,
194    then the DECL_WAITING_FRIENDS contains a list of types
195    waiting to make it their friend.  Note that these two can both
196    be in use at the same time!
197
198    If COMPLAIN is true, warning about duplicate friend is issued.
199    We want to have this diagnostics during parsing but not
200    when a template is being instantiated.  */
201
202 void
203 make_friend_class (tree type, tree friend_type, bool complain)
204 {
205   tree classes;
206
207   /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
208      the enclosing class.  FRIEND_DEPTH counts the number of template
209      headers used for this friend declaration.  TEMPLATE_MEMBER_P,
210      defined inside the `if' block for TYPENAME_TYPE case, is true if
211      a template header in FRIEND_DEPTH is intended for DECLARATOR.
212      For example, the code
213
214        template <class T> struct A {
215          template <class U> struct B {
216            template <class V> template <class W>
217              friend class C<V>::D;
218          };
219        };
220
221      will eventually give the following results
222
223      1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
224      2. FRIEND_DEPTH equals 2 (for `V' and `W').
225      3. TEMPLATE_MEMBER_P is true (for `W').
226
227      The friend is a template friend iff FRIEND_DEPTH is nonzero.  */
228
229   int class_template_depth = template_class_depth (type);
230   int friend_depth = processing_template_decl - class_template_depth;
231
232   if (! IS_AGGR_TYPE (friend_type))
233     {
234       error ("invalid type %qT declared %<friend%>", friend_type);
235       return;
236     }
237
238   if (friend_depth)
239     /* If the TYPE is a template then it makes sense for it to be
240        friends with itself; this means that each instantiation is
241        friends with all other instantiations.  */
242     {
243       if (CLASS_TYPE_P (friend_type)
244           && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
245           && uses_template_parms (friend_type))
246         {
247           /* [temp.friend]
248              Friend declarations shall not declare partial
249              specializations.  */
250           error ("partial specialization %qT declared %<friend%>",
251                  friend_type);
252           return;
253         }
254     }
255   else if (same_type_p (type, friend_type))
256     {
257       if (complain)
258         pedwarn ("class %qT is implicitly friends with itself",
259                  type);
260       return;
261     }
262
263   /* [temp.friend]
264
265      A friend of a class or class template can be a function or
266      class template, a specialization of a function template or
267      class template, or an ordinary (nontemplate) function or
268      class.  */
269   if (!friend_depth)
270     ;/* ok */
271   else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
272     {
273       if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
274           == TEMPLATE_ID_EXPR)
275         {
276           /* template <class U> friend class T::X<U>; */
277           /* [temp.friend]
278              Friend declarations shall not declare partial
279              specializations.  */
280           error ("partial specialization %qT declared %<friend%>",
281                  friend_type);
282           return;
283         }
284       else
285         {
286           /* We will figure this out later.  */
287           bool template_member_p = false;
288
289           tree ctype = TYPE_CONTEXT (friend_type);
290           tree name = TYPE_IDENTIFIER (friend_type);
291           tree decl;
292
293           if (!uses_template_parms_level (ctype, class_template_depth
294                                                  + friend_depth))
295             template_member_p = true;
296
297           if (class_template_depth)
298             {
299               /* We rely on tsubst_friend_class to check the
300                  validity of the declaration later.  */
301               if (template_member_p)
302                 friend_type
303                   = make_unbound_class_template (ctype,
304                                                  name,
305                                                  current_template_parms,
306                                                  tf_error);
307               else
308                 friend_type
309                   = make_typename_type (ctype, name, class_type, tf_error);
310             }
311           else
312             {
313               decl = lookup_member (ctype, name, 0, true);
314               if (!decl)
315                 {
316                   error ("%qT is not a member of %qT", name, ctype);
317                   return;
318                 }
319               if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
320                 {
321                   error ("%qT is not a member class template of %qT",
322                          name, ctype);
323                   error ("%q+D declared here", decl);
324                   return;
325                 }
326               if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
327                                          || !CLASS_TYPE_P (TREE_TYPE (decl))))
328                 {
329                   error ("%qT is not a nested class of %qT",
330                          name, ctype);
331                   error ("%q+D declared here", decl);
332                   return;
333                 }
334
335               friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
336             }
337         }
338     }
339   else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
340     {
341       /* template <class T> friend class T; */
342       error ("template parameter type %qT declared %<friend%>", friend_type);
343       return;
344     }
345   else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
346     {
347       /* template <class T> friend class A; where A is not a template */
348       error ("%q#T is not a template", friend_type);
349       return;
350     }
351   else
352     /* template <class T> friend class A; where A is a template */
353     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
354
355   if (friend_type == error_mark_node)
356     return;
357
358   /* See if it is already a friend.  */
359   for (classes = CLASSTYPE_FRIEND_CLASSES (type);
360        classes;
361        classes = TREE_CHAIN (classes))
362     {
363       tree probe = TREE_VALUE (classes);
364
365       if (TREE_CODE (friend_type) == TEMPLATE_DECL)
366         {
367           if (friend_type == probe)
368             {
369               if (complain)
370                 warning (0, "%qD is already a friend of %qT", probe, type);
371               break;
372             }
373         }
374       else if (TREE_CODE (probe) != TEMPLATE_DECL)
375         {
376           if (same_type_p (probe, friend_type))
377             {
378               if (complain)
379                 warning (0, "%qT is already a friend of %qT", probe, type);
380               break;
381             }
382         }
383     }
384
385   if (!classes)
386     {
387       maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
388
389       CLASSTYPE_FRIEND_CLASSES (type)
390         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
391       if (TREE_CODE (friend_type) == TEMPLATE_DECL)
392         friend_type = TREE_TYPE (friend_type);
393       if (!uses_template_parms (type))
394         CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
395           = tree_cons (NULL_TREE, type,
396                        CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
397     }
398 }
399
400 /* Main friend processor.
401
402    CTYPE is the class this friend belongs to.
403
404    DECLARATOR is the name of the friend.
405
406    DECL is the FUNCTION_DECL that the friend is.
407
408    FLAGS is just used for `grokclassfn'.
409
410    QUALS say what special qualifies should apply to the object
411    pointed to by `this'.  */
412
413 tree
414 do_friend (tree ctype, tree declarator, tree decl,
415            tree attrlist, enum overload_flags flags,
416            cp_cv_quals quals,
417            int funcdef_flag)
418 {
419   /* Every decl that gets here is a friend of something.  */
420   DECL_FRIEND_P (decl) = 1;
421
422   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
423     {
424       declarator = TREE_OPERAND (declarator, 0);
425       if (is_overloaded_fn (declarator))
426         declarator = DECL_NAME (get_first_fn (declarator));
427     }
428
429   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
430
431   if (ctype)
432     {
433       /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
434          the enclosing class.  FRIEND_DEPTH counts the number of template
435          headers used for this friend declaration.  TEMPLATE_MEMBER_P is
436          true if a template header in FRIEND_DEPTH is intended for
437          DECLARATOR.  For example, the code
438
439            template <class T> struct A {
440              template <class U> struct B {
441                template <class V> template <class W>
442                  friend void C<V>::f(W);
443              };
444            };
445
446          will eventually give the following results
447
448          1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
449          2. FRIEND_DEPTH equals 2 (for `V' and `W').
450          3. TEMPLATE_MEMBER_P is true (for `W').  */
451
452       int class_template_depth = template_class_depth (current_class_type);
453       int friend_depth = processing_template_decl - class_template_depth;
454       /* We will figure this out later.  */
455       bool template_member_p = false;
456
457       tree cname = TYPE_NAME (ctype);
458       if (TREE_CODE (cname) == TYPE_DECL)
459         cname = DECL_NAME (cname);
460
461       /* A method friend.  */
462       if (flags == NO_SPECIAL && declarator == cname)
463         DECL_CONSTRUCTOR_P (decl) = 1;
464
465       /* This will set up DECL_ARGUMENTS for us.  */
466       grokclassfn (ctype, decl, flags, quals);
467
468       if (friend_depth)
469         {
470           if (!uses_template_parms_level (ctype, class_template_depth
471                                                  + friend_depth))
472             template_member_p = true;
473         }
474
475       /* A nested class may declare a member of an enclosing class
476          to be a friend, so we do lookup here even if CTYPE is in
477          the process of being defined.  */
478       if (class_template_depth
479           || COMPLETE_TYPE_P (ctype)
480           || TYPE_BEING_DEFINED (ctype))
481         {
482           if (DECL_TEMPLATE_INFO (decl))
483             /* DECL is a template specialization.  No need to
484                build a new TEMPLATE_DECL.  */
485             ;
486           else if (class_template_depth)
487             /* We rely on tsubst_friend_function to check the
488                validity of the declaration later.  */
489             decl = push_template_decl_real (decl, /*is_friend=*/true);
490           else
491             decl = check_classfn (ctype, decl,
492                                   template_member_p
493                                   ? current_template_parms
494                                   : NULL_TREE);
495
496           if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
497             decl = DECL_TI_TEMPLATE (decl);
498
499           if (decl)
500             add_friend (current_class_type, decl, /*complain=*/true);
501         }
502       else
503         error ("member %qD declared as friend before type %qT defined",
504                   decl, ctype);
505     }
506   /* A global friend.
507      @@ or possibly a friend from a base class ?!?  */
508   else if (TREE_CODE (decl) == FUNCTION_DECL)
509     {
510       int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
511
512       /* Friends must all go through the overload machinery,
513          even though they may not technically be overloaded.
514
515          Note that because classes all wind up being top-level
516          in their scope, their friend wind up in top-level scope as well.  */
517       if (funcdef_flag)
518         SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
519
520       if (! DECL_USE_TEMPLATE (decl))
521         {
522           /* We must check whether the decl refers to template
523              arguments before push_template_decl_real adds a
524              reference to the containing template class.  */
525           int warn = (warn_nontemplate_friend
526                       && ! funcdef_flag && ! is_friend_template
527                       && current_template_parms
528                       && uses_template_parms (decl));
529
530           if (is_friend_template
531               || template_class_depth (current_class_type) != 0)
532             /* We can't call pushdecl for a template class, since in
533                general, such a declaration depends on template
534                parameters.  Instead, we call pushdecl when the class
535                is instantiated.  */
536             decl = push_template_decl_real (decl, /*is_friend=*/true);
537           else if (current_function_decl)
538             /* This must be a local class, so pushdecl will be ok, and
539                insert an unqualified friend into the local scope
540                (rather than the containing namespace scope, which the
541                next choice will do).  */
542             decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
543           else
544             {
545               /* We can't use pushdecl, as we might be in a template
546                  class specialization, and pushdecl will insert an
547                  unqualified friend decl into the template parameter
548                  scope, rather than the namespace containing it.  */
549               tree ns = decl_namespace_context (decl);
550
551               push_nested_namespace (ns);
552               decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
553               pop_nested_namespace (ns);
554             }
555
556           if (warn)
557             {
558               static int explained;
559               warning (0, "friend declaration %q#D declares a non-template "
560                        "function", decl);
561               if (! explained)
562                 {
563                   warning (0, "(if this is not what you intended, make sure "
564                            "the function template has already been declared "
565                            "and add <> after the function name here) "
566                            "-Wno-non-template-friend disables this warning");
567                   explained = 1;
568                 }
569             }
570         }
571
572       if (decl == error_mark_node)
573         return error_mark_node;
574
575       add_friend (current_class_type,
576                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
577                   /*complain=*/true);
578       DECL_FRIEND_P (decl) = 1;
579     }
580
581   /* Unfortunately, we have to handle attributes here.  Normally we would
582      handle them in start_decl_1, but since this is a friend decl start_decl_1
583      never gets to see it.  */
584
585   /* Set attributes here so if duplicate decl, will have proper attributes.  */
586   cplus_decl_attributes (&decl, attrlist, 0);
587
588   return decl;
589 }