Browse Source

Simplified type maps by unlinking them from declarations.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1157/head
Dimitar Dobrev 8 years ago
parent
commit
efa11ab745
  1. 5
      src/AST/CppTypePrinter.cs
  2. 14
      src/Generator/AST/Utils.cs
  3. 5
      src/Generator/Generators/CLI/CLIHeaders.cs
  4. 13
      src/Generator/Generators/CLI/CLIMarshal.cs
  5. 11
      src/Generator/Generators/CLI/CLISources.cs
  6. 2
      src/Generator/Generators/CLI/CLITypePrinter.cs
  7. 24
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  8. 21
      src/Generator/Generators/CSharp/CSharpSources.cs
  9. 4
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  10. 2
      src/Generator/Generators/TypePrinter.cs
  11. 16
      src/Generator/Passes/CheckIgnoredDecls.cs
  12. 2
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  13. 4
      src/Generator/Types/Std/Stdlib.cs
  14. 2
      src/Generator/Types/TypeIgnoreChecker.cs
  15. 5
      src/Generator/Types/TypeMap.cs
  16. 95
      src/Generator/Types/TypeMapDatabase.cs
  17. 2
      tests/CSharp/CSharp.cs

5
src/AST/CppTypePrinter.cs

@ -444,8 +444,9 @@ namespace CppSharp.AST @@ -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)

14
src/Generator/AST/Utils.cs

@ -96,7 +96,8 @@ namespace CppSharp.AST @@ -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 @@ -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 @@ -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 @@ -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 };

5
src/Generator/Generators/CLI/CLIHeaders.cs

@ -333,10 +333,7 @@ namespace CppSharp.Generators.CLI @@ -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);

13
src/Generator/Generators/CLI/CLIMarshal.cs

@ -237,7 +237,8 @@ namespace CppSharp.Generators.CLI @@ -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 @@ -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 @@ -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 @@ -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

11
src/Generator/Generators/CLI/CLISources.cs

@ -289,10 +289,7 @@ namespace CppSharp.Generators.CLI @@ -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 @@ -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 @@ -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);

2
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -197,7 +197,7 @@ namespace CppSharp.Generators.CLI @@ -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 };

24
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -58,17 +58,7 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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)
{

21
src/Generator/Generators/CSharp/CSharpSources.cs

@ -910,7 +910,8 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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 @@ -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 @@ -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);

4
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -31,7 +31,7 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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;

2
src/Generator/Generators/TypePrinter.cs

@ -50,8 +50,6 @@ namespace CppSharp.Generators @@ -50,8 +50,6 @@ namespace CppSharp.Generators
}
public MarshalKind PopMarshalKind() => marshalKinds.Pop();
public Declaration Declaration;
public Parameter Parameter;
#region Dummy implementations

16
src/Generator/Passes/CheckIgnoredDecls.cs

@ -111,7 +111,7 @@ namespace CppSharp.Passes @@ -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 @@ -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)

2
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -154,7 +154,7 @@ namespace CppSharp.Passes @@ -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()
{

4
src/Generator/Types/Std/Stdlib.cs

@ -221,11 +221,11 @@ namespace CppSharp.Types.Std @@ -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 {

2
src/Generator/Types/TypeIgnoreChecker.cs

@ -88,7 +88,7 @@ namespace CppSharp @@ -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();

5
src/Generator/Types/TypeMap.cs

@ -104,10 +104,7 @@ namespace CppSharp.Types @@ -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);
}
}

95
src/Generator/Types/TypeMapDatabase.cs

@ -50,58 +50,6 @@ namespace CppSharp.Types @@ -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 @@ -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 @@ -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<Type, TypeMap> typeMaps = new Dictionary<Type, TypeMap>();
}

2
tests/CSharp/CSharp.cs

@ -179,7 +179,7 @@ namespace CppSharp.Tests @@ -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);

Loading…
Cancel
Save