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