//===- DialectImplementation.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 contains utilities classes for implementing dialect attributes and // types. // //===----------------------------------------------------------------------===// #ifndef MLIR_IR_DIALECTIMPLEMENTATION_H #define MLIR_IR_DIALECTIMPLEMENTATION_H #include "mlir/IR/OpImplementation.h" #include <type_traits> namespace { // reference https://stackoverflow.com/a/16000226 template <typename T, typename = void> struct HasStaticDialectName : std::false_type { … }; HasStaticDialectName<T, typename std::enable_if<std::is_same< ::llvm::StringLiteral, std::decay_t<decltype(T::dialectName)>>::value, void>::type>; } // namespace namespace mlir { //===----------------------------------------------------------------------===// // DialectAsmPrinter //===----------------------------------------------------------------------===// /// This is a pure-virtual base class that exposes the asmprinter hooks /// necessary to implement a custom printAttribute/printType() method on a /// dialect. class DialectAsmPrinter : public AsmPrinter { … }; //===----------------------------------------------------------------------===// // DialectAsmParser //===----------------------------------------------------------------------===// /// The DialectAsmParser has methods for interacting with the asm parser when /// parsing attributes and types. class DialectAsmParser : public AsmParser { … }; //===----------------------------------------------------------------------===// // Parse Fields //===----------------------------------------------------------------------===// /// Provide a template class that can be specialized by users to dispatch to /// parsers. Auto-generated parsers generate calls to `FieldParser<T>::parse`, /// where `T` is the parameter storage type, to parse custom types. template <typename T, typename = T> struct FieldParser; /// Parse an attribute. FieldParser<AttributeT, std::enable_if_t<std::is_base_of<Attribute, AttributeT>::value, AttributeT>>; /// Parse an attribute. FieldParser<TypeT, std::enable_if_t<std::is_base_of<Type, TypeT>::value, TypeT>>; /// Parse any integer. FieldParser<IntT, std::enable_if_t<std::is_integral<IntT>::value, IntT>>; /// Parse a string. template <> struct FieldParser<std::string> { … }; /// Parse an Optional attribute. FieldParser<std::optional<AttributeT>, std::enable_if_t<std::is_base_of<Attribute, AttributeT>::value, std::optional<AttributeT>>>; /// Parse an Optional integer. FieldParser<std::optional<IntT>, std::enable_if_t<std::is_integral<IntT>::value, std::optional<IntT>>>; namespace detail { has_push_back_t; } // namespace detail /// Parse any container that supports back insertion as a list. FieldParser<ContainerT, std::enable_if_t<llvm::is_detected<mlir::detail::has_push_back_t, ContainerT>::value, ContainerT>>; /// Parse an affine map. template <> struct FieldParser<AffineMap> { … }; } // namespace mlir #endif // MLIR_IR_DIALECTIMPLEMENTATION_H