OSDN Git Service

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