annotate dmd/GotoStatement.d @ 63:cab4c37afb89

A bunch of implementations
author korDen
date Mon, 23 Aug 2010 16:52:24 +0400
parents 10317f0c89a5
children 2e2a5c3f943a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.GotoStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.CompoundStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.LabelDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.TryFinallyStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.codegen.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 class GotoStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 Identifier ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 LabelDsymbol label = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 TryFinallyStatement tf = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 this(Loc loc, Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 this.ident = ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 GotoStatement s = new GotoStatement(loc, ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 FuncDeclaration fd = sc.parent.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 //printf("GotoStatement.semantic()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 tf = sc.tf;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 label = fd.searchLabel(ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 if (!label.statement && sc.fes)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 /* Either the goto label is forward referenced or it
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 * is in the function that the enclosing foreach is in.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 * Can't know yet, so wrap the goto in a compound statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 * so we can patch it later, and add it to a 'look at this later'
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 * list.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 Statements a = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 Statement s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 a.push(cast(void*)this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 s = new CompoundStatement(loc, a);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 sc.fes.gotos.push(cast(void*)s); // 'look at this later' list
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if (label.statement && label.statement.tf != sc.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 error("cannot goto in or out of finally block");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 //printf("GotoStatement.blockExit(%p)\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 return BE.BEgoto;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
78 Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 void toIR(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 block* b;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 block* bdest;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 if (!label.statement)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 error("label %s is undefined", label.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 if (tf !is label.statement.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 error("cannot goto forward out of or into finally block");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 bdest = labelToBlock(loc, blx, label);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 if (!bdest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 b = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 // Adjust exception handler scope index if in different try blocks
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 if (b.Btry != bdest.Btry)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 // Check that bdest is in an enclosing try block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 for (block* bt = b.Btry; bt != bdest.Btry; bt = bt.Btry)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 if (!bt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 //printf("b.Btry = %p, bdest.Btry = %p\n", b.Btry, bdest.Btry);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 error("cannot goto into try block");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 //setScopeIndex(blx, b, bdest.Btry ? bdest.Btry.Bscope_index : -1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 list_append(&b.Bsucc,bdest);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 block_next(blx,BCgoto,null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 buf.writestring("goto ");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 buf.writestring(ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 buf.writebyte(';');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 buf.writenl();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 GotoStatement isGotoStatement() { return this; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }