0
|
1 module dmd.WithStatement;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Statement;
|
|
5 import dmd.Expression;
|
|
6 import dmd.VarDeclaration;
|
|
7 import dmd.Loc;
|
|
8 import dmd.OutBuffer;
|
129
|
9 import dmd.ScopeDsymbol;
|
|
10 import dmd.TypeExp;
|
|
11 import dmd.TOK;
|
|
12 import dmd.Initializer;
|
|
13 import dmd.ExpInitializer;
|
|
14 import dmd.Id;
|
|
15 import dmd.ScopeExp;
|
|
16 import dmd.WithScopeSymbol;
|
|
17 import dmd.TY;
|
|
18 import dmd.Type;
|
0
|
19 import dmd.HdrGenState;
|
|
20 import dmd.InlineScanState;
|
|
21 import dmd.IRState;
|
|
22 import dmd.Scope;
|
|
23 import dmd.BE;
|
|
24
|
129
|
25 import dmd.backend.Symbol;
|
|
26 import dmd.backend.block;
|
|
27 import dmd.backend.Blockx;
|
|
28 import dmd.backend.elem;
|
|
29 import dmd.backend.Util;
|
|
30 import dmd.backend.OPER;
|
|
31
|
0
|
32 class WithStatement : Statement
|
|
33 {
|
|
34 Expression exp;
|
|
35 Statement body_;
|
|
36 VarDeclaration wthis;
|
|
37
|
|
38 this(Loc loc, Expression exp, Statement body_)
|
|
39 {
|
|
40 super(loc);
|
49
|
41 this.exp = exp;
|
|
42 this.body_ = body_;
|
|
43 wthis = null;
|
0
|
44 }
|
|
45
|
72
|
46 override Statement syntaxCopy()
|
0
|
47 {
|
129
|
48 WithStatement s = new WithStatement(loc, exp.syntaxCopy(), body_ ? body_.syntaxCopy() : null);
|
|
49 return s;
|
0
|
50 }
|
|
51
|
72
|
52 override Statement semantic(Scope sc)
|
0
|
53 {
|
129
|
54 ScopeDsymbol sym;
|
|
55 Initializer init;
|
|
56
|
|
57 //printf("WithStatement.semantic()\n");
|
|
58 exp = exp.semantic(sc);
|
|
59 exp = resolveProperties(sc, exp);
|
|
60 if (exp.op == TOKimport)
|
|
61 {
|
|
62 ScopeExp es = cast(ScopeExp)exp;
|
|
63
|
|
64 sym = es.sds;
|
|
65 }
|
|
66 else if (exp.op == TOKtype)
|
|
67 {
|
|
68 TypeExp es = cast(TypeExp)exp;
|
|
69
|
|
70 sym = es.type.toDsymbol(sc).isScopeDsymbol();
|
|
71 if (!sym)
|
|
72 {
|
|
73 error("%s has no members", es.toChars());
|
|
74 body_ = body_.semantic(sc);
|
|
75 return this;
|
|
76 }
|
|
77 }
|
|
78 else
|
|
79 {
|
|
80 Type t = exp.type;
|
|
81
|
|
82 assert(t);
|
|
83 t = t.toBasetype();
|
|
84 if (t.isClassHandle())
|
|
85 {
|
|
86 init = new ExpInitializer(loc, exp);
|
|
87 wthis = new VarDeclaration(loc, exp.type, Id.withSym, init);
|
|
88 wthis.semantic(sc);
|
|
89
|
|
90 sym = new WithScopeSymbol(this);
|
|
91 sym.parent = sc.scopesym;
|
|
92 }
|
|
93 else if (t.ty == Tstruct)
|
|
94 {
|
|
95 Expression e = exp.addressOf(sc);
|
|
96 init = new ExpInitializer(loc, e);
|
|
97 wthis = new VarDeclaration(loc, e.type, Id.withSym, init);
|
|
98 wthis.semantic(sc);
|
|
99 sym = new WithScopeSymbol(this);
|
|
100 sym.parent = sc.scopesym;
|
|
101 }
|
|
102 else
|
|
103 {
|
|
104 error("with expressions must be class objects, not '%s'", exp.type.toChars());
|
|
105 return null;
|
|
106 }
|
|
107 }
|
|
108 sc = sc.push(sym);
|
|
109
|
|
110 if (body_)
|
|
111 body_ = body_.semantic(sc);
|
|
112
|
|
113 sc.pop();
|
|
114
|
|
115 return this;
|
0
|
116 }
|
|
117
|
72
|
118 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
119 {
|
|
120 assert(false);
|
|
121 }
|
|
122
|
72
|
123 override bool usesEH()
|
0
|
124 {
|
|
125 assert(false);
|
|
126 }
|
|
127
|
72
|
128 override BE blockExit()
|
0
|
129 {
|
129
|
130 BE result = BEnone;
|
|
131 if (exp.canThrow())
|
|
132 result = BEthrow;
|
|
133 if (body_)
|
|
134 result |= body_.blockExit();
|
|
135 else
|
|
136 result |= BEfallthru;
|
|
137 return result;
|
0
|
138 }
|
|
139
|
72
|
140 override Statement inlineScan(InlineScanState* iss)
|
0
|
141 {
|
|
142 assert(false);
|
|
143 }
|
|
144
|
72
|
145 override void toIR(IRState* irs)
|
0
|
146 {
|
129
|
147 Symbol* sp;
|
|
148 elem* e;
|
|
149 elem* ei;
|
|
150 ExpInitializer ie;
|
|
151 Blockx* blx = irs.blx;
|
|
152
|
|
153 //printf("WithStatement.toIR()\n");
|
|
154 if (exp.op == TOKimport || exp.op == TOKtype)
|
|
155 {
|
|
156 }
|
|
157 else
|
|
158 {
|
|
159 // Declare with handle
|
|
160 sp = wthis.toSymbol();
|
|
161 symbol_add(sp);
|
|
162
|
|
163 // Perform initialization of with handle
|
|
164 ie = wthis.init.isExpInitializer();
|
|
165 assert(ie);
|
|
166 ei = ie.exp.toElem(irs);
|
|
167 e = el_var(sp);
|
|
168 e = el_bin(OPeq,e.Ety, e, ei);
|
|
169 elem_setLoc(e, loc);
|
|
170 incUsage(irs, loc);
|
|
171 block_appendexp(blx.curblock,e);
|
|
172 }
|
|
173 // Execute with block
|
|
174 if (body_)
|
|
175 body_.toIR(irs);
|
0
|
176 }
|
72
|
177 }
|