OSDN Git Service

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