Skip to content

CppSharp.AST.Declaration properties QualifiedOriginalName, QualifiedLogicalOriginalName return wrong string #1739

@stachu99

Description

@stachu99
Brief Description

In CppSharp.AST.Declaration properties QualifiedOriginalName, QualifiedLogicalOriginalName return wrong string names.

OS: Windows 10
Target: MSVC
GeneratorKind.CLI

Examle

nestedtest.h contains an Union with a nested declaration of a Struct.

union _deskControlU { struct _deskControlS { unsigned char control; unsigned char cnt; } detailS; unsigned short mbData[sizeof (struct _deskControlS) / 2]; };

Using default settings works fine and generated code of the class DeskControlU contains proper code contained
nested class DeskControlS with e.g., 'property ::_deskControlS* NativePtr;'. ::_deskControlS* is as expected.

An exmaple with wrong QualifiedOriginalName.
There is a TranslationUnitPass translates nested classes into not nested classes.

public class SetNestedClassesNotNested : TranslationUnitPass
{
    private List<Class> _nestedClassesToMove;

    public SetNestedClassesNotNested()
        => VisitOptions.ResetFlags(VisitFlags.ClassProperties);

    public override bool VisitClassDecl(Class @class)
    {
        if (!(@class.Namespace is Namespace))
            _nestedClassesToMove.Add(@class);

        return base.VisitClassDecl(@class);
    }

    public override bool VisitTranslationUnit(TranslationUnit unit)
    {
        _nestedClassesToMove = new List<Class>();
        if (!base.VisitTranslationUnit(unit)) return false;

        foreach (var @class in _nestedClassesToMove)
        {
            var parent = @class.Namespace;
            @class.Namespace = GetNamespace(@class);
            @class.Namespace.Declarations.Add(@class);
            parent.Declarations.Remove(@class);
        }
        return true;
    }

    private Namespace GetNamespace(Declaration declaration)
    {
        var @namespace = declaration.Namespace;
        while (!(@namespace is Namespace))
        {
            @namespace = @namespace.Namespace;
         }
        return (Namespace)@namespace;
    }
}

With above TranslationUnitPass generated code contains two classes. DeskControlS is not nested as a default and contains e.g., the property 'property ::_deskControlS* NativePtr;'. Should be ::_deskControlU::_deskControlS*. This QualifiedOriginalName is wrong.

Proposition for changes in CppSharp.AST.Declaration.

    public string GetQualifiedOriginalName(Func<Declaration, string> getName, Func<Declaration, DeclarationContext> getNamespace)
    {
        DeclarationContext declarationContext = getNamespace(this);
        if (declarationContext == null)
            return getName(this);

        if (declarationContext.IsRoot)
            return getName(this);

        var namespaces = GatherOriginalNamespaces(getNamespace(this));
        
        var names = namespaces.Select(getName).ToList();
        names.Add(getName(this));
        names = names.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

        return string.Join(QualifiedNameSeparator, names);
    }

    public static IEnumerable<Declaration> GatherOriginalNamespaces(DeclarationContext @namespace)
    {
        var namespaces = new Stack<Declaration>();

        var currentNamespace = @namespace;
        while (currentNamespace != null)
        {
            var isInlineNamespace = currentNamespace is Namespace &&
                ((Namespace)currentNamespace).IsInline;
            if (!isInlineNamespace)
                namespaces.Push(currentNamespace);
            currentNamespace = currentNamespace.OriginalNamespace;
        }

        return namespaces;
    }

    public string QualifiedOriginalName
    {
        get
        {
            return GetQualifiedOriginalName(
                decl => GetDeclName(decl, decl.OriginalName), decl => decl.OriginalNamespace);
        }
    }

    public string QualifiedLogicalOriginalName
    {
        get
        {
            return GetQualifiedOriginalName(
                decl => GetDeclName(decl, decl.LogicalOriginalName), decl => decl.OriginalNamespace);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions