Browse Source

Simplified the method for generating a variable in C#.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/955/head
Dimitar Dobrev 9 years ago
parent
commit
65afb2628c
  1. 38
      src/Generator/Generators/CSharp/CSharpSources.cs

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

@ -743,18 +743,17 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateVariableSetter<T>(T decl) where T : Declaration, ITypedDecl private void GenerateVariableSetter(Variable var)
{ {
var var = decl as Variable;
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
var location = $@"CppSharp.SymbolResolver.ResolveSymbol(""{ var location = $@"CppSharp.SymbolResolver.ResolveSymbol(""{
GetLibraryOf(decl)}"", ""{var.Mangled}"")"; GetLibraryOf(var)}"", ""{var.Mangled}"")";
string ptr = Generator.GeneratedIdentifier("ptr"); string ptr = Generator.GeneratedIdentifier("ptr");
var arrayType = decl.Type as ArrayType; Type varType = var.Type.Desugar();
var @class = decl.Namespace as Class; var arrayType = varType as ArrayType;
var @class = var.Namespace as Class;
var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType; var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType;
if (isRefTypeArray) if (isRefTypeArray)
WriteLine($@"var {ptr} = { WriteLine($@"var {ptr} = {
@ -762,26 +761,26 @@ namespace CppSharp.Generators.CSharp
arrayType.QualifiedType.Qualifiers.IsConst ? arrayType.QualifiedType.Qualifiers.IsConst ?
string.Empty : "(byte*)")}{location};"); string.Empty : "(byte*)")}{location};");
else else
WriteLine($"var {ptr} = ({var.Type}*){location};"); WriteLine($"var {ptr} = ({varType}*){location};");
TypePrinter.PopContext(); TypePrinter.PopContext();
var param = new Parameter var param = new Parameter
{ {
Name = "value", Name = "value",
QualifiedType = decl.QualifiedType QualifiedType = var.QualifiedType
}; };
var ctx = new CSharpMarshalContext(Context) var ctx = new CSharpMarshalContext(Context)
{ {
Parameter = param, Parameter = param,
ArgName = param.Name, ArgName = param.Name,
ReturnType = new QualifiedType(var.Type) ReturnType = new QualifiedType(varType)
}; };
ctx.PushMarshalKind(MarshalKind.Variable); ctx.PushMarshalKind(MarshalKind.Variable);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
decl.QualifiedType.Visit(marshal); var.QualifiedType.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before)) if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before); Write(marshal.Context.Before);
@ -1022,19 +1021,18 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateVariableGetter<T>(T decl) where T : Declaration, ITypedDecl private void GenerateVariableGetter(Variable var)
{ {
var var = decl as Variable;
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
string library = GetLibraryOf(decl); string library = GetLibraryOf(var);
var location = $"CppSharp.SymbolResolver.ResolveSymbol(\"{library}\", \"{var.Mangled}\")"; var location = $"CppSharp.SymbolResolver.ResolveSymbol(\"{library}\", \"{var.Mangled}\")";
var ptr = Generator.GeneratedIdentifier("ptr"); var ptr = Generator.GeneratedIdentifier("ptr");
var arrayType = decl.Type as ArrayType; Type varType = var.Type.Desugar();
var @class = decl.Namespace as Class; var arrayType = varType as ArrayType;
var @class = var.Namespace as Class;
var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType; var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType;
if (isRefTypeArray) if (isRefTypeArray)
{ {
@ -1046,16 +1044,16 @@ namespace CppSharp.Generators.CSharp
else else
{ {
TypePrinter.PushMarshalKind(MarshalKind.ReturnVariableArray); TypePrinter.PushMarshalKind(MarshalKind.ReturnVariableArray);
var varType = var.Type.Visit(TypePrinter); var varReturnType = varType.Visit(TypePrinter);
TypePrinter.PopMarshalKind(); TypePrinter.PopMarshalKind();
WriteLine($"var {ptr} = ({varType}*){location};"); WriteLine($"var {ptr} = ({varReturnType}*){location};");
} }
TypePrinter.PopContext(); TypePrinter.PopContext();
var ctx = new CSharpMarshalContext(Context) var ctx = new CSharpMarshalContext(Context)
{ {
ArgName = decl.Name, ArgName = var.Name,
ReturnVarName = (isRefTypeArray || ReturnVarName = (isRefTypeArray ||
(arrayType != null && arrayType.Type.Desugar().IsPrimitiveType()) ? string.Empty : "*") (arrayType != null && arrayType.Type.Desugar().IsPrimitiveType()) ? string.Empty : "*")
+ Generator.GeneratedIdentifier("ptr"), + Generator.GeneratedIdentifier("ptr"),
@ -1063,7 +1061,7 @@ namespace CppSharp.Generators.CSharp
}; };
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx); var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
decl.QualifiedType.Visit(marshal); var.QualifiedType.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before)) if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before); Write(marshal.Context.Before);

Loading…
Cancel
Save