Skip to content

Commit

Permalink
Provide TypePrinters with more context
Browse files Browse the repository at this point in the history
  • Loading branch information
Saalvage committed Oct 22, 2023
1 parent 3b2a15d commit 38bc0e4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,14 @@ public override bool VisitFieldDecl(Field field)
Context.Parameter = new Parameter
{
Name = Context.ArgName,
QualifiedType = field.QualifiedType
QualifiedType = field.QualifiedType,
};

return field.Type.Visit(this, field.QualifiedType.Qualifiers);
Context.Field = field;
var ret = field.Type.Visit(this, field.QualifiedType.Qualifiers);
Context.Field = null;

return ret;
}

public override bool VisitProperty(Property property)
Expand All @@ -843,7 +847,11 @@ public override bool VisitProperty(Property property)
QualifiedType = property.QualifiedType
};

return base.VisitProperty(property);
Context.Property = property;
var ret = base.VisitProperty(property);
Context.Property = null;

return ret;
}

public override bool VisitEnumDecl(Enumeration @enum)
Expand Down
31 changes: 25 additions & 6 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,12 @@ private void GatherClassInternalFunctions(Class @class, bool includeCtors,
private IEnumerable<string> GatherInternalParams(Function function, out TypePrinterResult retType)
{
TypePrinter.PushContext(TypePrinterContextKind.Native);

retType = function.ReturnType.Visit(TypePrinter);

TypePrinter.Function = function;
var @params = function.GatherInternalParams(Context.ParserOptions.IsItaniumLikeAbi).Select(p =>
$"{p.Visit(TypePrinter)} {p.Name}").ToList();
TypePrinter.Function = null;

TypePrinter.PopContext();

Expand Down Expand Up @@ -995,7 +996,9 @@ private bool GenerateFunctionSetter(Class @class, Property property)
var actualProperty = GetActualProperty(property, @class);
if (actualProperty == null)
{
TypePrinter.Property = property;
WriteLine($@"throw new MissingMethodException(""Method {property.Name} missing from explicit specialization {@class.Visit(TypePrinter)}."");");
TypePrinter.Property = null;
return false;
}
property = actualProperty;
Expand Down Expand Up @@ -1042,7 +1045,8 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
{
Parameter = param,
ArgName = param.Name,
ReturnVarName = returnVar
ReturnVarName = returnVar,
Field = field,
};
ctx.PushMarshalKind(MarshalKind.NativeField);

Expand Down Expand Up @@ -1141,7 +1145,8 @@ private void GenerateIndexerSetter(Function function)
ParameterIndex = function.Parameters.Count(
p => p.Kind != ParameterKind.IndirectReturnType),
ReturnType = new QualifiedType(type),
ArgName = "value"
ArgName = "value",
Function = function,
};
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
type.Visit(marshal);
Expand Down Expand Up @@ -1282,7 +1287,7 @@ private bool GenerateVariableGetter(Variable var)
var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{
ArgName = var.Name,
ReturnType = var.QualifiedType
ReturnType = var.QualifiedType,
};
ctx.PushMarshalKind(MarshalKind.ReturnVariableArray);

Expand Down Expand Up @@ -1333,7 +1338,9 @@ private bool GenerateFunctionGetter(Class @class, Property property)
var actualProperty = GetActualProperty(property, @class);
if (actualProperty == null)
{
TypePrinter.Property = property;
WriteLine($@"throw new MissingMethodException(""Method {property.Name} missing from explicit specialization {@class.Visit(TypePrinter)}."");");
TypePrinter.Property = null;
return false;
}
QualifiedType type = default;
Expand Down Expand Up @@ -1405,7 +1412,8 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
{
ArgName = field.Name,
ReturnVarName = returnVar,
ReturnType = returnType
ReturnType = returnType,
Field = field,
};
ctx.PushMarshalKind(MarshalKind.NativeField);

Expand Down Expand Up @@ -1550,7 +1558,9 @@ private void GenerateProperties(Class @class)
}

GenerateDeclarationCommon(prop);
TypePrinter.Property = prop;
var printedType = prop.Type.Visit(TypePrinter);
TypePrinter.Property = null;
if (prop.ExplicitInterfaceImpl == null)
{
Write(Helpers.GetAccess(prop.Access));
Expand Down Expand Up @@ -1926,7 +1936,8 @@ private void GenerateVTableManagedCall(Method method)
ReturnType = param.QualifiedType,
ReturnVarName = param.Name,
ParameterIndex = i,
Parameter = param
Parameter = param,
Function = method,
};

