OSDN Git Service

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