Repository: shader-slang/slang
File: source/slang/slang-ast-builder.cpp (C++)

Code before:
    auto rsType = getSpecializedBuiltinType(valueType, "OptionalType");
    return as<OptionalType>(rsType);
}

PtrTypeBase* ASTBuilder::getPtrType(Type* valueType, char const* ptrTypeName)
{
    return as<PtrTypeBase>(getSpecializedBuiltinType(valueType, ptrTypeName));
}

PtrTypeBase* ASTBuilder::getPtrType(
    Type* valueType,
    Val* accessQualifier,
    Val* addrSpace,
    char const* ptrTypeName)
{
    Val* args[] = {valueType, accessQualifier, addrSpace};
    return as<PtrTypeBase>(getSpecializedBuiltinType(makeArrayView(args), ptrTypeName));
}

PtrTypeBase* ASTBuilder::getPtrType(
    Type* valueType,
    AccessQualifier accessQualifier,
    AddressSpace addrSpace,
    char const* ptrTypeName)
{
    Type* typeOfAccessQualifier = getSpecializedBuiltinType({}, "AccessQualifier");
    Type* typeOfAddressSpace = getSpecializedBuiltinType({}, "AddressSpace");
    return as<PtrTypeBase>(getPtrType(
        valueType,
        getIntVal(typeOfAccessQualifier, (IntegerLiteralValue)accessQualifier),
        getIntVal(typeOfAddressSpace, (IntegerLiteralValue)addrSpace),
        ptrTypeName));
}

ArrayExpressionType* ASTBuilder::getArrayType(Type* elementType, IntVal* elementCount)
{
    if (!elementCount)
        elementCount = getIntVal(getIntType(), kUnsizedArrayMagicLength);

    if (elementCount->getType() != getIntType())
    {
        // Canonicalize constant elementCount to int.
        if (auto elementCountConstantInt = as<ConstantIntVal>(elementCount))
        {
            elementCount = getIntVal(getIntType(), elementCountConstantInt->getValue());
        }
        else
        {
            elementCount = getTypeCastIntVal(getIntType(), elementCount);
        }
    }

Review comment (performance):
Maybe good to cache these types inside ASTBuilder so we don't do repetitive lookups every time it is needed.

Code after:
RefType* ASTBuilder::getRefType(Type* valueType)
{
    return dynamicCast<RefType>(getPtrType(valueType, "RefType"));
}

ConstRefType* ASTBuilder::getConstRefType(Type* valueType)
{
    return dynamicCast<ConstRefType>(getPtrType(valueType, "ConstRefType"));
}

OptionalType* ASTBuilder::getOptionalType(Type* valueType)
{
    auto rsType = getSpecializedBuiltinType(valueType, "OptionalType");
    return as<OptionalType>(rsType);
}

PtrTypeBase* ASTBuilder::getPtrType(Type* valueType, char const* ptrTypeName)
{
    return as<PtrTypeBase>(getSpecializedBuiltinType(valueType, ptrTypeName));
}

PtrTypeBase* ASTBuilder::getPtrType(
    Type* valueType,
    Val* accessQualifier,
    Val* addrSpace,
    char const* ptrTypeName)
{
    Val* args[] = {valueType, accessQualifier, addrSpace};
    return as<PtrTypeBase>(getSpecializedBuiltinType(makeArrayView(args), ptrTypeName));
}

PtrTypeBase* ASTBuilder::getPtrType(
    Type* valueType,
    AccessQualifier accessQualifier,
    AddressSpace addrSpace,
    char const* ptrTypeName)
{
    Type* typeOfAccessQualifier = getMagicEnumType("AccessQualifier");
    Type* typeOfAddressSpace = getMagicEnumType("AddressSpace");
    return as<PtrTypeBase>(getPtrType(
        valueType,
        getIntVal(typeOfAccessQualifier, (IntegerLiteralValue)accessQualifier),
        getIntVal(typeOfAddressSpace, (IntegerLiteralValue)addrSpace),
        ptrTypeName));
}

ArrayExpressionType* ASTBuilder::getArrayType(Type* elementType, IntVal* elementCount)
{
    if (!elementCount)
        elementCount = getIntVal(getIntType(), kUnsizedArrayMagicLength);