Skip to content

Commit

Permalink
Typemap cleanup (#1802)
Browse files Browse the repository at this point in the history
* TypeMap: prepare refactoring into a modular design

* TypeMap: refactor C++ backend into common methods

* TypeMap: refactor CLI backend into common methods

* CLI.Gen.cs: fix omitted typemap from previous commit

* Common.Gen.cs: fixed silly modification while testing

* GeneratorKind: add FindGeneratorKindByID method

* TypeMapDatabase: heavy refactor: group typemaps by GeneratorKind

* TypeMap: refactor CSharp backend into common methods + migration

* TypeMap: cleanup patches from previous commits

* TypeMapDatabase: fix passing GeneratorKind to FindTypeMap calls

* Stdlib.CSharp.cs: move std::map typemap from Stdlib.CLI.cs

* TypeMapDatabase: improve parameter name
  • Loading branch information
deadlocklogic committed Dec 7, 2023
1 parent 8c2da6d commit e068f2a
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 355 deletions.
2 changes: 1 addition & 1 deletion src/Generator/AST/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, Type type)
return false;

var typePrinterContext = new TypePrinterContext { Type = type };
var mappedTo = typeMap.CSharpSignatureType(typePrinterContext);
var mappedTo = typeMap.SignatureType(typePrinterContext);
mappedTo = mappedTo.Desugar();
mappedTo = (mappedTo.GetFinalPointee() ?? mappedTo).Desugar();
return (mappedTo.IsPrimitiveType() ||
Expand Down
5 changes: 5 additions & 0 deletions src/Generator/GeneratorKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public GeneratorKind(string id, string name, System.Type generatorType, System.T
Registered.Add(this);
}

public static GeneratorKind FindGeneratorKindByID(string id)
{
return Registered.Where(kind => kind.ID == id).First();
}

public Generator CreateGenerator(BindingContext context)
{
return (Generator)Activator.CreateInstance(GeneratorType, context);
Expand Down
14 changes: 7 additions & 7 deletions src/Generator/Generators/C/CppMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}

Expand Down Expand Up @@ -173,7 +173,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}

Expand All @@ -193,7 +193,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}

Expand Down Expand Up @@ -341,7 +341,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}

Expand Down Expand Up @@ -478,7 +478,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}

Expand Down Expand Up @@ -516,7 +516,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}

Expand Down Expand Up @@ -563,7 +563,7 @@ private void MarshalRefClass(Class @class)
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/C/CppTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public virtual bool FindTypeMap(CppSharp.AST.Type type, out TypePrinterResult re
typePrinter.PushContext(ContextKind);
typePrinter.PushScope(ScopeKind);

var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter);
var typeName = typeMap.SignatureType(typePrinterContext).Visit(typePrinter);
result = new TypePrinterResult(typeName) { TypeMap = typeMap };

return true;
Expand Down
14 changes: 7 additions & 7 deletions src/Generator/Generators/CLI/CLIMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}

Expand All @@ -240,7 +240,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}

Expand Down Expand Up @@ -406,7 +406,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}

Expand Down Expand Up @@ -605,7 +605,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}

Expand Down Expand Up @@ -641,7 +641,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}

Expand Down Expand Up @@ -688,7 +688,7 @@ private void MarshalRefClass(Class @class)
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Generator/Generators/CLI/CLITypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
{
var typePrinterContext = new TypePrinterContext { Type = tag };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

Declaration decl = tag.Declaration;
Expand Down Expand Up @@ -112,7 +112,7 @@ public override TypePrinterResult VisitDelegate(FunctionType function)
Type = pointer
};

return typeMap.CLISignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}

var pointee = pointer.Pointee.Desugar();
Expand Down Expand Up @@ -217,7 +217,7 @@ public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive)
{
typeMap.Type = typedef;
var typePrinterContext = new TypePrinterContext { Type = typedef };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

FunctionType func;
Expand All @@ -241,7 +241,7 @@ public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive)
if (TypeMapDatabase.FindTypeMap(template, out typeMap) && !typeMap.IsIgnored)
{
var typePrinterContext = new TypePrinterContext { Type = template };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

return decl.Name;
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CSharpMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}

