xref: /llvm-project/mlir/include/mlir/Tools/PDLL/ODS/Constraint.h (revision b3fc0fa84a09540d8fc7214899021acbf2fd6ff8)
1 //===- Constraint.h - MLIR PDLL ODS Constraints -----------------*- 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 // This file contains a PDLL description of ODS constraints. These are used to
10 // support the import of constraints defined outside of PDLL.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TOOLS_PDLL_ODS_CONSTRAINT_H_
15 #define MLIR_TOOLS_PDLL_ODS_CONSTRAINT_H_
16 
17 #include <string>
18 
19 #include "mlir/Support/LLVM.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 
23 namespace mlir {
24 namespace pdll {
25 namespace ods {
26 
27 //===----------------------------------------------------------------------===//
28 // Constraint
29 //===----------------------------------------------------------------------===//
30 
31 /// This class represents a generic ODS constraint.
32 class Constraint {
33 public:
34   /// Return the unique name of this constraint.
getName()35   StringRef getName() const { return name; }
36 
37   /// Return the demangled name of this constraint. This tries to strip out bits
38   /// of the name that are purely for uniquing, and show the underlying name. As
39   /// such, this name does guarantee uniqueness and should only be used for
40   /// logging or other lossy friendly "pretty" output.
41   StringRef getDemangledName() const;
42 
43   /// Return the summary of this constraint.
getSummary()44   StringRef getSummary() const { return summary; }
45 
46 protected:
Constraint(StringRef name,StringRef summary)47   Constraint(StringRef name, StringRef summary)
48       : name(name.str()), summary(summary.str()) {}
49   Constraint(const Constraint &) = delete;
50 
51 private:
52   /// The name of the constraint.
53   std::string name;
54   /// A summary of the constraint.
55   std::string summary;
56 };
57 
58 //===----------------------------------------------------------------------===//
59 // AttributeConstraint
60 //===----------------------------------------------------------------------===//
61 
62 /// This class represents a generic ODS Attribute constraint.
63 class AttributeConstraint : public Constraint {
64 public:
65   /// Return the name of the underlying c++ class of this constraint.
getCppClass()66   StringRef getCppClass() const { return cppClassName; }
67 
68 private:
AttributeConstraint(StringRef name,StringRef summary,StringRef cppClassName)69   AttributeConstraint(StringRef name, StringRef summary, StringRef cppClassName)
70       : Constraint(name, summary), cppClassName(cppClassName.str()) {}
71 
72   /// The c++ class of the constraint.
73   std::string cppClassName;
74 
75   /// Allow access to the constructor.
76   friend class Context;
77 };
78 
79 //===----------------------------------------------------------------------===//
80 // TypeConstraint
81 //===----------------------------------------------------------------------===//
82 
83 /// This class represents a generic ODS Type constraint.
84 class TypeConstraint : public Constraint {
85 public:
86   /// Return the name of the underlying c++ class of this constraint.
getCppClass()87   StringRef getCppClass() const { return cppClassName; }
88 
89 private:
TypeConstraint(StringRef name,StringRef summary,StringRef cppClassName)90   TypeConstraint(StringRef name, StringRef summary, StringRef cppClassName)
91       : Constraint(name, summary), cppClassName(cppClassName.str()) {}
92 
93   /// The c++ class of the constraint.
94   std::string cppClassName;
95 
96   /// Allow access to the constructor.
97   friend class Context;
98 };
99 
100 } // namespace ods
101 } // namespace pdll
102 } // namespace mlir
103 
104 #endif // MLIR_TOOLS_PDLL_ODS_CONSTRAINT_H_
105