diff --git a/src/CppParser/Bootstrap/Bootstrap.cs b/src/CppParser/Bootstrap/Bootstrap.cs index 797bd05b..77dc78dc 100644 --- a/src/CppParser/Bootstrap/Bootstrap.cs +++ b/src/CppParser/Bootstrap/Bootstrap.cs @@ -109,8 +109,8 @@ namespace CppSharp exprCxxUnit.Visit(exprSubclassVisitor); ExprClasses = exprSubclassVisitor.Classes; - CodeGeneratorHelpers.CppTypePrinter = new CppTypePrinter(driver.Context) - { ScopeKind = TypePrintScopeKind.Local }; + CodeGeneratorHelpers.CppTypePrinter = new CppTypePrinter(driver.Context); + CodeGeneratorHelpers.CppTypePrinter.PushScope(TypePrintScopeKind.Local); GenerateStmt(driver.Context); GenerateExpr(driver.Context); @@ -501,7 +501,7 @@ namespace CppSharp : base(context) { Declarations = declarations; - TypePrinter.ScopeKind = TypePrintScopeKind.Local; + TypePrinter.PushScope(TypePrintScopeKind.Local); TypePrinter.PrintModuleOutputNamespace = false; } @@ -1438,7 +1438,7 @@ namespace CppSharp public override void Process() { Context.Options.GeneratorKind = GeneratorKind.CPlusPlus; - CTypePrinter.ScopeKind = TypePrintScopeKind.Local; + CTypePrinter.PushScope(TypePrintScopeKind.Local); GenerateFilePreamble(CommentKind.BCPL); NewLine(); @@ -1755,12 +1755,9 @@ namespace CppSharp public static string GetQualifiedName(Declaration decl, TypePrinter typePrinter) { - var scopeKind = typePrinter.ScopeKind; - typePrinter.ScopeKind = TypePrintScopeKind.Qualified; - + typePrinter.PushScope(TypePrintScopeKind.Qualified); var qualifiedName = decl.Visit(typePrinter).Type; - - typePrinter.ScopeKind = scopeKind; + typePrinter.PopScope(); qualifiedName = CleanClangNamespaceFromName(qualifiedName); @@ -1905,10 +1902,9 @@ namespace CppSharp if (iteratorType.IsPointer()) iteratorType = iteratorType.GetFinalPointee(); - var scopeKind = typePrinter.ScopeKind; - typePrinter.ScopeKind = TypePrintScopeKind.Qualified; + typePrinter.PushScope(TypePrintScopeKind.Qualified); var iteratorTypeName = iteratorType.Visit(typePrinter).Type; - typePrinter.ScopeKind = scopeKind; + typePrinter.PopScope(); iteratorTypeName = CleanClangNamespaceFromName(iteratorTypeName); diff --git a/src/Generator.Tests/AST/TestAST.cs b/src/Generator.Tests/AST/TestAST.cs index b31cb640..63d5c6fd 100644 --- a/src/Generator.Tests/AST/TestAST.cs +++ b/src/Generator.Tests/AST/TestAST.cs @@ -422,11 +422,8 @@ namespace CppSharp.Generator.Tests.AST [Test] public void TestPrintingConstPointerWithConstType() { - var cppTypePrinter = new CppTypePrinter(Context) - { - ScopeKind = TypePrintScopeKind.Qualified, - ResolveTypeMaps = false - }; + var cppTypePrinter = new CppTypePrinter(Context) { ResolveTypeMaps = false }; + cppTypePrinter.PushScope(TypePrintScopeKind.Qualified); var builtin = new BuiltinType(PrimitiveType.Char); var pointee = new QualifiedType(builtin, new TypeQualifiers { IsConst = true }); var pointer = new QualifiedType(new PointerType(pointee), new TypeQualifiers { IsConst = true }); @@ -438,7 +435,8 @@ namespace CppSharp.Generator.Tests.AST public void TestPrintingSpecializationWithConstValue() { var template = AstContext.FindDecl("TestSpecializationArguments").First(); - var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified }; + var cppTypePrinter = new CppTypePrinter(Context); + cppTypePrinter.PushScope(TypePrintScopeKind.Qualified); Assert.That(template.Specializations.Last().Visit(cppTypePrinter).Type, Is.EqualTo("TestSpecializationArguments")); } @@ -490,7 +488,8 @@ namespace CppSharp.Generator.Tests.AST [Test] public void TestVolatile() { - var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified }; + var cppTypePrinter = new CppTypePrinter(Context); + cppTypePrinter.PushScope(TypePrintScopeKind.Qualified); var builtin = new BuiltinType(PrimitiveType.Char); var pointee = new QualifiedType(builtin, new TypeQualifiers { IsConst = true, IsVolatile = true }); var type = pointee.Visit(cppTypePrinter).Type; @@ -508,7 +507,8 @@ namespace CppSharp.Generator.Tests.AST public void TestPrintNestedInSpecialization() { var template = AstContext.FindDecl("TestTemplateClass").First(); - var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified }; + var cppTypePrinter = new CppTypePrinter(Context); + cppTypePrinter.PushScope(TypePrintScopeKind.Qualified); Assert.That(template.Specializations[4].Classes.First().Visit(cppTypePrinter).Type, Is.EqualTo("TestTemplateClass::NestedInTemplate")); } @@ -517,7 +517,8 @@ namespace CppSharp.Generator.Tests.AST public void TestPrintQualifiedSpecialization() { var functionWithSpecializationArg = AstContext.FindFunction("functionWithSpecializationArg").First(); - var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified }; + var cppTypePrinter = new CppTypePrinter(Context); + cppTypePrinter.PushScope(TypePrintScopeKind.Qualified); Assert.That(functionWithSpecializationArg.Parameters[0].Visit(cppTypePrinter).Type, Is.EqualTo("const TestTemplateClass")); } diff --git a/src/Generator/Generators/C/CppSources.cs b/src/Generator/Generators/C/CppSources.cs index 43a4cf46..7f145bde 100644 --- a/src/Generator/Generators/C/CppSources.cs +++ b/src/Generator/Generators/C/CppSources.cs @@ -353,14 +353,20 @@ namespace CppSharp.Generators.Cpp public override string GetMethodIdentifier(Function function, TypePrinterContextKind context = TypePrinterContextKind.Managed) { - var method = function as Method; - if (method != null) + string id; + if (function is Method method) { var @class = method.Namespace as Class; - return $"{QualifiedIdentifier(@class)}::{base.GetMethodIdentifier(method, context)}"; + CTypePrinter.PushScope(TypePrintScopeKind.Qualified); + id = $"{QualifiedIdentifier(@class)}::{base.GetMethodIdentifier(method, context)}"; + CTypePrinter.PopScope(); + return id; } - return base.GetMethodIdentifier(function); + CTypePrinter.PushScope(TypePrintScopeKind.Qualified); + id = base.GetMethodIdentifier(function); + CTypePrinter.PopScope(); + return id; } public override bool VisitMethodDecl(Method method) diff --git a/src/Generator/Generators/C/CppTypePrinter.cs b/src/Generator/Generators/C/CppTypePrinter.cs index 1e28517b..2d36c09d 100644 --- a/src/Generator/Generators/C/CppTypePrinter.cs +++ b/src/Generator/Generators/C/CppTypePrinter.cs @@ -27,7 +27,6 @@ namespace CppSharp.Generators.C { Context = context; PrintFlavorKind = CppTypePrintFlavorKind.Cpp; - ScopeKind = TypePrintScopeKind.GlobalQualified; PrintTypeQualifiers = true; PrintTypeModifiers = true; } @@ -60,12 +59,12 @@ namespace CppSharp.Generators.C var typePrinter = new CppTypePrinter(Context) { PrintFlavorKind = PrintFlavorKind, - ScopeKind = ScopeKind, PrintTypeQualifiers = PrintTypeQualifiers, PrintTypeModifiers = PrintTypeModifiers, ResolveTypeMaps = false }; typePrinter.PushContext(ContextKind); + typePrinter.PushScope(ScopeKind); var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter); result = new TypePrinterResult(typeName) { TypeMap = typeMap }; diff --git a/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs b/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs index bbd6e1d6..d3081ac2 100644 --- a/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs +++ b/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs @@ -126,8 +126,9 @@ namespace CppSharp.Generators.CSharp var names = new List { mapped.OriginalName }; foreach (TypePrintScopeKind kind in Enum.GetValues(typeof(TypePrintScopeKind))) { - var cppTypePrinter = new CppTypePrinter(context) { ScopeKind = kind }; + var cppTypePrinter = new CppTypePrinter(context); cppTypePrinter.PushContext(TypePrinterContextKind.Native); + cppTypePrinter.PushScope(kind); names.Add(mapped.Visit(cppTypePrinter)); } foreach (var name in names.Where(context.TypeMaps.TypeMaps.ContainsKey)) diff --git a/src/Generator/Generators/NAPI/NAPISources.cs b/src/Generator/Generators/NAPI/NAPISources.cs index 5ba39d33..85c644dd 100644 --- a/src/Generator/Generators/NAPI/NAPISources.cs +++ b/src/Generator/Generators/NAPI/NAPISources.cs @@ -113,14 +113,14 @@ namespace CppSharp.Generators.Cpp var cTypePrinter = new CppTypePrinter(context) { PrintFlavorKind = CppTypePrintFlavorKind.C, - ScopeKind = TypePrintScopeKind.Local }; + cTypePrinter.PushScope(TypePrintScopeKind.Local); var functionName = cTypePrinter.VisitDeclaration(decl).ToString(); if (scope == TypePrintScopeKind.Local) return functionName; - cTypePrinter.ScopeKind = scope; + cTypePrinter.PushScope(scope); var qualifiedParentName = cTypePrinter.VisitDeclaration(decl.Namespace).ToString(); // HACK: CppTypePrinter code calls into decl.QualifiedName, which does not take into diff --git a/src/Generator/Generators/TypePrinter.cs b/src/Generator/Generators/TypePrinter.cs index 8d603444..b31efab6 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -49,6 +49,7 @@ namespace CppSharp.Generators { private readonly Stack contexts; private readonly Stack marshalKinds; + private readonly Stack scopeKinds; public TypePrinterContextKind ContextKind => contexts.Peek(); @@ -56,30 +57,28 @@ namespace CppSharp.Generators public MarshalKind MarshalKind => marshalKinds.Peek(); - public TypePrintScopeKind ScopeKind = TypePrintScopeKind.GlobalQualified; + public TypePrintScopeKind ScopeKind => scopeKinds.Peek(); public bool IsGlobalQualifiedScope => ScopeKind == TypePrintScopeKind.GlobalQualified; public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.Managed) { contexts = new Stack(); marshalKinds = new Stack(); + scopeKinds = new Stack(); PushContext(contextKind); PushMarshalKind(MarshalKind.Unknown); + PushScope(TypePrintScopeKind.GlobalQualified); } - public void PushContext(TypePrinterContextKind contextKind) - { - contexts.Push(contextKind); - } - + public void PushContext(TypePrinterContextKind kind) => contexts.Push(kind); public TypePrinterContextKind PopContext() => contexts.Pop(); - public void PushMarshalKind(MarshalKind marshalKind) - { - marshalKinds.Push(marshalKind); - } - + public void PushMarshalKind(MarshalKind kind) => marshalKinds.Push(kind); public MarshalKind PopMarshalKind() => marshalKinds.Pop(); + + public void PushScope(TypePrintScopeKind kind) => scopeKinds.Push(kind); + public TypePrintScopeKind PopScope() => scopeKinds.Pop(); + public Parameter Parameter; #region Dummy implementations diff --git a/src/Generator/Passes/SymbolsCodeGenerator.cs b/src/Generator/Passes/SymbolsCodeGenerator.cs index a890af99..91d7acd8 100644 --- a/src/Generator/Passes/SymbolsCodeGenerator.cs +++ b/src/Generator/Passes/SymbolsCodeGenerator.cs @@ -18,12 +18,12 @@ namespace CppSharp.Passes { cppTypePrinter = new CppTypePrinter(Context) { - ScopeKind = TypePrintScopeKind.Qualified, ResolveTypedefs = true, ResolveTypeMaps = false }; cppTypePrinter.PushContext(TypePrinterContextKind.Native); + cppTypePrinter.PushScope(TypePrintScopeKind.Qualified); } public override void Process() diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index 7b2428b7..1a0edbac 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -110,8 +110,9 @@ namespace CppSharp.Types new[] { TypePrintScopeKind.Local, TypePrintScopeKind.Qualified }) { typePrinter.ResolveTypedefs = resolveTypeDefs; - typePrinter.ScopeKind = typePrintScopeKind; + typePrinter.PushScope(typePrintScopeKind); var typeName = type.Visit(typePrinter); + typePrinter.PopScope(); if (FindTypeMap(typeName, out typeMap)) { typeMap.Type = type;