Merge from vendor branch TCPDUMP:
[dragonfly.git] / contrib / gcc-3.4 / 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   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       /* Nested classes are implicitly friends of their enclosing types, as
82          per core issue 45 (this is a change from the standard).  */
83       for (context = supplicant;
84            context && TYPE_P (context);
85            context = TYPE_CONTEXT (context))
86         if (type == context)
87           return 1;
88       
89       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
90       for (; list ; list = TREE_CHAIN (list))
91         {
92           tree t = TREE_VALUE (list);
93
94           if (TREE_CODE (t) == TEMPLATE_DECL ? 
95               is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
96               same_type_p (supplicant, t))
97             return 1;
98         }
99     }      
100
101   if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
102     context = DECL_CONTEXT (supplicant);
103   else if (! declp)
104     /* Local classes have the same access as the enclosing function.  */
105     context = decl_function_context (TYPE_MAIN_DECL (supplicant));
106   else
107     context = NULL_TREE;
108
109   /* A namespace is not friend to anybody.  */
110   if (context && TREE_CODE (context) == NAMESPACE_DECL)
111     context = NULL_TREE;
112
113   if (context)
114     return is_friend (type, context);
115
116   return 0;
117 }
118
119 /* Add a new friend to the friends of the aggregate type TYPE.
120    DECL is the FUNCTION_DECL of the friend being added.
121
122    If COMPLAIN is true, warning about duplicate friend is issued.
123    We want to have this diagnostics during parsing but not
124    when a template is being instantiated.  */
125
126 void
127 add_friend (tree type, tree decl, bool complain)
128 {
129   tree typedecl;
130   tree list;
131   tree name;
132
133   if (decl == error_mark_node)
134     return;
135
136   typedecl = TYPE_MAIN_DECL (type);
137   list = DECL_FRIENDLIST (typedecl);
138   name = DECL_NAME (decl);
139   type = TREE_TYPE (typedecl);
140
141   while (list)
142     {
143       if (name == FRIEND_NAME (list))
144         {
145           tree friends = FRIEND_DECLS (list);
146           for (; friends ; friends = TREE_CHAIN (friends))
147             {
148               if (decl == TREE_VALUE (friends))
149                 {
150                   if (complain)
151                     warning ("`%D' is already a friend of class `%T'",
152                              decl, type);
153                   return;
154                 }
155             }
156
157           maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
158
159           TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
160                                          TREE_VALUE (list));
161           return;
162         }
163       list = TREE_CHAIN (list);
164     }
165
166   if (DECL_CLASS_SCOPE_P (decl))
167     {
168       tree class_binfo = TYPE_BINFO (DECL_CONTEXT (decl));
169       if (!uses_template_parms (BINFO_TYPE (class_binfo)))
170         perform_or_defer_access_check (class_binfo, decl);
171     }
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   int is_template_friend;
203
204   if (! IS_AGGR_TYPE (friend_type))
205     {
206       error ("invalid type `%T' declared `friend'", friend_type);
207       return;
208     }
209
210   if (processing_template_decl > template_class_depth (type))
211     /* If the TYPE is a template then it makes sense for it to be
212        friends with itself; this means that each instantiation is
213        friends with all other instantiations.  */
214     {
215       if (CLASS_TYPE_P (friend_type)
216           && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
217           && uses_template_parms (friend_type))
218         {
219           /* [temp.friend]
220              Friend declarations shall not declare partial
221              specializations.  */
222           error ("partial specialization `%T' declared `friend'",
223                  friend_type);
224           return;
225         }
226   
227       is_template_friend = 1;
228     }
229   else if (same_type_p (type, friend_type))
230     {
231       if (complain)
232         pedwarn ("class `%T' is implicitly friends with itself",
233                  type);
234       return;
235     }
236   else
237     is_template_friend = 0;
238
239   /* [temp.friend]
240
241      A friend of a class or class template can be a function or
242      class template, a specialization of a function template or
243      class template, or an ordinary (nontemplate) function or
244      class.  */
245   if (!is_template_friend)
246     ;/* ok */
247   else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
248     {
249       /* template <class T> friend typename S<T>::X; */
250       error ("typename type `%#T' declared `friend'", friend_type);
251       return;
252     }
253   else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
254     {
255       /* template <class T> friend class T; */
256       error ("template parameter type `%T' declared `friend'", friend_type);
257       return;
258     }
259   else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
260     {
261       /* template <class T> friend class A; where A is not a template */
262       error ("`%#T' is not a template", friend_type);
263       return;
264     }
265
266   if (is_template_friend)
267     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
268
269   /* See if it is already a friend.  */
270   for (classes = CLASSTYPE_FRIEND_CLASSES (type);
271        classes;
272        classes = TREE_CHAIN (classes))
273     {
274       tree probe = TREE_VALUE (classes);
275
276       if (TREE_CODE (friend_type) == TEMPLATE_DECL)
277         {
278           if (friend_type == probe)
279             {
280               if (complain)
281                 warning ("`%D' is already a friend of `%T'",
282                          probe, type);
283               break;
284             }
285         }
286       else if (TREE_CODE (probe) != TEMPLATE_DECL)
287         {
288           if (same_type_p (probe, friend_type))
289             {
290               if (complain)
291                 warning ("`%T' is already a friend of `%T'",
292                          probe, type);
293               break;
294             }
295         }
296     }
297   
298   if (!classes) 
299     {
300       maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
301
302       CLASSTYPE_FRIEND_CLASSES (type)
303         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
304       if (is_template_friend)
305         friend_type = TREE_TYPE (friend_type);
306       if (!uses_template_parms (type))
307         CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
308           = tree_cons (NULL_TREE, type, 
309                        CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); 
310     }
311 }
312
313 /* Main friend processor. 
314
315    CTYPE is the class this friend belongs to.
316
317    DECLARATOR is the name of the friend.
318
319    DECL is the FUNCTION_DECL that the friend is.
320
321    FLAGS is just used for `grokclassfn'.
322
323    QUALS say what special qualifies should apply to the object
324    pointed to by `this'.  */
325
326 tree
327 do_friend (tree ctype, tree declarator, tree decl,
328            tree attrlist, enum overload_flags flags, tree quals,
329            int funcdef_flag)
330 {
331   /* Every decl that gets here is a friend of something.  */
332   DECL_FRIEND_P (decl) = 1;
333
334   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
335     {
336       declarator = TREE_OPERAND (declarator, 0);
337       if (is_overloaded_fn (declarator))
338         declarator = DECL_NAME (get_first_fn (declarator));
339     }
340
341   if (TREE_CODE (decl) != FUNCTION_DECL)
342     abort ();
343
344   if (ctype)
345     {
346       /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
347          the enclosing class.  FRIEND_DEPTH counts the number of template
348          headers used for this friend declaration.  TEMPLATE_MEMBER_P is
349          true if a template header in FRIEND_DEPTH is intended for
350          DECLARATOR.  For example, the code
351
352            template <class T> struct A {
353              template <class U> struct B {
354                template <class V> template <class W>
355                  friend void C<V>::f(W);
356              };
357            };
358
359          will eventually give the following results
360
361          1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
362          2. FRIEND_DEPTH equals 2 (for `V' and `W').
363          3. TEMPLATE_MEMBER_P is true (for `W').  */
364
365       int class_template_depth = template_class_depth (current_class_type);
366       int friend_depth = processing_template_decl - class_template_depth;
367       /* We will figure this out later.  */
368       bool template_member_p = false;
369
370       tree cname = TYPE_NAME (ctype);
371       if (TREE_CODE (cname) == TYPE_DECL)
372         cname = DECL_NAME (cname);
373
374       /* A method friend.  */
375       if (flags == NO_SPECIAL && declarator == cname)
376         DECL_CONSTRUCTOR_P (decl) = 1;
377
378       /* This will set up DECL_ARGUMENTS for us.  */
379       grokclassfn (ctype, decl, flags, quals);
380
381       if (friend_depth)
382         {
383           if (!uses_template_parms_level (ctype, class_template_depth
384                                                  + friend_depth))
385             template_member_p = true;
386         }
387
388       /* A nested class may declare a member of an enclosing class
389          to be a friend, so we do lookup here even if CTYPE is in
390          the process of being defined.  */
391       if (class_template_depth
392           || COMPLETE_TYPE_P (ctype)
393           || TYPE_BEING_DEFINED (ctype))
394         {
395           if (DECL_TEMPLATE_INFO (decl))
396             /* DECL is a template specialization.  No need to
397                build a new TEMPLATE_DECL.  */
398             ;
399           else if (class_template_depth)
400             /* We rely on tsubst_friend_function to check the
401                validity of the declaration later.  */
402             decl = push_template_decl_real (decl, /*is_friend=*/1);
403           else
404             decl = check_classfn (ctype, decl, template_member_p);
405
406           if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
407             decl = DECL_TI_TEMPLATE (decl);
408
409           if (decl)
410             add_friend (current_class_type, decl, /*complain=*/true);
411         }
412       else
413         error ("member `%D' declared as friend before type `%T' defined",
414                   decl, ctype);
415     }
416   /* A global friend.
417      @@ or possibly a friend from a base class ?!?  */
418   else if (TREE_CODE (decl) == FUNCTION_DECL)
419     {
420       int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
421
422       /* Friends must all go through the overload machinery,
423          even though they may not technically be overloaded.
424
425          Note that because classes all wind up being top-level
426          in their scope, their friend wind up in top-level scope as well.  */
427       if (funcdef_flag)
428         SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
429
430       if (! DECL_USE_TEMPLATE (decl))
431         {
432           /* We must check whether the decl refers to template
433              arguments before push_template_decl_real adds a
434              reference to the containing template class.  */
435           int warn = (warn_nontemplate_friend
436                       && ! funcdef_flag && ! is_friend_template
437                       && current_template_parms
438                       && uses_template_parms (decl));
439
440           if (is_friend_template
441               || template_class_depth (current_class_type) != 0)
442             /* We can't call pushdecl for a template class, since in
443                general, such a declaration depends on template
444                parameters.  Instead, we call pushdecl when the class
445                is instantiated.  */
446             decl = push_template_decl_real (decl, /*is_friend=*/1); 
447           else if (current_function_decl)
448             /* This must be a local class, so pushdecl will be ok, and
449                insert an unqualified friend into the local scope
450                (rather than the containing namespace scope, which the
451                next choice will do).  */
452             decl = pushdecl (decl);
453           else
454             {
455               /* We can't use pushdecl, as we might be in a template
456                  class specialization, and pushdecl will insert an
457                  unqualified friend decl into the template parameter
458                  scope, rather than the namespace containing it.  */
459               tree ns = decl_namespace_context (decl);
460               
461               push_nested_namespace (ns);
462               decl = pushdecl_namespace_level (decl);
463               pop_nested_namespace (ns);
464             }
465
466           if (warn)
467             {
468               static int explained;
469               warning ("friend declaration `%#D' declares a non-template function", decl);
470               if (! explained)
471                 {
472                   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");
473                   explained = 1;
474                 }
475             }
476         }
477
478       if (decl == error_mark_node)
479         return error_mark_node;
480       
481       add_friend (current_class_type, 
482                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
483                   /*complain=*/true);
484       DECL_FRIEND_P (decl) = 1;
485     }
486
487   /* Unfortunately, we have to handle attributes here.  Normally we would
488      handle them in start_decl_1, but since this is a friend decl start_decl_1
489      never gets to see it.  */
490
491   /* Set attributes here so if duplicate decl, will have proper attributes.  */
492   cplus_decl_attributes (&decl, attrlist, 0);
493
494   return decl;
495 }