Mercurial > projects > ddmd
annotate dmd/TryFinallyStatement.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | e3afd1303184 |
children |
rev | line source |
---|---|
0 | 1 module dmd.TryFinallyStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
5 import dmd.Loc; | |
6 import dmd.OutBuffer; | |
7 import dmd.HdrGenState; | |
8 import dmd.Scope; | |
9 import dmd.InlineScanState; | |
10 import dmd.CompoundStatement; | |
11 import dmd.IRState; | |
12 import dmd.BE; | |
13 | |
14 import dmd.backend.block; | |
15 import dmd.backend.Blockx; | |
16 import dmd.backend.BC; | |
17 import dmd.backend.Util; | |
18 | |
19 import dmd.codegen.Util; | |
20 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
21 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
22 |
0 | 23 class TryFinallyStatement : Statement |
24 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
25 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
26 |
0 | 27 Statement body_; |
28 Statement finalbody; | |
29 | |
30 this(Loc loc, Statement body_, Statement finalbody) | |
31 { | |
178 | 32 register(); |
0 | 33 super(loc); |
34 this.body_ = body_; | |
35 this.finalbody = finalbody; | |
36 } | |
37 | |
72 | 38 override Statement syntaxCopy() |
0 | 39 { |
40 assert(false); | |
41 } | |
42 | |
72 | 43 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 44 { |
174 | 45 buf.printf("try\n{\n"); |
46 body_.toCBuffer(buf, hgs); | |
47 buf.printf("}\nfinally\n{\n"); | |
48 finalbody.toCBuffer(buf, hgs); | |
49 buf.writeByte('}'); | |
50 buf.writenl(); | |
0 | 51 } |
52 | |
72 | 53 override Statement semantic(Scope sc) |
0 | 54 { |
55 //printf("TryFinallyStatement::semantic()\n"); | |
56 body_ = body_.semantic(sc); | |
57 sc = sc.push(); | |
58 sc.tf = this; | |
59 sc.sbreak = null; | |
60 sc.scontinue = null; // no break or continue out of finally block | |
61 finalbody = finalbody.semantic(sc); | |
62 sc.pop(); | |
63 if (!body_) | |
64 return finalbody; | |
65 if (!finalbody) | |
66 return body_; | |
67 if (body_.blockExit() == BE.BEfallthru) | |
68 { | |
69 Statement s = new CompoundStatement(loc, body_, finalbody); | |
70 return s; | |
71 } | |
72 return this; | |
73 } | |
74 | |
72 | 75 override bool hasBreak() |
0 | 76 { |
77 assert(false); | |
78 } | |
79 | |
72 | 80 override bool hasContinue() |
0 | 81 { |
82 assert(false); | |
83 } | |
84 | |
72 | 85 override bool usesEH() |
0 | 86 { |
87 assert(false); | |
88 } | |
89 | |
72 | 90 override BE blockExit() |
0 | 91 { |
92 if (body_) | |
93 return body_.blockExit(); | |
94 return BE.BEfallthru; | |
95 } | |
96 | |
72 | 97 override Statement inlineScan(InlineScanState* iss) |
0 | 98 { |
99 if (body_) | |
100 body_ = body_.inlineScan(iss); | |
101 if (finalbody) | |
102 finalbody = finalbody.inlineScan(iss); | |
103 return this; | |
104 } | |
105 | |
106 /**************************************** | |
107 * A try-finally statement. | |
108 * Builds the following: | |
109 * _try | |
110 * block | |
111 * _finally | |
112 * finalbody | |
113 * _ret | |
114 */ | |
72 | 115 override void toIR(IRState* irs) |
0 | 116 { |
117 //printf("TryFinallyStatement.toIR()\n"); | |
118 Blockx* blx = irs.blx; | |
119 | |
120 version (SEH) { | |
121 nteh_declarvars(blx); | |
122 } | |
123 | |
124 block* tryblock = block_goto(blx, BCgoto, null); | |
125 | |
126 int previndex = blx.scope_index; | |
127 tryblock.Blast_index = previndex; | |
128 tryblock.Bscope_index = blx.next_index++; | |
129 blx.scope_index = tryblock.Bscope_index; | |
130 | |
131 // Current scope index | |
132 setScopeIndex(blx,tryblock,tryblock.Bscope_index); | |
133 | |
134 blx.tryblock = tryblock; | |
135 block_goto(blx,BC_try,null); | |
136 | |
137 IRState bodyirs = IRState(irs, this); | |
138 block* breakblock = block_calloc(blx); | |
139 block* contblock = block_calloc(blx); | |
140 | |
141 if (body_) | |
142 body_.toIR(&bodyirs); | |
143 blx.tryblock = tryblock.Btry; // back to previous tryblock | |
144 | |
145 setScopeIndex(blx,blx.curblock,previndex); | |
146 blx.scope_index = previndex; | |
147 | |
148 block_goto(blx,BCgoto, breakblock); | |
149 block* finallyblock = block_goto(blx,BCgoto,contblock); | |
150 | |
151 list_append(&tryblock.Bsucc,finallyblock); | |
152 | |
153 block_goto(blx,BC_finally,null); | |
154 | |
155 IRState finallyState = IRState(irs, this); | |
156 breakblock = block_calloc(blx); | |
157 contblock = block_calloc(blx); | |
158 | |
159 setScopeIndex(blx, blx.curblock, previndex); | |
160 if (finalbody) | |
161 finalbody.toIR(&finallyState); | |
162 block_goto(blx, BCgoto, contblock); | |
163 block_goto(blx, BCgoto, breakblock); | |
164 | |
165 block* retblock = blx.curblock; | |
166 block_next(blx,BC_ret,null); | |
167 | |
168 list_append(&finallyblock.Bsucc, blx.curblock); | |
169 list_append(&retblock.Bsucc, blx.curblock); | |
170 } | |
72 | 171 } |