annotate dmd/Catch.d @ 63:cab4c37afb89

A bunch of implementations
author korDen
date Mon, 23 Aug 2010 16:52:24 +0400
parents 10317f0c89a5
children e28b18c23469
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.TypeIdentifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 class Catch
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 Loc loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 Type type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 Identifier ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 VarDeclaration var = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 Statement handler;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 this(Loc loc, Type t, Identifier id, Statement handler)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 //printf("Catch(%s, loc = %s)\n", id.toChars(), loc.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 this.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 this.ident = id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 this.handler = handler;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Catch syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
36 Catch c = new Catch(loc, (type ? type.syntaxCopy() : null), ident, (handler ? handler.syntaxCopy() : null));
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
37 return c;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 void semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 ScopeDsymbol sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 //printf("Catch.semantic(%s)\n", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 version (IN_GCC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 if (sc.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 /* This is because the _d_local_unwind() gets the stack munged
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 * up on this. The workaround is to place any try-catches into
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 * a separate function, and call that.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 * To fix, have the compiler automatically convert the finally
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 * body into a nested function.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 error(loc, "cannot put catch statement inside finally block");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 sym = new ScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 sym.parent = sc.scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 sc = sc.push(sym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 type = new TypeIdentifier(Loc(0), Id.Object_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if (!type.toBasetype().isClassHandle())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 error("can only catch class objects, not '%s'", type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 else if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 var = new VarDeclaration(loc, type, ident, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 var.parent = sc.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 sc.insert(var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 handler = handler.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 return handler ? handler.blockExit() : BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 }