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