OSDN Git Service

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