1 //===- Constraint.cpp - Constraint class ----------------------------------===//
2 //
3 // Part of the MLIR 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 {
27     assert(record->isSubClassOf("Constraint"));
28   }
29 }
30 
31 Constraint::Constraint(Kind kind, const llvm::Record *record)
32     : def(record), kind(kind) {}
33 
34 Pred Constraint::getPredicate() const {
35   auto *val = def->getValue("predicate");
36 
37   // If no predicate is specified, then return the null predicate (which
38   // corresponds to true).
39   if (!val)
40     return Pred();
41 
42   const auto *pred = dyn_cast<llvm::DefInit>(val->getValue());
43   return Pred(pred);
44 }
45 
46 std::string Constraint::getConditionTemplate() const {
47   return getPredicate().getCondition();
48 }
49 
50 llvm::StringRef Constraint::getDescription() const {
51   auto doc = def->getValueAsString("description");
52   if (doc.empty())
53     return def->getName();
54   return doc;
55 }
56 
57 AppliedConstraint::AppliedConstraint(Constraint &&constraint,
58                                      llvm::StringRef self,
59                                      std::vector<std::string> &&entities)
60     : constraint(constraint), self(self), entities(std::move(entities)) {}
61