• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Interfaces.h - Interface wrapper classes -----------------*- 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_TABLEGEN_INTERFACES_H_
10 #define MLIR_TABLEGEN_INTERFACES_H_
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 
16 namespace llvm {
17 class Init;
18 class Record;
19 } // end namespace llvm
20 
21 namespace mlir {
22 namespace tblgen {
23 
24 // Wrapper class with helper methods for accessing InterfaceMethod defined
25 // in TableGen.
26 class InterfaceMethod {
27 public:
28   // This struct represents a single method argument.
29   struct Argument {
30     StringRef type;
31     StringRef name;
32   };
33 
34   explicit InterfaceMethod(const llvm::Record *def);
35 
36   // Return the return type of this method.
37   StringRef getReturnType() const;
38 
39   // Return the name of this method.
40   StringRef getName() const;
41 
42   // Return if this method is static.
43   bool isStatic() const;
44 
45   // Return the body for this method if it has one.
46   llvm::Optional<StringRef> getBody() const;
47 
48   // Return the default implementation for this method if it has one.
49   llvm::Optional<StringRef> getDefaultImplementation() const;
50 
51   // Return the description of this method if it has one.
52   llvm::Optional<StringRef> getDescription() const;
53 
54   // Arguments.
55   ArrayRef<Argument> getArguments() const;
56   bool arg_empty() const;
57 
58 private:
59   // The TableGen definition of this method.
60   const llvm::Record *def;
61 
62   // The arguments of this method.
63   SmallVector<Argument, 2> arguments;
64 };
65 
66 //===----------------------------------------------------------------------===//
67 // Interface
68 //===----------------------------------------------------------------------===//
69 
70 // Wrapper class with helper methods for accessing Interfaces defined in
71 // TableGen.
72 class Interface {
73 public:
74   explicit Interface(const llvm::Record *def);
75 
76   // Return the name of this interface.
77   StringRef getName() const;
78 
79   // Return the C++ namespace of this interface.
80   StringRef getCppNamespace() const;
81 
82   // Return the methods of this interface.
83   ArrayRef<InterfaceMethod> getMethods() const;
84 
85   // Return the description of this method if it has one.
86   llvm::Optional<StringRef> getDescription() const;
87 
88   // Return the interfaces extra class declaration code.
89   llvm::Optional<StringRef> getExtraClassDeclaration() const;
90 
91   // Return the traits extra class declaration code.
92   llvm::Optional<StringRef> getExtraTraitClassDeclaration() const;
93 
94   // Return the verify method body if it has one.
95   llvm::Optional<StringRef> getVerify() const;
96 
97   // Returns the Tablegen definition this interface was constructed from.
getDef()98   const llvm::Record &getDef() const { return *def; }
99 
100 private:
101   // The TableGen definition of this interface.
102   const llvm::Record *def;
103 
104   // The methods of this interface.
105   SmallVector<InterfaceMethod, 8> methods;
106 };
107 
108 // An interface that is registered to an Attribute.
109 struct AttrInterface : public Interface {
110   using Interface::Interface;
111 
112   static bool classof(const Interface *interface);
113 };
114 // An interface that is registered to an Operation.
115 struct OpInterface : public Interface {
116   using Interface::Interface;
117 
118   static bool classof(const Interface *interface);
119 };
120 // An interface that is registered to a Type.
121 struct TypeInterface : public Interface {
122   using Interface::Interface;
123 
124   static bool classof(const Interface *interface);
125 };
126 } // end namespace tblgen
127 } // end namespace mlir
128 
129 #endif // MLIR_TABLEGEN_INTERFACES_H_
130