OSDN Git Service

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