Expand Down Expand Up @@ -471,7 +471,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CSharpMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3158,7 +3158,7 @@ private void GenerateClassConstructor(Method method, Class @class)
Type = indirectRetType.Type.Desugar()
};

WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
WriteLine("{0} {1};", typeMap.SignatureType(typePrinterContext),
Helpers.ReturnIdentifier);
}
else
Expand Down
20 changes: 10 additions & 10 deletions src/Generator/Generators/CSharp/CSharpTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
return string.Empty;

TypeMap typeMap;
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
if (TypeMapDatabase.FindTypeMap(tag, GeneratorKind.CSharp, out typeMap))
{
typeMap.Type = tag;

Expand All @@ -47,7 +47,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
Type = tag
};

return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

return base.VisitTagType(tag, quals);
Expand Down Expand Up @@ -150,7 +150,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals)
{
TypeMap typeMap;
if (TypeMapDatabase.FindTypeMap(builtin, out typeMap))
if (TypeMapDatabase.FindTypeMap(builtin, GeneratorKind.CSharp, out typeMap))
{
var typePrinterContext = new TypePrinterContext()
{
Expand All @@ -159,7 +159,7 @@ public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQual
Type = builtin,
Parameter = Parameter
};
return typeMap.CSharpSignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}
return base.VisitBuiltinType(builtin, quals);
}
Expand All @@ -183,15 +183,15 @@ public override TypePrinterResult VisitFunctionType(FunctionType function, TypeQ
if (allowStrings && pointer.IsConstCharString())
{
TypeMap typeMap;
TypeMapDatabase.FindTypeMap(pointer, out typeMap);
TypeMapDatabase.FindTypeMap(pointer, GeneratorKind.CSharp, out typeMap);
var typePrinterContext = new TypePrinterContext()
{
Kind = ContextKind,
MarshalKind = MarshalKind,
Type = pointer.Pointee,
Parameter = Parameter
};
return typeMap.CSharpSignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}

var pointee = pointer.Pointee.Desugar();
Expand Down Expand Up @@ -258,7 +258,7 @@ public override TypePrinterResult VisitFunctionType(FunctionType function, TypeQ
var decl = typedef.Declaration;

TypeMap typeMap;
if (TypeMapDatabase.FindTypeMap(typedef, out typeMap))
if (TypeMapDatabase.FindTypeMap(typedef, GeneratorKind.CSharp, out typeMap))
{
typeMap.Type = typedef;

Expand All @@ -270,7 +270,7 @@ public override TypePrinterResult VisitFunctionType(FunctionType function, TypeQ
Parameter = Parameter
};

return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

FunctionType func;
Expand Down Expand Up @@ -299,7 +299,7 @@ public override TypePrinterResult VisitFunctionType(FunctionType function, TypeQ
template.Template.TemplatedDecl;

TypeMap typeMap;
if (!TypeMapDatabase.FindTypeMap(template, out typeMap))
if (!TypeMapDatabase.FindTypeMap(template, GeneratorKind.CSharp, out typeMap))
{
if (ContextKind == TypePrinterContextKind.Managed &&
decl == template.Template.TemplatedDecl &&
Expand Down Expand Up @@ -330,7 +330,7 @@ public override TypePrinterResult VisitFunctionType(FunctionType function, TypeQ
MarshalKind = MarshalKind
};

return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

public override TypePrinterResult VisitDependentTemplateSpecializationType(
Expand Down
7 changes: 2 additions & 5 deletions src/Generator/Generators/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ public static bool IsPrimitiveTypeConvertibleToRef(this Type type)
Type = typeMap.Type
};

switch (generatorKind)
if (generatorKind == GeneratorKind.CLI || generatorKind == GeneratorKind.CSharp)
{
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI):
return typeMap.CLISignatureType(typePrinterContext).Desugar();
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp):
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
return typeMap.SignatureType(typePrinterContext).Desugar();
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Generator/Generators/NAPI/NAPIMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}

Expand Down Expand Up @@ -194,7 +194,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}

Expand All @@ -214,7 +214,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}

Expand Down Expand Up @@ -343,7 +343,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}

Expand Down Expand Up @@ -591,7 +591,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}

Expand Down Expand Up @@ -628,7 +628,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}

Expand Down
Loading

0 comments on commit e068f2a

Please sign in to comment.