1*0a6a1f1dSLionel Sambuc //===- ThreadSafetyLogical.cpp ---------------------------------*- C++ --*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc // This file defines a representation for logical expressions with SExpr leaves
10*0a6a1f1dSLionel Sambuc // that are used as part of fact-checking capability expressions.
11*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
12*0a6a1f1dSLionel Sambuc
13*0a6a1f1dSLionel Sambuc #include "clang/Analysis/Analyses/ThreadSafetyLogical.h"
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc using namespace llvm;
16*0a6a1f1dSLionel Sambuc using namespace clang::threadSafety::lexpr;
17*0a6a1f1dSLionel Sambuc
18*0a6a1f1dSLionel Sambuc // Implication. We implement De Morgan's Laws by maintaining LNeg and RNeg
19*0a6a1f1dSLionel Sambuc // to keep track of whether LHS and RHS are negated.
implies(const LExpr * LHS,bool LNeg,const LExpr * RHS,bool RNeg)20*0a6a1f1dSLionel Sambuc static bool implies(const LExpr *LHS, bool LNeg, const LExpr *RHS, bool RNeg) {
21*0a6a1f1dSLionel Sambuc // In comments below, we write => for implication.
22*0a6a1f1dSLionel Sambuc
23*0a6a1f1dSLionel Sambuc // Calculates the logical AND implication operator.
24*0a6a1f1dSLionel Sambuc const auto LeftAndOperator = [=](const BinOp *A) {
25*0a6a1f1dSLionel Sambuc return implies(A->left(), LNeg, RHS, RNeg) &&
26*0a6a1f1dSLionel Sambuc implies(A->right(), LNeg, RHS, RNeg);
27*0a6a1f1dSLionel Sambuc };
28*0a6a1f1dSLionel Sambuc const auto RightAndOperator = [=](const BinOp *A) {
29*0a6a1f1dSLionel Sambuc return implies(LHS, LNeg, A->left(), RNeg) &&
30*0a6a1f1dSLionel Sambuc implies(LHS, LNeg, A->right(), RNeg);
31*0a6a1f1dSLionel Sambuc };
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc // Calculates the logical OR implication operator.
34*0a6a1f1dSLionel Sambuc const auto LeftOrOperator = [=](const BinOp *A) {
35*0a6a1f1dSLionel Sambuc return implies(A->left(), LNeg, RHS, RNeg) ||
36*0a6a1f1dSLionel Sambuc implies(A->right(), LNeg, RHS, RNeg);
37*0a6a1f1dSLionel Sambuc };
38*0a6a1f1dSLionel Sambuc const auto RightOrOperator = [=](const BinOp *A) {
39*0a6a1f1dSLionel Sambuc return implies(LHS, LNeg, A->left(), RNeg) ||
40*0a6a1f1dSLionel Sambuc implies(LHS, LNeg, A->right(), RNeg);
41*0a6a1f1dSLionel Sambuc };
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc // Recurse on right.
44*0a6a1f1dSLionel Sambuc switch (RHS->kind()) {
45*0a6a1f1dSLionel Sambuc case LExpr::And:
46*0a6a1f1dSLionel Sambuc // When performing right recursion:
47*0a6a1f1dSLionel Sambuc // C => A & B [if] C => A and C => B
48*0a6a1f1dSLionel Sambuc // When performing right recursion (negated):
49*0a6a1f1dSLionel Sambuc // C => !(A & B) [if] C => !A | !B [===] C => !A or C => !B
50*0a6a1f1dSLionel Sambuc return RNeg ? RightOrOperator(cast<And>(RHS))
51*0a6a1f1dSLionel Sambuc : RightAndOperator(cast<And>(RHS));
52*0a6a1f1dSLionel Sambuc case LExpr::Or:
53*0a6a1f1dSLionel Sambuc // When performing right recursion:
54*0a6a1f1dSLionel Sambuc // C => (A | B) [if] C => A or C => B
55*0a6a1f1dSLionel Sambuc // When performing right recursion (negated):
56*0a6a1f1dSLionel Sambuc // C => !(A | B) [if] C => !A & !B [===] C => !A and C => !B
57*0a6a1f1dSLionel Sambuc return RNeg ? RightAndOperator(cast<Or>(RHS))
58*0a6a1f1dSLionel Sambuc : RightOrOperator(cast<Or>(RHS));
59*0a6a1f1dSLionel Sambuc case LExpr::Not:
60*0a6a1f1dSLionel Sambuc // Note that C => !A is very different from !(C => A). It would be incorrect
61*0a6a1f1dSLionel Sambuc // to return !implies(LHS, RHS).
62*0a6a1f1dSLionel Sambuc return implies(LHS, LNeg, cast<Not>(RHS)->exp(), !RNeg);
63*0a6a1f1dSLionel Sambuc case LExpr::Terminal:
64*0a6a1f1dSLionel Sambuc // After reaching the terminal, it's time to recurse on the left.
65*0a6a1f1dSLionel Sambuc break;
66*0a6a1f1dSLionel Sambuc }
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc // RHS is now a terminal. Recurse on Left.
69*0a6a1f1dSLionel Sambuc switch (LHS->kind()) {
70*0a6a1f1dSLionel Sambuc case LExpr::And:
71*0a6a1f1dSLionel Sambuc // When performing left recursion:
72*0a6a1f1dSLionel Sambuc // A & B => C [if] A => C or B => C
73*0a6a1f1dSLionel Sambuc // When performing left recursion (negated):
74*0a6a1f1dSLionel Sambuc // !(A & B) => C [if] !A | !B => C [===] !A => C and !B => C
75*0a6a1f1dSLionel Sambuc return LNeg ? LeftAndOperator(cast<And>(LHS))
76*0a6a1f1dSLionel Sambuc : LeftOrOperator(cast<And>(LHS));
77*0a6a1f1dSLionel Sambuc case LExpr::Or:
78*0a6a1f1dSLionel Sambuc // When performing left recursion:
79*0a6a1f1dSLionel Sambuc // A | B => C [if] A => C and B => C
80*0a6a1f1dSLionel Sambuc // When performing left recursion (negated):
81*0a6a1f1dSLionel Sambuc // !(A | B) => C [if] !A & !B => C [===] !A => C or !B => C
82*0a6a1f1dSLionel Sambuc return LNeg ? LeftOrOperator(cast<Or>(LHS))
83*0a6a1f1dSLionel Sambuc : LeftAndOperator(cast<Or>(LHS));
84*0a6a1f1dSLionel Sambuc case LExpr::Not:
85*0a6a1f1dSLionel Sambuc // Note that A => !C is very different from !(A => C). It would be incorrect
86*0a6a1f1dSLionel Sambuc // to return !implies(LHS, RHS).
87*0a6a1f1dSLionel Sambuc return implies(cast<Not>(LHS)->exp(), !LNeg, RHS, RNeg);
88*0a6a1f1dSLionel Sambuc case LExpr::Terminal:
89*0a6a1f1dSLionel Sambuc // After reaching the terminal, it's time to perform identity comparisons.
90*0a6a1f1dSLionel Sambuc break;
91*0a6a1f1dSLionel Sambuc }
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc // A => A
94*0a6a1f1dSLionel Sambuc // !A => !A
95*0a6a1f1dSLionel Sambuc if (LNeg != RNeg)
96*0a6a1f1dSLionel Sambuc return false;
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc // FIXME -- this should compare SExprs for equality, not pointer equality.
99*0a6a1f1dSLionel Sambuc return cast<Terminal>(LHS)->expr() == cast<Terminal>(RHS)->expr();
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc
102*0a6a1f1dSLionel Sambuc namespace clang {
103*0a6a1f1dSLionel Sambuc namespace threadSafety {
104*0a6a1f1dSLionel Sambuc namespace lexpr {
105*0a6a1f1dSLionel Sambuc
implies(const LExpr * LHS,const LExpr * RHS)106*0a6a1f1dSLionel Sambuc bool implies(const LExpr *LHS, const LExpr *RHS) {
107*0a6a1f1dSLionel Sambuc // Start out by assuming that LHS and RHS are not negated.
108*0a6a1f1dSLionel Sambuc return ::implies(LHS, false, RHS, false);
109*0a6a1f1dSLionel Sambuc }
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc }
112*0a6a1f1dSLionel Sambuc }
113