annotate dmd/PtrExp.d @ 178:e3afd1303184

Many small bugs fixed Made all classes derive from TObject to detect memory leaks (functionality is disabled for now) Began work on overriding backend memory allocations (to avoid memory leaks)
author korDen
date Sun, 17 Oct 2010 07:42:00 +0400
parents af724d3510d7
children b0d41ff5e0df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
1 module dmd.PtrExp;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
4 import dmd.Expression;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
5 import dmd.Identifier;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
6 import dmd.backend.elem;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
7 import dmd.UnaExp;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
8 import dmd.InterState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
9 import dmd.Type;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
10 import dmd.OutBuffer;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
11 import dmd.Loc;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
12 import dmd.Scope;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
13 import dmd.IRState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
14 import dmd.HdrGenState;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.AddrExp;
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
19 import dmd.PREC;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.StructLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.TypePointer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.TypeArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.ErrorExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.expression.Ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.backend.mTY;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
32 import dmd.backend.OPER;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
33
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 class PtrExp : UnaExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 this(Loc loc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
38 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 super(loc, TOK.TOKstar, PtrExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 // if (e.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 // type = ((TypePointer *)e.type).next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 this(Loc loc, Expression e, Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
46 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 super(loc, TOKstar, PtrExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
51 override Expression semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 printf("PtrExp::semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 UnaExp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 e1 = resolveProperties(sc, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 if (!e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 writef("PtrExp.semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 Expression e = op_overload(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 Type tb = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 switch (tb.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 case Tpointer:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 type = (cast(TypePointer)tb).next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 case Tsarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 case Tarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 type = (cast(TypeArray)tb).next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 e1 = e1.castTo(sc, type.pointerTo());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 error("can only * a pointer, not a '%s'", e1.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 return new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 rvalue();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
87 override int isLvalue()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
61
4ae0d790a452 OnScopeStatement.syntaxCopy
korDen
parents: 0
diff changeset
89 return 1;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 }
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
91
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
92 override void checkEscapeRef()
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
93 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
94 e1.checkEscape();
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
95 }
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
96
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
97 override Expression toLvalue(Scope sc, Expression e)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 tym = tybasic(e1.ET.Tty);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 if (!(tyscalar(tym) ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 tym == TYstruct ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 tym == TYarray && e.Eoper == TOKaddr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 synerr(EM_lvalue); // lvalue expected
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 version (DMDV2) {
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
111 override Expression modifiableLvalue(Scope sc, Expression e)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 //printf("PtrExp.modifiableLvalue() %s, type %s\n", toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 if (e1.op == TOKsymoff)
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
116 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 SymOffExp se = cast(SymOffExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 se.var.checkModify(loc, sc, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 //return toLvalue(sc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 return Expression.modifiableLvalue(sc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
125 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
127 buf.writeByte('*');
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
128 expToCBuffer(buf, hgs, e1, precedence[op]);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
131 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 //printf("PtrExp::toElem() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 e = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 e = el_una(OPER.OPind, type.totym(), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 if (tybasic(e.Ety) == TYM.TYstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 e.Enumbytes = cast(uint)type.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 el_setLoc(e,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
148 override Expression optimize(int result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 //printf("PtrExp.optimize(result = x%x) %s\n", result, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 // Convert *&ex to ex
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 if (e1.op == TOK.TOKaddress)
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
154 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 Expression ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 ex = (cast(AddrExp)e1).e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 if (type.equals(ex.type))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 e = ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 e = ex.copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 // Constant fold *(&structliteral + offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 if (e1.op == TOK.TOKadd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 e = Ptr(type, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 if (e !is EXP_CANT_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 if (e1.op == TOK.TOKsymoff)
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
178 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 SymOffExp se = cast(SymOffExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 VarDeclaration v = se.var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 Expression e = expandVar(result, v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 if (e && e.op == TOK.TOKstructliteral)
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 135
diff changeset
183 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 StructLiteralExp sle = cast(StructLiteralExp)e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 e = sle.getField(type, se.offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 if (e && e !is EXP_CANT_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
193 override Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
198 override Identifier opId()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203