1 /* decl.cc -- Lower D frontend declarations to GCC trees.
2 Copyright (C) 2006-2020 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"
64 /* Return identifier for the external mangled name of DECL. */
67 d_mangle_decl (Dsymbol
*decl
)
69 if (decl
->isFuncDeclaration ())
70 return mangleExact ((FuncDeclaration
*) decl
);
74 mangleToBuffer (decl
, &buf
);
75 return buf
.extractChars ();
79 /* Generate a mangled identifier using NAME and SUFFIX, prefixed by the
80 assembler name for DECL. */
83 mangle_internal_decl (Dsymbol
*decl
, const char *name
, const char *suffix
)
85 const char *prefix
= d_mangle_decl (decl
);
86 unsigned namelen
= strlen (name
);
87 unsigned buflen
= (2 + strlen (prefix
) + namelen
+ strlen (suffix
)) * 2;
88 char *buf
= (char *) alloca (buflen
);
90 snprintf (buf
, buflen
, "_D%s%u%s%s", prefix
, namelen
, name
, suffix
);
91 tree ident
= get_identifier (buf
);
93 /* Symbol is not found in user code, but generate a readable name for it
94 anyway for debug and diagnostic reporting. */
95 snprintf (buf
, buflen
, "%s.%s", decl
->toPrettyChars (), name
);
96 IDENTIFIER_PRETTY_NAME (ident
) = get_identifier (buf
);
101 /* Returns true if DECL is from the gcc.attribute module. */
104 gcc_attribute_p (Dsymbol
*decl
)
106 ModuleDeclaration
*md
= decl
->getModule ()->md
;
108 if (md
&& md
->packages
&& md
->packages
->length
== 1)
110 if (!strcmp ((*md
->packages
)[0]->toChars (), "gcc")
111 && !strcmp (md
->id
->toChars (), "attribute"))
118 /* Implements the visitor interface to lower all Declaration AST classes
119 emitted from the D Front-end to GCC trees.
120 All visit methods accept one parameter D, which holds the frontend AST
121 of the declaration to compile. These also don't return any value, instead
122 generated code are appened to global_declarations or added to the
123 current_binding_level by d_pushdecl(). */
125 class DeclVisitor
: public Visitor
127 using Visitor::visit
;
129 /* If we're lowering the body of a version(unittest) condition. */
130 bool in_version_unittest_
;
135 this->in_version_unittest_
= false;
138 /* Helper for generating code for the dsymbol AST class D.
139 Sets up the location of the symbol before lowering. */
141 void build_dsymbol (Dsymbol
*d
)
143 location_t saved_location
= input_location
;
144 input_location
= make_location_t (d
->loc
);
146 input_location
= saved_location
;
149 /* This should be overridden by each declaration class. */
151 void visit (Dsymbol
*)
155 /* Compile a D module, and all members of it. */
157 void visit (Module
*d
)
159 if (d
->semanticRun
>= PASSobj
)
162 build_module_tree (d
);
163 d
->semanticRun
= PASSobj
;
166 /* Write the imported symbol to debug. */
168 void visit (Import
*d
)
170 if (d
->semanticRun
>= PASSobj
)
173 /* Implements import declarations by telling the debug back-end we are
174 importing the NAMESPACE_DECL of the module or IMPORTED_DECL of the
175 declaration into the current lexical scope CONTEXT. NAME is set if
176 this is a renamed import. */
180 /* Get the context of this import, this should never be null. */
181 tree context
= d_module_context ();
183 if (d
->ident
== NULL
)
185 /* Importing declaration list. */
186 for (size_t i
= 0; i
< d
->names
.length
; i
++)
188 AliasDeclaration
*aliasdecl
= d
->aliasdecls
[i
];
189 tree decl
= build_import_decl (aliasdecl
);
191 /* Skip over unhandled imports. */
192 if (decl
== NULL_TREE
)
195 Identifier
*alias
= d
->aliases
[i
];
196 tree name
= (alias
!= NULL
)
197 ? get_identifier (alias
->toChars ()) : NULL_TREE
;
199 debug_hooks
->imported_module_or_decl (decl
, name
, context
,
205 /* Importing the entire module. */
206 tree decl
= build_import_decl (d
->mod
);
208 tree name
= (d
->aliasId
!= NULL
)
209 ? get_identifier (d
->aliasId
->toChars ()) : NULL_TREE
;
211 debug_hooks
->imported_module_or_decl (decl
, name
, context
,
215 d
->semanticRun
= PASSobj
;
218 /* Expand any local variables found in tuples. */
220 void visit (TupleDeclaration
*d
)
222 for (size_t i
= 0; i
< d
->objects
->length
; i
++)
224 RootObject
*o
= (*d
->objects
)[i
];
225 if (o
->dyncast () == DYNCAST_EXPRESSION
)
227 DsymbolExp
*de
= ((Expression
*) o
)->isDsymbolExp ();
228 if (de
!= NULL
&& de
->s
->isDeclaration ())
229 this->build_dsymbol (de
->s
);
234 /* Walk over all declarations in the attribute scope. */
236 void visit (AttribDeclaration
*d
)
238 Dsymbols
*ds
= d
->include (NULL
);
243 for (size_t i
= 0; i
< ds
->length
; i
++)
244 this->build_dsymbol ((*ds
)[i
]);
247 /* Pragmas are a way to pass special information to the compiler and to add
248 vendor specific extensions to D. We don't do anything here, yet. */
250 void visit (PragmaDeclaration
*d
)
252 if (!global
.params
.ignoreUnsupportedPragmas
)
254 if (d
->ident
== Identifier::idPool ("lib")
255 || d
->ident
== Identifier::idPool ("startaddress"))
257 warning_at (make_location_t (d
->loc
), OPT_Wunknown_pragmas
,
258 "pragma(%s) not implemented", d
->ident
->toChars ());
262 visit ((AttribDeclaration
*) d
);
265 /* Conditional compilation is the process of selecting which code to compile
266 and which code to not compile. Look for version conditions that may */
268 void visit (ConditionalDeclaration
*d
)
270 bool old_condition
= this->in_version_unittest_
;
272 if (global
.params
.useUnitTests
)
274 VersionCondition
*vc
= d
->condition
->isVersionCondition ();
275 if (vc
&& vc
->ident
== Identifier::idPool ("unittest"))
276 this->in_version_unittest_
= true;
279 visit ((AttribDeclaration
*) d
);
281 this->in_version_unittest_
= old_condition
;
284 /* Walk over all members in the namespace scope. */
286 void visit (Nspace
*d
)
288 if (isError (d
) || !d
->members
)
291 for (size_t i
= 0; i
< d
->members
->length
; i
++)
292 this->build_dsymbol ((*d
->members
)[i
]);
295 /* Templates are D's approach to generic programming. They have no members
296 that can be emitted, however if the template is nested and used as a
297 voldemort type, then it's members must be compiled before the parent
298 function finishes. */
300 void visit (TemplateDeclaration
*d
)
302 /* Type cannot be directly named outside of the scope it's declared in, so
303 the only way it can be escaped is if the function has auto return. */
304 FuncDeclaration
*fd
= d_function_chain
? d_function_chain
->function
: NULL
;
306 if (!fd
|| !fd
->isAuto ())
309 /* Check if the function returns an instantiated type that may contain
310 nested members. Only applies to classes or structs. */
311 Type
*tb
= fd
->type
->nextOf ()->baseElemOf ();
313 while (tb
->ty
== Tarray
|| tb
->ty
== Tpointer
)
314 tb
= tb
->nextOf ()->baseElemOf ();
316 TemplateInstance
*ti
= NULL
;
318 if (tb
->ty
== Tstruct
)
319 ti
= tb
->isTypeStruct ()->sym
->isInstantiated ();
320 else if (tb
->ty
== Tclass
)
321 ti
= tb
->isTypeClass ()->sym
->isInstantiated ();
323 /* Return type is instantiated from this template declaration, walk over
324 all members of the instance. */
325 if (ti
&& ti
->tempdecl
== d
)
326 this->build_dsymbol (ti
);
329 /* Walk over all members in the instantiated template. */
331 void visit (TemplateInstance
*d
)
333 if (isError (d
)|| !d
->members
)
336 if (!d
->needsCodegen ())
339 for (size_t i
= 0; i
< d
->members
->length
; i
++)
340 this->build_dsymbol ((*d
->members
)[i
]);
343 /* Walk over all members in the mixin template scope. */
345 void visit (TemplateMixin
*d
)
347 if (isError (d
)|| !d
->members
)
350 for (size_t i
= 0; i
< d
->members
->length
; i
++)
351 this->build_dsymbol ((*d
->members
)[i
]);
354 /* Write out compiler generated TypeInfo, initializer and functions for the
355 given struct declaration, walking over all static members. */
357 void visit (StructDeclaration
*d
)
359 if (d
->semanticRun
>= PASSobj
)
362 if (d
->type
->ty
== Terror
)
364 error_at (make_location_t (d
->loc
),
365 "had semantic errors when compiling");
369 /* Add this decl to the current binding level. */
370 tree ctype
= build_ctype (d
->type
);
371 if (TYPE_NAME (ctype
))
372 d_pushdecl (TYPE_NAME (ctype
));
374 /* Anonymous structs/unions only exist as part of others,
375 do not output forward referenced structs. */
376 if (d
->isAnonymous () || !d
->members
)
379 /* Don't emit any symbols from gcc.attribute module. */
380 if (gcc_attribute_p (d
))
383 /* Generate TypeInfo. */
384 if (have_typeinfo_p (Type::dtypeinfo
))
385 create_typeinfo (d
->type
, NULL
);
387 /* Generate static initializer. */
388 d
->sinit
= aggregate_initializer_decl (d
);
389 DECL_INITIAL (d
->sinit
) = layout_struct_initializer (d
);
391 if (d
->isInstantiated ())
392 d_linkonce_linkage (d
->sinit
);
394 d_finish_decl (d
->sinit
);
396 /* Put out the members. There might be static constructors in the members
397 list, and they cannot be put in separate object files. */
398 for (size_t i
= 0; i
< d
->members
->length
; i
++)
399 this->build_dsymbol ((*d
->members
)[i
]);
401 /* Put out xopEquals, xopCmp and xopHash. */
402 if (d
->xeq
&& d
->xeq
!= d
->xerreq
)
403 this->build_dsymbol (d
->xeq
);
405 if (d
->xcmp
&& d
->xcmp
!= d
->xerrcmp
)
406 this->build_dsymbol (d
->xcmp
);
409 this->build_dsymbol (d
->xhash
);
411 d
->semanticRun
= PASSobj
;
414 /* Finish semantic analysis of functions in vtbl for class CD. */
416 bool finish_vtable (ClassDeclaration
*d
)
418 bool has_errors
= false;
420 /* Finish semantic analysis of functions in vtbl[]. */
421 for (size_t i
= d
->vtblOffset (); i
< d
->vtbl
.length
; i
++)
423 FuncDeclaration
*fd
= d
->vtbl
[i
]->isFuncDeclaration ();
425 if (!fd
|| (!fd
->fbody
&& d
->isAbstract ()))
428 /* Ensure function has a return value. */
429 if (!fd
->functionSemantic ())
432 /* No name hiding to check for. */
433 if (!d
->isFuncHidden (fd
) || fd
->isFuture ())
436 /* The function fd is hidden from the view of the class.
437 If it overlaps with any function in the vtbl[], then
439 for (size_t j
= 1; j
< d
->vtbl
.length
; j
++)
444 FuncDeclaration
*fd2
= d
->vtbl
[j
]->isFuncDeclaration ();
445 if (!fd2
->ident
->equals (fd
->ident
))
448 /* The function is marked as @__future, a deprecation has
449 already been given by the frontend. */
450 if (fd2
->isFuture ())
453 if (fd
->leastAsSpecialized (fd2
) || fd2
->leastAsSpecialized (fd
))
455 error_at (make_location_t (fd
->loc
), "use of %qs",
456 fd
->toPrettyChars ());
457 inform (make_location_t (fd2
->loc
), "is hidden by %qs",
458 fd2
->toPrettyChars ());
459 inform (make_location_t (d
->loc
),
460 "use %<alias %s = %s.%s;%> to introduce base class "
461 "overload set", fd
->toChars (),
462 fd
->parent
->toChars (), fd
->toChars ());
472 /* Write out compiler generated TypeInfo, initializer and vtables for the
473 given class declaration, walking over all static members. */
475 void visit (ClassDeclaration
*d
)
477 if (d
->semanticRun
>= PASSobj
)
480 if (d
->type
->ty
== Terror
)
482 error_at (make_location_t (d
->loc
),
483 "had semantic errors when compiling");
490 /* Put out the members. */
491 for (size_t i
= 0; i
< d
->members
->length
; i
++)
492 this->build_dsymbol ((*d
->members
)[i
]);
494 /* If something goes wrong during final semantic pass, don't bother with
495 the rest as we may have incomplete info. */
496 if (!this->finish_vtable (d
))
499 /* Generate C symbols. */
500 d
->csym
= get_classinfo_decl (d
);
501 d
->vtblsym
= get_vtable_decl (d
);
502 d
->sinit
= aggregate_initializer_decl (d
);
504 /* Generate static initializer. */
505 DECL_INITIAL (d
->sinit
) = layout_class_initializer (d
);
506 d_linkonce_linkage (d
->sinit
);
507 d_finish_decl (d
->sinit
);
509 /* Put out the TypeInfo. */
510 if (have_typeinfo_p (Type::dtypeinfo
))
511 create_typeinfo (d
->type
, NULL
);
513 DECL_INITIAL (d
->csym
) = layout_classinfo (d
);
514 d_linkonce_linkage (d
->csym
);
515 d_finish_decl (d
->csym
);
517 /* Put out the vtbl[]. */
518 vec
<constructor_elt
, va_gc
> *elms
= NULL
;
520 /* First entry is ClassInfo reference. */
521 if (d
->vtblOffset ())
522 CONSTRUCTOR_APPEND_ELT (elms
, size_zero_node
, build_address (d
->csym
));
524 for (size_t i
= d
->vtblOffset (); i
< d
->vtbl
.length
; i
++)
526 FuncDeclaration
*fd
= d
->vtbl
[i
]->isFuncDeclaration ();
528 if (fd
&& (fd
->fbody
|| !d
->isAbstract ()))
530 CONSTRUCTOR_APPEND_ELT (elms
, size_int (i
),
531 build_address (get_symbol_decl (fd
)));
535 DECL_INITIAL (d
->vtblsym
)
536 = build_constructor (TREE_TYPE (d
->vtblsym
), elms
);
537 d_comdat_linkage (d
->vtblsym
);
538 d_finish_decl (d
->vtblsym
);
540 /* Add this decl to the current binding level. */
541 tree ctype
= TREE_TYPE (build_ctype (d
->type
));
542 if (TYPE_NAME (ctype
))
543 d_pushdecl (TYPE_NAME (ctype
));
545 d
->semanticRun
= PASSobj
;
548 /* Write out compiler generated TypeInfo and vtables for the given interface
549 declaration, walking over all static members. */
551 void visit (InterfaceDeclaration
*d
)
553 if (d
->semanticRun
>= PASSobj
)
556 if (d
->type
->ty
== Terror
)
558 error_at (make_location_t (d
->loc
),
559 "had semantic errors when compiling");
566 /* Put out the members. */
567 for (size_t i
= 0; i
< d
->members
->length
; i
++)
568 this->build_dsymbol ((*d
->members
)[i
]);
570 /* Generate C symbols. */
571 d
->csym
= get_classinfo_decl (d
);
573 /* Put out the TypeInfo. */
574 if (have_typeinfo_p (Type::dtypeinfo
))
576 create_typeinfo (d
->type
, NULL
);
577 this->build_dsymbol (d
->type
->vtinfo
);
580 DECL_INITIAL (d
->csym
) = layout_classinfo (d
);
581 d_linkonce_linkage (d
->csym
);
582 d_finish_decl (d
->csym
);
584 /* Add this decl to the current binding level. */
585 tree ctype
= TREE_TYPE (build_ctype (d
->type
));
586 if (TYPE_NAME (ctype
))
587 d_pushdecl (TYPE_NAME (ctype
));
589 d
->semanticRun
= PASSobj
;
592 /* Write out compiler generated TypeInfo and initializer for the given
595 void visit (EnumDeclaration
*d
)
597 if (d
->semanticRun
>= PASSobj
)
600 if (d
->errors
|| d
->type
->ty
== Terror
)
602 error_at (make_location_t (d
->loc
),
603 "had semantic errors when compiling");
607 if (d
->isAnonymous ())
610 /* Generate TypeInfo. */
611 if (have_typeinfo_p (Type::dtypeinfo
))
612 create_typeinfo (d
->type
, NULL
);
614 TypeEnum
*tc
= d
->type
->isTypeEnum ();
615 if (tc
->sym
->members
&& !d
->type
->isZeroInit ())
617 /* Generate static initializer. */
618 d
->sinit
= enum_initializer_decl (d
);
619 DECL_INITIAL (d
->sinit
) = build_expr (tc
->sym
->defaultval
, true);
621 if (d
->isInstantiated ())
622 d_linkonce_linkage (d
->sinit
);
624 d_finish_decl (d
->sinit
);
627 /* Add this decl to the current binding level. */
628 tree ctype
= build_ctype (d
->type
);
629 if (TYPE_NAME (ctype
))
630 d_pushdecl (TYPE_NAME (ctype
));
632 d
->semanticRun
= PASSobj
;
635 /* Finish up a variable declaration and push it into the current scope.
636 This can either be a static, local or manifest constant. */
638 void visit (VarDeclaration
*d
)
640 if (d
->semanticRun
>= PASSobj
)
643 if (d
->type
->ty
== Terror
)
645 error_at (make_location_t (d
->loc
),
646 "had semantic errors when compiling");
652 this->build_dsymbol (d
->toAlias ());
656 /* Do not store variables we cannot take the address of,
657 but keep the values for purposes of debugging. */
658 if (!d
->canTakeAddressOf ())
660 /* Don't know if there is a good way to handle instantiations. */
661 if (d
->isInstantiated ())
664 /* Cannot make an expression out of a void initializer. */
665 if (!d
->_init
|| d
->_init
->isVoidInitializer ())
668 tree decl
= get_symbol_decl (d
);
669 Expression
*ie
= initializerToExpression (d
->_init
);
671 /* CONST_DECL was initially intended for enumerals and may be used for
672 scalars in general, but not for aggregates. Here a non-constant
673 value is generated anyway so as the CONST_DECL only serves as a
674 placeholder for the value, however the DECL itself should never be
675 referenced in any generated code, or passed to the back-end. */
676 if (!d
->type
->isscalar ())
677 DECL_INITIAL (decl
) = build_expr (ie
, false);
680 DECL_INITIAL (decl
) = build_expr (ie
, true);
682 rest_of_decl_compilation (decl
, 1, 0);
685 else if (d
->isDataseg () && !(d
->storage_class
& STCextern
))
687 tree decl
= get_symbol_decl (d
);
689 /* Duplicated VarDeclarations map to the same symbol. Check if this
690 is the one declaration which will be emitted. */
691 tree ident
= DECL_ASSEMBLER_NAME (decl
);
692 if (IDENTIFIER_DSYMBOL (ident
) && IDENTIFIER_DSYMBOL (ident
) != d
)
695 /* How big a symbol can be should depend on back-end. */
696 tree size
= build_integer_cst (d
->type
->size (d
->loc
),
697 build_ctype (Type::tsize_t
));
698 if (!valid_constant_size_p (size
))
700 error_at (make_location_t (d
->loc
), "size is too large");
706 /* Use the explicit initializer, this includes `void`. */
707 if (!d
->_init
->isVoidInitializer ())
709 Expression
*e
= initializerToExpression (d
->_init
, d
->type
);
710 DECL_INITIAL (decl
) = build_expr (e
, true);
715 /* Use default initializer for the type. */
716 if (TypeStruct
*ts
= d
->type
->isTypeStruct ())
717 DECL_INITIAL (decl
) = layout_struct_initializer (ts
->sym
);
720 Expression
*e
= d
->type
->defaultInitLiteral (d
->loc
);
721 DECL_INITIAL (decl
) = build_expr (e
, true);
725 /* Frontend should have already caught this. */
726 gcc_assert (!integer_zerop (size
)
727 || d
->type
->toBasetype ()->ty
== Tsarray
);
729 d_finish_decl (decl
);
731 /* Maybe record the var against the current module. */
732 register_module_decl (d
);
734 else if (!d
->isDataseg () && !d
->isMember ())
736 /* This is needed for VarDeclarations in mixins that are to be local
737 variables of a function. Otherwise, it would be enough to make
738 a check for isVarDeclaration() in DeclarationExp codegen. */
739 declare_local_var (d
);
741 if (d
->_init
&& !d
->_init
->isVoidInitializer ())
743 tree decl
= get_symbol_decl (d
);
745 ExpInitializer
*vinit
= d
->_init
->isExpInitializer ();
746 Expression
*ie
= initializerToExpression (vinit
);
747 tree exp
= build_expr (ie
);
749 /* Maybe put variable on list of things needing destruction. */
750 if (d
->needsScopeDtor ())
752 vec_safe_push (d_function_chain
->vars_in_scope
, decl
);
753 /* Force a TARGET_EXPR to add the corresponding cleanup. */
754 exp
= force_target_expr (compound_expr (exp
, decl
));
755 TARGET_EXPR_CLEANUP (exp
) = build_expr (d
->edtor
);
762 d
->semanticRun
= PASSobj
;
765 /* Generate and compile a static TypeInfo declaration, but only if it is
766 needed in the current compilation. */
768 void visit (TypeInfoDeclaration
*d
)
770 if (d
->semanticRun
>= PASSobj
)
773 if (speculative_type_p (d
->tinfo
))
776 tree t
= get_typeinfo_decl (d
);
777 DECL_INITIAL (t
) = layout_typeinfo (d
);
779 d
->semanticRun
= PASSobj
;
782 /* Finish up a function declaration and compile it all the way
783 down to assembler language output. */
785 void visit (FuncDeclaration
*d
)
787 /* Already generated the function. */
788 if (d
->semanticRun
>= PASSobj
)
791 /* Don't emit any symbols from gcc.attribute module. */
792 if (gcc_attribute_p (d
))
795 /* Not emitting unittest functions. */
796 if (!global
.params
.useUnitTests
&& d
->isUnitTestDeclaration ())
799 /* Check if any errors occurred when running semantic. */
800 if (TypeFunction
*tf
= d
->type
->isTypeFunction ())
802 if (tf
->next
== NULL
|| tf
->next
->ty
== Terror
)
806 if (d
->semantic3Errors
)
811 FuncDeclaration
*fdp
= d
;
812 while (fdp
&& fdp
->isNested ())
814 fdp
= fdp
->toParent2 ()->isFuncDeclaration ();
819 /* Parent failed to compile, but errors were gagged. */
820 if (fdp
->semantic3Errors
)
825 /* Ensure all semantic passes have run. */
826 if (d
->semanticRun
< PASSsemantic3
)
828 d
->functionSemantic3 ();
829 Module::runDeferredSemantic3 ();
835 /* Duplicated FuncDeclarations map to the same symbol. Check if this
836 is the one declaration which will be emitted. */
837 tree fndecl
= get_symbol_decl (d
);
838 tree ident
= DECL_ASSEMBLER_NAME (fndecl
);
839 if (IDENTIFIER_DSYMBOL (ident
) && IDENTIFIER_DSYMBOL (ident
) != d
)
844 rest_of_decl_compilation (fndecl
, 1, 0);
848 if (global
.params
.verbose
)
849 message ("function %s", d
->toPrettyChars ());
851 /* Start generating code for this function. */
852 gcc_assert (d
->semanticRun
== PASSsemantic3done
);
853 d
->semanticRun
= PASSobj
;
855 tree old_context
= start_function (d
);
857 tree parm_decl
= NULL_TREE
;
858 tree param_list
= NULL_TREE
;
860 /* Special arguments... */
863 For nested functions, D still generates a vthis, but it
864 should not be referenced in any expression. */
867 parm_decl
= get_symbol_decl (d
->vthis
);
868 DECL_ARTIFICIAL (parm_decl
) = 1;
869 TREE_READONLY (parm_decl
) = 1;
871 if (d
->vthis
->type
== Type::tvoidptr
)
873 /* Replace generic pointer with back-end closure type
874 (this wins for gdb). */
875 tree frame_type
= FRAMEINFO_TYPE (get_frameinfo (d
));
876 gcc_assert (frame_type
!= NULL_TREE
);
877 TREE_TYPE (parm_decl
) = build_pointer_type (frame_type
);
880 param_list
= chainon (param_list
, parm_decl
);
881 d_function_chain
->static_chain
= parm_decl
;
884 /* _arguments parameter. */
887 parm_decl
= get_symbol_decl (d
->v_arguments
);
888 param_list
= chainon (param_list
, parm_decl
);
891 /* formal function parameters. */
892 size_t n_parameters
= d
->parameters
? d
->parameters
->length
: 0;
894 for (size_t i
= 0; i
< n_parameters
; i
++)
896 VarDeclaration
*param
= (*d
->parameters
)[i
];
897 parm_decl
= get_symbol_decl (param
);
898 /* Chain them in the correct order. */
899 param_list
= chainon (param_list
, parm_decl
);
902 DECL_ARGUMENTS (fndecl
) = param_list
;
903 DECL_IN_UNITTEST_CONDITION_P (fndecl
) = this->in_version_unittest_
;
904 rest_of_decl_compilation (fndecl
, 1, 0);
906 /* If this is a member function that nested (possibly indirectly) in another
907 function, construct an expession for this member function's static chain
908 by going through parent link of nested classes. */
911 AggregateDeclaration
*ad
= d
->isThis ();
912 tree this_tree
= get_symbol_decl (d
->vthis
);
914 while (ad
->isNested ())
916 Dsymbol
*pd
= ad
->toParent2 ();
917 tree vthis_field
= get_symbol_decl (ad
->vthis
);
918 this_tree
= component_ref (build_deref (this_tree
), vthis_field
);
920 ad
= pd
->isAggregateDeclaration ();
923 cfun
->language
->static_chain
= this_tree
;
929 /* May change cfun->static_chain. */
933 declare_local_var (d
->vresult
);
938 /* Named return value optimisation support for D.
939 Implemented by overriding all the RETURN_EXPRs and replacing all
940 occurrences of VAR with the RESULT_DECL for the function.
941 This is only worth doing for functions that can return in memory. */
942 tree resdecl
= DECL_RESULT (fndecl
);
944 if (TREE_ADDRESSABLE (TREE_TYPE (resdecl
))
945 || aggregate_value_p (TREE_TYPE (resdecl
), fndecl
))
947 /* Return non-trivial structs by invisible reference. */
948 if (TREE_ADDRESSABLE (TREE_TYPE (resdecl
)))
950 TREE_TYPE (resdecl
) = build_reference_type (TREE_TYPE (resdecl
));
951 DECL_BY_REFERENCE (resdecl
) = 1;
952 TREE_ADDRESSABLE (resdecl
) = 0;
953 relayout_decl (resdecl
);
954 d
->shidden
= build_deref (resdecl
);
957 d
->shidden
= resdecl
;
959 if (d
->nrvo_can
&& d
->nrvo_var
)
961 tree var
= get_symbol_decl (d
->nrvo_var
);
963 /* Copy name from VAR to RESULT. */
964 DECL_NAME (resdecl
) = DECL_NAME (var
);
965 /* Don't forget that we take its address. */
966 TREE_ADDRESSABLE (var
) = 1;
968 SET_DECL_VALUE_EXPR (var
, resdecl
);
969 DECL_HAS_VALUE_EXPR_P (var
) = 1;
970 SET_DECL_LANG_NRVO (var
, d
->shidden
);
974 build_function_body (d
);
976 /* Initialize the _argptr variable. */
979 tree body
= pop_stmt_list ();
980 tree var
= get_decl_tree (d
->v_argptr
);
981 var
= build_address (var
);
983 tree init
= build_call_expr (builtin_decl_explicit (BUILT_IN_VA_START
),
985 declare_local_var (d
->v_argptr
);
988 tree cleanup
= build_call_expr (builtin_decl_explicit (BUILT_IN_VA_END
),
990 add_stmt (build2 (TRY_FINALLY_EXPR
, void_type_node
, body
, cleanup
));
993 finish_function (old_context
);
995 /* Maybe record the function against the current module. */
996 register_module_decl (d
);
1000 /* Main entry point for the DeclVisitor interface to send
1001 the Declaration AST class D to GCC back-end. */
1004 build_decl_tree (Dsymbol
*d
)
1006 location_t saved_location
= input_location
;
1008 /* Set input location, empty DECL_SOURCE_FILE can crash debug generator. */
1009 if (d
->loc
.filename
)
1010 input_location
= make_location_t (d
->loc
);
1012 input_location
= make_location_t (Loc ("<no_file>", 1, 0));
1014 DeclVisitor v
= DeclVisitor ();
1015 v
.build_dsymbol (d
);
1017 input_location
= saved_location
;
1020 /* Return the decl for the symbol, create it if it doesn't already exist. */
1023 get_symbol_decl (Declaration
*decl
)
1028 /* Deal with placeholder symbols immediately:
1029 SymbolDeclaration is used as a shell around an initializer symbol. */
1030 SymbolDeclaration
*sd
= decl
->isSymbolDeclaration ();
1033 decl
->csym
= aggregate_initializer_decl (sd
->dsym
);
1037 /* Global static TypeInfo declaration. */
1038 if (decl
->isTypeInfoDeclaration ())
1039 return get_typeinfo_decl ((TypeInfoDeclaration
*) decl
);
1041 /* FuncAliasDeclaration is used to import functions from another scope. */
1042 FuncAliasDeclaration
*fad
= decl
->isFuncAliasDeclaration ();
1045 decl
->csym
= get_symbol_decl (fad
->funcalias
);
1049 /* It is possible for a field declaration symbol to be requested
1050 before the parent type has been built. */
1051 if (decl
->isField ())
1053 AggregateDeclaration
*ad
= decl
->toParent ()->isAggregateDeclaration ();
1054 gcc_assert (ad
!= NULL
);
1056 /* Finishing off the type should create the associated FIELD_DECL. */
1057 build_ctype (ad
->type
);
1058 gcc_assert (decl
->csym
!= NULL
);
1063 /* Build the tree for the symbol. */
1064 FuncDeclaration
*fd
= decl
->isFuncDeclaration ();
1067 /* Run full semantic on functions we need to know about. */
1068 if (!fd
->functionSemantic ())
1070 decl
->csym
= error_mark_node
;
1074 decl
->csym
= build_decl (make_location_t (decl
->loc
), FUNCTION_DECL
,
1075 get_identifier (decl
->ident
->toChars ()),
1078 /* Set function type afterwards as there could be self references. */
1079 TREE_TYPE (decl
->csym
) = build_ctype (fd
->type
);
1081 /* Set DECL_INITIAL now if the function has a definition. */
1083 DECL_INITIAL (decl
->csym
) = error_mark_node
;
1085 DECL_EXTERNAL (decl
->csym
) = 1;
1089 /* Build the variable declaration. */
1090 VarDeclaration
*vd
= decl
->isVarDeclaration ();
1091 gcc_assert (vd
!= NULL
);
1093 tree_code code
= vd
->isParameter () ? PARM_DECL
1094 : !vd
->canTakeAddressOf () ? CONST_DECL
1096 decl
->csym
= build_decl (make_location_t (decl
->loc
), code
,
1097 get_identifier (decl
->ident
->toChars ()),
1098 declaration_type (vd
));
1100 /* If any alignment was set on the declaration. */
1101 if (vd
->alignment
!= STRUCTALIGN_DEFAULT
)
1103 SET_DECL_ALIGN (decl
->csym
, vd
->alignment
* BITS_PER_UNIT
);
1104 DECL_USER_ALIGN (decl
->csym
) = 1;
1107 if (vd
->storage_class
& STCextern
)
1108 DECL_EXTERNAL (decl
->csym
) = 1;
1111 /* Set the declaration mangled identifier if static. */
1112 if (decl
->isCodeseg () || decl
->isDataseg ())
1116 if (decl
->mangleOverride
.length
)
1119 get_identifier_with_length (decl
->mangleOverride
.ptr
,
1120 decl
->mangleOverride
.length
);
1123 mangled_name
= get_identifier (d_mangle_decl (decl
));
1125 mangled_name
= targetm
.mangle_decl_assembler_name (decl
->csym
,
1127 /* The frontend doesn't handle duplicate definitions of unused symbols
1128 with the same mangle. So a check is done here instead. */
1129 if (IDENTIFIER_DSYMBOL (mangled_name
))
1131 Declaration
*other
= IDENTIFIER_DSYMBOL (mangled_name
);
1132 tree olddecl
= decl
->csym
;
1133 decl
->csym
= get_symbol_decl (other
);
1135 /* The current declaration is a prototype or marked extern, merge
1136 applied user attributes and return. */
1137 if (DECL_EXTERNAL (olddecl
) && !DECL_INITIAL (olddecl
))
1139 apply_user_attributes (decl
, decl
->csym
);
1142 /* The previous declaration is a prototype or marked extern, set the
1143 current declaration as the main reference of the symbol. */
1144 else if (DECL_EXTERNAL (decl
->csym
) && !DECL_INITIAL (decl
->csym
))
1146 IDENTIFIER_DSYMBOL (mangled_name
) = decl
;
1147 DECL_EXTERNAL (decl
->csym
) = 0;
1149 /* Non-extern, non-templated decls shouldn't be defined twice. */
1150 else if (!decl
->isInstantiated ())
1151 ScopeDsymbol::multiplyDefined (decl
->loc
, decl
, other
);
1155 IDENTIFIER_PRETTY_NAME (mangled_name
)
1156 = get_identifier (decl
->toPrettyChars (true));
1157 IDENTIFIER_DSYMBOL (mangled_name
) = decl
;
1159 SET_DECL_ASSEMBLER_NAME (decl
->csym
, mangled_name
);
1163 DECL_LANG_SPECIFIC (decl
->csym
) = build_lang_decl (decl
);
1164 DECL_CONTEXT (decl
->csym
) = d_decl_context (decl
);
1166 if (TREE_CODE (decl
->csym
) == PARM_DECL
)
1168 /* Pass non-trivial structs by invisible reference. */
1169 if (TREE_ADDRESSABLE (TREE_TYPE (decl
->csym
)))
1171 tree argtype
= build_reference_type (TREE_TYPE (decl
->csym
));
1172 argtype
= build_qualified_type (argtype
, TYPE_QUAL_RESTRICT
);
1173 gcc_assert (!DECL_BY_REFERENCE (decl
->csym
));
1174 TREE_TYPE (decl
->csym
) = argtype
;
1175 DECL_BY_REFERENCE (decl
->csym
) = 1;
1176 TREE_ADDRESSABLE (decl
->csym
) = 0;
1177 relayout_decl (decl
->csym
);
1178 decl
->storage_class
|= STCref
;
1181 DECL_ARG_TYPE (decl
->csym
) = TREE_TYPE (decl
->csym
);
1182 gcc_assert (TREE_CODE (DECL_CONTEXT (decl
->csym
)) == FUNCTION_DECL
);
1184 else if (TREE_CODE (decl
->csym
) == CONST_DECL
)
1186 /* Manifest constants have no address in memory. */
1187 TREE_CONSTANT (decl
->csym
) = 1;
1188 TREE_READONLY (decl
->csym
) = 1;
1190 else if (TREE_CODE (decl
->csym
) == FUNCTION_DECL
)
1192 /* The real function type may differ from its declaration. */
1193 tree fntype
= TREE_TYPE (decl
->csym
);
1194 tree newfntype
= NULL_TREE
;
1196 if (fd
->isNested ())
1198 /* Add an extra argument for the frame/closure pointer, this is also
1199 required to be compatible with D delegates. */
1200 newfntype
= build_vthis_function (void_type_node
, fntype
);
1202 else if (fd
->isThis ())
1204 /* Add an extra argument for the `this' parameter. The handle type is
1205 used even if there is no debug info. It is needed to make sure
1206 virtual member functions are not called statically. */
1207 AggregateDeclaration
*ad
= fd
->isMember2 ();
1208 tree handle
= build_ctype (ad
->handleType ());
1210 /* If handle is a pointer type, get record type. */
1211 if (!ad
->isStructDeclaration ())
1212 handle
= TREE_TYPE (handle
);
1214 newfntype
= build_vthis_function (handle
, fntype
);
1216 /* Set the vindex on virtual functions. */
1217 if (fd
->isVirtual () && fd
->vtblIndex
!= -1)
1219 DECL_VINDEX (decl
->csym
) = size_int (fd
->vtblIndex
);
1220 DECL_VIRTUAL_P (decl
->csym
) = 1;
1223 else if (fd
->isMain () || fd
->isCMain ())
1225 /* The main function is named `D main' to distinguish from C main. */
1227 DECL_NAME (decl
->csym
) = get_identifier (fd
->toPrettyChars (true));
1229 /* `void main' is implicitly converted to returning an int. */
1230 newfntype
= build_function_type (d_int_type
, TYPE_ARG_TYPES (fntype
));
1233 if (newfntype
!= NULL_TREE
)
1235 /* Copy the old attributes from the original type. */
1236 TYPE_ATTRIBUTES (newfntype
) = TYPE_ATTRIBUTES (fntype
);
1237 TYPE_LANG_SPECIFIC (newfntype
) = TYPE_LANG_SPECIFIC (fntype
);
1238 TREE_ADDRESSABLE (newfntype
) = TREE_ADDRESSABLE (fntype
);
1239 TREE_TYPE (decl
->csym
) = newfntype
;
1243 /* Miscellaneous function flags. */
1245 /* In [pragma/inline], functions decorated with `pragma(inline)' affects
1246 whether they are inlined or not. */
1247 if (fd
->inlining
== PINLINEalways
)
1248 DECL_DECLARED_INLINE_P (decl
->csym
) = 1;
1249 else if (fd
->inlining
== PINLINEnever
)
1250 DECL_UNINLINABLE (decl
->csym
) = 1;
1252 /* Function was declared `naked'. */
1255 insert_decl_attribute (decl
->csym
, "naked");
1256 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
->csym
) = 1;
1259 /* Vector array operations are always compiler generated. */
1262 TREE_PUBLIC (decl
->csym
) = 1;
1263 DECL_ARTIFICIAL (decl
->csym
) = 1;
1264 DECL_DECLARED_INLINE_P (decl
->csym
) = 1;
1265 d_comdat_linkage (decl
->csym
);
1268 /* And so are ensure and require contracts. */
1269 if (fd
->ident
== Identifier::idPool ("ensure")
1270 || fd
->ident
== Identifier::idPool ("require"))
1272 DECL_ARTIFICIAL (decl
->csym
) = 1;
1273 TREE_PUBLIC (decl
->csym
) = 1;
1276 if (decl
->storage_class
& STCfinal
)
1277 DECL_FINAL_P (decl
->csym
) = 1;
1279 /* Check whether this function is expanded by the frontend. */
1280 DECL_INTRINSIC_CODE (decl
->csym
) = INTRINSIC_NONE
;
1281 maybe_set_intrinsic (fd
);
1283 /* For nested functions in particular, unnest fndecl in the cgraph, as
1284 all static chain passing is handled by the front-end. Do this even
1285 if we are not emitting the body. */
1286 struct cgraph_node
*node
= cgraph_node::get_create (decl
->csym
);
1287 if (nested_function_origin (node
))
1288 unnest_function (node
);
1291 /* Mark compiler generated temporaries as artificial. */
1292 if (decl
->storage_class
& STCtemp
)
1293 DECL_ARTIFICIAL (decl
->csym
) = 1;
1295 /* Propagate shared on the decl. */
1296 if (TYPE_SHARED (TREE_TYPE (decl
->csym
)))
1297 TREE_ADDRESSABLE (decl
->csym
) = 1;
1299 /* Symbol was marked volatile. */
1300 if (decl
->storage_class
& STCvolatile
)
1301 TREE_THIS_VOLATILE (decl
->csym
) = 1;
1303 /* Protection attributes are used by the debugger. */
1304 if (decl
->protection
.kind
== Prot::private_
)
1305 TREE_PRIVATE (decl
->csym
) = 1;
1306 else if (decl
->protection
.kind
== Prot::protected_
)
1307 TREE_PROTECTED (decl
->csym
) = 1;
1309 /* Likewise, so could the deprecated attribute. */
1310 if (decl
->storage_class
& STCdeprecated
)
1311 TREE_DEPRECATED (decl
->csym
) = 1;
1313 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1314 /* Have to test for import first. */
1315 if (decl
->isImportedSymbol ())
1317 insert_decl_attribute (decl
->csym
, "dllimport");
1318 DECL_DLLIMPORT_P (decl
->csym
) = 1;
1320 else if (decl
->isExport ())
1321 insert_decl_attribute (decl
->csym
, "dllexport");
1324 if (decl
->isDataseg () || decl
->isCodeseg () || decl
->isThreadlocal ())
1326 /* Set TREE_PUBLIC by default, but allow private template to override. */
1327 if (!fd
|| !fd
->isNested ())
1328 TREE_PUBLIC (decl
->csym
) = 1;
1330 TREE_STATIC (decl
->csym
) = 1;
1331 /* The decl has not been defined -- yet. */
1332 DECL_EXTERNAL (decl
->csym
) = 1;
1334 if (decl
->isInstantiated ())
1335 d_linkonce_linkage (decl
->csym
);
1338 /* Symbol is going in thread local storage. */
1339 if (decl
->isThreadlocal () && !DECL_ARTIFICIAL (decl
->csym
))
1341 if (global
.params
.vtls
)
1342 message (decl
->loc
, "`%s` is thread local", decl
->toChars ());
1344 set_decl_tls_model (decl
->csym
, decl_default_tls_model (decl
->csym
));
1347 /* Apply any user attributes that may affect semantic meaning. */
1348 apply_user_attributes (decl
, decl
->csym
);
1350 /* %% Probably should be a little more intelligent about setting this. */
1351 TREE_USED (decl
->csym
) = 1;
1352 d_keep (decl
->csym
);
1357 /* Returns a declaration for a VAR_DECL. Used to create compiler-generated
1358 global variables. */
1361 declare_extern_var (tree ident
, tree type
)
1363 /* If the VAR_DECL has already been declared, return it. */
1364 if (IDENTIFIER_DECL_TREE (ident
))
1365 return IDENTIFIER_DECL_TREE (ident
);
1367 tree name
= IDENTIFIER_PRETTY_NAME (ident
)
1368 ? IDENTIFIER_PRETTY_NAME (ident
) : ident
;
1369 tree decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
1371 IDENTIFIER_DECL_TREE (ident
) = decl
;
1374 SET_DECL_ASSEMBLER_NAME (decl
, ident
);
1375 DECL_ARTIFICIAL (decl
) = 1;
1376 TREE_STATIC (decl
) = 1;
1377 TREE_PUBLIC (decl
) = 1;
1379 /* The decl has not been defined -- yet. */
1380 DECL_EXTERNAL (decl
) = 1;
1385 /* Add local variable VAR into the current function body. */
1388 declare_local_var (VarDeclaration
*var
)
1390 gcc_assert (!var
->isDataseg () && !var
->isMember ());
1391 gcc_assert (current_function_decl
!= NULL_TREE
);
1393 FuncDeclaration
*fd
= cfun
->language
->function
;
1394 tree decl
= get_symbol_decl (var
);
1396 gcc_assert (!TREE_STATIC (decl
));
1398 DECL_CONTEXT (decl
) = current_function_decl
;
1400 /* Compiler generated symbols. */
1401 if (var
== fd
->vresult
|| var
== fd
->v_argptr
)
1402 DECL_ARTIFICIAL (decl
) = 1;
1404 if (DECL_LANG_FRAME_FIELD (decl
))
1406 /* Fixes debugging local variables. */
1407 SET_DECL_VALUE_EXPR (decl
, get_decl_tree (var
));
1408 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1412 /* Return an unnamed local temporary of type TYPE. */
1415 build_local_temp (tree type
)
1417 tree decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, type
);
1419 DECL_CONTEXT (decl
) = current_function_decl
;
1420 DECL_ARTIFICIAL (decl
) = 1;
1421 DECL_IGNORED_P (decl
) = 1;
1427 /* Return the correct decl to be used for DECL. For VAR_DECLs, this could
1428 instead be a FIELD_DECL from a closure, or a RESULT_DECL from a named return
1429 value. For PARM_DECLs, this could be a FIELD_DECL for a non-local `this'.
1430 For all other kinds of decls, this just returns the result of
1431 get_symbol_decl(). */
1434 get_decl_tree (Declaration
*decl
)
1436 tree t
= get_symbol_decl (decl
);
1437 FuncDeclaration
*fd
= cfun
? cfun
->language
->function
: NULL
;
1438 VarDeclaration
*vd
= decl
->isVarDeclaration ();
1440 /* If cfun is NULL, then this is a global static. */
1441 if (vd
== NULL
|| fd
== NULL
)
1444 /* Get the named return value. */
1445 if (DECL_LANG_NRVO (t
))
1446 return DECL_LANG_NRVO (t
);
1448 /* Get the closure holding the var decl. */
1449 if (DECL_LANG_FRAME_FIELD (t
))
1451 FuncDeclaration
*parent
= vd
->toParent2 ()->isFuncDeclaration ();
1452 tree frame_ref
= get_framedecl (fd
, parent
);
1454 return component_ref (build_deref (frame_ref
),
1455 DECL_LANG_FRAME_FIELD (t
));
1458 /* Get the non-local `this' value by going through parent link
1459 of nested classes, this routine pretty much undoes what
1460 getRightThis in the frontend removes from codegen. */
1461 if (vd
->parent
!= fd
&& vd
->isThisDeclaration ())
1463 /* Find the first parent that is a member function. */
1464 while (!fd
->isMember2 ())
1466 gcc_assert (fd
->vthis
);
1467 fd
= fd
->toParent2 ()->isFuncDeclaration ();
1468 gcc_assert (fd
!= NULL
);
1471 AggregateDeclaration
*ad
= fd
->isThis ();
1472 gcc_assert (ad
!= NULL
);
1474 /* The parent function is for the same `this' declaration we are
1475 building a chain to. Non-local declaration is inaccessible. */
1476 if (fd
->vthis
== vd
)
1477 return error_no_frame_access (fd
);
1479 t
= get_decl_tree (fd
->vthis
);
1480 Dsymbol
*outer
= fd
;
1482 while (outer
!= vd
->parent
)
1484 gcc_assert (ad
!= NULL
);
1485 outer
= ad
->toParent2 ();
1487 /* Get the this->this parent link. */
1488 tree vfield
= get_symbol_decl (ad
->vthis
);
1489 t
= component_ref (build_deref (t
), vfield
);
1491 ad
= outer
->isAggregateDeclaration ();
1495 fd
= outer
->isFuncDeclaration ();
1498 /* If outer function creates a closure, then the `this'
1499 value would be the closure pointer, and the real
1500 `this' the first field of that closure. */
1501 tree ff
= get_frameinfo (fd
);
1502 if (FRAMEINFO_CREATES_FRAME (ff
))
1504 t
= build_nop (build_pointer_type (FRAMEINFO_TYPE (ff
)), t
);
1505 t
= indirect_ref (build_ctype (fd
->vthis
->type
), t
);
1508 if (fd
== vd
->parent
)
1511 /* Continue looking for the right `this'. */
1512 outer
= outer
->toParent2 ();
1513 fd
= outer
->isFuncDeclaration ();
1516 ad
= outer
->isAggregateDeclaration ();
1522 /* Auto variable that the back end will handle for us. */
1526 /* Finish up a variable declaration and compile it all the way to
1527 the assembler language output. */
1530 d_finish_decl (tree decl
)
1532 gcc_assert (!error_operand_p (decl
));
1534 /* We are sending this symbol to object file, can't be extern. */
1535 TREE_STATIC (decl
) = 1;
1536 DECL_EXTERNAL (decl
) = 0;
1538 /* Update the TLS model as the linkage has been modified. */
1539 if (DECL_THREAD_LOCAL_P (decl
))
1540 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1542 relayout_decl (decl
);
1544 if (flag_checking
&& DECL_INITIAL (decl
))
1546 /* Initializer must never be bigger than symbol size. */
1547 dinteger_t tsize
= int_size_in_bytes (TREE_TYPE (decl
));
1548 dinteger_t dtsize
= int_size_in_bytes (TREE_TYPE (DECL_INITIAL (decl
)));
1552 tree name
= DECL_ASSEMBLER_NAME (decl
);
1554 internal_error ("Mismatch between declaration %qE size (%wd) and "
1555 "its initializer size (%wd).",
1556 IDENTIFIER_PRETTY_NAME (name
)
1557 ? IDENTIFIER_PRETTY_NAME (name
) : name
,
1562 /* Without weak symbols, symbol should be put in .common, but that can't
1563 be done if there is a nonzero initializer. */
1564 if (DECL_COMDAT (decl
) && DECL_COMMON (decl
)
1565 && initializer_zerop (DECL_INITIAL (decl
)))
1566 DECL_INITIAL (decl
) = error_mark_node
;
1568 /* Add this decl to the current binding level. */
1571 rest_of_decl_compilation (decl
, 1, 0);
1574 /* Thunk code is based on g++. */
1576 static int thunk_labelno
;
1578 /* Create a static alias to function. */
1581 make_alias_for_thunk (tree function
)
1586 /* Thunks may reference extern functions which cannot be aliased. */
1587 if (DECL_EXTERNAL (function
))
1590 targetm
.asm_out
.generate_internal_label (buf
, "LTHUNK", thunk_labelno
);
1593 alias
= build_decl (DECL_SOURCE_LOCATION (function
), FUNCTION_DECL
,
1594 get_identifier (buf
), TREE_TYPE (function
));
1595 DECL_LANG_SPECIFIC (alias
) = DECL_LANG_SPECIFIC (function
);
1596 lang_hooks
.dup_lang_specific_decl (alias
);
1597 DECL_CONTEXT (alias
) = NULL_TREE
;
1598 TREE_READONLY (alias
) = TREE_READONLY (function
);
1599 TREE_THIS_VOLATILE (alias
) = TREE_THIS_VOLATILE (function
);
1600 TREE_PUBLIC (alias
) = 0;
1602 DECL_EXTERNAL (alias
) = 0;
1603 DECL_ARTIFICIAL (alias
) = 1;
1605 DECL_DECLARED_INLINE_P (alias
) = 0;
1606 DECL_INITIAL (alias
) = error_mark_node
;
1607 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (function
));
1609 TREE_ADDRESSABLE (alias
) = 1;
1610 TREE_USED (alias
) = 1;
1611 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
1613 if (!flag_syntax_only
)
1615 cgraph_node
*aliasn
;
1616 aliasn
= cgraph_node::create_same_body_alias (alias
, function
);
1617 gcc_assert (aliasn
!= NULL
);
1622 /* Emit the definition of a D vtable thunk. */
1625 finish_thunk (tree thunk
, tree function
)
1627 /* Setup how D thunks are outputted. */
1628 int fixed_offset
= -THUNK_LANG_OFFSET (thunk
);
1629 bool this_adjusting
= true;
1632 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
))
1633 alias
= make_alias_for_thunk (function
);
1637 TREE_ADDRESSABLE (function
) = 1;
1638 TREE_USED (function
) = 1;
1640 if (flag_syntax_only
)
1642 TREE_ASM_WRITTEN (thunk
) = 1;
1646 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
)
1647 && targetm_common
.have_named_sections
)
1650 symtab_node
*symbol
= symtab_node::get (function
);
1652 if (symbol
!= NULL
&& symbol
->alias
)
1654 if (symbol
->analyzed
)
1655 fn
= symtab_node::get (function
)->ultimate_alias_target ()->decl
;
1657 fn
= symtab_node::get (function
)->alias_target
;
1659 resolve_unique_section (fn
, 0, flag_function_sections
);
1661 if (DECL_SECTION_NAME (fn
) != NULL
&& DECL_ONE_ONLY (fn
))
1663 resolve_unique_section (thunk
, 0, flag_function_sections
);
1665 /* Output the thunk into the same section as function. */
1666 set_decl_section_name (thunk
, DECL_SECTION_NAME (fn
));
1667 symtab_node::get (thunk
)->implicit_section
1668 = symtab_node::get (fn
)->implicit_section
;
1672 /* Set up cloned argument trees for the thunk. */
1674 for (tree a
= DECL_ARGUMENTS (function
); a
; a
= DECL_CHAIN (a
))
1676 tree x
= copy_node (a
);
1678 DECL_CONTEXT (x
) = thunk
;
1679 SET_DECL_RTL (x
, NULL
);
1680 DECL_HAS_VALUE_EXPR_P (x
) = 0;
1681 TREE_ADDRESSABLE (x
) = 0;
1684 DECL_ARGUMENTS (thunk
) = nreverse (t
);
1685 TREE_ASM_WRITTEN (thunk
) = 1;
1687 cgraph_node
*funcn
, *thunk_node
;
1689 funcn
= cgraph_node::get_create (function
);
1691 thunk_node
= funcn
->create_thunk (thunk
, thunk
, this_adjusting
,
1692 fixed_offset
, 0, 0, 0, alias
);
1694 if (DECL_ONE_ONLY (function
))
1695 thunk_node
->add_to_same_comdat_group (funcn
);
1697 /* Target assemble_mi_thunk doesn't work across section boundaries
1698 on many targets, instead force thunk to be expanded in gimple. */
1699 if (DECL_EXTERNAL (function
))
1701 /* cgraph::expand_thunk writes over current_function_decl, so if this
1702 could ever be in use by the codegen pass, we want to know about it. */
1703 gcc_assert (current_function_decl
== NULL_TREE
);
1705 if (!stdarg_p (TREE_TYPE (thunk
)))
1707 thunk_node
->create_edge (funcn
, NULL
, thunk_node
->count
);
1708 expand_thunk (thunk_node
, false, true);
1711 /* Tell the back-end to not bother inlining the function, this is
1712 assumed not to work as it could be referencing symbols outside
1713 of the current compilation unit. */
1714 DECL_UNINLINABLE (function
) = 1;
1718 /* Return a thunk to DECL. Thunks adjust the incoming `this' pointer by OFFSET.
1719 Adjustor thunks are created and pointers to them stored in the method entries
1720 in the vtable in order to set the this pointer to the start of the object
1721 instance corresponding to the implementing method. */
1724 make_thunk (FuncDeclaration
*decl
, int offset
)
1726 tree function
= get_symbol_decl (decl
);
1728 if (!DECL_ARGUMENTS (function
) || !DECL_RESULT (function
))
1730 /* Compile the function body before generating the thunk, this is done
1731 even if the decl is external to the current module. */
1733 build_decl_tree (decl
);
1736 /* Build parameters for functions that are not being compiled,
1737 so that they can be correctly cloned in finish_thunk. */
1738 tree fntype
= TREE_TYPE (function
);
1739 tree params
= NULL_TREE
;
1741 for (tree t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1743 if (t
== void_list_node
)
1746 tree param
= build_decl (DECL_SOURCE_LOCATION (function
),
1747 PARM_DECL
, NULL_TREE
, TREE_VALUE (t
));
1748 DECL_ARG_TYPE (param
) = TREE_TYPE (param
);
1749 DECL_ARTIFICIAL (param
) = 1;
1750 DECL_IGNORED_P (param
) = 1;
1751 DECL_CONTEXT (param
) = function
;
1752 params
= chainon (params
, param
);
1755 DECL_ARGUMENTS (function
) = params
;
1757 /* Also build the result decl, which is needed when force creating
1758 the thunk in gimple inside cgraph_node::expand_thunk. */
1759 tree resdecl
= build_decl (DECL_SOURCE_LOCATION (function
),
1760 RESULT_DECL
, NULL_TREE
,
1761 TREE_TYPE (fntype
));
1762 DECL_ARTIFICIAL (resdecl
) = 1;
1763 DECL_IGNORED_P (resdecl
) = 1;
1764 DECL_CONTEXT (resdecl
) = function
;
1765 DECL_RESULT (function
) = resdecl
;
1769 /* Don't build the thunk if the compilation step failed. */
1771 return error_mark_node
;
1773 /* See if we already have the thunk in question. */
1774 for (tree t
= DECL_LANG_THUNKS (function
); t
; t
= DECL_CHAIN (t
))
1776 if (THUNK_LANG_OFFSET (t
) == offset
)
1780 tree thunk
= build_decl (DECL_SOURCE_LOCATION (function
),
1781 FUNCTION_DECL
, NULL_TREE
, TREE_TYPE (function
));
1782 DECL_LANG_SPECIFIC (thunk
) = DECL_LANG_SPECIFIC (function
);
1783 lang_hooks
.dup_lang_specific_decl (thunk
);
1784 THUNK_LANG_OFFSET (thunk
) = offset
;
1786 TREE_READONLY (thunk
) = TREE_READONLY (function
);
1787 TREE_THIS_VOLATILE (thunk
) = TREE_THIS_VOLATILE (function
);
1788 TREE_NOTHROW (thunk
) = TREE_NOTHROW (function
);
1790 DECL_CONTEXT (thunk
) = d_decl_context (decl
);
1792 /* Thunks inherit the public access of the function they are targetting.
1793 When the function is outside the current compilation unit however, then the
1794 thunk must be kept private to not conflict. */
1795 TREE_PUBLIC (thunk
) = TREE_PUBLIC (function
) && !DECL_EXTERNAL (function
);
1797 DECL_EXTERNAL (thunk
) = 0;
1799 /* Thunks are always addressable. */
1800 TREE_ADDRESSABLE (thunk
) = 1;
1801 TREE_USED (thunk
) = 1;
1802 DECL_ARTIFICIAL (thunk
) = 1;
1803 DECL_DECLARED_INLINE_P (thunk
) = 0;
1805 DECL_VISIBILITY (thunk
) = DECL_VISIBILITY (function
);
1806 DECL_COMDAT (thunk
) = DECL_COMDAT (function
);
1807 DECL_WEAK (thunk
) = DECL_WEAK (function
);
1809 tree target_name
= DECL_ASSEMBLER_NAME (function
);
1810 unsigned identlen
= IDENTIFIER_LENGTH (target_name
) + 14;
1811 const char *ident
= XNEWVEC (const char, identlen
);
1812 snprintf (CONST_CAST (char *, ident
), identlen
,
1813 "_DT%u%s", offset
, IDENTIFIER_POINTER (target_name
));
1815 DECL_NAME (thunk
) = get_identifier (ident
);
1816 SET_DECL_ASSEMBLER_NAME (thunk
, DECL_NAME (thunk
));
1820 finish_thunk (thunk
, function
);
1822 /* Add it to the list of thunks associated with the function. */
1823 DECL_LANG_THUNKS (thunk
) = NULL_TREE
;
1824 DECL_CHAIN (thunk
) = DECL_LANG_THUNKS (function
);
1825 DECL_LANG_THUNKS (function
) = thunk
;
1830 /* Create the FUNCTION_DECL for a function definition.
1831 This function creates a binding context for the function body
1832 as well as setting up the FUNCTION_DECL in current_function_decl.
1833 Returns the previous function context if it was already set. */
1836 start_function (FuncDeclaration
*fd
)
1838 tree fndecl
= get_symbol_decl (fd
);
1840 /* Function has been defined, check now whether we intend to send it to
1841 object file, or it really is extern. Such as inlinable functions from
1842 modules not in this compilation, or thunk aliases. */
1843 TemplateInstance
*ti
= fd
->isInstantiated ();
1844 if (ti
&& ti
->needsCodegen ())
1846 /* Warn about templates instantiated in this compilation. */
1847 if (ti
== fd
->parent
)
1849 warning (OPT_Wtemplates
, "%s %qs instantiated",
1850 ti
->kind (), ti
->toPrettyChars (false));
1853 DECL_EXTERNAL (fndecl
) = 0;
1857 Module
*md
= fd
->getModule ();
1858 if (md
&& md
->isRoot ())
1859 DECL_EXTERNAL (fndecl
) = 0;
1862 DECL_INITIAL (fndecl
) = error_mark_node
;
1864 /* Add this decl to the current binding level. */
1865 d_pushdecl (fndecl
);
1867 /* Save the current function context. */
1868 tree old_context
= current_function_decl
;
1871 push_function_context ();
1873 /* Let GCC know the current scope is this function. */
1874 current_function_decl
= fndecl
;
1876 tree restype
= TREE_TYPE (TREE_TYPE (fndecl
));
1877 tree resdecl
= build_decl (make_location_t (fd
->loc
), RESULT_DECL
,
1878 NULL_TREE
, restype
);
1880 DECL_RESULT (fndecl
) = resdecl
;
1881 DECL_CONTEXT (resdecl
) = fndecl
;
1882 DECL_ARTIFICIAL (resdecl
) = 1;
1883 DECL_IGNORED_P (resdecl
) = 1;
1885 /* Initialize the RTL code for the function. */
1886 allocate_struct_function (fndecl
, false);
1888 /* Store the end of the function. */
1889 if (fd
->endloc
.filename
)
1890 cfun
->function_end_locus
= make_location_t (fd
->endloc
);
1892 cfun
->function_end_locus
= DECL_SOURCE_LOCATION (fndecl
);
1894 cfun
->language
= ggc_cleared_alloc
<language_function
> ();
1895 cfun
->language
->function
= fd
;
1897 /* Default chain value is `null' unless parent found. */
1898 cfun
->language
->static_chain
= null_pointer_node
;
1900 /* Find module for this function. */
1901 for (Dsymbol
*p
= fd
->parent
; p
!= NULL
; p
= p
->parent
)
1903 cfun
->language
->module
= p
->isModule ();
1904 if (cfun
->language
->module
)
1907 gcc_assert (cfun
->language
->module
!= NULL
);
1909 /* Begin the statement tree for this function. */
1911 push_binding_level (level_function
);
1916 /* Finish up a function declaration and compile that function all
1917 the way to assembler language output. The free the storage for
1918 the function definition. Restores the previous function context. */
1921 finish_function (tree old_context
)
1923 tree fndecl
= current_function_decl
;
1925 /* Tie off the statement tree for this function. */
1926 tree block
= pop_binding_level ();
1927 tree body
= pop_stmt_list ();
1928 tree bind
= build3 (BIND_EXPR
, void_type_node
,
1929 BLOCK_VARS (block
), body
, block
);
1931 gcc_assert (vec_safe_is_empty (d_function_chain
->stmt_list
));
1933 /* Back-end expects a statement list to come from somewhere, however
1934 pop_stmt_list returns expressions when there is a single statement.
1935 So here we create a statement list unconditionally. */
1936 if (TREE_CODE (body
) != STATEMENT_LIST
)
1938 tree stmtlist
= alloc_stmt_list ();
1939 append_to_statement_list_force (body
, &stmtlist
);
1940 BIND_EXPR_BODY (bind
) = stmtlist
;
1942 else if (!STATEMENT_LIST_HEAD (body
))
1944 /* For empty functions add a void return. */
1945 append_to_statement_list_force (return_expr (NULL_TREE
), &body
);
1948 DECL_SAVED_TREE (fndecl
) = bind
;
1950 if (!errorcount
&& !global
.errors
)
1952 /* Dump the D-specific tree IR. */
1953 dump_function (TDI_original
, fndecl
);
1955 cgraph_node::finalize_function (fndecl
, true);
1958 /* We're leaving the context of this function, so free it. */
1959 ggc_free (cfun
->language
);
1960 cfun
->language
= NULL
;
1964 pop_function_context ();
1966 current_function_decl
= old_context
;
1969 /* Mark DECL, which is a VAR_DECL or FUNCTION_DECL as a symbol that
1970 must be emitted in this, output module. */
1973 mark_needed (tree decl
)
1975 TREE_USED (decl
) = 1;
1977 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1979 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
1980 node
->forced_by_abi
= true;
1982 else if (VAR_P (decl
))
1984 struct varpool_node
*node
= varpool_node::get_create (decl
);
1985 node
->forced_by_abi
= true;
1989 /* Get the VAR_DECL of the vtable symbol for DECL. If this does not yet exist,
1990 create it. The vtable is accessible via ClassInfo, but since it is needed
1991 frequently (like for rtti comparisons), make it directly accessible. */
1994 get_vtable_decl (ClassDeclaration
*decl
)
1997 return decl
->vtblsym
;
1999 tree ident
= mangle_internal_decl (decl
, "__vtbl", "Z");
2000 /* Note: Using a static array type for the VAR_DECL, the DECL_INITIAL value
2001 will have a different type. However the back-end seems to accept this. */
2002 tree type
= build_ctype (Type::tvoidptr
->sarrayOf (decl
->vtbl
.length
));
2004 decl
->vtblsym
= declare_extern_var (ident
, type
);
2005 DECL_LANG_SPECIFIC (decl
->vtblsym
) = build_lang_decl (NULL
);
2007 /* Class is a reference, want the record type. */
2008 DECL_CONTEXT (decl
->vtblsym
) = TREE_TYPE (build_ctype (decl
->type
));
2009 TREE_READONLY (decl
->vtblsym
) = 1;
2010 DECL_VIRTUAL_P (decl
->vtblsym
) = 1;
2012 SET_DECL_ALIGN (decl
->vtblsym
, TARGET_VTABLE_ENTRY_ALIGN
);
2013 DECL_USER_ALIGN (decl
->vtblsym
) = true;
2015 return decl
->vtblsym
;
2018 /* Helper function of build_class_instance. Find the field inside aggregate
2019 TYPE identified by IDENT at field OFFSET. */
2022 find_aggregate_field (tree type
, tree ident
, tree offset
)
2024 tree fields
= TYPE_FIELDS (type
);
2026 for (tree field
= fields
; field
!= NULL_TREE
; field
= TREE_CHAIN (field
))
2028 if (DECL_NAME (field
) == NULL_TREE
2029 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
2030 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2032 /* Search nesting anonymous structs and unions. */
2033 tree vfield
= find_aggregate_field (TREE_TYPE (field
),
2035 if (vfield
!= NULL_TREE
)
2038 else if (DECL_NAME (field
) == ident
2039 && (offset
== NULL_TREE
2040 || DECL_FIELD_OFFSET (field
) == offset
))
2042 /* Found matching field at offset. */
2050 /* Helper function of build_new_class_expr. Return a constructor that matches
2051 the layout of the class expression EXP. */
2054 build_class_instance (ClassReferenceExp
*exp
)
2056 ClassDeclaration
*cd
= exp
->originalClass ();
2057 tree type
= TREE_TYPE (build_ctype (exp
->value
->stype
));
2058 vec
<constructor_elt
, va_gc
> *ve
= NULL
;
2060 /* The set base vtable field. */
2061 tree vptr
= build_address (get_vtable_decl (cd
));
2062 CONSTRUCTOR_APPEND_ELT (ve
, TYPE_FIELDS (type
), vptr
);
2064 /* Go through the inheritance graph from top to bottom. This will add all
2065 values to the constructor out of order, however build_struct_literal
2066 will re-order all values before returning the finished literal. */
2067 for (ClassDeclaration
*bcd
= cd
; bcd
!= NULL
; bcd
= bcd
->baseClass
)
2069 /* Anonymous vtable interface fields are laid out before the fields of
2070 each class. The interface offset is used to determine where to put
2071 the classinfo offset reference. */
2072 for (size_t i
= 0; i
< bcd
->vtblInterfaces
->length
; i
++)
2074 BaseClass
*bc
= (*bcd
->vtblInterfaces
)[i
];
2076 for (ClassDeclaration
*cd2
= cd
; 1; cd2
= cd2
->baseClass
)
2078 gcc_assert (cd2
!= NULL
);
2080 unsigned csymoffset
= base_vtable_offset (cd2
, bc
);
2081 /* If the base class vtable was found. */
2082 if (csymoffset
!= ~0u)
2084 tree csym
= build_address (get_classinfo_decl (cd2
));
2085 csym
= build_offset (csym
, size_int (csymoffset
));
2087 tree field
= find_aggregate_field (type
, NULL_TREE
,
2088 size_int (bc
->offset
));
2089 gcc_assert (field
!= NULL_TREE
);
2091 CONSTRUCTOR_APPEND_ELT (ve
, field
, csym
);
2097 /* Generate initial values of all fields owned by current class.
2098 Use both the name and offset to find the right field. */
2099 for (size_t i
= 0; i
< bcd
->fields
.length
; i
++)
2101 VarDeclaration
*vfield
= bcd
->fields
[i
];
2102 int index
= exp
->findFieldIndexByName (vfield
);
2103 gcc_assert (index
!= -1);
2105 Expression
*value
= (*exp
->value
->elements
)[index
];
2109 /* Use find_aggregate_field to get the overridden field decl,
2110 instead of the field associated with the base class. */
2111 tree field
= get_symbol_decl (bcd
->fields
[i
]);
2112 field
= find_aggregate_field (type
, DECL_NAME (field
),
2113 DECL_FIELD_OFFSET (field
));
2114 gcc_assert (field
!= NULL_TREE
);
2116 CONSTRUCTOR_APPEND_ELT (ve
, field
, build_expr (value
, true));
2120 return build_struct_literal (type
, ve
);
2123 /* Get the VAR_DECL of a class instance representing EXPR as static data.
2124 If this does not yet exist, create it. This is used to support initializing
2125 a static variable that is of a class type using values known during CTFE.
2126 In user code, it is analogous to the following code snippet.
2128 enum E = new C(1, 2, 3);
2130 That we write the contents of `C(1, 2, 3)' to static data is only a compiler
2131 implementation detail. The initialization of these symbols could be done at
2132 run-time using during as part of the module initialization or shared static
2133 constructors phase of run-time start-up - whichever comes after `gc_init()'.
2134 And infact that would be the better thing to do here eventually. */
2137 build_new_class_expr (ClassReferenceExp
*expr
)
2139 if (expr
->value
->sym
)
2140 return expr
->value
->sym
;
2142 /* Build the reference symbol. */
2143 tree type
= build_ctype (expr
->value
->stype
);
2144 expr
->value
->sym
= build_artificial_decl (TREE_TYPE (type
), NULL_TREE
, "C");
2146 DECL_INITIAL (expr
->value
->sym
) = build_class_instance (expr
);
2147 d_pushdecl (expr
->value
->sym
);
2148 rest_of_decl_compilation (expr
->value
->sym
, 1, 0);
2150 return expr
->value
->sym
;
2153 /* Get the VAR_DECL of the static initializer symbol for the struct/class DECL.
2154 If this does not yet exist, create it. The static initializer data is
2155 accessible via TypeInfo, and is also used in `new class' and default
2156 initializing struct literals. */
2159 aggregate_initializer_decl (AggregateDeclaration
*decl
)
2164 /* Class is a reference, want the record type. */
2165 tree type
= build_ctype (decl
->type
);
2166 StructDeclaration
*sd
= decl
->isStructDeclaration ();
2168 type
= TREE_TYPE (type
);
2170 tree ident
= mangle_internal_decl (decl
, "__init", "Z");
2172 decl
->sinit
= declare_extern_var (ident
, type
);
2173 DECL_LANG_SPECIFIC (decl
->sinit
) = build_lang_decl (NULL
);
2175 DECL_CONTEXT (decl
->sinit
) = type
;
2176 TREE_READONLY (decl
->sinit
) = 1;
2178 /* Honor struct alignment set by user. */
2179 if (sd
&& sd
->alignment
!= STRUCTALIGN_DEFAULT
)
2181 SET_DECL_ALIGN (decl
->sinit
, sd
->alignment
* BITS_PER_UNIT
);
2182 DECL_USER_ALIGN (decl
->sinit
) = true;
2188 /* Generate the data for the static initializer. */
2191 layout_class_initializer (ClassDeclaration
*cd
)
2193 NewExp
*ne
= NewExp::create (cd
->loc
, NULL
, NULL
, cd
->type
, NULL
);
2194 ne
->type
= cd
->type
;
2196 Expression
*e
= ne
->ctfeInterpret ();
2197 gcc_assert (e
->op
== TOKclassreference
);
2199 return build_class_instance (e
->isClassReferenceExp ());
2203 layout_struct_initializer (StructDeclaration
*sd
)
2205 StructLiteralExp
*sle
= StructLiteralExp::create (sd
->loc
, sd
, NULL
);
2207 if (!sd
->fill (sd
->loc
, sle
->elements
, true))
2210 sle
->type
= sd
->type
;
2211 return build_expr (sle
, true);
2214 /* Get the VAR_DECL of the static initializer symbol for the enum DECL.
2215 If this does not yet exist, create it. The static initializer data is
2216 accessible via TypeInfo_Enum, but the field member type is a byte[] that
2217 requires a pointer to a symbol reference. */
2220 enum_initializer_decl (EnumDeclaration
*decl
)
2225 tree type
= build_ctype (decl
->type
);
2227 Identifier
*ident_save
= decl
->ident
;
2229 decl
->ident
= Identifier::generateId ("__enum");
2230 tree ident
= mangle_internal_decl (decl
, "__init", "Z");
2231 decl
->ident
= ident_save
;
2233 decl
->sinit
= declare_extern_var (ident
, type
);
2234 DECL_LANG_SPECIFIC (decl
->sinit
) = build_lang_decl (NULL
);
2236 DECL_CONTEXT (decl
->sinit
) = d_decl_context (decl
);
2237 TREE_READONLY (decl
->sinit
) = 1;
2242 /* Return an anonymous static variable of type TYPE, initialized with INIT,
2243 and optionally prefixing the name with PREFIX. */
2246 build_artificial_decl (tree type
, tree init
, const char *prefix
)
2248 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, NULL_TREE
, type
);
2249 const char *name
= prefix
? prefix
: "___s";
2252 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
2253 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
2254 DECL_NAME (decl
) = DECL_ASSEMBLER_NAME (decl
);
2256 TREE_PUBLIC (decl
) = 0;
2257 TREE_STATIC (decl
) = 1;
2258 TREE_USED (decl
) = 1;
2259 DECL_IGNORED_P (decl
) = 1;
2260 DECL_ARTIFICIAL (decl
) = 1;
2262 /* Perhaps at some point the initializer constant should be hashed
2263 to remove duplicates. */
2264 DECL_INITIAL (decl
) = init
;
2269 /* Build TYPE_DECL for the declaration DSYM. */
2272 build_type_decl (tree type
, Dsymbol
*dsym
)
2274 if (TYPE_STUB_DECL (type
))
2277 /* If a templated type, use the template instance name, as that includes all
2278 template parameters. */
2279 const char *name
= dsym
->parent
->isTemplateInstance ()
2280 ? ((TemplateInstance
*) dsym
->parent
)->toChars () : dsym
->ident
->toChars ();
2282 tree decl
= build_decl (make_location_t (dsym
->loc
), TYPE_DECL
,
2283 get_identifier (name
), type
);
2284 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (d_mangle_decl (dsym
)));
2285 TREE_PUBLIC (decl
) = 1;
2286 DECL_CONTEXT (decl
) = d_decl_context (dsym
);
2288 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
2289 TYPE_NAME (type
) = decl
;
2291 /* Not sure if there is a need for separate TYPE_DECLs in
2292 TYPE_NAME and TYPE_STUB_DECL. */
2293 if (TREE_CODE (type
) == ENUMERAL_TYPE
|| RECORD_OR_UNION_TYPE_P (type
))
2295 DECL_ARTIFICIAL (decl
) = 1;
2296 TYPE_STUB_DECL (type
) = decl
;
2298 else if (type
!= TYPE_MAIN_VARIANT (type
))
2299 DECL_ORIGINAL_TYPE (decl
) = TYPE_MAIN_VARIANT (type
);
2301 rest_of_decl_compilation (decl
, DECL_FILE_SCOPE_P (decl
), 0);
2304 /* Create a declaration for field NAME of a given TYPE, setting the flags
2305 for whether the field is ARTIFICIAL and/or IGNORED. */
2308 create_field_decl (tree type
, const char *name
, int artificial
, int ignored
)
2310 tree decl
= build_decl (input_location
, FIELD_DECL
,
2311 name
? get_identifier (name
) : NULL_TREE
, type
);
2312 DECL_ARTIFICIAL (decl
) = artificial
;
2313 DECL_IGNORED_P (decl
) = ignored
;
2318 /* Return the COMDAT group into which DECL should be placed. */
2321 d_comdat_group (tree decl
)
2323 /* If already part of a comdat group, use that. */
2324 if (DECL_COMDAT_GROUP (decl
))
2325 return DECL_COMDAT_GROUP (decl
);
2327 return DECL_ASSEMBLER_NAME (decl
);
2330 /* Set DECL up to have the closest approximation of "initialized common"
2331 linkage available. */
2334 d_comdat_linkage (tree decl
)
2337 make_decl_one_only (decl
, d_comdat_group (decl
));
2338 else if (TREE_CODE (decl
) == FUNCTION_DECL
2339 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
2340 /* We can just emit function and compiler-generated variables statically;
2341 having multiple copies is (for the most part) only a waste of space. */
2342 TREE_PUBLIC (decl
) = 0;
2343 else if (DECL_INITIAL (decl
) == NULL_TREE
2344 || DECL_INITIAL (decl
) == error_mark_node
)
2345 /* Fallback, cannot have multiple copies. */
2346 DECL_COMMON (decl
) = 1;
2348 if (TREE_PUBLIC (decl
))
2349 DECL_COMDAT (decl
) = 1;
2352 /* Set DECL up to have the closest approximation of "linkonce" linkage. */
2355 d_linkonce_linkage (tree decl
)
2357 /* Weak definitions have to be public. */
2358 if (!TREE_PUBLIC (decl
))
2361 /* Necessary to allow DECL_ONE_ONLY or DECL_WEAK functions to be inlined. */
2362 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2363 DECL_DECLARED_INLINE_P (decl
) = 1;
2365 /* No weak support, fallback to COMDAT linkage. */
2367 return d_comdat_linkage (decl
);
2369 make_decl_one_only (decl
, d_comdat_group (decl
));