1 /* decl.cc -- Lower D frontend declarations to GCC trees.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
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)
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.
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/>. */
20 #include "coretypes.h"
22 #include "dmd/aggregate.h"
23 #include "dmd/attrib.h"
26 #include "dmd/declaration.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"
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"
41 #include "tree-iterator.h"
42 #include "fold-const.h"
43 #include "diagnostic.h"
44 #include "langhooks.h"
46 #include "common/common-target.h"
49 #include "stringpool.h"
51 #include "stor-layout.h"
55 #include "tree-pretty-print.h"
56 #include "tree-nested.h"
57 #include "alloc-pool.h"
58 #include "symbol-summary.h"
59 #include "symtab-thunks.h"
60 #include "gimple-expr.h"
66 /* Return identifier for the external mangled name of DECL. */
69 d_mangle_decl (Dsymbol
*decl
)
71 if (decl
->isFuncDeclaration ())
72 return mangleExact ((FuncDeclaration
*) decl
);
76 mangleToBuffer (decl
, &buf
);
77 return buf
.extractChars ();
81 /* Generate a mangled identifier using NAME and SUFFIX, prefixed by the
82 assembler name for DECL. */
85 mangle_internal_decl (Dsymbol
*decl
, const char *name
, const char *suffix
)
87 const char *prefix
= d_mangle_decl (decl
);
88 unsigned namelen
= strlen (name
);
89 unsigned buflen
= (2 + strlen (prefix
) + namelen
+ strlen (suffix
)) * 2;
90 char *buf
= (char *) alloca (buflen
);
92 snprintf (buf
, buflen
, "_D%s%u%s%s", prefix
, namelen
, name
, suffix
);
93 tree ident
= get_identifier (buf
);
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
);
103 /* Returns true if DECL is from the gcc.attributes module. */
106 gcc_attribute_p (Dsymbol
*decl
)
108 ModuleDeclaration
*md
= decl
->getModule ()->md
;
110 if (md
&& md
->packages
.length
== 1)
112 if (!strcmp (md
->packages
.ptr
[0]->toChars (), "gcc")
113 && !strcmp (md
->id
->toChars (), "attributes"))
120 /* Return the DECL_RESULT for the function declaration DECL, create it if it
121 doesn't already exist. */
124 get_fndecl_result (FuncDeclaration
*decl
)
126 tree fndecl
= get_symbol_decl (decl
);
127 tree resdecl
= DECL_RESULT (fndecl
);
129 if (resdecl
!= NULL_TREE
)
132 resdecl
= build_decl (make_location_t (decl
->loc
), RESULT_DECL
,
133 NULL_TREE
, TREE_TYPE (TREE_TYPE (fndecl
)));
135 DECL_ARTIFICIAL (resdecl
) = 1;
136 DECL_IGNORED_P (resdecl
) = 1;
137 DECL_CONTEXT (resdecl
) = fndecl
;
138 DECL_RESULT (fndecl
) = resdecl
;
142 /* Return the list of PARAM_DECLs for the function declaration DECL, create it
143 if it doesn't already exist. */
146 get_fndecl_arguments (FuncDeclaration
*decl
)
148 tree fndecl
= get_symbol_decl (decl
);
149 tree param_list
= DECL_ARGUMENTS (fndecl
);
151 if (param_list
!= NULL_TREE
)
156 /* Handle special arguments first. */
159 For nested functions, D still generates a vthis, but it
160 should not be referenced in any expression. */
163 tree parm_decl
= get_symbol_decl (decl
->vthis
);
164 DECL_ARTIFICIAL (parm_decl
) = 1;
165 TREE_READONLY (parm_decl
) = 1;
167 if (decl
->vthis
->type
== Type::tvoidptr
)
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
);
176 param_list
= chainon (param_list
, parm_decl
);
179 /* `_arguments' parameter. */
180 if (decl
->v_arguments
)
182 tree parm_decl
= get_symbol_decl (decl
->v_arguments
);
183 param_list
= chainon (param_list
, parm_decl
);
186 /* Now add on formal function parameters. */
187 size_t n_parameters
= decl
->parameters
? decl
->parameters
->length
: 0;
189 for (size_t i
= 0; i
< n_parameters
; i
++)
191 VarDeclaration
*param
= (*decl
->parameters
)[i
];
192 tree parm_decl
= get_symbol_decl (param
);
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
)
199 /* Chain them in the correct order. */
200 param_list
= chainon (param_list
, parm_decl
);
205 /* Build parameters from the function type. */
206 tree fntype
= TREE_TYPE (fndecl
);
208 for (tree t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
210 if (t
== void_list_node
)
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
);
223 DECL_ARGUMENTS (fndecl
) = param_list
;
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(). */
234 class DeclVisitor
: public Visitor
236 using Visitor::visit
;
238 /* If we're lowering the body of a version(unittest) condition. */
239 bool in_version_unittest_
;
244 this->in_version_unittest_
= false;
247 /* Helper for generating code for the dsymbol AST class D.
248 Sets up the location of the symbol before lowering. */
250 void build_dsymbol (Dsymbol
*d
)
252 location_t saved_location
= input_location
;
253 input_location
= make_location_t (d
->loc
);
255 input_location
= saved_location
;
258 /* This should be overridden by each declaration class. */
260 void visit (Dsymbol
*) final override
264 /* Compile a D module, and all members of it. */
266 void visit (Module
*d
) final override
268 if (d
->semanticRun
>= PASS::obj
)
271 build_module_tree (d
);
272 d
->semanticRun
= PASS::obj
;
275 /* Write the imported symbol to debug. */
277 void visit (Import
*d
) final override
279 if (d
->semanticRun
>= PASS::obj
)
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. */
289 /* Get the context of this import, this should never be null. */
290 tree context
= d_module_context ();
292 if (d
->ident
== NULL
)
294 /* Importing declaration list. */
295 for (size_t i
= 0; i
< d
->names
.length
; i
++)
297 AliasDeclaration
*aliasdecl
= d
->aliasdecls
[i
];
298 tree decl
= build_import_decl (aliasdecl
);
300 /* Skip over unhandled imports. */
301 if (decl
== NULL_TREE
)
304 Identifier
*alias
= d
->aliases
[i
];
305 tree name
= (alias
!= NULL
)
306 ? get_identifier (alias
->toChars ()) : NULL_TREE
;
308 if (TREE_CODE (decl
) != TREE_LIST
)
309 debug_hooks
->imported_module_or_decl (decl
, name
, context
,
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);
322 /* Importing the entire module. */
323 tree decl
= build_import_decl (d
->mod
);
325 tree name
= (d
->aliasId
!= NULL
)
326 ? get_identifier (d
->aliasId
->toChars ()) : NULL_TREE
;
328 debug_hooks
->imported_module_or_decl (decl
, name
, context
,
332 d
->semanticRun
= PASS::obj
;
335 /* Expand any local variables found in tuples. */
337 void visit (TupleDeclaration
*d
) final override
339 for (size_t i
= 0; i
< d
->objects
->length
; i
++)
341 RootObject
*o
= (*d
->objects
)[i
];
342 if (o
->dyncast () == DYNCAST_EXPRESSION
)
344 VarExp
*ve
= ((Expression
*) o
)->isVarExp ();
346 this->build_dsymbol (ve
->var
);
351 /* Walk over all declarations in the attribute scope. */
353 void visit (AttribDeclaration
*d
) final override
355 Dsymbols
*ds
= d
->include (NULL
);
360 for (size_t i
= 0; i
< ds
->length
; i
++)
361 this->build_dsymbol ((*ds
)[i
]);
364 /* Pragmas are a way to pass special information to the compiler and to add
365 vendor specific extensions to D. */
367 void visit (PragmaDeclaration
*d
) final override
369 if (d
->ident
== Identifier::idPool ("lib")
370 || d
->ident
== Identifier::idPool ("startaddress"))
372 if (!global
.params
.ignoreUnsupportedPragmas
)
374 warning_at (make_location_t (d
->loc
), OPT_Wunknown_pragmas
,
375 "pragma(%s) not implemented", d
->ident
->toChars ());
379 visit ((AttribDeclaration
*) d
);
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 */
385 void visit (ConditionalDeclaration
*d
) final override
387 bool old_condition
= this->in_version_unittest_
;
389 if (global
.params
.useUnitTests
)
391 VersionCondition
*vc
= d
->condition
->isVersionCondition ();
392 if (vc
&& vc
->ident
== Identifier::idPool ("unittest"))
393 this->in_version_unittest_
= true;
396 visit ((AttribDeclaration
*) d
);
398 this->in_version_unittest_
= old_condition
;
401 /* Walk over all members in the namespace scope. */
403 void visit (Nspace
*d
) final override
405 if (isError (d
) || !d
->members
)
408 for (size_t i
= 0; i
< d
->members
->length
; i
++)
409 this->build_dsymbol ((*d
->members
)[i
]);
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. */
417 void visit (TemplateDeclaration
*d
) final override
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
;
423 if (!fd
|| !fd
->isAuto ())
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 ();
430 while (tb
->ty
== TY::Tarray
|| tb
->ty
== TY::Tpointer
)
431 tb
= tb
->nextOf ()->baseElemOf ();
433 TemplateInstance
*ti
= NULL
;
435 if (tb
->ty
== TY::Tstruct
)
436 ti
= tb
->isTypeStruct ()->sym
->isInstantiated ();
437 else if (tb
->ty
== TY::Tclass
)
438 ti
= tb
->isTypeClass ()->sym
->isInstantiated ();
440 /* Return type is instantiated from this template declaration, walk over
441 all members of the instance. */
442 if (ti
&& ti
->tempdecl
== d
)
443 this->build_dsymbol (ti
);
446 /* Walk over all members in the instantiated template. */
448 void visit (TemplateInstance
*d
) final override
450 if (isError (d
)|| !d
->members
)
453 if (!d
->needsCodegen ())
456 for (size_t i
= 0; i
< d
->members
->length
; i
++)
457 this->build_dsymbol ((*d
->members
)[i
]);
460 /* Walk over all members in the mixin template scope. */
462 void visit (TemplateMixin
*d
) final override
464 if (isError (d
)|| !d
->members
)
467 for (size_t i
= 0; i
< d
->members
->length
; i
++)
468 this->build_dsymbol ((*d
->members
)[i
]);
471 /* Write out compiler generated TypeInfo, initializer and functions for the
472 given struct declaration, walking over all static members. */
474 void visit (StructDeclaration
*d
) final override
476 if (d
->semanticRun
>= PASS::obj
)
479 if (d
->type
->ty
== TY::Terror
)
481 error_at (make_location_t (d
->loc
),
482 "had semantic errors when compiling");
486 /* Don't emit any symbols from gcc.attributes module. */
487 if (gcc_attribute_p (d
))
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
));
495 /* Anonymous structs/unions only exist as part of others,
496 do not output forward referenced structs. */
497 if (d
->isAnonymous () || !d
->members
)
500 /* Generate TypeInfo. */
501 if (have_typeinfo_p (Type::dtypeinfo
))
502 create_typeinfo (d
->type
, NULL
);
504 /* Generate static initializer. */
505 tree sinit
= aggregate_initializer_decl (d
);
506 DECL_INITIAL (sinit
) = layout_struct_initializer (d
);
507 d_finish_decl (sinit
);
509 /* Put out the members. There might be static constructors in the members
510 list, and they cannot be put in separate object files. */
511 for (size_t i
= 0; i
< d
->members
->length
; i
++)
512 this->build_dsymbol ((*d
->members
)[i
]);
514 /* Put out xopEquals, xopCmp and xopHash. */
515 if (d
->xeq
&& d
->xeq
!= d
->xerreq
)
516 this->build_dsymbol (d
->xeq
);
518 if (d
->xcmp
&& d
->xcmp
!= d
->xerrcmp
)
519 this->build_dsymbol (d
->xcmp
);
522 this->build_dsymbol (d
->xhash
);
524 d
->semanticRun
= PASS::obj
;
527 /* Finish semantic analysis of functions in vtbl for class CD. */
529 bool finish_vtable (ClassDeclaration
*d
)
531 bool has_errors
= false;
533 /* Finish semantic analysis of functions in vtbl[]. */
534 for (size_t i
= d
->vtblOffset (); i
< d
->vtbl
.length
; i
++)
536 FuncDeclaration
*fd
= d
->vtbl
[i
]->isFuncDeclaration ();
538 if (!fd
|| (!fd
->fbody
&& d
->isAbstract ()))
541 /* Ensure function has a return value. */
542 if (!fd
->functionSemantic ())
545 /* No name hiding to check for. */
546 if (!d
->isFuncHidden (fd
) || fd
->isFuture ())
549 /* The function fd is hidden from the view of the class.
550 If it overlaps with any function in the vtbl[], then
552 for (size_t j
= 1; j
< d
->vtbl
.length
; j
++)
557 FuncDeclaration
*fd2
= d
->vtbl
[j
]->isFuncDeclaration ();
558 if (!fd2
->ident
->equals (fd
->ident
))
561 /* The function is marked as @__future, a deprecation has
562 already been given by the frontend. */
563 if (fd2
->isFuture ())
566 if (fd
->leastAsSpecialized (fd2
, NULL
) != MATCH::nomatch
567 || fd2
->leastAsSpecialized (fd
, NULL
) != MATCH::nomatch
)
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 ());
586 /* Write out compiler generated TypeInfo, initializer and vtables for the
587 given class declaration, walking over all static members. */
589 void visit (ClassDeclaration
*d
) final override
591 if (d
->semanticRun
>= PASS::obj
)
594 if (d
->type
->ty
== TY::Terror
)
596 error_at (make_location_t (d
->loc
),
597 "had semantic errors when compiling");
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
));
609 /* Put out the members. */
610 for (size_t i
= 0; i
< d
->members
->length
; i
++)
611 this->build_dsymbol ((*d
->members
)[i
]);
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
))
618 /* Generate C symbols. */
619 d
->csym
= get_classinfo_decl (d
);
620 Dsymbol
*vtblsym
= d
->vtblSymbol ();
621 vtblsym
->csym
= get_vtable_decl (d
);
622 tree sinit
= aggregate_initializer_decl (d
);
624 /* Generate static initializer. */
625 DECL_INITIAL (sinit
) = layout_class_initializer (d
);
626 d_finish_decl (sinit
);
628 /* Put out the TypeInfo. */
629 if (have_typeinfo_p (Type::dtypeinfo
))
630 create_typeinfo (d
->type
, NULL
);
632 DECL_INITIAL (d
->csym
) = layout_classinfo (d
);
633 d_finish_decl (d
->csym
);
635 /* Put out the vtbl[]. */
636 vec
<constructor_elt
, va_gc
> *elms
= NULL
;
638 /* First entry is ClassInfo reference. */
639 if (d
->vtblOffset ())
640 CONSTRUCTOR_APPEND_ELT (elms
, size_zero_node
, build_address (d
->csym
));
642 for (size_t i
= d
->vtblOffset (); i
< d
->vtbl
.length
; i
++)
644 FuncDeclaration
*fd
= d
->vtbl
[i
]->isFuncDeclaration ();
646 if (fd
&& (fd
->fbody
|| !d
->isAbstract ()))
648 CONSTRUCTOR_APPEND_ELT (elms
, size_int (i
),
649 build_address (get_symbol_decl (fd
)));
653 DECL_INITIAL (vtblsym
->csym
)
654 = build_constructor (TREE_TYPE (vtblsym
->csym
), elms
);
655 d_finish_decl (vtblsym
->csym
);
657 d
->semanticRun
= PASS::obj
;
660 /* Write out compiler generated TypeInfo and vtables for the given interface
661 declaration, walking over all static members. */
663 void visit (InterfaceDeclaration
*d
) final override
665 if (d
->semanticRun
>= PASS::obj
)
668 if (d
->type
->ty
== TY::Terror
)
670 error_at (make_location_t (d
->loc
),
671 "had semantic errors when compiling");
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
));
683 /* Put out the members. */
684 for (size_t i
= 0; i
< d
->members
->length
; i
++)
685 this->build_dsymbol ((*d
->members
)[i
]);
687 /* Generate C symbols. */
688 d
->csym
= get_classinfo_decl (d
);
690 /* Put out the TypeInfo. */
691 if (have_typeinfo_p (Type::dtypeinfo
))
693 create_typeinfo (d
->type
, NULL
);
694 this->build_dsymbol (d
->type
->vtinfo
);
697 DECL_INITIAL (d
->csym
) = layout_classinfo (d
);
698 d_finish_decl (d
->csym
);
700 d
->semanticRun
= PASS::obj
;
703 /* Write out compiler generated TypeInfo and initializer for the given
706 void visit (EnumDeclaration
*d
) final override
708 if (d
->semanticRun
>= PASS::obj
)
711 if (d
->errors
|| d
->type
->ty
== TY::Terror
)
713 error_at (make_location_t (d
->loc
),
714 "had semantic errors when compiling");
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
));
723 if (d
->isAnonymous ())
726 /* Generate TypeInfo. */
727 if (have_typeinfo_p (Type::dtypeinfo
))
728 create_typeinfo (d
->type
, NULL
);
730 TypeEnum
*tc
= d
->type
->isTypeEnum ();
731 if (tc
->sym
->members
&& !d
->type
->isZeroInit ())
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
);
739 d
->semanticRun
= PASS::obj
;
742 /* Finish up a variable declaration and push it into the current scope.
743 This can either be a static, local or manifest constant. */
745 void visit (VarDeclaration
*d
) final override
747 if (d
->semanticRun
>= PASS::obj
)
750 if (d
->type
->ty
== TY::Terror
)
752 error_at (make_location_t (d
->loc
),
753 "had semantic errors when compiling");
757 /* Variables of type `noreturn` are just placeholders, and evaluate to
758 `assert(0)` if ever read. */
759 if (d
->type
->isTypeNoreturn ())
761 if (!d
->isDataseg () && !d
->isMember ()
762 && d
->_init
&& !d
->_init
->isVoidInitializer ())
764 /* Evaluate RHS for side effects first. */
765 Expression
*ie
= initializerToExpression (d
->_init
);
766 add_stmt (build_expr (ie
));
768 Expression
*e
= d
->type
->defaultInitLiteral (d
->loc
);
769 add_stmt (build_expr (e
));
777 this->build_dsymbol (d
->toAlias ());
781 if (!d
->canTakeAddressOf ())
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 ())
787 tree decl
= get_symbol_decl (d
);
789 rest_of_decl_compilation (decl
, 1, 0);
792 else if (d
->isDataseg ())
794 tree decl
= get_symbol_decl (d
);
796 /* Only need to build the VAR_DECL for extern declarations. */
797 if (d
->storage_class
& STCextern
)
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
)
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
))
811 error_at (make_location_t (d
->loc
), "size is too large");
817 /* Use the explicit initializer, this includes `void`. */
818 if (!d
->_init
->isVoidInitializer ())
820 Expression
*e
= initializerToExpression (d
->_init
, d
->type
);
821 DECL_INITIAL (decl
) = build_expr (e
, true);
824 else if (!d
->type
->isZeroInit ())
826 /* Use default initializer for the type. */
827 if (TypeStruct
*ts
= d
->type
->isTypeStruct ())
828 DECL_INITIAL (decl
) = layout_struct_initializer (ts
->sym
);
831 Expression
*e
= d
->type
->defaultInitLiteral (d
->loc
);
832 DECL_INITIAL (decl
) = build_expr (e
, true);
836 /* Frontend should have already caught this. */
837 gcc_assert (!integer_zerop (size
)
838 || d
->type
->toBasetype ()->isTypeSArray ());
840 d_finish_decl (decl
);
842 /* Maybe record the var against the current module. */
843 register_module_decl (d
);
845 else if (!d
->isDataseg () && !d
->isMember ())
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
);
852 if (d
->_init
&& !d
->_init
->isVoidInitializer ())
854 tree decl
= get_symbol_decl (d
);
856 ExpInitializer
*vinit
= d
->_init
->isExpInitializer ();
857 Expression
*ie
= initializerToExpression (vinit
);
858 tree exp
= build_expr (ie
);
860 /* Maybe put variable on list of things needing destruction. */
861 if (d
->needsScopeDtor ())
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
);
873 d
->semanticRun
= PASS::obj
;
876 /* Generate and compile a static TypeInfo declaration, but only if it is
877 needed in the current compilation. */
879 void visit (TypeInfoDeclaration
*d
) final override
881 if (d
->semanticRun
>= PASS::obj
)
884 if (speculative_type_p (d
->tinfo
))
887 tree t
= get_typeinfo_decl (d
);
888 DECL_INITIAL (t
) = layout_typeinfo (d
);
890 d
->semanticRun
= PASS::obj
;
893 /* Finish up a function declaration and compile it all the way
894 down to assembler language output. */
896 void visit (FuncDeclaration
*d
) final override
898 /* Already generated the function. */
899 if (d
->semanticRun
>= PASS::obj
)
902 /* Don't emit any symbols from gcc.attributes module. */
903 if (gcc_attribute_p (d
))
906 /* Front-end decided this function doesn't require code generation. */
907 if (d
->skipCodegen ())
910 /* Not emitting unittest functions. */
911 if (!global
.params
.useUnitTests
&& d
->isUnitTestDeclaration ())
914 /* Check if any errors occurred when running semantic. */
915 if (TypeFunction
*tf
= d
->type
->isTypeFunction ())
917 if (tf
->next
== NULL
|| tf
->next
->ty
== TY::Terror
)
921 if (d
->hasSemantic3Errors ())
926 FuncDeclaration
*fdp
= d
;
927 while (fdp
&& fdp
->isNested ())
929 fdp
= fdp
->toParent2 ()->isFuncDeclaration ();
934 /* Parent failed to compile, but errors were gagged. */
935 if (fdp
->hasSemantic3Errors ())
940 /* Ensure all semantic passes have run. */
941 if (d
->semanticRun
< PASS::semantic3
)
943 gcc_assert (!doing_semantic_analysis_p
);
945 doing_semantic_analysis_p
= true;
946 d
->functionSemantic3 ();
947 Module::runDeferredSemantic3 ();
948 doing_semantic_analysis_p
= false;
954 /* Start generating code for this function. */
955 gcc_assert (d
->semanticRun
== PASS::semantic3done
);
956 d
->semanticRun
= PASS::obj
;
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
)
967 rest_of_decl_compilation (fndecl
, 1, 0);
971 if (global
.params
.verbose
)
972 message ("function %s", d
->toPrettyChars ());
974 tree old_context
= start_function (d
);
975 tree param_list
= get_fndecl_arguments (d
);
977 DECL_IN_UNITTEST_CONDITION_P (fndecl
) = this->in_version_unittest_
;
978 rest_of_decl_compilation (fndecl
, 1, 0);
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. */
984 d_function_chain
->static_chain
= get_symbol_decl (d
->vthis
);
988 AggregateDeclaration
*ad
= d
->isThis ();
989 tree this_tree
= get_symbol_decl (d
->vthis
);
991 while (ad
->isNested ())
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
);
997 ad
= pd
->isAggregateDeclaration ();
1000 d_function_chain
->static_chain
= this_tree
;
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. */
1010 tree resdecl
= DECL_RESULT (fndecl
);
1012 if (TREE_ADDRESSABLE (TREE_TYPE (resdecl
))
1013 || aggregate_value_p (TREE_TYPE (resdecl
), fndecl
))
1015 /* Return non-trivial structs by invisible reference. */
1016 if (TREE_ADDRESSABLE (TREE_TYPE (resdecl
)))
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
);
1022 d
->shidden
= build_deref (resdecl
);
1025 d
->shidden
= resdecl
;
1027 if (d
->isNRVO () && d
->nrvo_var
)
1029 tree var
= get_symbol_decl (d
->nrvo_var
);
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;
1036 SET_DECL_VALUE_EXPR (var
, resdecl
);
1037 DECL_HAS_VALUE_EXPR_P (var
) = 1;
1038 SET_DECL_LANG_NRVO (var
, d
->shidden
);
1042 /* May change cfun->static_chain. */
1046 declare_local_var (d
->vresult
);
1051 build_function_body (d
);
1053 /* Initialize the _argptr variable. */
1056 tree body
= pop_stmt_list ();
1057 tree var
= get_decl_tree (d
->v_argptr
);
1058 var
= build_address (var
);
1060 tree init
= build_call_expr (builtin_decl_explicit (BUILT_IN_VA_START
),
1061 2, var
, tree_last (param_list
));
1062 declare_local_var (d
->v_argptr
);
1065 tree cleanup
= build_call_expr (builtin_decl_explicit (BUILT_IN_VA_END
),
1067 add_stmt (build2 (TRY_FINALLY_EXPR
, void_type_node
, body
, cleanup
));
1070 finish_function (old_context
);
1072 /* Maybe record the function against the current module. */
1073 register_module_decl (d
);
1077 /* Main entry point for the DeclVisitor interface to send
1078 the Declaration AST class D to GCC back-end. */
1081 build_decl_tree (Dsymbol
*d
)
1083 location_t saved_location
= input_location
;
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
);
1089 input_location
= make_location_t (Loc ("<no_file>", 1, 0));
1091 DeclVisitor v
= DeclVisitor ();
1092 v
.build_dsymbol (d
);
1094 input_location
= saved_location
;
1097 /* Returns true if function FD always needs to be implicitly defined, such as
1098 it was declared `pragma(inline)'. */
1101 function_needs_inline_definition_p (FuncDeclaration
*fd
)
1103 /* Function has already been defined. */
1104 if (!DECL_EXTERNAL (fd
->csym
))
1107 /* No function body available for inlining. */
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 ())
1118 /* Check whether function will be regularly defined later in the current
1119 translation unit. */
1120 Module
*md
= fd
->getModule ();
1121 if (md
&& md
->isRoot ())
1124 /* Non-inlineable functions are always external. */
1125 if (DECL_UNINLINABLE (fd
->csym
))
1128 /* Ignore functions that aren't decorated with `pragma(inline)'. */
1129 if (!DECL_DECLARED_INLINE_P (fd
->csym
))
1132 /* Weak functions cannot be inlined. */
1133 if (lookup_attribute ("weak", DECL_ATTRIBUTES (fd
->csym
)))
1136 /* Naked functions cannot be inlined. */
1137 if (lookup_attribute ("naked", DECL_ATTRIBUTES (fd
->csym
)))
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. */
1147 maybe_build_decl_tree (Declaration
*decl
)
1149 gcc_assert (decl
->csym
!= NULL_TREE
);
1151 /* Still running semantic analysis on declaration, or it has already had its
1153 if (doing_semantic_analysis_p
|| decl
->semanticRun
>= PASS::obj
)
1156 if (error_operand_p (decl
->csym
))
1159 if (FuncDeclaration
*fd
= decl
->isFuncDeclaration ())
1161 /* Externally defined inline functions need to be emitted. */
1162 if (function_needs_inline_definition_p (fd
))
1164 DECL_EXTERNAL (fd
->csym
) = 0;
1165 d_defer_declaration (fd
);
1172 /* Return the decl for the symbol, create it if it doesn't already exist. */
1175 get_symbol_decl (Declaration
*decl
)
1178 return maybe_build_decl_tree (decl
);
1180 /* Deal with placeholder symbols immediately:
1181 SymbolDeclaration is used as a shell around an initializer symbol. */
1182 SymbolDeclaration
*sd
= decl
->isSymbolDeclaration ();
1185 decl
->csym
= aggregate_initializer_decl (sd
->dsym
);
1189 /* Global static TypeInfo declaration. */
1190 if (decl
->isTypeInfoDeclaration ())
1191 return get_typeinfo_decl ((TypeInfoDeclaration
*) decl
);
1193 /* FuncAliasDeclaration is used to import functions from another scope. */
1194 FuncAliasDeclaration
*fad
= decl
->isFuncAliasDeclaration ();
1197 decl
->csym
= get_symbol_decl (fad
->funcalias
);
1201 /* It is possible for a field declaration symbol to be requested
1202 before the parent type has been built. */
1203 if (decl
->isField ())
1205 AggregateDeclaration
*ad
= decl
->toParent ()->isAggregateDeclaration ();
1206 gcc_assert (ad
!= NULL
);
1208 /* Finishing off the type should create the associated FIELD_DECL. */
1209 build_ctype (ad
->type
);
1210 gcc_assert (decl
->csym
!= NULL
);
1215 /* Build the tree for the symbol. */
1216 FuncDeclaration
*fd
= decl
->isFuncDeclaration ();
1219 /* Run full semantic on functions we need to know about. */
1220 if (!fd
->functionSemantic ())
1222 decl
->csym
= error_mark_node
;
1226 decl
->csym
= build_decl (make_location_t (decl
->loc
), FUNCTION_DECL
,
1227 get_identifier (decl
->ident
->toChars ()),
1230 /* Set function type afterwards as there could be self references. */
1231 TREE_TYPE (decl
->csym
) = build_ctype (fd
->type
);
1233 /* Set DECL_INITIAL now if the function has a definition. */
1235 DECL_INITIAL (decl
->csym
) = error_mark_node
;
1237 DECL_EXTERNAL (decl
->csym
) = 1;
1241 /* Build the variable declaration. */
1242 VarDeclaration
*vd
= decl
->isVarDeclaration ();
1243 gcc_assert (vd
!= NULL
);
1245 tree_code code
= vd
->isParameter () ? PARM_DECL
1246 : !vd
->canTakeAddressOf () ? CONST_DECL
1248 decl
->csym
= build_decl (make_location_t (decl
->loc
), code
,
1249 get_identifier (decl
->ident
->toChars ()),
1250 declaration_type (vd
));
1252 /* If any alignment was set on the declaration. */
1253 if (!vd
->alignment
.isDefault ())
1255 SET_DECL_ALIGN (decl
->csym
, vd
->alignment
.get () * BITS_PER_UNIT
);
1256 DECL_USER_ALIGN (decl
->csym
) = 1;
1259 if (vd
->storage_class
& STCextern
)
1260 DECL_EXTERNAL (decl
->csym
) = 1;
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
)
1269 /* Cannot make an expression out of a void initializer. */
1270 if (vd
->_init
&& !vd
->_init
->isVoidInitializer ())
1272 Expression
*ie
= initializerToExpression (vd
->_init
);
1274 if (!vd
->type
->isscalar ())
1275 DECL_INITIAL (decl
->csym
) = build_expr (ie
, false);
1277 DECL_INITIAL (decl
->csym
) = build_expr (ie
, true);
1281 /* [type-qualifiers/const-and-immutable]
1283 `immutable` applies to data that cannot change. Immutable data values,
1284 once constructed, remain the same for the duration of the program's
1286 if (vd
->isImmutable () && !vd
->setInCtorOnly ())
1287 TREE_READONLY (decl
->csym
) = 1;
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
1292 if (vd
->isConst () && !vd
->isDataseg ())
1293 TREE_READONLY (decl
->csym
) = 1;
1296 /* Set the declaration mangled identifier if static. */
1297 if (decl
->isCodeseg () || decl
->isDataseg ())
1301 if (decl
->mangleOverride
.length
)
1304 get_identifier_with_length (decl
->mangleOverride
.ptr
,
1305 decl
->mangleOverride
.length
);
1308 mangled_name
= get_identifier (d_mangle_decl (decl
));
1310 mangled_name
= targetm
.mangle_decl_assembler_name (decl
->csym
,
1312 /* The frontend doesn't handle duplicate definitions of unused symbols
1313 with the same mangle. So a check is done here instead. */
1314 if (IDENTIFIER_DSYMBOL (mangled_name
))
1316 Declaration
*other
= IDENTIFIER_DSYMBOL (mangled_name
);
1317 tree olddecl
= decl
->csym
;
1318 decl
->csym
= get_symbol_decl (other
);
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
);
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
))
1328 apply_user_attributes (decl
, decl
->csym
);
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
))
1335 IDENTIFIER_DSYMBOL (mangled_name
) = decl
;
1336 DECL_EXTERNAL (decl
->csym
) = 0;
1338 /* Non-extern, non-templated decls shouldn't be defined twice. */
1339 else if (!decl
->isInstantiated ())
1340 ScopeDsymbol::multiplyDefined (decl
->loc
, decl
, other
);
1344 IDENTIFIER_PRETTY_NAME (mangled_name
)
1345 = get_identifier (decl
->toPrettyChars (true));
1346 IDENTIFIER_DSYMBOL (mangled_name
) = decl
;
1348 SET_DECL_ASSEMBLER_NAME (decl
->csym
, mangled_name
);
1352 DECL_LANG_SPECIFIC (decl
->csym
) = build_lang_decl (decl
);
1353 DECL_CONTEXT (decl
->csym
) = d_decl_context (decl
);
1355 if (TREE_CODE (decl
->csym
) == PARM_DECL
)
1357 /* Pass non-trivial structs by invisible reference. */
1358 if (TREE_ADDRESSABLE (TREE_TYPE (decl
->csym
)))
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
;
1370 DECL_ARG_TYPE (decl
->csym
) = TREE_TYPE (decl
->csym
);
1371 gcc_assert (TREE_CODE (DECL_CONTEXT (decl
->csym
)) == FUNCTION_DECL
);
1373 else if (TREE_CODE (decl
->csym
) == CONST_DECL
)
1375 /* Manifest constants have no address in memory. */
1376 TREE_CONSTANT (decl
->csym
) = 1;
1377 TREE_READONLY (decl
->csym
) = 1;
1379 else if (TREE_CODE (decl
->csym
) == FUNCTION_DECL
)
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 ())
1387 fatal_error (make_location_t (fd
->loc
),
1388 "function requires a dual-context, which is not yet "
1389 "supported by GDC");
1392 /* The real function type may differ from its declaration. */
1393 tree fntype
= TREE_TYPE (decl
->csym
);
1394 tree newfntype
= NULL_TREE
;
1396 if (fd
->isNested ())
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
);
1402 else if (fd
->isThis ())
1404 /* Add an extra argument for the `this' parameter. The handle type is
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 ());
1410 /* If handle is a pointer type, get record type. */
1411 if (!ad
->isStructDeclaration ())
1412 handle
= TREE_TYPE (handle
);
1414 newfntype
= build_vthis_function (handle
, fntype
);
1416 /* Set the vindex on virtual functions. */
1417 if (fd
->isVirtual () && fd
->vtblIndex
!= -1)
1419 DECL_VINDEX (decl
->csym
) = size_int (fd
->vtblIndex
);
1420 DECL_VIRTUAL_P (decl
->csym
) = 1;
1423 /* Align method to the minimum boundary for target. */
1424 SET_DECL_ALIGN (decl
->csym
, MINIMUM_METHOD_BOUNDARY
);
1426 else if (fd
->isMain () || fd
->isCMain ())
1428 /* The main function is named `D main' to distinguish from C main. */
1430 DECL_NAME (decl
->csym
) = get_identifier (fd
->toPrettyChars (true));
1432 /* `void main' is implicitly converted to returning an int. */
1433 newfntype
= build_function_type (d_int_type
, TYPE_ARG_TYPES (fntype
));
1436 if (newfntype
!= NULL_TREE
)
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
;
1446 /* Miscellaneous function flags. */
1448 /* In [pragma/inline], functions decorated with `pragma(inline)' affects
1449 whether they are inlined or not. */
1450 if (fd
->inlining
== PINLINE::always
)
1451 DECL_DECLARED_INLINE_P (decl
->csym
) = 1;
1452 else if (fd
->inlining
== PINLINE::never
)
1453 DECL_UNINLINABLE (decl
->csym
) = 1;
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
1458 if (fd
->isCrtCtor ())
1460 DECL_STATIC_CONSTRUCTOR (decl
->csym
) = 1;
1461 decl_init_priority_insert (decl
->csym
, DEFAULT_INIT_PRIORITY
);
1463 else if (fd
->isCrtDtor ())
1465 DECL_STATIC_DESTRUCTOR (decl
->csym
) = 1;
1466 decl_fini_priority_insert (decl
->csym
, DEFAULT_INIT_PRIORITY
);
1469 /* Function was declared `naked'. */
1472 insert_decl_attribute (decl
->csym
, "naked");
1473 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
->csym
) = 1;
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);
1482 /* Mark compiler generated functions as artificial. */
1483 if (fd
->isGenerated ())
1484 DECL_ARTIFICIAL (decl
->csym
) = 1;
1486 /* Ensure and require contracts are lexically nested in the function they
1487 part of, but are always publicly callable. */
1488 if (fd
->ident
== Identifier::idPool ("ensure")
1489 || fd
->ident
== Identifier::idPool ("require"))
1490 TREE_PUBLIC (decl
->csym
) = 1;
1492 if (decl
->storage_class
& STCfinal
)
1493 DECL_FINAL_P (decl
->csym
) = 1;
1495 /* Function is of type `noreturn' or `typeof(*null)'. */
1496 if (fd
->type
->nextOf ()->isTypeNoreturn ())
1497 TREE_THIS_VOLATILE (decl
->csym
) = 1;
1499 /* Check whether this function is expanded by the frontend. */
1500 DECL_INTRINSIC_CODE (decl
->csym
) = INTRINSIC_NONE
;
1501 maybe_set_intrinsic (fd
);
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
);
1507 if (nested_function_origin (node
))
1508 unnest_function (node
);
1511 /* Mark compiler generated temporaries as artificial. */
1512 if (decl
->storage_class
& STCtemp
)
1513 DECL_ARTIFICIAL (decl
->csym
) = 1;
1515 /* Propagate shared on the decl. */
1516 if (TYPE_SHARED (TREE_TYPE (decl
->csym
)))
1517 TREE_ADDRESSABLE (decl
->csym
) = 1;
1519 /* Symbol was marked volatile. */
1520 if (decl
->storage_class
& STCvolatile
)
1521 TREE_THIS_VOLATILE (decl
->csym
) = 1;
1523 /* Symbol was marked register. */
1524 if (decl
->storage_class
& STCregister
)
1525 DECL_REGISTER (decl
->csym
) = 1;
1527 /* Symbol was declared with deprecated attribute. */
1528 if (decl
->storage_class
& STCdeprecated
)
1529 TREE_DEPRECATED (decl
->csym
) = 1;
1531 if (decl
->isDataseg () || decl
->isCodeseg () || decl
->isThreadlocal ())
1533 /* Set TREE_PUBLIC by default, but allow private template to override. */
1534 if (!fd
|| !fd
->isNested ())
1535 TREE_PUBLIC (decl
->csym
) = 1;
1537 TREE_STATIC (decl
->csym
) = 1;
1538 /* The decl has not been defined -- yet. */
1539 DECL_EXTERNAL (decl
->csym
) = 1;
1541 /* Visibility attributes are used by the debugger. */
1542 set_visibility_for_decl (decl
->csym
, decl
);
1544 DECL_INSTANTIATED (decl
->csym
) = (decl
->isInstantiated () != NULL
);
1545 set_linkage_for_decl (decl
->csym
);
1548 /* Symbol is going in thread local storage. */
1549 if (decl
->isThreadlocal () && !DECL_ARTIFICIAL (decl
->csym
))
1551 if (global
.params
.vtls
)
1552 message (decl
->loc
, "`%s` is thread local", decl
->toChars ());
1554 set_decl_tls_model (decl
->csym
, decl_default_tls_model (decl
->csym
));
1557 /* Apply any user attributes that may affect semantic meaning. */
1558 apply_user_attributes (decl
, decl
->csym
);
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
))
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 ());
1572 /* %% Probably should be a little more intelligent about setting this. */
1573 TREE_USED (decl
->csym
) = 1;
1574 d_keep (decl
->csym
);
1576 return maybe_build_decl_tree (decl
);
1579 /* Returns a declaration for a VAR_DECL. Used to create compiler-generated
1580 global variables. */
1583 declare_extern_var (tree ident
, tree type
)
1585 /* If the VAR_DECL has already been declared, return it. */
1586 if (IDENTIFIER_DECL_TREE (ident
))
1587 return IDENTIFIER_DECL_TREE (ident
);
1589 tree name
= IDENTIFIER_PRETTY_NAME (ident
)
1590 ? IDENTIFIER_PRETTY_NAME (ident
) : ident
;
1591 tree decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
1593 IDENTIFIER_DECL_TREE (ident
) = decl
;
1596 SET_DECL_ASSEMBLER_NAME (decl
, ident
);
1597 DECL_ARTIFICIAL (decl
) = 1;
1598 TREE_STATIC (decl
) = 1;
1599 TREE_PUBLIC (decl
) = 1;
1601 /* The decl has not been defined -- yet. */
1602 DECL_EXTERNAL (decl
) = 1;
1604 set_linkage_for_decl (decl
);
1609 /* Add local variable VAR into the current function body. */
1612 declare_local_var (VarDeclaration
*var
)
1614 gcc_assert (!var
->isDataseg () && !var
->isMember ());
1615 gcc_assert (current_function_decl
!= NULL_TREE
);
1617 FuncDeclaration
*fd
= cfun
->language
->function
;
1618 tree decl
= get_symbol_decl (var
);
1620 gcc_assert (!TREE_STATIC (decl
));
1622 DECL_CONTEXT (decl
) = current_function_decl
;
1624 /* Compiler generated symbols. */
1625 if (var
== fd
->vresult
|| var
== fd
->v_argptr
)
1626 DECL_ARTIFICIAL (decl
) = 1;
1628 if (DECL_LANG_FRAME_FIELD (decl
))
1630 /* Fixes debugging local variables. */
1631 SET_DECL_VALUE_EXPR (decl
, get_decl_tree (var
));
1632 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1636 /* Return an unnamed local temporary of type TYPE. */
1639 build_local_temp (tree type
)
1641 tree decl
= create_tmp_var_raw (type
);
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(). */
1654 get_decl_tree (Declaration
*decl
)
1656 tree t
= get_symbol_decl (decl
);
1657 FuncDeclaration
*fd
= cfun
? cfun
->language
->function
: NULL
;
1658 VarDeclaration
*vd
= decl
->isVarDeclaration ();
1660 /* If cfun is NULL, then this is a global static. */
1661 if (vd
== NULL
|| fd
== NULL
)
1664 /* Get the closure holding the var decl. */
1665 if (DECL_LANG_FRAME_FIELD (t
))
1667 FuncDeclaration
*parent
= vd
->toParent2 ()->isFuncDeclaration ();
1668 tree frame_ref
= get_framedecl (fd
, parent
);
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
);
1680 /* Get the named return value. */
1681 if (DECL_LANG_NRVO (t
))
1682 return DECL_LANG_NRVO (t
);
1684 /* Get the non-local `this' value by going through parent link
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 ())
1689 /* Find the first parent that is a member function. */
1690 while (!fd
->isMember2 ())
1692 gcc_assert (fd
->vthis
);
1693 fd
= fd
->toParent2 ()->isFuncDeclaration ();
1694 gcc_assert (fd
!= NULL
);
1697 AggregateDeclaration
*ad
= fd
->isThis ();
1698 gcc_assert (ad
!= NULL
);
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
);
1705 t
= get_decl_tree (fd
->vthis
);
1706 Dsymbol
*outer
= fd
;
1708 while (outer
!= vd
->parent
)
1710 gcc_assert (ad
!= NULL
);
1711 outer
= ad
->toParent2 ();
1713 /* Get the this->this parent link. */
1714 tree vfield
= get_symbol_decl (ad
->vthis
);
1715 t
= component_ref (build_deref (t
), vfield
);
1717 ad
= outer
->isAggregateDeclaration ();
1721 fd
= outer
->isFuncDeclaration ();
1724 /* If outer function creates a closure, then the `this'
1725 value would be the closure pointer, and the real
1726 `this' the first field of that closure. */
1727 tree ff
= get_frameinfo (fd
);
1728 if (FRAMEINFO_CREATES_FRAME (ff
))
1730 t
= build_nop (build_pointer_type (FRAMEINFO_TYPE (ff
)), t
);
1731 t
= indirect_ref (build_ctype (fd
->vthis
->type
), t
);
1734 if (fd
== vd
->parent
)
1737 /* Continue looking for the right `this'. */
1738 outer
= outer
->toParent2 ();
1739 fd
= outer
->isFuncDeclaration ();
1742 ad
= outer
->isAggregateDeclaration ();
1748 /* Auto variable that the back end will handle for us. */
1752 /* Finish up a variable declaration and compile it all the way to
1753 the assembler language output. */
1756 d_finish_decl (tree decl
)
1758 gcc_assert (!error_operand_p (decl
));
1760 /* We are sending this symbol to object file, can't be extern. */
1761 TREE_STATIC (decl
) = 1;
1762 DECL_EXTERNAL (decl
) = 0;
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
));
1768 relayout_decl (decl
);
1770 if (flag_checking
&& DECL_INITIAL (decl
))
1772 /* Initializer must never be bigger than symbol size. */
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
)));
1779 tree name
= DECL_ASSEMBLER_NAME (decl
);
1781 internal_error ("mismatch between declaration %qE size (%wd) and "
1782 "its initializer size (%wd)",
1783 IDENTIFIER_PRETTY_NAME (name
)
1784 ? IDENTIFIER_PRETTY_NAME (name
) : name
,
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
;
1795 /* Add this decl to the current binding level. */
1798 rest_of_decl_compilation (decl
, 1, 0);
1801 /* Thunk code is based on g++. */
1803 static int thunk_labelno
;
1805 /* Create a static alias to function. */
1808 make_alias_for_thunk (tree function
)
1813 /* Thunks may reference extern functions which cannot be aliased. */
1814 if (DECL_EXTERNAL (function
))
1817 targetm
.asm_out
.generate_internal_label (buf
, "LTHUNK", thunk_labelno
);
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;
1829 DECL_EXTERNAL (alias
) = 0;
1830 DECL_ARTIFICIAL (alias
) = 1;
1832 DECL_DECLARED_INLINE_P (alias
) = 0;
1833 DECL_INITIAL (alias
) = error_mark_node
;
1834 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (function
));
1836 TREE_ADDRESSABLE (alias
) = 1;
1837 TREE_USED (alias
) = 1;
1838 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
1840 if (!flag_syntax_only
)
1842 cgraph_node
*aliasn
;
1843 aliasn
= cgraph_node::create_same_body_alias (alias
, function
);
1844 gcc_assert (aliasn
!= NULL
);
1849 /* Emit the definition of a D vtable thunk. */
1852 finish_thunk (tree thunk
, tree function
)
1854 /* Setup how D thunks are outputted. */
1855 int fixed_offset
= -THUNK_LANG_OFFSET (thunk
);
1856 bool this_adjusting
= true;
1859 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
))
1860 alias
= make_alias_for_thunk (function
);
1864 TREE_ADDRESSABLE (function
) = 1;
1865 TREE_USED (function
) = 1;
1866 DECL_EXTERNAL (thunk
) = 0;
1868 if (flag_syntax_only
)
1870 TREE_ASM_WRITTEN (thunk
) = 1;
1874 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
)
1875 && targetm_common
.have_named_sections
)
1878 symtab_node
*symbol
= symtab_node::get (function
);
1880 if (symbol
!= NULL
&& symbol
->alias
)
1882 if (symbol
->analyzed
)
1883 fn
= symtab_node::get (function
)->ultimate_alias_target ()->decl
;
1885 fn
= symtab_node::get (function
)->alias_target
;
1887 resolve_unique_section (fn
, 0, flag_function_sections
);
1889 if (DECL_SECTION_NAME (fn
) != NULL
&& DECL_ONE_ONLY (fn
))
1891 resolve_unique_section (thunk
, 0, flag_function_sections
);
1893 /* Output the thunk into the same section as function. */
1894 set_decl_section_name (thunk
, fn
);
1895 symtab_node::get (thunk
)->implicit_section
1896 = symtab_node::get (fn
)->implicit_section
;
1900 /* Set up cloned argument trees for the thunk. */
1902 for (tree a
= DECL_ARGUMENTS (function
); a
; a
= DECL_CHAIN (a
))
1904 tree x
= copy_node (a
);
1906 DECL_CONTEXT (x
) = thunk
;
1907 SET_DECL_RTL (x
, NULL
);
1908 DECL_HAS_VALUE_EXPR_P (x
) = 0;
1909 TREE_ADDRESSABLE (x
) = 0;
1912 DECL_ARGUMENTS (thunk
) = nreverse (t
);
1913 TREE_ASM_WRITTEN (thunk
) = 1;
1915 cgraph_node
*funcn
, *thunk_node
;
1917 funcn
= cgraph_node::get_create (function
);
1919 thunk_node
= funcn
->create_thunk (thunk
, thunk
, this_adjusting
,
1920 fixed_offset
, 0, 0, 0, alias
);
1922 if (DECL_ONE_ONLY (function
))
1923 thunk_node
->add_to_same_comdat_group (funcn
);
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. */
1932 make_thunk (FuncDeclaration
*decl
, int offset
)
1934 tree function
= get_symbol_decl (decl
);
1936 if (!DECL_ARGUMENTS (function
) || !DECL_RESULT (function
))
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
);
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
);
1948 /* Don't build the thunk if the compilation step failed. */
1950 return error_mark_node
;
1952 /* See if we already have the thunk in question. */
1953 for (tree t
= DECL_LANG_THUNKS (function
); t
; t
= DECL_CHAIN (t
))
1955 if (THUNK_LANG_OFFSET (t
) == offset
)
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
;
1965 TREE_READONLY (thunk
) = TREE_READONLY (function
);
1966 TREE_THIS_VOLATILE (thunk
) = TREE_THIS_VOLATILE (function
);
1967 TREE_NOTHROW (thunk
) = TREE_NOTHROW (function
);
1969 DECL_CONTEXT (thunk
) = d_decl_context (decl
);
1971 /* Thunks inherit the public access of the function they are targeting. */
1972 TREE_PUBLIC (thunk
) = TREE_PUBLIC (function
);
1973 /* The thunk has not been defined -- yet. */
1974 DECL_EXTERNAL (thunk
) = 1;
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;
1982 if (TREE_PUBLIC (thunk
))
1984 DECL_VISIBILITY (thunk
) = DECL_VISIBILITY (function
);
1985 DECL_COMDAT (thunk
) = DECL_COMDAT (function
);
1986 DECL_WEAK (thunk
) = DECL_WEAK (function
);
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. */
1994 if (decl
->resolvedLinkage () == LINK::cpp
)
1995 ident
= target
.cpp
.thunkMangle (decl
, offset
);
1998 tree target_name
= DECL_ASSEMBLER_NAME (function
);
1999 unsigned identlen
= IDENTIFIER_LENGTH (target_name
) + 14;
2000 ident
= XNEWVEC (const char, identlen
);
2002 snprintf (CONST_CAST (char *, ident
), identlen
,
2003 "_DTi%u%s", offset
, IDENTIFIER_POINTER (target_name
));
2006 DECL_NAME (thunk
) = get_identifier (ident
);
2007 SET_DECL_ASSEMBLER_NAME (thunk
, DECL_NAME (thunk
));
2011 if (decl
->resolvedLinkage () != LINK::cpp
)
2012 free (CONST_CAST (char *, ident
));
2014 /* Thunks are connected to the definitions of the functions, so thunks are
2015 not produced for external functions. */
2016 if (!DECL_EXTERNAL (function
))
2017 finish_thunk (thunk
, function
);
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
;
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. */
2033 start_function (FuncDeclaration
*fd
)
2035 tree fndecl
= get_symbol_decl (fd
);
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;
2040 DECL_INITIAL (fndecl
) = error_mark_node
;
2042 /* Add this decl to the current binding level. */
2043 d_pushdecl (fndecl
);
2045 /* Save the current function context. */
2046 tree old_context
= current_function_decl
;
2049 push_function_context ();
2051 /* Let GCC know the current scope is this function. */
2052 current_function_decl
= fndecl
;
2054 /* Build the result decl before calling allocate_struct_function. */
2055 DECL_RESULT (fndecl
) = get_fndecl_result (fd
);
2057 /* Initialize the RTL code for the function. */
2058 allocate_struct_function (fndecl
, false);
2060 /* Store the end of the function. */
2061 if (fd
->endloc
.filename
)
2062 cfun
->function_end_locus
= make_location_t (fd
->endloc
);
2064 cfun
->function_end_locus
= DECL_SOURCE_LOCATION (fndecl
);
2066 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
2067 cfun
->language
->function
= fd
;
2069 /* Default chain value is `null' unless parent found. */
2070 cfun
->language
->static_chain
= null_pointer_node
;
2072 /* Find module for this function. */
2073 for (Dsymbol
*p
= fd
->parent
; p
!= NULL
; p
= p
->parent
)
2075 cfun
->language
->module
= p
->isModule ();
2076 if (cfun
->language
->module
)
2079 gcc_assert (cfun
->language
->module
!= NULL
);
2081 /* Begin the statement tree for this function. */
2083 push_binding_level (level_function
);
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. */
2093 finish_function (tree old_context
)
2095 tree fndecl
= current_function_decl
;
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
);
2103 gcc_assert (vec_safe_is_empty (d_function_chain
->stmt_list
));
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
)
2110 tree stmtlist
= alloc_stmt_list ();
2111 append_to_statement_list_force (body
, &stmtlist
);
2112 BIND_EXPR_BODY (bind
) = stmtlist
;
2114 else if (!STATEMENT_LIST_HEAD (body
))
2116 /* For empty functions add a void return. */
2117 append_to_statement_list_force (return_expr (NULL_TREE
), &body
);
2120 DECL_SAVED_TREE (fndecl
) = bind
;
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
);
2126 if (!errorcount
&& !global
.errors
)
2128 /* Dump the D-specific tree IR. */
2129 dump_function (TDI_original
, fndecl
);
2131 cgraph_node::finalize_function (fndecl
, true);
2134 /* We're leaving the context of this function, so free it. */
2135 ggc_free (cfun
->language
);
2136 cfun
->language
= NULL
;
2140 pop_function_context ();
2142 current_function_decl
= old_context
;
2145 /* Mark DECL, which is a VAR_DECL or FUNCTION_DECL as a symbol that
2146 must be emitted in this, output module. */
2149 d_mark_needed (tree decl
)
2151 TREE_USED (decl
) = 1;
2153 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2155 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
2156 node
->forced_by_abi
= true;
2158 else if (VAR_P (decl
))
2160 struct varpool_node
*node
= varpool_node::get_create (decl
);
2161 node
->forced_by_abi
= true;
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. */
2170 get_vtable_decl (ClassDeclaration
*decl
)
2172 if (decl
->vtblsym
&& decl
->vtblsym
->csym
)
2173 return decl
->vtblsym
->csym
;
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. */
2178 tree type
= build_ctype (Type::tvoidptr
->sarrayOf (decl
->vtbl
.length
));
2180 Dsymbol
*vtblsym
= decl
->vtblSymbol ();
2181 vtblsym
->csym
= declare_extern_var (ident
, type
);
2182 DECL_LANG_SPECIFIC (vtblsym
->csym
) = build_lang_decl (NULL
);
2184 /* Class is a reference, want the record type. */
2185 DECL_CONTEXT (vtblsym
->csym
) = TREE_TYPE (build_ctype (decl
->type
));
2186 TREE_READONLY (vtblsym
->csym
) = 1;
2187 DECL_VIRTUAL_P (vtblsym
->csym
) = 1;
2189 SET_DECL_ALIGN (vtblsym
->csym
, TARGET_VTABLE_ENTRY_ALIGN
);
2190 DECL_USER_ALIGN (vtblsym
->csym
) = true;
2192 return vtblsym
->csym
;
2195 /* Helper function of build_class_instance. Find the field inside aggregate
2196 TYPE identified by IDENT at field OFFSET. */
2199 find_aggregate_field (tree type
, tree ident
, tree offset
)
2201 tree fields
= TYPE_FIELDS (type
);
2203 for (tree field
= fields
; field
!= NULL_TREE
; field
= TREE_CHAIN (field
))
2205 if (DECL_NAME (field
) == NULL_TREE
2206 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
2207 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2209 /* Search nesting anonymous structs and unions. */
2210 tree vfield
= find_aggregate_field (TREE_TYPE (field
),
2212 if (vfield
!= NULL_TREE
)
2215 else if (DECL_NAME (field
) == ident
2216 && (offset
== NULL_TREE
2217 || DECL_FIELD_OFFSET (field
) == offset
))
2219 /* Found matching field at offset. */
2227 /* Helper function of build_new_class_expr. Return a constructor that matches
2228 the layout of the class expression EXP. */
2231 build_class_instance (ClassReferenceExp
*exp
)
2233 ClassDeclaration
*cd
= exp
->originalClass ();
2234 tree type
= TREE_TYPE (build_ctype (exp
->value
->stype
));
2235 vec
<constructor_elt
, va_gc
> *ve
= NULL
;
2237 /* The set base vtable field. */
2238 tree vptr
= build_address (get_vtable_decl (cd
));
2239 CONSTRUCTOR_APPEND_ELT (ve
, TYPE_FIELDS (type
), vptr
);
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
)
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. */
2249 for (size_t i
= 0; i
< bcd
->vtblInterfaces
->length
; i
++)
2251 BaseClass
*bc
= (*bcd
->vtblInterfaces
)[i
];
2253 for (ClassDeclaration
*cd2
= cd
; 1; cd2
= cd2
->baseClass
)
2255 gcc_assert (cd2
!= NULL
);
2257 unsigned csymoffset
= base_vtable_offset (cd2
, bc
);
2258 /* If the base class vtable was found. */
2259 if (csymoffset
!= ~0u)
2261 tree csym
= build_address (get_classinfo_decl (cd2
));
2262 csym
= build_offset (csym
, size_int (csymoffset
));
2264 tree field
= find_aggregate_field (type
, NULL_TREE
,
2265 size_int (bc
->offset
));
2266 gcc_assert (field
!= NULL_TREE
);
2268 CONSTRUCTOR_APPEND_ELT (ve
, field
, csym
);
2274 /* Generate initial values of all fields owned by current class.
2275 Use both the name and offset to find the right field. */
2276 for (size_t i
= 0; i
< bcd
->fields
.length
; i
++)
2278 VarDeclaration
*vfield
= bcd
->fields
[i
];
2279 int index
= exp
->findFieldIndexByName (vfield
);
2280 gcc_assert (index
!= -1);
2282 Expression
*value
= (*exp
->value
->elements
)[index
];
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
);
2293 CONSTRUCTOR_APPEND_ELT (ve
, field
, build_expr (value
, true));
2297 return build_struct_literal (type
, ve
);
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.
2305 enum E = new C(1, 2, 3);
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. */
2314 build_new_class_expr (ClassReferenceExp
*expr
)
2316 if (expr
->value
->sym
)
2317 return expr
->value
->sym
;
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");
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);
2327 return expr
->value
->sym
;
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
2332 accessible via TypeInfo, and is also used in `new class' and default
2333 initializing struct literals. */
2336 aggregate_initializer_decl (AggregateDeclaration
*decl
)
2339 return (tree
) decl
->sinit
;
2341 /* Class is a reference, want the record type. */
2342 tree type
= build_ctype (decl
->type
);
2343 StructDeclaration
*sd
= decl
->isStructDeclaration ();
2345 type
= TREE_TYPE (type
);
2347 tree ident
= mangle_internal_decl (decl
, "__init", "Z");
2349 tree sinit
= declare_extern_var (ident
, type
);
2350 DECL_LANG_SPECIFIC (sinit
) = build_lang_decl (NULL
);
2352 DECL_CONTEXT (sinit
) = type
;
2353 TREE_READONLY (sinit
) = 1;
2355 /* Honor struct alignment set by user. */
2356 if (sd
&& !sd
->alignment
.isDefault ())
2358 SET_DECL_ALIGN (sinit
, sd
->alignment
.get () * BITS_PER_UNIT
);
2359 DECL_USER_ALIGN (sinit
) = true;
2362 decl
->sinit
= sinit
;
2366 /* Generate the data for the static initializer. */
2369 layout_class_initializer (ClassDeclaration
*cd
)
2371 NewExp
*ne
= NewExp::create (cd
->loc
, NULL
, cd
->type
, NULL
);
2372 ne
->type
= cd
->type
;
2374 Expression
*e
= ne
->ctfeInterpret ();
2375 gcc_assert (e
->op
== EXP::classReference
);
2377 return build_class_instance (e
->isClassReferenceExp ());
2381 layout_struct_initializer (StructDeclaration
*sd
)
2383 StructLiteralExp
*sle
= StructLiteralExp::create (sd
->loc
, sd
, NULL
);
2385 if (!sd
->fill (sd
->loc
, *sle
->elements
, true))
2388 sle
->type
= sd
->type
;
2389 return build_expr (sle
, true);
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. */
2398 enum_initializer_decl (EnumDeclaration
*decl
)
2403 gcc_assert (decl
->ident
);
2405 tree type
= build_ctype (decl
->type
);
2406 tree ident
= mangle_internal_decl (decl
, "__init", "Z");
2408 decl
->sinit
= declare_extern_var (ident
, type
);
2409 DECL_LANG_SPECIFIC (decl
->sinit
) = build_lang_decl (NULL
);
2411 DECL_CONTEXT (decl
->sinit
) = d_decl_context (decl
);
2412 TREE_READONLY (decl
->sinit
) = 1;
2417 /* Return an anonymous static variable of type TYPE, initialized with INIT,
2418 and optionally prefixing the name with PREFIX. */
2421 build_artificial_decl (tree type
, tree init
, const char *prefix
)
2423 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, NULL_TREE
, type
);
2424 const char *name
= prefix
? prefix
: "___s";
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
);
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;
2437 /* Perhaps at some point the initializer constant should be hashed
2438 to remove duplicates. */
2439 DECL_INITIAL (decl
) = init
;
2444 /* Build TYPE_DECL for the declaration DSYM. */
2447 build_type_decl (tree type
, Dsymbol
*dsym
)
2449 if (TYPE_STUB_DECL (type
))
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 ();
2457 tree decl
= build_decl (make_location_t (dsym
->loc
), TYPE_DECL
,
2458 get_identifier (name
), type
);
2459 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (d_mangle_decl (dsym
)));
2460 TREE_PUBLIC (decl
) = 1;
2461 DECL_CONTEXT (decl
) = d_decl_context (dsym
);
2463 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
2464 TYPE_NAME (type
) = decl
;
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
))
2470 DECL_ARTIFICIAL (decl
) = 1;
2471 TYPE_STUB_DECL (type
) = decl
;
2473 else if (type
!= TYPE_MAIN_VARIANT (type
))
2474 DECL_ORIGINAL_TYPE (decl
) = TYPE_MAIN_VARIANT (type
);
2477 /* Create a declaration for field NAME of a given TYPE, setting the flags
2478 for whether the field is ARTIFICIAL and/or IGNORED. */
2481 create_field_decl (tree type
, const char *name
, int artificial
, int ignored
)
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
;
2491 /* Return the COMDAT group into which DECL should be placed. */
2494 d_comdat_group (tree decl
)
2496 /* If already part of a comdat group, use that. */
2497 if (DECL_COMDAT_GROUP (decl
))
2498 return DECL_COMDAT_GROUP (decl
);
2500 return DECL_ASSEMBLER_NAME (decl
);
2503 /* Set DECL up to have the closest approximation of "initialized common"
2504 linkage available. */
2507 d_comdat_linkage (tree decl
)
2509 /* COMDAT definitions have to be public. */
2510 gcc_assert (TREE_PUBLIC (decl
));
2512 if (supports_one_only ())
2513 make_decl_one_only (decl
, d_comdat_group (decl
));
2514 else if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INSTANTIATED (decl
))
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;
2524 if (TREE_PUBLIC (decl
) && DECL_INSTANTIATED (decl
))
2525 DECL_COMDAT (decl
) = 1;
2528 /* Set DECL up to have the closest approximation of "weak" linkage. */
2531 d_weak_linkage (tree decl
)
2533 /* Weak definitions have to be public. */
2534 gcc_assert (TREE_PUBLIC (decl
));
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
);
2540 declare_weak (decl
);
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. */
2547 set_linkage_for_decl (tree decl
)
2549 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
) && TREE_STATIC (decl
));
2551 /* Non-public decls keep their internal linkage. */
2552 if (!TREE_PUBLIC (decl
))
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
)))
2559 return d_comdat_linkage (decl
);
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
))
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
)
2571 /* Make sure that instantiations are not removed. */
2572 if (flag_weak_templates
&& DECL_INSTANTIATED (decl
))
2573 d_mark_needed (decl
);
2575 return d_comdat_linkage (decl
);
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
);
2583 /* Compiler generated public symbols can appear in multiple contexts. */
2584 if (DECL_ARTIFICIAL (decl
))
2585 return d_weak_linkage (decl
);
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. */
2592 set_visibility_for_decl (tree node
, Dsymbol
*sym
)
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;
2600 /* If the declaration was declared `export', append either the dllimport
2601 or dllexport attribute. */
2602 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
)
2604 const char *attrname
= NULL
;
2606 /* Have to test for import first. */
2607 if (sym
->isImportedSymbol ())
2608 attrname
= "dllimport";
2609 else if (sym
->isExport ())
2610 attrname
= "dllexport";
2612 if (attrname
!= NULL
)
2615 insert_decl_attribute (node
, attrname
);
2616 else if (TYPE_P (node
))
2617 insert_type_attribute (node
, attrname
);