Mercurial > projects > ddmd
annotate dmd/PtrExp.d @ 191:52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
Also changed Array allocation policy:
Now doesn't reallocate but malloc's, followed by a memcpy (no free).
(this fixes a crash while compiling druntime. Same bug in dmd)
author | korDen@korDen-pc |
---|---|
date | Sun, 25 Mar 2012 03:11:12 +0400 |
parents | b0d41ff5e0df |
children |
rev | line source |
---|---|
72 | 1 module dmd.PtrExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.Identifier; | |
6 import dmd.backend.elem; | |
7 import dmd.UnaExp; | |
8 import dmd.InterState; | |
9 import dmd.Type; | |
10 import dmd.OutBuffer; | |
11 import dmd.Loc; | |
12 import dmd.Scope; | |
13 import dmd.IRState; | |
14 import dmd.HdrGenState; | |
0 | 15 import dmd.TOK; |
16 import dmd.GlobalExpressions; | |
17 import dmd.SymOffExp; | |
18 import dmd.AddrExp; | |
174 | 19 import dmd.PREC; |
0 | 20 import dmd.VarDeclaration; |
21 import dmd.StructLiteralExp; | |
22 import dmd.TypePointer; | |
23 import dmd.TypeArray; | |
24 import dmd.ErrorExp; | |
25 import dmd.TY; | |
26 import dmd.expression.Ptr; | |
27 import dmd.expression.Util; | |
28 | |
29 import dmd.backend.Util; | |
30 import dmd.backend.TYM; | |
31 import dmd.backend.mTY; | |
72 | 32 import dmd.backend.OPER; |
33 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
34 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
35 |
0 | 36 class PtrExp : UnaExp |
37 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
38 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
39 |
0 | 40 this(Loc loc, Expression e) |
41 { | |
178 | 42 register(); |
0 | 43 super(loc, TOK.TOKstar, PtrExp.sizeof, e); |
44 // if (e.type) | |
45 // type = ((TypePointer *)e.type).next; | |
46 } | |
47 | |
48 this(Loc loc, Expression e, Type t) | |
49 { | |
178 | 50 register(); |
0 | 51 super(loc, TOKstar, PtrExp.sizeof, e); |
52 type = t; | |
53 } | |
54 | |
72 | 55 override Expression semantic(Scope sc) |
0 | 56 { |
57 version (LOGSEMANTIC) { | |
58 printf("PtrExp::semantic('%s')\n", toChars()); | |
59 } | |
60 if (!type) | |
61 { | |
62 UnaExp.semantic(sc); | |
63 e1 = resolveProperties(sc, e1); | |
64 if (!e1.type) | |
65 writef("PtrExp.semantic('%s')\n", toChars()); | |
66 Expression e = op_overload(sc); | |
67 if (e) | |
68 return e; | |
69 Type tb = e1.type.toBasetype(); | |
70 switch (tb.ty) | |
71 { | |
72 case Tpointer: | |
73 type = (cast(TypePointer)tb).next; | |
74 break; | |
75 | |
76 case Tsarray: | |
77 case Tarray: | |
78 type = (cast(TypeArray)tb).next; | |
79 e1 = e1.castTo(sc, type.pointerTo()); | |
80 break; | |
81 | |
82 default: | |
83 error("can only * a pointer, not a '%s'", e1.type.toChars()); | |
84 return new ErrorExp(); | |
85 } | |
86 rvalue(); | |
87 } | |
88 return this; | |
89 } | |
90 | |
191
52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
korDen@korDen-pc
parents:
187
diff
changeset
|
91 override bool isLvalue() |
0 | 92 { |
191
52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
korDen@korDen-pc
parents:
187
diff
changeset
|
93 return true; |
0 | 94 } |
174 | 95 |
135 | 96 override void checkEscapeRef() |
97 { | |
98 e1.checkEscape(); | |
99 } | |
174 | 100 |
72 | 101 override Expression toLvalue(Scope sc, Expression e) |
0 | 102 { |
103 static if (false) { | |
104 tym = tybasic(e1.ET.Tty); | |
105 if (!(tyscalar(tym) || | |
106 tym == TYstruct || | |
107 tym == TYarray && e.Eoper == TOKaddr) | |
108 ) | |
109 synerr(EM_lvalue); // lvalue expected | |
110 } | |
111 return this; | |
112 } | |
113 | |
114 version (DMDV2) { | |
72 | 115 override Expression modifiableLvalue(Scope sc, Expression e) |
0 | 116 { |
117 //printf("PtrExp.modifiableLvalue() %s, type %s\n", toChars(), type.toChars()); | |
118 | |
119 if (e1.op == TOKsymoff) | |
174 | 120 { |
0 | 121 SymOffExp se = cast(SymOffExp)e1; |
122 se.var.checkModify(loc, sc, type); | |
123 //return toLvalue(sc, e); | |
124 } | |
125 | |
126 return Expression.modifiableLvalue(sc, e); | |
127 } | |
128 } | |
72 | 129 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 130 { |
174 | 131 buf.writeByte('*'); |
132 expToCBuffer(buf, hgs, e1, precedence[op]); | |
0 | 133 } |
134 | |
72 | 135 override elem* toElem(IRState* irs) |
0 | 136 { |
137 elem* e; | |
138 | |
139 //printf("PtrExp::toElem() %s\n", toChars()); | |
140 e = e1.toElem(irs); | |
141 e = el_una(OPER.OPind, type.totym(), e); | |
142 | |
143 if (tybasic(e.Ety) == TYM.TYstruct) | |
144 { | |
145 e.Enumbytes = cast(uint)type.size(); | |
146 } | |
147 | |
148 el_setLoc(e,loc); | |
149 return e; | |
150 } | |
151 | |
72 | 152 override Expression optimize(int result) |
0 | 153 { |
154 //printf("PtrExp.optimize(result = x%x) %s\n", result, toChars()); | |
155 e1 = e1.optimize(result); | |
156 // Convert *&ex to ex | |
157 if (e1.op == TOK.TOKaddress) | |
174 | 158 { |
0 | 159 Expression e; |
160 Expression ex; | |
161 | |
162 ex = (cast(AddrExp)e1).e1; | |
163 if (type.equals(ex.type)) | |
164 e = ex; | |
165 else | |
166 { | |
167 e = ex.copy(); | |
168 e.type = type; | |
169 } | |
170 return e; | |
171 } | |
172 // Constant fold *(&structliteral + offset) | |
173 if (e1.op == TOK.TOKadd) | |
174 { | |
175 Expression e; | |
176 e = Ptr(type, e1); | |
177 if (e !is EXP_CANT_INTERPRET) | |
178 return e; | |
179 } | |
180 | |
181 if (e1.op == TOK.TOKsymoff) | |
174 | 182 { |
0 | 183 SymOffExp se = cast(SymOffExp)e1; |
184 VarDeclaration v = se.var.isVarDeclaration(); | |
185 Expression e = expandVar(result, v); | |
186 if (e && e.op == TOK.TOKstructliteral) | |
174 | 187 { |
0 | 188 StructLiteralExp sle = cast(StructLiteralExp)e; |
189 e = sle.getField(type, se.offset); | |
190 if (e && e !is EXP_CANT_INTERPRET) | |
191 return e; | |
192 } | |
193 } | |
194 return this; | |
195 } | |
196 | |
72 | 197 override Expression interpret(InterState istate) |
0 | 198 { |
199 assert(false); | |
200 } | |
201 | |
72 | 202 override Identifier opId() |
0 | 203 { |
204 assert(false); | |
205 } | |
206 } | |
207 |