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 }
|
|
88
|
72
|
89 override Expression toLvalue(Scope sc, Expression e)
|
0
|
90 {
|
|
91 static if (false) {
|
|
92 tym = tybasic(e1.ET.Tty);
|
|
93 if (!(tyscalar(tym) ||
|
|
94 tym == TYstruct ||
|
|
95 tym == TYarray && e.Eoper == TOKaddr)
|
|
96 )
|
|
97 synerr(EM_lvalue); // lvalue expected
|
|
98 }
|
|
99 return this;
|
|
100 }
|
|
101
|
|
102 version (DMDV2) {
|
72
|
103 override Expression modifiableLvalue(Scope sc, Expression e)
|
0
|
104 {
|
|
105 //printf("PtrExp.modifiableLvalue() %s, type %s\n", toChars(), type.toChars());
|
|
106
|
|
107 if (e1.op == TOKsymoff)
|
|
108 {
|
|
109 SymOffExp se = cast(SymOffExp)e1;
|
|
110 se.var.checkModify(loc, sc, type);
|
|
111 //return toLvalue(sc, e);
|
|
112 }
|
|
113
|
|
114 return Expression.modifiableLvalue(sc, e);
|
|
115 }
|
|
116 }
|
72
|
117 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
118 {
|
|
119 assert(false);
|
|
120 }
|
|
121
|
72
|
122 override elem* toElem(IRState* irs)
|
0
|
123 {
|
|
124 elem* e;
|
|
125
|
|
126 //printf("PtrExp::toElem() %s\n", toChars());
|
|
127 e = e1.toElem(irs);
|
|
128 e = el_una(OPER.OPind, type.totym(), e);
|
|
129
|
|
130 if (tybasic(e.Ety) == TYM.TYstruct)
|
|
131 {
|
|
132 e.Enumbytes = cast(uint)type.size();
|
|
133 }
|
|
134
|
|
135 el_setLoc(e,loc);
|
|
136 return e;
|
|
137 }
|
|
138
|
72
|
139 override Expression optimize(int result)
|
0
|
140 {
|
|
141 //printf("PtrExp.optimize(result = x%x) %s\n", result, toChars());
|
|
142 e1 = e1.optimize(result);
|
|
143 // Convert *&ex to ex
|
|
144 if (e1.op == TOK.TOKaddress)
|
|
145 {
|
|
146 Expression e;
|
|
147 Expression ex;
|
|
148
|
|
149 ex = (cast(AddrExp)e1).e1;
|
|
150 if (type.equals(ex.type))
|
|
151 e = ex;
|
|
152 else
|
|
153 {
|
|
154 e = ex.copy();
|
|
155 e.type = type;
|
|
156 }
|
|
157 return e;
|
|
158 }
|
|
159 // Constant fold *(&structliteral + offset)
|
|
160 if (e1.op == TOK.TOKadd)
|
|
161 {
|
|
162 Expression e;
|
|
163 e = Ptr(type, e1);
|
|
164 if (e !is EXP_CANT_INTERPRET)
|
|
165 return e;
|
|
166 }
|
|
167
|
|
168 if (e1.op == TOK.TOKsymoff)
|
|
169 {
|
|
170 SymOffExp se = cast(SymOffExp)e1;
|
|
171 VarDeclaration v = se.var.isVarDeclaration();
|
|
172 Expression e = expandVar(result, v);
|
|
173 if (e && e.op == TOK.TOKstructliteral)
|
|
174 {
|
|
175 StructLiteralExp sle = cast(StructLiteralExp)e;
|
|
176 e = sle.getField(type, se.offset);
|
|
177 if (e && e !is EXP_CANT_INTERPRET)
|
|
178 return e;
|
|
179 }
|
|
180 }
|
|
181 return this;
|
|
182 }
|
|
183
|
72
|
184 override Expression interpret(InterState istate)
|
0
|
185 {
|
|
186 assert(false);
|
|
187 }
|
|
188
|
72
|
189 override Identifier opId()
|
0
|
190 {
|
|
191 assert(false);
|
|
192 }
|
|
193 }
|
|
194
|