150397Sobrien/* Help friends in C++. 2169689Skan Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 3132718Skan Free Software Foundation, Inc. 450397Sobrien 5132718SkanThis file is part of GCC. 650397Sobrien 7132718SkanGCC is free software; you can redistribute it and/or modify 850397Sobrienit under the terms of the GNU General Public License as published by 950397Sobrienthe Free Software Foundation; either version 2, or (at your option) 1050397Sobrienany later version. 1150397Sobrien 12132718SkanGCC is distributed in the hope that it will be useful, 1350397Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of 1450397SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1550397SobrienGNU General Public License for more details. 1650397Sobrien 1750397SobrienYou should have received a copy of the GNU General Public License 18132718Skanalong with GCC; see the file COPYING. If not, write to 19169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor, 20169689SkanBoston, MA 02110-1301, USA. */ 2150397Sobrien 2250397Sobrien#include "config.h" 2350397Sobrien#include "system.h" 24132718Skan#include "coretypes.h" 25132718Skan#include "tm.h" 2650397Sobrien#include "tree.h" 2750397Sobrien#include "rtl.h" 2890075Sobrien#include "expr.h" 2950397Sobrien#include "cp-tree.h" 3050397Sobrien#include "flags.h" 3150397Sobrien#include "output.h" 3250397Sobrien#include "toplev.h" 3350397Sobrien 3450397Sobrien/* Friend data structures are described in cp-tree.h. */ 3550397Sobrien 36117395Skan/* Returns nonzero if SUPPLICANT is a friend of TYPE. */ 3752284Sobrien 3850397Sobrienint 39132718Skanis_friend (tree type, tree supplicant) 4050397Sobrien{ 4150397Sobrien int declp; 42132718Skan tree list; 4350397Sobrien tree context; 4450397Sobrien 4550397Sobrien if (supplicant == NULL_TREE || type == NULL_TREE) 4650397Sobrien return 0; 4750397Sobrien 4890075Sobrien declp = DECL_P (supplicant); 4950397Sobrien 5050397Sobrien if (declp) 5150397Sobrien /* It's a function decl. */ 5250397Sobrien { 5350397Sobrien tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); 5450397Sobrien tree name = DECL_NAME (supplicant); 5550397Sobrien 5650397Sobrien for (; list ; list = TREE_CHAIN (list)) 5750397Sobrien { 5852284Sobrien if (name == FRIEND_NAME (list)) 5950397Sobrien { 6052284Sobrien tree friends = FRIEND_DECLS (list); 6150397Sobrien for (; friends ; friends = TREE_CHAIN (friends)) 6250397Sobrien { 63132718Skan tree friend = TREE_VALUE (friends); 64132718Skan 65132718Skan if (friend == NULL_TREE) 6650397Sobrien continue; 6750397Sobrien 68132718Skan if (supplicant == friend) 6952284Sobrien return 1; 7050397Sobrien 71132718Skan if (is_specialization_of_friend (supplicant, friend)) 72117395Skan return 1; 7350397Sobrien } 7450397Sobrien break; 7550397Sobrien } 7650397Sobrien } 7750397Sobrien } 7850397Sobrien else 7950397Sobrien /* It's a type. */ 8050397Sobrien { 81169689Skan if (same_type_p (supplicant, type)) 82169689Skan return 1; 83169689Skan 8450397Sobrien list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type))); 8550397Sobrien for (; list ; list = TREE_CHAIN (list)) 8650397Sobrien { 8750397Sobrien tree t = TREE_VALUE (list); 8850397Sobrien 89169689Skan if (TREE_CODE (t) == TEMPLATE_DECL ? 90169689Skan is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) : 9152284Sobrien same_type_p (supplicant, t)) 9250397Sobrien return 1; 9350397Sobrien } 94169689Skan } 9550397Sobrien 96169689Skan if (declp) 97169689Skan { 98169689Skan if (DECL_FUNCTION_MEMBER_P (supplicant)) 99169689Skan context = DECL_CONTEXT (supplicant); 100169689Skan else 101169689Skan context = NULL_TREE; 102169689Skan } 10350397Sobrien else 104169689Skan { 105169689Skan if (TYPE_CLASS_SCOPE_P (supplicant)) 106169689Skan /* Nested classes get the same access as their enclosing types, as 107169689Skan per DR 45 (this is a change from the standard). */ 108169689Skan context = TYPE_CONTEXT (supplicant); 109169689Skan else 110169689Skan /* Local classes have the same access as the enclosing function. */ 111169689Skan context = decl_function_context (TYPE_MAIN_DECL (supplicant)); 112169689Skan } 11350397Sobrien 114117395Skan /* A namespace is not friend to anybody. */ 11550397Sobrien if (context && TREE_CODE (context) == NAMESPACE_DECL) 11650397Sobrien context = NULL_TREE; 11750397Sobrien 11850397Sobrien if (context) 11950397Sobrien return is_friend (type, context); 12050397Sobrien 12150397Sobrien return 0; 12250397Sobrien} 12350397Sobrien 12450397Sobrien/* Add a new friend to the friends of the aggregate type TYPE. 125132718Skan DECL is the FUNCTION_DECL of the friend being added. 12650397Sobrien 127132718Skan If COMPLAIN is true, warning about duplicate friend is issued. 128132718Skan We want to have this diagnostics during parsing but not 129132718Skan when a template is being instantiated. */ 130132718Skan 13152284Sobrienvoid 132132718Skanadd_friend (tree type, tree decl, bool complain) 13350397Sobrien{ 13452284Sobrien tree typedecl; 13552284Sobrien tree list; 13652284Sobrien tree name; 137169689Skan tree ctx; 13850397Sobrien 13952284Sobrien if (decl == error_mark_node) 14052284Sobrien return; 14152284Sobrien 14252284Sobrien typedecl = TYPE_MAIN_DECL (type); 14352284Sobrien list = DECL_FRIENDLIST (typedecl); 14452284Sobrien name = DECL_NAME (decl); 14552284Sobrien type = TREE_TYPE (typedecl); 14652284Sobrien 14750397Sobrien while (list) 14850397Sobrien { 14952284Sobrien if (name == FRIEND_NAME (list)) 15050397Sobrien { 15152284Sobrien tree friends = FRIEND_DECLS (list); 15250397Sobrien for (; friends ; friends = TREE_CHAIN (friends)) 15350397Sobrien { 15450397Sobrien if (decl == TREE_VALUE (friends)) 15550397Sobrien { 156132718Skan if (complain) 157169689Skan warning (0, "%qD is already a friend of class %qT", 158132718Skan decl, type); 15950397Sobrien return; 16050397Sobrien } 16150397Sobrien } 162117395Skan 163117395Skan maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1); 164117395Skan 165132718Skan TREE_VALUE (list) = tree_cons (NULL_TREE, decl, 16650397Sobrien TREE_VALUE (list)); 16750397Sobrien return; 16850397Sobrien } 16950397Sobrien list = TREE_CHAIN (list); 17050397Sobrien } 17152284Sobrien 172169689Skan ctx = DECL_CONTEXT (decl); 173169689Skan if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx)) 174169689Skan perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl); 175132718Skan 176117395Skan maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1); 177117395Skan 17850397Sobrien DECL_FRIENDLIST (typedecl) 179132718Skan = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl), 18050397Sobrien DECL_FRIENDLIST (typedecl)); 18152284Sobrien if (!uses_template_parms (type)) 182169689Skan DECL_BEFRIENDING_CLASSES (decl) 18352284Sobrien = tree_cons (NULL_TREE, type, 18452284Sobrien DECL_BEFRIENDING_CLASSES (decl)); 18550397Sobrien} 18650397Sobrien 18750397Sobrien/* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already 18850397Sobrien been defined, we make all of its member functions friends of 18950397Sobrien TYPE. If not, we make it a pending friend, which can later be added 19050397Sobrien when its definition is seen. If a type is defined, then its TYPE_DECL's 19150397Sobrien DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend 19250397Sobrien classes that are not defined. If a type has not yet been defined, 19350397Sobrien then the DECL_WAITING_FRIENDS contains a list of types 19450397Sobrien waiting to make it their friend. Note that these two can both 195132718Skan be in use at the same time! 19650397Sobrien 197132718Skan If COMPLAIN is true, warning about duplicate friend is issued. 198132718Skan We want to have this diagnostics during parsing but not 199132718Skan when a template is being instantiated. */ 200132718Skan 20150397Sobrienvoid 202132718Skanmake_friend_class (tree type, tree friend_type, bool complain) 20350397Sobrien{ 20450397Sobrien tree classes; 20550397Sobrien 206169689Skan /* CLASS_TEMPLATE_DEPTH counts the number of template headers for 207169689Skan the enclosing class. FRIEND_DEPTH counts the number of template 208169689Skan headers used for this friend declaration. TEMPLATE_MEMBER_P, 209169689Skan defined inside the `if' block for TYPENAME_TYPE case, is true if 210169689Skan a template header in FRIEND_DEPTH is intended for DECLARATOR. 211169689Skan For example, the code 212169689Skan 213169689Skan template <class T> struct A { 214169689Skan template <class U> struct B { 215169689Skan template <class V> template <class W> 216169689Skan friend class C<V>::D; 217169689Skan }; 218169689Skan }; 219169689Skan 220169689Skan will eventually give the following results 221169689Skan 222169689Skan 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U'). 223169689Skan 2. FRIEND_DEPTH equals 2 (for `V' and `W'). 224169689Skan 3. TEMPLATE_MEMBER_P is true (for `W'). 225169689Skan 226169689Skan The friend is a template friend iff FRIEND_DEPTH is nonzero. */ 227169689Skan 228169689Skan int class_template_depth = template_class_depth (type); 229169689Skan int friend_depth = processing_template_decl - class_template_depth; 230169689Skan 23190075Sobrien if (! IS_AGGR_TYPE (friend_type)) 23250397Sobrien { 233169689Skan error ("invalid type %qT declared %<friend%>", friend_type); 23450397Sobrien return; 23550397Sobrien } 23652284Sobrien 237169689Skan if (friend_depth) 23850397Sobrien /* If the TYPE is a template then it makes sense for it to be 23950397Sobrien friends with itself; this means that each instantiation is 24050397Sobrien friends with all other instantiations. */ 241117395Skan { 242117395Skan if (CLASS_TYPE_P (friend_type) 243117395Skan && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type) 244117395Skan && uses_template_parms (friend_type)) 245117395Skan { 246117395Skan /* [temp.friend] 247117395Skan Friend declarations shall not declare partial 248117395Skan specializations. */ 249169689Skan error ("partial specialization %qT declared %<friend%>", 250117395Skan friend_type); 251117395Skan return; 252117395Skan } 253117395Skan } 25452284Sobrien else if (same_type_p (type, friend_type)) 25550397Sobrien { 256132718Skan if (complain) 257169689Skan pedwarn ("class %qT is implicitly friends with itself", 258132718Skan type); 25950397Sobrien return; 26050397Sobrien } 26150397Sobrien 26290075Sobrien /* [temp.friend] 26390075Sobrien 26490075Sobrien A friend of a class or class template can be a function or 26590075Sobrien class template, a specialization of a function template or 26690075Sobrien class template, or an ordinary (nontemplate) function or 267117395Skan class. */ 268169689Skan if (!friend_depth) 26990075Sobrien ;/* ok */ 27090075Sobrien else if (TREE_CODE (friend_type) == TYPENAME_TYPE) 27190075Sobrien { 272169689Skan if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type)) 273169689Skan == TEMPLATE_ID_EXPR) 274169689Skan { 275169689Skan /* template <class U> friend class T::X<U>; */ 276169689Skan /* [temp.friend] 277169689Skan Friend declarations shall not declare partial 278169689Skan specializations. */ 279169689Skan error ("partial specialization %qT declared %<friend%>", 280169689Skan friend_type); 281169689Skan return; 282169689Skan } 283169689Skan else 284169689Skan { 285169689Skan /* We will figure this out later. */ 286169689Skan bool template_member_p = false; 287169689Skan 288169689Skan tree ctype = TYPE_CONTEXT (friend_type); 289169689Skan tree name = TYPE_IDENTIFIER (friend_type); 290169689Skan tree decl; 291169689Skan 292169689Skan if (!uses_template_parms_level (ctype, class_template_depth 293169689Skan + friend_depth)) 294169689Skan template_member_p = true; 295169689Skan 296169689Skan if (class_template_depth) 297169689Skan { 298169689Skan /* We rely on tsubst_friend_class to check the 299169689Skan validity of the declaration later. */ 300169689Skan if (template_member_p) 301169689Skan friend_type 302169689Skan = make_unbound_class_template (ctype, 303169689Skan name, 304169689Skan current_template_parms, 305169689Skan tf_error); 306169689Skan else 307169689Skan friend_type 308169689Skan = make_typename_type (ctype, name, class_type, tf_error); 309169689Skan } 310169689Skan else 311169689Skan { 312169689Skan decl = lookup_member (ctype, name, 0, true); 313169689Skan if (!decl) 314169689Skan { 315169689Skan error ("%qT is not a member of %qT", name, ctype); 316169689Skan return; 317169689Skan } 318169689Skan if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl)) 319169689Skan { 320169689Skan error ("%qT is not a member class template of %qT", 321169689Skan name, ctype); 322169689Skan error ("%q+D declared here", decl); 323169689Skan return; 324169689Skan } 325169689Skan if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL 326169689Skan || !CLASS_TYPE_P (TREE_TYPE (decl)))) 327169689Skan { 328169689Skan error ("%qT is not a nested class of %qT", 329169689Skan name, ctype); 330169689Skan error ("%q+D declared here", decl); 331169689Skan return; 332169689Skan } 333169689Skan 334169689Skan friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)); 335169689Skan } 336169689Skan } 33790075Sobrien } 33890075Sobrien else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) 33990075Sobrien { 34090075Sobrien /* template <class T> friend class T; */ 341169689Skan error ("template parameter type %qT declared %<friend%>", friend_type); 34290075Sobrien return; 34390075Sobrien } 34490075Sobrien else if (!CLASSTYPE_TEMPLATE_INFO (friend_type)) 34590075Sobrien { 34690075Sobrien /* template <class T> friend class A; where A is not a template */ 347169689Skan error ("%q#T is not a template", friend_type); 34890075Sobrien return; 34990075Sobrien } 350169689Skan else 351169689Skan /* template <class T> friend class A; where A is a template */ 35250397Sobrien friend_type = CLASSTYPE_TI_TEMPLATE (friend_type); 35350397Sobrien 354169689Skan if (friend_type == error_mark_node) 355169689Skan return; 356169689Skan 357132718Skan /* See if it is already a friend. */ 358132718Skan for (classes = CLASSTYPE_FRIEND_CLASSES (type); 359132718Skan classes; 360132718Skan classes = TREE_CHAIN (classes)) 36150397Sobrien { 362132718Skan tree probe = TREE_VALUE (classes); 363132718Skan 364132718Skan if (TREE_CODE (friend_type) == TEMPLATE_DECL) 365132718Skan { 366132718Skan if (friend_type == probe) 367132718Skan { 368132718Skan if (complain) 369169689Skan warning (0, "%qD is already a friend of %qT", probe, type); 370132718Skan break; 371132718Skan } 372132718Skan } 373132718Skan else if (TREE_CODE (probe) != TEMPLATE_DECL) 374132718Skan { 375132718Skan if (same_type_p (probe, friend_type)) 376132718Skan { 377132718Skan if (complain) 378169689Skan warning (0, "%qT is already a friend of %qT", probe, type); 379132718Skan break; 380132718Skan } 381132718Skan } 382132718Skan } 383169689Skan 384169689Skan if (!classes) 385132718Skan { 386117395Skan maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1); 387117395Skan 38850397Sobrien CLASSTYPE_FRIEND_CLASSES (type) 38950397Sobrien = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type)); 390169689Skan if (TREE_CODE (friend_type) == TEMPLATE_DECL) 39152284Sobrien friend_type = TREE_TYPE (friend_type); 39252284Sobrien if (!uses_template_parms (type)) 39352284Sobrien CLASSTYPE_BEFRIENDING_CLASSES (friend_type) 394169689Skan = tree_cons (NULL_TREE, type, 395169689Skan CLASSTYPE_BEFRIENDING_CLASSES (friend_type)); 39650397Sobrien } 39750397Sobrien} 39850397Sobrien 399169689Skan/* Record DECL (a FUNCTION_DECL) as a friend of the 400169689Skan CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the 401169689Skan class of which it is a member, as named in the friend declaration. 402169689Skan DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the 403169689Skan friend declaration is a definition of the function. FLAGS is as 404169689Skan for grokclass fn. */ 40550397Sobrien 40650397Sobrientree 407132718Skando_friend (tree ctype, tree declarator, tree decl, 408169689Skan tree attrlist, enum overload_flags flags, 409169689Skan bool funcdef_flag) 41050397Sobrien{ 411169689Skan gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); 412169689Skan gcc_assert (!ctype || IS_AGGR_TYPE (ctype)); 413169689Skan 41450397Sobrien /* Every decl that gets here is a friend of something. */ 41550397Sobrien DECL_FRIEND_P (decl) = 1; 41650397Sobrien 41750397Sobrien if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 41850397Sobrien { 41950397Sobrien declarator = TREE_OPERAND (declarator, 0); 42050397Sobrien if (is_overloaded_fn (declarator)) 42150397Sobrien declarator = DECL_NAME (get_first_fn (declarator)); 42250397Sobrien } 42350397Sobrien 42450397Sobrien if (ctype) 42550397Sobrien { 426132718Skan /* CLASS_TEMPLATE_DEPTH counts the number of template headers for 427132718Skan the enclosing class. FRIEND_DEPTH counts the number of template 428132718Skan headers used for this friend declaration. TEMPLATE_MEMBER_P is 429132718Skan true if a template header in FRIEND_DEPTH is intended for 430132718Skan DECLARATOR. For example, the code 431132718Skan 432132718Skan template <class T> struct A { 433132718Skan template <class U> struct B { 434132718Skan template <class V> template <class W> 435132718Skan friend void C<V>::f(W); 436132718Skan }; 437132718Skan }; 438132718Skan 439132718Skan will eventually give the following results 440132718Skan 441132718Skan 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U'). 442132718Skan 2. FRIEND_DEPTH equals 2 (for `V' and `W'). 443132718Skan 3. TEMPLATE_MEMBER_P is true (for `W'). */ 444132718Skan 445132718Skan int class_template_depth = template_class_depth (current_class_type); 446132718Skan int friend_depth = processing_template_decl - class_template_depth; 447132718Skan /* We will figure this out later. */ 448132718Skan bool template_member_p = false; 449132718Skan 45050397Sobrien tree cname = TYPE_NAME (ctype); 45150397Sobrien if (TREE_CODE (cname) == TYPE_DECL) 45250397Sobrien cname = DECL_NAME (cname); 45350397Sobrien 45450397Sobrien /* A method friend. */ 455132718Skan if (flags == NO_SPECIAL && declarator == cname) 45652284Sobrien DECL_CONSTRUCTOR_P (decl) = 1; 45750397Sobrien 458169689Skan grokclassfn (ctype, decl, flags); 45950397Sobrien 460132718Skan if (friend_depth) 461132718Skan { 462132718Skan if (!uses_template_parms_level (ctype, class_template_depth 463132718Skan + friend_depth)) 464132718Skan template_member_p = true; 465132718Skan } 46650397Sobrien 46752284Sobrien /* A nested class may declare a member of an enclosing class 46852284Sobrien to be a friend, so we do lookup here even if CTYPE is in 46952284Sobrien the process of being defined. */ 470132718Skan if (class_template_depth 471132718Skan || COMPLETE_TYPE_P (ctype) 472132718Skan || TYPE_BEING_DEFINED (ctype)) 47352284Sobrien { 474132718Skan if (DECL_TEMPLATE_INFO (decl)) 475132718Skan /* DECL is a template specialization. No need to 476132718Skan build a new TEMPLATE_DECL. */ 477132718Skan ; 478132718Skan else if (class_template_depth) 479132718Skan /* We rely on tsubst_friend_function to check the 480132718Skan validity of the declaration later. */ 481169689Skan decl = push_template_decl_real (decl, /*is_friend=*/true); 482132718Skan else 483169689Skan decl = check_classfn (ctype, decl, 484169689Skan template_member_p 485169689Skan ? current_template_parms 486169689Skan : NULL_TREE); 48750397Sobrien 488132718Skan if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL) 489132718Skan decl = DECL_TI_TEMPLATE (decl); 490132718Skan 49152284Sobrien if (decl) 492132718Skan add_friend (current_class_type, decl, /*complain=*/true); 49350397Sobrien } 49450397Sobrien else 495169689Skan error ("member %qD declared as friend before type %qT defined", 49652284Sobrien decl, ctype); 49750397Sobrien } 49850397Sobrien /* A global friend. 49950397Sobrien @@ or possibly a friend from a base class ?!? */ 50050397Sobrien else if (TREE_CODE (decl) == FUNCTION_DECL) 50150397Sobrien { 502132718Skan int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P (); 503132718Skan 50450397Sobrien /* Friends must all go through the overload machinery, 50550397Sobrien even though they may not technically be overloaded. 50650397Sobrien 50750397Sobrien Note that because classes all wind up being top-level 50850397Sobrien in their scope, their friend wind up in top-level scope as well. */ 50950397Sobrien if (funcdef_flag) 51090075Sobrien SET_DECL_FRIEND_CONTEXT (decl, current_class_type); 51150397Sobrien 51250397Sobrien if (! DECL_USE_TEMPLATE (decl)) 51350397Sobrien { 51490075Sobrien /* We must check whether the decl refers to template 51590075Sobrien arguments before push_template_decl_real adds a 51690075Sobrien reference to the containing template class. */ 51790075Sobrien int warn = (warn_nontemplate_friend 51890075Sobrien && ! funcdef_flag && ! is_friend_template 51990075Sobrien && current_template_parms 52090075Sobrien && uses_template_parms (decl)); 52190075Sobrien 52290075Sobrien if (is_friend_template 52390075Sobrien || template_class_depth (current_class_type) != 0) 52490075Sobrien /* We can't call pushdecl for a template class, since in 52590075Sobrien general, such a declaration depends on template 52690075Sobrien parameters. Instead, we call pushdecl when the class 52790075Sobrien is instantiated. */ 528169689Skan decl = push_template_decl_real (decl, /*is_friend=*/true); 52990075Sobrien else if (current_function_decl) 53090075Sobrien /* This must be a local class, so pushdecl will be ok, and 53190075Sobrien insert an unqualified friend into the local scope 53290075Sobrien (rather than the containing namespace scope, which the 533117395Skan next choice will do). */ 534169689Skan decl = pushdecl_maybe_friend (decl, /*is_friend=*/true); 53590075Sobrien else 53690075Sobrien { 53790075Sobrien /* We can't use pushdecl, as we might be in a template 538169689Skan class specialization, and pushdecl will insert an 539169689Skan unqualified friend decl into the template parameter 540169689Skan scope, rather than the namespace containing it. */ 54190075Sobrien tree ns = decl_namespace_context (decl); 542169689Skan 54390075Sobrien push_nested_namespace (ns); 544169689Skan decl = pushdecl_namespace_level (decl, /*is_friend=*/true); 54590075Sobrien pop_nested_namespace (ns); 54690075Sobrien } 54750397Sobrien 54890075Sobrien if (warn) 54950397Sobrien { 55050397Sobrien static int explained; 551169689Skan warning (0, "friend declaration %q#D declares a non-template " 552169689Skan "function", decl); 55350397Sobrien if (! explained) 55450397Sobrien { 555169689Skan warning (0, "(if this is not what you intended, make sure " 556169689Skan "the function template has already been declared " 557169689Skan "and add <> after the function name here) " 558169689Skan "-Wno-non-template-friend disables this warning"); 55950397Sobrien explained = 1; 56050397Sobrien } 56150397Sobrien } 56250397Sobrien } 56350397Sobrien 564132718Skan if (decl == error_mark_node) 565132718Skan return error_mark_node; 566169689Skan 567169689Skan add_friend (current_class_type, 568132718Skan is_friend_template ? DECL_TI_TEMPLATE (decl) : decl, 569132718Skan /*complain=*/true); 57050397Sobrien DECL_FRIEND_P (decl) = 1; 57150397Sobrien } 57252284Sobrien 57352284Sobrien /* Unfortunately, we have to handle attributes here. Normally we would 57452284Sobrien handle them in start_decl_1, but since this is a friend decl start_decl_1 57552284Sobrien never gets to see it. */ 57652284Sobrien 57752284Sobrien /* Set attributes here so if duplicate decl, will have proper attributes. */ 57890075Sobrien cplus_decl_attributes (&decl, attrlist, 0); 57950397Sobrien 58050397Sobrien return decl; 58150397Sobrien} 582