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     : def(record), kind(CK_Uncategorized) {
21   // Look through OpVariable's to their constraint.
22   if (def->isSubClassOf("OpVariable"))
23     def = def->getValueAsDef("constraint");
24   if (def->isSubClassOf("TypeConstraint")) {
25     kind = CK_Type;
26   } else if (def->isSubClassOf("AttrConstraint")) {
27     kind = CK_Attr;
28   } else if (def->isSubClassOf("RegionConstraint")) {
29     kind = CK_Region;
30   } else if (def->isSubClassOf("SuccessorConstraint")) {
31     kind = CK_Successor;
32   } else {
33     assert(def->isSubClassOf("Constraint"));
34   }
35 }
36 
37 Constraint::Constraint(Kind kind, const llvm::Record *record)
38     : def(record), kind(kind) {
39   // Look through OpVariable's to their constraint.
40   if (def->isSubClassOf("OpVariable"))
41     def = def->getValueAsDef("constraint");
42 }
43 
44 Pred Constraint::getPredicate() const {
45   auto *val = def->getValue("predicate");
46 
47   // If no predicate is specified, then return the null predicate (which
48   // corresponds to true).
49   if (!val)
50     return Pred();
51 
52   const auto *pred = dyn_cast<llvm::DefInit>(val->getValue());
53   return Pred(pred);
54 }
55 
56 std::string Constraint::getConditionTemplate() const {
57   return getPredicate().getCondition();
58 }
59 
60 StringRef Constraint::getSummary() const {
61   if (Optional<StringRef> summary = def->getValueAsOptionalString("summary"))
62     return *summary;
63   return def->getName();
64 }
65 
66 AppliedConstraint::AppliedConstraint(Constraint &&constraint,
67                                      llvm::StringRef self,
68                                      std::vector<std::string> &&entities)
69     : constraint(constraint), self(std::string(self)),
70       entities(std::move(entities)) {}
71