comparison dmd/TypeInfoStructDeclaration.d @ 0:10317f0c89a5

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