comparison dmd/TypePointer.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.TypePointer;
2
3 import dmd.Type;
4 import dmd.Loc;
5 import dmd.Scope;
6 import dmd.TypeNext;
7 import dmd.OutBuffer;
8 import dmd.HdrGenState;
9 import dmd.MATCH;
10 import dmd.Expression;
11 import dmd.NullExp;
12 import dmd.TypeInfoDeclaration;
13 import dmd.TypeInfoPointerDeclaration;
14 import dmd.CppMangleState;
15 import dmd.TY;
16 import dmd.Util;
17 import dmd.MOD;
18 import dmd.Global;
19
20 import dmd.backend.TYPE;
21 import dmd.backend.Util;
22 import dmd.backend.TYM;
23
24 class TypePointer : TypeNext
25 {
26 this(Type t)
27 {
28 super(TY.Tpointer, t);
29 }
30 version (DumbClone) {
31 } else {
32 final TypePointer cloneTo(TypePointer t)
33 {
34 super.cloneTo(t);
35 return t;
36 }
37
38 TypePointer clone()
39 {
40 assert(this.classinfo == TypePointer.classinfo);
41 return cloneTo(new TypePointer(next));
42 }
43 }
44 Type syntaxCopy()
45 {
46 Type t = next.syntaxCopy();
47 if (t == next)
48 t = this;
49 else
50 {
51 t = new TypePointer(t);
52 t.mod = mod;
53 }
54 return t;
55 }
56
57 Type semantic(Loc loc, Scope sc)
58 {
59 //printf("TypePointer.semantic()\n");
60 if (deco)
61 return this;
62 Type n = next.semantic(loc, sc);
63 switch (n.toBasetype().ty)
64 {
65 case TY.Ttuple:
66 error(loc, "can't have pointer to %s", n.toChars());
67 n = tint32;
68 break;
69 default:
70 break;
71 }
72 if (n !is next)
73 {
74 deco = null;
75 }
76 next = n;
77 transitive();
78 return merge();
79 }
80
81 ulong size(Loc loc)
82 {
83 return PTRSIZE;
84 }
85
86 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod)
87 {
88 //printf("TypePointer::toCBuffer2() next = %d\n", next->ty);
89 if (mod != this.mod)
90 {
91 toCBuffer3(buf, hgs, mod);
92 return;
93 }
94 next.toCBuffer2(buf, hgs, this.mod);
95 if (next.ty != Tfunction)
96 buf.writeByte('*');
97 }
98
99 MATCH implicitConvTo(Type to)
100 {
101 //printf("TypePointer.implicitConvTo(to = %s) %s\n", to.toChars(), toChars());
102
103 if (equals(to))
104 return MATCH.MATCHexact;
105 if (to.ty == TY.Tpointer)
106 {
107 TypePointer tp = cast(TypePointer)to;
108 assert(tp.next);
109
110 if (!(next.mod == tp.next.mod || tp.next.mod == MOD.MODconst))
111 return MATCH.MATCHnomatch; // not const-compatible
112
113 /* Alloc conversion to void[]
114 */
115 if (next.ty != TY.Tvoid && tp.next.ty == TY.Tvoid)
116 {
117 return MATCH.MATCHconvert;
118 }
119
120 MATCH m = next.constConv(tp.next);
121 if (m != MATCH.MATCHnomatch)
122 {
123 if (m == MATCH.MATCHexact && mod != to.mod)
124 m = MATCH.MATCHconst;
125 return m;
126 }
127
128 /* Conversion of ptr to derived to ptr to base
129 */
130 int offset = 0;
131 if (tp.next.isBaseOf(next, &offset) && offset == 0)
132 return MATCH.MATCHconvert;
133 }
134 return MATCH.MATCHnomatch;
135 }
136
137 bool isscalar()
138 {
139 return true;
140 }
141
142 Expression defaultInit(Loc loc)
143 {
144 version (LOGDEFAULTINIT) {
145 printf("TypePointer::defaultInit() '%s'\n", toChars());
146 }
147 Expression e = new NullExp(loc);
148 e.type = this;
149 return e;
150 }
151
152 bool isZeroInit(Loc loc)
153 {
154 return true;
155 }
156
157 TypeInfoDeclaration getTypeInfoDeclaration()
158 {
159 return new TypeInfoPointerDeclaration(this);
160 }
161
162 bool hasPointers()
163 {
164 return true;
165 }
166
167 version (CPP_MANGLE) {
168 void toCppMangle(OutBuffer buf, CppMangleState* cms)
169 {
170 assert(false);
171 }
172 }
173
174 type* toCtype()
175 {
176 type* tn;
177 type* t;
178
179 //printf("TypePointer.toCtype() %s\n", toChars());
180 if (ctype)
181 return ctype;
182
183 if (1 || global.params.symdebug)
184 { /* Need to always do this, otherwise C++ name mangling
185 * goes awry.
186 */
187 t = type_alloc(TYM.TYnptr);
188 ctype = t;
189 tn = next.toCtype();
190 t.Tnext = tn;
191 tn.Tcount++;
192 }
193 else
194 t = type_fake(totym());
195
196 t.Tcount++;
197 ctype = t;
198 return t;
199 }
200 }