1 //===- Constraint.cpp - Constraint class ----------------------------------===//
2 //
3 // Copyright 2019 The MLIR Authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // =============================================================================
17 //
18 // Constraint wrapper to simplify using TableGen Record for constraints.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #include "mlir/TableGen/Constraint.h"
23 #include "llvm/TableGen/Record.h"
24 
25 using namespace mlir::tblgen;
26 
27 Constraint::Constraint(const llvm::Record *record)
28     : def(record), kind(CK_Uncategorized) {
29   if (record->isSubClassOf("TypeConstraint")) {
30     kind = CK_Type;
31   } else if (record->isSubClassOf("AttrConstraint")) {
32     kind = CK_Attr;
33   } else if (record->isSubClassOf("RegionConstraint")) {
34     kind = CK_Region;
35   } else {
36     assert(record->isSubClassOf("Constraint"));
37   }
38 }
39 
40 Constraint::Constraint(Kind kind, const llvm::Record *record)
41     : def(record), kind(kind) {}
42 
43 Pred Constraint::getPredicate() const {
44   auto *val = def->getValue("predicate");
45 
46   // If no predicate is specified, then return the null predicate (which
47   // corresponds to true).
48   if (!val)
49     return Pred();
50 
51   const auto *pred = dyn_cast<llvm::DefInit>(val->getValue());
52   return Pred(pred);
53 }
54 
55 std::string Constraint::getConditionTemplate() const {
56   return getPredicate().getCondition();
57 }
58 
59 llvm::StringRef Constraint::getDescription() const {
60   auto doc = def->getValueAsString("description");
61   if (doc.empty())
62     return def->getName();
63   return doc;
64 }
65 
66 AppliedConstraint::AppliedConstraint(Constraint &&constraint,
67                                      llvm::StringRef self,
68                                      std::vector<std::string> &&entities)
69     : constraint(constraint), self(self), entities(std::move(entities)) {}
70