Fix ICE in lto_symtab_merge_symbols_1 (PR lto/88004).
[official-gcc.git] / gcc / d / modules.cc
blob80573e1c2f2472f7265a5aafba1ad2c1f74ccff9
1 /* modules.cc -- D module initialization and termination.
2 Copyright (C) 2013-2018 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)
7 any later version.
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/>. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
22 #include "dmd/declaration.h"
23 #include "dmd/identifier.h"
24 #include "dmd/module.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "tm.h"
29 #include "function.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "toplev.h"
33 #include "target.h"
34 #include "common/common-target.h"
35 #include "stringpool.h"
37 #include "d-tree.h"
40 /* D generates module information to inform the runtime library which modules
41 need some kind of special handling. All `static this()', `static ~this()',
42 and `unittest' functions for a given module are aggregated into a single
43 function - one for each kind - and a pointer to that function is inserted
44 into the ModuleInfo instance for that module.
46 Module information for a particular module is indicated with an ABI defined
47 structure derived from ModuleInfo. ModuleInfo is a variably sized struct
48 with two fixed base fields. The first field `flags' determines what
49 information is packed immediately after the record type.
51 Like TypeInfo, the runtime library provides the definitions of the ModuleInfo
52 structure, as well as accessors for the variadic fields. So we only define
53 layout compatible POD_structs for ModuleInfo. */
55 /* The internally represented ModuleInfo and CompilerDSO types. */
56 static tree moduleinfo_type;
57 static tree compiler_dso_type;
58 static tree dso_registry_fn;
60 /* The DSO slot for use by the druntime implementation. */
61 static tree dso_slot_node;
63 /* For registering and deregistering DSOs with druntime, we have one global
64 constructor and destructor per object that calls _d_dso_registry with the
65 respective DSO record. To ensure that this is only done once, a
66 `dso_initialized' variable is introduced to guard repeated calls. */
67 static tree dso_initialized_node;
69 /* The beginning and end of the `minfo' section. */
70 static tree start_minfo_node;
71 static tree stop_minfo_node;
73 /* Record information about module initialization, termination,
74 unit testing, and thread local storage in the compilation. */
76 struct GTY(()) module_info
78 vec<tree, va_gc> *ctors;
79 vec<tree, va_gc> *dtors;
80 vec<tree, va_gc> *ctorgates;
82 vec<tree, va_gc> *sharedctors;
83 vec<tree, va_gc> *shareddtors;
84 vec<tree, va_gc> *sharedctorgates;
86 vec<tree, va_gc> *unitTests;
89 /* These must match the values in libdruntime/object_.d. */
91 enum module_info_flags
93 MIctorstart = 0x1,
94 MIctordone = 0x2,
95 MIstandalone = 0x4,
96 MItlsctor = 0x8,
97 MItlsdtor = 0x10,
98 MIctor = 0x20,
99 MIdtor = 0x40,
100 MIxgetMembers = 0x80,
101 MIictor = 0x100,
102 MIunitTest = 0x200,
103 MIimportedModules = 0x400,
104 MIlocalClasses = 0x800,
105 MIname = 0x1000
108 /* The ModuleInfo information structure for the module currently being compiled.
109 Assuming that only ever process one at a time. */
111 static module_info *current_moduleinfo;
113 /* The declaration of the current module being compiled. */
115 static Module *current_module_decl;
117 /* Static constructors and destructors (not D `static this'). */
119 static GTY(()) vec<tree, va_gc> *static_ctor_list;
120 static GTY(()) vec<tree, va_gc> *static_dtor_list;
122 /* Returns an internal function identified by IDENT. This is used
123 by both module initialization and dso handlers. */
125 static FuncDeclaration *
126 get_internal_fn (tree ident)
128 Module *mod = current_module_decl;
129 const char *name = IDENTIFIER_POINTER (ident);
131 if (!mod)
132 mod = Module::rootModule;
134 if (name[0] == '*')
136 tree s = mangle_internal_decl (mod, name + 1, "FZv");
137 name = IDENTIFIER_POINTER (s);
140 FuncDeclaration *fd = FuncDeclaration::genCfunc (NULL, Type::tvoid,
141 Identifier::idPool (name));
142 fd->loc = Loc (mod->srcfile->toChars (), 1, 0);
143 fd->parent = mod;
144 fd->protection.kind = PROTprivate;
145 fd->semanticRun = PASSsemantic3done;
147 return fd;
150 /* Generate an internal function identified by IDENT.
151 The function body to add is in EXPR. */
153 static tree
154 build_internal_fn (tree ident, tree expr)
156 FuncDeclaration *fd = get_internal_fn (ident);
157 tree decl = get_symbol_decl (fd);
159 tree old_context = start_function (fd);
160 rest_of_decl_compilation (decl, 1, 0);
161 add_stmt (expr);
162 finish_function (old_context);
164 /* D static ctors, static dtors, unittests, and the ModuleInfo
165 chain function are always private. */
166 TREE_PUBLIC (decl) = 0;
167 TREE_USED (decl) = 1;
168 DECL_ARTIFICIAL (decl) = 1;
170 return decl;
173 /* Build and emit a function identified by IDENT that increments (in order)
174 all variables in GATES, then calls the list of functions in FUNCTIONS. */
176 static tree
177 build_funcs_gates_fn (tree ident, vec<tree, va_gc> *functions,
178 vec<tree, va_gc> *gates)
180 tree expr_list = NULL_TREE;
182 /* Increment gates first. */
183 for (size_t i = 0; i < vec_safe_length (gates); i++)
185 tree decl = (*gates)[i];
186 tree value = build2 (PLUS_EXPR, TREE_TYPE (decl),
187 decl, integer_one_node);
188 tree var_expr = modify_expr (decl, value);
189 expr_list = compound_expr (expr_list, var_expr);
192 /* Call Functions. */
193 for (size_t i = 0; i < vec_safe_length (functions); i++)
195 tree decl = (*functions)[i];
196 tree call_expr = build_call_expr (decl, 0);
197 expr_list = compound_expr (expr_list, call_expr);
200 if (expr_list)
201 return build_internal_fn (ident, expr_list);
203 return NULL_TREE;
206 /* Return the type for ModuleInfo, create it if it doesn't already exist. */
208 static tree
209 get_moduleinfo_type (void)
211 if (moduleinfo_type)
212 return moduleinfo_type;
214 /* Layout of ModuleInfo is:
215 uint flags;
216 uint index; */
217 tree fields = create_field_decl (d_uint_type, NULL, 1, 1);
218 DECL_CHAIN (fields) = create_field_decl (d_uint_type, NULL, 1, 1);
220 moduleinfo_type = make_node (RECORD_TYPE);
221 finish_builtin_struct (moduleinfo_type, "ModuleInfo", fields, NULL_TREE);
223 return moduleinfo_type;
226 /* Get the VAR_DECL of the ModuleInfo for DECL. If this does not yet exist,
227 create it. The ModuleInfo decl is used to keep track of constructors,
228 destructors, unittests, members, classes, and imports for the given module.
229 This is used by the D runtime for module initialization and termination. */
231 static tree
232 get_moduleinfo_decl (Module *decl)
234 if (decl->csym)
235 return decl->csym;
237 tree ident = mangle_internal_decl (decl, "__ModuleInfo", "Z");
238 tree type = get_moduleinfo_type ();
240 decl->csym = declare_extern_var (ident, type);
241 DECL_LANG_SPECIFIC (decl->csym) = build_lang_decl (NULL);
243 DECL_CONTEXT (decl->csym) = build_import_decl (decl);
244 /* Not readonly, moduleinit depends on this. */
245 TREE_READONLY (decl->csym) = 0;
247 return decl->csym;
250 /* Return the type for CompilerDSOData, create it if it doesn't exist. */
252 static tree
253 get_compiler_dso_type (void)
255 if (compiler_dso_type)
256 return compiler_dso_type;
258 /* Layout of CompilerDSOData is:
259 size_t version;
260 void** slot;
261 ModuleInfo** _minfo_beg;
262 ModuleInfo** _minfo_end;
263 FuncTable* _deh_beg;
264 FuncTable* _deh_end;
266 Note, finish_builtin_struct() expects these fields in reverse order. */
267 tree fields = create_field_decl (ptr_type_node, NULL, 1, 1);
268 tree field = create_field_decl (ptr_type_node, NULL, 1, 1);
269 DECL_CHAIN (field) = fields;
270 fields = field;
272 field = create_field_decl (build_pointer_type (get_moduleinfo_type ()),
273 NULL, 1, 1);
274 DECL_CHAIN (field) = fields;
275 fields = field;
276 field = create_field_decl (build_pointer_type (get_moduleinfo_type ()),
277 NULL, 1, 1);
278 DECL_CHAIN (field) = fields;
279 fields = field;
281 field = create_field_decl (build_pointer_type (ptr_type_node), NULL, 1, 1);
282 DECL_CHAIN (field) = fields;
283 fields = field;
285 field = create_field_decl (size_type_node, NULL, 1, 1);
286 DECL_CHAIN (field) = fields;
287 fields = field;
289 compiler_dso_type = make_node (RECORD_TYPE);
290 finish_builtin_struct (compiler_dso_type, "CompilerDSOData",
291 fields, NULL_TREE);
293 return compiler_dso_type;
296 /* Returns the _d_dso_registry FUNCTION_DECL. */
298 static tree
299 get_dso_registry_fn (void)
301 if (dso_registry_fn)
302 return dso_registry_fn;
304 tree dso_type = get_compiler_dso_type ();
305 tree fntype = build_function_type_list (void_type_node,
306 build_pointer_type (dso_type),
307 NULL_TREE);
308 dso_registry_fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
309 get_identifier ("_d_dso_registry"), fntype);
310 TREE_PUBLIC (dso_registry_fn) = 1;
311 DECL_EXTERNAL (dso_registry_fn) = 1;
313 return dso_registry_fn;
316 /* Depending on CTOR_P, builds and emits eiter a constructor or destructor
317 calling _d_dso_registry if `dso_initialized' is `false' in a constructor
318 or `true' in a destructor. */
320 static tree
321 build_dso_cdtor_fn (bool ctor_p)
323 const char *name = ctor_p ? GDC_PREFIX ("dso_ctor") : GDC_PREFIX ("dso_dtor");
324 tree condition = ctor_p ? boolean_true_node : boolean_false_node;
326 /* Declaration of dso_ctor/dso_dtor is:
328 extern(C) void dso_{c,d}tor (void)
330 if (dso_initialized != condition)
332 dso_initialized = condition;
333 CompilerDSOData dso = {1, &dsoSlot, &__start_minfo, &__stop_minfo};
334 _d_dso_registry (&dso);
338 FuncDeclaration *fd = get_internal_fn (get_identifier (name));
339 tree decl = get_symbol_decl (fd);
341 TREE_PUBLIC (decl) = 1;
342 DECL_ARTIFICIAL (decl) = 1;
343 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
344 DECL_VISIBILITY_SPECIFIED (decl) = 1;
346 d_comdat_linkage (decl);
348 /* Start laying out the body. */
349 tree old_context = start_function (fd);
350 rest_of_decl_compilation (decl, 1, 0);
352 /* if (dso_initialized != condition). */
353 tree if_cond = build_boolop (NE_EXPR, dso_initialized_node, condition);
355 /* dso_initialized = condition; */
356 tree expr_list = modify_expr (dso_initialized_node, condition);
358 /* CompilerDSOData dso = {1, &dsoSlot, &__start_minfo, &__stop_minfo}; */
359 tree dso_type = get_compiler_dso_type ();
360 tree dso = build_local_temp (dso_type);
362 vec<constructor_elt, va_gc> *ve = NULL;
363 CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_integer_cst (1, size_type_node));
364 CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (dso_slot_node));
365 CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (start_minfo_node));
366 CONSTRUCTOR_APPEND_ELT (ve, NULL_TREE, build_address (stop_minfo_node));
368 tree assign_expr = modify_expr (dso, build_struct_literal (dso_type, ve));
369 expr_list = compound_expr (expr_list, assign_expr);
371 /* _d_dso_registry (&dso); */
372 tree call_expr = build_call_expr (get_dso_registry_fn (), 1,
373 build_address (dso));
374 expr_list = compound_expr (expr_list, call_expr);
376 add_stmt (build_vcondition (if_cond, expr_list, void_node));
377 finish_function (old_context);
379 return decl;
382 /* Build a variable used in the dso_registry code identified by NAME,
383 and data type TYPE. The variable always has VISIBILITY_HIDDEN and
384 TREE_PUBLIC flags set. */
386 static tree
387 build_dso_registry_var (const char * name, tree type)
389 tree var = declare_extern_var (get_identifier (name), type);
390 DECL_VISIBILITY (var) = VISIBILITY_HIDDEN;
391 DECL_VISIBILITY_SPECIFIED (var) = 1;
392 return var;
395 /* Place a reference to the ModuleInfo symbol MINFO for DECL into the
396 `minfo' section. Then create the global ctors/dtors to call the
397 _d_dso_registry function if necessary. */
399 static void
400 register_moduleinfo (Module *decl, tree minfo)
402 gcc_assert (targetm_common.have_named_sections);
404 /* Build the ModuleInfo reference, this is done once for every Module. */
405 tree ident = mangle_internal_decl (decl, "__moduleRef", "Z");
406 tree mref = declare_extern_var (ident, ptr_type_node);
408 /* Build the initializer and emit. Do not start section with a `.' character
409 so that the linker will provide a __start_ and __stop_ symbol to indicate
410 the start and end address of the section respectively.
411 https://sourceware.org/binutils/docs-2.26/ld/Orphan-Sections.html. */
412 DECL_INITIAL (mref) = build_address (minfo);
413 DECL_EXTERNAL (mref) = 0;
414 DECL_PRESERVE_P (mref) = 1;
416 set_decl_section_name (mref, "minfo");
417 d_pushdecl (mref);
418 rest_of_decl_compilation (mref, 1, 0);
420 /* Only for the first D module being emitted do we need to generate a static
421 constructor and destructor for. These are only required once per shared
422 library, so it's safe to emit them only once per object file. */
423 static bool first_module = true;
424 if (!first_module)
425 return;
427 start_minfo_node = build_dso_registry_var ("__start_minfo", ptr_type_node);
428 rest_of_decl_compilation (start_minfo_node, 1, 0);
430 stop_minfo_node = build_dso_registry_var ("__stop_minfo", ptr_type_node);
431 rest_of_decl_compilation (stop_minfo_node, 1, 0);
433 /* Declare dso_slot and dso_initialized. */
434 dso_slot_node = build_dso_registry_var (GDC_PREFIX ("dso_slot"),
435 ptr_type_node);
436 DECL_EXTERNAL (dso_slot_node) = 0;
437 d_comdat_linkage (dso_slot_node);
438 rest_of_decl_compilation (dso_slot_node, 1, 0);
440 dso_initialized_node = build_dso_registry_var (GDC_PREFIX ("dso_initialized"),
441 boolean_type_node);
442 DECL_EXTERNAL (dso_initialized_node) = 0;
443 d_comdat_linkage (dso_initialized_node);
444 rest_of_decl_compilation (dso_initialized_node, 1, 0);
446 /* Declare dso_ctor() and dso_dtor(). */
447 tree dso_ctor = build_dso_cdtor_fn (true);
448 vec_safe_push (static_ctor_list, dso_ctor);
450 tree dso_dtor = build_dso_cdtor_fn (false);
451 vec_safe_push (static_dtor_list, dso_dtor);
453 first_module = false;
456 /* Convenience function for layout_moduleinfo_fields. Adds a field of TYPE to
457 the moduleinfo record at OFFSET, incrementing the offset to the next field
458 position. No alignment is taken into account, all fields are packed. */
460 static void
461 layout_moduleinfo_field (tree type, tree rec_type, HOST_WIDE_INT& offset)
463 tree field = create_field_decl (type, NULL, 1, 1);
464 insert_aggregate_field (rec_type, field, offset);
465 offset += int_size_in_bytes (type);
468 /* Layout fields that immediately come after the moduleinfo TYPE for DECL.
469 Data relating to the module is packed into the type on an as-needed
470 basis, this is done to keep its size to a minimum. */
472 static tree
473 layout_moduleinfo_fields (Module *decl, tree type)
475 HOST_WIDE_INT offset = int_size_in_bytes (type);
476 type = copy_aggregate_type (type);
478 /* First fields added are all the function pointers. */
479 if (decl->sctor)
480 layout_moduleinfo_field (ptr_type_node, type, offset);
482 if (decl->sdtor)
483 layout_moduleinfo_field (ptr_type_node, type, offset);
485 if (decl->ssharedctor)
486 layout_moduleinfo_field (ptr_type_node, type, offset);
488 if (decl->sshareddtor)
489 layout_moduleinfo_field (ptr_type_node, type, offset);
491 if (decl->findGetMembers ())
492 layout_moduleinfo_field (ptr_type_node, type, offset);
494 if (decl->sictor)
495 layout_moduleinfo_field (ptr_type_node, type, offset);
497 if (decl->stest)
498 layout_moduleinfo_field (ptr_type_node, type, offset);
500 /* Array of module imports is laid out as a length field, followed by
501 a static array of ModuleInfo pointers. */
502 size_t aimports_dim = decl->aimports.dim;
503 for (size_t i = 0; i < decl->aimports.dim; i++)
505 Module *mi = decl->aimports[i];
506 if (!mi->needmoduleinfo)
507 aimports_dim--;
510 if (aimports_dim)
512 layout_moduleinfo_field (size_type_node, type, offset);
513 layout_moduleinfo_field (make_array_type (Type::tvoidptr, aimports_dim),
514 type, offset);
517 /* Array of local ClassInfo decls are laid out in the same way. */
518 ClassDeclarations aclasses;
519 for (size_t i = 0; i < decl->members->dim; i++)
521 Dsymbol *member = (*decl->members)[i];
522 member->addLocalClass (&aclasses);
525 if (aclasses.dim)
527 layout_moduleinfo_field (size_type_node, type, offset);
528 layout_moduleinfo_field (make_array_type (Type::tvoidptr, aclasses.dim),
529 type, offset);
532 /* Lastly, the name of the module is a static char array. */
533 size_t namelen = strlen (decl->toPrettyChars ()) + 1;
534 layout_moduleinfo_field (make_array_type (Type::tchar, namelen),
535 type, offset);
537 finish_aggregate_type (offset, 1, type, NULL);
539 return type;
542 /* Output the ModuleInfo for module DECL and register it with druntime. */
544 static void
545 layout_moduleinfo (Module *decl)
547 ClassDeclarations aclasses;
548 FuncDeclaration *sgetmembers;
550 for (size_t i = 0; i < decl->members->dim; i++)
552 Dsymbol *member = (*decl->members)[i];
553 member->addLocalClass (&aclasses);
556 size_t aimports_dim = decl->aimports.dim;
557 for (size_t i = 0; i < decl->aimports.dim; i++)
559 Module *mi = decl->aimports[i];
560 if (!mi->needmoduleinfo)
561 aimports_dim--;
564 sgetmembers = decl->findGetMembers ();
566 size_t flags = 0;
567 if (decl->sctor)
568 flags |= MItlsctor;
569 if (decl->sdtor)
570 flags |= MItlsdtor;
571 if (decl->ssharedctor)
572 flags |= MIctor;
573 if (decl->sshareddtor)
574 flags |= MIdtor;
575 if (sgetmembers)
576 flags |= MIxgetMembers;
577 if (decl->sictor)
578 flags |= MIictor;
579 if (decl->stest)
580 flags |= MIunitTest;
581 if (aimports_dim)
582 flags |= MIimportedModules;
583 if (aclasses.dim)
584 flags |= MIlocalClasses;
585 if (!decl->needmoduleinfo)
586 flags |= MIstandalone;
588 flags |= MIname;
590 tree minfo = get_moduleinfo_decl (decl);
591 tree type = layout_moduleinfo_fields (decl, TREE_TYPE (minfo));
593 /* Put out the two named fields in a ModuleInfo decl:
594 uint flags;
595 uint index; */
596 vec<constructor_elt, va_gc> *minit = NULL;
598 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
599 build_integer_cst (flags, d_uint_type));
601 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
602 build_integer_cst (0, d_uint_type));
604 /* Order of appearance, depending on flags:
605 void function() tlsctor;
606 void function() tlsdtor;
607 void* function() xgetMembers;
608 void function() ctor;
609 void function() dtor;
610 void function() ictor;
611 void function() unitTest;
612 ModuleInfo*[] importedModules;
613 TypeInfo_Class[] localClasses;
614 char[N] name;
616 if (flags & MItlsctor)
617 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sctor));
619 if (flags & MItlsdtor)
620 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sdtor));
622 if (flags & MIctor)
623 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
624 build_address (decl->ssharedctor));
626 if (flags & MIdtor)
627 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
628 build_address (decl->sshareddtor));
630 if (flags & MIxgetMembers)
631 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
632 build_address (get_symbol_decl (sgetmembers)));
634 if (flags & MIictor)
635 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->sictor));
637 if (flags & MIunitTest)
638 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, build_address (decl->stest));
640 if (flags & MIimportedModules)
642 vec<constructor_elt, va_gc> *elms = NULL;
643 tree satype = make_array_type (Type::tvoidptr, aimports_dim);
644 size_t idx = 0;
646 for (size_t i = 0; i < decl->aimports.dim; i++)
648 Module *mi = decl->aimports[i];
649 if (mi->needmoduleinfo)
651 CONSTRUCTOR_APPEND_ELT (elms, size_int (idx),
652 build_address (get_moduleinfo_decl (mi)));
653 idx++;
657 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, size_int (aimports_dim));
658 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
659 build_constructor (satype, elms));
662 if (flags & MIlocalClasses)
664 vec<constructor_elt, va_gc> *elms = NULL;
665 tree satype = make_array_type (Type::tvoidptr, aclasses.dim);
667 for (size_t i = 0; i < aclasses.dim; i++)
669 ClassDeclaration *cd = aclasses[i];
670 CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
671 build_address (get_classinfo_decl (cd)));
674 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, size_int (aclasses.dim));
675 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE,
676 build_constructor (satype, elms));
679 if (flags & MIname)
681 /* Put out module name as a 0-terminated C-string, to save bytes. */
682 const char *name = decl->toPrettyChars ();
683 size_t namelen = strlen (name) + 1;
684 tree strtree = build_string (namelen, name);
685 TREE_TYPE (strtree) = make_array_type (Type::tchar, namelen);
686 CONSTRUCTOR_APPEND_ELT (minit, NULL_TREE, strtree);
689 TREE_TYPE (minfo) = type;
690 DECL_INITIAL (minfo) = build_struct_literal (type, minit);
691 d_finish_decl (minfo);
693 /* Register the module against druntime. */
694 register_moduleinfo (decl, minfo);
697 /* Send the Module AST class DECL to GCC back-end. */
699 void
700 build_module_tree (Module *decl)
702 /* There may be more than one module per object file, but should only
703 ever compile them one at a time. */
704 assert (!current_moduleinfo && !current_module_decl);
706 module_info mi = module_info ();
708 current_moduleinfo = &mi;
709 current_module_decl = decl;
711 /* Layout module members. */
712 if (decl->members)
714 for (size_t i = 0; i < decl->members->dim; i++)
716 Dsymbol *s = (*decl->members)[i];
717 build_decl_tree (s);
721 /* Default behavior is to always generate module info because of templates.
722 Can be switched off for not compiling against runtime library. */
723 if (!global.params.betterC
724 && decl->ident != Identifier::idPool ("__entrypoint"))
726 if (mi.ctors || mi.ctorgates)
727 decl->sctor = build_funcs_gates_fn (get_identifier ("*__modctor"),
728 mi.ctors, mi.ctorgates);
730 if (mi.dtors)
731 decl->sdtor = build_funcs_gates_fn (get_identifier ("*__moddtor"),
732 mi.dtors, NULL);
734 if (mi.sharedctors || mi.sharedctorgates)
735 decl->ssharedctor
736 = build_funcs_gates_fn (get_identifier ("*__modsharedctor"),
737 mi.sharedctors, mi.sharedctorgates);
739 if (mi.shareddtors)
740 decl->sshareddtor
741 = build_funcs_gates_fn (get_identifier ("*__modshareddtor"),
742 mi.shareddtors, NULL);
744 if (mi.unitTests)
745 decl->stest = build_funcs_gates_fn (get_identifier ("*__modtest"),
746 mi.unitTests, NULL);
748 layout_moduleinfo (decl);
751 current_moduleinfo = NULL;
752 current_module_decl = NULL;
755 /* Returns the current function or module context for the purpose
756 of imported_module_or_decl. */
758 tree
759 d_module_context (void)
761 if (cfun != NULL)
762 return current_function_decl;
764 gcc_assert (current_module_decl != NULL);
765 return build_import_decl (current_module_decl);
768 /* Maybe record declaration D against our module information structure. */
770 void
771 register_module_decl (Declaration *d)
773 FuncDeclaration *fd = d->isFuncDeclaration ();
774 if (fd != NULL)
776 tree decl = get_symbol_decl (fd);
778 /* If a static constructor, push into the current ModuleInfo.
779 Checks for `shared' first because it derives from the non-shared
780 constructor type in the front-end. */
781 if (fd->isSharedStaticCtorDeclaration ())
782 vec_safe_push (current_moduleinfo->sharedctors, decl);
783 else if (fd->isStaticCtorDeclaration ())
784 vec_safe_push (current_moduleinfo->ctors, decl);
786 /* If a static destructor, do same as with constructors, but also
787 increment the destructor's vgate at construction time. */
788 if (fd->isSharedStaticDtorDeclaration ())
790 VarDeclaration *vgate = ((SharedStaticDtorDeclaration *) fd)->vgate;
791 if (vgate != NULL)
793 tree gate = get_symbol_decl (vgate);
794 vec_safe_push (current_moduleinfo->sharedctorgates, gate);
796 vec_safe_insert (current_moduleinfo->shareddtors, 0, decl);
798 else if (fd->isStaticDtorDeclaration ())
800 VarDeclaration *vgate = ((StaticDtorDeclaration *) fd)->vgate;
801 if (vgate != NULL)
803 tree gate = get_symbol_decl (vgate);
804 vec_safe_push (current_moduleinfo->ctorgates, gate);
806 vec_safe_insert (current_moduleinfo->dtors, 0, decl);
809 /* If a unittest function. */
810 if (fd->isUnitTestDeclaration ())
811 vec_safe_push (current_moduleinfo->unitTests, decl);
815 /* Wrapup all global declarations and start the final compilation. */
817 void
818 d_finish_compilation (tree *vec, int len)
820 /* Complete all generated thunks. */
821 symtab->process_same_body_aliases ();
823 /* Process all file scopes in this compilation, and the external_scope,
824 through wrapup_global_declarations. */
825 for (int i = 0; i < len; i++)
827 tree decl = vec[i];
828 wrapup_global_declarations (&decl, 1);
831 /* If the target does not directly support static constructors,
832 static_ctor_list contains a list of all static constructors defined
833 so far. This routine will create a function to call all of those
834 and is picked up by collect2. */
835 if (static_ctor_list)
837 tree decl = build_funcs_gates_fn (get_file_function_name ("I"),
838 static_ctor_list, NULL);
839 DECL_STATIC_CONSTRUCTOR (decl) = 1;
840 decl_init_priority_insert (decl, DEFAULT_INIT_PRIORITY);
843 if (static_dtor_list)
845 tree decl = build_funcs_gates_fn (get_file_function_name ("D"),
846 static_dtor_list, NULL);
847 DECL_STATIC_DESTRUCTOR (decl) = 1;
848 decl_fini_priority_insert (decl, DEFAULT_INIT_PRIORITY);
853 #include "gt-d-modules.h"