Line data Source code
1 : //
2 : // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 : //
6 :
7 : #include "compiler/translator/UnfoldShortCircuitAST.h"
8 :
9 : namespace sh
10 : {
11 :
12 : namespace
13 : {
14 :
15 : // "x || y" is equivalent to "x ? true : y".
16 0 : TIntermTernary *UnfoldOR(TIntermTyped *x, TIntermTyped *y)
17 : {
18 0 : TConstantUnion *u = new TConstantUnion;
19 0 : u->setBConst(true);
20 : TIntermConstantUnion *trueNode = new TIntermConstantUnion(
21 0 : u, TType(EbtBool, EbpUndefined, EvqConst, 1));
22 0 : return new TIntermTernary(x, trueNode, y);
23 : }
24 :
25 : // "x && y" is equivalent to "x ? y : false".
26 0 : TIntermTernary *UnfoldAND(TIntermTyped *x, TIntermTyped *y)
27 : {
28 0 : TConstantUnion *u = new TConstantUnion;
29 0 : u->setBConst(false);
30 : TIntermConstantUnion *falseNode = new TIntermConstantUnion(
31 0 : u, TType(EbtBool, EbpUndefined, EvqConst, 1));
32 0 : return new TIntermTernary(x, y, falseNode);
33 : }
34 :
35 : } // namespace anonymous
36 :
37 0 : bool UnfoldShortCircuitAST::visitBinary(Visit visit, TIntermBinary *node)
38 : {
39 0 : TIntermTernary *replacement = nullptr;
40 :
41 0 : switch (node->getOp())
42 : {
43 : case EOpLogicalOr:
44 0 : replacement = UnfoldOR(node->getLeft(), node->getRight());
45 0 : break;
46 : case EOpLogicalAnd:
47 0 : replacement = UnfoldAND(node->getLeft(), node->getRight());
48 0 : break;
49 : default:
50 0 : break;
51 : }
52 0 : if (replacement)
53 : {
54 0 : queueReplacement(node, replacement, OriginalNode::IS_DROPPED);
55 : }
56 0 : return true;
57 : }
58 :
59 : } // namespace sh
|