friend.c 18 KB

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