0
|
1 module dmd.Catch;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Loc;
|
|
5 import dmd.Type;
|
|
6 import dmd.Scope;
|
|
7 import dmd.Identifier;
|
|
8 import dmd.VarDeclaration;
|
|
9 import dmd.Statement;
|
|
10 import dmd.OutBuffer;
|
|
11 import dmd.Id;
|
|
12 import dmd.TypeIdentifier;
|
|
13 import dmd.Util;
|
|
14 import dmd.ScopeDsymbol;
|
|
15 import dmd.HdrGenState;
|
|
16 import dmd.BE;
|
|
17
|
178
|
18 import dmd.TObject;
|
|
19
|
|
20 class Catch : TObject
|
0
|
21 {
|
|
22 Loc loc;
|
|
23 Type type;
|
|
24 Identifier ident;
|
|
25 VarDeclaration var = null;
|
|
26 Statement handler;
|
|
27
|
|
28 this(Loc loc, Type t, Identifier id, Statement handler)
|
|
29 {
|
178
|
30 register();
|
|
31
|
0
|
32 //printf("Catch(%s, loc = %s)\n", id.toChars(), loc.toChars());
|
|
33 this.loc = loc;
|
|
34 this.type = t;
|
|
35 this.ident = id;
|
|
36 this.handler = handler;
|
|
37 }
|
|
38
|
|
39 Catch syntaxCopy()
|
|
40 {
|
63
|
41 Catch c = new Catch(loc, (type ? type.syntaxCopy() : null), ident, (handler ? handler.syntaxCopy() : null));
|
|
42 return c;
|
0
|
43 }
|
|
44
|
|
45 void semantic(Scope sc)
|
|
46 {
|
|
47 ScopeDsymbol sym;
|
|
48
|
|
49 //printf("Catch.semantic(%s)\n", ident.toChars());
|
|
50
|
|
51 version (IN_GCC) {
|
|
52 } else {
|
|
53 if (sc.tf)
|
|
54 {
|
|
55 /* This is because the _d_local_unwind() gets the stack munged
|
|
56 * up on this. The workaround is to place any try-catches into
|
|
57 * a separate function, and call that.
|
|
58 * To fix, have the compiler automatically convert the finally
|
|
59 * body into a nested function.
|
|
60 */
|
|
61 error(loc, "cannot put catch statement inside finally block");
|
|
62 }
|
|
63 }
|
|
64
|
|
65 sym = new ScopeDsymbol();
|
|
66 sym.parent = sc.scopesym;
|
|
67 sc = sc.push(sym);
|
|
68
|
|
69 if (!type)
|
|
70 type = new TypeIdentifier(Loc(0), Id.Object_);
|
|
71 type = type.semantic(loc, sc);
|
|
72 if (!type.toBasetype().isClassHandle())
|
|
73 error("can only catch class objects, not '%s'", type.toChars());
|
|
74 else if (ident)
|
|
75 {
|
|
76 var = new VarDeclaration(loc, type, ident, null);
|
|
77 var.parent = sc.parent;
|
|
78 sc.insert(var);
|
|
79 }
|
|
80 handler = handler.semantic(sc);
|
|
81
|
|
82 sc.pop();
|
|
83 }
|
|
84
|
|
85 BE blockExit()
|
|
86 {
|
|
87 return handler ? handler.blockExit() : BE.BEfallthru;
|
|
88 }
|
|
89
|
|
90 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
91 {
|
174
|
92 buf.writestring("catch");
|
|
93 if (type)
|
|
94 {
|
|
95 buf.writebyte('(');
|
|
96 type.toCBuffer(buf, ident, hgs);
|
|
97 buf.writebyte(')');
|
|
98 }
|
|
99 buf.writenl();
|
|
100 buf.writebyte('{');
|
|
101 buf.writenl();
|
|
102 if (handler)
|
|
103 handler.toCBuffer(buf, hgs);
|
|
104 buf.writebyte('}');
|
|
105 buf.writenl();
|
0
|
106 }
|
|
107 } |