• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Constraint.h - Constraint class --------------------------*- C++ -*-===//
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 #ifndef MLIR_TABLEGEN_CONSTRAINT_H_
14 #define MLIR_TABLEGEN_CONSTRAINT_H_
15 
16 #include "mlir/Support/LLVM.h"
17 #include "mlir/TableGen/Predicate.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace llvm {
22 class Record;
23 } // end namespace llvm
24 
25 namespace mlir {
26 namespace tblgen {
27 
28 // Wrapper class with helper methods for accessing Constraint defined in
29 // TableGen.
30 class Constraint {
31 public:
32   Constraint(const llvm::Record *record);
33 
34   bool operator==(const Constraint &that) { return def == that.def; }
35   bool operator!=(const Constraint &that) { return def != that.def; }
36 
37   // Returns the predicate for this constraint.
38   Pred getPredicate() const;
39 
40   // Returns the condition template that can be used to check if a type or
41   // attribute satisfies this constraint.  The template may contain "{0}" that
42   // must be substituted with an expression returning an mlir::Type or
43   // mlir::Attribute.
44   std::string getConditionTemplate() const;
45 
46   // Returns the user-readable description of this constraint. If the
47   // description is not provided, returns the TableGen def name.
48   StringRef getDescription() const;
49 
50   // Constraint kind
51   enum Kind { CK_Attr, CK_Region, CK_Successor, CK_Type, CK_Uncategorized };
52 
getKind()53   Kind getKind() const { return kind; }
54 
55 protected:
56   Constraint(Kind kind, const llvm::Record *record);
57 
58   // The TableGen definition of this constraint.
59   const llvm::Record *def;
60 
61 private:
62   // What kind of constraint this is.
63   Kind kind;
64 };
65 
66 // An constraint and the concrete entities to place the constraint on.
67 struct AppliedConstraint {
68   AppliedConstraint(Constraint &&constraint, StringRef self,
69                     std::vector<std::string> &&entities);
70 
71   Constraint constraint;
72   // The symbol to replace `$_self` special placeholder in the constraint.
73   std::string self;
74   // The symbols to replace `$N` positional placeholders in the constraint.
75   std::vector<std::string> entities;
76 };
77 
78 } // end namespace tblgen
79 } // end namespace mlir
80 
81 #endif // MLIR_TABLEGEN_CONSTRAINT_H_
82