Repository: shader-slang/slang
File: source/slang/slang-emit-c-like.cpp (C++)

Code before:
    case kIROp_CastIntToPtr:
    case kIROp_PtrCast:
        {
            // Simple constructor call
            auto prec = getInfo(EmitOp::Prefix);
            needClose = maybeEmitParens(outerPrec, prec);

            m_writer->emit("(");
            emitType(inst->getDataType());
            m_writer->emit(")");

            emitOperand(inst->getOperand(0), rightSide(outerPrec, prec));
            break;
        }
    case kIROp_FieldExtract:
        {
            // Extract field from aggregate
            IRFieldExtract* fieldExtract = (IRFieldExtract*)inst;

            auto prec = getInfo(EmitOp::Postfix);
            needClose = maybeEmitParens(outerPrec, prec);

            auto base = fieldExtract->getBase();
            emitOperand(base, leftSide(outerPrec, prec));

            // Enhanced pointer detection for all C-like targets
            auto baseType = base->getDataType();
            if (baseType->getOp() == kIROp_ClassType || isPtrToClassType(baseType) ||
                base->getOp() == kIROp_GetAddress ||
                baseType->getOp() == kIROp_PtrType) // Add general pointer check
                m_writer->emit("->");
            else
                m_writer->emit(".");
            m_writer->emit(getName(fieldExtract->getField()));
            break;
        }
    case kIROp_FieldAddress:
        {
            // Extract field "address" from aggregate

            IRFieldAddress* ii = (IRFieldAddress*)inst;

            if (doesTargetSupportPtrTypes())
            {
                auto prec = getInfo(EmitOp::Prefix);
                needClose = maybeEmitParens(outerPrec, prec);
                m_writer->emit("&");
                outerPrec = rightSide(outerPrec, prec);
                auto innerPrec = getInfo(EmitOp::Postfix);
                bool innerNeedClose = maybeEmitParens(outerPrec, innerPrec);
                auto base = ii->getBase();

Review comment (bug):
this logic suggest that something is fundamentally wrong with other parts of the PR. ElementExtract is sup[posed to extract a field from an SSA register, and not to read a field from a pointer. For that you use GetElementAddress inst.

Code after:
    case kIROp_CastIntToPtr:
    case kIROp_PtrCast:
        {
            // Simple constructor call
            auto prec = getInfo(EmitOp::Prefix);
            needClose = maybeEmitParens(outerPrec, prec);

            m_writer->emit("(");
            emitType(inst->getDataType());
            m_writer->emit(")");

            emitOperand(inst->getOperand(0), rightSide(outerPrec, prec));
            break;
        }
    case kIROp_FieldExtract:
        {
            // Extract field from aggregate
            IRFieldExtract* fieldExtract = (IRFieldExtract*)inst;

            auto prec = getInfo(EmitOp::Postfix);
            needClose = maybeEmitParens(outerPrec, prec);

            auto base = fieldExtract->getBase();
            emitOperand(base, leftSide(outerPrec, prec));
            if (base->getDataType()->getOp() == kIROp_ClassType)
                m_writer->emit("->");
            else
                m_writer->emit(".");
            m_writer->emit(getName(fieldExtract->getField()));
            break;
        }
    case kIROp_FieldAddress:
        {
            // Extract field "address" from aggregate

            IRFieldAddress* ii = (IRFieldAddress*)inst;

            if (doesTargetSupportPtrTypes())
            {
                auto prec = getInfo(EmitOp::Prefix);
                needClose = maybeEmitParens(outerPrec, prec);
                m_writer->emit("&");
                outerPrec = rightSide(outerPrec, prec);
                auto innerPrec = getInfo(EmitOp::Postfix);
                bool innerNeedClose = maybeEmitParens(outerPrec, innerPrec);
                auto base = ii->getBase();
                if (isPtrToClassType(base->getDataType()))
                    emitDereferenceOperand(base, leftSide(outerPrec, innerPrec));
                else
                    emitOperand(base, leftSide(outerPrec, innerPrec));
                m_writer->emit("->");