OSDN Git Service

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