xref: /llvm-project/mlir/include/mlir/Tools/PDLL/ODS/Context.h (revision 1c2edb026ed67ddbb30ebe3e2d2f4f17a882a881)
1 //===- Context.h - MLIR PDLL ODS Context ------------------------*- 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 #ifndef MLIR_TOOLS_PDLL_ODS_CONTEXT_H_
10 #define MLIR_TOOLS_PDLL_ODS_CONTEXT_H_
11 
12 #include <string>
13 
14 #include "mlir/Support/LLVM.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 
19 namespace llvm {
20 class SMLoc;
21 } // namespace llvm
22 
23 namespace mlir {
24 namespace pdll {
25 namespace ods {
26 class AttributeConstraint;
27 class Dialect;
28 class Operation;
29 class TypeConstraint;
30 
31 /// This class contains all of the registered ODS operation classes.
32 class Context {
33 public:
34   Context();
35   ~Context();
36 
37   /// Insert a new attribute constraint with the context. Returns the inserted
38   /// constraint, or a previously inserted constraint with the same name.
39   const AttributeConstraint &insertAttributeConstraint(StringRef name,
40                                                        StringRef summary,
41                                                        StringRef cppClass);
42 
43   /// Insert a new type constraint with the context. Returns the inserted
44   /// constraint, or a previously inserted constraint with the same name.
45   const TypeConstraint &insertTypeConstraint(StringRef name, StringRef summary,
46                                              StringRef cppClass);
47 
48   /// Insert a new dialect with the context. Returns the inserted dialect, or a
49   /// previously inserted dialect with the same name.
50   Dialect &insertDialect(StringRef name);
51 
52   /// Lookup a dialect registered with the given name, or null if no dialect
53   /// with that name was inserted.
54   const Dialect *lookupDialect(StringRef name) const;
55 
56   /// Return a range of all of the registered dialects.
getDialects()57   auto getDialects() const {
58     return llvm::make_pointee_range(llvm::make_second_range(dialects));
59   }
60 
61   /// Insert a new operation with the context. Returns the inserted operation,
62   /// and a boolean indicating if the operation newly inserted (false if the
63   /// operation already existed).
64   std::pair<Operation *, bool>
65   insertOperation(StringRef name, StringRef summary, StringRef desc,
66                   StringRef nativeClassName, bool supportsResultTypeInferrence,
67                   SMLoc loc);
68 
69   /// Lookup an operation registered with the given name, or null if no
70   /// operation with that name is registered.
71   const Operation *lookupOperation(StringRef name) const;
72 
73   /// Print the contents of this context to the provided stream.
74   void print(raw_ostream &os) const;
75 
76 private:
77   llvm::StringMap<std::unique_ptr<AttributeConstraint>> attributeConstraints;
78   llvm::StringMap<std::unique_ptr<Dialect>> dialects;
79   llvm::StringMap<std::unique_ptr<TypeConstraint>> typeConstraints;
80 };
81 } // namespace ods
82 } // namespace pdll
83 } // namespace mlir
84 
85 #endif // MLIR_PDL_pdll_ODS_CONTEXT_H_
86