Debian Codegraph @

gcc/d/decl.cc

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