/
Constraint.h
88 lines (70 loc) · 2.92 KB
/
Constraint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===- Constraint.h - Constraint class --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Constraint wrapper to simplify using TableGen Record for constraints.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TABLEGEN_CONSTRAINT_H_
#define MLIR_TABLEGEN_CONSTRAINT_H_
#include "mlir/Support/LLVM.h"
#include "mlir/TableGen/Predicate.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
namespace llvm {
class Record;
} // namespace llvm
namespace mlir {
namespace tblgen {
// Wrapper class with helper methods for accessing Constraint defined in
// TableGen.
class Constraint {
public:
// Constraint kind
enum Kind { CK_Attr, CK_Region, CK_Successor, CK_Type, CK_Uncategorized };
// Create a constraint with a TableGen definition and a kind.
Constraint(const llvm::Record *record, Kind kind) : def(record), kind(kind) {}
// Create a constraint with a TableGen definition, and infer the kind.
Constraint(const llvm::Record *record);
/// Constraints are pointer-comparable.
bool operator==(const Constraint &that) { return def == that.def; }
bool operator!=(const Constraint &that) { return def != that.def; }
// Returns the predicate for this constraint.
Pred getPredicate() const;
// Returns the condition template that can be used to check if a type or
// attribute satisfies this constraint. The template may contain "{0}" that
// must be substituted with an expression returning an mlir::Type or
// mlir::Attribute.
std::string getConditionTemplate() const;
// Returns the user-readable description of this constraint. If the
// description is not provided, returns the TableGen def name.
StringRef getSummary() const;
/// Returns the name of the TablGen def of this constraint. In some cases
/// where the current def is anonymous, the name of the base def is used (e.g.
/// `Optional<>`/`Variadic<>` type constraints).
StringRef getDefName() const;
Kind getKind() const { return kind; }
protected:
// The TableGen definition of this constraint.
const llvm::Record *def;
private:
// What kind of constraint this is.
Kind kind;
};
// An constraint and the concrete entities to place the constraint on.
struct AppliedConstraint {
AppliedConstraint(Constraint &&constraint, StringRef self,
std::vector<std::string> &&entities);
Constraint constraint;
// The symbol to replace `$_self` special placeholder in the constraint.
std::string self;
// The symbols to replace `$N` positional placeholders in the constraint.
std::vector<std::string> entities;
};
} // namespace tblgen
} // namespace mlir
#endif // MLIR_TABLEGEN_CONSTRAINT_H_