method.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  1. /* Handle the hair of processing (but not expanding) inline functions.
  2. Also manage function and variable name overloading.
  3. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  4. Contributed by Michael Tiemann (tiemann@cygnus.com)
  5. This file is part of GCC.
  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. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Handle method declarations. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "stringpool.h"
  33. #include "varasm.h"
  34. #include "cp-tree.h"
  35. #include "flags.h"
  36. #include "toplev.h"
  37. #include "tm_p.h"
  38. #include "target.h"
  39. #include "common/common-target.h"
  40. #include "diagnostic.h"
  41. #include "hash-map.h"
  42. #include "is-a.h"
  43. #include "plugin-api.h"
  44. #include "hard-reg-set.h"
  45. #include "input.h"
  46. #include "function.h"
  47. #include "ipa-ref.h"
  48. #include "cgraph.h"
  49. /* Various flags to control the mangling process. */
  50. enum mangling_flags
  51. {
  52. /* No flags. */
  53. mf_none = 0,
  54. /* The thing we are presently mangling is part of a template type,
  55. rather than a fully instantiated type. Therefore, we may see
  56. complex expressions where we would normally expect to see a
  57. simple integer constant. */
  58. mf_maybe_uninstantiated = 1,
  59. /* When mangling a numeric value, use the form `_XX_' (instead of
  60. just `XX') if the value has more than one digit. */
  61. mf_use_underscores_around_value = 2
  62. };
  63. typedef enum mangling_flags mangling_flags;
  64. static void do_build_copy_assign (tree);
  65. static void do_build_copy_constructor (tree);
  66. static tree make_alias_for_thunk (tree);
  67. /* Called once to initialize method.c. */
  68. void
  69. init_method (void)
  70. {
  71. init_mangle ();
  72. }
  73. /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
  74. indicates whether it is a this or result adjusting thunk.
  75. FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
  76. (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
  77. never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
  78. adjusting thunks, we scale it to a byte offset. For covariant
  79. thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
  80. the returned thunk with finish_thunk. */
  81. tree
  82. make_thunk (tree function, bool this_adjusting,
  83. tree fixed_offset, tree virtual_offset)
  84. {
  85. HOST_WIDE_INT d;
  86. tree thunk;
  87. gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
  88. /* We can have this thunks to covariant thunks, but not vice versa. */
  89. gcc_assert (!DECL_THIS_THUNK_P (function));
  90. gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
  91. /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
  92. if (this_adjusting && virtual_offset)
  93. virtual_offset
  94. = size_binop (MULT_EXPR,
  95. virtual_offset,
  96. convert (ssizetype,
  97. TYPE_SIZE_UNIT (vtable_entry_type)));
  98. d = tree_to_shwi (fixed_offset);
  99. /* See if we already have the thunk in question. For this_adjusting
  100. thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
  101. will be a BINFO. */
  102. for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
  103. if (DECL_THIS_THUNK_P (thunk) == this_adjusting
  104. && THUNK_FIXED_OFFSET (thunk) == d
  105. && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
  106. && (!virtual_offset
  107. || (this_adjusting
  108. ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
  109. virtual_offset)
  110. : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
  111. return thunk;
  112. /* All thunks must be created before FUNCTION is actually emitted;
  113. the ABI requires that all thunks be emitted together with the
  114. function to which they transfer control. */
  115. gcc_assert (!TREE_ASM_WRITTEN (function));
  116. /* Likewise, we can only be adding thunks to a function declared in
  117. the class currently being laid out. */
  118. gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
  119. && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
  120. thunk = build_decl (DECL_SOURCE_LOCATION (function),
  121. FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
  122. DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
  123. cxx_dup_lang_specific_decl (thunk);
  124. DECL_VIRTUAL_P (thunk) = true;
  125. SET_DECL_THUNKS (thunk, NULL_TREE);
  126. DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
  127. TREE_READONLY (thunk) = TREE_READONLY (function);
  128. TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
  129. TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
  130. SET_DECL_THUNK_P (thunk, this_adjusting);
  131. THUNK_TARGET (thunk) = function;
  132. THUNK_FIXED_OFFSET (thunk) = d;
  133. THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
  134. THUNK_ALIAS (thunk) = NULL_TREE;
  135. DECL_INTERFACE_KNOWN (thunk) = 1;
  136. DECL_NOT_REALLY_EXTERN (thunk) = 1;
  137. DECL_COMDAT (thunk) = DECL_COMDAT (function);
  138. DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
  139. /* The thunk itself is not a constructor or destructor, even if
  140. the thing it is thunking to is. */
  141. DECL_DESTRUCTOR_P (thunk) = 0;
  142. DECL_CONSTRUCTOR_P (thunk) = 0;
  143. DECL_EXTERNAL (thunk) = 1;
  144. DECL_ARTIFICIAL (thunk) = 1;
  145. /* The THUNK is not a pending inline, even if the FUNCTION is. */
  146. DECL_PENDING_INLINE_P (thunk) = 0;
  147. DECL_DECLARED_INLINE_P (thunk) = 0;
  148. /* Nor is it a template instantiation. */
  149. DECL_USE_TEMPLATE (thunk) = 0;
  150. DECL_TEMPLATE_INFO (thunk) = NULL;
  151. /* Add it to the list of thunks associated with FUNCTION. */
  152. DECL_CHAIN (thunk) = DECL_THUNKS (function);
  153. SET_DECL_THUNKS (function, thunk);
  154. return thunk;
  155. }
  156. /* Finish THUNK, a thunk decl. */
  157. void
  158. finish_thunk (tree thunk)
  159. {
  160. tree function, name;
  161. tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
  162. tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
  163. gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
  164. if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
  165. virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
  166. function = THUNK_TARGET (thunk);
  167. name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
  168. fixed_offset, virtual_offset);
  169. /* We can end up with declarations of (logically) different
  170. covariant thunks, that do identical adjustments. The two thunks
  171. will be adjusting between within different hierarchies, which
  172. happen to have the same layout. We must nullify one of them to
  173. refer to the other. */
  174. if (DECL_RESULT_THUNK_P (thunk))
  175. {
  176. tree cov_probe;
  177. for (cov_probe = DECL_THUNKS (function);
  178. cov_probe; cov_probe = DECL_CHAIN (cov_probe))
  179. if (DECL_NAME (cov_probe) == name)
  180. {
  181. gcc_assert (!DECL_THUNKS (thunk));
  182. THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
  183. ? THUNK_ALIAS (cov_probe) : cov_probe);
  184. break;
  185. }
  186. }
  187. DECL_NAME (thunk) = name;
  188. SET_DECL_ASSEMBLER_NAME (thunk, name);
  189. }
  190. static GTY (()) int thunk_labelno;
  191. /* Create a static alias to target. */
  192. tree
  193. make_alias_for (tree target, tree newid)
  194. {
  195. tree alias = build_decl (DECL_SOURCE_LOCATION (target),
  196. TREE_CODE (target), newid, TREE_TYPE (target));
  197. DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
  198. cxx_dup_lang_specific_decl (alias);
  199. DECL_CONTEXT (alias) = NULL;
  200. TREE_READONLY (alias) = TREE_READONLY (target);
  201. TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
  202. TREE_PUBLIC (alias) = 0;
  203. DECL_INTERFACE_KNOWN (alias) = 1;
  204. if (DECL_LANG_SPECIFIC (alias))
  205. {
  206. DECL_NOT_REALLY_EXTERN (alias) = 1;
  207. DECL_USE_TEMPLATE (alias) = 0;
  208. DECL_TEMPLATE_INFO (alias) = NULL;
  209. }
  210. DECL_EXTERNAL (alias) = 0;
  211. DECL_ARTIFICIAL (alias) = 1;
  212. DECL_TEMPLATE_INSTANTIATED (alias) = 0;
  213. if (TREE_CODE (alias) == FUNCTION_DECL)
  214. {
  215. DECL_SAVED_FUNCTION_DATA (alias) = NULL;
  216. DECL_DESTRUCTOR_P (alias) = 0;
  217. DECL_CONSTRUCTOR_P (alias) = 0;
  218. DECL_PENDING_INLINE_P (alias) = 0;
  219. DECL_DECLARED_INLINE_P (alias) = 0;
  220. DECL_INITIAL (alias) = error_mark_node;
  221. DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
  222. }
  223. else
  224. TREE_STATIC (alias) = 1;
  225. TREE_ADDRESSABLE (alias) = 1;
  226. TREE_USED (alias) = 1;
  227. SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
  228. return alias;
  229. }
  230. static tree
  231. make_alias_for_thunk (tree function)
  232. {
  233. tree alias;
  234. char buf[256];
  235. targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
  236. thunk_labelno++;
  237. alias = make_alias_for (function, get_identifier (buf));
  238. if (!flag_syntax_only)
  239. {
  240. struct cgraph_node *funcn, *aliasn;
  241. funcn = cgraph_node::get (function);
  242. gcc_checking_assert (funcn);
  243. aliasn = cgraph_node::create_same_body_alias (alias, function);
  244. DECL_ASSEMBLER_NAME (function);
  245. gcc_assert (aliasn != NULL);
  246. }
  247. return alias;
  248. }
  249. /* Emit the definition of a C++ multiple inheritance or covariant
  250. return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
  251. immediately. */
  252. void
  253. use_thunk (tree thunk_fndecl, bool emit_p)
  254. {
  255. tree a, t, function, alias;
  256. tree virtual_offset;
  257. HOST_WIDE_INT fixed_offset, virtual_value;
  258. bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
  259. struct cgraph_node *funcn, *thunk_node;
  260. /* We should have called finish_thunk to give it a name. */
  261. gcc_assert (DECL_NAME (thunk_fndecl));
  262. /* We should never be using an alias, always refer to the
  263. aliased thunk. */
  264. gcc_assert (!THUNK_ALIAS (thunk_fndecl));
  265. if (TREE_ASM_WRITTEN (thunk_fndecl))
  266. return;
  267. function = THUNK_TARGET (thunk_fndecl);
  268. if (DECL_RESULT (thunk_fndecl))
  269. /* We already turned this thunk into an ordinary function.
  270. There's no need to process this thunk again. */
  271. return;
  272. if (DECL_THUNK_P (function))
  273. /* The target is itself a thunk, process it now. */
  274. use_thunk (function, emit_p);
  275. /* Thunks are always addressable; they only appear in vtables. */
  276. TREE_ADDRESSABLE (thunk_fndecl) = 1;
  277. /* Figure out what function is being thunked to. It's referenced in
  278. this translation unit. */
  279. TREE_ADDRESSABLE (function) = 1;
  280. mark_used (function);
  281. if (!emit_p)
  282. return;
  283. if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
  284. alias = make_alias_for_thunk (function);
  285. else
  286. alias = function;
  287. fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
  288. virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
  289. if (virtual_offset)
  290. {
  291. if (!this_adjusting)
  292. virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
  293. virtual_value = tree_to_shwi (virtual_offset);
  294. gcc_assert (virtual_value);
  295. }
  296. else
  297. virtual_value = 0;
  298. /* And, if we need to emit the thunk, it's used. */
  299. mark_used (thunk_fndecl);
  300. /* This thunk is actually defined. */
  301. DECL_EXTERNAL (thunk_fndecl) = 0;
  302. /* The linkage of the function may have changed. FIXME in linkage
  303. rewrite. */
  304. gcc_assert (DECL_INTERFACE_KNOWN (function));
  305. TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
  306. DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
  307. DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
  308. = DECL_VISIBILITY_SPECIFIED (function);
  309. DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
  310. DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
  311. if (flag_syntax_only)
  312. {
  313. TREE_ASM_WRITTEN (thunk_fndecl) = 1;
  314. return;
  315. }
  316. push_to_top_level ();
  317. if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
  318. && targetm_common.have_named_sections)
  319. {
  320. tree fn = function;
  321. struct symtab_node *symbol;
  322. if ((symbol = symtab_node::get (function))
  323. && symbol->alias)
  324. {
  325. if (symbol->analyzed)
  326. fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
  327. else
  328. fn = symtab_node::get (function)->alias_target;
  329. }
  330. resolve_unique_section (fn, 0, flag_function_sections);
  331. if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
  332. {
  333. resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
  334. /* Output the thunk into the same section as function. */
  335. set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
  336. symtab_node::get (thunk_fndecl)->implicit_section
  337. = symtab_node::get (fn)->implicit_section;
  338. }
  339. }
  340. /* Set up cloned argument trees for the thunk. */
  341. t = NULL_TREE;
  342. for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
  343. {
  344. tree x = copy_node (a);
  345. DECL_CHAIN (x) = t;
  346. DECL_CONTEXT (x) = thunk_fndecl;
  347. SET_DECL_RTL (x, NULL);
  348. DECL_HAS_VALUE_EXPR_P (x) = 0;
  349. TREE_ADDRESSABLE (x) = 0;
  350. t = x;
  351. }
  352. a = nreverse (t);
  353. DECL_ARGUMENTS (thunk_fndecl) = a;
  354. TREE_ASM_WRITTEN (thunk_fndecl) = 1;
  355. funcn = cgraph_node::get (function);
  356. gcc_checking_assert (funcn);
  357. thunk_node = funcn->create_thunk (thunk_fndecl, function,
  358. this_adjusting, fixed_offset, virtual_value,
  359. virtual_offset, alias);
  360. if (DECL_ONE_ONLY (function))
  361. thunk_node->add_to_same_comdat_group (funcn);
  362. pop_from_top_level ();
  363. }
  364. /* Code for synthesizing methods which have default semantics defined. */
  365. /* True iff CTYPE has a trivial SFK. */
  366. static bool
  367. type_has_trivial_fn (tree ctype, special_function_kind sfk)
  368. {
  369. switch (sfk)
  370. {
  371. case sfk_constructor:
  372. return !TYPE_HAS_COMPLEX_DFLT (ctype);
  373. case sfk_copy_constructor:
  374. return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
  375. case sfk_move_constructor:
  376. return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
  377. case sfk_copy_assignment:
  378. return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
  379. case sfk_move_assignment:
  380. return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
  381. case sfk_destructor:
  382. return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
  383. case sfk_inheriting_constructor:
  384. return false;
  385. default:
  386. gcc_unreachable ();
  387. }
  388. }
  389. /* Note that CTYPE has a non-trivial SFK even though we previously thought
  390. it was trivial. */
  391. static void
  392. type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
  393. {
  394. switch (sfk)
  395. {
  396. case sfk_constructor:
  397. TYPE_HAS_COMPLEX_DFLT (ctype) = true;
  398. return;
  399. case sfk_copy_constructor:
  400. TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
  401. return;
  402. case sfk_move_constructor:
  403. TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
  404. return;
  405. case sfk_copy_assignment:
  406. TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
  407. return;
  408. case sfk_move_assignment:
  409. TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
  410. return;
  411. case sfk_destructor:
  412. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
  413. return;
  414. case sfk_inheriting_constructor:
  415. default:
  416. gcc_unreachable ();
  417. }
  418. }
  419. /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
  420. bool
  421. trivial_fn_p (tree fn)
  422. {
  423. if (!DECL_DEFAULTED_FN (fn))
  424. return false;
  425. /* If fn is a clone, get the primary variant. */
  426. if (tree prim = DECL_CLONED_FUNCTION (fn))
  427. fn = prim;
  428. return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
  429. }
  430. /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
  431. given the parameter or parameters PARM, possibly inherited constructor
  432. base INH, or move flag MOVE_P. */
  433. static tree
  434. add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
  435. tree member_init_list)
  436. {
  437. tree init;
  438. if (inh)
  439. {
  440. /* An inheriting constructor only has a mem-initializer for
  441. the base it inherits from. */
  442. if (BINFO_TYPE (binfo) != inh)
  443. return member_init_list;
  444. tree *p = &init;
  445. init = NULL_TREE;
  446. for (; parm; parm = DECL_CHAIN (parm))
  447. {
  448. tree exp = convert_from_reference (parm);
  449. if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
  450. || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
  451. exp = move (exp);
  452. *p = build_tree_list (NULL_TREE, exp);
  453. p = &TREE_CHAIN (*p);
  454. }
  455. }
  456. else
  457. {
  458. init = build_base_path (PLUS_EXPR, parm, binfo, 1,
  459. tf_warning_or_error);
  460. if (move_p)
  461. init = move (init);
  462. init = build_tree_list (NULL_TREE, init);
  463. }
  464. return tree_cons (binfo, init, member_init_list);
  465. }
  466. /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
  467. constructor. */
  468. static void
  469. do_build_copy_constructor (tree fndecl)
  470. {
  471. tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
  472. bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
  473. bool trivial = trivial_fn_p (fndecl);
  474. tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
  475. if (!inh)
  476. parm = convert_from_reference (parm);
  477. if (trivial
  478. && is_empty_class (current_class_type))
  479. /* Don't copy the padding byte; it might not have been allocated
  480. if *this is a base subobject. */;
  481. else if (trivial)
  482. {
  483. tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
  484. finish_expr_stmt (t);
  485. }
  486. else
  487. {
  488. tree fields = TYPE_FIELDS (current_class_type);
  489. tree member_init_list = NULL_TREE;
  490. int cvquals = cp_type_quals (TREE_TYPE (parm));
  491. int i;
  492. tree binfo, base_binfo;
  493. tree init;
  494. vec<tree, va_gc> *vbases;
  495. /* Initialize all the base-classes with the parameter converted
  496. to their type so that we get their copy constructor and not
  497. another constructor that takes current_class_type. We must
  498. deal with the binfo's directly as a direct base might be
  499. inaccessible due to ambiguity. */
  500. for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
  501. vec_safe_iterate (vbases, i, &binfo); i++)
  502. {
  503. member_init_list = add_one_base_init (binfo, parm, move_p, inh,
  504. member_init_list);
  505. }
  506. for (binfo = TYPE_BINFO (current_class_type), i = 0;
  507. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  508. {
  509. if (BINFO_VIRTUAL_P (base_binfo))
  510. continue;
  511. member_init_list = add_one_base_init (base_binfo, parm, move_p,
  512. inh, member_init_list);
  513. }
  514. for (; fields; fields = DECL_CHAIN (fields))
  515. {
  516. tree field = fields;
  517. tree expr_type;
  518. if (TREE_CODE (field) != FIELD_DECL)
  519. continue;
  520. if (inh)
  521. continue;
  522. expr_type = TREE_TYPE (field);
  523. if (DECL_NAME (field))
  524. {
  525. if (VFIELD_NAME_P (DECL_NAME (field)))
  526. continue;
  527. }
  528. else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
  529. /* Just use the field; anonymous types can't have
  530. nontrivial copy ctors or assignment ops or this
  531. function would be deleted. */;
  532. else
  533. continue;
  534. /* Compute the type of "init->field". If the copy-constructor
  535. parameter is, for example, "const S&", and the type of
  536. the field is "T", then the type will usually be "const
  537. T". (There are no cv-qualified variants of reference
  538. types.) */
  539. if (TREE_CODE (expr_type) != REFERENCE_TYPE)
  540. {
  541. int quals = cvquals;
  542. if (DECL_MUTABLE_P (field))
  543. quals &= ~TYPE_QUAL_CONST;
  544. quals |= cp_type_quals (expr_type);
  545. expr_type = cp_build_qualified_type (expr_type, quals);
  546. }
  547. init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
  548. if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
  549. /* 'move' breaks bit-fields, and has no effect for scalars. */
  550. && !scalarish_type_p (expr_type))
  551. init = move (init);
  552. init = build_tree_list (NULL_TREE, init);
  553. member_init_list = tree_cons (field, init, member_init_list);
  554. }
  555. finish_mem_initializers (member_init_list);
  556. }
  557. }
  558. static void
  559. do_build_copy_assign (tree fndecl)
  560. {
  561. tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
  562. tree compound_stmt;
  563. bool move_p = move_fn_p (fndecl);
  564. bool trivial = trivial_fn_p (fndecl);
  565. int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
  566. compound_stmt = begin_compound_stmt (0);
  567. parm = convert_from_reference (parm);
  568. if (trivial
  569. && is_empty_class (current_class_type))
  570. /* Don't copy the padding byte; it might not have been allocated
  571. if *this is a base subobject. */;
  572. else if (trivial)
  573. {
  574. tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
  575. finish_expr_stmt (t);
  576. }
  577. else
  578. {
  579. tree fields;
  580. int cvquals = cp_type_quals (TREE_TYPE (parm));
  581. int i;
  582. tree binfo, base_binfo;
  583. /* Assign to each of the direct base classes. */
  584. for (binfo = TYPE_BINFO (current_class_type), i = 0;
  585. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  586. {
  587. tree converted_parm;
  588. vec<tree, va_gc> *parmvec;
  589. /* We must convert PARM directly to the base class
  590. explicitly since the base class may be ambiguous. */
  591. converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
  592. tf_warning_or_error);
  593. if (move_p)
  594. converted_parm = move (converted_parm);
  595. /* Call the base class assignment operator. */
  596. parmvec = make_tree_vector_single (converted_parm);
  597. finish_expr_stmt
  598. (build_special_member_call (current_class_ref,
  599. ansi_assopname (NOP_EXPR),
  600. &parmvec,
  601. base_binfo,
  602. flags,
  603. tf_warning_or_error));
  604. release_tree_vector (parmvec);
  605. }
  606. /* Assign to each of the non-static data members. */
  607. for (fields = TYPE_FIELDS (current_class_type);
  608. fields;
  609. fields = DECL_CHAIN (fields))
  610. {
  611. tree comp = current_class_ref;
  612. tree init = parm;
  613. tree field = fields;
  614. tree expr_type;
  615. int quals;
  616. if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
  617. continue;
  618. expr_type = TREE_TYPE (field);
  619. if (CP_TYPE_CONST_P (expr_type))
  620. {
  621. error ("non-static const member %q#D, can%'t use default "
  622. "assignment operator", field);
  623. continue;
  624. }
  625. else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
  626. {
  627. error ("non-static reference member %q#D, can%'t use "
  628. "default assignment operator", field);
  629. continue;
  630. }
  631. if (DECL_NAME (field))
  632. {
  633. if (VFIELD_NAME_P (DECL_NAME (field)))
  634. continue;
  635. }
  636. else if (ANON_AGGR_TYPE_P (expr_type)
  637. && TYPE_FIELDS (expr_type) != NULL_TREE)
  638. /* Just use the field; anonymous types can't have
  639. nontrivial copy ctors or assignment ops or this
  640. function would be deleted. */;
  641. else
  642. continue;
  643. comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
  644. /* Compute the type of init->field */
  645. quals = cvquals;
  646. if (DECL_MUTABLE_P (field))
  647. quals &= ~TYPE_QUAL_CONST;
  648. expr_type = cp_build_qualified_type (expr_type, quals);
  649. init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
  650. if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
  651. /* 'move' breaks bit-fields, and has no effect for scalars. */
  652. && !scalarish_type_p (expr_type))
  653. init = move (init);
  654. if (DECL_NAME (field))
  655. init = cp_build_modify_expr (comp, NOP_EXPR, init,
  656. tf_warning_or_error);
  657. else
  658. init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
  659. finish_expr_stmt (init);
  660. }
  661. }
  662. finish_return_stmt (current_class_ref);
  663. finish_compound_stmt (compound_stmt);
  664. }
  665. /* Synthesize FNDECL, a non-static member function. */
  666. void
  667. synthesize_method (tree fndecl)
  668. {
  669. bool nested = (current_function_decl != NULL_TREE);
  670. tree context = decl_function_context (fndecl);
  671. bool need_body = true;
  672. tree stmt;
  673. location_t save_input_location = input_location;
  674. int error_count = errorcount;
  675. int warning_count = warningcount + werrorcount;
  676. /* Reset the source location, we might have been previously
  677. deferred, and thus have saved where we were first needed. */
  678. DECL_SOURCE_LOCATION (fndecl)
  679. = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
  680. /* If we've been asked to synthesize a clone, just synthesize the
  681. cloned function instead. Doing so will automatically fill in the
  682. body for the clone. */
  683. if (DECL_CLONED_FUNCTION_P (fndecl))
  684. fndecl = DECL_CLONED_FUNCTION (fndecl);
  685. /* We may be in the middle of deferred access check. Disable
  686. it now. */
  687. push_deferring_access_checks (dk_no_deferred);
  688. if (! context)
  689. push_to_top_level ();
  690. else if (nested)
  691. push_function_context ();
  692. input_location = DECL_SOURCE_LOCATION (fndecl);
  693. start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
  694. stmt = begin_function_body ();
  695. if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
  696. {
  697. do_build_copy_assign (fndecl);
  698. need_body = false;
  699. }
  700. else if (DECL_CONSTRUCTOR_P (fndecl))
  701. {
  702. tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
  703. if (arg_chain != void_list_node)
  704. do_build_copy_constructor (fndecl);
  705. else
  706. finish_mem_initializers (NULL_TREE);
  707. }
  708. /* If we haven't yet generated the body of the function, just
  709. generate an empty compound statement. */
  710. if (need_body)
  711. {
  712. tree compound_stmt;
  713. compound_stmt = begin_compound_stmt (BCS_FN_BODY);
  714. finish_compound_stmt (compound_stmt);
  715. }
  716. finish_function_body (stmt);
  717. expand_or_defer_fn (finish_function (0));
  718. input_location = save_input_location;
  719. if (! context)
  720. pop_from_top_level ();
  721. else if (nested)
  722. pop_function_context ();
  723. pop_deferring_access_checks ();
  724. if (error_count != errorcount || warning_count != warningcount + werrorcount)
  725. inform (input_location, "synthesized method %qD first required here ",
  726. fndecl);
  727. }
  728. /* Build a reference to type TYPE with cv-quals QUALS, which is an
  729. rvalue if RVALUE is true. */
  730. static tree
  731. build_stub_type (tree type, int quals, bool rvalue)
  732. {
  733. tree argtype = cp_build_qualified_type (type, quals);
  734. return cp_build_reference_type (argtype, rvalue);
  735. }
  736. /* Build a dummy glvalue from dereferencing a dummy reference of type
  737. REFTYPE. */
  738. static tree
  739. build_stub_object (tree reftype)
  740. {
  741. if (TREE_CODE (reftype) != REFERENCE_TYPE)
  742. reftype = cp_build_reference_type (reftype, /*rval*/true);
  743. tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
  744. return convert_from_reference (stub);
  745. }
  746. /* Determine which function will be called when looking up NAME in TYPE,
  747. called with a single ARGTYPE argument, or no argument if ARGTYPE is
  748. null. FLAGS and COMPLAIN are as for build_new_method_call.
  749. Returns a FUNCTION_DECL if all is well.
  750. Returns NULL_TREE if overload resolution failed.
  751. Returns error_mark_node if the chosen function cannot be called. */
  752. static tree
  753. locate_fn_flags (tree type, tree name, tree argtype, int flags,
  754. tsubst_flags_t complain)
  755. {
  756. tree ob, fn, fns, binfo, rval;
  757. vec<tree, va_gc> *args;
  758. if (TYPE_P (type))
  759. binfo = TYPE_BINFO (type);
  760. else
  761. {
  762. binfo = type;
  763. type = BINFO_TYPE (binfo);
  764. }
  765. ob = build_stub_object (cp_build_reference_type (type, false));
  766. args = make_tree_vector ();
  767. if (argtype)
  768. {
  769. if (TREE_CODE (argtype) == TREE_LIST)
  770. {
  771. for (tree elt = argtype; elt != void_list_node;
  772. elt = TREE_CHAIN (elt))
  773. {
  774. tree type = TREE_VALUE (elt);
  775. tree arg = build_stub_object (type);
  776. vec_safe_push (args, arg);
  777. }
  778. }
  779. else
  780. {
  781. tree arg = build_stub_object (argtype);
  782. args->quick_push (arg);
  783. }
  784. }
  785. fns = lookup_fnfields (binfo, name, 0);
  786. rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
  787. release_tree_vector (args);
  788. if (fn && rval == error_mark_node)
  789. return rval;
  790. else
  791. return fn;
  792. }
  793. /* Locate the dtor of TYPE. */
  794. tree
  795. get_dtor (tree type, tsubst_flags_t complain)
  796. {
  797. tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
  798. LOOKUP_NORMAL, complain);
  799. if (fn == error_mark_node)
  800. return NULL_TREE;
  801. return fn;
  802. }
  803. /* Locate the default ctor of TYPE. */
  804. tree
  805. locate_ctor (tree type)
  806. {
  807. tree fn;
  808. push_deferring_access_checks (dk_no_check);
  809. fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
  810. LOOKUP_SPECULATIVE, tf_none);
  811. pop_deferring_access_checks ();
  812. if (fn == error_mark_node)
  813. return NULL_TREE;
  814. return fn;
  815. }
  816. /* Likewise, but give any appropriate errors. */
  817. tree
  818. get_default_ctor (tree type)
  819. {
  820. tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
  821. LOOKUP_NORMAL, tf_warning_or_error);
  822. if (fn == error_mark_node)
  823. return NULL_TREE;
  824. return fn;
  825. }
  826. /* Locate the copy ctor of TYPE. */
  827. tree
  828. get_copy_ctor (tree type, tsubst_flags_t complain)
  829. {
  830. int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
  831. ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
  832. tree argtype = build_stub_type (type, quals, false);
  833. tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
  834. LOOKUP_NORMAL, complain);
  835. if (fn == error_mark_node)
  836. return NULL_TREE;
  837. return fn;
  838. }
  839. /* Locate the copy assignment operator of TYPE. */
  840. tree
  841. get_copy_assign (tree type)
  842. {
  843. int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
  844. ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
  845. tree argtype = build_stub_type (type, quals, false);
  846. tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
  847. LOOKUP_NORMAL, tf_warning_or_error);
  848. if (fn == error_mark_node)
  849. return NULL_TREE;
  850. return fn;
  851. }
  852. /* Locate the inherited constructor of constructor CTOR. */
  853. tree
  854. get_inherited_ctor (tree ctor)
  855. {
  856. gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
  857. push_deferring_access_checks (dk_no_check);
  858. tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
  859. complete_ctor_identifier,
  860. FUNCTION_FIRST_USER_PARMTYPE (ctor),
  861. LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
  862. tf_none);
  863. pop_deferring_access_checks ();
  864. if (fn == error_mark_node)
  865. return NULL_TREE;
  866. return fn;
  867. }
  868. /* walk_tree helper function for is_trivially_xible. If *TP is a call,
  869. return it if it calls something other than a trivial special member
  870. function. */
  871. static tree
  872. check_nontriv (tree *tp, int *, void *)
  873. {
  874. tree fn;
  875. if (TREE_CODE (*tp) == CALL_EXPR)
  876. fn = CALL_EXPR_FN (*tp);
  877. else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
  878. fn = AGGR_INIT_EXPR_FN (*tp);
  879. else
  880. return NULL_TREE;
  881. if (TREE_CODE (fn) == ADDR_EXPR)
  882. fn = TREE_OPERAND (fn, 0);
  883. if (TREE_CODE (fn) != FUNCTION_DECL
  884. || !trivial_fn_p (fn))
  885. return fn;
  886. return NULL_TREE;
  887. }
  888. /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
  889. static tree
  890. assignable_expr (tree to, tree from)
  891. {
  892. ++cp_unevaluated_operand;
  893. to = build_stub_object (to);
  894. from = build_stub_object (from);
  895. tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
  896. --cp_unevaluated_operand;
  897. return r;
  898. }
  899. /* The predicate condition for a template specialization
  900. is_constructible<T, Args...> shall be satisfied if and only if the
  901. following variable definition would be well-formed for some invented
  902. variable t: T t(create<Args>()...);
  903. Return something equivalent in well-formedness and triviality. */
  904. static tree
  905. constructible_expr (tree to, tree from)
  906. {
  907. tree expr;
  908. if (CLASS_TYPE_P (to))
  909. {
  910. tree ctype = to;
  911. vec<tree, va_gc> *args = NULL;
  912. if (TREE_CODE (to) != REFERENCE_TYPE)
  913. to = cp_build_reference_type (to, /*rval*/false);
  914. tree ob = build_stub_object (to);
  915. for (; from; from = TREE_CHAIN (from))
  916. vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
  917. expr = build_special_member_call (ob, complete_ctor_identifier, &args,
  918. ctype, LOOKUP_NORMAL, tf_none);
  919. if (expr == error_mark_node)
  920. return error_mark_node;
  921. /* The current state of the standard vis-a-vis LWG 2116 is that
  922. is_*constructible involves destruction as well. */
  923. if (type_build_dtor_call (ctype))
  924. {
  925. tree dtor = build_special_member_call (ob, complete_dtor_identifier,
  926. NULL, ctype, LOOKUP_NORMAL,
  927. tf_none);
  928. if (dtor == error_mark_node)
  929. return error_mark_node;
  930. if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
  931. expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
  932. }
  933. }
  934. else
  935. {
  936. if (from == NULL_TREE)
  937. return build_value_init (to, tf_none);
  938. else if (TREE_CHAIN (from))
  939. return error_mark_node; // too many initializers
  940. from = build_stub_object (TREE_VALUE (from));
  941. expr = perform_direct_initialization_if_possible (to, from,
  942. /*cast*/false,
  943. tf_none);
  944. }
  945. return expr;
  946. }
  947. /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
  948. constructible (otherwise) from FROM, which is a single type for
  949. assignment or a list of types for construction. */
  950. bool
  951. is_trivially_xible (enum tree_code code, tree to, tree from)
  952. {
  953. tree expr;
  954. if (code == MODIFY_EXPR)
  955. expr = assignable_expr (to, from);
  956. else if (from && TREE_CHAIN (from))
  957. return false; // only 0- and 1-argument ctors can be trivial
  958. else
  959. expr = constructible_expr (to, from);
  960. if (expr == error_mark_node)
  961. return false;
  962. tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
  963. return !nt;
  964. }
  965. /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
  966. DELETED_P or give an error message MSG with argument ARG. */
  967. static void
  968. process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
  969. bool *deleted_p, bool *constexpr_p,
  970. bool diag, tree arg)
  971. {
  972. if (!fn || fn == error_mark_node)
  973. goto bad;
  974. if (spec_p)
  975. {
  976. maybe_instantiate_noexcept (fn);
  977. tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
  978. *spec_p = merge_exception_specifiers (*spec_p, raises);
  979. }
  980. if (!trivial_fn_p (fn))
  981. {
  982. if (trivial_p)
  983. *trivial_p = false;
  984. if (TREE_CODE (arg) == FIELD_DECL
  985. && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
  986. {
  987. if (deleted_p)
  988. *deleted_p = true;
  989. if (diag)
  990. error ("union member %q+D with non-trivial %qD", arg, fn);
  991. }
  992. }
  993. if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
  994. {
  995. *constexpr_p = false;
  996. if (diag)
  997. {
  998. inform (0, "defaulted constructor calls non-constexpr "
  999. "%q+D", fn);
  1000. explain_invalid_constexpr_fn (fn);
  1001. }
  1002. }
  1003. return;
  1004. bad:
  1005. if (deleted_p)
  1006. *deleted_p = true;
  1007. }
  1008. /* Subroutine of synthesized_method_walk to allow recursion into anonymous
  1009. aggregates. */
  1010. static void
  1011. walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
  1012. int quals, bool copy_arg_p, bool move_p,
  1013. bool assign_p, tree *spec_p, bool *trivial_p,
  1014. bool *deleted_p, bool *constexpr_p,
  1015. bool diag, int flags, tsubst_flags_t complain)
  1016. {
  1017. tree field;
  1018. for (field = fields; field; field = DECL_CHAIN (field))
  1019. {
  1020. tree mem_type, argtype, rval;
  1021. if (TREE_CODE (field) != FIELD_DECL
  1022. || DECL_ARTIFICIAL (field))
  1023. continue;
  1024. mem_type = strip_array_types (TREE_TYPE (field));
  1025. if (assign_p)
  1026. {
  1027. bool bad = true;
  1028. if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
  1029. {
  1030. if (diag)
  1031. error ("non-static const member %q#D, can%'t use default "
  1032. "assignment operator", field);
  1033. }
  1034. else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
  1035. {
  1036. if (diag)
  1037. error ("non-static reference member %q#D, can%'t use "
  1038. "default assignment operator", field);
  1039. }
  1040. else
  1041. bad = false;
  1042. if (bad && deleted_p)
  1043. *deleted_p = true;
  1044. }
  1045. else if (sfk == sfk_constructor)
  1046. {
  1047. bool bad;
  1048. if (DECL_INITIAL (field))
  1049. {
  1050. if (diag && DECL_INITIAL (field) == error_mark_node)
  1051. inform (0, "initializer for %q+#D is invalid", field);
  1052. if (trivial_p)
  1053. *trivial_p = false;
  1054. /* Core 1351: If the field has an NSDMI that could throw, the
  1055. default constructor is noexcept(false). */
  1056. if (spec_p)
  1057. {
  1058. tree nsdmi = get_nsdmi (field, /*ctor*/false);
  1059. if (!expr_noexcept_p (nsdmi, complain))
  1060. *spec_p = noexcept_false_spec;
  1061. }
  1062. /* Don't do the normal processing. */
  1063. continue;
  1064. }
  1065. bad = false;
  1066. if (CP_TYPE_CONST_P (mem_type)
  1067. && default_init_uninitialized_part (mem_type))
  1068. {
  1069. if (diag)
  1070. {
  1071. error ("uninitialized const member in %q#T",
  1072. current_class_type);
  1073. inform (DECL_SOURCE_LOCATION (field),
  1074. "%q#D should be initialized", field);
  1075. }
  1076. bad = true;
  1077. }
  1078. else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
  1079. {
  1080. if (diag)
  1081. {
  1082. error ("uninitialized reference member in %q#T",
  1083. current_class_type);
  1084. inform (DECL_SOURCE_LOCATION (field),
  1085. "%q#D should be initialized", field);
  1086. }
  1087. bad = true;
  1088. }
  1089. if (bad && deleted_p)
  1090. *deleted_p = true;
  1091. /* For an implicitly-defined default constructor to be constexpr,
  1092. every member must have a user-provided default constructor or
  1093. an explicit initializer. */
  1094. if (constexpr_p && !CLASS_TYPE_P (mem_type)
  1095. && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
  1096. {
  1097. *constexpr_p = false;
  1098. if (diag)
  1099. inform (0, "defaulted default constructor does not "
  1100. "initialize %q+#D", field);
  1101. }
  1102. }
  1103. else if (sfk == sfk_copy_constructor)
  1104. {
  1105. /* 12.8p11b5 */
  1106. if (TREE_CODE (mem_type) == REFERENCE_TYPE
  1107. && TYPE_REF_IS_RVALUE (mem_type))
  1108. {
  1109. if (diag)
  1110. error ("copying non-static data member %q#D of rvalue "
  1111. "reference type", field);
  1112. if (deleted_p)
  1113. *deleted_p = true;
  1114. }
  1115. }
  1116. if (!CLASS_TYPE_P (mem_type))
  1117. continue;
  1118. if (ANON_AGGR_TYPE_P (mem_type))
  1119. {
  1120. walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
  1121. copy_arg_p, move_p, assign_p, spec_p, trivial_p,
  1122. deleted_p, constexpr_p,
  1123. diag, flags, complain);
  1124. continue;
  1125. }
  1126. if (copy_arg_p)
  1127. {
  1128. int mem_quals = cp_type_quals (mem_type) | quals;
  1129. if (DECL_MUTABLE_P (field))
  1130. mem_quals &= ~TYPE_QUAL_CONST;
  1131. argtype = build_stub_type (mem_type, mem_quals, move_p);
  1132. }
  1133. else
  1134. argtype = NULL_TREE;
  1135. rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
  1136. process_subob_fn (rval, spec_p, trivial_p, deleted_p,
  1137. constexpr_p, diag, field);
  1138. }
  1139. }
  1140. /* The caller wants to generate an implicit declaration of SFK for CTYPE
  1141. which is const if relevant and CONST_P is set. If spec_p, trivial_p and
  1142. deleted_p are non-null, set their referent appropriately. If diag is
  1143. true, we're either being called from maybe_explain_implicit_delete to
  1144. give errors, or if constexpr_p is non-null, from
  1145. explain_invalid_constexpr_fn. */
  1146. static void
  1147. synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
  1148. tree *spec_p, bool *trivial_p, bool *deleted_p,
  1149. bool *constexpr_p, bool diag,
  1150. tree inherited_base, tree inherited_parms)
  1151. {
  1152. tree binfo, base_binfo, scope, fnname, rval, argtype;
  1153. bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
  1154. vec<tree, va_gc> *vbases;
  1155. int i, quals, flags;
  1156. tsubst_flags_t complain;
  1157. bool ctor_p;
  1158. if (spec_p)
  1159. *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
  1160. if (deleted_p)
  1161. {
  1162. /* "The closure type associated with a lambda-expression has a deleted
  1163. default constructor and a deleted copy assignment operator."
  1164. This is diagnosed in maybe_explain_implicit_delete. */
  1165. if (LAMBDA_TYPE_P (ctype)
  1166. && (sfk == sfk_constructor
  1167. || sfk == sfk_copy_assignment))
  1168. {
  1169. *deleted_p = true;
  1170. return;
  1171. }
  1172. *deleted_p = false;
  1173. }
  1174. ctor_p = false;
  1175. assign_p = false;
  1176. check_vdtor = false;
  1177. switch (sfk)
  1178. {
  1179. case sfk_move_assignment:
  1180. case sfk_copy_assignment:
  1181. assign_p = true;
  1182. fnname = ansi_assopname (NOP_EXPR);
  1183. break;
  1184. case sfk_destructor:
  1185. check_vdtor = true;
  1186. /* The synthesized method will call base dtors, but check complete
  1187. here to avoid having to deal with VTT. */
  1188. fnname = complete_dtor_identifier;
  1189. break;
  1190. case sfk_constructor:
  1191. case sfk_move_constructor:
  1192. case sfk_copy_constructor:
  1193. case sfk_inheriting_constructor:
  1194. ctor_p = true;
  1195. fnname = complete_ctor_identifier;
  1196. break;
  1197. default:
  1198. gcc_unreachable ();
  1199. }
  1200. gcc_assert ((sfk == sfk_inheriting_constructor)
  1201. == (inherited_base != NULL_TREE));
  1202. /* If that user-written default constructor would satisfy the
  1203. requirements of a constexpr constructor (7.1.5), the
  1204. implicitly-defined default constructor is constexpr. */
  1205. if (constexpr_p)
  1206. *constexpr_p = ctor_p;
  1207. move_p = false;
  1208. switch (sfk)
  1209. {
  1210. case sfk_constructor:
  1211. case sfk_destructor:
  1212. case sfk_inheriting_constructor:
  1213. copy_arg_p = false;
  1214. break;
  1215. case sfk_move_constructor:
  1216. case sfk_move_assignment:
  1217. move_p = true;
  1218. case sfk_copy_constructor:
  1219. case sfk_copy_assignment:
  1220. copy_arg_p = true;
  1221. break;
  1222. default:
  1223. gcc_unreachable ();
  1224. }
  1225. expected_trivial = type_has_trivial_fn (ctype, sfk);
  1226. if (trivial_p)
  1227. *trivial_p = expected_trivial;
  1228. /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
  1229. class versions and other properties of the type. But a subobject
  1230. class can be trivially copyable and yet have overload resolution
  1231. choose a template constructor for initialization, depending on
  1232. rvalueness and cv-quals. And furthermore, a member in a base might
  1233. be trivial but deleted or otherwise not callable. So we can't exit
  1234. early in C++0x. The same considerations apply in C++98/03, but
  1235. there the definition of triviality does not consider overload
  1236. resolution, so a constructor can be trivial even if it would otherwise
  1237. call a non-trivial constructor. */
  1238. if (expected_trivial
  1239. && (!copy_arg_p || cxx_dialect < cxx11))
  1240. {
  1241. if (constexpr_p && sfk == sfk_constructor)
  1242. {
  1243. bool cx = trivial_default_constructor_is_constexpr (ctype);
  1244. *constexpr_p = cx;
  1245. if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
  1246. /* A trivial constructor doesn't have any NSDMI. */
  1247. inform (input_location, "defaulted default constructor does "
  1248. "not initialize any non-static data member");
  1249. }
  1250. if (!diag && cxx_dialect < cxx11)
  1251. return;
  1252. }
  1253. ++cp_unevaluated_operand;
  1254. ++c_inhibit_evaluation_warnings;
  1255. push_deferring_access_checks (dk_no_deferred);
  1256. scope = push_scope (ctype);
  1257. flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
  1258. if (!inherited_base)
  1259. flags |= LOOKUP_DEFAULTED;
  1260. complain = diag ? tf_warning_or_error : tf_none;
  1261. if (const_p)
  1262. quals = TYPE_QUAL_CONST;
  1263. else
  1264. quals = TYPE_UNQUALIFIED;
  1265. argtype = NULL_TREE;
  1266. for (binfo = TYPE_BINFO (ctype), i = 0;
  1267. BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  1268. {
  1269. tree basetype = BINFO_TYPE (base_binfo);
  1270. if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
  1271. /* We'll handle virtual bases below. */
  1272. continue;
  1273. if (copy_arg_p)
  1274. argtype = build_stub_type (basetype, quals, move_p);
  1275. else if (basetype == inherited_base)
  1276. argtype = inherited_parms;
  1277. rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
  1278. if (inherited_base)
  1279. argtype = NULL_TREE;
  1280. process_subob_fn (rval, spec_p, trivial_p, deleted_p,
  1281. constexpr_p, diag, basetype);
  1282. if (ctor_p)
  1283. {
  1284. /* In a constructor we also need to check the subobject
  1285. destructors for cleanup of partially constructed objects. */
  1286. rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
  1287. NULL_TREE, flags, complain);
  1288. /* Note that we don't pass down trivial_p; the subobject
  1289. destructors don't affect triviality of the constructor. Nor
  1290. do they affect constexpr-ness (a constant expression doesn't
  1291. throw) or exception-specification (a throw from one of the
  1292. dtors would be a double-fault). */
  1293. process_subob_fn (rval, NULL, NULL,
  1294. deleted_p, NULL, false,
  1295. basetype);
  1296. }
  1297. if (check_vdtor && type_has_virtual_destructor (basetype))
  1298. {
  1299. rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
  1300. ptr_type_node, flags, complain);
  1301. /* Unlike for base ctor/op=/dtor, for operator delete it's fine
  1302. to have a null rval (no class-specific op delete). */
  1303. if (rval && rval == error_mark_node && deleted_p)
  1304. *deleted_p = true;
  1305. check_vdtor = false;
  1306. }
  1307. if (diag && assign_p && move_p
  1308. && BINFO_VIRTUAL_P (base_binfo)
  1309. && rval && TREE_CODE (rval) == FUNCTION_DECL
  1310. && move_fn_p (rval) && !trivial_fn_p (rval)
  1311. && vbase_has_user_provided_move_assign (basetype))
  1312. warning (OPT_Wvirtual_move_assign,
  1313. "defaulted move assignment for %qT calls a non-trivial "
  1314. "move assignment operator for virtual base %qT",
  1315. ctype, basetype);
  1316. }
  1317. vbases = CLASSTYPE_VBASECLASSES (ctype);
  1318. if (vec_safe_is_empty (vbases))
  1319. /* No virtual bases to worry about. */;
  1320. else if (!assign_p)
  1321. {
  1322. if (constexpr_p)
  1323. *constexpr_p = false;
  1324. FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
  1325. {
  1326. tree basetype = BINFO_TYPE (base_binfo);
  1327. if (copy_arg_p)
  1328. argtype = build_stub_type (basetype, quals, move_p);
  1329. rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
  1330. process_subob_fn (rval, spec_p, trivial_p, deleted_p,
  1331. constexpr_p, diag, basetype);
  1332. if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
  1333. {
  1334. rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
  1335. NULL_TREE, flags, complain);
  1336. process_subob_fn (rval, NULL, NULL,
  1337. deleted_p, NULL, false,
  1338. basetype);
  1339. }
  1340. }
  1341. }
  1342. /* Now handle the non-static data members. */
  1343. walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
  1344. copy_arg_p, move_p, assign_p, spec_p, trivial_p,
  1345. deleted_p, constexpr_p,
  1346. diag, flags, complain);
  1347. if (ctor_p)
  1348. walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
  1349. sfk_destructor, TYPE_UNQUALIFIED, false,
  1350. false, false, NULL, NULL,
  1351. deleted_p, NULL,
  1352. false, flags, complain);
  1353. pop_scope (scope);
  1354. pop_deferring_access_checks ();
  1355. --cp_unevaluated_operand;
  1356. --c_inhibit_evaluation_warnings;
  1357. }
  1358. /* DECL is a defaulted function whose exception specification is now
  1359. needed. Return what it should be. */
  1360. tree
  1361. get_defaulted_eh_spec (tree decl)
  1362. {
  1363. if (DECL_CLONED_FUNCTION_P (decl))
  1364. decl = DECL_CLONED_FUNCTION (decl);
  1365. special_function_kind sfk = special_function_p (decl);
  1366. tree ctype = DECL_CONTEXT (decl);
  1367. tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
  1368. tree parm_type = TREE_VALUE (parms);
  1369. bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
  1370. tree spec = empty_except_spec;
  1371. synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
  1372. NULL, false, DECL_INHERITED_CTOR_BASE (decl),
  1373. parms);
  1374. return spec;
  1375. }
  1376. /* DECL is a deleted function. If it's implicitly deleted, explain why and
  1377. return true; else return false. */
  1378. bool
  1379. maybe_explain_implicit_delete (tree decl)
  1380. {
  1381. /* If decl is a clone, get the primary variant. */
  1382. decl = DECL_ORIGIN (decl);
  1383. gcc_assert (DECL_DELETED_FN (decl));
  1384. if (DECL_DEFAULTED_FN (decl))
  1385. {
  1386. /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
  1387. static hash_set<tree> *explained;
  1388. special_function_kind sfk;
  1389. location_t loc;
  1390. bool informed;
  1391. tree ctype;
  1392. if (!explained)
  1393. explained = new hash_set<tree>;
  1394. if (explained->add (decl))
  1395. return true;
  1396. sfk = special_function_p (decl);
  1397. ctype = DECL_CONTEXT (decl);
  1398. loc = input_location;
  1399. input_location = DECL_SOURCE_LOCATION (decl);
  1400. informed = false;
  1401. if (LAMBDA_TYPE_P (ctype))
  1402. {
  1403. informed = true;
  1404. if (sfk == sfk_constructor)
  1405. inform (DECL_SOURCE_LOCATION (decl),
  1406. "a lambda closure type has a deleted default constructor");
  1407. else if (sfk == sfk_copy_assignment)
  1408. inform (DECL_SOURCE_LOCATION (decl),
  1409. "a lambda closure type has a deleted copy assignment operator");
  1410. else
  1411. informed = false;
  1412. }
  1413. else if (DECL_ARTIFICIAL (decl)
  1414. && (sfk == sfk_copy_assignment
  1415. || sfk == sfk_copy_constructor)
  1416. && (type_has_user_declared_move_constructor (ctype)
  1417. || type_has_user_declared_move_assign (ctype)))
  1418. {
  1419. inform (0, "%q+#D is implicitly declared as deleted because %qT "
  1420. "declares a move constructor or move assignment operator",
  1421. decl, ctype);
  1422. informed = true;
  1423. }
  1424. if (!informed)
  1425. {
  1426. tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
  1427. tree parm_type = TREE_VALUE (parms);
  1428. bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
  1429. tree raises = NULL_TREE;
  1430. bool deleted_p = false;
  1431. tree scope = push_scope (ctype);
  1432. synthesized_method_walk (ctype, sfk, const_p,
  1433. &raises, NULL, &deleted_p, NULL, false,
  1434. DECL_INHERITED_CTOR_BASE (decl), parms);
  1435. if (deleted_p)
  1436. {
  1437. inform (0, "%q+#D is implicitly deleted because the default "
  1438. "definition would be ill-formed:", decl);
  1439. synthesized_method_walk (ctype, sfk, const_p,
  1440. NULL, NULL, NULL, NULL, true,
  1441. DECL_INHERITED_CTOR_BASE (decl), parms);
  1442. }
  1443. else if (!comp_except_specs
  1444. (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
  1445. raises, ce_normal))
  1446. inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
  1447. "deleted because its exception-specification does not "
  1448. "match the implicit exception-specification %qX",
  1449. decl, raises);
  1450. #ifdef ENABLE_CHECKING
  1451. else
  1452. gcc_unreachable ();
  1453. #endif
  1454. pop_scope (scope);
  1455. }
  1456. input_location = loc;
  1457. return true;
  1458. }
  1459. return false;
  1460. }
  1461. /* DECL is a defaulted function which was declared constexpr. Explain why
  1462. it can't be constexpr. */
  1463. void
  1464. explain_implicit_non_constexpr (tree decl)
  1465. {
  1466. tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
  1467. bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
  1468. bool dummy;
  1469. synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
  1470. special_function_p (decl), const_p,
  1471. NULL, NULL, NULL, &dummy, true,
  1472. DECL_INHERITED_CTOR_BASE (decl),
  1473. FUNCTION_FIRST_USER_PARMTYPE (decl));
  1474. }
  1475. /* DECL is an instantiation of an inheriting constructor template. Deduce
  1476. the correct exception-specification and deletedness for this particular
  1477. specialization. */
  1478. void
  1479. deduce_inheriting_ctor (tree decl)
  1480. {
  1481. gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
  1482. tree spec;
  1483. bool trivial, constexpr_, deleted;
  1484. synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
  1485. false, &spec, &trivial, &deleted, &constexpr_,
  1486. /*diag*/false,
  1487. DECL_INHERITED_CTOR_BASE (decl),
  1488. FUNCTION_FIRST_USER_PARMTYPE (decl));
  1489. DECL_DELETED_FN (decl) = deleted;
  1490. TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
  1491. }
  1492. /* Implicitly declare the special function indicated by KIND, as a
  1493. member of TYPE. For copy constructors and assignment operators,
  1494. CONST_P indicates whether these functions should take a const
  1495. reference argument or a non-const reference. Returns the
  1496. FUNCTION_DECL for the implicitly declared function. */
  1497. tree
  1498. implicitly_declare_fn (special_function_kind kind, tree type,
  1499. bool const_p, tree inherited_ctor,
  1500. tree inherited_parms)
  1501. {
  1502. tree fn;
  1503. tree parameter_types = void_list_node;
  1504. tree return_type;
  1505. tree fn_type;
  1506. tree raises = empty_except_spec;
  1507. tree rhs_parm_type = NULL_TREE;
  1508. tree this_parm;
  1509. tree name;
  1510. HOST_WIDE_INT saved_processing_template_decl;
  1511. bool deleted_p;
  1512. bool constexpr_p;
  1513. /* Because we create declarations for implicitly declared functions
  1514. lazily, we may be creating the declaration for a member of TYPE
  1515. while in some completely different context. However, TYPE will
  1516. never be a dependent class (because we never want to do lookups
  1517. for implicitly defined functions in a dependent class).
  1518. Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
  1519. because we only create clones for constructors and destructors
  1520. when not in a template. */
  1521. gcc_assert (!dependent_type_p (type));
  1522. saved_processing_template_decl = processing_template_decl;
  1523. processing_template_decl = 0;
  1524. type = TYPE_MAIN_VARIANT (type);
  1525. if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
  1526. {
  1527. if (kind == sfk_destructor)
  1528. /* See comment in check_special_function_return_type. */
  1529. return_type = build_pointer_type (void_type_node);
  1530. else
  1531. return_type = build_pointer_type (type);
  1532. }
  1533. else
  1534. return_type = void_type_node;
  1535. switch (kind)
  1536. {
  1537. case sfk_destructor:
  1538. /* Destructor. */
  1539. name = constructor_name (type);
  1540. break;
  1541. case sfk_constructor:
  1542. /* Default constructor. */
  1543. name = constructor_name (type);
  1544. break;
  1545. case sfk_copy_constructor:
  1546. case sfk_copy_assignment:
  1547. case sfk_move_constructor:
  1548. case sfk_move_assignment:
  1549. case sfk_inheriting_constructor:
  1550. {
  1551. bool move_p;
  1552. if (kind == sfk_copy_assignment
  1553. || kind == sfk_move_assignment)
  1554. {
  1555. return_type = build_reference_type (type);
  1556. name = ansi_assopname (NOP_EXPR);
  1557. }
  1558. else
  1559. name = constructor_name (type);
  1560. if (kind == sfk_inheriting_constructor)
  1561. parameter_types = inherited_parms;
  1562. else
  1563. {
  1564. if (const_p)
  1565. rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
  1566. else
  1567. rhs_parm_type = type;
  1568. move_p = (kind == sfk_move_assignment
  1569. || kind == sfk_move_constructor);
  1570. rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
  1571. parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
  1572. }
  1573. break;
  1574. }
  1575. default:
  1576. gcc_unreachable ();
  1577. }
  1578. tree inherited_base = (inherited_ctor
  1579. ? DECL_CONTEXT (inherited_ctor)
  1580. : NULL_TREE);
  1581. bool trivial_p = false;
  1582. if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
  1583. {
  1584. /* For an inheriting constructor template, just copy these flags from
  1585. the inherited constructor template for now. */
  1586. raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
  1587. deleted_p = DECL_DELETED_FN (inherited_ctor);
  1588. constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
  1589. }
  1590. else if (cxx_dialect >= cxx11)
  1591. {
  1592. raises = unevaluated_noexcept_spec ();
  1593. synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
  1594. &deleted_p, &constexpr_p, false,
  1595. inherited_base, inherited_parms);
  1596. }
  1597. else
  1598. synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
  1599. &deleted_p, &constexpr_p, false,
  1600. inherited_base, inherited_parms);
  1601. /* Don't bother marking a deleted constructor as constexpr. */
  1602. if (deleted_p)
  1603. constexpr_p = false;
  1604. /* A trivial copy/move constructor is also a constexpr constructor,
  1605. unless the class has virtual bases (7.1.5p4). */
  1606. else if (trivial_p && cxx_dialect >= cxx11
  1607. && (kind == sfk_copy_constructor
  1608. || kind == sfk_move_constructor)
  1609. && !CLASSTYPE_VBASECLASSES (type))
  1610. gcc_assert (constexpr_p);
  1611. if (!trivial_p && type_has_trivial_fn (type, kind))
  1612. type_set_nontrivial_flag (type, kind);
  1613. /* Create the function. */
  1614. fn_type = build_method_type_directly (type, return_type, parameter_types);
  1615. if (raises)
  1616. fn_type = build_exception_variant (fn_type, raises);
  1617. fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
  1618. if (kind != sfk_inheriting_constructor)
  1619. DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
  1620. if (kind == sfk_constructor || kind == sfk_copy_constructor
  1621. || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
  1622. DECL_CONSTRUCTOR_P (fn) = 1;
  1623. else if (kind == sfk_destructor)
  1624. DECL_DESTRUCTOR_P (fn) = 1;
  1625. else
  1626. {
  1627. DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
  1628. SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
  1629. }
  1630. /* If pointers to member functions use the least significant bit to
  1631. indicate whether a function is virtual, ensure a pointer
  1632. to this function will have that bit clear. */
  1633. if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
  1634. && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
  1635. DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
  1636. /* Create the explicit arguments. */
  1637. if (rhs_parm_type)
  1638. {
  1639. /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
  1640. want its type to be included in the mangled function
  1641. name. */
  1642. tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
  1643. TREE_READONLY (decl) = 1;
  1644. retrofit_lang_decl (decl);
  1645. DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
  1646. DECL_ARGUMENTS (fn) = decl;
  1647. }
  1648. else if (kind == sfk_inheriting_constructor)
  1649. {
  1650. tree *p = &DECL_ARGUMENTS (fn);
  1651. int index = 1;
  1652. for (tree parm = inherited_parms; parm != void_list_node;
  1653. parm = TREE_CHAIN (parm))
  1654. {
  1655. *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
  1656. retrofit_lang_decl (*p);
  1657. DECL_PARM_LEVEL (*p) = 1;
  1658. DECL_PARM_INDEX (*p) = index++;
  1659. DECL_CONTEXT (*p) = fn;
  1660. p = &DECL_CHAIN (*p);
  1661. }
  1662. SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
  1663. DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
  1664. /* A constructor so declared has the same access as the corresponding
  1665. constructor in X. */
  1666. TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
  1667. TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
  1668. /* Copy constexpr from the inherited constructor even if the
  1669. inheriting constructor doesn't satisfy the requirements. */
  1670. constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
  1671. }
  1672. /* Add the "this" parameter. */
  1673. this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
  1674. DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
  1675. DECL_ARGUMENTS (fn) = this_parm;
  1676. grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
  1677. DECL_IN_AGGR_P (fn) = 1;
  1678. DECL_ARTIFICIAL (fn) = 1;
  1679. DECL_DEFAULTED_FN (fn) = 1;
  1680. if (cxx_dialect >= cxx11)
  1681. {
  1682. /* "The closure type associated with a lambda-expression has a deleted
  1683. default constructor and a deleted copy assignment operator." */
  1684. if ((kind == sfk_constructor
  1685. || kind == sfk_copy_assignment)
  1686. && LAMBDA_TYPE_P (type))
  1687. deleted_p = true;
  1688. DECL_DELETED_FN (fn) = deleted_p;
  1689. DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
  1690. }
  1691. DECL_EXTERNAL (fn) = true;
  1692. DECL_NOT_REALLY_EXTERN (fn) = 1;
  1693. DECL_DECLARED_INLINE_P (fn) = 1;
  1694. DECL_COMDAT (fn) = 1;
  1695. set_linkage_according_to_type (type, fn);
  1696. rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
  1697. gcc_assert (!TREE_USED (fn));
  1698. /* Restore PROCESSING_TEMPLATE_DECL. */
  1699. processing_template_decl = saved_processing_template_decl;
  1700. if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
  1701. fn = add_inherited_template_parms (fn, inherited_ctor);
  1702. /* Warn about calling a non-trivial move assignment in a virtual base. */
  1703. if (kind == sfk_move_assignment && !deleted_p && !trivial_p
  1704. && CLASSTYPE_VBASECLASSES (type))
  1705. {
  1706. location_t loc = input_location;
  1707. input_location = DECL_SOURCE_LOCATION (fn);
  1708. synthesized_method_walk (type, kind, const_p,
  1709. NULL, NULL, NULL, NULL, true,
  1710. NULL_TREE, NULL_TREE);
  1711. input_location = loc;
  1712. }
  1713. return fn;
  1714. }
  1715. /* Gives any errors about defaulted functions which need to be deferred
  1716. until the containing class is complete. */
  1717. void
  1718. defaulted_late_check (tree fn)
  1719. {
  1720. /* Complain about invalid signature for defaulted fn. */
  1721. tree ctx = DECL_CONTEXT (fn);
  1722. special_function_kind kind = special_function_p (fn);
  1723. bool fn_const_p = (copy_fn_p (fn) == 2);
  1724. tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
  1725. NULL, NULL);
  1726. tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
  1727. if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
  1728. TREE_TYPE (TREE_TYPE (implicit_fn)))
  1729. || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
  1730. TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
  1731. {
  1732. error ("defaulted declaration %q+D", fn);
  1733. error_at (DECL_SOURCE_LOCATION (fn),
  1734. "does not match expected signature %qD", implicit_fn);
  1735. }
  1736. /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
  1737. exception-specification only if it is compatible (15.4) with the
  1738. exception-specification on the implicit declaration. If a function
  1739. is explicitly defaulted on its first declaration, (...) it is
  1740. implicitly considered to have the same exception-specification as if
  1741. it had been implicitly declared. */
  1742. maybe_instantiate_noexcept (fn);
  1743. tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
  1744. if (!fn_spec)
  1745. {
  1746. if (DECL_DEFAULTED_IN_CLASS_P (fn))
  1747. TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
  1748. }
  1749. else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
  1750. /* Equivalent to the implicit spec. */;
  1751. else if (DECL_DEFAULTED_IN_CLASS_P (fn)
  1752. && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
  1753. /* We can't compare an explicit exception-specification on a
  1754. constructor defaulted in the class body to the implicit
  1755. exception-specification until after we've parsed any NSDMI; see
  1756. after_nsdmi_defaulted_late_checks. */;
  1757. else
  1758. {
  1759. tree eh_spec = get_defaulted_eh_spec (fn);
  1760. if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
  1761. {
  1762. if (DECL_DEFAULTED_IN_CLASS_P (fn))
  1763. DECL_DELETED_FN (fn) = true;
  1764. else
  1765. error ("function %q+D defaulted on its redeclaration "
  1766. "with an exception-specification that differs from "
  1767. "the implicit exception-specification %qX", fn, eh_spec);
  1768. }
  1769. }
  1770. if (DECL_DEFAULTED_IN_CLASS_P (fn)
  1771. && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
  1772. {
  1773. /* Hmm...should we do this for out-of-class too? Should it be OK to
  1774. add constexpr later like inline, rather than requiring
  1775. declarations to match? */
  1776. DECL_DECLARED_CONSTEXPR_P (fn) = true;
  1777. if (kind == sfk_constructor)
  1778. TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
  1779. }
  1780. if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
  1781. && DECL_DECLARED_CONSTEXPR_P (fn))
  1782. {
  1783. if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
  1784. {
  1785. error ("explicitly defaulted function %q+D cannot be declared "
  1786. "as constexpr because the implicit declaration is not "
  1787. "constexpr:", fn);
  1788. explain_implicit_non_constexpr (fn);
  1789. }
  1790. DECL_DECLARED_CONSTEXPR_P (fn) = false;
  1791. }
  1792. if (DECL_DELETED_FN (implicit_fn))
  1793. DECL_DELETED_FN (fn) = 1;
  1794. }
  1795. /* OK, we've parsed the NSDMI for class T, now we can check any explicit
  1796. exception-specifications on functions defaulted in the class body. */
  1797. void
  1798. after_nsdmi_defaulted_late_checks (tree t)
  1799. {
  1800. if (uses_template_parms (t))
  1801. return;
  1802. if (t == error_mark_node)
  1803. return;
  1804. for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
  1805. if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
  1806. {
  1807. tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
  1808. if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
  1809. continue;
  1810. tree eh_spec = get_defaulted_eh_spec (fn);
  1811. if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
  1812. eh_spec, ce_normal))
  1813. DECL_DELETED_FN (fn) = true;
  1814. }
  1815. }
  1816. /* Returns true iff FN can be explicitly defaulted, and gives any
  1817. errors if defaulting FN is ill-formed. */
  1818. bool
  1819. defaultable_fn_check (tree fn)
  1820. {
  1821. special_function_kind kind = sfk_none;
  1822. if (template_parm_scope_p ())
  1823. {
  1824. error ("a template cannot be defaulted");
  1825. return false;
  1826. }
  1827. if (DECL_CONSTRUCTOR_P (fn))
  1828. {
  1829. if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
  1830. kind = sfk_constructor;
  1831. else if (copy_fn_p (fn) > 0
  1832. && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
  1833. == void_list_node))
  1834. kind = sfk_copy_constructor;
  1835. else if (move_fn_p (fn))
  1836. kind = sfk_move_constructor;
  1837. }
  1838. else if (DECL_DESTRUCTOR_P (fn))
  1839. kind = sfk_destructor;
  1840. else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
  1841. && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
  1842. {
  1843. if (copy_fn_p (fn))
  1844. kind = sfk_copy_assignment;
  1845. else if (move_fn_p (fn))
  1846. kind = sfk_move_assignment;
  1847. }
  1848. if (kind == sfk_none)
  1849. {
  1850. error ("%qD cannot be defaulted", fn);
  1851. return false;
  1852. }
  1853. else
  1854. {
  1855. for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
  1856. t && t != void_list_node; t = TREE_CHAIN (t))
  1857. if (TREE_PURPOSE (t))
  1858. {
  1859. error ("defaulted function %q+D with default argument", fn);
  1860. break;
  1861. }
  1862. /* Avoid do_warn_unused_parameter warnings. */
  1863. for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
  1864. if (DECL_NAME (p))
  1865. TREE_NO_WARNING (p) = 1;
  1866. if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
  1867. /* Defer checking. */;
  1868. else if (!processing_template_decl)
  1869. defaulted_late_check (fn);
  1870. return true;
  1871. }
  1872. }
  1873. /* Add an implicit declaration to TYPE for the kind of function
  1874. indicated by SFK. Return the FUNCTION_DECL for the new implicit
  1875. declaration. */
  1876. tree
  1877. lazily_declare_fn (special_function_kind sfk, tree type)
  1878. {
  1879. tree fn;
  1880. /* Whether or not the argument has a const reference type. */
  1881. bool const_p = false;
  1882. switch (sfk)
  1883. {
  1884. case sfk_constructor:
  1885. CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
  1886. break;
  1887. case sfk_copy_constructor:
  1888. const_p = TYPE_HAS_CONST_COPY_CTOR (type);
  1889. CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
  1890. break;
  1891. case sfk_move_constructor:
  1892. CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
  1893. break;
  1894. case sfk_copy_assignment:
  1895. const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
  1896. CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
  1897. break;
  1898. case sfk_move_assignment:
  1899. CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
  1900. break;
  1901. case sfk_destructor:
  1902. CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
  1903. break;
  1904. default:
  1905. gcc_unreachable ();
  1906. }
  1907. /* Declare the function. */
  1908. fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
  1909. /* [class.copy]/8 If the class definition declares a move constructor or
  1910. move assignment operator, the implicitly declared copy constructor is
  1911. defined as deleted.... */
  1912. if ((sfk == sfk_copy_assignment
  1913. || sfk == sfk_copy_constructor)
  1914. && (type_has_user_declared_move_constructor (type)
  1915. || type_has_user_declared_move_assign (type)))
  1916. DECL_DELETED_FN (fn) = true;
  1917. /* A destructor may be virtual. */
  1918. if (sfk == sfk_destructor
  1919. || sfk == sfk_move_assignment
  1920. || sfk == sfk_copy_assignment)
  1921. check_for_override (fn, type);
  1922. /* Add it to CLASSTYPE_METHOD_VEC. */
  1923. add_method (type, fn, NULL_TREE);
  1924. /* Add it to TYPE_METHODS. */
  1925. if (sfk == sfk_destructor
  1926. && DECL_VIRTUAL_P (fn))
  1927. /* The ABI requires that a virtual destructor go at the end of the
  1928. vtable. */
  1929. TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
  1930. else
  1931. {
  1932. DECL_CHAIN (fn) = TYPE_METHODS (type);
  1933. TYPE_METHODS (type) = fn;
  1934. }
  1935. maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
  1936. if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
  1937. || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
  1938. /* Create appropriate clones. */
  1939. clone_function_decl (fn, /*update_method_vec=*/true);
  1940. return fn;
  1941. }
  1942. /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
  1943. as there are artificial parms in FN. */
  1944. tree
  1945. skip_artificial_parms_for (const_tree fn, tree list)
  1946. {
  1947. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
  1948. list = TREE_CHAIN (list);
  1949. else
  1950. return list;
  1951. if (DECL_HAS_IN_CHARGE_PARM_P (fn))
  1952. list = TREE_CHAIN (list);
  1953. if (DECL_HAS_VTT_PARM_P (fn))
  1954. list = TREE_CHAIN (list);
  1955. return list;
  1956. }
  1957. /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
  1958. artificial parms in FN. */
  1959. int
  1960. num_artificial_parms_for (const_tree fn)
  1961. {
  1962. int count = 0;
  1963. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
  1964. count++;
  1965. else
  1966. return 0;
  1967. if (DECL_HAS_IN_CHARGE_PARM_P (fn))
  1968. count++;
  1969. if (DECL_HAS_VTT_PARM_P (fn))
  1970. count++;
  1971. return count;
  1972. }
  1973. #include "gt-cp-method.h"