PageRenderTime 68ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/gcc/d/decl.cc

http://github.com/D-Programming-GDC/GDC
C++ | 2397 lines | 1578 code | 450 blank | 369 comment | 422 complexity | 4b9537d1e8769b8c9e3d967c117d8408 MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /* decl.cc -- Lower D frontend declarations to GCC trees.
  2. Copyright (C) 2006-2018 Free Software Foundation, Inc.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GCC; see the file COPYING3. If not see
  13. <http://www.gnu.org/licenses/>. */
  14. #include "config.h"
  15. #include "system.h"
  16. #include "coretypes.h"
  17. #include "dmd/aggregate.h"
  18. #include "dmd/attrib.h"
  19. #include "dmd/ctfe.h"
  20. #include "dmd/declaration.h"
  21. #include "dmd/enum.h"
  22. #include "dmd/errors.h"
  23. #include "dmd/globals.h"
  24. #include "dmd/hdrgen.h"
  25. #include "dmd/identifier.h"
  26. #include "dmd/import.h"
  27. #include "dmd/init.h"
  28. #include "dmd/mangle.h"
  29. #include "dmd/module.h"
  30. #include "dmd/nspace.h"
  31. #include "dmd/target.h"
  32. #include "dmd/template.h"
  33. #include "tree.h"
  34. #include "tree-iterator.h"
  35. #include "fold-const.h"
  36. #include "diagnostic.h"
  37. #include "langhooks.h"
  38. #include "target.h"
  39. #include "common/common-target.h"
  40. #include "cgraph.h"
  41. #include "toplev.h"
  42. #include "stringpool.h"
  43. #include "varasm.h"
  44. #include "stor-layout.h"
  45. #include "attribs.h"
  46. #include "function.h"
  47. #include "debug.h"
  48. #include "tree-pretty-print.h"
  49. #include "d-tree.h"
  50. /* Return identifier for the external mangled name of DECL. */
  51. const char *
  52. mangle_decl (Dsymbol *decl)
  53. {
  54. if (decl->isFuncDeclaration ())
  55. return mangleExact ((FuncDeclaration *)decl);
  56. else
  57. {
  58. OutBuffer buf;
  59. mangleToBuffer (decl, &buf);
  60. return buf.extractString ();
  61. }
  62. }
  63. /* Generate a mangled identifier using NAME and SUFFIX, prefixed by the
  64. assembler name for DECL. */
  65. tree
  66. mangle_internal_decl (Dsymbol *decl, const char *name, const char *suffix)
  67. {
  68. const char *prefix = mangle_decl (decl);
  69. unsigned namelen = strlen (name);
  70. unsigned buflen = (2 + strlen (prefix) + namelen + strlen (suffix)) * 2;
  71. char *buf = (char *) alloca (buflen);
  72. snprintf (buf, buflen, "_D%s%u%s%s", prefix, namelen, name, suffix);
  73. tree ident = get_identifier (buf);
  74. /* Symbol is not found in user code, but generate a readable name for it
  75. anyway for debug and diagnostic reporting. */
  76. snprintf (buf, buflen, "%s.%s", decl->toPrettyChars (), name);
  77. IDENTIFIER_PRETTY_NAME (ident) = get_identifier (buf);
  78. return ident;
  79. }
  80. /* Returns true if DECL is from the gcc.attribute module. */
  81. static bool
  82. gcc_attribute_p (Dsymbol *decl)
  83. {
  84. ModuleDeclaration *md = decl->getModule ()->md;
  85. if (md && md->packages && md->packages->dim == 1)
  86. {
  87. if (!strcmp ((*md->packages)[0]->toChars (), "gcc")
  88. && !strcmp (md->id->toChars (), "attribute"))
  89. return true;
  90. }
  91. return false;
  92. }
  93. /* Subroutine of pragma declaration visitor for marking the function in the
  94. defined in SYM as a global constructor or destructor. If ISCTOR is true,
  95. then we're applying pragma(crt_constructor). */
  96. static int
  97. apply_pragma_crt (Dsymbol *sym, bool isctor)
  98. {
  99. AttribDeclaration *ad = sym->isAttribDeclaration ();
  100. if (ad != NULL)
  101. {
  102. int nested = 0;
  103. /* Walk all declarations of the attribute scope. */
  104. Dsymbols *ds = ad->include (NULL);
  105. if (ds)
  106. {
  107. for (size_t i = 0; i < ds->dim; i++)
  108. nested += apply_pragma_crt ((*ds)[i], isctor);
  109. }
  110. return nested;
  111. }
  112. FuncDeclaration *fd = sym->isFuncDeclaration ();
  113. if (fd != NULL)
  114. {
  115. tree decl = get_decl_tree (fd);
  116. /* Apply flags to the function. */
  117. if (isctor)
  118. {
  119. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  120. decl_init_priority_insert (decl, DEFAULT_INIT_PRIORITY);
  121. }
  122. else
  123. {
  124. DECL_STATIC_DESTRUCTOR (decl) = 1;
  125. decl_fini_priority_insert (decl, DEFAULT_INIT_PRIORITY);
  126. }
  127. if (fd->linkage != LINKc)
  128. {
  129. error_at (make_location_t (fd->loc),
  130. "must be %<extern(C)%> for %<pragma(%s)%>",
  131. isctor ? "crt_constructor" : "crt_destructor");
  132. }
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. /* Implements the visitor interface to lower all Declaration AST classes
  138. emitted from the D Front-end to GCC trees.
  139. All visit methods accept one parameter D, which holds the frontend AST
  140. of the declaration to compile. These also don't return any value, instead
  141. generated code are appened to global_declarations or added to the
  142. current_binding_level by d_pushdecl(). */
  143. class DeclVisitor : public Visitor
  144. {
  145. using Visitor::visit;
  146. public:
  147. DeclVisitor (void)
  148. {
  149. }
  150. /* This should be overridden by each declaration class. */
  151. void visit (Dsymbol *)
  152. {
  153. }
  154. /* Compile a D module, and all members of it. */
  155. void visit (Module *d)
  156. {
  157. if (d->semanticRun >= PASSobj)
  158. return;
  159. build_module_tree (d);
  160. d->semanticRun = PASSobj;
  161. }
  162. /* Write the imported symbol to debug. */
  163. void visit (Import *d)
  164. {
  165. /* Implements import declarations by telling the debug back-end we are
  166. importing the NAMESPACE_DECL of the module or IMPORTED_DECL of the
  167. declaration into the current lexical scope CONTEXT. NAME is set if
  168. this is a renamed import. */
  169. if (d->isstatic)
  170. return;
  171. /* Get the context of this import, this should never be null. */
  172. tree context = d_module_context ();
  173. if (d->ident == NULL)
  174. {
  175. /* Importing declaration list. */
  176. for (size_t i = 0; i < d->names.dim; i++)
  177. {
  178. AliasDeclaration *aliasdecl = d->aliasdecls[i];
  179. tree decl = build_import_decl (aliasdecl);
  180. /* Skip over unhandled imports. */
  181. if (decl == NULL_TREE)
  182. continue;
  183. Identifier *alias = d->aliases[i];
  184. tree name = (alias != NULL)
  185. ? get_identifier (alias->toChars ()) : NULL_TREE;
  186. debug_hooks->imported_module_or_decl (decl, name, context,
  187. false, false);
  188. }
  189. }
  190. else
  191. {
  192. /* Importing the entire module. */
  193. tree decl = build_import_decl (d->mod);
  194. tree name = (d->aliasId != NULL)
  195. ? get_identifier (d->aliasId->toChars ()) : NULL_TREE;
  196. debug_hooks->imported_module_or_decl (decl, name, context,
  197. false, false);
  198. }
  199. }
  200. /* Expand any local variables found in tuples. */
  201. void visit (TupleDeclaration *d)
  202. {
  203. for (size_t i = 0; i < d->objects->dim; i++)
  204. {
  205. RootObject *o = (*d->objects)[i];
  206. if ((o->dyncast () == DYNCAST_EXPRESSION)
  207. && ((Expression *) o)->op == TOKdsymbol)
  208. {
  209. Declaration *d = ((DsymbolExp *) o)->s->isDeclaration ();
  210. if (d)
  211. d->accept (this);
  212. }
  213. }
  214. }
  215. /* Walk over all declarations in the attribute scope. */
  216. void visit (AttribDeclaration *d)
  217. {
  218. Dsymbols *ds = d->include (NULL);
  219. if (!ds)
  220. return;
  221. for (size_t i = 0; i < ds->dim; i++)
  222. {
  223. Dsymbol *s = (*ds)[i];
  224. s->accept (this);
  225. }
  226. }
  227. /* Pragmas are a way to pass special information to the compiler and to add
  228. vendor specific extensions to D. */
  229. void visit (PragmaDeclaration *d)
  230. {
  231. if (!global.params.ignoreUnsupportedPragmas)
  232. {
  233. if (d->ident == Identifier::idPool ("lib")
  234. || d->ident == Identifier::idPool ("startaddress"))
  235. {
  236. warning_at (make_location_t (d->loc), OPT_Wunknown_pragmas,
  237. "pragma(%s) not implemented", d->ident->toChars ());
  238. }
  239. }
  240. visit ((AttribDeclaration *) d);
  241. /* Handle pragma(crt_constructor) and pragma(crt_destructor). Apply flag
  242. to indicate that the functions enclosed should run automatically at the
  243. beginning or end of execution. */
  244. if (d->ident == Identifier::idPool ("crt_constructor")
  245. || d->ident == Identifier::idPool ("crt_destructor"))
  246. {
  247. bool isctor = (d->ident == Identifier::idPool ("crt_constructor"));
  248. if (apply_pragma_crt (d, isctor) > 1)
  249. error_at (make_location_t (d->loc),
  250. "can only apply to a single declaration");
  251. }
  252. }
  253. /* Walk over all members in the namespace scope. */
  254. void visit (Nspace *d)
  255. {
  256. if (isError (d) || !d->members)
  257. return;
  258. for (size_t i = 0; i < d->members->dim; i++)
  259. {
  260. Dsymbol *s = (*d->members)[i];
  261. s->accept (this);
  262. }
  263. }
  264. /* Walk over all members in the instantiated template. */
  265. void visit (TemplateInstance *d)
  266. {
  267. if (isError (d)|| !d->members)
  268. return;
  269. if (!d->needsCodegen ())
  270. return;
  271. for (size_t i = 0; i < d->members->dim; i++)
  272. {
  273. Dsymbol *s = (*d->members)[i];
  274. s->accept (this);
  275. }
  276. }
  277. /* Walk over all members in the mixin template scope. */
  278. void visit (TemplateMixin *d)
  279. {
  280. if (isError (d)|| !d->members)
  281. return;
  282. for (size_t i = 0; i < d->members->dim; i++)
  283. {
  284. Dsymbol *s = (*d->members)[i];
  285. s->accept (this);
  286. }
  287. }
  288. /* Write out compiler generated TypeInfo, initializer and functions for the
  289. given struct declaration, walking over all static members. */
  290. void visit (StructDeclaration *d)
  291. {
  292. if (d->type->ty == Terror)
  293. {
  294. error_at (make_location_t (d->loc),
  295. "had semantic errors when compiling");
  296. return;
  297. }
  298. /* Add this decl to the current binding level. */
  299. tree ctype = build_ctype (d->type);
  300. if (TYPE_NAME (ctype))
  301. d_pushdecl (TYPE_NAME (ctype));
  302. /* Anonymous structs/unions only exist as part of others,
  303. do not output forward referenced structs. */
  304. if (d->isAnonymous () || !d->members)
  305. return;
  306. /* Don't emit any symbols from gcc.attribute module. */
  307. if (gcc_attribute_p (d))
  308. return;
  309. /* Generate TypeInfo. */
  310. if (have_typeinfo_p (Type::dtypeinfo))
  311. create_typeinfo (d->type, NULL);
  312. /* Generate static initializer. */
  313. d->sinit = aggregate_initializer_decl (d);
  314. DECL_INITIAL (d->sinit) = layout_struct_initializer (d);
  315. if (d->isInstantiated ())
  316. d_linkonce_linkage (d->sinit);
  317. d_finish_decl (d->sinit);
  318. /* Put out the members. */
  319. for (size_t i = 0; i < d->members->dim; i++)
  320. {
  321. Dsymbol *member = (*d->members)[i];
  322. /* There might be static ctors in the members, and they cannot
  323. be put in separate object files. */
  324. member->accept (this);
  325. }
  326. /* Put out xopEquals, xopCmp and xopHash. */
  327. if (d->xeq && d->xeq != d->xerreq)
  328. d->xeq->accept (this);
  329. if (d->xcmp && d->xcmp != d->xerrcmp)
  330. d->xcmp->accept (this);
  331. if (d->xhash)
  332. d->xhash->accept (this);
  333. }
  334. /* Finish semantic analysis of functions in vtbl for class CD. */
  335. bool finish_vtable (ClassDeclaration *d)
  336. {
  337. bool has_errors = false;
  338. /* Finish semantic analysis of functions in vtbl[]. */
  339. for (size_t i = d->vtblOffset (); i < d->vtbl.dim; i++)
  340. {
  341. FuncDeclaration *fd = d->vtbl[i]->isFuncDeclaration ();
  342. if (!fd || (!fd->fbody && d->isAbstract ()))
  343. continue;
  344. /* Ensure function has a return value. */
  345. if (!fd->functionSemantic ())
  346. has_errors = true;
  347. /* No name hiding to check for. */
  348. if (!d->isFuncHidden (fd) || fd->isFuture ())
  349. continue;
  350. /* The function fd is hidden from the view of the class.
  351. If it overlaps with any function in the vtbl[], then
  352. issue an error. */
  353. for (size_t j = 1; j < d->vtbl.dim; j++)
  354. {
  355. if (j == i)
  356. continue;
  357. FuncDeclaration *fd2 = d->vtbl[j]->isFuncDeclaration ();
  358. if (!fd2->ident->equals (fd->ident))
  359. continue;
  360. /* The function is marked as @__future, a deprecation has
  361. already been given by the frontend. */
  362. if (fd2->isFuture ())
  363. continue;
  364. if (fd->leastAsSpecialized (fd2) || fd2->leastAsSpecialized (fd))
  365. {
  366. TypeFunction *tf = (TypeFunction *) fd->type;
  367. if (tf->ty == Tfunction)
  368. {
  369. error_at (make_location_t (fd->loc), "use of %qs",
  370. fd->toPrettyChars ());
  371. inform (make_location_t (fd2->loc), "is hidden by %qs",
  372. fd2->toPrettyChars ());
  373. inform (make_location_t (d->loc),
  374. "use %<alias %s = %s.%s;%> to introduce base class "
  375. "overload set.", fd->toChars (),
  376. fd->parent->toChars (), fd->toChars ());
  377. }
  378. else
  379. {
  380. error_at (make_location_t (fd->loc), "use of %qs",
  381. fd->toPrettyChars ());
  382. inform (make_location_t (fd2->loc), "is hidden by %qs",
  383. fd2->toPrettyChars ());
  384. }
  385. has_errors = true;
  386. break;
  387. }
  388. }
  389. }
  390. return !has_errors;
  391. }
  392. /* Write out compiler generated TypeInfo, initializer and vtables for the
  393. given class declaration, walking over all static members. */
  394. void visit (ClassDeclaration *d)
  395. {
  396. if (d->type->ty == Terror)
  397. {
  398. error_at (make_location_t (d->loc),
  399. "had semantic errors when compiling");
  400. return;
  401. }
  402. if (!d->members)
  403. return;
  404. /* Put out the members. */
  405. for (size_t i = 0; i < d->members->dim; i++)
  406. {
  407. Dsymbol *member = (*d->members)[i];
  408. member->accept (this);
  409. }
  410. /* If something goes wrong during final semantic pass, don't bother with
  411. the rest as we may have incomplete info. */
  412. if (!this->finish_vtable (d))
  413. return;
  414. /* Generate C symbols. */
  415. d->csym = get_classinfo_decl (d);
  416. Dsymbol *vtblsym = d->vtblSymbol ();
  417. vtblsym->csym = get_vtable_decl (d);
  418. d->sinit = aggregate_initializer_decl (d);
  419. /* Generate static initializer. */
  420. DECL_INITIAL (d->sinit) = layout_class_initializer (d);
  421. d_linkonce_linkage (d->sinit);
  422. d_finish_decl (d->sinit);
  423. /* Put out the TypeInfo. */
  424. if (have_typeinfo_p (Type::dtypeinfo))
  425. create_typeinfo (d->type, NULL);
  426. DECL_INITIAL (d->csym) = layout_classinfo (d);
  427. d_linkonce_linkage (d->csym);
  428. d_finish_decl (d->csym);
  429. /* Put out the vtbl[]. */
  430. vec<constructor_elt, va_gc> *elms = NULL;
  431. /* First entry is ClassInfo reference. */
  432. if (d->vtblOffset ())
  433. CONSTRUCTOR_APPEND_ELT (elms, size_zero_node, build_address (d->csym));
  434. for (size_t i = d->vtblOffset (); i < d->vtbl.dim; i++)
  435. {
  436. FuncDeclaration *fd = d->vtbl[i]->isFuncDeclaration ();
  437. if (fd && (fd->fbody || !d->isAbstract ()))
  438. {
  439. CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
  440. build_address (get_symbol_decl (fd)));
  441. }
  442. }
  443. DECL_INITIAL (vtblsym->csym)
  444. = build_constructor (TREE_TYPE (vtblsym->csym), elms);
  445. d_comdat_linkage (vtblsym->csym);
  446. d_finish_decl (vtblsym->csym);
  447. /* Add this decl to the current binding level. */
  448. tree ctype = TREE_TYPE (build_ctype (d->type));
  449. if (TYPE_NAME (ctype))
  450. d_pushdecl (TYPE_NAME (ctype));
  451. }
  452. /* Write out compiler generated TypeInfo and vtables for the given interface
  453. declaration, walking over all static members. */
  454. void visit (InterfaceDeclaration *d)
  455. {
  456. if (d->type->ty == Terror)
  457. {
  458. error_at (make_location_t (d->loc),
  459. "had semantic errors when compiling");
  460. return;
  461. }
  462. if (!d->members)
  463. return;
  464. /* Put out the members. */
  465. for (size_t i = 0; i < d->members->dim; i++)
  466. {
  467. Dsymbol *member = (*d->members)[i];
  468. member->accept (this);
  469. }
  470. /* Generate C symbols. */
  471. d->csym = get_classinfo_decl (d);
  472. /* Put out the TypeInfo. */
  473. if (have_typeinfo_p (Type::dtypeinfo))
  474. {
  475. create_typeinfo (d->type, NULL);
  476. d->type->vtinfo->accept (this);
  477. }
  478. DECL_INITIAL (d->csym) = layout_classinfo (d);
  479. d_linkonce_linkage (d->csym);
  480. d_finish_decl (d->csym);
  481. /* Add this decl to the current binding level. */
  482. tree ctype = TREE_TYPE (build_ctype (d->type));
  483. if (TYPE_NAME (ctype))
  484. d_pushdecl (TYPE_NAME (ctype));
  485. }
  486. /* Write out compiler generated TypeInfo and initializer for the given
  487. enum declaration. */
  488. void visit (EnumDeclaration *d)
  489. {
  490. if (d->semanticRun >= PASSobj)
  491. return;
  492. if (d->errors || d->type->ty == Terror)
  493. {
  494. error_at (make_location_t (d->loc),
  495. "had semantic errors when compiling");
  496. return;
  497. }
  498. if (d->isAnonymous ())
  499. return;
  500. /* Generate TypeInfo. */
  501. if (have_typeinfo_p (Type::dtypeinfo))
  502. create_typeinfo (d->type, NULL);
  503. TypeEnum *tc = (TypeEnum *) d->type;
  504. if (tc->sym->members && !d->type->isZeroInit ())
  505. {
  506. /* Generate static initializer. */
  507. d->sinit = enum_initializer_decl (d);
  508. DECL_INITIAL (d->sinit) = build_expr (tc->sym->defaultval, true);
  509. if (d->isInstantiated ())
  510. d_linkonce_linkage (d->sinit);
  511. d_finish_decl (d->sinit);
  512. /* Add this decl to the current binding level. */
  513. tree ctype = build_ctype (d->type);
  514. if (TREE_CODE (ctype) == ENUMERAL_TYPE && TYPE_NAME (ctype))
  515. d_pushdecl (TYPE_NAME (ctype));
  516. }
  517. d->semanticRun = PASSobj;
  518. }
  519. /* Finish up a variable declaration and push it into the current scope.
  520. This can either be a static, local or manifest constant. */
  521. void visit (VarDeclaration *d)
  522. {
  523. if (d->type->ty == Terror)
  524. {
  525. error_at (make_location_t (d->loc),
  526. "had semantic errors when compiling");
  527. return;
  528. }
  529. if (d->aliassym)
  530. {
  531. d->toAlias ()->accept (this);
  532. return;
  533. }
  534. /* Do not store variables we cannot take the address of,
  535. but keep the values for purposes of debugging. */
  536. if (!d->canTakeAddressOf ())
  537. {
  538. /* Don't know if there is a good way to handle instantiations. */
  539. if (d->isInstantiated ())
  540. return;
  541. tree decl = get_symbol_decl (d);
  542. gcc_assert (d->_init && !d->_init->isVoidInitializer ());
  543. Expression *ie = initializerToExpression (d->_init);
  544. /* CONST_DECL was initially intended for enumerals and may be used for
  545. scalars in general, but not for aggregates. Here a non-constant
  546. value is generated anyway so as the CONST_DECL only serves as a
  547. placeholder for the value, however the DECL itself should never be
  548. referenced in any generated code, or passed to the back-end. */
  549. if (!d->type->isscalar ())
  550. DECL_INITIAL (decl) = build_expr (ie, false);
  551. else
  552. {
  553. DECL_INITIAL (decl) = build_expr (ie, true);
  554. d_pushdecl (decl);
  555. rest_of_decl_compilation (decl, 1, 0);
  556. }
  557. }
  558. else if (d->isDataseg () && !(d->storage_class & STCextern))
  559. {
  560. tree decl = get_symbol_decl (d);
  561. /* Duplicated VarDeclarations map to the same symbol. Check if this
  562. is the one declaration which will be emitted. */
  563. tree ident = DECL_ASSEMBLER_NAME (decl);
  564. if (IDENTIFIER_DSYMBOL (ident) && IDENTIFIER_DSYMBOL (ident) != d)
  565. return;
  566. /* How big a symbol can be should depend on back-end. */
  567. tree size = build_integer_cst (d->type->size (d->loc),
  568. build_ctype (Type::tsize_t));
  569. if (!valid_constant_size_p (size))
  570. {
  571. error_at (make_location_t (d->loc), "size is too large");
  572. return;
  573. }
  574. if (d->_init && !d->_init->isVoidInitializer ())
  575. {
  576. Expression *e = initializerToExpression (d->_init, d->type);
  577. DECL_INITIAL (decl) = build_expr (e, true);
  578. }
  579. else
  580. {
  581. if (d->type->ty == Tstruct)
  582. {
  583. StructDeclaration *sd = ((TypeStruct *) d->type)->sym;
  584. DECL_INITIAL (decl) = layout_struct_initializer (sd);
  585. }
  586. else
  587. {
  588. Expression *e = d->type->defaultInitLiteral (d->loc);
  589. DECL_INITIAL (decl) = build_expr (e, true);
  590. }
  591. }
  592. /* Frontend should have already caught this. */
  593. gcc_assert (!integer_zerop (size)
  594. || d->type->toBasetype ()->ty == Tsarray);
  595. d_finish_decl (decl);
  596. /* Maybe record the var against the current module. */
  597. register_module_decl (d);
  598. }
  599. else if (!d->isDataseg () && !d->isMember ())
  600. {
  601. /* This is needed for VarDeclarations in mixins that are to be local
  602. variables of a function. Otherwise, it would be enough to make
  603. a check for isVarDeclaration() in DeclarationExp codegen. */
  604. declare_local_var (d);
  605. if (d->_init)
  606. {
  607. tree decl = get_symbol_decl (d);
  608. if (!d->_init->isVoidInitializer ())
  609. {
  610. ExpInitializer *vinit = d->_init->isExpInitializer ();
  611. Expression *ie = initializerToExpression (vinit);
  612. tree exp = build_expr (ie);
  613. /* Maybe put variable on list of things needing destruction. */
  614. if (d->needsScopeDtor ())
  615. {
  616. vec_safe_push (d_function_chain->vars_in_scope, decl);
  617. /* Force a TARGET_EXPR to add the corresponding cleanup. */
  618. exp = force_target_expr (compound_expr (exp, decl));
  619. TARGET_EXPR_CLEANUP (exp) = build_expr (d->edtor);
  620. }
  621. add_stmt (exp);
  622. }
  623. else if (d->size (d->loc) != 0)
  624. {
  625. /* Zero-length arrays do not have an initializer. */
  626. warning (OPT_Wuninitialized, "uninitialized variable '%s'",
  627. d->ident ? d->ident->toChars () : "(no name)");
  628. }
  629. }
  630. }
  631. }
  632. /* Generate and compile a static TypeInfo declaration, but only if it is
  633. needed in the current compilation. */
  634. void visit (TypeInfoDeclaration *d)
  635. {
  636. if (speculative_type_p (d->tinfo))
  637. return;
  638. tree t = get_typeinfo_decl (d);
  639. DECL_INITIAL (t) = layout_typeinfo (d);
  640. d_finish_decl (t);
  641. }
  642. /* Finish up a function declaration and compile it all the way
  643. down to assembler language output. */
  644. void visit (FuncDeclaration *d)
  645. {
  646. /* Already generated the function. */
  647. if (d->semanticRun >= PASSobj)
  648. return;
  649. /* Don't emit any symbols from gcc.attribute module. */
  650. if (gcc_attribute_p (d))
  651. return;
  652. /* Not emitting unittest functions. */
  653. if (!global.params.useUnitTests && d->isUnitTestDeclaration ())
  654. return;
  655. /* Check if any errors occurred when running semantic. */
  656. if (d->type->ty == Tfunction)
  657. {
  658. TypeFunction *tf = (TypeFunction *) d->type;
  659. if (tf->next == NULL || tf->next->ty == Terror)
  660. return;
  661. }
  662. if (d->semantic3Errors)
  663. return;
  664. if (d->isNested ())
  665. {
  666. FuncDeclaration *fdp = d;
  667. while (fdp && fdp->isNested ())
  668. {
  669. fdp = fdp->toParent2 ()->isFuncDeclaration ();
  670. if (fdp == NULL)
  671. break;
  672. /* Parent failed to compile, but errors were gagged. */
  673. if (fdp->semantic3Errors)
  674. return;
  675. }
  676. }
  677. /* Ensure all semantic passes have run. */
  678. if (d->semanticRun < PASSsemantic3)
  679. {
  680. d->functionSemantic3 ();
  681. Module::runDeferredSemantic3 ();
  682. }
  683. if (global.errors)
  684. return;
  685. /* Duplicated FuncDeclarations map to the same symbol. Check if this
  686. is the one declaration which will be emitted. */
  687. tree fndecl = get_symbol_decl (d);
  688. tree ident = DECL_ASSEMBLER_NAME (fndecl);
  689. if (IDENTIFIER_DSYMBOL (ident) && IDENTIFIER_DSYMBOL (ident) != d)
  690. return;
  691. if (!d->fbody)
  692. {
  693. rest_of_decl_compilation (fndecl, 1, 0);
  694. return;
  695. }
  696. if (global.params.verbose)
  697. message ("function %s", d->toPrettyChars ());
  698. /* Start generating code for this function. */
  699. gcc_assert (d->semanticRun == PASSsemantic3done);
  700. d->semanticRun = PASSobj;
  701. tree old_context = start_function (d);
  702. tree parm_decl = NULL_TREE;
  703. tree param_list = NULL_TREE;
  704. /* Special arguments... */
  705. /* 'this' parameter:
  706. For nested functions, D still generates a vthis, but it
  707. should not be referenced in any expression. */
  708. if (d->vthis)
  709. {
  710. parm_decl = get_symbol_decl (d->vthis);
  711. DECL_ARTIFICIAL (parm_decl) = 1;
  712. TREE_READONLY (parm_decl) = 1;
  713. if (d->vthis->type == Type::tvoidptr)
  714. {
  715. /* Replace generic pointer with back-end closure type
  716. (this wins for gdb). */
  717. tree frame_type = FRAMEINFO_TYPE (get_frameinfo (d));
  718. gcc_assert (frame_type != NULL_TREE);
  719. TREE_TYPE (parm_decl) = build_pointer_type (frame_type);
  720. }
  721. param_list = chainon (param_list, parm_decl);
  722. d_function_chain->static_chain = parm_decl;
  723. }
  724. /* _arguments parameter. */
  725. if (d->v_arguments)
  726. {
  727. parm_decl = get_symbol_decl (d->v_arguments);
  728. param_list = chainon (param_list, parm_decl);
  729. }
  730. /* formal function parameters. */
  731. size_t n_parameters = d->parameters ? d->parameters->dim : 0;
  732. for (size_t i = 0; i < n_parameters; i++)
  733. {
  734. VarDeclaration *param = (*d->parameters)[i];
  735. parm_decl = get_symbol_decl (param);
  736. /* Chain them in the correct order. */
  737. param_list = chainon (param_list, parm_decl);
  738. }
  739. DECL_ARGUMENTS (fndecl) = param_list;
  740. rest_of_decl_compilation (fndecl, 1, 0);
  741. /* If this is a member function that nested (possibly indirectly) in another
  742. function, construct an expession for this member function's static chain
  743. by going through parent link of nested classes. */
  744. if (d->isThis ())
  745. {
  746. AggregateDeclaration *ad = d->isThis ();
  747. tree this_tree = get_symbol_decl (d->vthis);
  748. while (ad->isNested ())
  749. {
  750. Dsymbol *pd = ad->toParent2 ();
  751. tree vthis_field = get_symbol_decl (ad->vthis);
  752. this_tree = component_ref (build_deref (this_tree), vthis_field);
  753. ad = pd->isAggregateDeclaration ();
  754. if (ad == NULL)
  755. {
  756. cfun->language->static_chain = this_tree;
  757. break;
  758. }
  759. }
  760. }
  761. /* May change cfun->static_chain. */
  762. build_closure (d);
  763. if (d->vresult)
  764. declare_local_var (d->vresult);
  765. if (d->v_argptr)
  766. push_stmt_list ();
  767. /* Named return value optimisation support for D.
  768. Implemented by overriding all the RETURN_EXPRs and replacing all
  769. occurrences of VAR with the RESULT_DECL for the function.
  770. This is only worth doing for functions that can return in memory. */
  771. if (d->nrvo_can)
  772. {
  773. tree restype = TREE_TYPE (DECL_RESULT (fndecl));
  774. if (!AGGREGATE_TYPE_P (restype))
  775. d->nrvo_can = 0;
  776. else
  777. d->nrvo_can = aggregate_value_p (restype, fndecl);
  778. }
  779. if (d->nrvo_can)
  780. {
  781. tree resdecl = DECL_RESULT (fndecl);
  782. TREE_TYPE (resdecl)
  783. = build_reference_type (TREE_TYPE (resdecl));
  784. DECL_BY_REFERENCE (resdecl) = 1;
  785. TREE_ADDRESSABLE (resdecl) = 0;
  786. relayout_decl (resdecl);
  787. if (d->nrvo_var)
  788. {
  789. tree var = get_symbol_decl (d->nrvo_var);
  790. /* Copy name from VAR to RESULT. */
  791. DECL_NAME (resdecl) = DECL_NAME (var);
  792. /* Don't forget that we take its address. */
  793. TREE_ADDRESSABLE (var) = 1;
  794. resdecl = build_deref (resdecl);
  795. SET_DECL_VALUE_EXPR (var, resdecl);
  796. DECL_HAS_VALUE_EXPR_P (var) = 1;
  797. SET_DECL_LANG_NRVO (var, resdecl);
  798. }
  799. }
  800. build_function_body (d);
  801. /* Initialize the _argptr variable. */
  802. if (d->v_argptr)
  803. {
  804. tree body = pop_stmt_list ();
  805. tree var = get_decl_tree (d->v_argptr);
  806. var = build_address (var);
  807. tree init = build_call_expr (builtin_decl_explicit (BUILT_IN_VA_START),
  808. 2, var, parm_decl);
  809. declare_local_var (d->v_argptr);
  810. add_stmt (init);
  811. tree cleanup = build_call_expr (builtin_decl_explicit (BUILT_IN_VA_END),
  812. 1, var);
  813. add_stmt (build2 (TRY_FINALLY_EXPR, void_type_node, body, cleanup));
  814. }
  815. finish_function (old_context);
  816. /* Maybe record the function against the current module. */
  817. register_module_decl (d);
  818. }
  819. };
  820. /* Main entry point for the DeclVisitor interface to send
  821. the Declaration AST class D to GCC back-end. */
  822. void
  823. build_decl_tree (Dsymbol *d)
  824. {
  825. location_t saved_location = input_location;
  826. /* Set input location, empty DECL_SOURCE_FILE can crash debug generator. */
  827. if (d->loc.filename)
  828. input_location = make_location_t (d->loc);
  829. else
  830. input_location = make_location_t (Loc ("<no_file>", 1, 0));
  831. DeclVisitor v = DeclVisitor ();
  832. d->accept (&v);
  833. input_location = saved_location;
  834. }
  835. /* Return the decl for the symbol, create it if it doesn't already exist. */
  836. tree
  837. get_symbol_decl (Declaration *decl)
  838. {
  839. if (decl->csym)
  840. return decl->csym;
  841. /* Deal with placeholder symbols immediately:
  842. SymbolDeclaration is used as a shell around an initializer symbol. */
  843. SymbolDeclaration *sd = decl->isSymbolDeclaration ();
  844. if (sd)
  845. {
  846. decl->csym = aggregate_initializer_decl (sd->dsym);
  847. return decl->csym;
  848. }
  849. /* Global static TypeInfo declaration. */
  850. if (decl->isTypeInfoDeclaration ())
  851. return get_typeinfo_decl ((TypeInfoDeclaration *) decl);
  852. /* FuncAliasDeclaration is used to import functions from another scope. */
  853. FuncAliasDeclaration *fad = decl->isFuncAliasDeclaration ();
  854. if (fad)
  855. {
  856. decl->csym = get_symbol_decl (fad->funcalias);
  857. return decl->csym;
  858. }
  859. /* It is possible for a field declaration symbol to be requested
  860. before the parent type has been built. */
  861. if (decl->isField ())
  862. {
  863. AggregateDeclaration *ad = decl->toParent ()->isAggregateDeclaration ();
  864. gcc_assert (ad != NULL);
  865. /* Finishing off the type should create the associated FIELD_DECL. */
  866. build_ctype (ad->type);
  867. gcc_assert (decl->csym != NULL);
  868. return decl->csym;
  869. }
  870. /* Build the tree for the symbol. */
  871. FuncDeclaration *fd = decl->isFuncDeclaration ();
  872. if (fd)
  873. {
  874. /* Run full semantic on functions we need to know about. */
  875. if (!fd->functionSemantic ())
  876. {
  877. decl->csym = error_mark_node;
  878. return decl->csym;
  879. }
  880. decl->csym = build_decl (make_location_t (decl->loc), FUNCTION_DECL,
  881. get_identifier (decl->ident->toChars ()),
  882. NULL_TREE);
  883. /* Set function type afterwards as there could be self references. */
  884. TREE_TYPE (decl->csym) = build_ctype (fd->type);
  885. if (!fd->fbody)
  886. DECL_EXTERNAL (decl->csym) = 1;
  887. }
  888. else
  889. {
  890. /* Build the variable declaration. */
  891. VarDeclaration *vd = decl->isVarDeclaration ();
  892. gcc_assert (vd != NULL);
  893. tree_code code = vd->isParameter () ? PARM_DECL
  894. : !vd->canTakeAddressOf () ? CONST_DECL
  895. : VAR_DECL;
  896. decl->csym = build_decl (make_location_t (decl->loc), code,
  897. get_identifier (decl->ident->toChars ()),
  898. declaration_type (vd));
  899. /* If any alignment was set on the declaration. */
  900. if (vd->alignment != STRUCTALIGN_DEFAULT)
  901. {
  902. SET_DECL_ALIGN (decl->csym, vd->alignment * BITS_PER_UNIT);
  903. DECL_USER_ALIGN (decl->csym) = 1;
  904. }
  905. if (vd->storage_class & STCextern)
  906. DECL_EXTERNAL (decl->csym) = 1;
  907. }
  908. /* Set the declaration mangled identifier if static. */
  909. if (decl->isCodeseg () || decl->isDataseg ())
  910. {
  911. tree mangled_name;
  912. if (decl->mangleOverride.length)
  913. {
  914. mangled_name
  915. = get_identifier_with_length (decl->mangleOverride.ptr,
  916. decl->mangleOverride.length);
  917. }
  918. else
  919. mangled_name = get_identifier (mangle_decl (decl));
  920. mangled_name = targetm.mangle_decl_assembler_name (decl->csym,
  921. mangled_name);
  922. /* The frontend doesn't handle duplicate definitions of unused symbols
  923. with the same mangle. So a check is done here instead. */
  924. if (!DECL_EXTERNAL (decl->csym))
  925. {
  926. if (IDENTIFIER_DSYMBOL (mangled_name))
  927. {
  928. Declaration *other = IDENTIFIER_DSYMBOL (mangled_name);
  929. /* Non-templated variables shouldn't be defined twice. */
  930. if (!decl->isInstantiated ())
  931. ScopeDsymbol::multiplyDefined (decl->loc, decl, other);
  932. decl->csym = get_symbol_decl (other);
  933. return decl->csym;
  934. }
  935. IDENTIFIER_PRETTY_NAME (mangled_name)
  936. = get_identifier (decl->toPrettyChars (true));
  937. IDENTIFIER_DSYMBOL (mangled_name) = decl;
  938. }
  939. SET_DECL_ASSEMBLER_NAME (decl->csym, mangled_name);
  940. }
  941. DECL_LANG_SPECIFIC (decl->csym) = build_lang_decl (decl);
  942. DECL_CONTEXT (decl->csym) = d_decl_context (decl);
  943. if (TREE_CODE (decl->csym) == PARM_DECL)
  944. {
  945. /* Pass non-trivial structs by invisible reference. */
  946. if (TREE_ADDRESSABLE (TREE_TYPE (decl->csym)))
  947. {
  948. tree argtype = build_reference_type (TREE_TYPE (decl->csym));
  949. argtype = build_qualified_type (argtype, TYPE_QUAL_RESTRICT);
  950. gcc_assert (!DECL_BY_REFERENCE (decl->csym));
  951. TREE_TYPE (decl->csym) = argtype;
  952. DECL_BY_REFERENCE (decl->csym) = 1;
  953. TREE_ADDRESSABLE (decl->csym) = 0;
  954. relayout_decl (decl->csym);
  955. decl->storage_class |= STCref;
  956. }
  957. DECL_ARG_TYPE (decl->csym) = TREE_TYPE (decl->csym);
  958. gcc_assert (TREE_CODE (DECL_CONTEXT (decl->csym)) == FUNCTION_DECL);
  959. }
  960. else if (TREE_CODE (decl->csym) == CONST_DECL)
  961. {
  962. /* Manifest constants have no address in memory. */
  963. TREE_CONSTANT (decl->csym) = 1;
  964. TREE_READONLY (decl->csym) = 1;
  965. }
  966. else if (TREE_CODE (decl->csym) == FUNCTION_DECL)
  967. {
  968. /* The real function type may differ from its declaration. */
  969. tree fntype = TREE_TYPE (decl->csym);
  970. tree newfntype = NULL_TREE;
  971. if (fd->isNested ())
  972. {
  973. /* Add an extra argument for the frame/closure pointer, this is also
  974. required to be compatible with D delegates. */
  975. newfntype = build_vthis_function (void_type_node, fntype);
  976. }
  977. else if (fd->isThis ())
  978. {
  979. /* Add an extra argument for the 'this' parameter. The handle type is
  980. used even if there is no debug info. It is needed to make sure
  981. virtual member functions are not called statically. */
  982. AggregateDeclaration *ad = fd->isMember2 ();
  983. tree handle = build_ctype (ad->handleType ());
  984. /* If handle is a pointer type, get record type. */
  985. if (!ad->isStructDeclaration ())
  986. handle = TREE_TYPE (handle);
  987. newfntype = build_vthis_function (handle, fntype);
  988. /* Set the vindex on virtual functions. */
  989. if (fd->isVirtual () && fd->vtblIndex != -1)
  990. {
  991. DECL_VINDEX (decl->csym) = size_int (fd->vtblIndex);
  992. DECL_VIRTUAL_P (decl->csym) = 1;
  993. }
  994. }
  995. else if (fd->isMain () || fd->isCMain ())
  996. {
  997. /* The main function is named 'D main' to distinguish from C main. */
  998. if (fd->isMain ())
  999. DECL_NAME (decl->csym) = get_identifier (fd->toPrettyChars (true));
  1000. /* 'void main' is implicitly converted to returning an int. */
  1001. newfntype = build_function_type (d_int_type, TYPE_ARG_TYPES (fntype));
  1002. }
  1003. if (newfntype != NULL_TREE)
  1004. {
  1005. /* Copy the old attributes from the original type. */
  1006. TYPE_ATTRIBUTES (newfntype) = TYPE_ATTRIBUTES (fntype);
  1007. TYPE_LANG_SPECIFIC (newfntype) = TYPE_LANG_SPECIFIC (fntype);
  1008. TREE_ADDRESSABLE (newfntype) = TREE_ADDRESSABLE (fntype);
  1009. TREE_TYPE (decl->csym) = newfntype;
  1010. d_keep (newfntype);
  1011. }
  1012. /* Miscellaneous function flags. */
  1013. if (fd->isMember2 () || fd->isFuncLiteralDeclaration ())
  1014. {
  1015. /* See grokmethod in cp/decl.c. Maybe we shouldn't be setting inline
  1016. flags without reason or proper handling. */
  1017. DECL_DECLARED_INLINE_P (decl->csym) = 1;
  1018. DECL_NO_INLINE_WARNING_P (decl->csym) = 1;
  1019. }
  1020. /* Function was declared 'naked'. */
  1021. if (fd->naked)
  1022. {
  1023. insert_decl_attribute (decl->csym, "naked");
  1024. DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl->csym) = 1;
  1025. }
  1026. /* Vector array operations are always compiler generated. */
  1027. if (fd->isArrayOp)
  1028. {
  1029. TREE_PUBLIC (decl->csym) = 1;
  1030. DECL_ARTIFICIAL (decl->csym) = 1;
  1031. DECL_DECLARED_INLINE_P (decl->csym) = 1;
  1032. d_comdat_linkage (decl->csym);
  1033. }
  1034. /* And so are ensure and require contracts. */
  1035. if (fd->ident == Identifier::idPool ("ensure")
  1036. || fd->ident == Identifier::idPool ("require"))
  1037. {
  1038. DECL_ARTIFICIAL (decl->csym) = 1;
  1039. TREE_PUBLIC (decl->csym) = 1;
  1040. }
  1041. if (decl->storage_class & STCfinal)
  1042. DECL_FINAL_P (decl->csym) = 1;
  1043. /* Check whether this function is expanded by the frontend. */
  1044. DECL_INTRINSIC_CODE (decl->csym) = INTRINSIC_NONE;
  1045. maybe_set_intrinsic (fd);
  1046. /* For nested functions in particular, unnest fndecl in the cgraph, as
  1047. all static chain passing is handled by the front-end. Do this even
  1048. if we are not emitting the body. */
  1049. struct cgraph_node *node = cgraph_node::get_create (decl->csym);
  1050. if (node->origin)
  1051. node->unnest ();
  1052. }
  1053. /* Mark compiler generated temporaries as artificial. */
  1054. if (decl->storage_class & STCtemp)
  1055. DECL_ARTIFICIAL (decl->csym) = 1;
  1056. /* Propagate shared on the decl. */
  1057. if (TYPE_SHARED (TREE_TYPE (decl->csym)))
  1058. TREE_ADDRESSABLE (decl->csym) = 1;
  1059. /* Symbol was marked volatile. */
  1060. if (decl->storage_class & STCvolatile)
  1061. TREE_THIS_VOLATILE (decl->csym) = 1;
  1062. /* Protection attributes are used by the debugger. */
  1063. if (decl->protection.kind == Prot::private_)
  1064. TREE_PRIVATE (decl->csym) = 1;
  1065. else if (decl->protection.kind == Prot::protected_)
  1066. TREE_PROTECTED (decl->csym) = 1;
  1067. /* Likewise, so could the deprecated attribute. */
  1068. if (decl->storage_class & STCdeprecated)
  1069. TREE_DEPRECATED (decl->csym) = 1;
  1070. #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
  1071. /* Have to test for import first. */
  1072. if (decl->isImportedSymbol ())
  1073. {
  1074. insert_decl_attribute (decl->csym, "dllimport");
  1075. DECL_DLLIMPORT_P (decl->csym) = 1;
  1076. }
  1077. else if (decl->isExport ())
  1078. insert_decl_attribute (decl->csym, "dllexport");
  1079. #endif
  1080. if (decl->isDataseg () || decl->isCodeseg () || decl->isThreadlocal ())
  1081. {
  1082. /* Set TREE_PUBLIC by default, but allow private template to override. */
  1083. if (!fd || !fd->isNested ())
  1084. TREE_PUBLIC (decl->csym) = 1;
  1085. TREE_STATIC (decl->csym) = 1;
  1086. /* The decl has not been defined -- yet. */
  1087. DECL_EXTERNAL (decl->csym) = 1;
  1088. if (decl->isInstantiated ())
  1089. d_linkonce_linkage (decl->csym);
  1090. }
  1091. /* Symbol is going in thread local storage. */
  1092. if (decl->isThreadlocal () && !DECL_ARTIFICIAL (decl->csym))
  1093. {
  1094. if (global.params.vtls)
  1095. message (decl->loc, "`%s` is thread local", decl->toChars ());
  1096. set_decl_tls_model (decl->csym, decl_default_tls_model (decl->csym));
  1097. }
  1098. /* Apply any user attributes that may affect semantic meaning. */
  1099. if (decl->userAttribDecl)
  1100. {
  1101. Expressions *attrs = decl->userAttribDecl->getAttributes ();
  1102. decl_attributes (&decl->csym, build_attributes (attrs), 0);
  1103. }
  1104. else if (DECL_ATTRIBUTES (decl->csym) != NULL)
  1105. decl_attributes (&decl->csym, DECL_ATTRIBUTES (decl->csym), 0);
  1106. /* %% Probably should be a little more intelligent about setting this. */
  1107. TREE_USED (decl->csym) = 1;
  1108. d_keep (decl->csym);
  1109. return decl->csym;
  1110. }
  1111. /* Returns a declaration for a VAR_DECL. Used to create compiler-generated
  1112. global variables. */
  1113. tree
  1114. declare_extern_var (tree ident, tree type)
  1115. {
  1116. /* If the VAR_DECL has already been declared, return it. */
  1117. if (IDENTIFIER_DECL_TREE (ident))
  1118. return IDENTIFIER_DECL_TREE (ident);
  1119. tree name = IDENTIFIER_PRETTY_NAME (ident)
  1120. ? IDENTIFIER_PRETTY_NAME (ident) : ident;
  1121. tree decl = build_decl (input_location, VAR_DECL, name, type);
  1122. IDENTIFIER_DECL_TREE (ident) = decl;
  1123. d_keep (decl);
  1124. SET_DECL_ASSEMBLER_NAME (decl, ident);
  1125. DECL_ARTIFICIAL (decl) = 1;
  1126. TREE_STATIC (decl) = 1;
  1127. TREE_PUBLIC (decl) = 1;
  1128. /* The decl has not been defined -- yet. */
  1129. DECL_EXTERNAL (decl) = 1;
  1130. return decl;
  1131. }
  1132. /* Add local variable VAR into the current function body. */
  1133. void
  1134. declare_local_var (VarDeclaration *var)
  1135. {
  1136. gcc_assert (!var->isDataseg () && !var->isMember ());
  1137. gcc_assert (current_function_decl != NULL_TREE);
  1138. FuncDeclaration *fd = cfun->language->function;
  1139. tree decl = get_symbol_decl (var);
  1140. gcc_assert (!TREE_STATIC (decl));
  1141. d_pushdecl (decl);
  1142. DECL_CONTEXT (decl) = current_function_decl;
  1143. /* Compiler generated symbols. */
  1144. if (var == fd->vresult || var == fd->v_argptr)
  1145. DECL_ARTIFICIAL (decl) = 1;
  1146. if (DECL_LANG_FRAME_FIELD (decl))
  1147. {
  1148. /* Fixes debugging local variables. */
  1149. SET_DECL_VALUE_EXPR (decl, get_decl_tree (var));
  1150. DECL_HAS_VALUE_EXPR_P (decl) = 1;
  1151. }
  1152. }
  1153. /* Return an unnamed local temporary of type TYPE. */
  1154. tree
  1155. build_local_temp (tree type)
  1156. {
  1157. tree decl = build_decl (input_location, VAR_DECL, NULL_TREE, type);
  1158. DECL_CONTEXT (decl) = current_function_decl;
  1159. DECL_ARTIFICIAL (decl) = 1;
  1160. DECL_IGNORED_P (decl) = 1;
  1161. d_pushdecl (decl);
  1162. return decl;
  1163. }
  1164. /* Return the correct decl to be used for DECL. For VAR_DECLs, this could
  1165. instead be a FIELD_DECL from a closure, or a RESULT_DECL from a named return
  1166. value. For PARM_DECLs, this could be a FIELD_DECL for a non-local `this'.
  1167. For all other kinds of decls, this just returns the result of
  1168. get_symbol_decl(). */
  1169. tree
  1170. get_decl_tree (Declaration *decl)
  1171. {
  1172. tree t = get_symbol_decl (decl);
  1173. FuncDeclaration *fd = cfun ? cfun->language->function : NULL;
  1174. VarDeclaration *vd = decl->isVarDeclaration ();
  1175. /* If cfun is NULL, then this is a global static. */
  1176. if (vd == NULL || fd == NULL)
  1177. return t;
  1178. /* Get the named return value. */
  1179. if (DECL_LANG_NRVO (t))
  1180. return DECL_LANG_NRVO (t);
  1181. /* Get the closure holding the var decl. */
  1182. if (DECL_LANG_FRAME_FIELD (t))
  1183. {
  1184. FuncDeclaration *parent = vd->toParent2 ()->isFuncDeclaration ();
  1185. tree frame_ref = get_framedecl (fd, parent);
  1186. return component_ref (build_deref (frame_ref),
  1187. DECL_LANG_FRAME_FIELD (t));
  1188. }
  1189. /* Get the non-local 'this' value by going through parent link
  1190. of nested classes, this routine pretty much undoes what
  1191. getRightThis in the frontend removes from codegen. */
  1192. if (vd->parent != fd && vd->isThisDeclaration ())
  1193. {
  1194. /* Find the first parent that is a member function. */
  1195. while (!fd->isMember2 ())
  1196. {
  1197. gcc_assert (fd->vthis);
  1198. fd = fd->toParent2 ()->isFuncDeclaration ();
  1199. gcc_assert (fd != NULL);
  1200. }
  1201. AggregateDeclaration *ad = fd->isThis ();
  1202. gcc_assert (ad != NULL);
  1203. t = get_decl_tree (fd->vthis);
  1204. Dsymbol *outer = fd;
  1205. while (outer != vd->parent)
  1206. {
  1207. gcc_assert (ad != NULL);
  1208. outer = ad->toParent2 ();
  1209. /* Get the this->this parent link. */
  1210. tree vfield = get_symbol_decl (ad->vthis);
  1211. t = component_ref (build_deref (t), vfield);
  1212. ad = outer->isAggregateDeclaration ();
  1213. if (ad != NULL)
  1214. continue;
  1215. fd = outer->isFuncDeclaration ();
  1216. while (fd != NULL)
  1217. {
  1218. /* If outer function creates a closure, then the 'this'
  1219. value would be the closure pointer, and the real
  1220. 'this' the first field of that closure. */
  1221. tree ff = get_frameinfo (fd);
  1222. if (FRAMEINFO_CREATES_FRAME (ff))
  1223. {
  1224. t = build_nop (build_pointer_type (FRAMEINFO_TYPE (ff)), t);
  1225. t = indirect_ref (build_ctype (fd->vthis->type), t);
  1226. }
  1227. if (fd == vd->parent)
  1228. break;
  1229. /* Continue looking for the right `this'. */
  1230. outer = outer->toParent2 ();
  1231. fd = outer->isFuncDeclaration ();
  1232. }
  1233. ad = outer->isAggregateDeclaration ();
  1234. }
  1235. return t;
  1236. }
  1237. /* Auto variable that the back end will handle for us. */
  1238. return t;
  1239. }
  1240. /* Update the TLS model on variable DECL, typically after the linkage
  1241. has been modified. */
  1242. static void
  1243. reset_decl_tls_model (tree decl)
  1244. {
  1245. if (DECL_THREAD_LOCAL_P (decl))
  1246. set_decl_tls_model (decl, decl_default_tls_model (decl));
  1247. }
  1248. /* Finish up a variable declaration and compile it all the way to
  1249. the assembler language output. */
  1250. void
  1251. d_finish_decl (tree decl)
  1252. {
  1253. gcc_assert (!error_operand_p (decl));
  1254. /* We are sending this symbol to object file, can't be extern. */
  1255. TREE_STATIC (decl) = 1;
  1256. DECL_EXTERNAL (decl) = 0;
  1257. reset_decl_tls_model (decl);
  1258. relayout_decl (decl);
  1259. if (flag_checking && DECL_INITIAL (decl))
  1260. {
  1261. /* Initializer must never be bigger than symbol size. */
  1262. dinteger_t tsize = int_size_in_bytes (TREE_TYPE (decl));
  1263. dinteger_t dtsize = int_size_in_bytes (TREE_TYPE (DECL_INITIAL (decl)));
  1264. if (tsize < dtsize)
  1265. {
  1266. tree name = DECL_ASSEMBLER_NAME (decl);
  1267. internal_error ("Mismatch between declaration %qE size (%wd) and "
  1268. "its initializer size (%wd).",
  1269. IDENTIFIER_PRETTY_NAME (name)
  1270. ? IDENTIFIER_PRETTY_NAME (name) : name,
  1271. tsize, dtsize);
  1272. }
  1273. }
  1274. /* Without weak symbols, symbol should be put in .common, but that can't
  1275. be done if there is a nonzero initializer. */
  1276. if (DECL_COMDAT (decl) && DECL_COMMON (decl)
  1277. && initializer_zerop (DECL_INITIAL (decl)))
  1278. DECL_INITIAL (decl) = error_mark_node;
  1279. /* Add this decl to the current binding level. */
  1280. d_pushdecl (decl);
  1281. rest_of_decl_compilation (decl, 1, 0);
  1282. }
  1283. /* Thunk code is based on g++. */
  1284. static int thunk_labelno;
  1285. /* Create a static alias to function. */
  1286. static tree
  1287. make_alias_for_thunk (tree function)
  1288. {
  1289. tree alias;
  1290. char buf[256];
  1291. /* Thunks may reference extern functions which cannot be aliased. */
  1292. if (DECL_EXTERNAL (function))
  1293. return function;
  1294. targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
  1295. thunk_labelno++;
  1296. alias = build_decl (DECL_SOURCE_LOCATION (function), FUNCTION_DECL,
  1297. get_identifier (buf), TREE_TYPE (function));
  1298. DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (function);
  1299. lang_hooks.dup_lang_specific_decl (alias);
  1300. DECL_CONTEXT (alias) = NULL_TREE;
  1301. TREE_READONLY (alias) = TREE_READONLY (function);
  1302. TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (function);
  1303. TREE_PUBLIC (alias) = 0;
  1304. DECL_EXTERNAL (alias) = 0;
  1305. DECL_ARTIFICIAL (alias) = 1;
  1306. DECL_DECLARED_INLINE_P (alias) = 0;
  1307. DECL_INITIAL (alias) = error_mark_node;
  1308. DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (function));
  1309. TREE_ADDRESSABLE (alias) = 1;
  1310. TREE_USED (alias) = 1;
  1311. SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
  1312. if (!flag_syntax_only)
  1313. {
  1314. cgraph_node *aliasn;
  1315. aliasn = cgraph_node::create_same_body_alias (alias, function);
  1316. gcc_assert (aliasn != NULL);
  1317. }
  1318. return alias;
  1319. }
  1320. /* Emit the definition of a D vtable thunk. */
  1321. static void
  1322. finish_thunk (tree thunk, tree function)
  1323. {
  1324. /* Setup how D thunks are outputted. */
  1325. int fixed_offset = -THUNK_LANG_OFFSET (thunk);
  1326. bool this_adjusting = true;
  1327. tree alias;
  1328. if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
  1329. alias = make_alias_for_thunk (function);
  1330. else
  1331. alias = function;
  1332. TREE_ADDRESSABLE (function) = 1;
  1333. TREE_USED (function) = 1;
  1334. if (flag_syntax_only)
  1335. {
  1336. TREE_ASM_WRITTEN (thunk) = 1;
  1337. return;
  1338. }
  1339. if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
  1340. && targetm_common.have_named_sections)
  1341. {
  1342. tree fn = function;
  1343. symtab_node *symbol = symtab_node::get (function);
  1344. if (symbol != NULL && symbol->alias)
  1345. {
  1346. if (symbol->analyzed)
  1347. fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
  1348. else
  1349. fn = symtab_node::get (function)->alias_target;
  1350. }
  1351. resolve_unique_section (fn, 0, flag_function_sections);
  1352. if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
  1353. {
  1354. resolve_unique_section (thunk, 0, flag_function_sections);
  1355. /* Output the thunk into the same section as function. */
  1356. set_decl_section_name (thunk, DECL_SECTION_NAME (fn));
  1357. symtab_node::get (thunk)->implicit_section
  1358. = symtab_node::get (fn)->implicit_section;
  1359. }
  1360. }
  1361. /* Set up cloned argument trees for the thunk. */
  1362. tree t = NULL_TREE;
  1363. for (tree a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
  1364. {
  1365. tree x = copy_node (a);
  1366. DECL_CHAIN (x) = t;
  1367. DECL_CONTEXT (x) = thunk;
  1368. SET_DECL_RTL (x, NULL);
  1369. DECL_HAS_VALUE_EXPR_P (x) = 0;
  1370. TREE_ADDRESSABLE (x) = 0;
  1371. t = x;
  1372. }
  1373. DECL_ARGUMENTS (thunk) = nreverse (t);
  1374. TREE_ASM_WRITTEN (thunk) = 1;
  1375. cgraph_node *funcn, *thunk_node;
  1376. funcn = cgraph_node::get_create (function);
  1377. gcc_assert (funcn);
  1378. thunk_node = funcn->create_thunk (thunk, thunk, this_adjusting,
  1379. fixed_offset, 0, 0, 0, alias);
  1380. if (DECL_ONE_ONLY (function))
  1381. thunk_node->add_to_same_comdat_group (funcn);
  1382. /* Target assemble_mi_thunk doesn't work across section boundaries
  1383. on many targets, instead force thunk to be expanded in gimple. */
  1384. if (DECL_EXTERNAL (function))
  1385. {
  1386. /* cgraph::expand_thunk writes over current_function_decl, so if this
  1387. could ever be in use by the codegen pass, we want to know about it. */
  1388. gcc_assert (current_function_decl == NULL_TREE);
  1389. if (!stdarg_p (TREE_TYPE (thunk)))
  1390. {
  1391. thunk_node->create_edge (funcn, NULL, thunk_node->count);
  1392. thunk_node->expand_thunk (false, true);
  1393. }
  1394. /* Tell the back-end to not bother inlining the function, this is
  1395. assumed not to work as it could be referencing symbols outside
  1396. of the current compilation unit. */
  1397. DECL_UNINLINABLE (function) = 1;
  1398. }
  1399. }
  1400. /* Return a thunk to DECL. Thunks adjust the incoming `this' pointer by OFFSET.
  1401. Adjustor thunks are created and pointers to them stored in the method entries
  1402. in the vtable in order to set the this pointer to the start of the object
  1403. instance corresponding to the implementing method. */
  1404. tree
  1405. make_thunk (FuncDeclaration *decl, int offset)
  1406. {
  1407. tree function = get_symbol_decl (decl);
  1408. if (!DECL_ARGUMENTS (function) || !DECL_RESULT (function))
  1409. {
  1410. /* Compile the function body before generating the thunk, this is done
  1411. even if the decl is external to the current module. */
  1412. if (decl->fbody)
  1413. build_decl_tree (decl);
  1414. else
  1415. {
  1416. /* Build parameters for functions that are not being compiled,
  1417. so that they can be correctly cloned in finish_thunk. */
  1418. tree fntype = TREE_TYPE (function);
  1419. tree params = NULL_TREE;
  1420. for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
  1421. {
  1422. if (t == void_list_node)
  1423. break;
  1424. tree param = build_decl (DECL_SOURCE_LOCATION (function),
  1425. PARM_DECL, NULL_TREE, TREE_VALUE (t));
  1426. DECL_ARG_TYPE (param) = TREE_TYPE (param);
  1427. DECL_ARTIFICIAL (param) = 1;
  1428. DECL_IGNORED_P (param) = 1;
  1429. DECL_CONTEXT (param) = function;
  1430. params = chainon (params, param);
  1431. }
  1432. DECL_ARGUMENTS (function) = params;
  1433. /* Also build the result decl, which is needed when force creating
  1434. the thunk in gimple inside cgraph_node::expand_thunk. */
  1435. tree resdecl = build_decl (DECL_SOURCE_LOCATION (function),
  1436. RESULT_DECL, NULL_TREE,
  1437. TREE_TYPE (fntype));
  1438. DECL_ARTIFICIAL (resdecl) = 1;
  1439. DECL_IGNORED_P (resdecl) = 1;
  1440. DECL_CONTEXT (resdecl) = function;
  1441. DECL_RESULT (function) = resdecl;
  1442. }
  1443. }
  1444. /* Don't build the thunk if the compilation step failed. */
  1445. if (global.errors)
  1446. return error_mark_node;
  1447. /* See if we already have the thunk in question. */
  1448. for (tree t = DECL_LANG_THUNKS (function); t; t = DECL_CHAIN (t))
  1449. {
  1450. if (THUNK_LANG_OFFSET (t) == offset)
  1451. return t;
  1452. }
  1453. tree thunk = build_decl (DECL_SOURCE_LOCATION (function),
  1454. FUNCTION_DECL, NULL_TREE, TREE_TYPE (f

Large files files are truncated, but you can click here to view the full file