]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/d/decl.cc
d: Fix accesses of immutable arrays using constant index still bounds checked
[thirdparty/gcc.git] / gcc / d / decl.cc
CommitLineData
b4c522fa 1/* decl.cc -- Lower D frontend declarations to GCC trees.
83ffe9cd 2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
b4c522fa
IB
3
4GCC is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 3, or (at your option)
7any later version.
8
9GCC is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with GCC; see the file COPYING3. If not see
16<http://www.gnu.org/licenses/>. */
17
18#include "config.h"
19#include "system.h"
20#include "coretypes.h"
21
22#include "dmd/aggregate.h"
23#include "dmd/attrib.h"
c50eadba 24#include "dmd/cond.h"
b4c522fa
IB
25#include "dmd/ctfe.h"
26#include "dmd/declaration.h"
27#include "dmd/enum.h"
28#include "dmd/errors.h"
29#include "dmd/globals.h"
30#include "dmd/hdrgen.h"
31#include "dmd/identifier.h"
32#include "dmd/import.h"
33#include "dmd/init.h"
34#include "dmd/mangle.h"
35#include "dmd/module.h"
36#include "dmd/nspace.h"
37#include "dmd/target.h"
38#include "dmd/template.h"
39
40#include "tree.h"
41#include "tree-iterator.h"
42#include "fold-const.h"
43#include "diagnostic.h"
44#include "langhooks.h"
45#include "target.h"
46#include "common/common-target.h"
47#include "cgraph.h"
48#include "toplev.h"
49#include "stringpool.h"
50#include "varasm.h"
51#include "stor-layout.h"
52#include "attribs.h"
53#include "function.h"
54#include "debug.h"
55#include "tree-pretty-print.h"
89576d86 56#include "tree-nested.h"
67f3791f
JH
57#include "alloc-pool.h"
58#include "symbol-summary.h"
59#include "symtab-thunks.h"
ad4c44d2 60#include "gimple-expr.h"
b4c522fa
IB
61
62#include "d-tree.h"
bda51959 63#include "d-target.h"
b4c522fa
IB
64
65
66/* Return identifier for the external mangled name of DECL. */
67
b5446d0c
IB
68const char *
69d_mangle_decl (Dsymbol *decl)
b4c522fa
IB
70{
71 if (decl->isFuncDeclaration ())
af3c19f0 72 return mangleExact ((FuncDeclaration *) decl);
b4c522fa
IB
73 else
74 {
75 OutBuffer buf;
76 mangleToBuffer (decl, &buf);
fced594b 77 return buf.extractChars ();
b4c522fa
IB
78 }
79}
80
81/* Generate a mangled identifier using NAME and SUFFIX, prefixed by the
82 assembler name for DECL. */
83
84tree
85mangle_internal_decl (Dsymbol *decl, const char *name, const char *suffix)
86{
b5446d0c 87 const char *prefix = d_mangle_decl (decl);
b4c522fa
IB
88 unsigned namelen = strlen (name);
89 unsigned buflen = (2 + strlen (prefix) + namelen + strlen (suffix)) * 2;
90 char *buf = (char *) alloca (buflen);
91
92 snprintf (buf, buflen, "_D%s%u%s%s", prefix, namelen, name, suffix);
93 tree ident = get_identifier (buf);
94
95 /* Symbol is not found in user code, but generate a readable name for it
96 anyway for debug and diagnostic reporting. */
97 snprintf (buf, buflen, "%s.%s", decl->toPrettyChars (), name);
98 IDENTIFIER_PRETTY_NAME (ident) = get_identifier (buf);
99
100 return ident;
101}
102
f4c3ce32 103/* Returns true if DECL is from the gcc.attributes module. */
b4c522fa
IB
104
105static bool
106gcc_attribute_p (Dsymbol *decl)
107{
108 ModuleDeclaration *md = decl->getModule ()->md;
109
5fee5ec3 110 if (md && md->packages.length == 1)
b4c522fa 111 {
5fee5ec3 112 if (!strcmp (md->packages.ptr[0]->toChars (), "gcc")
8e84a142 113 && !strcmp (md->id->toChars (), "attributes"))
b4c522fa
IB
114 return true;
115 }
116
117 return false;
118}
119
499b0770
IB
120/* Return the DECL_RESULT for the function declaration DECL, create it if it
121 doesn't already exist. */
122
123static tree
124get_fndecl_result (FuncDeclaration *decl)
125{
126 tree fndecl = get_symbol_decl (decl);
127 tree resdecl = DECL_RESULT (fndecl);
128
129 if (resdecl != NULL_TREE)
130 return resdecl;
131
132 resdecl = build_decl (make_location_t (decl->loc), RESULT_DECL,
133 NULL_TREE, TREE_TYPE (TREE_TYPE (fndecl)));
134
135 DECL_ARTIFICIAL (resdecl) = 1;
136 DECL_IGNORED_P (resdecl) = 1;
137 DECL_CONTEXT (resdecl) = fndecl;
138 DECL_RESULT (fndecl) = resdecl;
139 return resdecl;
140}
141
142/* Return the list of PARAM_DECLs for the function declaration DECL, create it
143 if it doesn't already exist. */
144
145static tree
146get_fndecl_arguments (FuncDeclaration *decl)
147{
148 tree fndecl = get_symbol_decl (decl);
149 tree param_list = DECL_ARGUMENTS (fndecl);
150
151 if (param_list != NULL_TREE)
152 return param_list;
153
154 if (decl->fbody)
155 {
156 /* Handle special arguments first. */
157
158 /* `this' parameter:
159 For nested functions, D still generates a vthis, but it
160 should not be referenced in any expression. */
161 if (decl->vthis)
162 {
163 tree parm_decl = get_symbol_decl (decl->vthis);
164 DECL_ARTIFICIAL (parm_decl) = 1;
165 TREE_READONLY (parm_decl) = 1;
166
167 if (decl->vthis->type == Type::tvoidptr)
168 {
169 /* Replace generic pointer with back-end closure type
170 (this wins for gdb). */
171 tree frame_type = FRAMEINFO_TYPE (get_frameinfo (decl));
172 gcc_assert (frame_type != NULL_TREE);
173 TREE_TYPE (parm_decl) = build_pointer_type (frame_type);
174 }
175
176 param_list = chainon (param_list, parm_decl);
177 }
178
179 /* `_arguments' parameter. */
180 if (decl->v_arguments)
181 {
182 tree parm_decl = get_symbol_decl (decl->v_arguments);
183 param_list = chainon (param_list, parm_decl);
184 }
185
186 /* Now add on formal function parameters. */
187 size_t n_parameters = decl->parameters ? decl->parameters->length : 0;
188
189 for (size_t i = 0; i < n_parameters; i++)
190 {
191 VarDeclaration *param = (*decl->parameters)[i];
192 tree parm_decl = get_symbol_decl (param);
193
194 /* Type `noreturn` is a terminator, as no other arguments can possibly
195 be evaluated after it. */
196 if (TREE_TYPE (parm_decl) == noreturn_type_node)
197 break;
198
199 /* Chain them in the correct order. */
200 param_list = chainon (param_list, parm_decl);
201 }
202 }
203 else
204 {
205 /* Build parameters from the function type. */
206 tree fntype = TREE_TYPE (fndecl);
207
208 for (tree t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
209 {
210 if (t == void_list_node)
211 break;
212
213 tree param = build_decl (DECL_SOURCE_LOCATION (fndecl),
214 PARM_DECL, NULL_TREE, TREE_VALUE (t));
215 DECL_ARG_TYPE (param) = TREE_TYPE (param);
216 DECL_ARTIFICIAL (param) = 1;
217 DECL_IGNORED_P (param) = 1;
218 DECL_CONTEXT (param) = fndecl;
219 param_list = chainon (param_list, param);
220 }
221 }
222
223 DECL_ARGUMENTS (fndecl) = param_list;
224 return param_list;
225}
226
b4c522fa
IB
227/* Implements the visitor interface to lower all Declaration AST classes
228 emitted from the D Front-end to GCC trees.
229 All visit methods accept one parameter D, which holds the frontend AST
230 of the declaration to compile. These also don't return any value, instead
231 generated code are appened to global_declarations or added to the
232 current_binding_level by d_pushdecl(). */
233
234class DeclVisitor : public Visitor
235{
236 using Visitor::visit;
237
c50eadba
IB
238 /* If we're lowering the body of a version(unittest) condition. */
239 bool in_version_unittest_;
240
b4c522fa
IB
241public:
242 DeclVisitor (void)
243 {
c50eadba 244 this->in_version_unittest_ = false;
b4c522fa
IB
245 }
246
7edc15f2
IB
247 /* Helper for generating code for the dsymbol AST class D.
248 Sets up the location of the symbol before lowering. */
249
250 void build_dsymbol (Dsymbol *d)
251 {
252 location_t saved_location = input_location;
253 input_location = make_location_t (d->loc);
254 d->accept (this);
255 input_location = saved_location;
256 }
257
b4c522fa
IB
258 /* This should be overridden by each declaration class. */
259
442cf097 260 void visit (Dsymbol *) final override
b4c522fa
IB
261 {
262 }
263
264 /* Compile a D module, and all members of it. */
265
442cf097 266 void visit (Module *d) final override
b4c522fa 267 {
0fb57034 268 if (d->semanticRun >= PASS::obj)
b4c522fa
IB
269 return;
270
271 build_module_tree (d);
0fb57034 272 d->semanticRun = PASS::obj;
b4c522fa
IB
273 }
274
275 /* Write the imported symbol to debug. */
276
442cf097 277 void visit (Import *d) final override
b4c522fa 278 {
0fb57034 279 if (d->semanticRun >= PASS::obj)
e64deb74
IB
280 return;
281
b4c522fa
IB
282 /* Implements import declarations by telling the debug back-end we are
283 importing the NAMESPACE_DECL of the module or IMPORTED_DECL of the
284 declaration into the current lexical scope CONTEXT. NAME is set if
285 this is a renamed import. */
286 if (d->isstatic)
287 return;
288
289 /* Get the context of this import, this should never be null. */
290 tree context = d_module_context ();
291
292 if (d->ident == NULL)
293 {
294 /* Importing declaration list. */
2cbc99d1 295 for (size_t i = 0; i < d->names.length; i++)
b4c522fa
IB
296 {
297 AliasDeclaration *aliasdecl = d->aliasdecls[i];
298 tree decl = build_import_decl (aliasdecl);
299
300 /* Skip over unhandled imports. */
301 if (decl == NULL_TREE)
302 continue;
303
304 Identifier *alias = d->aliases[i];
305 tree name = (alias != NULL)
306 ? get_identifier (alias->toChars ()) : NULL_TREE;
307
d9d8c967
IB
308 if (TREE_CODE (decl) != TREE_LIST)
309 debug_hooks->imported_module_or_decl (decl, name, context,
310 false, false);
311 else
312 {
313 /* Overload sets return a list of imported decls. */
314 for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
315 debug_hooks->imported_module_or_decl (TREE_VALUE (decl), name,
316 context, false, false);
317 }
b4c522fa
IB
318 }
319 }
320 else
321 {
322 /* Importing the entire module. */
323 tree decl = build_import_decl (d->mod);
324
325 tree name = (d->aliasId != NULL)
326 ? get_identifier (d->aliasId->toChars ()) : NULL_TREE;
327
328 debug_hooks->imported_module_or_decl (decl, name, context,
329 false, false);
330 }
e64deb74 331
0fb57034 332 d->semanticRun = PASS::obj;
b4c522fa
IB
333 }
334
335 /* Expand any local variables found in tuples. */
336
442cf097 337 void visit (TupleDeclaration *d) final override
b4c522fa 338 {
2cbc99d1 339 for (size_t i = 0; i < d->objects->length; i++)
b4c522fa
IB
340 {
341 RootObject *o = (*d->objects)[i];
d873350a 342 if (o->dyncast () == DYNCAST_EXPRESSION)
b4c522fa 343 {
d97f3bca
IB
344 VarExp *ve = ((Expression *) o)->isVarExp ();
345 if (ve)
346 this->build_dsymbol (ve->var);
b4c522fa
IB
347 }
348 }
349 }
350
351 /* Walk over all declarations in the attribute scope. */
352
442cf097 353 void visit (AttribDeclaration *d) final override
b4c522fa 354 {
d3da83f6 355 Dsymbols *ds = d->include (NULL);
b4c522fa
IB
356
357 if (!ds)
358 return;
359
2cbc99d1 360 for (size_t i = 0; i < ds->length; i++)
7edc15f2 361 this->build_dsymbol ((*ds)[i]);
b4c522fa
IB
362 }
363
364 /* Pragmas are a way to pass special information to the compiler and to add
5fee5ec3 365 vendor specific extensions to D. */
b4c522fa 366
442cf097 367 void visit (PragmaDeclaration *d) final override
b4c522fa 368 {
5fee5ec3
IB
369 if (d->ident == Identifier::idPool ("lib")
370 || d->ident == Identifier::idPool ("startaddress"))
b4c522fa 371 {
5fee5ec3 372 if (!global.params.ignoreUnsupportedPragmas)
b4c522fa
IB
373 {
374 warning_at (make_location_t (d->loc), OPT_Wunknown_pragmas,
375 "pragma(%s) not implemented", d->ident->toChars ());
376 }
377 }
378
379 visit ((AttribDeclaration *) d);
380 }
381
c50eadba
IB
382 /* Conditional compilation is the process of selecting which code to compile
383 and which code to not compile. Look for version conditions that may */
384
442cf097 385 void visit (ConditionalDeclaration *d) final override
c50eadba
IB
386 {
387 bool old_condition = this->in_version_unittest_;
388
389 if (global.params.useUnitTests)
390 {
391 VersionCondition *vc = d->condition->isVersionCondition ();
392 if (vc && vc->ident == Identifier::idPool ("unittest"))
393 this->in_version_unittest_ = true;
394 }
395
396 visit ((AttribDeclaration *) d);
397
398 this->in_version_unittest_ = old_condition;
399 }
400
b4c522fa
IB
401 /* Walk over all members in the namespace scope. */
402
442cf097 403 void visit (Nspace *d) final override
b4c522fa
IB
404 {
405 if (isError (d) || !d->members)
406 return;
407
2cbc99d1 408 for (size_t i = 0; i < d->members->length; i++)
7edc15f2 409 this->build_dsymbol ((*d->members)[i]);
b4c522fa
IB
410 }
411
9dddefef
IB
412 /* Templates are D's approach to generic programming. They have no members
413 that can be emitted, however if the template is nested and used as a
414 voldemort type, then it's members must be compiled before the parent
415 function finishes. */
416
442cf097 417 void visit (TemplateDeclaration *d) final override
9dddefef
IB
418 {
419 /* Type cannot be directly named outside of the scope it's declared in, so
420 the only way it can be escaped is if the function has auto return. */
421 FuncDeclaration *fd = d_function_chain ? d_function_chain->function : NULL;
422
423 if (!fd || !fd->isAuto ())
424 return;
425
426 /* Check if the function returns an instantiated type that may contain
427 nested members. Only applies to classes or structs. */
428 Type *tb = fd->type->nextOf ()->baseElemOf ();
429
5fee5ec3 430 while (tb->ty == TY::Tarray || tb->ty == TY::Tpointer)
9dddefef
IB
431 tb = tb->nextOf ()->baseElemOf ();
432
433 TemplateInstance *ti = NULL;
434
5fee5ec3 435 if (tb->ty == TY::Tstruct)
89fdaf5a 436 ti = tb->isTypeStruct ()->sym->isInstantiated ();
5fee5ec3 437 else if (tb->ty == TY::Tclass)
89fdaf5a 438 ti = tb->isTypeClass ()->sym->isInstantiated ();
9dddefef
IB
439
440 /* Return type is instantiated from this template declaration, walk over
441 all members of the instance. */
442 if (ti && ti->tempdecl == d)
7edc15f2 443 this->build_dsymbol (ti);
9dddefef
IB
444 }
445
b4c522fa
IB
446 /* Walk over all members in the instantiated template. */
447
442cf097 448 void visit (TemplateInstance *d) final override
b4c522fa
IB
449 {
450 if (isError (d)|| !d->members)
451 return;
452
453 if (!d->needsCodegen ())
454 return;
455
2cbc99d1 456 for (size_t i = 0; i < d->members->length; i++)
7edc15f2 457 this->build_dsymbol ((*d->members)[i]);
b4c522fa
IB
458 }
459
460 /* Walk over all members in the mixin template scope. */
461
442cf097 462 void visit (TemplateMixin *d) final override
b4c522fa
IB
463 {
464 if (isError (d)|| !d->members)
465 return;
466
2cbc99d1 467 for (size_t i = 0; i < d->members->length; i++)
7edc15f2 468 this->build_dsymbol ((*d->members)[i]);
b4c522fa
IB
469 }
470
471 /* Write out compiler generated TypeInfo, initializer and functions for the
472 given struct declaration, walking over all static members. */
473
442cf097 474 void visit (StructDeclaration *d) final override
b4c522fa 475 {
0fb57034 476 if (d->semanticRun >= PASS::obj)
e64deb74
IB
477 return;
478
5fee5ec3 479 if (d->type->ty == TY::Terror)
b4c522fa
IB
480 {
481 error_at (make_location_t (d->loc),
482 "had semantic errors when compiling");
483 return;
484 }
485
f4c3ce32
IB
486 /* Don't emit any symbols from gcc.attributes module. */
487 if (gcc_attribute_p (d))
488 return;
489
b4c522fa
IB
490 /* Add this decl to the current binding level. */
491 tree ctype = build_ctype (d->type);
492 if (TYPE_NAME (ctype))
493 d_pushdecl (TYPE_NAME (ctype));
494
495 /* Anonymous structs/unions only exist as part of others,
496 do not output forward referenced structs. */
497 if (d->isAnonymous () || !d->members)
498 return;
499
b4c522fa 500 /* Generate TypeInfo. */
c0aebc60
IB
501 if (have_typeinfo_p (Type::dtypeinfo))
502 create_typeinfo (d->type, NULL);
b4c522fa
IB
503
504 /* Generate static initializer. */
99d6d3d9
IB
505 tree sinit = aggregate_initializer_decl (d);
506 DECL_INITIAL (sinit) = layout_struct_initializer (d);
507 d_finish_decl (sinit);
b4c522fa 508
7edc15f2
IB
509 /* Put out the members. There might be static constructors in the members
510 list, and they cannot be put in separate object files. */
2cbc99d1 511 for (size_t i = 0; i < d->members->length; i++)
7edc15f2 512 this->build_dsymbol ((*d->members)[i]);
b4c522fa
IB
513
514 /* Put out xopEquals, xopCmp and xopHash. */
515 if (d->xeq && d->xeq != d->xerreq)
7edc15f2 516 this->build_dsymbol (d->xeq);
b4c522fa
IB
517
518 if (d->xcmp && d->xcmp != d->xerrcmp)
7edc15f2 519 this->build_dsymbol (d->xcmp);
b4c522fa
IB
520
521 if (d->xhash)
7edc15f2 522 this->build_dsymbol (d->xhash);
e64deb74 523
0fb57034 524 d->semanticRun = PASS::obj;
b4c522fa
IB
525 }
526
527 /* Finish semantic analysis of functions in vtbl for class CD. */
528
529 bool finish_vtable (ClassDeclaration *d)
530 {
531 bool has_errors = false;
532
533 /* Finish semantic analysis of functions in vtbl[]. */
2cbc99d1 534 for (size_t i = d->vtblOffset (); i < d->vtbl.length; i++)
b4c522fa
IB
535 {
536 FuncDeclaration *fd = d->vtbl[i]->isFuncDeclaration ();
537
538 if (!fd || (!fd->fbody && d->isAbstract ()))
539 continue;
540
541 /* Ensure function has a return value. */
542 if (!fd->functionSemantic ())
543 has_errors = true;
544
545 /* No name hiding to check for. */
546 if (!d->isFuncHidden (fd) || fd->isFuture ())
547 continue;
548
549 /* The function fd is hidden from the view of the class.
550 If it overlaps with any function in the vtbl[], then
551 issue an error. */
2cbc99d1 552 for (size_t j = 1; j < d->vtbl.length; j++)
b4c522fa
IB
553 {
554 if (j == i)
555 continue;
556
557 FuncDeclaration *fd2 = d->vtbl[j]->isFuncDeclaration ();
558 if (!fd2->ident->equals (fd->ident))
559 continue;
560
561 /* The function is marked as @__future, a deprecation has
562 already been given by the frontend. */
563 if (fd2->isFuture ())
564 continue;
565
8da8c7d3
IB
566 if (fd->leastAsSpecialized (fd2, NULL) != MATCH::nomatch
567 || fd2->leastAsSpecialized (fd, NULL) != MATCH::nomatch)
b4c522fa 568 {
6c4db916
IB
569 error_at (make_location_t (fd->loc), "use of %qs",
570 fd->toPrettyChars ());
571 inform (make_location_t (fd2->loc), "is hidden by %qs",
572 fd2->toPrettyChars ());
573 inform (make_location_t (d->loc),
574 "use %<alias %s = %s.%s;%> to introduce base class "
575 "overload set", fd->toChars (),
576 fd->parent->toChars (), fd->toChars ());
b4c522fa
IB
577 has_errors = true;
578 break;
579 }
580 }
581 }
582
583 return !has_errors;
584 }
585
586 /* Write out compiler generated TypeInfo, initializer and vtables for the
587 given class declaration, walking over all static members. */
588
442cf097 589 void visit (ClassDeclaration *d) final override
b4c522fa 590 {
0fb57034 591 if (d->semanticRun >= PASS::obj)
e64deb74
IB
592 return;
593
5fee5ec3 594 if (d->type->ty == TY::Terror)
b4c522fa
IB
595 {
596 error_at (make_location_t (d->loc),
597 "had semantic errors when compiling");
598 return;
599 }
600
f4c3ce32
IB
601 /* Add this decl to the current binding level. */
602 tree ctype = TREE_TYPE (build_ctype (d->type));
603 if (TYPE_NAME (ctype))
604 d_pushdecl (TYPE_NAME (ctype));
605
b4c522fa
IB
606 if (!d->members)
607 return;
608
609 /* Put out the members. */
2cbc99d1 610 for (size_t i = 0; i < d->members->length; i++)
7edc15f2 611 this->build_dsymbol ((*d->members)[i]);
b4c522fa
IB
612
613 /* If something goes wrong during final semantic pass, don't bother with
614 the rest as we may have incomplete info. */
615 if (!this->finish_vtable (d))
616 return;
617
618 /* Generate C symbols. */
619 d->csym = get_classinfo_decl (d);
5fee5ec3
IB
620 Dsymbol *vtblsym = d->vtblSymbol ();
621 vtblsym->csym = get_vtable_decl (d);
99d6d3d9 622 tree sinit = aggregate_initializer_decl (d);
b4c522fa
IB
623
624 /* Generate static initializer. */
99d6d3d9
IB
625 DECL_INITIAL (sinit) = layout_class_initializer (d);
626 d_finish_decl (sinit);
b4c522fa
IB
627
628 /* Put out the TypeInfo. */
c0aebc60
IB
629 if (have_typeinfo_p (Type::dtypeinfo))
630 create_typeinfo (d->type, NULL);
631
b4c522fa 632 DECL_INITIAL (d->csym) = layout_classinfo (d);
b4c522fa
IB
633 d_finish_decl (d->csym);
634
635 /* Put out the vtbl[]. */
af3c19f0 636 vec <constructor_elt, va_gc> *elms = NULL;
b4c522fa
IB
637
638 /* First entry is ClassInfo reference. */
639 if (d->vtblOffset ())
640 CONSTRUCTOR_APPEND_ELT (elms, size_zero_node, build_address (d->csym));
641
2cbc99d1 642 for (size_t i = d->vtblOffset (); i < d->vtbl.length; i++)
b4c522fa
IB
643 {
644 FuncDeclaration *fd = d->vtbl[i]->isFuncDeclaration ();
645
af3c19f0 646 if (fd && (fd->fbody || !d->isAbstract ()))
b4c522fa
IB
647 {
648 CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
649 build_address (get_symbol_decl (fd)));
650 }
651 }
652
5fee5ec3
IB
653 DECL_INITIAL (vtblsym->csym)
654 = build_constructor (TREE_TYPE (vtblsym->csym), elms);
655 d_finish_decl (vtblsym->csym);
b4c522fa 656
0fb57034 657 d->semanticRun = PASS::obj;
b4c522fa
IB
658 }
659
660 /* Write out compiler generated TypeInfo and vtables for the given interface
661 declaration, walking over all static members. */
662
442cf097 663 void visit (InterfaceDeclaration *d) final override
b4c522fa 664 {
0fb57034 665 if (d->semanticRun >= PASS::obj)
e64deb74
IB
666 return;
667
5fee5ec3 668 if (d->type->ty == TY::Terror)
b4c522fa
IB
669 {
670 error_at (make_location_t (d->loc),
671 "had semantic errors when compiling");
672 return;
673 }
674
f4c3ce32
IB
675 /* Add this decl to the current binding level. */
676 tree ctype = TREE_TYPE (build_ctype (d->type));
677 if (TYPE_NAME (ctype))
678 d_pushdecl (TYPE_NAME (ctype));
679
b4c522fa
IB
680 if (!d->members)
681 return;
682
683 /* Put out the members. */
2cbc99d1 684 for (size_t i = 0; i < d->members->length; i++)
7edc15f2 685 this->build_dsymbol ((*d->members)[i]);
b4c522fa
IB
686
687 /* Generate C symbols. */
688 d->csym = get_classinfo_decl (d);
689
690 /* Put out the TypeInfo. */
c0aebc60
IB
691 if (have_typeinfo_p (Type::dtypeinfo))
692 {
693 create_typeinfo (d->type, NULL);
7edc15f2 694 this->build_dsymbol (d->type->vtinfo);
c0aebc60 695 }
b4c522fa
IB
696
697 DECL_INITIAL (d->csym) = layout_classinfo (d);
b4c522fa
IB
698 d_finish_decl (d->csym);
699
0fb57034 700 d->semanticRun = PASS::obj;
b4c522fa
IB
701 }
702
703 /* Write out compiler generated TypeInfo and initializer for the given
704 enum declaration. */
705
442cf097 706 void visit (EnumDeclaration *d) final override
b4c522fa 707 {
0fb57034 708 if (d->semanticRun >= PASS::obj)
b4c522fa
IB
709 return;
710
5fee5ec3 711 if (d->errors || d->type->ty == TY::Terror)
b4c522fa
IB
712 {
713 error_at (make_location_t (d->loc),
714 "had semantic errors when compiling");
715 return;
716 }
717
f4c3ce32
IB
718 /* Add this decl to the current binding level. */
719 tree ctype = build_ctype (d->type);
720 if (TREE_CODE (ctype) == ENUMERAL_TYPE && TYPE_NAME (ctype))
721 d_pushdecl (TYPE_NAME (ctype));
722
b4c522fa
IB
723 if (d->isAnonymous ())
724 return;
725
726 /* Generate TypeInfo. */
c0aebc60
IB
727 if (have_typeinfo_p (Type::dtypeinfo))
728 create_typeinfo (d->type, NULL);
b4c522fa 729
89fdaf5a 730 TypeEnum *tc = d->type->isTypeEnum ();
b4c522fa
IB
731 if (tc->sym->members && !d->type->isZeroInit ())
732 {
733 /* Generate static initializer. */
734 d->sinit = enum_initializer_decl (d);
735 DECL_INITIAL (d->sinit) = build_expr (tc->sym->defaultval, true);
b4c522fa 736 d_finish_decl (d->sinit);
b4c522fa
IB
737 }
738
0fb57034 739 d->semanticRun = PASS::obj;
b4c522fa
IB
740 }
741
742 /* Finish up a variable declaration and push it into the current scope.
743 This can either be a static, local or manifest constant. */
744
442cf097 745 void visit (VarDeclaration *d) final override
b4c522fa 746 {
0fb57034 747 if (d->semanticRun >= PASS::obj)
e64deb74
IB
748 return;
749
5fee5ec3 750 if (d->type->ty == TY::Terror)
b4c522fa
IB
751 {
752 error_at (make_location_t (d->loc),
753 "had semantic errors when compiling");
754 return;
755 }
756
0fb57034
IB
757 /* Variables of type `noreturn` are just placeholders, and evaluate to
758 `assert(0)` if ever read. */
759 if (d->type->isTypeNoreturn ())
760 {
761 if (!d->isDataseg () && !d->isMember ()
762 && d->_init && !d->_init->isVoidInitializer ())
763 {
b6df1132
IB
764 /* Evaluate RHS for side effects first. */
765 Expression *ie = initializerToExpression (d->_init);
766 add_stmt (build_expr (ie));
767
0fb57034 768 Expression *e = d->type->defaultInitLiteral (d->loc);
b6df1132 769 add_stmt (build_expr (e));
0fb57034
IB
770 }
771
772 return;
773 }
774
8da8c7d3 775 if (d->aliasTuple)
b4c522fa 776 {
7edc15f2 777 this->build_dsymbol (d->toAlias ());
b4c522fa
IB
778 return;
779 }
780
b4c522fa
IB
781 if (!d->canTakeAddressOf ())
782 {
36c9a3fe
IB
783 /* Do not store variables we cannot take the address of,
784 but keep the values for purposes of debugging. */
b4c522fa 785 if (!d->type->isscalar ())
b4c522fa 786 {
36c9a3fe 787 tree decl = get_symbol_decl (d);
b4c522fa
IB
788 d_pushdecl (decl);
789 rest_of_decl_compilation (decl, 1, 0);
790 }
791 }
91418c42 792 else if (d->isDataseg ())
b4c522fa
IB
793 {
794 tree decl = get_symbol_decl (d);
795
91418c42
IB
796 /* Only need to build the VAR_DECL for extern declarations. */
797 if (d->storage_class & STCextern)
798 return;
799
b4c522fa
IB
800 /* Duplicated VarDeclarations map to the same symbol. Check if this
801 is the one declaration which will be emitted. */
802 tree ident = DECL_ASSEMBLER_NAME (decl);
803 if (IDENTIFIER_DSYMBOL (ident) && IDENTIFIER_DSYMBOL (ident) != d)
804 return;
805
806 /* How big a symbol can be should depend on back-end. */
807 tree size = build_integer_cst (d->type->size (d->loc),
808 build_ctype (Type::tsize_t));
809 if (!valid_constant_size_p (size))
810 {
811 error_at (make_location_t (d->loc), "size is too large");
812 return;
813 }
814
eacfafbc 815 if (d->_init)
b4c522fa 816 {
eacfafbc
IB
817 /* Use the explicit initializer, this includes `void`. */
818 if (!d->_init->isVoidInitializer ())
819 {
820 Expression *e = initializerToExpression (d->_init, d->type);
821 DECL_INITIAL (decl) = build_expr (e, true);
822 }
b4c522fa 823 }
8da8c7d3 824 else if (!d->type->isZeroInit ())
b4c522fa 825 {
eacfafbc 826 /* Use default initializer for the type. */
89fdaf5a
IB
827 if (TypeStruct *ts = d->type->isTypeStruct ())
828 DECL_INITIAL (decl) = layout_struct_initializer (ts->sym);
b4c522fa
IB
829 else
830 {
831 Expression *e = d->type->defaultInitLiteral (d->loc);
832 DECL_INITIAL (decl) = build_expr (e, true);
833 }
834 }
835
836 /* Frontend should have already caught this. */
837 gcc_assert (!integer_zerop (size)
0fb57034 838 || d->type->toBasetype ()->isTypeSArray ());
b4c522fa
IB
839
840 d_finish_decl (decl);
841
842 /* Maybe record the var against the current module. */
843 register_module_decl (d);
844 }
845 else if (!d->isDataseg () && !d->isMember ())
846 {
847 /* This is needed for VarDeclarations in mixins that are to be local
848 variables of a function. Otherwise, it would be enough to make
849 a check for isVarDeclaration() in DeclarationExp codegen. */
850 declare_local_var (d);
851
27f698bd 852 if (d->_init && !d->_init->isVoidInitializer ())
b4c522fa
IB
853 {
854 tree decl = get_symbol_decl (d);
855
27f698bd
IB
856 ExpInitializer *vinit = d->_init->isExpInitializer ();
857 Expression *ie = initializerToExpression (vinit);
858 tree exp = build_expr (ie);
859
860 /* Maybe put variable on list of things needing destruction. */
861 if (d->needsScopeDtor ())
b4c522fa 862 {
27f698bd
IB
863 vec_safe_push (d_function_chain->vars_in_scope, decl);
864 /* Force a TARGET_EXPR to add the corresponding cleanup. */
865 exp = force_target_expr (compound_expr (exp, decl));
866 TARGET_EXPR_CLEANUP (exp) = build_expr (d->edtor);
b4c522fa 867 }
27f698bd
IB
868
869 add_stmt (exp);
b4c522fa
IB
870 }
871 }
e64deb74 872
0fb57034 873 d->semanticRun = PASS::obj;
b4c522fa
IB
874 }
875
876 /* Generate and compile a static TypeInfo declaration, but only if it is
877 needed in the current compilation. */
878
442cf097 879 void visit (TypeInfoDeclaration *d) final override
b4c522fa 880 {
0fb57034 881 if (d->semanticRun >= PASS::obj)
e64deb74
IB
882 return;
883
b4c522fa
IB
884 if (speculative_type_p (d->tinfo))
885 return;
886
887 tree t = get_typeinfo_decl (d);
888 DECL_INITIAL (t) = layout_typeinfo (d);
889 d_finish_decl (t);
0fb57034 890 d->semanticRun = PASS::obj;
b4c522fa
IB
891 }
892
893 /* Finish up a function declaration and compile it all the way
894 down to assembler language output. */
895
442cf097 896 void visit (FuncDeclaration *d) final override
b4c522fa
IB
897 {
898 /* Already generated the function. */
0fb57034 899 if (d->semanticRun >= PASS::obj)
b4c522fa
IB
900 return;
901
f4c3ce32 902 /* Don't emit any symbols from gcc.attributes module. */
b4c522fa
IB
903 if (gcc_attribute_p (d))
904 return;
905
8da8c7d3
IB
906 /* Front-end decided this function doesn't require code generation. */
907 if (d->skipCodegen ())
908 return;
909
b4c522fa
IB
910 /* Not emitting unittest functions. */
911 if (!global.params.useUnitTests && d->isUnitTestDeclaration ())
912 return;
913
914 /* Check if any errors occurred when running semantic. */
89fdaf5a 915 if (TypeFunction *tf = d->type->isTypeFunction ())
b4c522fa 916 {
5fee5ec3 917 if (tf->next == NULL || tf->next->ty == TY::Terror)
b4c522fa
IB
918 return;
919 }
920
235d5a96 921 if (d->hasSemantic3Errors ())
b4c522fa
IB
922 return;
923
924 if (d->isNested ())
925 {
926 FuncDeclaration *fdp = d;
927 while (fdp && fdp->isNested ())
928 {
929 fdp = fdp->toParent2 ()->isFuncDeclaration ();
930
931 if (fdp == NULL)
932 break;
933
934 /* Parent failed to compile, but errors were gagged. */
235d5a96 935 if (fdp->hasSemantic3Errors ())
b4c522fa
IB
936 return;
937 }
938 }
939
940 /* Ensure all semantic passes have run. */
0fb57034 941 if (d->semanticRun < PASS::semantic3)
b4c522fa 942 {
8063de4a
IB
943 gcc_assert (!doing_semantic_analysis_p);
944
945 doing_semantic_analysis_p = true;
b4c522fa
IB
946 d->functionSemantic3 ();
947 Module::runDeferredSemantic3 ();
8063de4a 948 doing_semantic_analysis_p = false;
b4c522fa
IB
949 }
950
951 if (global.errors)
952 return;
953
04284176
IB
954 /* Start generating code for this function. */
955 gcc_assert (d->semanticRun == PASS::semantic3done);
956 d->semanticRun = PASS::obj;
957
b4c522fa
IB
958 /* Duplicated FuncDeclarations map to the same symbol. Check if this
959 is the one declaration which will be emitted. */
960 tree fndecl = get_symbol_decl (d);
961 tree ident = DECL_ASSEMBLER_NAME (fndecl);
962 if (IDENTIFIER_DSYMBOL (ident) && IDENTIFIER_DSYMBOL (ident) != d)
963 return;
964
965 if (!d->fbody)
966 {
967 rest_of_decl_compilation (fndecl, 1, 0);
968 return;
969 }
970
971 if (global.params.verbose)
972 message ("function %s", d->toPrettyChars ());
973
b4c522fa 974 tree old_context = start_function (d);
499b0770 975 tree param_list = get_fndecl_arguments (d);
b4c522fa 976
c50eadba 977 DECL_IN_UNITTEST_CONDITION_P (fndecl) = this->in_version_unittest_;
b4c522fa
IB
978 rest_of_decl_compilation (fndecl, 1, 0);
979
980 /* If this is a member function that nested (possibly indirectly) in another
981 function, construct an expession for this member function's static chain
982 by going through parent link of nested classes. */
499b0770
IB
983 if (d->vthis)
984 d_function_chain->static_chain = get_symbol_decl (d->vthis);
985
b4c522fa
IB
986 if (d->isThis ())
987 {
988 AggregateDeclaration *ad = d->isThis ();
989 tree this_tree = get_symbol_decl (d->vthis);
990
991 while (ad->isNested ())
992 {
993 Dsymbol *pd = ad->toParent2 ();
994 tree vthis_field = get_symbol_decl (ad->vthis);
995 this_tree = component_ref (build_deref (this_tree), vthis_field);
996
997 ad = pd->isAggregateDeclaration ();
998 if (ad == NULL)
999 {
499b0770 1000 d_function_chain->static_chain = this_tree;
b4c522fa
IB
1001 break;
1002 }
1003 }
1004 }
1005
b4c522fa
IB
1006 /* Named return value optimisation support for D.
1007 Implemented by overriding all the RETURN_EXPRs and replacing all
1008 occurrences of VAR with the RESULT_DECL for the function.
1009 This is only worth doing for functions that can return in memory. */
87e36d9b 1010 tree resdecl = DECL_RESULT (fndecl);
b4c522fa 1011
87e36d9b
IB
1012 if (TREE_ADDRESSABLE (TREE_TYPE (resdecl))
1013 || aggregate_value_p (TREE_TYPE (resdecl), fndecl))
b4c522fa 1014 {
6bebbc03
IB
1015 /* Return non-trivial structs by invisible reference. */
1016 if (TREE_ADDRESSABLE (TREE_TYPE (resdecl)))
1017 {
1018 TREE_TYPE (resdecl) = build_reference_type (TREE_TYPE (resdecl));
1019 DECL_BY_REFERENCE (resdecl) = 1;
1020 TREE_ADDRESSABLE (resdecl) = 0;
1021 relayout_decl (resdecl);
87e36d9b 1022 d->shidden = build_deref (resdecl);
6bebbc03 1023 }
87e36d9b
IB
1024 else
1025 d->shidden = resdecl;
b4c522fa 1026
235d5a96 1027 if (d->isNRVO () && d->nrvo_var)
b4c522fa
IB
1028 {
1029 tree var = get_symbol_decl (d->nrvo_var);
1030
1031 /* Copy name from VAR to RESULT. */
1032 DECL_NAME (resdecl) = DECL_NAME (var);
1033 /* Don't forget that we take its address. */
1034 TREE_ADDRESSABLE (var) = 1;
6bebbc03 1035
b4c522fa
IB
1036 SET_DECL_VALUE_EXPR (var, resdecl);
1037 DECL_HAS_VALUE_EXPR_P (var) = 1;
87e36d9b 1038 SET_DECL_LANG_NRVO (var, d->shidden);
b4c522fa
IB
1039 }
1040 }
1041
235d5a96
IB
1042 /* May change cfun->static_chain. */
1043 build_closure (d);
1044
1045 if (d->vresult)
1046 declare_local_var (d->vresult);
1047
1048 if (d->v_argptr)
1049 push_stmt_list ();
1050
b4c522fa
IB
1051 build_function_body (d);
1052
1053 /* Initialize the _argptr variable. */
1054 if (d->v_argptr)
1055 {
1056 tree body = pop_stmt_list ();
1057 tree var = get_decl_tree (d->v_argptr);
1058 var = build_address (var);
1059
1060 tree init = build_call_expr (builtin_decl_explicit (BUILT_IN_VA_START),
499b0770 1061 2, var, tree_last (param_list));
b4c522fa
IB
1062 declare_local_var (d->v_argptr);
1063 add_stmt (init);
1064
1065 tree cleanup = build_call_expr (builtin_decl_explicit (BUILT_IN_VA_END),
1066 1, var);
1067 add_stmt (build2 (TRY_FINALLY_EXPR, void_type_node, body, cleanup));
1068 }
1069
1070 finish_function (old_context);
1071
1072 /* Maybe record the function against the current module. */
1073 register_module_decl (d);
1074 }
1075};
1076
1077/* Main entry point for the DeclVisitor interface to send
1078 the Declaration AST class D to GCC back-end. */
1079
1080void
1081build_decl_tree (Dsymbol *d)
1082{
1083 location_t saved_location = input_location;
1084
1085 /* Set input location, empty DECL_SOURCE_FILE can crash debug generator. */
1086 if (d->loc.filename)
1087 input_location = make_location_t (d->loc);
1088 else
1089 input_location = make_location_t (Loc ("<no_file>", 1, 0));
1090
1091 DeclVisitor v = DeclVisitor ();
7edc15f2 1092 v.build_dsymbol (d);
b4c522fa
IB
1093
1094 input_location = saved_location;
1095}
1096
04284176
IB
1097/* Returns true if function FD always needs to be implicitly defined, such as
1098 it was declared `pragma(inline)'. */
1099
1100static bool
1101function_needs_inline_definition_p (FuncDeclaration *fd)
1102{
1103 /* Function has already been defined. */
1104 if (!DECL_EXTERNAL (fd->csym))
1105 return false;
1106
04284176
IB
1107 /* No function body available for inlining. */
1108 if (!fd->fbody)
1109 return false;
1110
04284176
IB
1111 /* These functions are tied to the module they are defined in. */
1112 if (fd->isFuncLiteralDeclaration ()
1113 || fd->isUnitTestDeclaration ()
1114 || fd->isFuncAliasDeclaration ()
1115 || fd->isInvariantDeclaration ())
1116 return false;
1117
1118 /* Check whether function will be regularly defined later in the current
1119 translation unit. */
9fe7d3de
IB
1120 Module *md = fd->getModule ();
1121 if (md && md->isRoot ())
04284176
IB
1122 return false;
1123
8db5b71e
IB
1124 /* Non-inlineable functions are always external. */
1125 if (DECL_UNINLINABLE (fd->csym))
1126 return false;
1127
1128 /* Ignore functions that aren't decorated with `pragma(inline)'. */
1129 if (!DECL_DECLARED_INLINE_P (fd->csym))
1130 return false;
1131
04284176
IB
1132 /* Weak functions cannot be inlined. */
1133 if (lookup_attribute ("weak", DECL_ATTRIBUTES (fd->csym)))
1134 return false;
1135
1136 /* Naked functions cannot be inlined. */
1137 if (lookup_attribute ("naked", DECL_ATTRIBUTES (fd->csym)))
1138 return false;
1139
1140 return true;
1141}
1142
8db5b71e
IB
1143/* If the variable or function declaration in DECL needs to be defined, add it
1144 to the list of deferred declarations to build later. */
04284176
IB
1145
1146static tree
1147maybe_build_decl_tree (Declaration *decl)
1148{
1149 gcc_assert (decl->csym != NULL_TREE);
1150
1151 /* Still running semantic analysis on declaration, or it has already had its
1152 code generated. */
1153 if (doing_semantic_analysis_p || decl->semanticRun >= PASS::obj)
1154 return decl->csym;
1155
1156 if (error_operand_p (decl->csym))
1157 return decl->csym;
1158
1159 if (FuncDeclaration *fd = decl->isFuncDeclaration ())
1160 {
1161 /* Externally defined inline functions need to be emitted. */
1162 if (function_needs_inline_definition_p (fd))
1163 {
1164 DECL_EXTERNAL (fd->csym) = 0;
8db5b71e 1165 d_defer_declaration (fd);
04284176
IB
1166 }
1167 }
1168
1169 return decl->csym;
1170}
1171
b4c522fa
IB
1172/* Return the decl for the symbol, create it if it doesn't already exist. */
1173
1174tree
1175get_symbol_decl (Declaration *decl)
1176{
1177 if (decl->csym)
04284176 1178 return maybe_build_decl_tree (decl);
b4c522fa
IB
1179
1180 /* Deal with placeholder symbols immediately:
1181 SymbolDeclaration is used as a shell around an initializer symbol. */
1182 SymbolDeclaration *sd = decl->isSymbolDeclaration ();
1183 if (sd)
1184 {
1185 decl->csym = aggregate_initializer_decl (sd->dsym);
1186 return decl->csym;
1187 }
1188
1189 /* Global static TypeInfo declaration. */
1190 if (decl->isTypeInfoDeclaration ())
1191 return get_typeinfo_decl ((TypeInfoDeclaration *) decl);
1192
1193 /* FuncAliasDeclaration is used to import functions from another scope. */
1194 FuncAliasDeclaration *fad = decl->isFuncAliasDeclaration ();
1195 if (fad)
1196 {
1197 decl->csym = get_symbol_decl (fad->funcalias);
1198 return decl->csym;
1199 }
1200
1201 /* It is possible for a field declaration symbol to be requested
1202 before the parent type has been built. */
1203 if (decl->isField ())
1204 {
1205 AggregateDeclaration *ad = decl->toParent ()->isAggregateDeclaration ();
1206 gcc_assert (ad != NULL);
1207
1208 /* Finishing off the type should create the associated FIELD_DECL. */
1209 build_ctype (ad->type);
1210 gcc_assert (decl->csym != NULL);
1211
1212 return decl->csym;
1213 }
1214
1215 /* Build the tree for the symbol. */
1216 FuncDeclaration *fd = decl->isFuncDeclaration ();
1217 if (fd)
1218 {
1219 /* Run full semantic on functions we need to know about. */
1220 if (!fd->functionSemantic ())
1221 {
1222 decl->csym = error_mark_node;
1223 return decl->csym;
1224 }
1225
1226 decl->csym = build_decl (make_location_t (decl->loc), FUNCTION_DECL,
1227 get_identifier (decl->ident->toChars ()),
1228 NULL_TREE);
1229
1230 /* Set function type afterwards as there could be self references. */
1231 TREE_TYPE (decl->csym) = build_ctype (fd->type);
1232
013fca64
IB
1233 /* Set DECL_INITIAL now if the function has a definition. */
1234 if (fd->fbody)
1235 DECL_INITIAL (decl->csym) = error_mark_node;
1236 else
b4c522fa
IB
1237 DECL_EXTERNAL (decl->csym) = 1;
1238 }
1239 else
1240 {
1241 /* Build the variable declaration. */
1242 VarDeclaration *vd = decl->isVarDeclaration ();
1243 gcc_assert (vd != NULL);
1244
1245 tree_code code = vd->isParameter () ? PARM_DECL
1246 : !vd->canTakeAddressOf () ? CONST_DECL
1247 : VAR_DECL;
1248 decl->csym = build_decl (make_location_t (decl->loc), code,
1249 get_identifier (decl->ident->toChars ()),
1250 declaration_type (vd));
1251
1252 /* If any alignment was set on the declaration. */
0fb57034 1253 if (!vd->alignment.isDefault ())
b4c522fa 1254 {
0fb57034 1255 SET_DECL_ALIGN (decl->csym, vd->alignment.get () * BITS_PER_UNIT);
b4c522fa
IB
1256 DECL_USER_ALIGN (decl->csym) = 1;
1257 }
1258
1259 if (vd->storage_class & STCextern)
1260 DECL_EXTERNAL (decl->csym) = 1;
36c9a3fe
IB
1261
1262 /* CONST_DECL was initially intended for enumerals and may be used for
1263 scalars in general, but not for aggregates. Here a non-constant
1264 value is generated anyway so as the CONST_DECL only serves as a
1265 placeholder for the value, however the DECL itself should never be
1266 referenced in any generated code, or passed to the back-end. */
1267 if (vd->storage_class & STCmanifest)
1268 {
1269 /* Cannot make an expression out of a void initializer. */
1270 if (vd->_init && !vd->_init->isVoidInitializer ())
1271 {
1272 Expression *ie = initializerToExpression (vd->_init);
1273
1274 if (!vd->type->isscalar ())
1275 DECL_INITIAL (decl->csym) = build_expr (ie, false);
1276 else
1277 DECL_INITIAL (decl->csym) = build_expr (ie, true);
1278 }
1279 }
61b1c562
IB
1280
1281 /* [type-qualifiers/const-and-immutable]
1282
1283 `immutable` applies to data that cannot change. Immutable data values,
1284 once constructed, remain the same for the duration of the program's
1285 execution. */
1286 if (vd->isImmutable () && !vd->setInCtorOnly ())
1287 TREE_READONLY (decl->csym) = 1;
1288
1289 /* `const` applies to data that cannot be changed by the const reference
1290 to that data. It may, however, be changed by another reference to that
1291 same data. */
1292 if (vd->isConst () && !vd->isDataseg ())
1293 TREE_READONLY (decl->csym) = 1;
b4c522fa
IB
1294 }
1295
1296 /* Set the declaration mangled identifier if static. */
1297 if (decl->isCodeseg () || decl->isDataseg ())
1298 {
1299 tree mangled_name;
1300
98866120
IB
1301 if (decl->mangleOverride.length)
1302 {
1303 mangled_name =
1304 get_identifier_with_length (decl->mangleOverride.ptr,
1305 decl->mangleOverride.length);
1306 }
b4c522fa 1307 else
b5446d0c 1308 mangled_name = get_identifier (d_mangle_decl (decl));
b4c522fa
IB
1309
1310 mangled_name = targetm.mangle_decl_assembler_name (decl->csym,
1311 mangled_name);
1312 /* The frontend doesn't handle duplicate definitions of unused symbols
1313 with the same mangle. So a check is done here instead. */
013fca64 1314 if (IDENTIFIER_DSYMBOL (mangled_name))
b4c522fa 1315 {
013fca64
IB
1316 Declaration *other = IDENTIFIER_DSYMBOL (mangled_name);
1317 tree olddecl = decl->csym;
1318 decl->csym = get_symbol_decl (other);
b4c522fa 1319
8e84a142
IB
1320 /* Update the symbol location to the current definition. */
1321 if (DECL_EXTERNAL (decl->csym) && !DECL_INITIAL (decl->csym))
1322 DECL_SOURCE_LOCATION (decl->csym) = DECL_SOURCE_LOCATION (olddecl);
1323
013fca64
IB
1324 /* The current declaration is a prototype or marked extern, merge
1325 applied user attributes and return. */
1326 if (DECL_EXTERNAL (olddecl) && !DECL_INITIAL (olddecl))
1327 {
1328 apply_user_attributes (decl, decl->csym);
b4c522fa
IB
1329 return decl->csym;
1330 }
013fca64
IB
1331 /* The previous declaration is a prototype or marked extern, set the
1332 current declaration as the main reference of the symbol. */
1333 else if (DECL_EXTERNAL (decl->csym) && !DECL_INITIAL (decl->csym))
1334 {
1335 IDENTIFIER_DSYMBOL (mangled_name) = decl;
1336 DECL_EXTERNAL (decl->csym) = 0;
1337 }
1338 /* Non-extern, non-templated decls shouldn't be defined twice. */
1339 else if (!decl->isInstantiated ())
1340 ScopeDsymbol::multiplyDefined (decl->loc, decl, other);
1341 }
1342 else
1343 {
b4c522fa
IB
1344 IDENTIFIER_PRETTY_NAME (mangled_name)
1345 = get_identifier (decl->toPrettyChars (true));
1346 IDENTIFIER_DSYMBOL (mangled_name) = decl;
b4c522fa 1347
013fca64
IB
1348 SET_DECL_ASSEMBLER_NAME (decl->csym, mangled_name);
1349 }
b4c522fa
IB
1350 }
1351
1352 DECL_LANG_SPECIFIC (decl->csym) = build_lang_decl (decl);
1353 DECL_CONTEXT (decl->csym) = d_decl_context (decl);
1354
1355 if (TREE_CODE (decl->csym) == PARM_DECL)
1356 {
1357 /* Pass non-trivial structs by invisible reference. */
1358 if (TREE_ADDRESSABLE (TREE_TYPE (decl->csym)))
1359 {
1360 tree argtype = build_reference_type (TREE_TYPE (decl->csym));
1361 argtype = build_qualified_type (argtype, TYPE_QUAL_RESTRICT);
1362 gcc_assert (!DECL_BY_REFERENCE (decl->csym));
1363 TREE_TYPE (decl->csym) = argtype;
1364 DECL_BY_REFERENCE (decl->csym) = 1;
1365 TREE_ADDRESSABLE (decl->csym) = 0;
1366 relayout_decl (decl->csym);
1367 decl->storage_class |= STCref;
1368 }
1369
1370 DECL_ARG_TYPE (decl->csym) = TREE_TYPE (decl->csym);
1371 gcc_assert (TREE_CODE (DECL_CONTEXT (decl->csym)) == FUNCTION_DECL);
1372 }
1373 else if (TREE_CODE (decl->csym) == CONST_DECL)
1374 {
1375 /* Manifest constants have no address in memory. */
1376 TREE_CONSTANT (decl->csym) = 1;
1377 TREE_READONLY (decl->csym) = 1;
1378 }
1379 else if (TREE_CODE (decl->csym) == FUNCTION_DECL)
1380 {
5fee5ec3
IB
1381 /* Dual-context functions require the code generation to build an array
1382 for the context pointer of the function, making the delicate task of
1383 tracking which context to follow when encountering a non-local symbol,
1384 and so are a not planned to be supported. */
1385 if (fd->needThis () && !fd->isMember2 ())
1386 {
1387 fatal_error (make_location_t (fd->loc),
1388 "function requires a dual-context, which is not yet "
1389 "supported by GDC");
1390 }
1391
b4c522fa
IB
1392 /* The real function type may differ from its declaration. */
1393 tree fntype = TREE_TYPE (decl->csym);
1394 tree newfntype = NULL_TREE;
1395
1396 if (fd->isNested ())
1397 {
1398 /* Add an extra argument for the frame/closure pointer, this is also
1399 required to be compatible with D delegates. */
1400 newfntype = build_vthis_function (void_type_node, fntype);
1401 }
1402 else if (fd->isThis ())
1403 {
cdbf48be 1404 /* Add an extra argument for the `this' parameter. The handle type is
b4c522fa
IB
1405 used even if there is no debug info. It is needed to make sure
1406 virtual member functions are not called statically. */
1407 AggregateDeclaration *ad = fd->isMember2 ();
1408 tree handle = build_ctype (ad->handleType ());
1409
1410 /* If handle is a pointer type, get record type. */
1411 if (!ad->isStructDeclaration ())
1412 handle = TREE_TYPE (handle);
1413
1414 newfntype = build_vthis_function (handle, fntype);
1415
1416 /* Set the vindex on virtual functions. */
1417 if (fd->isVirtual () && fd->vtblIndex != -1)
1418 {
1419 DECL_VINDEX (decl->csym) = size_int (fd->vtblIndex);
1420 DECL_VIRTUAL_P (decl->csym) = 1;
1421 }
213b7d9c
IB
1422
1423 /* Align method to the minimum boundary for target. */
1424 SET_DECL_ALIGN (decl->csym, MINIMUM_METHOD_BOUNDARY);
b4c522fa
IB
1425 }
1426 else if (fd->isMain () || fd->isCMain ())
1427 {
cdbf48be 1428 /* The main function is named `D main' to distinguish from C main. */
b4c522fa
IB
1429 if (fd->isMain ())
1430 DECL_NAME (decl->csym) = get_identifier (fd->toPrettyChars (true));
1431
cdbf48be 1432 /* `void main' is implicitly converted to returning an int. */
b4c522fa
IB
1433 newfntype = build_function_type (d_int_type, TYPE_ARG_TYPES (fntype));
1434 }
1435
1436 if (newfntype != NULL_TREE)
1437 {
1438 /* Copy the old attributes from the original type. */
1439 TYPE_ATTRIBUTES (newfntype) = TYPE_ATTRIBUTES (fntype);
1440 TYPE_LANG_SPECIFIC (newfntype) = TYPE_LANG_SPECIFIC (fntype);
1441 TREE_ADDRESSABLE (newfntype) = TREE_ADDRESSABLE (fntype);
1442 TREE_TYPE (decl->csym) = newfntype;
1443 d_keep (newfntype);
1444 }
1445
1446 /* Miscellaneous function flags. */
b4c522fa 1447
cdbf48be 1448 /* In [pragma/inline], functions decorated with `pragma(inline)' affects
67e3020b 1449 whether they are inlined or not. */
5fee5ec3 1450 if (fd->inlining == PINLINE::always)
67e3020b 1451 DECL_DECLARED_INLINE_P (decl->csym) = 1;
5fee5ec3 1452 else if (fd->inlining == PINLINE::never)
67e3020b
IB
1453 DECL_UNINLINABLE (decl->csym) = 1;
1454
0fb57034
IB
1455 /* In [pragma/crtctor], Annotates a function so it is run after the C
1456 runtime library is initialized and before the D runtime library is
1457 initialized. */
235d5a96 1458 if (fd->isCrtCtor ())
0fb57034
IB
1459 {
1460 DECL_STATIC_CONSTRUCTOR (decl->csym) = 1;
1461 decl_init_priority_insert (decl->csym, DEFAULT_INIT_PRIORITY);
1462 }
235d5a96 1463 else if (fd->isCrtDtor ())
0fb57034
IB
1464 {
1465 DECL_STATIC_DESTRUCTOR (decl->csym) = 1;
1466 decl_fini_priority_insert (decl->csym, DEFAULT_INIT_PRIORITY);
235d5a96 1467 }
0fb57034 1468
cdbf48be 1469 /* Function was declared `naked'. */
235d5a96 1470 if (fd->isNaked ())
b4c522fa
IB
1471 {
1472 insert_decl_attribute (decl->csym, "naked");
1473 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl->csym) = 1;
1474 }
1475
423d34f6
IB
1476 /* In [expression/function_literals], function literals (aka lambdas)
1477 enable embedding anonymous functions and anonymous delegates directly
1478 into expressions. They are defined in each referencing module. */
1479 if (fd->isFuncLiteralDeclaration ())
1480 DECL_SET_LAMBDA_FUNCTION (decl->csym, true);
1481
76a7e7e7 1482 /* Mark compiler generated functions as artificial. */
235d5a96 1483 if (fd->isGenerated ())
76a7e7e7
IB
1484 DECL_ARTIFICIAL (decl->csym) = 1;
1485
76a7e7e7
IB
1486 /* Ensure and require contracts are lexically nested in the function they
1487 part of, but are always publicly callable. */
b4c522fa
IB
1488 if (fd->ident == Identifier::idPool ("ensure")
1489 || fd->ident == Identifier::idPool ("require"))
76a7e7e7 1490 TREE_PUBLIC (decl->csym) = 1;
b4c522fa
IB
1491
1492 if (decl->storage_class & STCfinal)
1493 DECL_FINAL_P (decl->csym) = 1;
1494
5a0aa603 1495 /* Function is of type `noreturn' or `typeof(*null)'. */
0fb57034 1496 if (fd->type->nextOf ()->isTypeNoreturn ())
5a0aa603
IB
1497 TREE_THIS_VOLATILE (decl->csym) = 1;
1498
b4c522fa
IB
1499 /* Check whether this function is expanded by the frontend. */
1500 DECL_INTRINSIC_CODE (decl->csym) = INTRINSIC_NONE;
1501 maybe_set_intrinsic (fd);
1502
1503 /* For nested functions in particular, unnest fndecl in the cgraph, as
1504 all static chain passing is handled by the front-end. Do this even
1505 if we are not emitting the body. */
1506 struct cgraph_node *node = cgraph_node::get_create (decl->csym);
89576d86
JH
1507 if (nested_function_origin (node))
1508 unnest_function (node);
b4c522fa
IB
1509 }
1510
1511 /* Mark compiler generated temporaries as artificial. */
1512 if (decl->storage_class & STCtemp)
1513 DECL_ARTIFICIAL (decl->csym) = 1;
1514
1515 /* Propagate shared on the decl. */
1516 if (TYPE_SHARED (TREE_TYPE (decl->csym)))
1517 TREE_ADDRESSABLE (decl->csym) = 1;
1518
1519 /* Symbol was marked volatile. */
1520 if (decl->storage_class & STCvolatile)
1521 TREE_THIS_VOLATILE (decl->csym) = 1;
1522
91418c42
IB
1523 /* Symbol was marked register. */
1524 if (decl->storage_class & STCregister)
1525 DECL_REGISTER (decl->csym) = 1;
1526
1527 /* Symbol was declared with deprecated attribute. */
b4c522fa
IB
1528 if (decl->storage_class & STCdeprecated)
1529 TREE_DEPRECATED (decl->csym) = 1;
1530
b4c522fa
IB
1531 if (decl->isDataseg () || decl->isCodeseg () || decl->isThreadlocal ())
1532 {
1533 /* Set TREE_PUBLIC by default, but allow private template to override. */
1534 if (!fd || !fd->isNested ())
1535 TREE_PUBLIC (decl->csym) = 1;
1536
1537 TREE_STATIC (decl->csym) = 1;
1538 /* The decl has not been defined -- yet. */
1539 DECL_EXTERNAL (decl->csym) = 1;
1540
636b01ab
IB
1541 /* Visibility attributes are used by the debugger. */
1542 set_visibility_for_decl (decl->csym, decl);
1543
76a7e7e7
IB
1544 DECL_INSTANTIATED (decl->csym) = (decl->isInstantiated () != NULL);
1545 set_linkage_for_decl (decl->csym);
b4c522fa
IB
1546 }
1547
1548 /* Symbol is going in thread local storage. */
1549 if (decl->isThreadlocal () && !DECL_ARTIFICIAL (decl->csym))
1550 {
1551 if (global.params.vtls)
1552 message (decl->loc, "`%s` is thread local", decl->toChars ());
1553
1554 set_decl_tls_model (decl->csym, decl_default_tls_model (decl->csym));
1555 }
1556
1557 /* Apply any user attributes that may affect semantic meaning. */
013fca64 1558 apply_user_attributes (decl, decl->csym);
b4c522fa 1559
91418c42
IB
1560 /* Handle any conflicts between D language attributes and compiler-recognized
1561 * user attributes. */
1562 if (VAR_P (decl->csym) && DECL_HARD_REGISTER (decl->csym))
1563 {
1564 if (decl->storage_class & STCextern)
1565 error_at (make_location_t (decl->loc), "explicit register variable "
1566 "%qs declared %<extern%>", decl->toChars ());
1567 else if (decl->isThreadlocal ())
1568 error_at (make_location_t (decl->loc), "explicit register variable "
1569 "%qs declared thread local", decl->toChars ());
1570 }
1571
b4c522fa
IB
1572 /* %% Probably should be a little more intelligent about setting this. */
1573 TREE_USED (decl->csym) = 1;
1574 d_keep (decl->csym);
1575
04284176 1576 return maybe_build_decl_tree (decl);
b4c522fa
IB
1577}
1578
1579/* Returns a declaration for a VAR_DECL. Used to create compiler-generated
1580 global variables. */
1581
1582tree
1583declare_extern_var (tree ident, tree type)
1584{
1585 /* If the VAR_DECL has already been declared, return it. */
1586 if (IDENTIFIER_DECL_TREE (ident))
1587 return IDENTIFIER_DECL_TREE (ident);
1588
1589 tree name = IDENTIFIER_PRETTY_NAME (ident)
1590 ? IDENTIFIER_PRETTY_NAME (ident) : ident;
1591 tree decl = build_decl (input_location, VAR_DECL, name, type);
1592
1593 IDENTIFIER_DECL_TREE (ident) = decl;
1594 d_keep (decl);
1595
1596 SET_DECL_ASSEMBLER_NAME (decl, ident);
1597 DECL_ARTIFICIAL (decl) = 1;
1598 TREE_STATIC (decl) = 1;
1599 TREE_PUBLIC (decl) = 1;
1600
1601 /* The decl has not been defined -- yet. */
1602 DECL_EXTERNAL (decl) = 1;
1603
5b63eb17
IB
1604 set_linkage_for_decl (decl);
1605
b4c522fa
IB
1606 return decl;
1607}
1608
1609/* Add local variable VAR into the current function body. */
1610
1611void
1612declare_local_var (VarDeclaration *var)
1613{
1614 gcc_assert (!var->isDataseg () && !var->isMember ());
1615 gcc_assert (current_function_decl != NULL_TREE);
1616
1617 FuncDeclaration *fd = cfun->language->function;
1618 tree decl = get_symbol_decl (var);
1619
1620 gcc_assert (!TREE_STATIC (decl));
1621 d_pushdecl (decl);
1622 DECL_CONTEXT (decl) = current_function_decl;
1623
1624 /* Compiler generated symbols. */
1625 if (var == fd->vresult || var == fd->v_argptr)
1626 DECL_ARTIFICIAL (decl) = 1;
1627
1628 if (DECL_LANG_FRAME_FIELD (decl))
1629 {
1630 /* Fixes debugging local variables. */
1631 SET_DECL_VALUE_EXPR (decl, get_decl_tree (var));
1632 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1633 }
1634}
1635
1636/* Return an unnamed local temporary of type TYPE. */
1637
1638tree
1639build_local_temp (tree type)
1640{
ad4c44d2 1641 tree decl = create_tmp_var_raw (type);
b4c522fa
IB
1642 d_pushdecl (decl);
1643
1644 return decl;
1645}
1646
1647/* Return the correct decl to be used for DECL. For VAR_DECLs, this could
1648 instead be a FIELD_DECL from a closure, or a RESULT_DECL from a named return
1649 value. For PARM_DECLs, this could be a FIELD_DECL for a non-local `this'.
1650 For all other kinds of decls, this just returns the result of
1651 get_symbol_decl(). */
1652
1653tree
1654get_decl_tree (Declaration *decl)
1655{
1656 tree t = get_symbol_decl (decl);
1657 FuncDeclaration *fd = cfun ? cfun->language->function : NULL;
1658 VarDeclaration *vd = decl->isVarDeclaration ();
1659
1660 /* If cfun is NULL, then this is a global static. */
1661 if (vd == NULL || fd == NULL)
1662 return t;
1663
b4c522fa
IB
1664 /* Get the closure holding the var decl. */
1665 if (DECL_LANG_FRAME_FIELD (t))
1666 {
1667 FuncDeclaration *parent = vd->toParent2 ()->isFuncDeclaration ();
1668 tree frame_ref = get_framedecl (fd, parent);
1669
235d5a96
IB
1670 tree field = component_ref (build_deref (frame_ref),
1671 DECL_LANG_FRAME_FIELD (t));
1672 /* Frame field can also be a reference to the DECL_RESULT of a function.
1673 Dereference it to get the value. */
1674 if (DECL_LANG_NRVO (t))
1675 field = build_deref (field);
1676
1677 return field;
b4c522fa
IB
1678 }
1679
235d5a96
IB
1680 /* Get the named return value. */
1681 if (DECL_LANG_NRVO (t))
1682 return DECL_LANG_NRVO (t);
1683
cdbf48be 1684 /* Get the non-local `this' value by going through parent link
b4c522fa
IB
1685 of nested classes, this routine pretty much undoes what
1686 getRightThis in the frontend removes from codegen. */
1687 if (vd->parent != fd && vd->isThisDeclaration ())
1688 {
1689 /* Find the first parent that is a member function. */
1690 while (!fd->isMember2 ())
1691 {
1692 gcc_assert (fd->vthis);
1693 fd = fd->toParent2 ()->isFuncDeclaration ();
1694 gcc_assert (fd != NULL);
1695 }
1696
1697 AggregateDeclaration *ad = fd->isThis ();
1698 gcc_assert (ad != NULL);
1699
2b1c2a4b
IB
1700 /* The parent function is for the same `this' declaration we are
1701 building a chain to. Non-local declaration is inaccessible. */
1702 if (fd->vthis == vd)
1703 return error_no_frame_access (fd);
1704
b4c522fa
IB
1705 t = get_decl_tree (fd->vthis);
1706 Dsymbol *outer = fd;
1707
1708 while (outer != vd->parent)
1709 {
1710 gcc_assert (ad != NULL);
1711 outer = ad->toParent2 ();
1712
1713 /* Get the this->this parent link. */
1714 tree vfield = get_symbol_decl (ad->vthis);
1715 t = component_ref (build_deref (t), vfield);
1716
1717 ad = outer->isAggregateDeclaration ();
1718 if (ad != NULL)
1719 continue;
1720
1721 fd = outer->isFuncDeclaration ();
1722 while (fd != NULL)
1723 {
cdbf48be 1724 /* If outer function creates a closure, then the `this'
b4c522fa 1725 value would be the closure pointer, and the real
cdbf48be 1726 `this' the first field of that closure. */
b4c522fa
IB
1727 tree ff = get_frameinfo (fd);
1728 if (FRAMEINFO_CREATES_FRAME (ff))
1729 {
1730 t = build_nop (build_pointer_type (FRAMEINFO_TYPE (ff)), t);
1731 t = indirect_ref (build_ctype (fd->vthis->type), t);
1732 }
1733
1734 if (fd == vd->parent)
1735 break;
1736
1737 /* Continue looking for the right `this'. */
1738 outer = outer->toParent2 ();
1739 fd = outer->isFuncDeclaration ();
1740 }
1741
1742 ad = outer->isAggregateDeclaration ();
1743 }
1744
1745 return t;
1746 }
1747
1748 /* Auto variable that the back end will handle for us. */
1749 return t;
1750}
1751
1752/* Finish up a variable declaration and compile it all the way to
1753 the assembler language output. */
1754
1755void
1756d_finish_decl (tree decl)
1757{
1758 gcc_assert (!error_operand_p (decl));
1759
1760 /* We are sending this symbol to object file, can't be extern. */
1761 TREE_STATIC (decl) = 1;
1762 DECL_EXTERNAL (decl) = 0;
1763
1764 /* Update the TLS model as the linkage has been modified. */
1765 if (DECL_THREAD_LOCAL_P (decl))
1766 set_decl_tls_model (decl, decl_default_tls_model (decl));
1767
1768 relayout_decl (decl);
1769
1770 if (flag_checking && DECL_INITIAL (decl))
1771 {
1772 /* Initializer must never be bigger than symbol size. */
d6ec661e
IB
1773 HOST_WIDE_INT tsize = int_size_in_bytes (TREE_TYPE (decl));
1774 HOST_WIDE_INT dtsize =
1775 int_size_in_bytes (TREE_TYPE (DECL_INITIAL (decl)));
b4c522fa
IB
1776
1777 if (tsize < dtsize)
1778 {
1779 tree name = DECL_ASSEMBLER_NAME (decl);
1780
206222e0 1781 internal_error ("mismatch between declaration %qE size (%wd) and "
e97cfaa9 1782 "its initializer size (%wd)",
b4c522fa
IB
1783 IDENTIFIER_PRETTY_NAME (name)
1784 ? IDENTIFIER_PRETTY_NAME (name) : name,
1785 tsize, dtsize);
1786 }
1787 }
1788
1789 /* Without weak symbols, symbol should be put in .common, but that can't
1790 be done if there is a nonzero initializer. */
1791 if (DECL_COMDAT (decl) && DECL_COMMON (decl)
1792 && initializer_zerop (DECL_INITIAL (decl)))
1793 DECL_INITIAL (decl) = error_mark_node;
1794
1795 /* Add this decl to the current binding level. */
1796 d_pushdecl (decl);
1797
1798 rest_of_decl_compilation (decl, 1, 0);
1799}
1800
1801/* Thunk code is based on g++. */
1802
1803static int thunk_labelno;
1804
1805/* Create a static alias to function. */
1806
1807static tree
1808make_alias_for_thunk (tree function)
1809{
1810 tree alias;
1811 char buf[256];
1812
1813 /* Thunks may reference extern functions which cannot be aliased. */
1814 if (DECL_EXTERNAL (function))
1815 return function;
1816
1817 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
1818 thunk_labelno++;
1819
1820 alias = build_decl (DECL_SOURCE_LOCATION (function), FUNCTION_DECL,
1821 get_identifier (buf), TREE_TYPE (function));
1822 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (function);
1823 lang_hooks.dup_lang_specific_decl (alias);
1824 DECL_CONTEXT (alias) = NULL_TREE;
1825 TREE_READONLY (alias) = TREE_READONLY (function);
1826 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (function);
1827 TREE_PUBLIC (alias) = 0;
1828
1829 DECL_EXTERNAL (alias) = 0;
1830 DECL_ARTIFICIAL (alias) = 1;
1831
1832 DECL_DECLARED_INLINE_P (alias) = 0;
1833 DECL_INITIAL (alias) = error_mark_node;
1834 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (function));
1835
1836 TREE_ADDRESSABLE (alias) = 1;
1837 TREE_USED (alias) = 1;
1838 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
1839
1840 if (!flag_syntax_only)
1841 {
1842 cgraph_node *aliasn;
1843 aliasn = cgraph_node::create_same_body_alias (alias, function);
1844 gcc_assert (aliasn != NULL);
1845 }
1846 return alias;
1847}
1848
1849/* Emit the definition of a D vtable thunk. */
1850
1851static void
1852finish_thunk (tree thunk, tree function)
1853{
1854 /* Setup how D thunks are outputted. */
1855 int fixed_offset = -THUNK_LANG_OFFSET (thunk);
1856 bool this_adjusting = true;
1857 tree alias;
1858
1859 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
1860 alias = make_alias_for_thunk (function);
1861 else
1862 alias = function;
1863
1864 TREE_ADDRESSABLE (function) = 1;
1865 TREE_USED (function) = 1;
d1bddcaf 1866 DECL_EXTERNAL (thunk) = 0;
b4c522fa
IB
1867
1868 if (flag_syntax_only)
1869 {
1870 TREE_ASM_WRITTEN (thunk) = 1;
1871 return;
1872 }
1873
1874 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
1875 && targetm_common.have_named_sections)
1876 {
1877 tree fn = function;
1878 symtab_node *symbol = symtab_node::get (function);
1879
1880 if (symbol != NULL && symbol->alias)
1881 {
1882 if (symbol->analyzed)
1883 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
1884 else
1885 fn = symtab_node::get (function)->alias_target;
1886 }
1887 resolve_unique_section (fn, 0, flag_function_sections);
1888
1889 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
1890 {
1891 resolve_unique_section (thunk, 0, flag_function_sections);
1892
1893 /* Output the thunk into the same section as function. */
8b9a92f7 1894 set_decl_section_name (thunk, fn);
b4c522fa
IB
1895 symtab_node::get (thunk)->implicit_section
1896 = symtab_node::get (fn)->implicit_section;
1897 }
1898 }
1899
1900 /* Set up cloned argument trees for the thunk. */
1901 tree t = NULL_TREE;
1902 for (tree a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
1903 {
1904 tree x = copy_node (a);
1905 DECL_CHAIN (x) = t;
1906 DECL_CONTEXT (x) = thunk;
1907 SET_DECL_RTL (x, NULL);
1908 DECL_HAS_VALUE_EXPR_P (x) = 0;
1909 TREE_ADDRESSABLE (x) = 0;
1910 t = x;
1911 }
1912 DECL_ARGUMENTS (thunk) = nreverse (t);
1913 TREE_ASM_WRITTEN (thunk) = 1;
1914
1915 cgraph_node *funcn, *thunk_node;
1916
1917 funcn = cgraph_node::get_create (function);
1918 gcc_assert (funcn);
1919 thunk_node = funcn->create_thunk (thunk, thunk, this_adjusting,
1920 fixed_offset, 0, 0, 0, alias);
1921
1922 if (DECL_ONE_ONLY (function))
1923 thunk_node->add_to_same_comdat_group (funcn);
b4c522fa
IB
1924}
1925
1926/* Return a thunk to DECL. Thunks adjust the incoming `this' pointer by OFFSET.
1927 Adjustor thunks are created and pointers to them stored in the method entries
1928 in the vtable in order to set the this pointer to the start of the object
1929 instance corresponding to the implementing method. */
1930
1931tree
1932make_thunk (FuncDeclaration *decl, int offset)
1933{
1934 tree function = get_symbol_decl (decl);
1935
1936 if (!DECL_ARGUMENTS (function) || !DECL_RESULT (function))
1937 {
d1bddcaf
IB
1938 /* Build parameters for functions that are not being compiled,
1939 so that they can be correctly cloned in finish_thunk. */
1940 tree function = get_symbol_decl (decl);
1941 DECL_ARGUMENTS (function) = get_fndecl_arguments (decl);
1942
1943 /* Also build the result decl, which is needed when force creating
1944 the thunk in gimple inside cgraph_node::expand_thunk. */
1945 DECL_RESULT (function) = get_fndecl_result (decl);
b4c522fa
IB
1946 }
1947
1948 /* Don't build the thunk if the compilation step failed. */
1949 if (global.errors)
1950 return error_mark_node;
1951
1952 /* See if we already have the thunk in question. */
1953 for (tree t = DECL_LANG_THUNKS (function); t; t = DECL_CHAIN (t))
1954 {
1955 if (THUNK_LANG_OFFSET (t) == offset)
1956 return t;
1957 }
1958
1959 tree thunk = build_decl (DECL_SOURCE_LOCATION (function),
1960 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
1961 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
1962 lang_hooks.dup_lang_specific_decl (thunk);
1963 THUNK_LANG_OFFSET (thunk) = offset;
1964
1965 TREE_READONLY (thunk) = TREE_READONLY (function);
1966 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
1967 TREE_NOTHROW (thunk) = TREE_NOTHROW (function);
1968
1969 DECL_CONTEXT (thunk) = d_decl_context (decl);
1970
d1bddcaf 1971 /* Thunks inherit the public access of the function they are targeting. */
5d4b824f 1972 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
d1bddcaf
IB
1973 /* The thunk has not been defined -- yet. */
1974 DECL_EXTERNAL (thunk) = 1;
b4c522fa
IB
1975
1976 /* Thunks are always addressable. */
1977 TREE_ADDRESSABLE (thunk) = 1;
1978 TREE_USED (thunk) = 1;
1979 DECL_ARTIFICIAL (thunk) = 1;
1980 DECL_DECLARED_INLINE_P (thunk) = 0;
1981
e22b7ae1
IB
1982 if (TREE_PUBLIC (thunk))
1983 {
1984 DECL_VISIBILITY (thunk) = DECL_VISIBILITY (function);
1985 DECL_COMDAT (thunk) = DECL_COMDAT (function);
1986 DECL_WEAK (thunk) = DECL_WEAK (function);
1987 }
b4c522fa 1988
5d4b824f
IB
1989 /* When the thunk is for an extern C++ function, let C++ do the thunk
1990 generation and just reference the symbol as extern, instead of
1991 forcing a D local thunk to be emitted. */
1992 const char *ident;
1993
5eb9927a 1994 if (decl->resolvedLinkage () == LINK::cpp)
5d4b824f
IB
1995 ident = target.cpp.thunkMangle (decl, offset);
1996 else
1997 {
1998 tree target_name = DECL_ASSEMBLER_NAME (function);
1999 unsigned identlen = IDENTIFIER_LENGTH (target_name) + 14;
2000 ident = XNEWVEC (const char, identlen);
2001
2002 snprintf (CONST_CAST (char *, ident), identlen,
2003 "_DTi%u%s", offset, IDENTIFIER_POINTER (target_name));
2004 }
b4c522fa
IB
2005
2006 DECL_NAME (thunk) = get_identifier (ident);
2007 SET_DECL_ASSEMBLER_NAME (thunk, DECL_NAME (thunk));
2008
2009 d_keep (thunk);
5fee5ec3 2010
5eb9927a 2011 if (decl->resolvedLinkage () != LINK::cpp)
5fee5ec3 2012 free (CONST_CAST (char *, ident));
b4c522fa 2013
d1bddcaf
IB
2014 /* Thunks are connected to the definitions of the functions, so thunks are
2015 not produced for external functions. */
5d4b824f
IB
2016 if (!DECL_EXTERNAL (function))
2017 finish_thunk (thunk, function);
b4c522fa
IB
2018
2019 /* Add it to the list of thunks associated with the function. */
2020 DECL_LANG_THUNKS (thunk) = NULL_TREE;
2021 DECL_CHAIN (thunk) = DECL_LANG_THUNKS (function);
2022 DECL_LANG_THUNKS (function) = thunk;
2023
2024 return thunk;
2025}
2026
2027/* Create the FUNCTION_DECL for a function definition.
2028 This function creates a binding context for the function body
2029 as well as setting up the FUNCTION_DECL in current_function_decl.
2030 Returns the previous function context if it was already set. */
2031
2032tree
2033start_function (FuncDeclaration *fd)
2034{
2035 tree fndecl = get_symbol_decl (fd);
2036
423d34f6
IB
2037 /* Function has been defined. Whether we intend to send it to object file, or
2038 discard it has already been determined by set_linkage_for_decl. */
2039 DECL_EXTERNAL (fndecl) = 0;
b4c522fa
IB
2040 DECL_INITIAL (fndecl) = error_mark_node;
2041
2042 /* Add this decl to the current binding level. */
2043 d_pushdecl (fndecl);
2044
2045 /* Save the current function context. */
2046 tree old_context = current_function_decl;
2047
2048 if (old_context)
2049 push_function_context ();
2050
2051 /* Let GCC know the current scope is this function. */
2052 current_function_decl = fndecl;
2053
499b0770
IB
2054 /* Build the result decl before calling allocate_struct_function. */
2055 DECL_RESULT (fndecl) = get_fndecl_result (fd);
b4c522fa
IB
2056
2057 /* Initialize the RTL code for the function. */
2058 allocate_struct_function (fndecl, false);
2059
2060 /* Store the end of the function. */
2061 if (fd->endloc.filename)
2062 cfun->function_end_locus = make_location_t (fd->endloc);
2063 else
2064 cfun->function_end_locus = DECL_SOURCE_LOCATION (fndecl);
2065
af3c19f0 2066 cfun->language = ggc_cleared_alloc <language_function> ();
b4c522fa
IB
2067 cfun->language->function = fd;
2068
cdbf48be 2069 /* Default chain value is `null' unless parent found. */
b4c522fa
IB
2070 cfun->language->static_chain = null_pointer_node;
2071
2072 /* Find module for this function. */
2073 for (Dsymbol *p = fd->parent; p != NULL; p = p->parent)
2074 {
2075 cfun->language->module = p->isModule ();
2076 if (cfun->language->module)
2077 break;
2078 }
2079 gcc_assert (cfun->language->module != NULL);
2080
2081 /* Begin the statement tree for this function. */
2082 push_stmt_list ();
2083 push_binding_level (level_function);
2084
2085 return old_context;
2086}
2087
2088/* Finish up a function declaration and compile that function all
2089 the way to assembler language output. The free the storage for
2090 the function definition. Restores the previous function context. */
2091
2092void
2093finish_function (tree old_context)
2094{
2095 tree fndecl = current_function_decl;
2096
2097 /* Tie off the statement tree for this function. */
2098 tree block = pop_binding_level ();
2099 tree body = pop_stmt_list ();
2100 tree bind = build3 (BIND_EXPR, void_type_node,
2101 BLOCK_VARS (block), body, block);
2102
2103 gcc_assert (vec_safe_is_empty (d_function_chain->stmt_list));
2104
2105 /* Back-end expects a statement list to come from somewhere, however
2106 pop_stmt_list returns expressions when there is a single statement.
2107 So here we create a statement list unconditionally. */
2108 if (TREE_CODE (body) != STATEMENT_LIST)
2109 {
2110 tree stmtlist = alloc_stmt_list ();
2111 append_to_statement_list_force (body, &stmtlist);
2112 BIND_EXPR_BODY (bind) = stmtlist;
2113 }
2114 else if (!STATEMENT_LIST_HEAD (body))
2115 {
2116 /* For empty functions add a void return. */
2117 append_to_statement_list_force (return_expr (NULL_TREE), &body);
2118 }
2119
2120 DECL_SAVED_TREE (fndecl) = bind;
2121
d1bddcaf
IB
2122 /* Finish any forward referenced thunks for the function. */
2123 for (tree t = DECL_LANG_THUNKS (fndecl); t; t = DECL_CHAIN (t))
2124 finish_thunk (t, fndecl);
2125
b4c522fa
IB
2126 if (!errorcount && !global.errors)
2127 {
2128 /* Dump the D-specific tree IR. */
2129 dump_function (TDI_original, fndecl);
2130
2131 cgraph_node::finalize_function (fndecl, true);
2132 }
2133
2134 /* We're leaving the context of this function, so free it. */
2135 ggc_free (cfun->language);
2136 cfun->language = NULL;
2137 set_cfun (NULL);
2138
2139 if (old_context)
2140 pop_function_context ();
2141
2142 current_function_decl = old_context;
2143}
2144
2145/* Mark DECL, which is a VAR_DECL or FUNCTION_DECL as a symbol that
2146 must be emitted in this, output module. */
2147
bda51959
IB
2148static void
2149d_mark_needed (tree decl)
b4c522fa
IB
2150{
2151 TREE_USED (decl) = 1;
2152
2153 if (TREE_CODE (decl) == FUNCTION_DECL)
2154 {
2155 struct cgraph_node *node = cgraph_node::get_create (decl);
2156 node->forced_by_abi = true;
2157 }
2158 else if (VAR_P (decl))
2159 {
2160 struct varpool_node *node = varpool_node::get_create (decl);
2161 node->forced_by_abi = true;
2162 }
2163}
2164
b4c522fa
IB
2165/* Get the VAR_DECL of the vtable symbol for DECL. If this does not yet exist,
2166 create it. The vtable is accessible via ClassInfo, but since it is needed
2167 frequently (like for rtti comparisons), make it directly accessible. */
2168
2169tree
2170get_vtable_decl (ClassDeclaration *decl)
2171{
5fee5ec3
IB
2172 if (decl->vtblsym && decl->vtblsym->csym)
2173 return decl->vtblsym->csym;
b4c522fa
IB
2174
2175 tree ident = mangle_internal_decl (decl, "__vtbl", "Z");
2176 /* Note: Using a static array type for the VAR_DECL, the DECL_INITIAL value
2177 will have a different type. However the back-end seems to accept this. */
2cbc99d1 2178 tree type = build_ctype (Type::tvoidptr->sarrayOf (decl->vtbl.length));
b4c522fa 2179
5fee5ec3
IB
2180 Dsymbol *vtblsym = decl->vtblSymbol ();
2181 vtblsym->csym = declare_extern_var (ident, type);
2182 DECL_LANG_SPECIFIC (vtblsym->csym) = build_lang_decl (NULL);
b4c522fa
IB
2183
2184 /* Class is a reference, want the record type. */
5fee5ec3
IB
2185 DECL_CONTEXT (vtblsym->csym) = TREE_TYPE (build_ctype (decl->type));
2186 TREE_READONLY (vtblsym->csym) = 1;
2187 DECL_VIRTUAL_P (vtblsym->csym) = 1;
b4c522fa 2188
5fee5ec3
IB
2189 SET_DECL_ALIGN (vtblsym->csym, TARGET_VTABLE_ENTRY_ALIGN);
2190 DECL_USER_ALIGN (vtblsym->csym) = true;
b4c522fa 2191
5fee5ec3 2192 return vtblsym->csym;
b4c522fa
IB
2193}
2194
2195/* Helper function of build_class_instance. Find the field inside aggregate
2196 TYPE identified by IDENT at field OFFSET. */
2197
2198static tree
2199find_aggregate_field (tree type, tree ident, tree offset)
2200{
2201 tree fields = TYPE_FIELDS (type);
2202
2203 for (tree field = fields; field != NULL_TREE; field = TREE_CHAIN (field))
2204 {
2205 if (DECL_NAME (field) == NULL_TREE
2206 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
2207 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2208 {
2209 /* Search nesting anonymous structs and unions. */
2210 tree vfield = find_aggregate_field (TREE_TYPE (field),
2211 ident, offset);
2212 if (vfield != NULL_TREE)
2213 return vfield;
2214 }
2215 else if (DECL_NAME (field) == ident
2216 && (offset == NULL_TREE
2217 || DECL_FIELD_OFFSET (field) == offset))
2218 {
2219 /* Found matching field at offset. */
2220 return field;
2221 }
2222 }
2223
2224 return NULL_TREE;
2225}
2226
2227/* Helper function of build_new_class_expr. Return a constructor that matches
2228 the layout of the class expression EXP. */
2229
2230static tree
2231build_class_instance (ClassReferenceExp *exp)
2232{
2233 ClassDeclaration *cd = exp->originalClass ();
2234 tree type = TREE_TYPE (build_ctype (exp->value->stype));
af3c19f0 2235 vec <constructor_elt, va_gc> *ve = NULL;
b4c522fa
IB
2236
2237 /* The set base vtable field. */
2238 tree vptr = build_address (get_vtable_decl (cd));
2239 CONSTRUCTOR_APPEND_ELT (ve, TYPE_FIELDS (type), vptr);
2240
2241 /* Go through the inheritance graph from top to bottom. This will add all
2242 values to the constructor out of order, however build_struct_literal
2243 will re-order all values before returning the finished literal. */
2244 for (ClassDeclaration *bcd = cd; bcd != NULL; bcd = bcd->baseClass)
2245 {
2246 /* Anonymous vtable interface fields are laid out before the fields of
2247 each class. The interface offset is used to determine where to put
2248 the classinfo offset reference. */
2cbc99d1 2249 for (size_t i = 0; i < bcd->vtblInterfaces->length; i++)
b4c522fa
IB
2250 {
2251 BaseClass *bc = (*bcd->vtblInterfaces)[i];
2252
2253 for (ClassDeclaration *cd2 = cd; 1; cd2 = cd2->baseClass)
2254 {
2255 gcc_assert (cd2 != NULL);
2256
2257 unsigned csymoffset = base_vtable_offset (cd2, bc);
2258 /* If the base class vtable was found. */
2259 if (csymoffset != ~0u)
2260 {
2261 tree csym = build_address (get_classinfo_decl (cd2));
2262 csym = build_offset (csym, size_int (csymoffset));
2263
2264 tree field = find_aggregate_field (type, NULL_TREE,
2265 size_int (bc->offset));
2266 gcc_assert (field != NULL_TREE);
2267
2268 CONSTRUCTOR_APPEND_ELT (ve, field, csym);
2269 break;
2270 }
2271 }
2272 }
2273
2274 /* Generate initial values of all fields owned by current class.
2275 Use both the name and offset to find the right field. */
2cbc99d1 2276 for (size_t i = 0; i < bcd->fields.length; i++)
b4c522fa
IB
2277 {
2278 VarDeclaration *vfield = bcd->fields[i];
2279 int index = exp->findFieldIndexByName (vfield);
2280 gcc_assert (index != -1);
2281
2282 Expression *value = (*exp->value->elements)[index];
2283 if (!value)
2284 continue;
2285
2286 /* Use find_aggregate_field to get the overridden field decl,
2287 instead of the field associated with the base class. */
2288 tree field = get_symbol_decl (bcd->fields[i]);
2289 field = find_aggregate_field (type, DECL_NAME (field),
2290 DECL_FIELD_OFFSET (field));
2291 gcc_assert (field != NULL_TREE);
2292
2293 CONSTRUCTOR_APPEND_ELT (ve, field, build_expr (value, true));
2294 }
2295 }
2296
2297 return build_struct_literal (type, ve);
2298}
2299
2300/* Get the VAR_DECL of a class instance representing EXPR as static data.
2301 If this does not yet exist, create it. This is used to support initializing
2302 a static variable that is of a class type using values known during CTFE.
2303 In user code, it is analogous to the following code snippet.
2304
2305 enum E = new C(1, 2, 3);
2306
2307 That we write the contents of `C(1, 2, 3)' to static data is only a compiler
2308 implementation detail. The initialization of these symbols could be done at
2309 run-time using during as part of the module initialization or shared static
2310 constructors phase of run-time start-up - whichever comes after `gc_init()'.
2311 And infact that would be the better thing to do here eventually. */
2312
2313tree
2314build_new_class_expr (ClassReferenceExp *expr)
2315{
2316 if (expr->value->sym)
2317 return expr->value->sym;
2318
2319 /* Build the reference symbol. */
2320 tree type = build_ctype (expr->value->stype);
2321 expr->value->sym = build_artificial_decl (TREE_TYPE (type), NULL_TREE, "C");
2322
2323 DECL_INITIAL (expr->value->sym) = build_class_instance (expr);
2324 d_pushdecl (expr->value->sym);
2325 rest_of_decl_compilation (expr->value->sym, 1, 0);
2326
2327 return expr->value->sym;
2328}
2329
2330/* Get the VAR_DECL of the static initializer symbol for the struct/class DECL.
2331 If this does not yet exist, create it. The static initializer data is
cdbf48be 2332 accessible via TypeInfo, and is also used in `new class' and default
b4c522fa
IB
2333 initializing struct literals. */
2334
2335tree
2336aggregate_initializer_decl (AggregateDeclaration *decl)
2337{
2338 if (decl->sinit)
99d6d3d9 2339 return (tree) decl->sinit;
b4c522fa
IB
2340
2341 /* Class is a reference, want the record type. */
2342 tree type = build_ctype (decl->type);
2343 StructDeclaration *sd = decl->isStructDeclaration ();
2344 if (!sd)
2345 type = TREE_TYPE (type);
2346
2347 tree ident = mangle_internal_decl (decl, "__init", "Z");
2348
99d6d3d9
IB
2349 tree sinit = declare_extern_var (ident, type);
2350 DECL_LANG_SPECIFIC (sinit) = build_lang_decl (NULL);
b4c522fa 2351
99d6d3d9
IB
2352 DECL_CONTEXT (sinit) = type;
2353 TREE_READONLY (sinit) = 1;
b4c522fa
IB
2354
2355 /* Honor struct alignment set by user. */
0fb57034 2356 if (sd && !sd->alignment.isDefault ())
b4c522fa 2357 {
0fb57034 2358 SET_DECL_ALIGN (sinit, sd->alignment.get () * BITS_PER_UNIT);
99d6d3d9 2359 DECL_USER_ALIGN (sinit) = true;
b4c522fa
IB
2360 }
2361
99d6d3d9
IB
2362 decl->sinit = sinit;
2363 return sinit;
b4c522fa
IB
2364}
2365
2366/* Generate the data for the static initializer. */
2367
2368tree
2369layout_class_initializer (ClassDeclaration *cd)
2370{
6384eff5 2371 NewExp *ne = NewExp::create (cd->loc, NULL, cd->type, NULL);
b4c522fa
IB
2372 ne->type = cd->type;
2373
2374 Expression *e = ne->ctfeInterpret ();
9c7d5e88 2375 gcc_assert (e->op == EXP::classReference);
b4c522fa 2376
d873350a 2377 return build_class_instance (e->isClassReferenceExp ());
b4c522fa
IB
2378}
2379
2380tree
2381layout_struct_initializer (StructDeclaration *sd)
2382{
2383 StructLiteralExp *sle = StructLiteralExp::create (sd->loc, sd, NULL);
2384
c8dfa79c 2385 if (!sd->fill (sd->loc, *sle->elements, true))
b4c522fa
IB
2386 gcc_unreachable ();
2387
2388 sle->type = sd->type;
2389 return build_expr (sle, true);
2390}
2391
2392/* Get the VAR_DECL of the static initializer symbol for the enum DECL.
2393 If this does not yet exist, create it. The static initializer data is
2394 accessible via TypeInfo_Enum, but the field member type is a byte[] that
2395 requires a pointer to a symbol reference. */
2396
2397tree
2398enum_initializer_decl (EnumDeclaration *decl)
2399{
2400 if (decl->sinit)
2401 return decl->sinit;
2402
370f66b6 2403 gcc_assert (decl->ident);
b4c522fa 2404
370f66b6 2405 tree type = build_ctype (decl->type);
b4c522fa 2406 tree ident = mangle_internal_decl (decl, "__init", "Z");
b4c522fa
IB
2407
2408 decl->sinit = declare_extern_var (ident, type);
2409 DECL_LANG_SPECIFIC (decl->sinit) = build_lang_decl (NULL);
2410
2411 DECL_CONTEXT (decl->sinit) = d_decl_context (decl);
2412 TREE_READONLY (decl->sinit) = 1;
2413
2414 return decl->sinit;
2415}
2416
2417/* Return an anonymous static variable of type TYPE, initialized with INIT,
2418 and optionally prefixing the name with PREFIX. */
2419
2420tree
2421build_artificial_decl (tree type, tree init, const char *prefix)
2422{
2423 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, NULL_TREE, type);
2424 const char *name = prefix ? prefix : "___s";
2425 char *label;
2426
2427 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
2428 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
2429 DECL_NAME (decl) = DECL_ASSEMBLER_NAME (decl);
2430
2431 TREE_PUBLIC (decl) = 0;
2432 TREE_STATIC (decl) = 1;
2433 TREE_USED (decl) = 1;
2434 DECL_IGNORED_P (decl) = 1;
2435 DECL_ARTIFICIAL (decl) = 1;
2436
2437 /* Perhaps at some point the initializer constant should be hashed
2438 to remove duplicates. */
2439 DECL_INITIAL (decl) = init;
2440
2441 return decl;
2442}
2443
2444/* Build TYPE_DECL for the declaration DSYM. */
2445
2446void
2447build_type_decl (tree type, Dsymbol *dsym)
2448{
2449 if (TYPE_STUB_DECL (type))
2450 return;
2451
9dddefef
IB
2452 /* If a templated type, use the template instance name, as that includes all
2453 template parameters. */
2454 const char *name = dsym->parent->isTemplateInstance ()
2455 ? ((TemplateInstance *) dsym->parent)->toChars () : dsym->ident->toChars ();
2456
b4c522fa 2457 tree decl = build_decl (make_location_t (dsym->loc), TYPE_DECL,
9dddefef 2458 get_identifier (name), type);
b5446d0c 2459 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (d_mangle_decl (dsym)));
b4c522fa 2460 TREE_PUBLIC (decl) = 1;
b4c522fa
IB
2461 DECL_CONTEXT (decl) = d_decl_context (dsym);
2462
2463 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
2464 TYPE_NAME (type) = decl;
2465
2466 /* Not sure if there is a need for separate TYPE_DECLs in
2467 TYPE_NAME and TYPE_STUB_DECL. */
2468 if (TREE_CODE (type) == ENUMERAL_TYPE || RECORD_OR_UNION_TYPE_P (type))
e4011c13
IB
2469 {
2470 DECL_ARTIFICIAL (decl) = 1;
2471 TYPE_STUB_DECL (type) = decl;
2472 }
2473 else if (type != TYPE_MAIN_VARIANT (type))
2474 DECL_ORIGINAL_TYPE (decl) = TYPE_MAIN_VARIANT (type);
b4c522fa
IB
2475}
2476
2477/* Create a declaration for field NAME of a given TYPE, setting the flags
2478 for whether the field is ARTIFICIAL and/or IGNORED. */
2479
2480tree
2481create_field_decl (tree type, const char *name, int artificial, int ignored)
2482{
2483 tree decl = build_decl (input_location, FIELD_DECL,
2484 name ? get_identifier (name) : NULL_TREE, type);
2485 DECL_ARTIFICIAL (decl) = artificial;
2486 DECL_IGNORED_P (decl) = ignored;
2487
2488 return decl;
2489}
2490
2491/* Return the COMDAT group into which DECL should be placed. */
2492
2493static tree
2494d_comdat_group (tree decl)
2495{
2496 /* If already part of a comdat group, use that. */
2497 if (DECL_COMDAT_GROUP (decl))
2498 return DECL_COMDAT_GROUP (decl);
2499
2500 return DECL_ASSEMBLER_NAME (decl);
2501}
2502
2503/* Set DECL up to have the closest approximation of "initialized common"
2504 linkage available. */
2505
76a7e7e7 2506static void
b4c522fa
IB
2507d_comdat_linkage (tree decl)
2508{
76a7e7e7
IB
2509 /* COMDAT definitions have to be public. */
2510 gcc_assert (TREE_PUBLIC (decl));
2511
2512 if (supports_one_only ())
b4c522fa 2513 make_decl_one_only (decl, d_comdat_group (decl));
76a7e7e7 2514 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INSTANTIATED (decl))
b4c522fa
IB
2515 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2516 /* We can just emit function and compiler-generated variables statically;
2517 having multiple copies is (for the most part) only a waste of space. */
2518 TREE_PUBLIC (decl) = 0;
2519 else if (DECL_INITIAL (decl) == NULL_TREE
2520 || DECL_INITIAL (decl) == error_mark_node)
2521 /* Fallback, cannot have multiple copies. */
2522 DECL_COMMON (decl) = 1;
2523
76a7e7e7 2524 if (TREE_PUBLIC (decl) && DECL_INSTANTIATED (decl))
b4c522fa
IB
2525 DECL_COMDAT (decl) = 1;
2526}
2527
76a7e7e7 2528/* Set DECL up to have the closest approximation of "weak" linkage. */
b4c522fa 2529
76a7e7e7
IB
2530static void
2531d_weak_linkage (tree decl)
b4c522fa
IB
2532{
2533 /* Weak definitions have to be public. */
76a7e7e7
IB
2534 gcc_assert (TREE_PUBLIC (decl));
2535
2536 /* Allow comdat linkage to be forced with the flag `-fno-weak-templates'. */
2537 if (!flag_weak_templates || !TARGET_SUPPORTS_WEAK)
2538 return d_comdat_linkage (decl);
2539
2540 declare_weak (decl);
2541}
2542
2543/* DECL is a FUNCTION_DECL or a VAR_DECL with static storage. Set flags to
2544 reflect the linkage that DECL will receive in the object file. */
2545
2546void
2547set_linkage_for_decl (tree decl)
2548{
2549 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl) && TREE_STATIC (decl));
2550
2551 /* Non-public decls keep their internal linkage. */
b4c522fa
IB
2552 if (!TREE_PUBLIC (decl))
2553 return;
2554
423d34f6
IB
2555 /* Function literals and functions declared as `pragma(inline, true)' can
2556 appear in multiple translation units. */
2557 if (TREE_CODE (decl) == FUNCTION_DECL
2558 && (DECL_DECLARED_INLINE_P (decl) || DECL_LAMBDA_FUNCTION_P (decl)))
76a7e7e7 2559 return d_comdat_linkage (decl);
b4c522fa 2560
04284176
IB
2561 /* Don't need to give private or protected symbols a special linkage. */
2562 if ((TREE_PRIVATE (decl) || TREE_PROTECTED (decl))
2563 && !DECL_INSTANTIATED (decl))
2564 return;
2565
bda51959
IB
2566 /* If all instantiations must go in COMDAT, give them that linkage.
2567 This also applies to other extern declarations, so that it is possible
2568 for them to override template declarations. */
2569 if (targetdm.d_templates_always_comdat)
2570 {
2571 /* Make sure that instantiations are not removed. */
2572 if (flag_weak_templates && DECL_INSTANTIATED (decl))
2573 d_mark_needed (decl);
2574
2575 return d_comdat_linkage (decl);
2576 }
2577
76a7e7e7
IB
2578 /* Instantiated variables and functions need to be overridable by any other
2579 symbol with the same name, so give them weak linkage. */
2580 if (DECL_INSTANTIATED (decl))
2581 return d_weak_linkage (decl);
b4c522fa 2582
76a7e7e7
IB
2583 /* Compiler generated public symbols can appear in multiple contexts. */
2584 if (DECL_ARTIFICIAL (decl))
2585 return d_weak_linkage (decl);
b4c522fa 2586}
636b01ab
IB
2587
2588/* NODE is a FUNCTION_DECL, VAR_DECL or RECORD_TYPE for the declaration SYM.
2589 Set flags to reflect visibility that NODE will get in the object file. */
2590
2591void
2592set_visibility_for_decl (tree node, Dsymbol *sym)
2593{
2594 Visibility visibility = sym->visible ();
2595 if (visibility.kind == Visibility::private_)
2596 TREE_PRIVATE (node) = 1;
2597 else if (visibility.kind == Visibility::protected_)
2598 TREE_PROTECTED (node) = 1;
2599
2600 /* If the declaration was declared `export', append either the dllimport
2601 or dllexport attribute. */
2602 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
2603 {
2604 const char *attrname = NULL;
2605
2606 /* Have to test for import first. */
2607 if (sym->isImportedSymbol ())
2608 attrname = "dllimport";
2609 else if (sym->isExport ())
2610 attrname = "dllexport";
2611
2612 if (attrname != NULL)
2613 {
2614 if (DECL_P (node))
2615 insert_decl_attribute (node, attrname);
2616 else if (TYPE_P (node))
2617 insert_type_attribute (node, attrname);
2618 }
2619 }
2620}