//===- AttributeSupport.h ---------------------------------------*- 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 // //===----------------------------------------------------------------------===// // // This file defines support types for registering dialect extended attributes. // //===----------------------------------------------------------------------===// #ifndef MLIR_IR_ATTRIBUTESUPPORT_H #define MLIR_IR_ATTRIBUTESUPPORT_H #include "mlir/IR/MLIRContext.h" #include "mlir/IR/StorageUniquerSupport.h" #include "mlir/IR/Types.h" #include "llvm/ADT/PointerIntPair.h" #include "llvm/ADT/Twine.h" namespace mlir { //===----------------------------------------------------------------------===// // AbstractAttribute //===----------------------------------------------------------------------===// /// This class contains all of the static information common to all instances of /// a registered Attribute. class AbstractAttribute { public: using HasTraitFn = llvm::unique_function; using WalkImmediateSubElementsFn = function_ref, function_ref)>; using ReplaceImmediateSubElementsFn = function_ref, ArrayRef)>; /// Look up the specified abstract attribute in the MLIRContext and return a /// reference to it. static const AbstractAttribute &lookup(TypeID typeID, MLIRContext *context); /// Look up the specified abstract attribute in the MLIRContext and return a /// reference to it if it exists. static std::optional> lookup(StringRef name, MLIRContext *context); /// This method is used by Dialect objects when they register the list of /// attributes they contain. template static AbstractAttribute get(Dialect &dialect) { return AbstractAttribute(dialect, T::getInterfaceMap(), T::getHasTraitFn(), T::getWalkImmediateSubElementsFn(), T::getReplaceImmediateSubElementsFn(), T::getTypeID(), T::name); } /// This method is used by Dialect objects to register attributes with /// custom TypeIDs. /// The use of this method is in general discouraged in favor of /// 'get(dialect)'. static AbstractAttribute get(Dialect &dialect, detail::InterfaceMap &&interfaceMap, HasTraitFn &&hasTrait, WalkImmediateSubElementsFn walkImmediateSubElementsFn, ReplaceImmediateSubElementsFn replaceImmediateSubElementsFn, TypeID typeID, StringRef name) { return AbstractAttribute(dialect, std::move(interfaceMap), std::move(hasTrait), walkImmediateSubElementsFn, replaceImmediateSubElementsFn, typeID, name); } /// Return the dialect this attribute was registered to. Dialect &getDialect() const { return const_cast(dialect); } /// Returns an instance of the concept object for the given interface if it /// was registered to this attribute, null otherwise. This should not be used /// directly. template typename T::Concept *getInterface() const { return interfaceMap.lookup(); } /// Returns true if the attribute has the interface with the given ID /// registered. bool hasInterface(TypeID interfaceID) const { return interfaceMap.contains(interfaceID); } /// Returns true if the attribute has a particular trait. template