OSDN Git Service

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