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