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