annotate dmd/ExpStatement.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 5c9b78899f5d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.ExpStatement;
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.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.AssertExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.DeclarationStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 class ExpStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 Expression exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 this(Loc loc, Expression exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 this.exp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Expression e = exp ? exp.syntaxCopy() : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 ExpStatement es = new ExpStatement(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 return es;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 if (exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 exp.toCBuffer(buf, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 buf.writeByte(';');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 if (!hgs.FLinit.init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 buf.writenl();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 if (exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 //printf("ExpStatement::semantic() %s\n", exp->toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 exp = resolveProperties(sc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 exp.checkSideEffect(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 exp = exp.optimize(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 if (exp.op == TOK.TOKdeclaration && !isDeclarationStatement())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 Statement s = new DeclarationStatement(loc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 //exp = exp.optimize(isDeclarationStatement() ? WANT.WANTvalue : 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 Expression interpret(InterState* istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 assert(false);
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 BE result = BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 if (exp.op == TOK.TOKhalt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 return BE.BEhalt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 if (exp.op == TOK.TOKassert)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 AssertExp a = cast(AssertExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 if (a.e1.isBool(false)) // if it's an assert(0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 return BE.BEhalt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 if (exp.canThrow())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 result |= BE.BEthrow;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 int inlineCost(InlineCostState* ics)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 return exp ? exp.inlineCost(ics) : 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 if (exp) printf("ExpStatement.doInline() '%s'\n", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 return exp ? exp.doInline(ids) : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 printf("ExpStatement.inlineScan(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 if (exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 exp = exp.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 void toIR(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 //printf("ExpStatement.toIR(), exp = %s\n", exp ? exp.toChars() : "");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 if (exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 block_appendexp(blx.curblock, exp.toElem(irs));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 }