xref: /freebsd/contrib/llvm-project/clang/lib/Analysis/FlowSensitive/Formula.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
106c3fb27SDimitry Andric //===- Formula.cpp ----------------------------------------------*- C++ -*-===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric 
906c3fb27SDimitry Andric #include "clang/Analysis/FlowSensitive/Formula.h"
1006c3fb27SDimitry Andric #include "clang/Basic/LLVM.h"
1106c3fb27SDimitry Andric #include "llvm/ADT/STLExtras.h"
1206c3fb27SDimitry Andric #include "llvm/ADT/StringRef.h"
1306c3fb27SDimitry Andric #include "llvm/Support/Allocator.h"
1406c3fb27SDimitry Andric #include "llvm/Support/ErrorHandling.h"
1506c3fb27SDimitry Andric #include <cassert>
16*5f757f3fSDimitry Andric #include <type_traits>
1706c3fb27SDimitry Andric 
1806c3fb27SDimitry Andric namespace clang::dataflow {
1906c3fb27SDimitry Andric 
20*5f757f3fSDimitry Andric const Formula &Formula::create(llvm::BumpPtrAllocator &Alloc, Kind K,
21*5f757f3fSDimitry Andric                                ArrayRef<const Formula *> Operands,
22*5f757f3fSDimitry Andric                                unsigned Value) {
2306c3fb27SDimitry Andric   assert(Operands.size() == numOperands(K));
2406c3fb27SDimitry Andric   if (Value != 0) // Currently, formulas have values or operands, not both.
2506c3fb27SDimitry Andric     assert(numOperands(K) == 0);
2606c3fb27SDimitry Andric   void *Mem = Alloc.Allocate(sizeof(Formula) +
2706c3fb27SDimitry Andric                                  Operands.size() * sizeof(Operands.front()),
2806c3fb27SDimitry Andric                              alignof(Formula));
2906c3fb27SDimitry Andric   Formula *Result = new (Mem) Formula();
3006c3fb27SDimitry Andric   Result->FormulaKind = K;
3106c3fb27SDimitry Andric   Result->Value = Value;
3206c3fb27SDimitry Andric   // Operands are stored as `const Formula *`s after the formula itself.
3306c3fb27SDimitry Andric   // We don't need to construct an object as pointers are trivial types.
3406c3fb27SDimitry Andric   // Formula is alignas(const Formula *), so alignment is satisfied.
3506c3fb27SDimitry Andric   llvm::copy(Operands, reinterpret_cast<const Formula **>(Result + 1));
3606c3fb27SDimitry Andric   return *Result;
3706c3fb27SDimitry Andric }
3806c3fb27SDimitry Andric 
3906c3fb27SDimitry Andric static llvm::StringLiteral sigil(Formula::Kind K) {
4006c3fb27SDimitry Andric   switch (K) {
4106c3fb27SDimitry Andric   case Formula::AtomRef:
42*5f757f3fSDimitry Andric   case Formula::Literal:
4306c3fb27SDimitry Andric     return "";
4406c3fb27SDimitry Andric   case Formula::Not:
4506c3fb27SDimitry Andric     return "!";
4606c3fb27SDimitry Andric   case Formula::And:
4706c3fb27SDimitry Andric     return " & ";
4806c3fb27SDimitry Andric   case Formula::Or:
4906c3fb27SDimitry Andric     return " | ";
5006c3fb27SDimitry Andric   case Formula::Implies:
5106c3fb27SDimitry Andric     return " => ";
5206c3fb27SDimitry Andric   case Formula::Equal:
5306c3fb27SDimitry Andric     return " = ";
5406c3fb27SDimitry Andric   }
5506c3fb27SDimitry Andric   llvm_unreachable("unhandled formula kind");
5606c3fb27SDimitry Andric }
5706c3fb27SDimitry Andric 
5806c3fb27SDimitry Andric void Formula::print(llvm::raw_ostream &OS, const AtomNames *Names) const {
5906c3fb27SDimitry Andric   if (Names && kind() == AtomRef)
6006c3fb27SDimitry Andric     if (auto It = Names->find(getAtom()); It != Names->end()) {
6106c3fb27SDimitry Andric       OS << It->second;
6206c3fb27SDimitry Andric       return;
6306c3fb27SDimitry Andric     }
6406c3fb27SDimitry Andric 
6506c3fb27SDimitry Andric   switch (numOperands(kind())) {
6606c3fb27SDimitry Andric   case 0:
67*5f757f3fSDimitry Andric     switch (kind()) {
68*5f757f3fSDimitry Andric     case AtomRef:
6906c3fb27SDimitry Andric       OS << getAtom();
7006c3fb27SDimitry Andric       break;
71*5f757f3fSDimitry Andric     case Literal:
72*5f757f3fSDimitry Andric       OS << (literal() ? "true" : "false");
73*5f757f3fSDimitry Andric       break;
74*5f757f3fSDimitry Andric     default:
75*5f757f3fSDimitry Andric       llvm_unreachable("unhandled formula kind");
76*5f757f3fSDimitry Andric     }
77*5f757f3fSDimitry Andric     break;
7806c3fb27SDimitry Andric   case 1:
7906c3fb27SDimitry Andric     OS << sigil(kind());
8006c3fb27SDimitry Andric     operands()[0]->print(OS, Names);
8106c3fb27SDimitry Andric     break;
8206c3fb27SDimitry Andric   case 2:
8306c3fb27SDimitry Andric     OS << '(';
8406c3fb27SDimitry Andric     operands()[0]->print(OS, Names);
8506c3fb27SDimitry Andric     OS << sigil(kind());
8606c3fb27SDimitry Andric     operands()[1]->print(OS, Names);
8706c3fb27SDimitry Andric     OS << ')';
8806c3fb27SDimitry Andric     break;
8906c3fb27SDimitry Andric   default:
9006c3fb27SDimitry Andric     llvm_unreachable("unhandled formula arity");
9106c3fb27SDimitry Andric   }
9206c3fb27SDimitry Andric }
9306c3fb27SDimitry Andric 
9406c3fb27SDimitry Andric } // namespace clang::dataflow