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
|
0
|
34 class PtrExp : UnaExp
|
|
35 {
|
|
36 this(Loc loc, Expression e)
|
|
37 {
|
178
|
38 register();
|
0
|
39 super(loc, TOK.TOKstar, PtrExp.sizeof, e);
|
|
40 // if (e.type)
|
|
41 // type = ((TypePointer *)e.type).next;
|
|
42 }
|
|
43
|
|
44 this(Loc loc, Expression e, Type t)
|
|
45 {
|
178
|
46 register();
|
0
|
47 super(loc, TOKstar, PtrExp.sizeof, e);
|
|
48 type = t;
|
|
49 }
|
|
50
|
72
|
51 override Expression semantic(Scope sc)
|
0
|
52 {
|
|
53 version (LOGSEMANTIC) {
|
|
54 printf("PtrExp::semantic('%s')\n", toChars());
|
|
55 }
|
|
56 if (!type)
|
|
57 {
|
|
58 UnaExp.semantic(sc);
|
|
59 e1 = resolveProperties(sc, e1);
|
|
60 if (!e1.type)
|
|
61 writef("PtrExp.semantic('%s')\n", toChars());
|
|
62 Expression e = op_overload(sc);
|
|
63 if (e)
|
|
64 return e;
|
|
65 Type tb = e1.type.toBasetype();
|
|
66 switch (tb.ty)
|
|
67 {
|
|
68 case Tpointer:
|
|
69 type = (cast(TypePointer)tb).next;
|
|
70 break;
|
|
71
|
|
72 case Tsarray:
|
|
73 case Tarray:
|
|
74 type = (cast(TypeArray)tb).next;
|
|
75 e1 = e1.castTo(sc, type.pointerTo());
|
|
76 break;
|
|
77
|
|
78 default:
|
|
79 error("can only * a pointer, not a '%s'", e1.type.toChars());
|
|
80 return new ErrorExp();
|
|
81 }
|
|
82 rvalue();
|
|
83 }
|
|
84 return this;
|
|
85 }
|
|
86
|
72
|
87 override int isLvalue()
|
0
|
88 {
|
61
|
89 return 1;
|
0
|
90 }
|
174
|
91
|
135
|
92 override void checkEscapeRef()
|
|
93 {
|
|
94 e1.checkEscape();
|
|
95 }
|
174
|
96
|
72
|
97 override Expression toLvalue(Scope sc, Expression e)
|
0
|
98 {
|
|
99 static if (false) {
|
|
100 tym = tybasic(e1.ET.Tty);
|
|
101 if (!(tyscalar(tym) ||
|
|
102 tym == TYstruct ||
|
|
103 tym == TYarray && e.Eoper == TOKaddr)
|
|
104 )
|
|
105 synerr(EM_lvalue); // lvalue expected
|
|
106 }
|
|
107 return this;
|
|
108 }
|
|
109
|
|
110 version (DMDV2) {
|
72
|
111 override Expression modifiableLvalue(Scope sc, Expression e)
|
0
|
112 {
|
|
113 //printf("PtrExp.modifiableLvalue() %s, type %s\n", toChars(), type.toChars());
|
|
114
|
|
115 if (e1.op == TOKsymoff)
|
174
|
116 {
|
0
|
117 SymOffExp se = cast(SymOffExp)e1;
|
|
118 se.var.checkModify(loc, sc, type);
|
|
119 //return toLvalue(sc, e);
|
|
120 }
|
|
121
|
|
122 return Expression.modifiableLvalue(sc, e);
|
|
123 }
|
|
124 }
|
72
|
125 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
126 {
|
174
|
127 buf.writeByte('*');
|
|
128 expToCBuffer(buf, hgs, e1, precedence[op]);
|
0
|
129 }
|
|
130
|
72
|
131 override elem* toElem(IRState* irs)
|
0
|
132 {
|
|
133 elem* e;
|
|
134
|
|
135 //printf("PtrExp::toElem() %s\n", toChars());
|
|
136 e = e1.toElem(irs);
|
|
137 e = el_una(OPER.OPind, type.totym(), e);
|
|
138
|
|
139 if (tybasic(e.Ety) == TYM.TYstruct)
|
|
140 {
|
|
141 e.Enumbytes = cast(uint)type.size();
|
|
142 }
|
|
143
|
|
144 el_setLoc(e,loc);
|
|
145 return e;
|
|
146 }
|
|
147
|
72
|
148 override Expression optimize(int result)
|
0
|
149 {
|
|
150 //printf("PtrExp.optimize(result = x%x) %s\n", result, toChars());
|
|
151 e1 = e1.optimize(result);
|
|
152 // Convert *&ex to ex
|
|
153 if (e1.op == TOK.TOKaddress)
|
174
|
154 {
|
0
|
155 Expression e;
|
|
156 Expression ex;
|
|
157
|
|
158 ex = (cast(AddrExp)e1).e1;
|
|
159 if (type.equals(ex.type))
|
|
160 e = ex;
|
|
161 else
|
|
162 {
|
|
163 e = ex.copy();
|
|
164 e.type = type;
|
|
165 }
|
|
166 return e;
|
|
167 }
|
|
168 // Constant fold *(&structliteral + offset)
|
|
169 if (e1.op == TOK.TOKadd)
|
|
170 {
|
|
171 Expression e;
|
|
172 e = Ptr(type, e1);
|
|
173 if (e !is EXP_CANT_INTERPRET)
|
|
174 return e;
|
|
175 }
|
|
176
|
|
177 if (e1.op == TOK.TOKsymoff)
|
174
|
178 {
|
0
|
179 SymOffExp se = cast(SymOffExp)e1;
|
|
180 VarDeclaration v = se.var.isVarDeclaration();
|
|
181 Expression e = expandVar(result, v);
|
|
182 if (e && e.op == TOK.TOKstructliteral)
|
174
|
183 {
|
0
|
184 StructLiteralExp sle = cast(StructLiteralExp)e;
|
|
185 e = sle.getField(type, se.offset);
|
|
186 if (e && e !is EXP_CANT_INTERPRET)
|
|
187 return e;
|
|
188 }
|
|
189 }
|
|
190 return this;
|
|
191 }
|
|
192
|
72
|
193 override Expression interpret(InterState istate)
|
0
|
194 {
|
|
195 assert(false);
|
|
196 }
|
|
197
|
72
|
198 override Identifier opId()
|
0
|
199 {
|
|
200 assert(false);
|
|
201 }
|
|
202 }
|
|
203
|