OSDN Git Service

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