annotate dmd/Catch.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.Catch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 63
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.TypeIdentifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
18 import dmd.TObject;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
19
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
20 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
21
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
22 class Catch : TObject
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 {
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
24 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
25
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 Loc loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 Type type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 Identifier ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 VarDeclaration var = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 Statement handler;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 this(Loc loc, Type t, Identifier id, Statement handler)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
34 register();
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
35
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 //printf("Catch(%s, loc = %s)\n", id.toChars(), loc.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 this.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 this.ident = id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 this.handler = handler;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 Catch syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 {
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
45 Catch c = new Catch(loc, (type ? type.syntaxCopy() : null), ident, (handler ? handler.syntaxCopy() : null));
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
46 return c;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 void semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 ScopeDsymbol sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 //printf("Catch.semantic(%s)\n", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 version (IN_GCC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 if (sc.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 /* This is because the _d_local_unwind() gets the stack munged
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 * up on this. The workaround is to place any try-catches into
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 * a separate function, and call that.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 * To fix, have the compiler automatically convert the finally
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 * body into a nested function.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 error(loc, "cannot put catch statement inside finally block");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 sym = new ScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 sym.parent = sc.scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 sc = sc.push(sym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 type = new TypeIdentifier(Loc(0), Id.Object_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (!type.toBasetype().isClassHandle())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 error("can only catch class objects, not '%s'", type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 else if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 var = new VarDeclaration(loc, type, ident, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 var.parent = sc.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 sc.insert(var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 handler = handler.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 return handler ? handler.blockExit() : BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
96 buf.writestring("catch");
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
97 if (type)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
98 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
99 buf.writebyte('(');
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
100 type.toCBuffer(buf, ident, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
101 buf.writebyte(')');
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
102 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
103 buf.writenl();
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
104 buf.writebyte('{');
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
105 buf.writenl();
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
106 if (handler)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
107 handler.toCBuffer(buf, hgs);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
108 buf.writebyte('}');
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
109 buf.writenl();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }