Class Type

Direct Known Subclasses:
Type.AliasType, Type.ArrayType, Type.GenericType, Type.ImageType, Type.MatrixType, Type.SamplerType, Type.ScalarType, Type.StructType, Type.VectorType

public class Type extends Symbol
Represents a type symbol, such as int or float4.
  • Field Details

    • kUnsizedArray

      public static final int kUnsizedArray
      See Also:
    • kMaxNestingDepth

      public static final int kMaxNestingDepth
      See Also:
    • kArray_TypeKind

      public static final byte kArray_TypeKind
      Kinds of Type.
      See Also:
    • kGeneric_TypeKind

      public static final byte kGeneric_TypeKind
      Kinds of Type.
      See Also:
    • kMatrix_TypeKind

      public static final byte kMatrix_TypeKind
      Kinds of Type.
      See Also:
    • kOther_TypeKind

      public static final byte kOther_TypeKind
      Kinds of Type.
      See Also:
    • kSampler_TypeKind

      public static final byte kSampler_TypeKind
      Kinds of Type.
      See Also:
    • kScalar_TypeKind

      public static final byte kScalar_TypeKind
      Kinds of Type.
      See Also:
    • kStruct_TypeKind

      public static final byte kStruct_TypeKind
      Kinds of Type.
      See Also:
    • kVector_TypeKind

      public static final byte kVector_TypeKind
      Kinds of Type.
      See Also:
    • kVoid_TypeKind

      public static final byte kVoid_TypeKind
      Kinds of Type.
      See Also:
    • kFloat_ScalarKind

      public static final byte kFloat_ScalarKind
      Kinds of ScalarType.
      See Also:
    • kSigned_ScalarKind

      public static final byte kSigned_ScalarKind
      Kinds of ScalarType.
      See Also:
    • kUnsigned_ScalarKind

      public static final byte kUnsigned_ScalarKind
      Kinds of ScalarType.
      See Also:
    • kBoolean_ScalarKind

      public static final byte kBoolean_ScalarKind
      Kinds of ScalarType.
      See Also:
    • kNonScalar_ScalarKind

      public static final byte kNonScalar_ScalarKind
      Kinds of ScalarType.
      See Also:
  • Method Details

    • makeAliasType

      public static @NonNull Type makeAliasType(@NonNull String name, @NonNull Type type)
      Creates an alias which maps to another type.
    • makeAliasType

      public static @NonNull Type makeAliasType(int position, String name, Type type)
      Creates an alias which maps to another type.
    • makeGenericType

      public static @NonNull Type makeGenericType(String name, Type... types)
      Create a generic type which maps to the listed types (e.g. __genFType is a generic type which can match float, float2, float3 or float4).
    • makeScalarType

      public static @NonNull Type makeScalarType(String name, String desc, byte kind, int rank, int width)
      Create a scalar type.
      Parameters:
      kind - a scalar kind
    • makeScalarType

      public static @NonNull Type makeScalarType(String name, String desc, byte kind, int rank, int minWidth, int width)
      Create a scalar type with minimum precision.
      Parameters:
      kind - a scalar kind
    • makeVectorType

      public static @NonNull Type makeVectorType(String name, String desc, Type componentType, int rows)
      Create a vector type.
      Parameters:
      componentType - a scalar type
    • makeMatrixType

      public static @NonNull Type makeMatrixType(String name, String desc, Type columnType, int cols)
      Create a matrix type.
      Parameters:
      columnType - a vector type
    • makeSamplerType

      public static @NonNull Type makeSamplerType(String name, String abbr, Type component, int dimensions, boolean isShadow, boolean isArrayed, boolean isMultiSampled, boolean isSampled, boolean isSampler)
      Create a sampler/image type. Includes images, subpass inputs, textures without sampler, textures with sampler, and pure samplers.
      • isSampled=true,isSampler=true: combined texture sampler (e.g. sampler2D)
      • isSampled=true,isSampler=false: pure texture (e.g. texture2D)
      • isSampled=false,isSampler=true: pure sampler (e.g. sampler)
      • isSampled=false,isSampler=false: image or subpass (e.g. image2D)
      isShadow: True for samplers that sample a depth texture with comparison (e.g. samplerShadow, sampler2DShadow, HLSL's SamplerComparisonState).
      Parameters:
      component - e.g. texture2D has a type of half
      dimensions - SpvDim (e.g. Spv.SpvDim1D)
    • makeImageType

      public static @NonNull Type makeImageType(String name, String abbr, Type component, int dimensions, boolean isArrayed, boolean isMultiSampled)
      Create an image or subpass type.
    • makeTextureType

      public static @NonNull Type makeTextureType(String name, String abbr, Type component, int dimensions, boolean isArrayed, boolean isMultiSampled)
      Create a texture type.
    • makeSeparateType

      public static @NonNull Type makeSeparateType(String name, String abbr, Type component, boolean isShadow)
      Create a separate sampler type.
    • makeCombinedType

      public static @NonNull Type makeCombinedType(String name, String abbr, Type component, int dimensions, boolean isShadow, boolean isArrayed, boolean isMultiSampled)
      Create a combined sampler type.
    • makeSpecialType

      public static @NonNull Type makeSpecialType(String name, String abbr, byte kind)
      Create a "special" type with the given name.
      Parameters:
      kind - a type kind
    • makeArrayType

      public static @NonNull Type makeArrayType(String name, Type type, int size)
      Creates an array type. Call isUsableInArray(Context, int) first.
      Parameters:
      type - the element type
    • makeStructType

      public static @NonNull Type makeStructType(@NonNull Context context, int position, @NonNull String name, @NonNull List<Type.Field> fields, boolean interfaceBlock)
      Creates a struct type with the given fields. Reports an error if the struct is ill-formed.
    • getKind

      public @NonNull Node.SymbolKind getKind()
      Specified by:
      getKind in class Symbol
      See Also:
    • getType

      public final @NonNull Type getType()
      Returns this.
      Specified by:
      getType in class Symbol
    • resolve

      public @NonNull Type resolve()
      If this is an alias, returns the underlying type, otherwise returns this.
    • getElementType

      public @NonNull Type getElementType()
      For arrays, returns the base type. For matrices, returns the column vector type. For vectors, returns the scalar type. For all other types, returns the type itself.
    • getComponentType

      public @NonNull Type getComponentType()
      For matrices and vectors, returns the scalar type of individual cells (e.g. mat2 has a component type of Float). For textures, returns the sampled type (e.g. texture2D has a component type of Float). For arrays, returns the component type of their base type. For all other types, returns the type itself.
    • matches

      public final boolean matches(@NonNull Type other)
      Returns true if these types are equal after alias resolution.
    • getDesc

      public final @NonNull String getDesc()
      Returns a descriptor of the type, meant for name-mangling. (e.g. float4x4 -> f44)
    • getTypeKind

      public final byte getTypeKind()
      Returns the category (scalar, vector, matrix, etc.) of this type.
    • getScalarKind

      public byte getScalarKind()
      Returns the ScalarKind of this type (always NonScalar for non-scalar values).
    • toString

      public final @NonNull String toString()
      Specified by:
      toString in class Node
      Returns:
      a string representation of this AST node
    • isInBuiltinTypes

      public final boolean isInBuiltinTypes()
      Returns true if this type is known to come from BuiltinTypes. If this returns true, the Type will always be available in the root SymbolTable and never needs to be copied to migrate an Expression from one location to another. If it returns false, the Type might not exist in a separate SymbolTable, and you'll need to consider copying it.
    • isBoolean

      public final boolean isBoolean()
      Returns true if this type is a bool.
    • isNumeric

      public final boolean isNumeric()
      Returns true if this is a numeric scalar type.
    • isFloat

      public final boolean isFloat()
      Returns true if this is a floating-point scalar type (float or half).
    • isSigned

      public final boolean isSigned()
      Returns true if this is a signed scalar type (int or short).
    • isUnsigned

      public final boolean isUnsigned()
      Returns true if this is an unsigned scalar type (uint or ushort).
    • isInteger

      public final boolean isInteger()
      Returns true if this is a signed or unsigned integer.
    • isFloatOrCompound

      public final boolean isFloatOrCompound()
      Returns true if this is a floating-point scalar type (float or half), or its vector/matrix form.
    • isSignedOrCompound

      public final boolean isSignedOrCompound()
      Returns true if this is a signed scalar type (int or short), or its vector form.
    • isUnsignedOrCompound

      public final boolean isUnsignedOrCompound()
      Returns true if this is an unsigned scalar type (uint or ushort), or its vector form.
    • isBooleanOrCompound

      public final boolean isBooleanOrCompound()
      Returns true if this type is a bool, or its vector form.
    • isOpaque

      public final boolean isOpaque()
      Returns true if this is an "opaque type" (an external object which the shader references in some fashion). Link
    • isGeneric

      public final boolean isGeneric()
    • getRank

      public int getRank()
      Returns the "rank" of a numeric type, in order of float > half > int > short. When operating on two numeric types, the result is the higher-rank type.
    • canCoerceTo

      public final boolean canCoerceTo(Type other, boolean allowNarrowing)
      Returns true if an instance of this type can be freely coerced (implicitly converted) to another type.
    • getCoercionCost

      public final long getCoercionCost(Type other)
      Determines the "cost" of coercing (implicitly converting) this type to another type. The cost is a number with no particular meaning other than that lower costs are preferable to higher costs.
      See Also:
    • getCoercibleTypes

      public @Unmodifiable List<Type> getCoercibleTypes()
      For generic types, returns the types that this generic type can substitute for.
    • coerceExpression

      public final @Nullable Expression coerceExpression(@NonNull Context context, @Nullable Expression expr)
      Coerces the passed-in expression to this type. If the types are incompatible, reports an error and returns null.
    • getMinValue

      public double getMinValue()
      Returns the minimum value that can fit in the type.
    • getMaxValue

      public double getMaxValue()
      Returns the maximum value that can fit in the type.
    • getCols

      public int getCols()
      For matrices, returns the number of columns (e.g. mat3x4 returns 3). For scalars and vectors, returns 1. For all other types, causes an assertion failure.
    • getRows

      public int getRows()
      For matrices and vectors, returns the number of rows (e.g. mat3x4 return 4). For scalars, returns 1. For all other types, causes an assertion failure.
    • getComponents

      public int getComponents()
      For type that contains scalars, returns the number of scalars. For all other types, causes an assertion failure.
    • getArraySize

      public int getArraySize()
      For arrays, returns either the size of the array (if known) or -1 (unsized). For all other types, causes an assertion failure.
    • getDimensions

      public int getDimensions()
      For sampler/image types, returns the SpvDim. For all other types, causes an assertion failure.
    • getFields

      public @Unmodifiable List<Type.Field> getFields()
    • isShadow

      public boolean isShadow()
      True for samplers that sample a depth texture with comparison (e.g., samplerShadow, sampler2DShadow, HLSL SamplerComparisonState).
    • isArrayed

      public boolean isArrayed()
      True for arrayed texture.
    • isVoid

      public final boolean isVoid()
    • isScalar

      public boolean isScalar()
    • isVector

      public boolean isVector()
    • isMatrix

      public boolean isMatrix()
    • isArray

      public boolean isArray()
    • isUnsizedArray

      public boolean isUnsizedArray()
    • isStruct

      public boolean isStruct()
    • isInterfaceBlock

      public boolean isInterfaceBlock()
    • isMultiSampled

      public boolean isMultiSampled()
    • isSampled

      public boolean isSampled()
    • isCombinedSampler

      public boolean isCombinedSampler()
      Returns:
      true for texture with sampler
    • isSeparateSampler

      public boolean isSeparateSampler()
      Returns:
      true for pure sampler
    • isStorageImage

      public boolean isStorageImage()
      Returns:
      true for image, except subpass input
    • getMinWidth

      public int getMinWidth()
      For scalars, returns the minimum size in bits of the type.
    • getWidth

      public int getWidth()
      For scalars, returns the size in bits of the type.
    • isRelaxedPrecision

      public boolean isRelaxedPrecision()
      Returns true if the component type has relaxed precision.
    • toVector

      public final Type toVector(@NonNull Context context, int rows)
      Returns the corresponding vector type with the specified number of rows.
    • toCompound

      public final Type toCompound(@NonNull Context context, int cols, int rows)
      Returns the corresponding vector or matrix type with the specified number of columns and rows.
    • getArrayName

      public @NonNull String getArrayName(int size)
      Converts an element type and a size (float, 10) into an array name ("float[10]").
    • getArrayName

      public static @NonNull String getArrayName(@NonNull String baseName, int size)
      Converts an element type and a size (float, 10) into an array name ("float[10]").
    • checkLiteralOutOfRange

      public boolean checkLiteralOutOfRange(Context context, int pos, double value)
      Checks if `value` can fit in this type. The type must be scalar.
    • isUsableInArray

      public boolean isUsableInArray(@NonNull Context context, int position)
      Reports errors and returns false if this type cannot be used as the element type for an array.
    • convertArraySize

      public int convertArraySize(@NonNull Context context, int position, @Nullable Expression size)
      Verifies that the expression is a valid constant array size for this type. Returns the array size, or reports errors and returns zero if the expression isn't a valid literal value.
    • convertArraySize

      public int convertArraySize(@NonNull Context context, int position, int sizePosition, long size)
    • getNestingDepth

      public int getNestingDepth()
      If the type is a struct, returns the depth of the most deeply-nested field.