111
|
1 /* Help friends in C++.
|
131
|
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
|
111
|
3
|
|
4 This file is part of GCC.
|
|
5
|
|
6 GCC 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 3, or (at your option)
|
|
9 any later version.
|
|
10
|
|
11 GCC 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 GCC; see the file COPYING3. If not see
|
|
18 <http://www.gnu.org/licenses/>. */
|
|
19
|
|
20 #include "config.h"
|
|
21 #include "system.h"
|
|
22 #include "coretypes.h"
|
|
23 #include "cp-tree.h"
|
|
24
|
|
25 /* Friend data structures are described in cp-tree.h. */
|
|
26
|
|
27
|
|
28 /* The GLOBAL_FRIEND scope (functions, classes, or templates) is
|
|
29 regarded as a friend of every class. This is only used by libcc1,
|
|
30 to enable GDB's code snippets to access private members without
|
|
31 disabling access control in general, which could cause different
|
|
32 template overload resolution results when accessibility matters
|
|
33 (e.g. tests for an accessible member). */
|
|
34
|
|
35 static GTY(()) tree global_friend;
|
|
36
|
|
37 /* Set the GLOBAL_FRIEND for this compilation session. It might be
|
|
38 set multiple times, but always to the same scope. */
|
|
39
|
|
40 void
|
|
41 set_global_friend (tree scope)
|
|
42 {
|
|
43 gcc_checking_assert (scope != NULL_TREE);
|
|
44 gcc_assert (!global_friend || global_friend == scope);
|
|
45 global_friend = scope;
|
|
46 }
|
|
47
|
|
48 /* Return TRUE if SCOPE is the global friend. */
|
|
49
|
|
50 bool
|
|
51 is_global_friend (tree scope)
|
|
52 {
|
|
53 gcc_checking_assert (scope != NULL_TREE);
|
|
54
|
|
55 if (global_friend == scope)
|
|
56 return true;
|
|
57
|
|
58 if (!global_friend)
|
|
59 return false;
|
|
60
|
|
61 if (is_specialization_of_friend (global_friend, scope))
|
|
62 return true;
|
|
63
|
|
64 return false;
|
|
65 }
|
|
66
|
|
67 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
|
|
68
|
|
69 int
|
|
70 is_friend (tree type, tree supplicant)
|
|
71 {
|
|
72 int declp;
|
|
73 tree list;
|
|
74 tree context;
|
|
75
|
|
76 if (supplicant == NULL_TREE || type == NULL_TREE)
|
|
77 return 0;
|
|
78
|
|
79 if (is_global_friend (supplicant))
|
|
80 return 1;
|
|
81
|
|
82 declp = DECL_P (supplicant);
|
|
83
|
|
84 if (declp)
|
|
85 /* It's a function decl. */
|
|
86 {
|
|
87 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
|
|
88 tree name = DECL_NAME (supplicant);
|
|
89
|
|
90 for (; list ; list = TREE_CHAIN (list))
|
|
91 {
|
|
92 if (name == FRIEND_NAME (list))
|
|
93 {
|
|
94 tree friends = FRIEND_DECLS (list);
|
|
95 for (; friends ; friends = TREE_CHAIN (friends))
|
|
96 {
|
|
97 tree this_friend = TREE_VALUE (friends);
|
|
98
|
|
99 if (this_friend == NULL_TREE)
|
|
100 continue;
|
|
101
|
|
102 if (supplicant == this_friend)
|
|
103 return 1;
|
|
104
|
|
105 if (is_specialization_of_friend (supplicant, this_friend))
|
|
106 return 1;
|
|
107 }
|
|
108 break;
|
|
109 }
|
|
110 }
|
|
111 }
|
|
112 else
|
|
113 /* It's a type. */
|
|
114 {
|
|
115 if (same_type_p (supplicant, type))
|
|
116 return 1;
|
|
117
|
|
118 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
|
|
119 for (; list ; list = TREE_CHAIN (list))
|
|
120 {
|
|
121 tree t = TREE_VALUE (list);
|
|
122
|
|
123 if (TREE_CODE (t) == TEMPLATE_DECL ?
|
|
124 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
|
|
125 same_type_p (supplicant, t))
|
|
126 return 1;
|
|
127 }
|
|
128 }
|
|
129
|
|
130 if (declp)
|
|
131 {
|
|
132 if (DECL_FUNCTION_MEMBER_P (supplicant))
|
|
133 context = DECL_CONTEXT (supplicant);
|
|
134 else
|
|
135 context = NULL_TREE;
|
|
136 }
|
|
137 else
|
|
138 {
|
|
139 if (TYPE_CLASS_SCOPE_P (supplicant))
|
|
140 /* Nested classes get the same access as their enclosing types, as
|
|
141 per DR 45 (this is a change from the standard). */
|
|
142 context = TYPE_CONTEXT (supplicant);
|
|
143 else
|
|
144 /* Local classes have the same access as the enclosing function. */
|
|
145 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
|
|
146 }
|
|
147
|
|
148 /* A namespace is not friend to anybody. */
|
|
149 if (context && TREE_CODE (context) == NAMESPACE_DECL)
|
|
150 context = NULL_TREE;
|
|
151
|
|
152 if (context)
|
|
153 return is_friend (type, context);
|
|
154
|
|
155 return 0;
|
|
156 }
|
|
157
|
|
158 /* Add a new friend to the friends of the aggregate type TYPE.
|
|
159 DECL is the FUNCTION_DECL of the friend being added.
|
|
160
|
|
161 If COMPLAIN is true, warning about duplicate friend is issued.
|
|
162 We want to have this diagnostics during parsing but not
|
|
163 when a template is being instantiated. */
|
|
164
|
|
165 void
|
|
166 add_friend (tree type, tree decl, bool complain)
|
|
167 {
|
|
168 tree typedecl;
|
|
169 tree list;
|
|
170 tree name;
|
|
171 tree ctx;
|
|
172
|
|
173 if (decl == error_mark_node)
|
|
174 return;
|
|
175
|
131
|
176 if (TREE_CODE (decl) == FUNCTION_DECL
|
|
177 && DECL_TEMPLATE_INSTANTIATION (decl))
|
|
178 /* We'll have parsed this as a declaration, and therefore not
|
|
179 marked the lookup set for keeping. Do that now. */
|
|
180 lookup_keep (DECL_TI_TEMPLATE (decl));
|
|
181
|
111
|
182 typedecl = TYPE_MAIN_DECL (type);
|
|
183 list = DECL_FRIENDLIST (typedecl);
|
|
184 name = DECL_NAME (decl);
|
|
185 type = TREE_TYPE (typedecl);
|
|
186
|
|
187 while (list)
|
|
188 {
|
|
189 if (name == FRIEND_NAME (list))
|
|
190 {
|
|
191 tree friends = FRIEND_DECLS (list);
|
|
192 for (; friends ; friends = TREE_CHAIN (friends))
|
|
193 {
|
|
194 if (decl == TREE_VALUE (friends))
|
|
195 {
|
|
196 if (complain)
|
|
197 warning (OPT_Wredundant_decls,
|
|
198 "%qD is already a friend of class %qT",
|
|
199 decl, type);
|
|
200 return;
|
|
201 }
|
|
202 }
|
|
203
|
|
204 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
|
|
205 TREE_VALUE (list));
|
|
206 break;
|
|
207 }
|
|
208 list = TREE_CHAIN (list);
|
|
209 }
|
|
210
|
|
211 ctx = DECL_CONTEXT (decl);
|
|
212 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
|
|
213 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
|
|
214 tf_warning_or_error);
|
|
215
|
|
216 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
|
|
217
|
|
218 if (!list)
|
|
219 DECL_FRIENDLIST (typedecl)
|
|
220 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
|
|
221 DECL_FRIENDLIST (typedecl));
|
|
222 if (!uses_template_parms (type))
|
|
223 DECL_BEFRIENDING_CLASSES (decl)
|
|
224 = tree_cons (NULL_TREE, type,
|
|
225 DECL_BEFRIENDING_CLASSES (decl));
|
|
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 If COMPLAIN is true, warning about duplicate friend is issued.
|
|
239 We want to have this diagnostics during parsing but not
|
|
240 when a template is being instantiated. */
|
|
241
|
|
242 void
|
|
243 make_friend_class (tree type, tree friend_type, bool complain)
|
|
244 {
|
|
245 tree classes;
|
|
246
|
|
247 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
|
|
248 the enclosing class. FRIEND_DEPTH counts the number of template
|
|
249 headers used for this friend declaration. TEMPLATE_MEMBER_P,
|
|
250 defined inside the `if' block for TYPENAME_TYPE case, is true if
|
|
251 a template header in FRIEND_DEPTH is intended for DECLARATOR.
|
|
252 For example, the code
|
|
253
|
|
254 template <class T> struct A {
|
|
255 template <class U> struct B {
|
|
256 template <class V> template <class W>
|
|
257 friend class C<V>::D;
|
|
258 };
|
|
259 };
|
|
260
|
|
261 will eventually give the following results
|
|
262
|
|
263 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
|
|
264 2. FRIEND_DEPTH equals 2 (for `V' and `W').
|
|
265 3. TEMPLATE_MEMBER_P is true (for `W').
|
|
266
|
|
267 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
|
|
268
|
|
269 int class_template_depth = template_class_depth (type);
|
|
270 int friend_depth = processing_template_decl - class_template_depth;
|
|
271
|
|
272 if (! MAYBE_CLASS_TYPE_P (friend_type)
|
|
273 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
|
|
274 {
|
|
275 /* N1791: If the type specifier in a friend declaration designates a
|
|
276 (possibly cv-qualified) class type, that class is declared as a
|
|
277 friend; otherwise, the friend declaration is ignored.
|
|
278
|
|
279 So don't complain in C++11 mode. */
|
|
280 if (cxx_dialect < cxx11)
|
|
281 pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
|
|
282 "invalid type %qT declared %<friend%>", friend_type);
|
|
283 return;
|
|
284 }
|
|
285
|
|
286 friend_type = cv_unqualified (friend_type);
|
|
287
|
|
288 if (check_for_bare_parameter_packs (friend_type))
|
|
289 return;
|
|
290
|
|
291 if (friend_depth)
|
|
292 {
|
131
|
293 /* [temp.friend] Friend declarations shall not declare partial
|
|
294 specializations. */
|
111
|
295 if (CLASS_TYPE_P (friend_type)
|
|
296 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
|
|
297 && uses_template_parms (friend_type))
|
|
298 {
|
|
299 error ("partial specialization %qT declared %<friend%>",
|
|
300 friend_type);
|
|
301 return;
|
|
302 }
|
131
|
303
|
111
|
304 if (TYPE_TEMPLATE_INFO (friend_type)
|
|
305 && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type)))
|
|
306 {
|
131
|
307 auto_diagnostic_group d;
|
111
|
308 error ("%qT is not a template", friend_type);
|
|
309 inform (location_of (friend_type), "previous declaration here");
|
|
310 if (TYPE_CLASS_SCOPE_P (friend_type)
|
|
311 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type))
|
|
312 && currently_open_class (TYPE_CONTEXT (friend_type)))
|
|
313 inform (input_location, "perhaps you need explicit template "
|
|
314 "arguments in your nested-name-specifier");
|
|
315 return;
|
|
316 }
|
|
317 }
|
131
|
318
|
|
319 /* It makes sense for a template class to be friends with itself,
|
|
320 that means the instantiations can be friendly. Other cases are
|
|
321 not so meaningful. */
|
|
322 if (!friend_depth && same_type_p (type, friend_type))
|
111
|
323 {
|
|
324 if (complain)
|
|
325 warning (0, "class %qT is implicitly friends with itself",
|
|
326 type);
|
|
327 return;
|
|
328 }
|
|
329
|
|
330 /* [temp.friend]
|
|
331
|
|
332 A friend of a class or class template can be a function or
|
|
333 class template, a specialization of a function template or
|
|
334 class template, or an ordinary (nontemplate) function or
|
|
335 class. */
|
|
336 if (!friend_depth)
|
|
337 ;/* ok */
|
|
338 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
|
|
339 {
|
|
340 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
|
|
341 == TEMPLATE_ID_EXPR)
|
|
342 {
|
|
343 /* template <class U> friend class T::X<U>; */
|
|
344 /* [temp.friend]
|
|
345 Friend declarations shall not declare partial
|
|
346 specializations. */
|
|
347 error ("partial specialization %qT declared %<friend%>",
|
|
348 friend_type);
|
|
349 return;
|
|
350 }
|
|
351 else
|
|
352 {
|
|
353 /* We will figure this out later. */
|
|
354 bool template_member_p = false;
|
|
355
|
|
356 tree ctype = TYPE_CONTEXT (friend_type);
|
|
357 tree name = TYPE_IDENTIFIER (friend_type);
|
|
358 tree decl;
|
|
359
|
|
360 if (!uses_template_parms_level (ctype, class_template_depth
|
|
361 + friend_depth))
|
|
362 template_member_p = true;
|
|
363
|
|
364 if (class_template_depth)
|
|
365 {
|
|
366 /* We rely on tsubst_friend_class to check the
|
|
367 validity of the declaration later. */
|
|
368 if (template_member_p)
|
|
369 friend_type
|
|
370 = make_unbound_class_template (ctype,
|
|
371 name,
|
|
372 current_template_parms,
|
|
373 tf_error);
|
|
374 else
|
|
375 friend_type
|
|
376 = make_typename_type (ctype, name, class_type, tf_error);
|
|
377 }
|
|
378 else
|
|
379 {
|
|
380 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
|
|
381 if (!decl)
|
|
382 {
|
|
383 error ("%qT is not a member of %qT", name, ctype);
|
|
384 return;
|
|
385 }
|
|
386 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
|
|
387 {
|
131
|
388 auto_diagnostic_group d;
|
111
|
389 error ("%qT is not a member class template of %qT",
|
|
390 name, ctype);
|
|
391 inform (DECL_SOURCE_LOCATION (decl),
|
|
392 "%qD declared here", decl);
|
|
393 return;
|
|
394 }
|
|
395 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
|
|
396 || !CLASS_TYPE_P (TREE_TYPE (decl))))
|
|
397 {
|
131
|
398 auto_diagnostic_group d;
|
111
|
399 error ("%qT is not a nested class of %qT",
|
|
400 name, ctype);
|
|
401 inform (DECL_SOURCE_LOCATION (decl),
|
|
402 "%qD declared here", decl);
|
|
403 return;
|
|
404 }
|
|
405
|
|
406 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
|
|
407 }
|
|
408 }
|
|
409 }
|
|
410 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
|
|
411 {
|
|
412 /* template <class T> friend class T; */
|
|
413 error ("template parameter type %qT declared %<friend%>", friend_type);
|
|
414 return;
|
|
415 }
|
|
416 else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
|
|
417 friend_type = TYPE_NAME (friend_type);
|
|
418 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
|
|
419 {
|
|
420 /* template <class T> friend class A; where A is not a template */
|
|
421 error ("%q#T is not a template", friend_type);
|
|
422 return;
|
|
423 }
|
|
424 else
|
|
425 /* template <class T> friend class A; where A is a template */
|
|
426 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
|
|
427
|
|
428 if (friend_type == error_mark_node)
|
|
429 return;
|
|
430
|
|
431 /* See if it is already a friend. */
|
|
432 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
|
|
433 classes;
|
|
434 classes = TREE_CHAIN (classes))
|
|
435 {
|
|
436 tree probe = TREE_VALUE (classes);
|
|
437
|
|
438 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
|
|
439 {
|
|
440 if (friend_type == probe)
|
|
441 {
|
|
442 if (complain)
|
|
443 warning (OPT_Wredundant_decls,
|
|
444 "%qD is already a friend of %qT", probe, type);
|
|
445 break;
|
|
446 }
|
|
447 }
|
|
448 else if (TREE_CODE (probe) != TEMPLATE_DECL)
|
|
449 {
|
|
450 if (same_type_p (probe, friend_type))
|
|
451 {
|
|
452 if (complain)
|
|
453 warning (OPT_Wredundant_decls,
|
|
454 "%qT is already a friend of %qT", probe, type);
|
|
455 break;
|
|
456 }
|
|
457 }
|
|
458 }
|
|
459
|
|
460 if (!classes)
|
|
461 {
|
|
462 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
|
|
463
|
|
464 CLASSTYPE_FRIEND_CLASSES (type)
|
|
465 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
|
|
466 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
|
|
467 friend_type = TREE_TYPE (friend_type);
|
|
468 if (!uses_template_parms (type))
|
|
469 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
|
|
470 = tree_cons (NULL_TREE, type,
|
|
471 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
|
|
472 }
|
|
473 }
|
|
474
|
|
475 /* Record DECL (a FUNCTION_DECL) as a friend of the
|
|
476 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
|
|
477 class of which it is a member, as named in the friend declaration.
|
|
478 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
|
|
479 friend declaration is a definition of the function. FLAGS is as
|
|
480 for grokclass fn. */
|
|
481
|
|
482 tree
|
|
483 do_friend (tree ctype, tree declarator, tree decl,
|
|
484 tree attrlist, enum overload_flags flags,
|
|
485 bool funcdef_flag)
|
|
486 {
|
|
487 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
|
|
488 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
|
|
489
|
|
490 /* Every decl that gets here is a friend of something. */
|
|
491 DECL_FRIEND_P (decl) = 1;
|
|
492
|
|
493 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
|
|
494 error ("friend declaration %qD may not have virt-specifiers",
|
|
495 decl);
|
|
496
|
|
497 /* Unfortunately, we have to handle attributes here. Normally we would
|
|
498 handle them in start_decl_1, but since this is a friend decl start_decl_1
|
|
499 never gets to see it. */
|
|
500
|
|
501 /* Set attributes here so if duplicate decl, will have proper attributes. */
|
|
502 cplus_decl_attributes (&decl, attrlist, 0);
|
|
503
|
|
504 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
|
|
505 {
|
|
506 declarator = TREE_OPERAND (declarator, 0);
|
131
|
507 if (!identifier_p (declarator))
|
|
508 declarator = OVL_NAME (declarator);
|
111
|
509 }
|
|
510
|
|
511 if (ctype)
|
|
512 {
|
|
513 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
|
|
514 the enclosing class. FRIEND_DEPTH counts the number of template
|
|
515 headers used for this friend declaration. TEMPLATE_MEMBER_P is
|
|
516 true if a template header in FRIEND_DEPTH is intended for
|
|
517 DECLARATOR. For example, the code
|
|
518
|
|
519 template <class T> struct A {
|
|
520 template <class U> struct B {
|
|
521 template <class V> template <class W>
|
|
522 friend void C<V>::f(W);
|
|
523 };
|
|
524 };
|
|
525
|
|
526 will eventually give the following results
|
|
527
|
|
528 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
|
|
529 2. FRIEND_DEPTH equals 2 (for `V' and `W').
|
|
530 3. TEMPLATE_MEMBER_P is true (for `W'). */
|
|
531
|
|
532 int class_template_depth = template_class_depth (current_class_type);
|
|
533 int friend_depth = processing_template_decl - class_template_depth;
|
|
534 /* We will figure this out later. */
|
|
535 bool template_member_p = false;
|
|
536
|
|
537 tree cname = TYPE_NAME (ctype);
|
|
538 if (TREE_CODE (cname) == TYPE_DECL)
|
|
539 cname = DECL_NAME (cname);
|
|
540
|
|
541 /* A method friend. */
|
|
542 if (flags == NO_SPECIAL && declarator == cname)
|
|
543 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
|
|
544
|
|
545 grokclassfn (ctype, decl, flags);
|
|
546
|
|
547 if (friend_depth)
|
|
548 {
|
|
549 if (!uses_template_parms_level (ctype, class_template_depth
|
|
550 + friend_depth))
|
|
551 template_member_p = true;
|
|
552 }
|
|
553
|
|
554 /* A nested class may declare a member of an enclosing class
|
|
555 to be a friend, so we do lookup here even if CTYPE is in
|
|
556 the process of being defined. */
|
|
557 if (class_template_depth
|
|
558 || COMPLETE_OR_OPEN_TYPE_P (ctype))
|
|
559 {
|
|
560 if (DECL_TEMPLATE_INFO (decl))
|
|
561 /* DECL is a template specialization. No need to
|
|
562 build a new TEMPLATE_DECL. */
|
|
563 ;
|
|
564 else if (class_template_depth)
|
|
565 /* We rely on tsubst_friend_function to check the
|
|
566 validity of the declaration later. */
|
|
567 decl = push_template_decl_real (decl, /*is_friend=*/true);
|
|
568 else
|
|
569 decl = check_classfn (ctype, decl,
|
|
570 template_member_p
|
|
571 ? current_template_parms
|
|
572 : NULL_TREE);
|
|
573
|
|
574 if ((template_member_p
|
|
575 /* Always pull out the TEMPLATE_DECL if we have a friend
|
|
576 template in a class template so that it gets tsubsted
|
|
577 properly later on (59956). tsubst_friend_function knows
|
|
578 how to tell this apart from a member template. */
|
|
579 || (class_template_depth && friend_depth))
|
|
580 && decl && TREE_CODE (decl) == FUNCTION_DECL)
|
|
581 decl = DECL_TI_TEMPLATE (decl);
|
|
582
|
|
583 if (decl)
|
|
584 add_friend (current_class_type, decl, /*complain=*/true);
|
|
585 }
|
|
586 else
|
|
587 error ("member %qD declared as friend before type %qT defined",
|
|
588 decl, ctype);
|
|
589 }
|
|
590 /* A global friend.
|
|
591 @@ or possibly a friend from a base class ?!? */
|
|
592 else if (TREE_CODE (decl) == FUNCTION_DECL)
|
|
593 {
|
|
594 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
|
|
595
|
|
596 /* Friends must all go through the overload machinery,
|
|
597 even though they may not technically be overloaded.
|
|
598
|
|
599 Note that because classes all wind up being top-level
|
|
600 in their scope, their friend wind up in top-level scope as well. */
|
|
601 if (funcdef_flag)
|
|
602 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
|
|
603
|
|
604 if (! DECL_USE_TEMPLATE (decl))
|
|
605 {
|
|
606 /* We must check whether the decl refers to template
|
|
607 arguments before push_template_decl_real adds a
|
|
608 reference to the containing template class. */
|
|
609 int warn = (warn_nontemplate_friend
|
|
610 && ! funcdef_flag && ! is_friend_template
|
|
611 && current_template_parms
|
|
612 && uses_template_parms (decl));
|
|
613
|
|
614 if (is_friend_template
|
|
615 || template_class_depth (current_class_type) != 0)
|
|
616 /* We can't call pushdecl for a template class, since in
|
|
617 general, such a declaration depends on template
|
|
618 parameters. Instead, we call pushdecl when the class
|
|
619 is instantiated. */
|
|
620 decl = push_template_decl_real (decl, /*is_friend=*/true);
|
|
621 else if (current_function_decl)
|
|
622 /* pushdecl will check there's a local decl already. */
|
|
623 decl = pushdecl (decl, /*is_friend=*/true);
|
|
624 else
|
|
625 {
|
|
626 /* We can't use pushdecl, as we might be in a template
|
|
627 class specialization, and pushdecl will insert an
|
|
628 unqualified friend decl into the template parameter
|
|
629 scope, rather than the namespace containing it. */
|
|
630 tree ns = decl_namespace_context (decl);
|
|
631
|
|
632 push_nested_namespace (ns);
|
|
633 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
|
|
634 pop_nested_namespace (ns);
|
|
635 }
|
|
636
|
|
637 if (warn)
|
|
638 {
|
|
639 static int explained;
|
|
640 bool warned;
|
|
641
|
131
|
642 auto_diagnostic_group d;
|
111
|
643 warned = warning (OPT_Wnon_template_friend, "friend declaration "
|
|
644 "%q#D declares a non-template function", decl);
|
|
645 if (! explained && warned)
|
|
646 {
|
|
647 inform (input_location, "(if this is not what you intended, make sure "
|
|
648 "the function template has already been declared "
|
|
649 "and add <> after the function name here) ");
|
|
650 explained = 1;
|
|
651 }
|
|
652 }
|
|
653 }
|
|
654
|
|
655 if (decl == error_mark_node)
|
|
656 return error_mark_node;
|
|
657
|
|
658 add_friend (current_class_type,
|
|
659 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
|
|
660 /*complain=*/true);
|
|
661 DECL_FRIEND_P (decl) = 1;
|
|
662 }
|
|
663
|
|
664 return decl;
|
|
665 }
|
|
666
|
|
667 #include "gt-cp-friend.h"
|