annotate dmd/TypeDelegate.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 2e2a5c3f943a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.TypeDelegate;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.TypeNext;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.MOD;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.AddExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.PtrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.NullExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.TypeFunction;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.CppMangleState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.Argument;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TypeInfoDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.TypeInfoDelegateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.TYPE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.backend.Classsym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.backend.SC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.backend.LIST;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 class TypeDelegate : TypeNext
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 // .next is a TypeFunction
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 this(Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 super(TY.Tfunction, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 ty = TY.Tdelegate;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 version (DumbClone) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 Type clone()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 Type syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 Type t = next.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 if (t == next)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 t = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 t = new TypeDelegate(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 t.mod = mod;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 return t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 Type semantic(Loc loc, Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 if (deco) // if semantic() already run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 //printf("already done\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 next = next.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 return merge();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 ulong size(Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return PTRSIZE * 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 if (mod != this.mod)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 toCBuffer3(buf, hgs, mod);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 TypeFunction tf = cast(TypeFunction)next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 tf.next.toCBuffer2(buf, hgs, MODundefined);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 buf.writestring(" delegate");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 Argument.argsToCBuffer(buf, hgs, tf.parameters, tf.varargs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 Expression defaultInit(Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 version (LOGDEFAULTINIT) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 printf("TypeDelegate.defaultInit() '%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e = new NullExp(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 e.type = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 bool isZeroInit(Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 bool checkBoolean()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 TypeInfoDeclaration getTypeInfoDeclaration()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 return new TypeInfoDelegateDeclaration(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 Expression dotExp(Scope sc, Expression e, Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 version (LOGDOTEXP) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 printf("TypeDelegate.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 if (ident == Id.ptr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 e.type = tvoidptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 else if (ident == Id.funcptr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 e = e.addressOf(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 e.type = tvoidptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 e = new AddExp(e.loc, e, new IntegerExp(PTRSIZE));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 e.type = tvoidptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 e = new PtrExp(e.loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 e.type = next.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 e = Type.dotExp(sc, e, ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 bool hasPointers()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 version (CPP_MANGLE) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 void toCppMangle(OutBuffer buf, CppMangleState* cms)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 type* toCtype()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 type* t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 if (ctype)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 return ctype;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 if (0 && global.params.symdebug)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 /* A delegate consists of:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 * _Delegate { void* frameptr; Function *funcptr; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 static Symbol* s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 if (!s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 s = symbol_calloc("_Delegate");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 s.Sclass = SC.SCstruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 s.Sstruct = struct_calloc();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 s.Sstruct.Sflags |= 0; /// huh?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 s.Sstruct.Salignsize = alignsize();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 s.Sstruct.Sstructalign = cast(ubyte)global.structalign;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 s.Sstruct.Sstructsize = cast(uint)size(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 slist_add(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 Symbol* s1 = symbol_name("frameptr", SC.SCmember, Type.tvoidptr.toCtype());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 list_append(&s.Sstruct.Sfldlst, s1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 Symbol* s2 = symbol_name("funcptr", SC.SCmember, Type.tvoidptr.toCtype());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 s2.Smemoff = cast(uint)Type.tvoidptr.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 list_append(&s.Sstruct.Sfldlst, s2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 t = type_alloc(TYM.TYstruct);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 t.Ttag = cast(Classsym*)s; // structure tag name
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 t.Tcount++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 s.Stype = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 if (global.params.symdebug == 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 // Generate D symbolic debug info, rather than C
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 t = type_allocn(TYM.TYdelegate, next.toCtype());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 t = type_fake(TYM.TYdelegate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 t.Tcount++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 ctype = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 return t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 }