]>
Commit | Line | Data |
---|---|---|
bd6dd845 | 1 | /* Help friends in C++. |
e77f031d | 2 | Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
9e1e64ec | 3 | 2007, 2008 Free Software Foundation, Inc. |
bd6dd845 | 4 | |
f5adbb8d | 5 | This file is part of GCC. |
bd6dd845 | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify |
bd6dd845 | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
bd6dd845 MS |
10 | any later version. |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, |
bd6dd845 MS |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
bd6dd845 MS |
20 | |
21 | #include "config.h" | |
8d052bc7 | 22 | #include "system.h" |
4977bab6 ZW |
23 | #include "coretypes.h" |
24 | #include "tm.h" | |
bd6dd845 MS |
25 | #include "tree.h" |
26 | #include "rtl.h" | |
8f17b5c5 | 27 | #include "expr.h" |
bd6dd845 MS |
28 | #include "cp-tree.h" |
29 | #include "flags.h" | |
30 | #include "output.h" | |
54f92bfb | 31 | #include "toplev.h" |
bd6dd845 | 32 | |
f84b4be9 | 33 | /* Friend data structures are described in cp-tree.h. */ |
bd6dd845 | 34 | |
838dfd8a | 35 | /* Returns nonzero if SUPPLICANT is a friend of TYPE. */ |
7d4bdeed | 36 | |
bd6dd845 | 37 | int |
52b7f433 | 38 | is_friend (tree type, tree supplicant) |
bd6dd845 MS |
39 | { |
40 | int declp; | |
926ce8bd | 41 | tree list; |
d50c6443 | 42 | tree context; |
bd6dd845 MS |
43 | |
44 | if (supplicant == NULL_TREE || type == NULL_TREE) | |
45 | return 0; | |
46 | ||
2f939d94 | 47 | declp = DECL_P (supplicant); |
bd6dd845 MS |
48 | |
49 | if (declp) | |
50 | /* It's a function decl. */ | |
51 | { | |
52 | tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); | |
53 | tree name = DECL_NAME (supplicant); | |
bd6dd845 MS |
54 | |
55 | for (; list ; list = TREE_CHAIN (list)) | |
56 | { | |
6a629cac | 57 | if (name == FRIEND_NAME (list)) |
bd6dd845 | 58 | { |
6a629cac | 59 | tree friends = FRIEND_DECLS (list); |
bd6dd845 MS |
60 | for (; friends ; friends = TREE_CHAIN (friends)) |
61 | { | |
be93747e | 62 | tree this_friend = TREE_VALUE (friends); |
f84b4be9 | 63 | |
be93747e | 64 | if (this_friend == NULL_TREE) |
d43f603d | 65 | continue; |
36a117a5 | 66 | |
be93747e | 67 | if (supplicant == this_friend) |
36a117a5 MM |
68 | return 1; |
69 | ||
be93747e | 70 | if (is_specialization_of_friend (supplicant, this_friend)) |
bd6dd845 MS |
71 | return 1; |
72 | } | |
73 | break; | |
74 | } | |
75 | } | |
76 | } | |
77 | else | |
78 | /* It's a type. */ | |
79 | { | |
03b1c206 JM |
80 | if (same_type_p (supplicant, type)) |
81 | return 1; | |
c8094d83 | 82 | |
bd6dd845 MS |
83 | list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type))); |
84 | for (; list ; list = TREE_CHAIN (list)) | |
6757edfe MM |
85 | { |
86 | tree t = TREE_VALUE (list); | |
87 | ||
c8094d83 | 88 | if (TREE_CODE (t) == TEMPLATE_DECL ? |
b939a023 | 89 | is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) : |
3bfdc719 | 90 | same_type_p (supplicant, t)) |
6757edfe MM |
91 | return 1; |
92 | } | |
c8094d83 | 93 | } |
bd6dd845 | 94 | |
03b1c206 JM |
95 | if (declp) |
96 | { | |
97 | if (DECL_FUNCTION_MEMBER_P (supplicant)) | |
98 | context = DECL_CONTEXT (supplicant); | |
99 | else | |
100 | context = NULL_TREE; | |
101 | } | |
d50c6443 | 102 | else |
03b1c206 JM |
103 | { |
104 | if (TYPE_CLASS_SCOPE_P (supplicant)) | |
105 | /* Nested classes get the same access as their enclosing types, as | |
106 | per DR 45 (this is a change from the standard). */ | |
107 | context = TYPE_CONTEXT (supplicant); | |
108 | else | |
109 | /* Local classes have the same access as the enclosing function. */ | |
110 | context = decl_function_context (TYPE_MAIN_DECL (supplicant)); | |
111 | } | |
d50c6443 | 112 | |
c6002625 | 113 | /* A namespace is not friend to anybody. */ |
2c73f9f5 ML |
114 | if (context && TREE_CODE (context) == NAMESPACE_DECL) |
115 | context = NULL_TREE; | |
116 | ||
d50c6443 JM |
117 | if (context) |
118 | return is_friend (type, context); | |
bd6dd845 MS |
119 | |
120 | return 0; | |
121 | } | |
122 | ||
123 | /* Add a new friend to the friends of the aggregate type TYPE. | |
19db77ce KL |
124 | DECL is the FUNCTION_DECL of the friend being added. |
125 | ||
126 | If COMPLAIN is true, warning about duplicate friend is issued. | |
127 | We want to have this diagnostics during parsing but not | |
128 | when a template is being instantiated. */ | |
bd6dd845 | 129 | |
0c0aac2f | 130 | void |
19db77ce | 131 | add_friend (tree type, tree decl, bool complain) |
bd6dd845 | 132 | { |
82bed870 MM |
133 | tree typedecl; |
134 | tree list; | |
135 | tree name; | |
0f2a66c9 | 136 | tree ctx; |
bd6dd845 | 137 | |
82bed870 MM |
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); | |
6a629cac MM |
144 | type = TREE_TYPE (typedecl); |
145 | ||
bd6dd845 MS |
146 | while (list) |
147 | { | |
6a629cac | 148 | if (name == FRIEND_NAME (list)) |
bd6dd845 | 149 | { |
6a629cac | 150 | tree friends = FRIEND_DECLS (list); |
bd6dd845 MS |
151 | for (; friends ; friends = TREE_CHAIN (friends)) |
152 | { | |
153 | if (decl == TREE_VALUE (friends)) | |
154 | { | |
19db77ce | 155 | if (complain) |
d4ee4d25 | 156 | warning (0, "%qD is already a friend of class %qT", |
437a91e6 | 157 | decl, type); |
bd6dd845 MS |
158 | return; |
159 | } | |
160 | } | |
7088fca9 KL |
161 | |
162 | maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1); | |
163 | ||
436f8a4c | 164 | TREE_VALUE (list) = tree_cons (NULL_TREE, decl, |
bd6dd845 MS |
165 | TREE_VALUE (list)); |
166 | return; | |
167 | } | |
168 | list = TREE_CHAIN (list); | |
169 | } | |
6a629cac | 170 | |
0f2a66c9 NS |
171 | ctx = DECL_CONTEXT (decl); |
172 | if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx)) | |
02022f3a | 173 | perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl); |
41c5ee06 | 174 | |
7088fca9 KL |
175 | maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1); |
176 | ||
bd6dd845 | 177 | DECL_FRIENDLIST (typedecl) |
436f8a4c | 178 | = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl), |
bd6dd845 | 179 | DECL_FRIENDLIST (typedecl)); |
0c0aac2f | 180 | if (!uses_template_parms (type)) |
c8094d83 | 181 | DECL_BEFRIENDING_CLASSES (decl) |
0c0aac2f MM |
182 | = tree_cons (NULL_TREE, type, |
183 | DECL_BEFRIENDING_CLASSES (decl)); | |
bd6dd845 MS |
184 | } |
185 | ||
bd6dd845 MS |
186 | /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already |
187 | been defined, we make all of its member functions friends of | |
188 | TYPE. If not, we make it a pending friend, which can later be added | |
189 | when its definition is seen. If a type is defined, then its TYPE_DECL's | |
190 | DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend | |
191 | classes that are not defined. If a type has not yet been defined, | |
192 | then the DECL_WAITING_FRIENDS contains a list of types | |
193 | waiting to make it their friend. Note that these two can both | |
19db77ce KL |
194 | be in use at the same time! |
195 | ||
196 | If COMPLAIN is true, warning about duplicate friend is issued. | |
197 | We want to have this diagnostics during parsing but not | |
198 | when a template is being instantiated. */ | |
bd6dd845 MS |
199 | |
200 | void | |
19db77ce | 201 | make_friend_class (tree type, tree friend_type, bool complain) |
bd6dd845 MS |
202 | { |
203 | tree classes; | |
b939a023 KL |
204 | |
205 | /* CLASS_TEMPLATE_DEPTH counts the number of template headers for | |
206 | the enclosing class. FRIEND_DEPTH counts the number of template | |
207 | headers used for this friend declaration. TEMPLATE_MEMBER_P, | |
208 | defined inside the `if' block for TYPENAME_TYPE case, is true if | |
209 | a template header in FRIEND_DEPTH is intended for DECLARATOR. | |
210 | For example, the code | |
211 | ||
212 | template <class T> struct A { | |
213 | template <class U> struct B { | |
214 | template <class V> template <class W> | |
215 | friend class C<V>::D; | |
216 | }; | |
217 | }; | |
218 | ||
219 | will eventually give the following results | |
220 | ||
221 | 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U'). | |
222 | 2. FRIEND_DEPTH equals 2 (for `V' and `W'). | |
223 | 3. TEMPLATE_MEMBER_P is true (for `W'). | |
224 | ||
225 | The friend is a template friend iff FRIEND_DEPTH is nonzero. */ | |
226 | ||
227 | int class_template_depth = template_class_depth (type); | |
228 | int friend_depth = processing_template_decl - class_template_depth; | |
bd6dd845 | 229 | |
9e1e64ec | 230 | if (! MAYBE_CLASS_TYPE_P (friend_type)) |
bd6dd845 | 231 | { |
15a7ee29 | 232 | error ("invalid type %qT declared %<friend%>", friend_type); |
bd6dd845 MS |
233 | return; |
234 | } | |
36a117a5 | 235 | |
b939a023 | 236 | if (friend_depth) |
ea4e080b MM |
237 | /* If the TYPE is a template then it makes sense for it to be |
238 | friends with itself; this means that each instantiation is | |
239 | friends with all other instantiations. */ | |
da9701a6 KL |
240 | { |
241 | if (CLASS_TYPE_P (friend_type) | |
242 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type) | |
243 | && uses_template_parms (friend_type)) | |
244 | { | |
245 | /* [temp.friend] | |
246 | Friend declarations shall not declare partial | |
247 | specializations. */ | |
15a7ee29 | 248 | error ("partial specialization %qT declared %<friend%>", |
da9701a6 KL |
249 | friend_type); |
250 | return; | |
251 | } | |
da9701a6 | 252 | } |
3bfdc719 | 253 | else if (same_type_p (type, friend_type)) |
bd6dd845 | 254 | { |
19db77ce | 255 | if (complain) |
37ec60ed | 256 | warning (0, "class %qT is implicitly friends with itself", |
19db77ce | 257 | type); |
bd6dd845 MS |
258 | return; |
259 | } | |
260 | ||
a8641661 NS |
261 | /* [temp.friend] |
262 | ||
263 | A friend of a class or class template can be a function or | |
264 | class template, a specialization of a function template or | |
265 | class template, or an ordinary (nontemplate) function or | |
c6002625 | 266 | class. */ |
b939a023 | 267 | if (!friend_depth) |
a8641661 NS |
268 | ;/* ok */ |
269 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
8bcc97de | 270 | { |
b939a023 KL |
271 | if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type)) |
272 | == TEMPLATE_ID_EXPR) | |
273 | { | |
274 | /* template <class U> friend class T::X<U>; */ | |
275 | /* [temp.friend] | |
276 | Friend declarations shall not declare partial | |
277 | specializations. */ | |
278 | error ("partial specialization %qT declared %<friend%>", | |
279 | friend_type); | |
280 | return; | |
281 | } | |
282 | else | |
283 | { | |
284 | /* We will figure this out later. */ | |
285 | bool template_member_p = false; | |
286 | ||
287 | tree ctype = TYPE_CONTEXT (friend_type); | |
288 | tree name = TYPE_IDENTIFIER (friend_type); | |
289 | tree decl; | |
290 | ||
291 | if (!uses_template_parms_level (ctype, class_template_depth | |
292 | + friend_depth)) | |
293 | template_member_p = true; | |
294 | ||
295 | if (class_template_depth) | |
296 | { | |
297 | /* We rely on tsubst_friend_class to check the | |
298 | validity of the declaration later. */ | |
299 | if (template_member_p) | |
300 | friend_type | |
301 | = make_unbound_class_template (ctype, | |
302 | name, | |
303 | current_template_parms, | |
304 | tf_error); | |
305 | else | |
306 | friend_type | |
fc6a28d7 | 307 | = make_typename_type (ctype, name, class_type, tf_error); |
b939a023 KL |
308 | } |
309 | else | |
310 | { | |
311 | decl = lookup_member (ctype, name, 0, true); | |
312 | if (!decl) | |
313 | { | |
314 | error ("%qT is not a member of %qT", name, ctype); | |
315 | return; | |
316 | } | |
317 | if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl)) | |
318 | { | |
319 | error ("%qT is not a member class template of %qT", | |
320 | name, ctype); | |
dee15844 | 321 | error ("%q+D declared here", decl); |
b939a023 KL |
322 | return; |
323 | } | |
324 | if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL | |
325 | || !CLASS_TYPE_P (TREE_TYPE (decl)))) | |
326 | { | |
327 | error ("%qT is not a nested class of %qT", | |
328 | name, ctype); | |
dee15844 | 329 | error ("%q+D declared here", decl); |
b939a023 KL |
330 | return; |
331 | } | |
332 | ||
333 | friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
334 | } | |
335 | } | |
a8641661 NS |
336 | } |
337 | else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) | |
338 | { | |
339 | /* template <class T> friend class T; */ | |
15a7ee29 | 340 | error ("template parameter type %qT declared %<friend%>", friend_type); |
a8641661 NS |
341 | return; |
342 | } | |
343 | else if (!CLASSTYPE_TEMPLATE_INFO (friend_type)) | |
344 | { | |
345 | /* template <class T> friend class A; where A is not a template */ | |
15a7ee29 | 346 | error ("%q#T is not a template", friend_type); |
8bcc97de MM |
347 | return; |
348 | } | |
b939a023 KL |
349 | else |
350 | /* template <class T> friend class A; where A is a template */ | |
ea4e080b MM |
351 | friend_type = CLASSTYPE_TI_TEMPLATE (friend_type); |
352 | ||
b939a023 KL |
353 | if (friend_type == error_mark_node) |
354 | return; | |
355 | ||
6df5158a NS |
356 | /* See if it is already a friend. */ |
357 | for (classes = CLASSTYPE_FRIEND_CLASSES (type); | |
358 | classes; | |
359 | classes = TREE_CHAIN (classes)) | |
360 | { | |
361 | tree probe = TREE_VALUE (classes); | |
362 | ||
363 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
364 | { | |
365 | if (friend_type == probe) | |
366 | { | |
19db77ce | 367 | if (complain) |
d4ee4d25 | 368 | warning (0, "%qD is already a friend of %qT", probe, type); |
6df5158a NS |
369 | break; |
370 | } | |
371 | } | |
372 | else if (TREE_CODE (probe) != TEMPLATE_DECL) | |
373 | { | |
374 | if (same_type_p (probe, friend_type)) | |
375 | { | |
19db77ce | 376 | if (complain) |
d4ee4d25 | 377 | warning (0, "%qT is already a friend of %qT", probe, type); |
6df5158a NS |
378 | break; |
379 | } | |
380 | } | |
381 | } | |
c8094d83 MS |
382 | |
383 | if (!classes) | |
bd6dd845 | 384 | { |
7088fca9 KL |
385 | maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1); |
386 | ||
bd6dd845 MS |
387 | CLASSTYPE_FRIEND_CLASSES (type) |
388 | = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type)); | |
b939a023 | 389 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
6a629cac | 390 | friend_type = TREE_TYPE (friend_type); |
0c0aac2f MM |
391 | if (!uses_template_parms (type)) |
392 | CLASSTYPE_BEFRIENDING_CLASSES (friend_type) | |
c8094d83 MS |
393 | = tree_cons (NULL_TREE, type, |
394 | CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); | |
bd6dd845 MS |
395 | } |
396 | } | |
397 | ||
1cd7d7bf MM |
398 | /* Record DECL (a FUNCTION_DECL) as a friend of the |
399 | CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the | |
400 | class of which it is a member, as named in the friend declaration. | |
401 | DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the | |
402 | friend declaration is a definition of the function. FLAGS is as | |
403 | for grokclass fn. */ | |
bd6dd845 MS |
404 | |
405 | tree | |
4546865e | 406 | do_friend (tree ctype, tree declarator, tree decl, |
c8094d83 | 407 | tree attrlist, enum overload_flags flags, |
357d956e | 408 | bool funcdef_flag) |
bd6dd845 | 409 | { |
1cd7d7bf | 410 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); |
9e1e64ec | 411 | gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype)); |
1cd7d7bf | 412 | |
bd6dd845 MS |
413 | /* Every decl that gets here is a friend of something. */ |
414 | DECL_FRIEND_P (decl) = 1; | |
415 | ||
bdd6df52 DS |
416 | /* Unfortunately, we have to handle attributes here. Normally we would |
417 | handle them in start_decl_1, but since this is a friend decl start_decl_1 | |
418 | never gets to see it. */ | |
419 | ||
420 | /* Set attributes here so if duplicate decl, will have proper attributes. */ | |
421 | cplus_decl_attributes (&decl, attrlist, 0); | |
422 | ||
2b9dc906 JM |
423 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
424 | { | |
425 | declarator = TREE_OPERAND (declarator, 0); | |
2b9dc906 JM |
426 | if (is_overloaded_fn (declarator)) |
427 | declarator = DECL_NAME (get_first_fn (declarator)); | |
428 | } | |
429 | ||
bd6dd845 MS |
430 | if (ctype) |
431 | { | |
d43f603d KL |
432 | /* CLASS_TEMPLATE_DEPTH counts the number of template headers for |
433 | the enclosing class. FRIEND_DEPTH counts the number of template | |
434 | headers used for this friend declaration. TEMPLATE_MEMBER_P is | |
435 | true if a template header in FRIEND_DEPTH is intended for | |
436 | DECLARATOR. For example, the code | |
437 | ||
438 | template <class T> struct A { | |
439 | template <class U> struct B { | |
440 | template <class V> template <class W> | |
441 | friend void C<V>::f(W); | |
442 | }; | |
443 | }; | |
444 | ||
445 | will eventually give the following results | |
446 | ||
447 | 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U'). | |
448 | 2. FRIEND_DEPTH equals 2 (for `V' and `W'). | |
449 | 3. TEMPLATE_MEMBER_P is true (for `W'). */ | |
450 | ||
451 | int class_template_depth = template_class_depth (current_class_type); | |
452 | int friend_depth = processing_template_decl - class_template_depth; | |
453 | /* We will figure this out later. */ | |
454 | bool template_member_p = false; | |
455 | ||
bd6dd845 MS |
456 | tree cname = TYPE_NAME (ctype); |
457 | if (TREE_CODE (cname) == TYPE_DECL) | |
458 | cname = DECL_NAME (cname); | |
459 | ||
460 | /* A method friend. */ | |
d43f603d | 461 | if (flags == NO_SPECIAL && declarator == cname) |
7a8f9fa9 JM |
462 | DECL_CONSTRUCTOR_P (decl) = 1; |
463 | ||
e2537f2c | 464 | grokclassfn (ctype, decl, flags); |
7a8f9fa9 | 465 | |
d43f603d KL |
466 | if (friend_depth) |
467 | { | |
468 | if (!uses_template_parms_level (ctype, class_template_depth | |
469 | + friend_depth)) | |
470 | template_member_p = true; | |
471 | } | |
472 | ||
7a8f9fa9 JM |
473 | /* A nested class may declare a member of an enclosing class |
474 | to be a friend, so we do lookup here even if CTYPE is in | |
475 | the process of being defined. */ | |
d43f603d KL |
476 | if (class_template_depth |
477 | || COMPLETE_TYPE_P (ctype) | |
ac90ae18 | 478 | || (CLASS_TYPE_P (ctype) && TYPE_BEING_DEFINED (ctype))) |
bd6dd845 | 479 | { |
d43f603d KL |
480 | if (DECL_TEMPLATE_INFO (decl)) |
481 | /* DECL is a template specialization. No need to | |
482 | build a new TEMPLATE_DECL. */ | |
483 | ; | |
484 | else if (class_template_depth) | |
485 | /* We rely on tsubst_friend_function to check the | |
486 | validity of the declaration later. */ | |
d63d5d0c | 487 | decl = push_template_decl_real (decl, /*is_friend=*/true); |
d43f603d | 488 | else |
c8094d83 MS |
489 | decl = check_classfn (ctype, decl, |
490 | template_member_p | |
44021471 GB |
491 | ? current_template_parms |
492 | : NULL_TREE); | |
d43f603d KL |
493 | |
494 | if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL) | |
495 | decl = DECL_TI_TEMPLATE (decl); | |
4c571114 | 496 | |
7a8f9fa9 | 497 | if (decl) |
19db77ce | 498 | add_friend (current_class_type, decl, /*complain=*/true); |
bd6dd845 MS |
499 | } |
500 | else | |
15a7ee29 | 501 | error ("member %qD declared as friend before type %qT defined", |
7a8f9fa9 | 502 | decl, ctype); |
bd6dd845 | 503 | } |
bd6dd845 MS |
504 | /* A global friend. |
505 | @@ or possibly a friend from a base class ?!? */ | |
506 | else if (TREE_CODE (decl) == FUNCTION_DECL) | |
507 | { | |
d43f603d KL |
508 | int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P (); |
509 | ||
bd6dd845 MS |
510 | /* Friends must all go through the overload machinery, |
511 | even though they may not technically be overloaded. | |
512 | ||
513 | Note that because classes all wind up being top-level | |
514 | in their scope, their friend wind up in top-level scope as well. */ | |
bd6dd845 | 515 | if (funcdef_flag) |
4f1c5b7d | 516 | SET_DECL_FRIEND_CONTEXT (decl, current_class_type); |
bd6dd845 | 517 | |
386b8a85 | 518 | if (! DECL_USE_TEMPLATE (decl)) |
37130ac3 | 519 | { |
8826b15b AO |
520 | /* We must check whether the decl refers to template |
521 | arguments before push_template_decl_real adds a | |
522 | reference to the containing template class. */ | |
523 | int warn = (warn_nontemplate_friend | |
524 | && ! funcdef_flag && ! is_friend_template | |
525 | && current_template_parms | |
526 | && uses_template_parms (decl)); | |
527 | ||
9ac1bd2e NS |
528 | if (is_friend_template |
529 | || template_class_depth (current_class_type) != 0) | |
530 | /* We can't call pushdecl for a template class, since in | |
531 | general, such a declaration depends on template | |
532 | parameters. Instead, we call pushdecl when the class | |
533 | is instantiated. */ | |
d63d5d0c | 534 | decl = push_template_decl_real (decl, /*is_friend=*/true); |
9ac1bd2e | 535 | else if (current_function_decl) |
7655e009 JM |
536 | { |
537 | /* This must be a local class. 11.5p11: | |
538 | ||
539 | If a friend declaration appears in a local class (9.8) and | |
540 | the name specified is an unqualified name, a prior | |
541 | declaration is looked up without considering scopes that | |
542 | are outside the innermost enclosing non-class scope. For a | |
543 | friend function declaration, if there is no prior | |
544 | declaration, the program is ill-formed. */ | |
545 | tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl)); | |
546 | if (t) | |
547 | decl = pushdecl_maybe_friend (decl, /*is_friend=*/true); | |
548 | else | |
549 | { | |
550 | error ("friend declaration %qD in local class without " | |
551 | "prior declaration", decl); | |
552 | return error_mark_node; | |
553 | } | |
554 | } | |
9ac1bd2e NS |
555 | else |
556 | { | |
557 | /* We can't use pushdecl, as we might be in a template | |
0cbd7506 MS |
558 | class specialization, and pushdecl will insert an |
559 | unqualified friend decl into the template parameter | |
560 | scope, rather than the namespace containing it. */ | |
9ac1bd2e | 561 | tree ns = decl_namespace_context (decl); |
c8094d83 | 562 | |
9ac1bd2e | 563 | push_nested_namespace (ns); |
d63d5d0c | 564 | decl = pushdecl_namespace_level (decl, /*is_friend=*/true); |
9ac1bd2e NS |
565 | pop_nested_namespace (ns); |
566 | } | |
37130ac3 | 567 | |
8826b15b | 568 | if (warn) |
37130ac3 JM |
569 | { |
570 | static int explained; | |
71205d17 MLI |
571 | bool warned; |
572 | ||
573 | warned = warning (OPT_Wnon_template_friend, "friend declaration " | |
574 | "%q#D declares a non-template function", decl); | |
575 | if (! explained && warned) | |
37130ac3 | 576 | { |
1f5b3869 | 577 | inform (input_location, "(if this is not what you intended, make sure " |
20af23d3 DM |
578 | "the function template has already been declared " |
579 | "and add <> after the function name here) "); | |
37130ac3 JM |
580 | explained = 1; |
581 | } | |
582 | } | |
583 | } | |
bd6dd845 | 584 | |
b1a19c7c MM |
585 | if (decl == error_mark_node) |
586 | return error_mark_node; | |
c8094d83 MS |
587 | |
588 | add_friend (current_class_type, | |
19db77ce KL |
589 | is_friend_template ? DECL_TI_TEMPLATE (decl) : decl, |
590 | /*complain=*/true); | |
bd6dd845 MS |
591 | DECL_FRIEND_P (decl) = 1; |
592 | } | |
7a8f9fa9 | 593 | |
bd6dd845 MS |
594 | return decl; |
595 | } |