xref.c (classname): New function.
[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 (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, friend_type, 0, 0, 1);
271
272   if (is_template_friend)
273     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
274
275   classes = CLASSTYPE_FRIEND_CLASSES (type);
276   while (classes 
277          /* Stop if we find the same type on the list.  */
278          && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
279               friend_type == TREE_VALUE (classes) :
280               comptypes (TREE_VALUE (classes), friend_type, 1)))
281     classes = TREE_CHAIN (classes);
282   if (classes) 
283     cp_warning ("`%T' is already a friend of `%T'",
284                 TREE_VALUE (classes), type);
285   else
286     {
287       CLASSTYPE_FRIEND_CLASSES (type)
288         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
289     }
290 }
291
292 /* Main friend processor.  This is large, and for modularity purposes,
293    has been removed from grokdeclarator.  It returns `void_type_node'
294    to indicate that something happened, though a FIELD_DECL is
295    not returned.
296
297    CTYPE is the class this friend belongs to.
298
299    DECLARATOR is the name of the friend.
300
301    DECL is the FUNCTION_DECL that the friend is.
302
303    In case we are parsing a friend which is part of an inline
304    definition, we will need to store PARM_DECL chain that comes
305    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
306
307    FLAGS is just used for `grokclassfn'.
308
309    QUALS say what special qualifies should apply to the object
310    pointed to by `this'.  */
311
312 tree
313 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
314      tree ctype, declarator, decl, parmdecls;
315      enum overload_flags flags;
316      tree quals;
317      int funcdef_flag;
318 {
319   int is_friend_template = 0;
320
321   /* Every decl that gets here is a friend of something.  */
322   DECL_FRIEND_P (decl) = 1;
323
324   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
325     {
326       declarator = TREE_OPERAND (declarator, 0);
327       if (TREE_CODE (declarator) == LOOKUP_EXPR)
328         declarator = TREE_OPERAND (declarator, 0);
329       if (is_overloaded_fn (declarator))
330         declarator = DECL_NAME (get_first_fn (declarator));
331     }
332
333   if (TREE_CODE (decl) == FUNCTION_DECL)
334     is_friend_template = processing_template_decl >
335       template_class_depth (current_class_type);
336
337   if (ctype)
338     {
339       tree cname = TYPE_NAME (ctype);
340       if (TREE_CODE (cname) == TYPE_DECL)
341         cname = DECL_NAME (cname);
342
343       /* A method friend.  */
344       if (TREE_CODE (decl) == FUNCTION_DECL)
345         {
346           if (flags == NO_SPECIAL && ctype && declarator == cname)
347             DECL_CONSTRUCTOR_P (decl) = 1;
348
349           /* This will set up DECL_ARGUMENTS for us.  */
350           grokclassfn (ctype, cname, decl, flags, quals);
351
352           if (is_friend_template)
353             decl = DECL_TI_TEMPLATE (push_template_decl (decl));
354
355           if (TYPE_SIZE (ctype) != 0 && template_class_depth (ctype) == 0)
356             decl = check_classfn (ctype, decl);
357
358           /* TYPE_BEING_DEFINED is a hack for nested classes having
359              member functions of the enclosing class as friends. Will
360              go away as parsing of classes gets rewritten. */
361           if (TREE_TYPE (decl) != error_mark_node)
362             {
363               if (TYPE_BEING_DEFINED (ctype) ||
364                   TYPE_SIZE (ctype) || template_class_depth (ctype) > 0)
365                 add_friend (current_class_type, decl);
366               else
367                 cp_error ("member `%D' declared as friend before type `%T' defined",
368                           decl, ctype);
369             }
370         }
371       else
372         {
373           /* Possibly a bunch of method friends.  */
374
375           /* Get the class they belong to.  */
376           tree ctype = IDENTIFIER_TYPE_VALUE (cname);
377           tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
378
379           if (fields)
380             add_friends (current_class_type, declarator, ctype);
381           else
382             cp_error ("method `%D' is not a member of class `%T'",
383                       declarator, ctype);
384           decl = void_type_node;
385         }
386     }
387   else if (TREE_CODE (decl) == FUNCTION_DECL
388            && (MAIN_NAME_P (declarator)
389                || (IDENTIFIER_LENGTH (declarator) > 10
390                    && IDENTIFIER_POINTER (declarator)[0] == '_'
391                    && IDENTIFIER_POINTER (declarator)[1] == '_'
392                    && strncmp (IDENTIFIER_POINTER (declarator)+2,
393                                "builtin_", 8) == 0)))
394     {
395       /* raw "main", and builtin functions never gets overloaded,
396          but they can become friends.  */
397       add_friend (current_class_type, decl);
398       DECL_FRIEND_P (decl) = 1;
399       decl = void_type_node;
400     }
401   /* A global friend.
402      @@ or possibly a friend from a base class ?!?  */
403   else if (TREE_CODE (decl) == FUNCTION_DECL)
404     {
405       /* Friends must all go through the overload machinery,
406          even though they may not technically be overloaded.
407
408          Note that because classes all wind up being top-level
409          in their scope, their friend wind up in top-level scope as well.  */
410       DECL_ASSEMBLER_NAME (decl)
411         = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
412                                TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
413       DECL_ARGUMENTS (decl) = parmdecls;
414       if (funcdef_flag)
415         DECL_CLASS_CONTEXT (decl) = current_class_type;
416
417       if (! DECL_USE_TEMPLATE (decl))
418         {
419           /* We can call pushdecl here, because the TREE_CHAIN of this
420              FUNCTION_DECL is not needed for other purposes.  Don't do this
421              for a template instantiation.  */
422           if (!is_friend_template)
423             {  
424               /* However, we don't call pushdecl() for a friend
425                  function of a template class, since in general,
426                  such a declaration depends on template
427                  parameters.  Instead, we call pushdecl when the
428                  class is instantiated.  */
429               if (template_class_depth (current_class_type) == 0)
430                 decl = pushdecl (decl);
431             }
432           else 
433             decl = push_template_decl (decl); 
434
435           if (! funcdef_flag && ! flag_guiding_decls && ! is_friend_template
436               && current_template_parms && uses_template_parms (decl))
437             {
438               static int explained;
439               cp_warning ("friend declaration `%#D'", decl);
440               warning ("  declares a non-template function");
441               if (! explained)
442                 {
443                   warning ("  (if this is not what you intended, make sure");
444                   warning ("  the function template has already been declared,");
445                   warning ("  and add <> after the function name here)");
446                   explained = 1;
447                 }
448             }
449         }
450
451       make_decl_rtl (decl, NULL_PTR, 1);
452       add_friend (current_class_type, 
453                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
454       DECL_FRIEND_P (decl) = 1;
455     }
456   else
457     {
458       /* @@ Should be able to ingest later definitions of this function
459          before use.  */
460       tree decl = lookup_name_nonclass (declarator);
461       if (decl == NULL_TREE)
462         {
463           cp_warning ("implicitly declaring `%T' as struct", declarator);
464           decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
465           decl = TYPE_MAIN_DECL (decl);
466         }
467
468       /* Allow abbreviated declarations of overloaded functions,
469          but not if those functions are really class names.  */
470       if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
471         {
472           cp_warning ("`friend %T' archaic, use `friend class %T' instead",
473                       declarator, declarator);
474           decl = TREE_TYPE (TREE_PURPOSE (decl));
475         }
476
477       if (TREE_CODE (decl) == TREE_LIST)
478         add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
479       else
480         make_friend_class (current_class_type, TREE_TYPE (decl));
481       decl = void_type_node;
482     }
483   return decl;
484 }