1 //===- Constraint.cpp - Constraint class ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Constraint wrapper to simplify using TableGen Record for constraints.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/TableGen/Constraint.h"
14 #include "llvm/TableGen/Record.h"
15 
16 using namespace mlir;
17 using namespace mlir::tblgen;
18 
19 Constraint::Constraint(const llvm::Record *record)
20     : Constraint(record, CK_Uncategorized) {
21   // Look through OpVariable's to their constraint.
22   if (def->isSubClassOf("OpVariable"))
23     def = def->getValueAsDef("constraint");
24 
25   if (def->isSubClassOf("TypeConstraint")) {
26     kind = CK_Type;
27   } else if (def->isSubClassOf("AttrConstraint")) {
28     kind = CK_Attr;
29   } else if (def->isSubClassOf("RegionConstraint")) {
30     kind = CK_Region;
31   } else if (def->isSubClassOf("SuccessorConstraint")) {
32     kind = CK_Successor;
33   } else {
34     assert(def->isSubClassOf("Constraint"));
35   }
36 }
37 
38 Pred Constraint::getPredicate() const {
39   auto *val = def->getValue("predicate");
40 
41   // If no predicate is specified, then return the null predicate (which
42   // corresponds to true).
43   if (!val)
44     return Pred();
45 
46   const auto *pred = dyn_cast<llvm::DefInit>(val->getValue());
47   return Pred(pred);
48 }
49 
50 std::string Constraint::getConditionTemplate() const {
51   return getPredicate().getCondition();
52 }
53 
54 StringRef Constraint::getSummary() const {
55   if (Optional<StringRef> summary = def->getValueAsOptionalString("summary"))
56     return *summary;
57   return def->getName();
58 }
59 
60 AppliedConstraint::AppliedConstraint(Constraint &&constraint,
61                                      llvm::StringRef self,
62                                      std::vector<std::string> &&entities)
63     : constraint(constraint), self(std::string(self)),
64       entities(std::move(entities)) {}
65