From efa11ab745b0c3cee013430257600c3b7d753da2 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Fri, 18 Jan 2019 03:51:09 +0200 Subject: [PATCH] Simplified type maps by unlinking them from declarations. Signed-off-by: Dimitar Dobrev --- src/AST/CppTypePrinter.cs | 5 +- src/Generator/AST/Utils.cs | 14 +-- src/Generator/Generators/CLI/CLIHeaders.cs | 5 +- src/Generator/Generators/CLI/CLIMarshal.cs | 13 ++- src/Generator/Generators/CLI/CLISources.cs | 11 +-- .../Generators/CLI/CLITypePrinter.cs | 2 +- .../Generators/CSharp/CSharpMarshal.cs | 24 +---- .../Generators/CSharp/CSharpSources.cs | 21 ++-- .../Generators/CSharp/CSharpTypePrinter.cs | 4 +- src/Generator/Generators/TypePrinter.cs | 2 - src/Generator/Passes/CheckIgnoredDecls.cs | 16 +++- .../Passes/HandleDefaultParamValuesPass.cs | 2 +- src/Generator/Types/Std/Stdlib.cs | 4 +- src/Generator/Types/TypeIgnoreChecker.cs | 2 +- src/Generator/Types/TypeMap.cs | 5 +- src/Generator/Types/TypeMapDatabase.cs | 95 ++++--------------- tests/CSharp/CSharp.cs | 2 +- 17 files changed, 71 insertions(+), 156 deletions(-) diff --git a/src/AST/CppTypePrinter.cs b/src/AST/CppTypePrinter.cs index 36114035..bf8593e7 100644 --- a/src/AST/CppTypePrinter.cs +++ b/src/AST/CppTypePrinter.cs @@ -444,8 +444,9 @@ namespace CppSharp.AST return typedef.OriginalName; var originalNamespace = typedef.OriginalNamespace.Visit(this); - return originalNamespace == "::" ? typedef.OriginalName : - $"{originalNamespace}::{typedef.OriginalName}"; + return string.IsNullOrEmpty(originalNamespace) || + originalNamespace == "::" ? + typedef.OriginalName : $"{originalNamespace}::{typedef.OriginalName}"; } public virtual string VisitTypeAliasDecl(TypeAlias typeAlias) diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 6ebbe19c..19b0b374 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -96,7 +96,8 @@ namespace CppSharp.AST if (specialization == null) return true; - if (IsSpecializationNeeded(container, typeMaps, internalOnly, specialization)) + if (IsSpecializationNeeded(container, typeMaps, internalOnly, + type, specialization)) return false; if (!internalOnly) @@ -104,7 +105,7 @@ namespace CppSharp.AST if (IsSpecializationSelfContained(specialization, container)) return true; - if (IsMappedToPrimitive(typeMaps, type, specialization)) + if (IsMappedToPrimitive(typeMaps, type)) return true; } @@ -161,11 +162,11 @@ namespace CppSharp.AST } private static bool IsSpecializationNeeded(Declaration container, - ITypeMapDatabase typeMaps, bool internalOnly, + ITypeMapDatabase typeMaps, bool internalOnly, Type type, ClassTemplateSpecialization specialization) { TypeMap typeMap; - typeMaps.FindTypeMap(specialization, out typeMap); + typeMaps.FindTypeMap(type, out typeMap); return (!internalOnly && (((specialization.Ignore || specialization.TemplatedDecl.TemplatedClass.Ignore) && typeMap == null) || @@ -204,11 +205,10 @@ namespace CppSharp.AST return false; } - public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, - Type type, Declaration declaration) + public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, Type type) { TypeMap typeMap; - if (!typeMaps.FindTypeMap(declaration, out typeMap)) + if (!typeMaps.FindTypeMap(type, out typeMap)) return false; var typePrinterContext = new TypePrinterContext { Type = type }; diff --git a/src/Generator/Generators/CLI/CLIHeaders.cs b/src/Generator/Generators/CLI/CLIHeaders.cs index efbeee75..9e4edb76 100644 --- a/src/Generator/Generators/CLI/CLIHeaders.cs +++ b/src/Generator/Generators/CLI/CLIHeaders.cs @@ -333,10 +333,7 @@ namespace CppSharp.Generators.CLI var function = functionTemplate.TemplatedFunction; - var typePrinter = new CLITypePrinter(Context) - { - Declaration = template - }; + var typePrinter = new CLITypePrinter(Context); typePrinter.PushContext(TypePrinterContextKind.Template); var retType = function.ReturnType.Visit(typePrinter); diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index eb5373a5..0d7bba80 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -237,7 +237,8 @@ namespace CppSharp.Generators.CLI var decl = typedef.Declaration; TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling) + if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && + typeMap.DoesMarshalling) { typeMap.Type = typedef; typeMap.CLIMarshalToManaged(Context); @@ -298,7 +299,7 @@ namespace CppSharp.Generators.CLI instance += "&"; instance += Context.ReturnVarName; - var needsCopy = !(Context.Declaration is Field); + var needsCopy = Context.MarshalKind != MarshalKind.NativeField; if (@class.IsRefType && needsCopy) { @@ -608,7 +609,8 @@ namespace CppSharp.Generators.CLI var decl = typedef.Declaration; TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling) + if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && + typeMap.DoesMarshalling) { typeMap.CLIMarshalToNative(Context); return typeMap.IsValueType; @@ -689,14 +691,15 @@ namespace CppSharp.Generators.CLI private void MarshalRefClass(Class @class) { + var type = Context.Parameter.Type.Desugar(); TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(@class, out typeMap) && typeMap.DoesMarshalling) + if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && + typeMap.DoesMarshalling) { typeMap.CLIMarshalToNative(Context); return; } - var type = Context.Parameter.Type.Desugar(); var method = Context.Function as Method; if (type.IsReference() && (method == null || // redundant for comparison operators, they are handled in a special way diff --git a/src/Generator/Generators/CLI/CLISources.cs b/src/Generator/Generators/CLI/CLISources.cs index 58e8ee11..be57c829 100644 --- a/src/Generator/Generators/CLI/CLISources.cs +++ b/src/Generator/Generators/CLI/CLISources.cs @@ -289,10 +289,7 @@ namespace CppSharp.Generators.CLI var function = template.TemplatedFunction; - var typePrinter = new CLITypePrinter(Context) - { - Declaration = template - }; + var typePrinter = new CLITypePrinter(Context); typePrinter.PushContext(TypePrinterContextKind.Template); var retType = function.ReturnType.Visit(typePrinter); @@ -485,12 +482,11 @@ namespace CppSharp.Generators.CLI var ctx = new MarshalContext(Context, CurrentIndentation) { - Declaration = decl, ArgName = decl.Name, ReturnVarName = variable, ReturnType = decl.QualifiedType }; - + ctx.PushMarshalKind(MarshalKind.NativeField); var marshal = new CLIMarshalNativeToManagedPrinter(ctx); decl.Visit(marshal); @@ -691,10 +687,9 @@ namespace CppSharp.Generators.CLI ArgName = property.Name, ReturnVarName = nativeField, ReturnType = property.QualifiedType, - Declaration = property.Field, ParameterIndex = paramIndex++ }; - + ctx.PushMarshalKind(MarshalKind.NativeField); var marshal = new CLIMarshalNativeToManagedPrinter(ctx); property.Visit(marshal); diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index 32cf234b..8ba5860b 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -197,7 +197,7 @@ namespace CppSharp.Generators.CLI var decl = typedef.Declaration; TypeMap typeMap = null; - if (TypeMapDatabase.FindTypeMap(decl, out typeMap)) + if (TypeMapDatabase.FindTypeMap(decl.Type, out typeMap)) { typeMap.Type = typedef; var typePrinterContext = new TypePrinterContext { Type = typedef }; diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index af6eee68..372869fe 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -58,17 +58,7 @@ namespace CppSharp.Generators.CSharp return true; } - public override bool VisitDeclaration(Declaration decl) - { - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling) - { - typeMap.CSharpMarshalToManaged(Context); - return false; - } - - return true; - } + public override bool VisitDeclaration(Declaration decl) => true; public override bool VisitArrayType(ArrayType array, TypeQualifiers quals) { @@ -431,17 +421,7 @@ namespace CppSharp.Generators.CSharp return true; } - public override bool VisitDeclaration(Declaration decl) - { - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling) - { - typeMap.CSharpMarshalToNative(Context); - return false; - } - - return true; - } + public override bool VisitDeclaration(Declaration decl) => true; public override bool VisitArrayType(ArrayType array, TypeQualifiers quals) { diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 99d5a44e..9f254212 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -910,7 +910,8 @@ namespace CppSharp.Generators.CSharp ctx.PushMarshalKind(MarshalKind.NativeField); var marshal = new CSharpMarshalManagedToNativePrinter(ctx); - ctx.Declaration = field; + ctx.PushMarshalKind(MarshalKind.NativeField); + ctx.ReturnType = field.QualifiedType; param.Visit(marshal); @@ -1188,14 +1189,13 @@ namespace CppSharp.Generators.CSharp // IntPtr ensures that non-copying object constructor is invoked. Class typeClass; if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType && - !ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type, typeClass)) + !ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type)) returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})"; } var ctx = new CSharpMarshalContext(Context, CurrentIndentation) { ArgName = field.Name, - Declaration = field, ReturnVarName = returnVar, ReturnType = returnType }; @@ -2734,16 +2734,17 @@ namespace CppSharp.Generators.CSharp var indirectRetType = originalFunction.Parameters.First( parameter => parameter.Kind == ParameterKind.IndirectReturnType); - Class retClass; - indirectRetType.Type.Desugar().TryGetClass(out retClass); + Type type = indirectRetType.Type.Desugar(); TypeMap typeMap; string construct = null; - if (Context.TypeMaps.FindTypeMap(retClass, out typeMap)) + if (Context.TypeMaps.FindTypeMap(type, out typeMap)) construct = typeMap.CSharpConstruct(); if (construct == null) { + Class retClass; + type.TryGetClass(out retClass); var @class = retClass.OriginalClass ?? retClass; WriteLine($@"var {Helpers.ReturnIdentifier} = new { TypePrinter.PrintNative(@class)}();"); @@ -2752,10 +2753,10 @@ namespace CppSharp.Generators.CSharp { if (string.IsNullOrWhiteSpace(construct)) { - var typePrinterContext = new TypePrinterContext - { - Type = indirectRetType.Type.Desugar() - }; + var typePrinterContext = new TypePrinterContext + { + Type = indirectRetType.Type.Desugar() + }; WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext), Helpers.ReturnIdentifier); diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index dd2a2744..105e76dc 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -31,7 +31,7 @@ namespace CppSharp.Generators.CSharp return string.Empty; TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(tag.Declaration, out typeMap)) + if (TypeMapDatabase.FindTypeMap(tag, out typeMap)) { typeMap.Type = tag; @@ -271,7 +271,7 @@ namespace CppSharp.Generators.CSharp var decl = typedef.Declaration; TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(decl, out typeMap)) + if (TypeMapDatabase.FindTypeMap(decl.Type, out typeMap)) { typeMap.Type = typedef; diff --git a/src/Generator/Generators/TypePrinter.cs b/src/Generator/Generators/TypePrinter.cs index ed6c5cf2..e5e0ed37 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -50,8 +50,6 @@ namespace CppSharp.Generators } public MarshalKind PopMarshalKind() => marshalKinds.Pop(); - - public Declaration Declaration; public Parameter Parameter; #region Dummy implementations diff --git a/src/Generator/Passes/CheckIgnoredDecls.cs b/src/Generator/Passes/CheckIgnoredDecls.cs index 7df0d0c8..f5237c43 100644 --- a/src/Generator/Passes/CheckIgnoredDecls.cs +++ b/src/Generator/Passes/CheckIgnoredDecls.cs @@ -111,7 +111,7 @@ namespace CppSharp.Passes TypeMap typeMap; if (!(type is FunctionType) && (decl == null || ((decl.GenerationKind != GenerationKind.Internal || - Context.TypeMaps.FindTypeMap(decl, out typeMap)) && + Context.TypeMaps.FindTypeMap(type, out typeMap)) && !HasInvalidType(field, out msg)))) return false; @@ -513,11 +513,17 @@ namespace CppSharp.Passes private bool IsDeclIgnored(Declaration decl) { var parameter = decl as Parameter; - if (parameter != null && parameter.Type.Desugar().IsPrimitiveType(PrimitiveType.Null)) - return true; + if (parameter != null) + { + if (parameter.Type.Desugar().IsPrimitiveType(PrimitiveType.Null)) + return true; - TypeMap typeMap; - return TypeMaps.FindTypeMap(decl, out typeMap) ? typeMap.IsIgnored : decl.Ignore; + TypeMap typeMap; + if (TypeMaps.FindTypeMap(parameter.Type, out typeMap)) + return typeMap.IsIgnored; + } + + return decl.Ignore; } private void IgnoreUnsupportedTemplates(Class @class) diff --git a/src/Generator/Passes/HandleDefaultParamValuesPass.cs b/src/Generator/Passes/HandleDefaultParamValuesPass.cs index ff6a8bed..86922160 100644 --- a/src/Generator/Passes/HandleDefaultParamValuesPass.cs +++ b/src/Generator/Passes/HandleDefaultParamValuesPass.cs @@ -154,7 +154,7 @@ namespace CppSharp.Passes var typePrinterResult = type.Visit(typePrinter).Type; TypeMap typeMap; - if (TypeMaps.FindTypeMap(decl, type, out typeMap)) + if (TypeMaps.FindTypeMap(type, out typeMap)) { var typePrinterContext = new TypePrinterContext() { diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index 2d71c34b..c083573d 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -221,11 +221,11 @@ namespace CppSharp.Types.Std ClassTemplateSpecialization basicString = GetBasicString(type); var typePrinter = new CSharpTypePrinter(ctx.Context); if (!ctx.Parameter.Type.Desugar().IsAddress() && - !(ctx.Declaration is Field)) + ctx.MarshalKind != MarshalKind.NativeField) ctx.Return.Write($"*({typePrinter.PrintNative(basicString)}*) "); string qualifiedBasicString = GetQualifiedBasicString(basicString); var assign = basicString.Methods.First(m => m.OriginalName == "assign"); - if (ctx.Declaration is Field) + if (ctx.MarshalKind == MarshalKind.NativeField) { ctx.Return.Write($@"{qualifiedBasicString}Extensions.{ Helpers.InternalStruct}.{assign.Name}(new { diff --git a/src/Generator/Types/TypeIgnoreChecker.cs b/src/Generator/Types/TypeIgnoreChecker.cs index 015fcdf3..1a594906 100644 --- a/src/Generator/Types/TypeIgnoreChecker.cs +++ b/src/Generator/Types/TypeIgnoreChecker.cs @@ -88,7 +88,7 @@ namespace CppSharp public override bool VisitTypedefDecl(TypedefDecl typedef) { TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(typedef, out typeMap)) + if (TypeMapDatabase.FindTypeMap(typedef.Type, out typeMap)) { if (typeMap.IsIgnored) Ignore(); diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 4f1891d5..d344bab4 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -104,10 +104,7 @@ namespace CppSharp.Types public interface ITypeMapDatabase { - bool FindTypeMapRecursive(Type type, out TypeMap typeMap); bool FindTypeMap(Type decl, out TypeMap typeMap); - bool FindTypeMap(Declaration decl, out TypeMap typeMap); + bool FindTypeMap(Declaration declaration, out TypeMap typeMap); } - - } diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index 37fc9e1b..7cd28897 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -50,58 +50,6 @@ namespace CppSharp.Types } } - public bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap) - { - if (type != null && typeMaps.ContainsKey(type)) - { - typeMap = typeMaps[type]; - return typeMap.IsEnabled; - } - - // We try to find type maps from the most qualified to less qualified - // types. Example: '::std::vector', 'std::vector' and 'vector' - - var typePrinter = new CppTypePrinter { PrintLogicalNames = true }; - - if (FindTypeMap(decl, type, out typeMap, typePrinter)) - return true; - - typePrinter.PrintScopeKind = TypePrintScopeKind.Qualified; - if (FindTypeMap(decl, type, out typeMap, typePrinter)) - return true; - - typePrinter.ResolveTypedefs = true; - if (FindTypeMap(decl, type, out typeMap, typePrinter)) - return true; - typePrinter.ResolveTypedefs = false; - - typePrinter.PrintScopeKind = TypePrintScopeKind.Local; - if (FindTypeMap(decl, type, out typeMap, typePrinter)) - return true; - - var specialization = decl as ClassTemplateSpecialization; - if (specialization != null && - FindTypeMap(specialization.TemplatedDecl, type, out typeMap, typePrinter)) - return true; - - var typedef = decl as TypedefDecl; - return typedef != null && FindTypeMap(typedef.Type, out typeMap); - } - - private bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap, CppTypePrinter typePrinter) - { - if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) - { - if (type != null && typeMap.Type == null) - { - typeMap.Type = type; - typeMaps[type] = typeMap; - } - return true; - } - return false; - } - public bool FindTypeMap(Type type, out TypeMap typeMap) { if (typeMaps.ContainsKey(type)) @@ -114,11 +62,19 @@ namespace CppSharp.Types if (template != null) { var specialization = template.GetClassTemplateSpecialization(); - if (specialization != null && FindTypeMap(specialization, type, out typeMap)) + if (specialization != null && + FindTypeMap(specialization, out typeMap)) return true; if (template.Template.TemplatedDecl != null) - return FindTypeMap(template.Template.TemplatedDecl, type, - out typeMap); + { + if (FindTypeMap(template.Template.TemplatedDecl, + out typeMap)) + { + typeMap.Type = type; + return true; + } + return false; + } } Type desugared = type.Desugar(); @@ -147,33 +103,14 @@ namespace CppSharp.Types typeMap = null; var typedef = type as TypedefType; - return typedef != null && FindTypeMap(typedef.Declaration, type, out typeMap); + return typedef != null && FindTypeMap(typedef.Declaration.Type, out typeMap); } - public bool FindTypeMap(Declaration decl, out TypeMap typeMap) - { - return FindTypeMap(decl, null, out typeMap); - } + public bool FindTypeMap(Declaration declaration, out TypeMap typeMap) => + FindTypeMap(new TagType(declaration), out typeMap); - public bool FindTypeMapRecursive(Type type, out TypeMap typeMap) - { - while (true) - { - if (FindTypeMap(type, out typeMap)) - return true; - - var desugaredType = type.Desugar(); - if (desugaredType == type) - return false; - - type = desugaredType; - } - } - - private bool FindTypeMap(string name, out TypeMap typeMap) - { - return TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled; - } + private bool FindTypeMap(string name, out TypeMap typeMap) => + TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled; private Dictionary typeMaps = new Dictionary(); } diff --git a/tests/CSharp/CSharp.cs b/tests/CSharp/CSharp.cs index 92127e43..7b5ee184 100644 --- a/tests/CSharp/CSharp.cs +++ b/tests/CSharp/CSharp.cs @@ -179,7 +179,7 @@ namespace CppSharp.Tests { get { - var type = (TemplateSpecializationType)Type; + var type = (TemplateSpecializationType) Type; var pointeeType = type.Arguments[0].Type; var checker = new TypeIgnoreChecker(TypeMapDatabase); pointeeType.Visit(checker);