OSDN Git Service

* cp-tree.h (struct saved_scope): Add incomplete field.
[pf3gnuchains/gcc-fork.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2    Copyright (C) 1997, 1998, 1999, 2000 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       /* The type and its nested classes are implicitly friends, as
105          per core issue 45 (this is a change from the standard).  */
106       for (context = supplicant;
107            context && TYPE_P (context);
108            context = TYPE_CONTEXT (context))
109         if (type == context)
110           return 1;
111       
112       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
113       for (; list ; list = TREE_CHAIN (list))
114         {
115           tree t = TREE_VALUE (list);
116
117           if (TREE_CODE (t) == TEMPLATE_DECL ? 
118               is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
119               same_type_p (supplicant, t))
120             return 1;
121         }
122     }      
123
124   if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
125     context = DECL_CLASS_CONTEXT (supplicant);
126   else if (! declp)
127     /* Local classes have the same access as the enclosing function.  */
128     context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
129   else
130     context = NULL_TREE;
131
132   /* A namespace is not friend to anybody. */
133   if (context && TREE_CODE (context) == NAMESPACE_DECL)
134     context = NULL_TREE;
135
136   if (context)
137     return is_friend (type, context);
138
139   return 0;
140 }
141
142 /* Add a new friend to the friends of the aggregate type TYPE.
143    DECL is the FUNCTION_DECL of the friend being added.  */
144
145 void
146 add_friend (type, decl)
147      tree type, decl;
148 {
149   tree typedecl;
150   tree list;
151   tree name;
152
153   if (decl == error_mark_node)
154     return;
155
156   typedecl = TYPE_MAIN_DECL (type);
157   list = DECL_FRIENDLIST (typedecl);
158   name = DECL_NAME (decl);
159   type = TREE_TYPE (typedecl);
160
161   while (list)
162     {
163       if (name == FRIEND_NAME (list))
164         {
165           tree friends = FRIEND_DECLS (list);
166           for (; friends ; friends = TREE_CHAIN (friends))
167             {
168               if (decl == TREE_VALUE (friends))
169                 {
170                   cp_warning ("`%D' is already a friend of class `%T'",
171                               decl, type);
172                   cp_warning_at ("previous friend declaration of `%D'",
173                                  TREE_VALUE (friends));
174                   return;
175                 }
176             }
177           TREE_VALUE (list) = tree_cons (error_mark_node, decl,
178                                          TREE_VALUE (list));
179           return;
180         }
181       list = TREE_CHAIN (list);
182     }
183
184   DECL_FRIENDLIST (typedecl)
185     = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
186                  DECL_FRIENDLIST (typedecl));
187   if (!uses_template_parms (type))
188     DECL_BEFRIENDING_CLASSES (decl) 
189       = tree_cons (NULL_TREE, type,
190                    DECL_BEFRIENDING_CLASSES (decl));
191 }
192
193 /* Declare that every member function NAME in FRIEND_TYPE
194    (which may be NULL_TREE) is a friend of type TYPE.  */
195
196 void
197 add_friends (type, name, friend_type)
198      tree type, name, friend_type;
199 {
200   tree typedecl = TYPE_MAIN_DECL (type);
201   tree list = DECL_FRIENDLIST (typedecl);
202
203   while (list)
204     {
205       if (name == FRIEND_NAME (list))
206         {
207           tree friends = FRIEND_DECLS (list);
208           while (friends && TREE_PURPOSE (friends) != friend_type)
209             friends = TREE_CHAIN (friends);
210           if (friends)
211             {
212               if (friend_type)
213                 warning ("method `%s::%s' is already a friend of class",
214                          TYPE_NAME_STRING (friend_type),
215                          IDENTIFIER_POINTER (name));
216               else
217                 warning ("function `%s' is already a friend of class `%s'",
218                          IDENTIFIER_POINTER (name),
219                          IDENTIFIER_POINTER (DECL_NAME (typedecl)));
220             }
221           else
222             TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
223                                            TREE_VALUE (list));
224           return;
225         }
226       list = TREE_CHAIN (list);
227     }
228   DECL_FRIENDLIST (typedecl)
229     = tree_cons (name,
230                  build_tree_list (friend_type, NULL_TREE),
231                  DECL_FRIENDLIST (typedecl));
232 }
233
234 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
235    been defined, we make all of its member functions friends of
236    TYPE.  If not, we make it a pending friend, which can later be added
237    when its definition is seen.  If a type is defined, then its TYPE_DECL's
238    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
239    classes that are not defined.  If a type has not yet been defined,
240    then the DECL_WAITING_FRIENDS contains a list of types
241    waiting to make it their friend.  Note that these two can both
242    be in use at the same time!  */
243
244 void
245 make_friend_class (type, friend_type)
246      tree type, friend_type;
247 {
248   tree classes;
249   int is_template_friend;
250
251   if (! IS_AGGR_TYPE (friend_type))
252     {
253       cp_error ("invalid type `%T' declared `friend'", friend_type);
254       return;
255     }
256
257   if (CLASS_TYPE_P (friend_type)
258       && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
259       && uses_template_parms (friend_type))
260     {
261       /* [temp.friend]
262          
263          Friend declarations shall not declare partial
264          specializations.  */
265       cp_error ("partial specialization `%T' declared `friend'",
266                 friend_type);
267       return;
268     }
269   
270   if (processing_template_decl > template_class_depth (type))
271     /* If the TYPE is a template then it makes sense for it to be
272        friends with itself; this means that each instantiation is
273        friends with all other instantiations.  */
274     is_template_friend = 1;
275   else if (same_type_p (type, friend_type))
276     {
277       pedwarn ("class `%s' is implicitly friends with itself",
278                TYPE_NAME_STRING (type));
279       return;
280     }
281   else
282     is_template_friend = 0;
283
284   if (is_template_friend 
285       && (TREE_CODE (friend_type) == TYPENAME_TYPE
286           || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM))
287     {
288       /* [temp.friend]
289
290            A friend of a class or class template can be a function or
291            class template, a specialization of a function template or
292            class template, or an ordinary (nontemplate) function or
293            class. 
294            
295          But, we're looking at something like:
296
297            template <class T> friend typename S<T>::X;
298
299          or:
300
301            template <class T> friend class T;
302
303          which isn't any of these.  */
304       if (TREE_CODE (friend_type) == TYPENAME_TYPE)
305         cp_error ("typename type `%T' declared `friend'",
306                   friend_type);
307       else
308         cp_error ("template parameter type `%T' declared `friend'",
309                   friend_type);
310       return;
311     }
312
313   GNU_xref_hier (type, friend_type, 0, 0, 1);
314
315   if (is_template_friend)
316     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
317
318   classes = CLASSTYPE_FRIEND_CLASSES (type);
319   while (classes 
320          /* Stop if we find the same type on the list.  */
321          && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
322               friend_type == TREE_VALUE (classes) :
323               same_type_p (TREE_VALUE (classes), friend_type)))
324     classes = TREE_CHAIN (classes);
325   if (classes) 
326     cp_warning ("`%T' is already a friend of `%T'",
327                 TREE_VALUE (classes), type);
328   else
329     {
330       CLASSTYPE_FRIEND_CLASSES (type)
331         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
332       if (is_template_friend)
333         friend_type = TREE_TYPE (friend_type);
334       if (!uses_template_parms (type))
335         CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
336           = tree_cons (NULL_TREE, type, 
337                        CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); 
338     }
339 }
340
341 /* Main friend processor.  This is large, and for modularity purposes,
342    has been removed from grokdeclarator.  It returns `void_type_node'
343    to indicate that something happened, though a FIELD_DECL is
344    not returned.
345
346    CTYPE is the class this friend belongs to.
347
348    DECLARATOR is the name of the friend.
349
350    DECL is the FUNCTION_DECL that the friend is.
351
352    In case we are parsing a friend which is part of an inline
353    definition, we will need to store PARM_DECL chain that comes
354    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
355
356    FLAGS is just used for `grokclassfn'.
357
358    QUALS say what special qualifies should apply to the object
359    pointed to by `this'.  */
360
361 tree
362 do_friend (ctype, declarator, decl, parmdecls, attrlist,
363            flags, quals, funcdef_flag)
364      tree ctype, declarator, decl, parmdecls, attrlist;
365      enum overload_flags flags;
366      tree quals;
367      int funcdef_flag;
368 {
369   int is_friend_template = 0;
370   tree prefix_attributes, attributes;
371
372   /* Every decl that gets here is a friend of something.  */
373   DECL_FRIEND_P (decl) = 1;
374
375   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
376     {
377       declarator = TREE_OPERAND (declarator, 0);
378       if (TREE_CODE (declarator) == LOOKUP_EXPR)
379         declarator = TREE_OPERAND (declarator, 0);
380       if (is_overloaded_fn (declarator))
381         declarator = DECL_NAME (get_first_fn (declarator));
382     }
383
384   if (TREE_CODE (decl) != FUNCTION_DECL)
385     my_friendly_abort (990513);
386
387   is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
388
389   if (ctype)
390     {
391       tree cname = TYPE_NAME (ctype);
392       if (TREE_CODE (cname) == TYPE_DECL)
393         cname = DECL_NAME (cname);
394
395       /* A method friend.  */
396       if (flags == NO_SPECIAL && ctype && declarator == cname)
397         DECL_CONSTRUCTOR_P (decl) = 1;
398
399       /* This will set up DECL_ARGUMENTS for us.  */
400       grokclassfn (ctype, decl, flags, quals);
401
402       if (is_friend_template)
403         decl = DECL_TI_TEMPLATE (push_template_decl (decl));
404       else if (template_class_depth (current_class_type))
405         decl = push_template_decl_real (decl, /*is_friend=*/1);
406
407       /* We can't do lookup in a type that involves template
408          parameters.  Instead, we rely on tsubst_friend_function
409          to check the validity of the declaration later.  */
410       if (processing_template_decl)
411         add_friend (current_class_type, decl);
412       /* A nested class may declare a member of an enclosing class
413          to be a friend, so we do lookup here even if CTYPE is in
414          the process of being defined.  */
415       else if (TYPE_SIZE (ctype) != 0 || TYPE_BEING_DEFINED (ctype))
416         {
417           decl = check_classfn (ctype, decl);
418
419           if (decl)
420             add_friend (current_class_type, decl);
421         }
422       else
423         cp_error ("member `%D' declared as friend before type `%T' defined",
424                   decl, ctype);
425     }
426   /* A global friend.
427      @@ or possibly a friend from a base class ?!?  */
428   else if (TREE_CODE (decl) == FUNCTION_DECL)
429     {
430       /* Friends must all go through the overload machinery,
431          even though they may not technically be overloaded.
432
433          Note that because classes all wind up being top-level
434          in their scope, their friend wind up in top-level scope as well.  */
435       DECL_ARGUMENTS (decl) = parmdecls;
436       if (funcdef_flag)
437         DECL_CLASS_CONTEXT (decl) = current_class_type;
438
439       if (! DECL_USE_TEMPLATE (decl))
440         {
441           /* We can call pushdecl here, because the TREE_CHAIN of this
442              FUNCTION_DECL is not needed for other purposes.  Don't do
443              this for a template instantiation.  However, we don't
444              call pushdecl() for a friend function of a template
445              class, since in general, such a declaration depends on
446              template parameters.  Instead, we call pushdecl when the
447              class is instantiated.  */
448           if (!is_friend_template
449               && template_class_depth (current_class_type) == 0)
450             decl = pushdecl (decl);
451           else 
452             decl = push_template_decl_real (decl, /*is_friend=*/1); 
453
454           if (warn_nontemplate_friend
455               && ! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
456               && current_template_parms && uses_template_parms (decl))
457             {
458               static int explained;
459               cp_warning ("friend declaration `%#D'", decl);
460               warning ("  declares a non-template function");
461               if (! explained)
462                 {
463                   warning ("  (if this is not what you intended, make sure");
464                   warning ("  the function template has already been declared,");
465                   warning ("  and add <> after the function name here)");
466                   warning ("  -Wno-non-template-friend disables this warning.");
467                   explained = 1;
468                 }
469             }
470         }
471
472       make_decl_rtl (decl, NULL_PTR, 1);
473       add_friend (current_class_type, 
474                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
475       DECL_FRIEND_P (decl) = 1;
476     }
477
478   /* Unfortunately, we have to handle attributes here.  Normally we would
479      handle them in start_decl_1, but since this is a friend decl start_decl_1
480      never gets to see it.  */
481
482   if (attrlist)
483     {
484       attributes = TREE_PURPOSE (attrlist);
485       prefix_attributes = TREE_VALUE (attrlist);
486     }
487   else
488     {
489       attributes = NULL_TREE;
490       prefix_attributes = NULL_TREE;
491     } 
492
493 #ifdef SET_DEFAULT_DECL_ATTRIBUTES
494   SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
495 #endif
496   
497   /* Set attributes here so if duplicate decl, will have proper attributes.  */
498   cplus_decl_attributes (decl, attributes, prefix_attributes);
499
500   return decl;
501 }