Mercurial > projects > ddmd
annotate dmd/TypeInfoStructDeclaration.d @ 126:1765f3ef917d
ClassDeclarations, Arguments -> Vector
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Fri, 03 Sep 2010 23:25:55 +0100 |
parents | e28b18c23469 |
children | 60bb0fe4563e |
rev | line source |
---|---|
72 | 1 module dmd.TypeInfoStructDeclaration; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Type; |
5 import dmd.TY; | |
6 import dmd.MOD; | |
7 import dmd.Loc; | |
8 import dmd.Argument; | |
9 import dmd.STC; | |
10 import dmd.TypeStruct; | |
11 import dmd.TypeFunction; | |
12 import dmd.StructDeclaration; | |
13 import dmd.FuncDeclaration; | |
14 import dmd.Dsymbol; | |
15 import dmd.ArrayTypes; | |
16 import dmd.Scope; | |
17 import dmd.LINK; | |
18 import dmd.Id; | |
19 import dmd.TypeInfoDeclaration; | |
20 import dmd.backend.dt_t; | |
21 import dmd.backend.TYM; | |
22 import dmd.backend.Util; | |
23 import dmd.expression.Util; | |
24 | |
25 import std.string : toStringz; | |
26 | |
0 | 27 class TypeInfoStructDeclaration : TypeInfoDeclaration |
28 { | |
29 this(Type tinfo) | |
30 { | |
31 super(tinfo, 0); | |
73 | 32 type = Type.typeinfostruct.type; |
0 | 33 } |
34 | |
72 | 35 override void toDt(dt_t** pdt) |
0 | 36 { |
72 | 37 //printf("TypeInfoStructDeclaration.toDt() '%s'\n", toChars()); |
38 | |
39 uint offset = Type.typeinfostruct.structsize; | |
40 | |
41 dtxoff(pdt, Type.typeinfostruct.toVtblSymbol(), 0, TYM.TYnptr); // vtbl for TypeInfo_Struct | |
42 dtdword(pdt, 0); // monitor | |
43 | |
44 assert(tinfo.ty == TY.Tstruct); | |
45 | |
46 TypeStruct tc = cast(TypeStruct)tinfo; | |
47 StructDeclaration sd = tc.sym; | |
48 | |
49 /* Put out: | |
50 * char[] name; | |
51 * void[] init; | |
52 * hash_t function(in void*) xtoHash; | |
53 * bool function(in void*, in void*) xopEquals; | |
54 * int function(in void*, in void*) xopCmp; | |
55 * string function(const(void)*) xtoString; | |
56 * uint m_flags; | |
57 * xgetMembers; | |
58 * xdtor; | |
59 * xpostblit; | |
60 * | |
61 * name[] | |
62 */ | |
63 | |
64 string name = sd.toPrettyChars(); | |
65 size_t namelen = name.length; | |
66 dtdword(pdt, namelen); | |
67 | |
68 //dtabytes(pdt, TYnptr, 0, namelen + 1, name); | |
69 dtxoff(pdt, toSymbol(), offset, TYM.TYnptr); | |
70 offset += namelen + 1; | |
71 | |
72 // void[] init; | |
73 dtdword(pdt, sd.structsize); // init.length | |
74 if (sd.zeroInit) | |
75 dtdword(pdt, 0); // null for 0 initialization | |
76 else | |
77 dtxoff(pdt, sd.toInitializer(), 0, TYM.TYnptr); // init.ptr | |
78 FuncDeclaration fd; | |
79 FuncDeclaration fdx; | |
80 TypeFunction tf; | |
81 Type ta; | |
82 Dsymbol s; | |
83 | |
84 static TypeFunction tftohash; | |
85 static TypeFunction tftostring; | |
86 | |
87 if (!tftohash) | |
88 { | |
89 scope Scope sc = new Scope(); | |
90 | |
91 tftohash = new TypeFunction(null, Type.thash_t, 0, LINK.LINKd); | |
92 tftohash.mod = MOD.MODconst; | |
93 tftohash = cast(TypeFunction)tftohash.semantic(Loc(0), sc); | |
94 | |
95 tftostring = new TypeFunction(null, Type.tchar.invariantOf().arrayOf(), 0, LINK.LINKd); | |
96 tftostring = cast(TypeFunction)tftostring.semantic(Loc(0), sc); | |
97 } | |
98 | |
99 TypeFunction tfeqptr; | |
100 { | |
101 // bool opEqual(const T*) const; | |
102 scope Scope sc = new Scope(); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
103 auto arguments = new Arguments; |
72 | 104 version (STRUCTTHISREF) { |
105 // arg type is ref const T | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
106 auto arg = new Argument(STC.STCref, tc.constOf(), null, null); |
72 | 107 } else { |
108 // arg type is const T* | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
109 auto arg = new Argument(STC.STCin, tc.pointerTo(), null, null); |
72 | 110 } |
111 | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
112 arguments.push(arg); |
72 | 113 tfeqptr = new TypeFunction(arguments, Type.tbool, 0, LINK.LINKd); |
114 tfeqptr.mod = MOD.MODconst; | |
115 tfeqptr = cast(TypeFunction)tfeqptr.semantic(Loc(0), sc); | |
116 } | |
117 | |
118 TypeFunction tfcmpptr; | |
119 { | |
120 scope Scope sc = new Scope(); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
121 auto arguments = new Arguments; |
72 | 122 version (STRUCTTHISREF) { |
123 // arg type is ref const T | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
124 auto arg = new Argument(STC.STCref, tc.constOf(), null, null); |
72 | 125 } else { |
126 // arg type is const T* | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
127 auto arg = new Argument(STC.STCin, tc.pointerTo(), null, null); |
72 | 128 } |
129 | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
130 arguments.push(arg); |
72 | 131 tfcmpptr = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd); |
132 tfcmpptr.mod = MOD.MODconst; | |
133 tfcmpptr = cast(TypeFunction)tfcmpptr.semantic(Loc(0), sc); | |
134 } | |
135 | |
136 s = search_function(sd, Id.tohash); | |
137 fdx = s ? s.isFuncDeclaration() : null; | |
138 if (fdx) | |
139 { | |
140 fd = fdx.overloadExactMatch(tftohash); | |
141 if (fd) | |
142 dtxoff(pdt, fd.toSymbol(), 0, TYM.TYnptr); | |
143 else | |
144 //fdx.error("must be declared as extern (D) uint toHash()"); | |
145 dtdword(pdt, 0); | |
146 } | |
147 else | |
148 dtdword(pdt, 0); | |
149 | |
150 s = search_function(sd, Id.eq); | |
151 fdx = s ? s.isFuncDeclaration() : null; | |
152 if (fdx) | |
153 { | |
154 //printf("test1 %s, %s, %s\n", fdx.toChars(), fdx.type.toChars(), tfeqptr.toChars()); | |
155 fd = fdx.overloadExactMatch(tfeqptr); | |
156 if (fd) | |
157 dtxoff(pdt, fd.toSymbol(), 0, TYM.TYnptr); | |
158 else | |
159 { | |
160 fd = fdx.overloadExactMatch(tfcmpptr); | |
161 if (fd) | |
162 fdx.error("must return bool, not int"); | |
163 //fdx.error("must be declared as extern (D) int %s(%s*)", fdx.toChars(), sd.toChars()); | |
164 dtdword(pdt, 0); | |
165 } | |
166 } | |
167 else | |
168 dtdword(pdt, 0); | |
169 | |
170 s = search_function(sd, Id.cmp); | |
171 fdx = s ? s.isFuncDeclaration() : null; | |
172 if (fdx) | |
173 { | |
174 //printf("test1 %s, %s, %s\n", fdx.toChars(), fdx.type.toChars(), tfeqptr.toChars()); | |
175 fd = fdx.overloadExactMatch(tfcmpptr); | |
176 if (fd) | |
177 { | |
178 dtxoff(pdt, fd.toSymbol(), 0, TYM.TYnptr); | |
179 //printf("test2\n"); | |
180 } | |
181 else | |
182 //fdx.error("must be declared as extern (D) int %s(%s*)", fdx.toChars(), sd.toChars()); | |
183 dtdword(pdt, 0); | |
184 } | |
185 else | |
186 dtdword(pdt, 0); | |
187 | |
188 s = search_function(sd, Id.tostring); | |
189 fdx = s ? s.isFuncDeclaration() : null; | |
190 if (fdx) | |
191 { | |
192 fd = fdx.overloadExactMatch(tftostring); | |
193 if (fd) | |
194 dtxoff(pdt, fd.toSymbol(), 0, TYM.TYnptr); | |
195 else | |
196 //fdx.error("must be declared as extern (D) char[] toString()"); | |
197 dtdword(pdt, 0); | |
198 } | |
199 else | |
200 dtdword(pdt, 0); | |
201 | |
202 // uint m_flags; | |
203 dtdword(pdt, tc.hasPointers()); | |
204 | |
205 version (DMDV2) { | |
206 // xgetMembers | |
207 FuncDeclaration sgetmembers = sd.findGetMembers(); | |
208 if (sgetmembers) | |
209 dtxoff(pdt, sgetmembers.toSymbol(), 0, TYM.TYnptr); | |
210 else | |
211 dtdword(pdt, 0); // xgetMembers | |
212 | |
213 // xdtor | |
214 FuncDeclaration sdtor = sd.dtor; | |
215 if (sdtor) | |
216 dtxoff(pdt, sdtor.toSymbol(), 0, TYM.TYnptr); | |
217 else | |
218 dtdword(pdt, 0); // xdtor | |
219 | |
220 // xpostblit | |
221 FuncDeclaration spostblit = sd.postblit; | |
222 if (spostblit) | |
223 dtxoff(pdt, spostblit.toSymbol(), 0, TYM.TYnptr); | |
224 else | |
225 dtdword(pdt, 0); // xpostblit | |
226 } | |
227 // name[] | |
228 dtnbytes(pdt, namelen + 1, toStringz(name)); | |
0 | 229 } |
230 } | |
231 |