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