comparison dmd/AndAndExp.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children cab4c37afb89
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.AndAndExp;
2
3 import dmd.Expression;
4 import dmd.InterState;
5 import dmd.Loc;
6 import dmd.Scope;
7 import dmd.IRState;
8 import dmd.CommaExp;
9 import dmd.Global;
10 import dmd.BoolExp;
11 import dmd.BinExp;
12 import dmd.TOK;
13 import dmd.WANT;
14 import dmd.IntegerExp;
15 import dmd.Type;
16 import dmd.TY;
17
18 import dmd.backend.elem;
19 import dmd.backend.OPER;
20 import dmd.backend.Util;
21
22 class AndAndExp : BinExp
23 {
24 this(Loc loc, Expression e1, Expression e2)
25 {
26 super(loc, TOK.TOKandand, AndAndExp.sizeof, e1, e2);
27 }
28
29 Expression semantic(Scope sc)
30 {
31 uint cs1;
32
33 // same as for OrOr
34 e1 = e1.semantic(sc);
35 e1 = resolveProperties(sc, e1);
36 e1 = e1.checkToPointer();
37 e1 = e1.checkToBoolean();
38 cs1 = sc.callSuper;
39
40 if (sc.flags & SCOPE.SCOPEstaticif)
41 {
42 /* If in static if, don't evaluate e2 if we don't have to.
43 */
44 e1 = e1.optimize(WANTflags);
45 if (e1.isBool(false))
46 {
47 return new IntegerExp(loc, 0, Type.tboolean);
48 }
49 }
50
51 e2 = e2.semantic(sc);
52 sc.mergeCallSuper(loc, cs1);
53 e2 = resolveProperties(sc, e2);
54 e2 = e2.checkToPointer();
55
56 type = Type.tboolean;
57 if (e2.type.ty == Tvoid)
58 type = Type.tvoid;
59 if (e2.op == TOKtype || e2.op == TOKimport)
60 error("%s is not an expression", e2.toChars());
61 return this;
62 }
63
64 Expression checkToBoolean()
65 {
66 e2 = e2.checkToBoolean();
67 return this;
68 }
69
70 int isBit()
71 {
72 assert(false);
73 }
74
75 Expression optimize(int result)
76 {
77 //printf("AndAndExp::optimize(%d) %s\n", result, toChars());
78 e1 = e1.optimize(WANTflags | (result & WANTinterpret));
79 Expression e = this;
80 if (e1.isBool(false))
81 {
82 e = new CommaExp(loc, e1, new IntegerExp(loc, 0, type));
83 e.type = type;
84 e = e.optimize(result);
85 }
86 else
87 {
88 e2 = e2.optimize(WANTflags | (result & WANTinterpret));
89 if (result && e2.type.toBasetype().ty == Tvoid && !global.errors)
90 error("void has no value");
91
92 if (e1.isConst())
93 {
94 if (e2.isConst())
95 {
96 int n1 = e1.isBool(1);
97 int n2 = e2.isBool(1);
98
99 e = new IntegerExp(loc, n1 && n2, type);
100 }
101 else if (e1.isBool(true))
102 e = new BoolExp(loc, e2, type);
103 }
104 }
105 return e;
106 }
107
108 Expression interpret(InterState* istate)
109 {
110 assert(false);
111 }
112
113 bool checkSideEffect(int flag)
114 {
115 assert(false);
116 }
117
118 elem* toElem(IRState* irs)
119 {
120 elem* e = toElemBin(irs, OPandand);
121 if (global.params.cov && e2.loc.linnum)
122 e.E2() = el_combine(incUsageElem(irs, e2.loc), e.E2);
123 return e;
124 }
125 }
126