|
| 1 | +//===- OpClass.h - Helper classes for Op C++ code emission ------*- 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 defines several classes for Op C++ code emission. They are only |
| 10 | +// expected to be used by MLIR TableGen backends. |
| 11 | +// |
| 12 | +// We emit the op declaration and definition into separate files: *Ops.h.inc |
| 13 | +// and *Ops.cpp.inc. The former is to be included in the dialect *Ops.h and |
| 14 | +// the latter for dialect *Ops.cpp. This way provides a cleaner interface. |
| 15 | +// |
| 16 | +// In order to do this split, we need to track method signature and |
| 17 | +// implementation logic separately. Signature information is used for both |
| 18 | +// declaration and definition, while implementation logic is only for |
| 19 | +// definition. So we have the following classes for C++ code emission. |
| 20 | +// |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | + |
| 23 | +#ifndef MLIR_TABLEGEN_OPCLASS_H_ |
| 24 | +#define MLIR_TABLEGEN_OPCLASS_H_ |
| 25 | + |
| 26 | +#include "mlir/Support/LLVM.h" |
| 27 | +#include "llvm/ADT/SmallVector.h" |
| 28 | +#include "llvm/ADT/StringRef.h" |
| 29 | + |
| 30 | +#include <string> |
| 31 | + |
| 32 | +namespace mlir { |
| 33 | +namespace tblgen { |
| 34 | +class FmtObjectBase; |
| 35 | + |
| 36 | +// Class for holding the signature of an op's method for C++ code emission |
| 37 | +class OpMethodSignature { |
| 38 | +public: |
| 39 | + OpMethodSignature(StringRef retType, StringRef name, StringRef params); |
| 40 | + |
| 41 | + // Writes the signature as a method declaration to the given `os`. |
| 42 | + void writeDeclTo(raw_ostream &os) const; |
| 43 | + // Writes the signature as the start of a method definition to the given `os`. |
| 44 | + // `namePrefix` is the prefix to be prepended to the method name (typically |
| 45 | + // namespaces for qualifying the method definition). |
| 46 | + void writeDefTo(raw_ostream &os, StringRef namePrefix) const; |
| 47 | + |
| 48 | +private: |
| 49 | + // Returns true if the given C++ `type` ends with '&' or '*', or is empty. |
| 50 | + static bool elideSpaceAfterType(StringRef type); |
| 51 | + |
| 52 | + std::string returnType; |
| 53 | + std::string methodName; |
| 54 | + std::string parameters; |
| 55 | +}; |
| 56 | + |
| 57 | +// Class for holding the body of an op's method for C++ code emission |
| 58 | +class OpMethodBody { |
| 59 | +public: |
| 60 | + explicit OpMethodBody(bool declOnly); |
| 61 | + |
| 62 | + OpMethodBody &operator<<(Twine content); |
| 63 | + OpMethodBody &operator<<(int content); |
| 64 | + OpMethodBody &operator<<(const FmtObjectBase &content); |
| 65 | + |
| 66 | + void writeTo(raw_ostream &os) const; |
| 67 | + |
| 68 | +private: |
| 69 | + // Whether this class should record method body. |
| 70 | + bool isEffective; |
| 71 | + std::string body; |
| 72 | +}; |
| 73 | + |
| 74 | +// Class for holding an op's method for C++ code emission |
| 75 | +class OpMethod { |
| 76 | +public: |
| 77 | + // Properties (qualifiers) of class methods. Bitfield is used here to help |
| 78 | + // querying properties. |
| 79 | + enum Property { |
| 80 | + MP_None = 0x0, |
| 81 | + MP_Static = 0x1, // Static method |
| 82 | + MP_Constructor = 0x2, // Constructor |
| 83 | + MP_Private = 0x4, // Private method |
| 84 | + }; |
| 85 | + |
| 86 | + OpMethod(StringRef retType, StringRef name, StringRef params, |
| 87 | + Property property, bool declOnly); |
| 88 | + |
| 89 | + OpMethodBody &body(); |
| 90 | + |
| 91 | + // Returns true if this is a static method. |
| 92 | + bool isStatic() const; |
| 93 | + |
| 94 | + // Returns true if this is a private method. |
| 95 | + bool isPrivate() const; |
| 96 | + |
| 97 | + // Writes the method as a declaration to the given `os`. |
| 98 | + void writeDeclTo(raw_ostream &os) const; |
| 99 | + // Writes the method as a definition to the given `os`. `namePrefix` is the |
| 100 | + // prefix to be prepended to the method name (typically namespaces for |
| 101 | + // qualifying the method definition). |
| 102 | + void writeDefTo(raw_ostream &os, StringRef namePrefix) const; |
| 103 | + |
| 104 | +private: |
| 105 | + Property properties; |
| 106 | + // Whether this method only contains a declaration. |
| 107 | + bool isDeclOnly; |
| 108 | + OpMethodSignature methodSignature; |
| 109 | + OpMethodBody methodBody; |
| 110 | +}; |
| 111 | + |
| 112 | +// A class used to emit C++ classes from Tablegen. Contains a list of public |
| 113 | +// methods and a list of private fields to be emitted. |
| 114 | +class Class { |
| 115 | +public: |
| 116 | + explicit Class(StringRef name); |
| 117 | + |
| 118 | + // Creates a new method in this class. |
| 119 | + OpMethod &newMethod(StringRef retType, StringRef name, StringRef params = "", |
| 120 | + OpMethod::Property = OpMethod::MP_None, |
| 121 | + bool declOnly = false); |
| 122 | + |
| 123 | + OpMethod &newConstructor(StringRef params = "", bool declOnly = false); |
| 124 | + |
| 125 | + // Creates a new field in this class. |
| 126 | + void newField(StringRef type, StringRef name, StringRef defaultValue = ""); |
| 127 | + |
| 128 | + // Writes this op's class as a declaration to the given `os`. |
| 129 | + void writeDeclTo(raw_ostream &os) const; |
| 130 | + // Writes the method definitions in this op's class to the given `os`. |
| 131 | + void writeDefTo(raw_ostream &os) const; |
| 132 | + |
| 133 | + // Returns the C++ class name of the op. |
| 134 | + StringRef getClassName() const { return className; } |
| 135 | + |
| 136 | +protected: |
| 137 | + std::string className; |
| 138 | + SmallVector<OpMethod, 8> methods; |
| 139 | + SmallVector<std::string, 4> fields; |
| 140 | +}; |
| 141 | + |
| 142 | +// Class for holding an op for C++ code emission |
| 143 | +class OpClass : public Class { |
| 144 | +public: |
| 145 | + explicit OpClass(StringRef name, StringRef extraClassDeclaration = ""); |
| 146 | + |
| 147 | + // Sets whether this OpClass should generate the using directive for its |
| 148 | + // associate operand adaptor class. |
| 149 | + void setHasOperandAdaptorClass(bool has); |
| 150 | + |
| 151 | + // Adds an op trait. |
| 152 | + void addTrait(Twine trait); |
| 153 | + |
| 154 | + // Writes this op's class as a declaration to the given `os`. Redefines |
| 155 | + // Class::writeDeclTo to also emit traits and extra class declarations. |
| 156 | + void writeDeclTo(raw_ostream &os) const; |
| 157 | + |
| 158 | +private: |
| 159 | + StringRef extraClassDeclaration; |
| 160 | + SmallVector<std::string, 4> traits; |
| 161 | + bool hasOperandAdaptor; |
| 162 | +}; |
| 163 | + |
| 164 | +} // namespace tblgen |
| 165 | +} // namespace mlir |
| 166 | + |
| 167 | +#endif // MLIR_TABLEGEN_OPCLASS_H_ |
0 commit comments