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