ctx.PushMarshalKind(MarshalKind.GenericDelegate);
Expand Down Expand Up @@ -2071,7 +2082,9 @@ private void GenerateVTableMethodDelegates(Class @class, Method method)

using (WriteBlock($"private static {retType} {vTableMethodDelegateName}Hook({string.Join(", ", @params)})"))
{
TypePrinter.Function = method;
WriteLine($@"var {Helpers.TargetIdentifier} = {@class.Visit(TypePrinter)}.__GetInstance({Helpers.InstanceField});");
TypePrinter.Function = null;
GenerateVTableManagedCall(method);
}

Expand Down Expand Up @@ -2735,12 +2748,16 @@ private bool GenerateMethodBody(Class @class, Method method,
m => m.InstantiatedFrom == (method.OriginalFunction ?? method));
if (specializedMethod == null)
{
TypePrinter.Function = method;
WriteLine($@"throw new MissingMethodException(""Method {method.Name} missing from explicit specialization {@class.Visit(TypePrinter)}."");");
TypePrinter.Function = null;
return false;
}
if (specializedMethod.Ignore)
{
TypePrinter.Function = method;
WriteLine($@"throw new MissingMethodException(""Method {method.Name} ignored in specialization {@class.Visit(TypePrinter)}."");");
TypePrinter.Function = null;
return false;
}

Expand Down Expand Up @@ -2950,6 +2967,7 @@ private void GenerateOperator(Method method, QualifiedType returnType)
parameter.Type.IsPrimitiveTypeConvertibleToRef() ?
"ref *" : string.Empty,
parameter.Name);
TypePrinter.Function = method;
var printedType = method.ConversionType.Visit(TypePrinter);
if (@interface != null)
{
Expand All @@ -2958,6 +2976,7 @@ private void GenerateOperator(Method method, QualifiedType returnType)
}
else
WriteLine($"return new {printedType}({paramName});");
TypePrinter.Function = null;
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion src/Generator/Generators/CSharp/CSharpTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
Kind = ContextKind,
MarshalKind = MarshalKind,
Type = typedef,
Parameter = Parameter
Parameter = Parameter,
};

return typeMap.CSharpSignatureType(typePrinterContext).ToString();
Expand Down Expand Up @@ -698,7 +698,9 @@ private static string GetParameterUsage(ParameterUsage usage)
public override TypePrinterResult VisitFieldDecl(Field field)
{
PushMarshalKind(MarshalKind.NativeField);
Field = field;
var fieldTypePrinted = field.QualifiedType.Visit(this);
Field = null;
PopMarshalKind();

var returnTypePrinter = new TypePrinterResult();
Expand Down
8 changes: 8 additions & 0 deletions src/Generator/Generators/ITypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public class TypePrinterContext
public MarshalKind MarshalKind;
public Declaration Declaration;
public Parameter Parameter;
public Property Property;
private Field field;
public Field Field
{
get => field ?? Property.Field;
set => field = value;
}
public Method Method;
public Type Type;

public TypePrinterContext() : this(TypePrinterContextKind.Normal)
Expand Down
1 change: 0 additions & 1 deletion src/Generator/Generators/Marshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public MarshalContext(BindingContext context, uint indentation)

public string ArgName { get; set; }
public int ParameterIndex { get; set; }
public Function Function { get; set; }

public uint Indentation { get; }
}
Expand Down
11 changes: 11 additions & 0 deletions src/Generator/Generators/TypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.M

public Parameter Parameter;

private Field field;
public Field Field
{
get => field ?? Property.Field;
set => field = value;
}

public Property Property;

public Function Function;

#region Dummy implementations

public virtual string ToString(CppSharp.AST.Type type)
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Passes/ValidateOperatorsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ private bool IsValidOperatorOverload(Method @operator)
new TypePrinterContext
{
Parameter = parameter,
Type = type
Type = type,
Method = @operator,
});
var cilType = mappedTo as CILType;
if (cilType?.Type == typeof(int))
Expand Down

0 comments on commit 38bc0e4

Please sign in to comment.