Mercurial > projects > ldc
annotate lphobos/std/moduleinit.d @ 650:aa6a0b7968f7
Added test case for bug #100
Removed dubious check for not emitting static private global in other modules without access. This should be handled properly somewhere else, it's causing unresolved global errors for stuff that should work (in MiniD)
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Sun, 05 Oct 2008 17:28:15 +0200 |
parents | 373489eeaf90 |
children | eef8ac26c66c |
rev | line source |
---|---|
89 | 1 // Modified for LLVMDC |
2 | |
3 module std.moduleinit; | |
4 | |
5 //debug = 1; | |
6 | |
7 private | |
8 { | |
9 import object; | |
10 import std.c.stdio; | |
11 import std.c.stdlib; | |
12 } | |
13 | |
14 enum | |
15 { MIctorstart = 1, // we've started constructing it | |
16 MIctordone = 2, // finished construction | |
17 MIstandalone = 4, // module ctor does not depend on other module | |
18 // ctors being done first | |
19 } | |
20 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
89
diff
changeset
|
21 // had to move the class to object.d, as its declaration is needed in the compiler code, |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
89
diff
changeset
|
22 // otherwise the DMDFE Module::moduleinfo member is NULL |
89 | 23 |
24 class ModuleCtorError : Exception | |
25 { | |
26 this(ModuleInfo m) | |
27 { | |
28 super("circular initialization dependency with module " ~ m.name); | |
29 } | |
30 } | |
31 | |
32 | |
117 | 33 // this gets initialized in _moduleCtor() |
89 | 34 extern (C) ModuleInfo[] _moduleinfo_array; |
35 | |
117 | 36 // this method returns the linker constructed, null terminated, array of moduleinfos |
37 extern (C) void** _d_get_moduleinfo_array(); | |
89 | 38 |
39 ModuleInfo[] _moduleinfo_dtors; | |
40 uint _moduleinfo_dtors_i; | |
41 | |
42 // Register termination function pointers | |
43 extern (C) int _fatexit(void *); | |
44 | |
45 /************************************* | |
46 * Initialize the modules. | |
47 */ | |
48 | |
49 extern (C) void _moduleCtor() | |
50 { | |
51 debug printf("_moduleCtor()\n"); | |
52 int len = 0; | |
53 | |
473
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
54 for (auto mr = _Dmodule_ref; mr; mr=mr.next) |
89 | 55 len++; |
56 _moduleinfo_array = new ModuleInfo[len]; | |
57 len = 0; | |
473
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
58 for (auto mr = _Dmodule_ref; mr; mr=mr.next) |
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
59 { _moduleinfo_array[len] = mr.mod; |
89 | 60 len++; |
61 } | |
62 | |
63 version (Win32) | |
64 { | |
65 // Ensure module destructors also get called on program termination | |
66 //_fatexit(&_STD_moduleDtor); | |
67 } | |
68 | |
117 | 69 _moduleinfo_dtors = new ModuleInfo[len]; |
89 | 70 debug printf("_moduleinfo_dtors = x%x\n", cast(void *)_moduleinfo_dtors); |
71 _moduleCtor2(_moduleinfo_array, 0); | |
72 | |
73 version (none) | |
74 { | |
75 foreach (m; _moduleinfo_array) | |
76 { | |
77 writefln("module %s, %d", m.name, m.localClasses.length); | |
78 foreach (c; m.localClasses) | |
79 { | |
80 writefln("\tclass %s", c.name); | |
81 } | |
82 } | |
83 } | |
84 } | |
85 | |
86 void _moduleCtor2(ModuleInfo[] mi, int skip) | |
87 { | |
88 debug printf("_moduleCtor2(): %d modules\n", mi.length); | |
89 for (uint i = 0; i < mi.length; i++) | |
90 { | |
91 ModuleInfo m = mi[i]; | |
92 | |
93 debug printf("\tmodule[%d] = '%p'\n", i, m); | |
94 if (!m) | |
95 continue; | |
473
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
96 debug printf("\tmodule[%d] = '%.*s'\n", i, m.name.length, m.name.ptr); |
89 | 97 if (m.flags & MIctordone) |
98 continue; | |
473
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
99 debug printf("\tmodule[%d] = '%.*s', m = 0x%x\n", i, m.name.length, m.name.ptr, m); |
89 | 100 |
101 if (m.ctor || m.dtor) | |
102 { | |
103 if (m.flags & MIctorstart) | |
104 { if (skip || m.flags & MIstandalone) | |
105 continue; | |
473
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
106 debug printf("\tmodule[%d] = '%.*s', cyclic dependency!\n", i, m.name.length, m.name.ptr); |
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
107 int x = 0; x /= x; |
89 | 108 throw new ModuleCtorError(m); |
109 } | |
110 | |
111 m.flags |= MIctorstart; | |
112 _moduleCtor2(m.importedModules, 0); | |
113 if (m.ctor) | |
114 (*m.ctor)(); | |
115 m.flags &= ~MIctorstart; | |
116 m.flags |= MIctordone; | |
117 | |
118 // Now that construction is done, register the destructor | |
119 //printf("\tadding module dtor x%x\n", m); | |
120 assert(_moduleinfo_dtors_i < _moduleinfo_dtors.length); | |
121 _moduleinfo_dtors[_moduleinfo_dtors_i++] = m; | |
122 } | |
123 else | |
124 { | |
125 m.flags |= MIctordone; | |
126 _moduleCtor2(m.importedModules, 1); | |
127 } | |
128 } | |
129 } | |
130 | |
131 | |
132 /********************************** | |
133 * Destruct the modules. | |
134 */ | |
135 | |
136 // Starting the name with "_STD" means under linux a pointer to the | |
137 // function gets put in the .dtors segment. | |
138 | |
139 extern (C) void _moduleDtor() | |
140 { | |
141 debug printf("_moduleDtor(): %d modules\n", _moduleinfo_dtors_i); | |
142 for (uint i = _moduleinfo_dtors_i; i-- != 0;) | |
143 { | |
144 ModuleInfo m = _moduleinfo_dtors[i]; | |
145 | |
146 debug printf("\tmodule[%d] = '%.*s', x%x\n", i, m.name, m); | |
147 if (m.dtor) | |
148 { | |
149 (*m.dtor)(); | |
150 } | |
151 } | |
152 debug printf("_moduleDtor() done\n"); | |
153 } | |
154 | |
155 /********************************** | |
156 * Run unit tests. | |
157 */ | |
158 | |
159 extern (C) void _moduleUnitTests() | |
160 { | |
473
373489eeaf90
Applied downs' lphobos update
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
117
diff
changeset
|
161 debug printf("_moduleUnitTests() %i\n", _moduleinfo_array.length); |
89 | 162 for (uint i = 0; i < _moduleinfo_array.length; i++) |
163 { | |
164 ModuleInfo m = _moduleinfo_array[i]; | |
165 | |
166 if (!m) | |
167 continue; | |
168 | |
169 debug printf("\tmodule[%d] = '%.*s'\n", i, m.name); | |
170 if (m.unitTest) | |
171 { | |
172 (*m.unitTest)(); | |
173 } | |
174 } | |
175 } | |
176 |