OSDN Git Service

Support for friend templates.
[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 <stdio.h>
23 #include "tree.h"
24 #include "rtl.h"
25 #include "cp-tree.h"
26 #include "flags.h"
27 #include "output.h"
28
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32
33 static void add_friend PROTO((tree, tree));
34 static void add_friends PROTO((tree, tree, tree));
35
36 /* Friend data structures are described in cp-tree.h.  */
37
38 int
39 is_friend (type, supplicant)
40      tree type, supplicant;
41 {
42   int declp;
43   register tree list;
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 (ctype == TREE_PURPOSE (friends))
70                     return 1;
71
72                   if (TREE_VALUE (friends) == NULL_TREE)
73                     continue;
74
75                   if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL)
76                     {
77                       tree t;
78
79                       /* Perhaps this function is a specialization of
80                          a friend template.  */
81                       for (t = supplicant;
82                            t != NULL_TREE;
83                            t = DECL_TEMPLATE_INFO (t) ? 
84                              DECL_TI_TEMPLATE (t) : NULL_TREE)
85                         /* FIXME: The use of comptypes here, and below, is
86                            bogus, since two specializations of a
87                            template parameter with non-type parameters
88                            may have the same type, but be different.  */
89                         if (comptypes (TREE_TYPE (t),
90                                        TREE_TYPE (TREE_VALUE (friends)), 1))
91                           return 1;
92
93                       continue;
94                     }
95
96                   if (comptypes (TREE_TYPE (supplicant),
97                                  TREE_TYPE (TREE_VALUE (friends)), 1))
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         if (supplicant == TREE_VALUE (list))
113           return 1;
114     }      
115
116   {
117     tree context;
118
119     if (! declp)
120       {
121         /* Are we a nested or local class?  If so, we aren't friends
122            with the CONTEXT.  */
123         if (IS_AGGR_TYPE (supplicant))
124           context = NULL_TREE;
125         else
126           context = DECL_CONTEXT (TYPE_MAIN_DECL (supplicant));
127       }
128     else if (DECL_FUNCTION_MEMBER_P (supplicant))
129       context = DECL_CLASS_CONTEXT (supplicant);
130     else
131       context = NULL_TREE;
132
133     if (context)
134       return is_friend (type, context);
135   }
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
254   if (IS_SIGNATURE (type))
255     {
256       error ("`friend' declaration in signature definition");
257       return;
258     }
259   if (IS_SIGNATURE (friend_type))
260     {
261       error ("signature type `%s' declared `friend'",
262              IDENTIFIER_POINTER (TYPE_IDENTIFIER (friend_type)));
263       return;
264     }
265   if (type == friend_type)
266     {
267       pedwarn ("class `%s' is implicitly friends with itself",
268                TYPE_NAME_STRING (type));
269       return;
270     }
271
272   GNU_xref_hier (TYPE_NAME_STRING (type),
273                  TYPE_NAME_STRING (friend_type), 0, 0, 1);
274
275   classes = CLASSTYPE_FRIEND_CLASSES (type);
276   while (classes && TREE_VALUE (classes) != friend_type)
277     classes = TREE_CHAIN (classes);
278   if (classes)
279     warning ("class `%s' is already friends with class `%s'",
280              TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
281   else
282     {
283       CLASSTYPE_FRIEND_CLASSES (type)
284         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
285     }
286 }
287
288 /* Main friend processor.  This is large, and for modularity purposes,
289    has been removed from grokdeclarator.  It returns `void_type_node'
290    to indicate that something happened, though a FIELD_DECL is
291    not returned.
292
293    CTYPE is the class this friend belongs to.
294
295    DECLARATOR is the name of the friend.
296
297    DECL is the FUNCTION_DECL that the friend is.
298
299    In case we are parsing a friend which is part of an inline
300    definition, we will need to store PARM_DECL chain that comes
301    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
302
303    FLAGS is just used for `grokclassfn'.
304
305    QUALS say what special qualifies should apply to the object
306    pointed to by `this'.  */
307
308 tree
309 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
310      tree ctype, declarator, decl, parmdecls;
311      enum overload_flags flags;
312      tree quals;
313      int funcdef_flag;
314 {
315   int is_friend_template = 0;
316
317   /* Every decl that gets here is a friend of something.  */
318   DECL_FRIEND_P (decl) = 1;
319
320   if (TREE_CODE (decl) == FUNCTION_DECL)
321     is_friend_template = processing_template_decl >
322       template_class_depth (current_class_type);
323
324   if (ctype)
325     {
326       tree cname = TYPE_NAME (ctype);
327       if (TREE_CODE (cname) == TYPE_DECL)
328         cname = DECL_NAME (cname);
329
330       /* A method friend.  */
331       if (TREE_CODE (decl) == FUNCTION_DECL)
332         {
333           if (flags == NO_SPECIAL && ctype && declarator == cname)
334             DECL_CONSTRUCTOR_P (decl) = 1;
335
336           /* This will set up DECL_ARGUMENTS for us.  */
337           grokclassfn (ctype, cname, decl, flags, quals);
338
339           if (is_friend_template)
340             decl = DECL_TI_TEMPLATE (push_template_decl (decl));
341
342           if (TYPE_SIZE (ctype) != 0 
343               && template_class_depth (ctype) == 0)
344             decl = check_classfn (ctype, decl);
345
346           if (TREE_TYPE (decl) != error_mark_node)
347             {
348               if (TYPE_SIZE (ctype) || template_class_depth (ctype) > 0)
349                 add_friend (current_class_type, decl);
350               else
351                 cp_error ("member `%D' declared as friend before type `%T' defined",
352                           decl, ctype);
353             }
354         }
355       else
356         {
357           /* Possibly a bunch of method friends.  */
358
359           /* Get the class they belong to.  */
360           tree ctype = IDENTIFIER_TYPE_VALUE (cname);
361           tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
362
363           if (fields)
364             add_friends (current_class_type, declarator, ctype);
365           else
366             error ("method `%s' is not a member of class `%s'",
367                    IDENTIFIER_POINTER (declarator),
368                    IDENTIFIER_POINTER (cname));
369           decl = void_type_node;
370         }
371     }
372   else if (TREE_CODE (decl) == FUNCTION_DECL
373            && ((IDENTIFIER_LENGTH (declarator) == 4
374                 && IDENTIFIER_POINTER (declarator)[0] == 'm'
375                 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
376                || (IDENTIFIER_LENGTH (declarator) > 10
377                    && IDENTIFIER_POINTER (declarator)[0] == '_'
378                    && IDENTIFIER_POINTER (declarator)[1] == '_'
379                    && strncmp (IDENTIFIER_POINTER (declarator)+2,
380                                "builtin_", 8) == 0)))
381     {
382       /* raw "main", and builtin functions never gets overloaded,
383          but they can become friends.  */
384       add_friend (current_class_type, decl);
385       DECL_FRIEND_P (decl) = 1;
386       decl = void_type_node;
387     }
388   /* A global friend.
389      @@ or possibly a friend from a base class ?!?  */
390   else if (TREE_CODE (decl) == FUNCTION_DECL)
391     {
392       /* Friends must all go through the overload machinery,
393          even though they may not technically be overloaded.
394
395          Note that because classes all wind up being top-level
396          in their scope, their friend wind up in top-level scope as well.  */
397       DECL_ASSEMBLER_NAME (decl)
398         = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
399                                TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
400       DECL_ARGUMENTS (decl) = parmdecls;
401       if (funcdef_flag)
402         DECL_CLASS_CONTEXT (decl) = current_class_type;
403
404       if (! DECL_USE_TEMPLATE (decl))
405         {
406           /* We can call pushdecl here, because the TREE_CHAIN of this
407              FUNCTION_DECL is not needed for other purposes.  Don't do this
408              for a template instantiation.  */
409           if (!is_friend_template)
410             {  
411               /* However, we don't call pushdecl() for a friend
412                  function of a template class, since in general,
413                  such a declaration depends on template
414                  parameters.  Instead, we call pushdecl when the
415                  class is instantiated.  */
416               if (template_class_depth (current_class_type) == 0)
417                 decl = pushdecl (decl);
418             }
419           else 
420             decl = push_template_decl (decl); 
421
422           if (! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
423               && current_template_parms && uses_template_parms (decl))
424             {
425               static int explained;
426               cp_warning ("friend declaration `%#D'", decl);
427               warning ("  declares a non-template function");
428               if (! explained)
429                 {
430                   warning ("  unless you compile with -fguiding-decls");
431                   warning ("  or add <> after the function name");
432                   explained = 1;
433                 }
434             }
435         }
436
437       make_decl_rtl (decl, NULL_PTR, 1);
438       add_friend (current_class_type, 
439                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
440       DECL_FRIEND_P (decl) = 1;
441     }
442   else
443     {
444       /* @@ Should be able to ingest later definitions of this function
445          before use.  */
446       tree decl = lookup_name_nonclass (declarator);
447       if (decl == NULL_TREE)
448         {
449           warning ("implicitly declaring `%s' as struct",
450                    IDENTIFIER_POINTER (declarator));
451           decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
452           decl = TYPE_MAIN_DECL (decl);
453         }
454
455       /* Allow abbreviated declarations of overloaded functions,
456          but not if those functions are really class names.  */
457       if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
458         {
459           warning ("`friend %s' archaic, use `friend class %s' instead",
460                    IDENTIFIER_POINTER (declarator),
461                    IDENTIFIER_POINTER (declarator));
462           decl = TREE_TYPE (TREE_PURPOSE (decl));
463         }
464
465       if (TREE_CODE (decl) == TREE_LIST)
466         add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
467       else
468         make_friend_class (current_class_type, TREE_TYPE (decl));
469       decl = void_type_node;
470     }
471   return decl;
472 }