Repository: shader-slang/slang
File: source/slang/slang-ir-clone.cpp (C++)

Code before:
            bool isExport = as<IRExportDecoration>(genLinkage);
            StringBuilder sb;
            sb.append(genLinkage->getMangledName());
            sb.append("G");
            IRSpecialize* specializationProvider = oldInst;
            if (auto targetAsSpec = as<IRSpecialize>(target))
            {
                specializationProvider = targetAsSpec;
            }

            DigestBuilder<SHA1> digestBuilder;
            for (UInt i = 0; i < specializationProvider->getArgCount(); ++i)
            {
                auto arg = specializationProvider->getArg(i);
                if (auto typeLinkage = arg->findDecoration<IRLinkageDecoration>())
                {
                    digestBuilder.append(typeLinkage->getMangledName().begin());
                }
                else
                {
                    // getTypeNameHint may produce a name with characters that can't
                    // be part of an identifier, so we need to filter it afterward.
                    StringBuilder typeNameHint;
                    StringBuilder linkageName;
                    getTypeNameHint(typeNameHint, arg);
                    emitNameForLinkage(linkageName, typeNameHint.getUnownedSlice());
                    digestBuilder.append(linkageName.getUnownedSlice().begin());
                }
            }
            sb.append(digestBuilder.finalize().toString());

            if (auto previousLinkage = target->findDecoration<IRLinkageDecoration>())
            {
                // Overwrite the previous linkage decoration, since it was not specialized
                previousLinkage->setOperand(0, builder->getStringValue(sb.getUnownedSlice()));
            }
            else
            {
                if (isExport)
                {
                    builder->addExportDecoration(target, sb.getUnownedSlice());
                }
                else
                {
                    builder->addImportDecoration(target, sb.getUnownedSlice());
                }
            }
        }
    }
}


Review comment (suggestion):
`emitNameForLinkage` is no longer needed because we no longer care if there are any "illegal" characters in the name.

I think it goes without saying that a better approach is to write a separate function to get a digest from `arg` without going through `getTypeNameHint`, but I can live with it for now.

Code after:
        }
        else
        {
            StringBuilder typeNameHint;
            getTypeNameHint(typeNameHint, arg);
            digestBuilder.append(typeNameHint.getUnownedSlice().begin());
        }
    }

    strBuilder.append(digestBuilder.finalize().toString());
}

// Copy the linkage decoration of oldInst (if present) and specialize it for target.
static void specializeLinkageDecoration(IRInst* target, IRSpecialize* oldInst, IRBuilder* builder)
{
    auto gen = as<IRGeneric>(oldInst->getBase());
    if (gen)
    {
        auto genLinkage = gen->findDecoration<IRLinkageDecoration>();
        if (genLinkage)
        {
            bool isExport = as<IRExportDecoration>(genLinkage);
            StringBuilder sb;
            sb.append(genLinkage->getMangledName());
            sb.append("G");
            IRSpecialize* specializationProvider = oldInst;
            if (auto targetAsSpec = as<IRSpecialize>(target))
            {
                specializationProvider = targetAsSpec;
            }
            StringBuilder specLinkName;
            getSpecializedLinkageName(specLinkName, specializationProvider);
            sb.append(specLinkName);

            if (auto previousLinkage = target->findDecoration<IRLinkageDecoration>())
            {
                // Overwrite the previous linkage decoration, since it was not specialized
                previousLinkage->setOperand(0, builder->getStringValue(sb.getUnownedSlice()));
            }
            else
            {
                if (isExport)
                {
                    builder->addExportDecoration(target, sb.getUnownedSlice());
                }
                else
                {
                    builder->addImportDecoration(target, sb.getUnownedSlice());
                }
            }
        }