OSDN Git Service

* cp-tree.h (add_friend): Declare.
[pf3gnuchains/gcc-fork.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2    Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "tree.h"
24 #include "rtl.h"
25 #include "cp-tree.h"
26 #include "flags.h"
27 #include "output.h"
28 #include "toplev.h"
29
30 /* Friend data structures are described in cp-tree.h.  */
31
32 /* Returns non-zero if SUPPLICANT is a friend of TYPE.  */
33
34 int
35 is_friend (type, supplicant)
36      tree type, supplicant;
37 {
38   int declp;
39   register tree list;
40   tree context;
41
42   if (supplicant == NULL_TREE || type == NULL_TREE)
43     return 0;
44
45   declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
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       tree ctype;
53
54       if (DECL_FUNCTION_MEMBER_P (supplicant))
55         ctype = DECL_CLASS_CONTEXT (supplicant);
56       else
57         ctype = NULL_TREE;
58
59       for (; list ; list = TREE_CHAIN (list))
60         {
61           if (name == FRIEND_NAME (list))
62             {
63               tree friends = FRIEND_DECLS (list);
64               for (; friends ; friends = TREE_CHAIN (friends))
65                 {
66                   if (same_type_p (ctype, TREE_PURPOSE (friends)))
67                     return 1;
68
69                   if (TREE_VALUE (friends) == NULL_TREE)
70                     continue;
71
72                   if (supplicant == TREE_VALUE (friends))
73                     return 1;
74
75                   /* With -fguiding-decls we are more lenient about
76                      friendship.  This is bogus in general since two
77                      specializations of a template with non-type
78                      template parameters may have the same type, but
79                      be different.  
80
81                      Temporarily, we are also more lenient to deal
82                      with nested friend functions, for which there can
83                      be more than one FUNCTION_DECL, despite being the
84                      same function.  When that's fixed, the
85                      FUNCTION_MEMBER_P bit can go.  */
86                   if ((flag_guiding_decls 
87                        || DECL_FUNCTION_MEMBER_P (supplicant))
88                       && same_type_p (TREE_TYPE (supplicant),
89                                       TREE_TYPE (TREE_VALUE (friends))))
90                     return 1;
91
92                   if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
93                       && is_specialization_of (supplicant, 
94                                                TREE_VALUE (friends)))
95                     return 1;
96                 }
97               break;
98             }
99         }
100     }
101   else
102     /* It's a type.  */
103     {
104       if (type == supplicant)
105         return 1;
106       
107       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
108       for (; list ; list = TREE_CHAIN (list))
109         {
110           tree t = TREE_VALUE (list);
111
112           if (TREE_CODE (t) == TEMPLATE_DECL ? 
113               is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
114               same_type_p (supplicant, t))
115             return 1;
116         }
117     }      
118
119   if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
120     context = DECL_CLASS_CONTEXT (supplicant);
121   else if (! declp)
122     /* Local classes have the same access as the enclosing function.  */
123     context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
124   else
125     context = NULL_TREE;
126
127   /* A namespace is not friend to anybody. */
128   if (context && TREE_CODE (context) == NAMESPACE_DECL)
129     context = NULL_TREE;
130
131   if (context)
132     return is_friend (type, context);
133
134   return 0;
135 }
136
137 /* Add a new friend to the friends of the aggregate type TYPE.
138    DECL is the FUNCTION_DECL of the friend being added.  */
139
140 void
141 add_friend (type, decl)
142      tree type, decl;
143 {
144   tree typedecl = TYPE_MAIN_DECL (type);
145   tree list = DECL_FRIENDLIST (typedecl);
146   tree name = DECL_NAME (decl);
147
148   type = TREE_TYPE (typedecl);
149
150   while (list)
151     {
152       if (name == FRIEND_NAME (list))
153         {
154           tree friends = FRIEND_DECLS (list);
155           for (; friends ; friends = TREE_CHAIN (friends))
156             {
157               if (decl == TREE_VALUE (friends))
158                 {
159                   cp_warning ("`%D' is already a friend of class `%T'",
160                               decl, type);
161                   cp_warning_at ("previous friend declaration of `%D'",
162                                  TREE_VALUE (friends));
163                   return;
164                 }
165             }
166           TREE_VALUE (list) = tree_cons (error_mark_node, decl,
167                                          TREE_VALUE (list));
168           return;
169         }
170       list = TREE_CHAIN (list);
171     }
172
173   DECL_FRIENDLIST (typedecl)
174     = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
175                  DECL_FRIENDLIST (typedecl));
176   if (!uses_template_parms (type))
177     DECL_BEFRIENDING_CLASSES (decl) 
178       = tree_cons (NULL_TREE, type,
179                    DECL_BEFRIENDING_CLASSES (decl));
180 }
181
182 /* Declare that every member function NAME in FRIEND_TYPE
183    (which may be NULL_TREE) is a friend of type TYPE.  */
184
185 void
186 add_friends (type, name, friend_type)
187      tree type, name, friend_type;
188 {
189   tree typedecl = TYPE_MAIN_DECL (type);
190   tree list = DECL_FRIENDLIST (typedecl);
191
192   while (list)
193     {
194       if (name == FRIEND_NAME (list))
195         {
196           tree friends = FRIEND_DECLS (list);
197           while (friends && TREE_PURPOSE (friends) != friend_type)
198             friends = TREE_CHAIN (friends);
199           if (friends)
200             {
201               if (friend_type)
202                 warning ("method `%s::%s' is already a friend of class",
203                          TYPE_NAME_STRING (friend_type),
204                          IDENTIFIER_POINTER (name));
205               else
206                 warning ("function `%s' is already a friend of class `%s'",
207                          IDENTIFIER_POINTER (name),
208                          IDENTIFIER_POINTER (DECL_NAME (typedecl)));
209             }
210           else
211             TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
212                                            TREE_VALUE (list));
213           return;
214         }
215       list = TREE_CHAIN (list);
216     }
217   DECL_FRIENDLIST (typedecl)
218     = tree_cons (name,
219                  build_tree_list (friend_type, NULL_TREE),
220                  DECL_FRIENDLIST (typedecl));
221 }
222
223 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
224    been defined, we make all of its member functions friends of
225    TYPE.  If not, we make it a pending friend, which can later be added
226    when its definition is seen.  If a type is defined, then its TYPE_DECL's
227    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
228    classes that are not defined.  If a type has not yet been defined,
229    then the DECL_WAITING_FRIENDS contains a list of types
230    waiting to make it their friend.  Note that these two can both
231    be in use at the same time!  */
232
233 void
234 make_friend_class (type, friend_type)
235      tree type, friend_type;
236 {
237   tree classes;
238   int is_template_friend;
239
240   if (IS_SIGNATURE (type))
241     {
242       error ("`friend' declaration in signature definition");
243       return;
244     }
245   if (IS_SIGNATURE (friend_type) || ! IS_AGGR_TYPE (friend_type))
246     {
247       cp_error ("invalid type `%T' declared `friend'", friend_type);
248       return;
249     }
250
251   if (CLASS_TYPE_P (friend_type)
252       && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
253       && uses_template_parms (friend_type))
254     {
255       /* [temp.friend]
256          
257          Friend declarations shall not declare partial
258          specializations.  */
259       cp_error ("partial specialization `%T' declared `friend'",
260                 friend_type);
261       return;
262     }
263
264   if (processing_template_decl > template_class_depth (type))
265     /* If the TYPE is a template then it makes sense for it to be
266        friends with itself; this means that each instantiation is
267        friends with all other instantiations.  */
268     is_template_friend = 1;
269   else if (same_type_p (type, friend_type))
270     {
271       pedwarn ("class `%s' is implicitly friends with itself",
272                TYPE_NAME_STRING (type));
273       return;
274     }
275   else
276     is_template_friend = 0;
277
278   GNU_xref_hier (type, friend_type, 0, 0, 1);
279
280   if (is_template_friend)
281     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
282
283   classes = CLASSTYPE_FRIEND_CLASSES (type);
284   while (classes 
285          /* Stop if we find the same type on the list.  */
286          && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
287               friend_type == TREE_VALUE (classes) :
288               same_type_p (TREE_VALUE (classes), friend_type)))
289     classes = TREE_CHAIN (classes);
290   if (classes) 
291     cp_warning ("`%T' is already a friend of `%T'",
292                 TREE_VALUE (classes), type);
293   else
294     {
295       CLASSTYPE_FRIEND_CLASSES (type)
296         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
297       if (is_template_friend)
298         friend_type = TREE_TYPE (friend_type);
299       if (!uses_template_parms (type))
300         CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
301           = tree_cons (NULL_TREE, type, 
302                        CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); 
303     }
304 }
305
306 /* Main friend processor.  This is large, and for modularity purposes,
307    has been removed from grokdeclarator.  It returns `void_type_node'
308    to indicate that something happened, though a FIELD_DECL is
309    not returned.
310
311    CTYPE is the class this friend belongs to.
312
313    DECLARATOR is the name of the friend.
314
315    DECL is the FUNCTION_DECL that the friend is.
316
317    In case we are parsing a friend which is part of an inline
318    definition, we will need to store PARM_DECL chain that comes
319    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
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 (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
328      tree ctype, declarator, decl, parmdecls;
329      enum overload_flags flags;
330      tree quals;
331      int funcdef_flag;
332 {
333   int is_friend_template = 0;
334
335   /* Every decl that gets here is a friend of something.  */
336   DECL_FRIEND_P (decl) = 1;
337
338   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
339     {
340       declarator = TREE_OPERAND (declarator, 0);
341       if (TREE_CODE (declarator) == LOOKUP_EXPR)
342         declarator = TREE_OPERAND (declarator, 0);
343       if (is_overloaded_fn (declarator))
344         declarator = DECL_NAME (get_first_fn (declarator));
345     }
346
347   if (TREE_CODE (decl) == FUNCTION_DECL)
348     is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
349
350   if (ctype)
351     {
352       tree cname = TYPE_NAME (ctype);
353       if (TREE_CODE (cname) == TYPE_DECL)
354         cname = DECL_NAME (cname);
355
356       /* A method friend.  */
357       if (TREE_CODE (decl) == FUNCTION_DECL)
358         {
359           if (flags == NO_SPECIAL && ctype && declarator == cname)
360             DECL_CONSTRUCTOR_P (decl) = 1;
361
362           /* This will set up DECL_ARGUMENTS for us.  */
363           grokclassfn (ctype, decl, flags, quals);
364
365           if (is_friend_template)
366             decl = DECL_TI_TEMPLATE (push_template_decl (decl));
367           else if (template_class_depth (current_class_type))
368             decl = push_template_decl_real (decl, /*is_friend=*/1);
369
370           /* We can't do lookup in a type that involves template
371              parameters.  Instead, we rely on tsubst_friend_function
372              to check the validity of the declaration later.  */
373           if (uses_template_parms (ctype))
374             add_friend (current_class_type, decl);
375           /* A nested class may declare a member of an enclosing class
376              to be a friend, so we do lookup here even if CTYPE is in
377              the process of being defined.  */
378           else if (TYPE_SIZE (ctype) != 0 || TYPE_BEING_DEFINED (ctype))
379             {
380               decl = check_classfn (ctype, decl);
381
382               if (decl)
383                 add_friend (current_class_type, decl);
384             }
385           else
386             cp_error ("member `%D' declared as friend before type `%T' defined",
387                       decl, ctype);
388         }
389       else
390         {
391           /* Possibly a bunch of method friends.  */
392
393           /* Get the class they belong to.  */
394           tree ctype = IDENTIFIER_TYPE_VALUE (cname);
395           tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
396
397           if (fields)
398             add_friends (current_class_type, declarator, ctype);
399           else
400             cp_error ("method `%D' is not a member of class `%T'",
401                       declarator, ctype);
402           decl = void_type_node;
403         }
404     }
405   /* A global friend.
406      @@ or possibly a friend from a base class ?!?  */
407   else if (TREE_CODE (decl) == FUNCTION_DECL)
408     {
409       /* Friends must all go through the overload machinery,
410          even though they may not technically be overloaded.
411
412          Note that because classes all wind up being top-level
413          in their scope, their friend wind up in top-level scope as well.  */
414       DECL_ARGUMENTS (decl) = parmdecls;
415       if (funcdef_flag)
416         DECL_CLASS_CONTEXT (decl) = current_class_type;
417
418       if (! DECL_USE_TEMPLATE (decl))
419         {
420           /* We can call pushdecl here, because the TREE_CHAIN of this
421              FUNCTION_DECL is not needed for other purposes.  Don't do
422              this for a template instantiation.  However, we don't
423              call pushdecl() for a friend function of a template
424              class, since in general, such a declaration depends on
425              template parameters.  Instead, we call pushdecl when the
426              class is instantiated.  */
427           if (!is_friend_template
428               && template_class_depth (current_class_type) == 0)
429             decl = pushdecl (decl);
430           else 
431             decl = push_template_decl_real (decl, /*is_friend=*/1); 
432
433           if (warn_nontemplate_friend
434               && ! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
435               && current_template_parms && uses_template_parms (decl))
436             {
437               static int explained;
438               cp_warning ("friend declaration `%#D'", decl);
439               warning ("  declares a non-template function");
440               if (! explained)
441                 {
442                   warning ("  (if this is not what you intended, make sure");
443                   warning ("  the function template has already been declared,");
444                   warning ("  and add <> after the function name here)");
445                   warning ("  -Wno-non-template-friend disables this warning.");
446                   explained = 1;
447                 }
448             }
449         }
450
451       make_decl_rtl (decl, NULL_PTR, 1);
452       add_friend (current_class_type, 
453                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
454       DECL_FRIEND_P (decl) = 1;
455     }
456   else
457     {
458       /* @@ Should be able to ingest later definitions of this function
459          before use.  */
460       tree decl = lookup_name_nonclass (declarator);
461       if (decl == NULL_TREE)
462         {
463           cp_warning ("implicitly declaring `%T' as struct", declarator);
464           decl = xref_tag (record_type_node, declarator, 1);
465           decl = TYPE_MAIN_DECL (decl);
466         }
467
468       /* Allow abbreviated declarations of overloaded functions,
469          but not if those functions are really class names.  */
470       if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
471         {
472           cp_warning ("`friend %T' archaic, use `friend class %T' instead",
473                       declarator, declarator);
474           decl = TREE_TYPE (TREE_PURPOSE (decl));
475         }
476
477       if (TREE_CODE (decl) == TREE_LIST)
478         add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
479       else
480         make_friend_class (current_class_type, TREE_TYPE (decl));
481       decl = void_type_node;
482     }
483   return decl;
484 }