OSDN Git Service

* cp-tree.h: Delete #defines for cp_error, cp_warning,
[pf3gnuchains/gcc-fork.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001 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 "expr.h"
26 #include "cp-tree.h"
27 #include "flags.h"
28 #include "output.h"
29 #include "toplev.h"
30
31 /* Friend data structures are described in cp-tree.h.  */
32
33 /* Returns non-zero if SUPPLICANT is a friend of TYPE.  */
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 = DECL_P (supplicant);
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
54       for (; list ; list = TREE_CHAIN (list))
55         {
56           if (name == FRIEND_NAME (list))
57             {
58               tree friends = FRIEND_DECLS (list);
59               for (; friends ; friends = TREE_CHAIN (friends))
60                 {
61                   if (TREE_VALUE (friends) == NULL_TREE)
62                     continue;
63
64                   if (supplicant == TREE_VALUE (friends))
65                     return 1;
66
67                   /* Temporarily, we are more lenient to deal with
68                      nested friend functions, for which there can be
69                      more than one FUNCTION_DECL, despite being the
70                      same function.  When that's fixed, this bit can
71                      go.  */
72                   if (DECL_FUNCTION_MEMBER_P (supplicant)
73                       && same_type_p (TREE_TYPE (supplicant),
74                                       TREE_TYPE (TREE_VALUE (friends))))
75                     return 1;
76
77                   if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
78                       && is_specialization_of (supplicant, 
79                                                TREE_VALUE (friends)))
80                     return 1;
81                 }
82               break;
83             }
84         }
85     }
86   else
87     /* It's a type.  */
88     {
89       /* Nested classes are implicitly friends of their enclosing types, as
90          per core issue 45 (this is a change from the standard).  */
91       for (context = supplicant;
92            context && TYPE_P (context);
93            context = TYPE_CONTEXT (context))
94         if (type == context)
95           return 1;
96       
97       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
98       for (; list ; list = TREE_CHAIN (list))
99         {
100           tree t = TREE_VALUE (list);
101
102           if (TREE_CODE (t) == TEMPLATE_DECL ? 
103               is_specialization_of (TYPE_MAIN_DECL (supplicant), t) :
104               same_type_p (supplicant, t))
105             return 1;
106         }
107     }      
108
109   if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
110     context = DECL_CONTEXT (supplicant);
111   else if (! declp)
112     /* Local classes have the same access as the enclosing function.  */
113     context = decl_function_context (TYPE_MAIN_DECL (supplicant));
114   else
115     context = NULL_TREE;
116
117   /* A namespace is not friend to anybody. */
118   if (context && TREE_CODE (context) == NAMESPACE_DECL)
119     context = NULL_TREE;
120
121   if (context)
122     return is_friend (type, context);
123
124   return 0;
125 }
126
127 /* Add a new friend to the friends of the aggregate type TYPE.
128    DECL is the FUNCTION_DECL of the friend being added.  */
129
130 void
131 add_friend (type, decl)
132      tree type, decl;
133 {
134   tree typedecl;
135   tree list;
136   tree name;
137
138   if (decl == error_mark_node)
139     return;
140
141   typedecl = TYPE_MAIN_DECL (type);
142   list = DECL_FRIENDLIST (typedecl);
143   name = DECL_NAME (decl);
144   type = TREE_TYPE (typedecl);
145
146   while (list)
147     {
148       if (name == FRIEND_NAME (list))
149         {
150           tree friends = FRIEND_DECLS (list);
151           for (; friends ; friends = TREE_CHAIN (friends))
152             {
153               if (decl == TREE_VALUE (friends))
154                 {
155                   warning ("`%D' is already a friend of class `%T'",
156                               decl, type);
157                   cp_warning_at ("previous friend declaration of `%D'",
158                                  TREE_VALUE (friends));
159                   return;
160                 }
161             }
162           TREE_VALUE (list) = tree_cons (error_mark_node, decl,
163                                          TREE_VALUE (list));
164           return;
165         }
166       list = TREE_CHAIN (list);
167     }
168
169   DECL_FRIENDLIST (typedecl)
170     = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
171                  DECL_FRIENDLIST (typedecl));
172   if (!uses_template_parms (type))
173     DECL_BEFRIENDING_CLASSES (decl) 
174       = tree_cons (NULL_TREE, type,
175                    DECL_BEFRIENDING_CLASSES (decl));
176 }
177
178 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
179    been defined, we make all of its member functions friends of
180    TYPE.  If not, we make it a pending friend, which can later be added
181    when its definition is seen.  If a type is defined, then its TYPE_DECL's
182    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
183    classes that are not defined.  If a type has not yet been defined,
184    then the DECL_WAITING_FRIENDS contains a list of types
185    waiting to make it their friend.  Note that these two can both
186    be in use at the same time!  */
187
188 void
189 make_friend_class (type, friend_type)
190      tree type, friend_type;
191 {
192   tree classes;
193   int is_template_friend;
194
195   if (! IS_AGGR_TYPE (friend_type))
196     {
197       error ("invalid type `%T' declared `friend'", friend_type);
198       return;
199     }
200
201   if (CLASS_TYPE_P (friend_type)
202       && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
203       && uses_template_parms (friend_type))
204     {
205       /* [temp.friend]
206          
207          Friend declarations shall not declare partial
208          specializations.  */
209       error ("partial specialization `%T' declared `friend'",
210                 friend_type);
211       return;
212     }
213   
214   if (processing_template_decl > template_class_depth (type))
215     /* If the TYPE is a template then it makes sense for it to be
216        friends with itself; this means that each instantiation is
217        friends with all other instantiations.  */
218     is_template_friend = 1;
219   else if (same_type_p (type, friend_type))
220     {
221       pedwarn ("class `%T' is implicitly friends with itself",
222                   type);
223       return;
224     }
225   else
226     is_template_friend = 0;
227
228   /* [temp.friend]
229
230      A friend of a class or class template can be a function or
231      class template, a specialization of a function template or
232      class template, or an ordinary (nontemplate) function or
233      class. */
234   if (!is_template_friend)
235     ;/* ok */
236   else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
237     {
238       /* template <class T> friend typename S<T>::X; */
239       error ("typename type `%#T' declared `friend'", friend_type);
240       return;
241     }
242   else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
243     {
244       /* template <class T> friend class T; */
245       error ("template parameter type `%T' declared `friend'", friend_type);
246       return;
247     }
248   else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
249     {
250       /* template <class T> friend class A; where A is not a template */
251       error ("`%#T' is not a template", friend_type);
252       return;
253     }
254
255   GNU_xref_hier (type, friend_type, 0, 0, 1);
256
257   if (is_template_friend)
258     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
259
260   classes = CLASSTYPE_FRIEND_CLASSES (type);
261   while (classes 
262          /* Stop if we find the same type on the list.  */
263          && !(TREE_CODE (TREE_VALUE (classes)) == TEMPLATE_DECL ?
264               friend_type == TREE_VALUE (classes) :
265               same_type_p (TREE_VALUE (classes), friend_type)))
266     classes = TREE_CHAIN (classes);
267   if (classes) 
268     warning ("`%T' is already a friend of `%T'",
269                 TREE_VALUE (classes), type);
270   else
271     {
272       CLASSTYPE_FRIEND_CLASSES (type)
273         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
274       if (is_template_friend)
275         friend_type = TREE_TYPE (friend_type);
276       if (!uses_template_parms (type))
277         CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
278           = tree_cons (NULL_TREE, type, 
279                        CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); 
280     }
281 }
282
283 /* Main friend processor.  This is large, and for modularity purposes,
284    has been removed from grokdeclarator.  It returns `void_type_node'
285    to indicate that something happened, though a FIELD_DECL is
286    not returned.
287
288    CTYPE is the class this friend belongs to.
289
290    DECLARATOR is the name of the friend.
291
292    DECL is the FUNCTION_DECL that the friend is.
293
294    In case we are parsing a friend which is part of an inline
295    definition, we will need to store PARM_DECL chain that comes
296    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
297
298    FLAGS is just used for `grokclassfn'.
299
300    QUALS say what special qualifies should apply to the object
301    pointed to by `this'.  */
302
303 tree
304 do_friend (ctype, declarator, decl, parmdecls, attrlist,
305            flags, quals, funcdef_flag)
306      tree ctype, declarator, decl, parmdecls, attrlist;
307      enum overload_flags flags;
308      tree quals;
309      int funcdef_flag;
310 {
311   int is_friend_template = 0;
312
313   /* Every decl that gets here is a friend of something.  */
314   DECL_FRIEND_P (decl) = 1;
315
316   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
317     {
318       declarator = TREE_OPERAND (declarator, 0);
319       if (TREE_CODE (declarator) == LOOKUP_EXPR)
320         declarator = TREE_OPERAND (declarator, 0);
321       if (is_overloaded_fn (declarator))
322         declarator = DECL_NAME (get_first_fn (declarator));
323     }
324
325   if (TREE_CODE (decl) != FUNCTION_DECL)
326     my_friendly_abort (990513);
327
328   is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
329
330   if (ctype)
331     {
332       tree cname = TYPE_NAME (ctype);
333       if (TREE_CODE (cname) == TYPE_DECL)
334         cname = DECL_NAME (cname);
335
336       /* A method friend.  */
337       if (flags == NO_SPECIAL && ctype && declarator == cname)
338         DECL_CONSTRUCTOR_P (decl) = 1;
339
340       /* This will set up DECL_ARGUMENTS for us.  */
341       grokclassfn (ctype, decl, flags, quals);
342
343       if (is_friend_template)
344         decl = DECL_TI_TEMPLATE (push_template_decl (decl));
345       else if (template_class_depth (current_class_type))
346         decl = push_template_decl_real (decl, /*is_friend=*/1);
347
348       /* We can't do lookup in a type that involves template
349          parameters.  Instead, we rely on tsubst_friend_function
350          to check the validity of the declaration later.  */
351       if (processing_template_decl)
352         add_friend (current_class_type, decl);
353       /* A nested class may declare a member of an enclosing class
354          to be a friend, so we do lookup here even if CTYPE is in
355          the process of being defined.  */
356       else if (COMPLETE_TYPE_P (ctype) || TYPE_BEING_DEFINED (ctype))
357         {
358           decl = check_classfn (ctype, decl);
359
360           if (decl)
361             add_friend (current_class_type, decl);
362         }
363       else
364         error ("member `%D' declared as friend before type `%T' defined",
365                   decl, ctype);
366     }
367   /* A global friend.
368      @@ or possibly a friend from a base class ?!?  */
369   else if (TREE_CODE (decl) == FUNCTION_DECL)
370     {
371       /* Friends must all go through the overload machinery,
372          even though they may not technically be overloaded.
373
374          Note that because classes all wind up being top-level
375          in their scope, their friend wind up in top-level scope as well.  */
376       DECL_ARGUMENTS (decl) = parmdecls;
377       if (funcdef_flag)
378         SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
379
380       if (! DECL_USE_TEMPLATE (decl))
381         {
382           /* We must check whether the decl refers to template
383              arguments before push_template_decl_real adds a
384              reference to the containing template class.  */
385           int warn = (warn_nontemplate_friend
386                       && ! funcdef_flag && ! is_friend_template
387                       && current_template_parms
388                       && uses_template_parms (decl));
389
390           if (is_friend_template
391               || template_class_depth (current_class_type) != 0)
392             /* We can't call pushdecl for a template class, since in
393                general, such a declaration depends on template
394                parameters.  Instead, we call pushdecl when the class
395                is instantiated.  */
396             decl = push_template_decl_real (decl, /*is_friend=*/1); 
397           else if (current_function_decl)
398             /* This must be a local class, so pushdecl will be ok, and
399                insert an unqualified friend into the local scope
400                (rather than the containing namespace scope, which the
401                next choice will do). */
402             decl = pushdecl (decl);
403           else
404             {
405               /* We can't use pushdecl, as we might be in a template
406                  class specialization, and pushdecl will insert an
407                  unqualified friend decl into the template parameter
408                  scope, rather than the namespace containing it. */
409               tree ns = decl_namespace_context (decl);
410               
411               push_nested_namespace (ns);
412               decl = pushdecl_namespace_level (decl);
413               pop_nested_namespace (ns);
414             }
415
416           if (warn)
417             {
418               static int explained;
419               warning ("friend declaration `%#D' declares a non-template function", decl);
420               if (! explained)
421                 {
422                   warning ("(if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning");
423                   explained = 1;
424                 }
425             }
426         }
427
428       add_friend (current_class_type, 
429                   is_friend_template ? DECL_TI_TEMPLATE (decl) : decl);
430       DECL_FRIEND_P (decl) = 1;
431     }
432
433   /* Unfortunately, we have to handle attributes here.  Normally we would
434      handle them in start_decl_1, but since this is a friend decl start_decl_1
435      never gets to see it.  */
436
437   /* Set attributes here so if duplicate decl, will have proper attributes.  */
438   cplus_decl_attributes (&decl, attrlist, 0);
439
440   return decl;
441 }