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