1//===-- Types.td -------------------------------------------*- tablegen -*-===//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
8//===----------------------------------------------------------------------===//
10#ifndef LLZK_POLYMORPHIC_TYPES
11#define LLZK_POLYMORPHIC_TYPES
13include "llzk/Dialect/Shared/Types.td"
14include "llzk/Dialect/Polymorphic/IR/Dialect.td"
16class PolymorphicDialectType<string name, string typeMnemonic,
17 list<Trait> traits = []>
18 : TypeDef<PolymorphicDialect, name, traits> {
19 let mnemonic = typeMnemonic;
22def TypeVarSummary { string r = "type variable"; }
24def TypeVarType : PolymorphicDialectType<"TypeVar", "tvar"> {
25 let summary = TypeVarSummary.r;
27 This type serves as a placeholder for a type that is instantiated
28 via a parameter of the struct.
30 For example, we can define a struct that holds a size-2 array where the
31 type of the elements in the array is specified by a parameter of the
32 struct and instantiated with a specific type at the uses of the struct.
36 struct.def @A<[@Ty]> {
37 field @x : !array.type<2 x !poly.tvar<@Ty>>
44 (ins TypeParameter<"::mlir::FlatSymbolRefAttr",
45 "reference to the struct parameter">:$nameRef);
47 let assemblyFormat = [{ `<` $nameRef `>` }];
49 let builders = [TypeBuilderWithInferredContext<
50 (ins "::mlir::FlatSymbolRefAttr":$nameRef),
51 [{ return $_get(nameRef.getContext(), nameRef); }]>];
53 let extraClassDeclaration = [{
54 inline ::mlir::StringRef getRefName() const { return getNameRef().getValue(); }
58// Type constraint matching only the given Type or TypeVarType.
59class VarTypeOr<Type allowedConcreteType, string text = TypeVarSummary.r>
60 : Type<CPred<"::llvm::isa<llzk::polymorphic::"
61 "TypeVarType,"#allowedConcreteType.cppClassName#">($_self)">,
62 allowedConcreteType.summary#" or "#text, "::mlir::Type">;
64#endif // LLZK_POLYMORPHIC_TYPES