13#ifndef CIRCT_DIALECT_FIRRTL_TYPES_H
14#define CIRCT_DIALECT_FIRRTL_TYPES_H
21#include "mlir/IR/OpDefinition.h"
22#include "mlir/IR/Types.h"
23#include "llvm/ADT/TypeSwitch.h"
28struct FIRRTLBaseTypeStorage;
29struct WidthTypeStorage;
30struct BundleTypeStorage;
31struct FVectorTypeStorage;
32struct FEnumTypeStorage;
33struct CMemoryTypeStorage;
36struct BaseTypeAliasStorage;
37struct OpenBundleTypeStorage;
38struct OpenVectorTypeStorage;
39struct ClassTypeStorage;
40struct DomainTypeStorage;
66class BaseTypeAliasType;
94 return llvm::isa<FIRRTLDialect>(type.getDialect());
148 :
public FIRRTLType::TypeBase<FIRRTLBaseType, FIRRTLType,
149 detail::FIRRTLBaseTypeStorage> {
159 bool isPassive()
const {
return getRecursiveTypeProperties().isPassive; }
191 return llvm::isa<FIRRTLDialect>(type.getDialect()) &&
192 !llvm::isa<
PropertyType, RefType, LHSType, OpenBundleType,
193 OpenVectorType, FStringType, DomainType>(type);
221 bool destOuterTypeIsConst =
false,
222 bool srcOuterTypeIsConst =
false,
223 bool requireSameWidths =
false);
230 bool destFlip =
false,
bool srcFlip =
false,
231 bool destOuterTypeIsConst =
false,
232 bool srcOuterTypeIsConst =
false);
236 bool srcOuterTypeIsConst =
false);
274template <
typename ConcreteType>
276 :
public mlir::TypeTrait::TraitBase<ConcreteType, WidthQualifiedTypeTrait> {
281 auto width =
static_cast<const ConcreteType *
>(
this)->getWidthOrSentinel();
289 return 0 <=
static_cast<const ConcreteType *
>(
this)->getWidthOrSentinel();
303 using FIRRTLBaseType::FIRRTLBaseType;
308 int32_t widthOrSentinel = -1,
bool isConst =
false);
310 bool isSigned() {
return mlir::isa<SIntType>(*
this); }
319 static bool classof(Type type) {
return mlir::isa<SIntType, UIntType>(type); }
330 return llvm::isa<AnyRefType, ClassType, StringType, FIntegerType, ListType,
331 PathType, BoolType, DoubleType>(type);
335 using FIRRTLType::FIRRTLType;
387std::optional<int64_t>
getBitWidth(FIRRTLBaseType type,
388 bool ignoreFlip =
false);
406#define GET_TYPEDEF_CLASSES
407#include "circt/Dialect/FIRRTL/FIRRTLTypes.h.inc"
417 return FIRRTLType(
static_cast<mlir::Type::ImplType *
>(pointer));
421 return FIRRTLType(
static_cast<mlir::Type::ImplType *
>(pointer));
439template <
typename head,
typename... tail>
446template <
typename BaseTy>
448 static constexpr bool isFIRRTLBaseType =
449 std::is_base_of<FIRRTLBaseType, BaseTy>::value &&
450 !std::is_same_v<FIRRTLBaseType, BaseTy>;
451 static constexpr bool isFIRRTLType =
452 std::is_base_of<FIRRTLType, BaseTy>::value;
455 static constexpr bool value = isFIRRTLBaseType || !isFIRRTLType;
458template <
typename... BaseTy>
461 if (isa<BaseTy...>(type))
467 if (
auto alias = dyn_cast<BaseTypeAliasType>(type))
468 return type_isa<BaseTy...>(alias.getInnerType());
475template <
typename... BaseTy>
482template <
typename BaseTy>
484 assert(type_isa<BaseTy>(type) &&
"type must convert to requested type");
487 if (isa<BaseTy>(type))
488 return cast<BaseTy>(type);
492 if (
auto alias = dyn_cast<BaseTypeAliasType>(type))
493 return type_cast<BaseTy>(alias.getInnerType());
498 return cast<BaseTy>(type);
501template <
typename BaseTy>
503 if (type_isa<BaseTy>(type))
504 return type_cast<BaseTy>(type);
508template <
typename BaseTy>
511 if (type_isa_and_nonnull<BaseTy>(type))
512 return type_cast<BaseTy>(type);
523template <
typename T,
typename ResultT =
void>
533 template <
typename CaseT,
typename CallableT>
540 if (
auto caseValue = circt::firrtl::type_dyn_cast<CaseT>(this->value))
541 result.emplace(caseFn(caseValue));
546 template <
typename CallableT>
547 [[nodiscard]] ResultT
550 return std::move(*
result);
551 return defaultFn(this->value);
555 [[nodiscard]] ResultT
558 return std::move(*
result);
559 return defaultResult;
562 [[nodiscard]]
operator ResultT() {
564 return std::move(*
result);
584 template <
typename CaseT,
typename CallableT>
591 if (
auto caseValue = circt::firrtl::type_dyn_cast<CaseT>(this->value)) {
599 template <
typename CallableT>
602 defaultFn(this->value);
607 bool foundMatch =
false;
610template <
typename BaseTy>
613 firrtl::FIRRTLBaseType,
614 detail::FIRRTLBaseTypeStorage> {
620 static bool classof(Type other) {
return type_isa<BaseTy>(other); }
623 operator BaseTy()
const {
return circt::firrtl::type_cast<BaseTy>(*
this); }
625 BaseTy
base()
const {
return circt::firrtl::type_cast<BaseTy>(*
this); }
637struct mlir::AttrTypeSubElementHandler<
circt::firrtl::ClassElement> {
641 AttrTypeImmediateSubElementWalker &walker) {
642 walker.walk(param.
name);
643 walker.walk(param.
type);
646 AttrSubElementReplacements &attrRepls,
647 TypeSubElementReplacements &typeRepls) {
648 return ClassElement(cast<StringAttr>(attrRepls.take_front(1)[0]),
649 typeRepls.take_front(1)[0], param.
direction);
655struct mlir::AttrTypeSubElementHandler<
656 circt::firrtl::BundleType::BundleElement> {
660 AttrTypeImmediateSubElementWalker &walker) {
661 walker.walk(param.name);
662 walker.walk(param.type);
665 AttrSubElementReplacements &attrRepls,
666 TypeSubElementReplacements &typeRepls) {
668 cast<StringAttr>(attrRepls.take_front(1)[0]), param.isFlip,
669 cast<circt::firrtl::FIRRTLBaseType>(typeRepls.take_front(1)[0]));
675struct mlir::AttrTypeSubElementHandler<
676 circt::firrtl::OpenBundleType::BundleElement> {
680 AttrTypeImmediateSubElementWalker &walker) {
681 walker.walk(param.name);
682 walker.walk(param.type);
685 AttrSubElementReplacements &attrRepls,
686 TypeSubElementReplacements &typeRepls) {
688 cast<StringAttr>(attrRepls.take_front(1)[0]), param.isFlip,
689 cast<circt::firrtl::FIRRTLType>(typeRepls.take_front(1)[0]));
695struct mlir::AttrTypeSubElementHandler<
circt::firrtl::FEnumType::EnumElement> {
699 AttrTypeImmediateSubElementWalker &walker) {
700 walker.walk(param.name);
701 walker.walk(param.value);
702 walker.walk(param.type);
705 AttrSubElementReplacements &attrRepls,
706 TypeSubElementReplacements &typeRepls) {
707 auto attrs = attrRepls.take_front(2);
709 cast<StringAttr>(attrs[0]), cast<IntegerAttr>(attrs[1]),
710 cast<circt::firrtl::FIRRTLBaseType>(typeRepls.take_front(1)[0]));
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static bool classof(Type other)
A struct to check if there is a type derived from FIRRTLBaseType.
static constexpr bool value
FIRRTLBaseType getConstType(bool isConst) const
Return a 'const' or non-'const' version of this type.
FIRRTLBaseType getAnonymousType()
Return this type with any type alias types recursively removed from itself.
static bool classof(Type type)
Support method to enable LLVM-style type casting.
bool isResetType()
Return true if this is a valid "reset" type.
bool isRegisterType()
Returns true if this is a non-const "passive" that which is not analog.
FIRRTLBaseType getMaskType()
Return this type with all ground types replaced with UInt<1>.
FIRRTLBaseType getPassiveType()
Return this type with any flip types recursively removed from itself.
int32_t getBitWidthOrSentinel()
If this is an IntType, AnalogType, or sugar type for a single bit (Clock, Reset, etc) then return the...
FIRRTLBaseType getAllConstDroppedType()
Return this type with a 'const' modifiers dropped.
bool isPassive() const
Return true if this is a "passive" type - one that contains no "flip" types recursively within itself...
FIRRTLBaseType getWidthlessType()
Return this type with widths of all ground types removed.
bool isConst() const
Returns true if this is a 'const' type that can only hold compile-time constant values.
void Default(CallableT &&defaultFn)
As a default, invoke the given callable within the root value.
FIRRTLTypeSwitch< T, void > & Case(CallableT &&caseFn)
Add a case on the given type.
FIRRTLTypeSwitch(FIRRTLTypeSwitch &&other)=default
This class implements the same functionality as TypeSwitch except that it uses firrtl::type_dyn_cast ...
ResultT Default(ResultT defaultResult)
As a default, return the given value.
FIRRTLTypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
FIRRTLTypeSwitch(FIRRTLTypeSwitch &&other)=default
ResultT Default(CallableT &&defaultFn)
As a default, invoke the given callable within the root value.
std::optional< ResultT > result
The pointer to the result of this switch statement, once known, null before that.
bool containsReference()
Return true if this is or contains a Reference type.
bool isGround()
Return true if this is a 'ground' type, aka a non-aggregate type.
static bool classof(Type type)
Support method to enable LLVM-style type casting.
bool hasUninferredWidth()
Return true if this type contains an uninferred bit width.
bool containsTypeAlias()
Return true if this is an anonymous type (no type alias).
RecursiveTypeProperties getRecursiveTypeProperties() const
Return the recursive properties of the type, containing the isPassive, containsAnalog,...
bool hasUninferredReset()
Return true if this type contains an uninferred bit reset.
bool isConst() const
Returns true if this is a 'const' type that can only hold compile-time constant values.
bool containsAnalog()
Return true if this is or contains an Analog type.
bool containsConst()
Returns true if this is or contains a 'const' type.
This is the common base class between SIntType and UIntType.
IntType getConstType(bool isConst) const
Return a 'const' or non-'const' version of this type.
static bool classof(Type type)
int32_t getWidthOrSentinel() const
Return the width of this type, or -1 if it has none specified.
static IntType get(MLIRContext *context, bool isSigned, int32_t widthOrSentinel=-1, bool isConst=false)
Return an SIntType or UIntType with the specified signedness, width, and constness.
static bool classof(Type type)
Support method to enable LLVM-style type casting.
Trait for types which have a width.
bool hasWidth() const
Return true if this integer type has a known width.
std::optional< int32_t > getWidth() const
Return an optional containing the width, if the width is known (or empty if width is unknown).
BaseTy type_cast(Type type)
Direction
This represents the direction of a single port.
mlir::TypedValue< FIRRTLBaseType > FIRRTLBaseValue
ParseResult parseNestedType(FIRRTLType &result, AsmParser &parser)
Parse a FIRRTLType.
bool areAnonymousTypesEquivalent(FIRRTLBaseType lhs, FIRRTLBaseType rhs)
Return true if anonymous types of given arguments are equivalent by pointer comparison.
bool type_isa_and_nonnull(Type type)
ParseResult parseNestedBaseType(FIRRTLBaseType &result, AsmParser &parser)
bool isTypeInOut(mlir::Type type)
Returns true if the given type has some flipped (aka unaligned) dataflow.
bool areTypesRefCastable(Type dstType, Type srcType)
Return true if destination ref type can be cast from source ref type, per FIRRTL spec rules they must...
bool areTypesEquivalent(FIRRTLType destType, FIRRTLType srcType, bool destOuterTypeIsConst=false, bool srcOuterTypeIsConst=false, bool requireSameWidths=false)
Returns whether the two types are equivalent.
bool areTypesWeaklyEquivalent(FIRRTLType destType, FIRRTLType srcType, bool destFlip=false, bool srcFlip=false, bool destOuterTypeIsConst=false, bool srcOuterTypeIsConst=false)
Returns true if two types are weakly equivalent.
mlir::Type getPassiveType(mlir::Type anyBaseFIRRTLType)
bool isTypeLarger(FIRRTLBaseType dstType, FIRRTLBaseType srcType)
Returns true if the destination is at least as wide as a source.
bool containsConst(Type type)
Returns true if the type is or contains a 'const' type whose value is guaranteed to be unchanging at ...
mlir::TypedValue< FIRRTLType > FIRRTLValue
bool hasZeroBitWidth(FIRRTLType type)
Return true if the type has zero bit width.
BaseTy type_dyn_cast(Type type)
void printNestedType(Type type, AsmPrinter &os)
Print a type defined by this dialect.
BaseTy type_dyn_cast_or_null(Type type)
mlir::TypedValue< PropertyType > FIRRTLPropertyValue
bool isConst(Type type)
Returns true if this is a 'const' type whose value is guaranteed to be unchanging at circuit executio...
llvm::hash_code hash_value(const ClassElement &element)
bool areTypesConstCastable(FIRRTLType destType, FIRRTLType srcType, bool srcOuterTypeIsConst=false)
Returns whether the srcType can be const-casted to the destType.
ParseResult parseNestedPropertyType(PropertyType &result, AsmParser &parser)
std::optional< int64_t > getBitWidth(FIRRTLBaseType type, bool ignoreFlip=false)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
llvm::hash_code hash_value(const T &e)
bool isInOut() const
Return true if this is an inout port.
ClassElement(StringAttr name, Type type, Direction direction)
bool isOutput() const
Return true if this is a simple input-only element.
bool isInput() const
Return true if this is a simple output-only element.
StringRef getName() const
bool operator!=(const ClassElement &rhs) const
bool operator==(const ClassElement &rhs) const
A collection of bits indicating the recursive properties of a type.
bool containsReference
Whether the type contains a reference type.
bool isPassive
Whether the type only contains passive elements.
bool containsAnalog
Whether the type contains an analog type.
bool hasUninferredReset
Whether the type has any uninferred reset.
bool containsTypeAlias
Whether the type contains a type alias.
bool containsConst
Whether the type contains a const type.
bool hasUninferredWidth
Whether the type has any uninferred bit widths.
static FIRRTLType getTombstoneKey()
static FIRRTLType getEmptyKey()
static unsigned getHashValue(FIRRTLType val)
static bool isEqual(FIRRTLType LHS, FIRRTLType RHS)
static BundleElement replace(const BundleElement ¶m, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
circt::firrtl::BundleType::BundleElement BundleElement
static void walk(const BundleElement ¶m, AttrTypeImmediateSubElementWalker &walker)
static ClassElement replace(ClassElement param, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
static void walk(ClassElement param, AttrTypeImmediateSubElementWalker &walker)
circt::firrtl::FEnumType::EnumElement EnumElement
static void walk(const EnumElement ¶m, AttrTypeImmediateSubElementWalker &walker)
static EnumElement replace(const EnumElement ¶m, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
static BundleElement replace(const BundleElement ¶m, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
circt::firrtl::OpenBundleType::BundleElement BundleElement
static void walk(const BundleElement ¶m, AttrTypeImmediateSubElementWalker &walker)