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