Skip to content

Commit 6dec97f

Browse files
committed
Fixed the generated C# for public fields with type a dependent pointer.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 4b31087 commit 6dec97f

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

+29-16
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,8 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
896896
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
897897
ctx.Declaration = field;
898898

899-
var arrayType = field.Type.Desugar() as ArrayType;
899+
Type type = field.Type.Desugar();
900+
var arrayType = type as ArrayType;
900901

901902
if (arrayType != null && @class.IsValueType)
902903
{
@@ -923,13 +924,20 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
923924

924925
if (marshal.Context.Return.StringBuilder.Length > 0)
925926
{
926-
WriteLine("{0} = {1}{2};", ctx.ReturnVarName,
927-
field.Type.IsPointer() && field.Type.GetFinalPointee().IsPrimitiveType() &&
928-
!CSharpTypePrinter.IsConstCharString(field.Type) ?
929-
string.Format("({0}) ", CSharpTypePrinter.IntPtrType) :
930-
string.Empty,
931-
marshal.Context.Return);
932-
927+
Write($"{ctx.ReturnVarName} = ");
928+
if (type.IsPointer())
929+
{
930+
Type pointee = type.GetFinalPointee();
931+
if (pointee.IsPrimitiveType() &&
932+
!CSharpTypePrinter.IsConstCharString(type))
933+
{
934+
Write($"({CSharpTypePrinter.IntPtrType}) ");
935+
var templateSubstitution = pointee.Desugar(false) as TemplateParameterSubstitutionType;
936+
if (templateSubstitution != null)
937+
Write($"(object) ");
938+
}
939+
}
940+
WriteLine($"{marshal.Context.Return};");
933941
}
934942

935943
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
@@ -1191,21 +1199,26 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
11911199
if (ctx.HasCodeBlock)
11921200
PushIndent();
11931201

1202+
Write("return ");
1203+
11941204
var @return = marshal.Context.Return.ToString();
11951205
if (field.Type.IsPointer())
11961206
{
1197-
var final = field.Type.GetFinalPointee().Desugar();
1198-
if (final.IsPrimitiveType() && !final.IsPrimitiveType(PrimitiveType.Void) &&
1207+
var final = field.Type.GetFinalPointee().Desugar(resolveTemplateSubstitution: false);
1208+
var templateSubstitution = final as TemplateParameterSubstitutionType;
1209+
if (templateSubstitution != null)
1210+
Write($"({templateSubstitution.ReplacedParameter.Parameter.Name}) (object) ");
1211+
if ((final.IsPrimitiveType() && !final.IsPrimitiveType(PrimitiveType.Void) &&
11991212
(!final.IsPrimitiveType(PrimitiveType.Char) &&
12001213
!final.IsPrimitiveType(PrimitiveType.WideChar) ||
12011214
(!Context.Options.MarshalCharAsManagedChar &&
1202-
!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst)))
1203-
@return = string.Format("({0}*) {1}", field.Type.GetPointee().Desugar(), @return);
1204-
if (!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst &&
1205-
final.IsPrimitiveType(PrimitiveType.WideChar))
1206-
@return = string.Format("({0}*) {1}", field.Type.GetPointee().Desugar(), @return);
1215+
!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst)) &&
1216+
templateSubstitution == null) ||
1217+
(!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst &&
1218+
final.IsPrimitiveType(PrimitiveType.WideChar)))
1219+
Write($"({field.Type.GetPointee().Desugar()}*) ");
12071220
}
1208-
WriteLine("return {0};", @return);
1221+
WriteLine($"{@return};");
12091222

12101223
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
12111224
WriteCloseBraceIndent();

src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public static void GenerateField(this CSharpSources gen, Class @class,
3535
{
3636
if (@class.IsDependent)
3737
{
38-
if (@class.Fields.Any(f => f.Type.Desugar() is TemplateParameterType))
38+
if (@class.Fields.Any(f => f.Type.IsDependent))
3939
{
4040
foreach (var parameter in @class.TemplateParameters)
4141
gen.WriteLine($"var __{parameter.Name} = typeof({parameter.Name});");
4242

43-
foreach (var specialization in @class.Specializations.Where(s => !s.Ignore))
43+
foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated))
4444
{
4545
WriteTemplateSpecializationCheck(gen, @class, specialization);
4646
gen.WriteStartBraceIndent();

src/Generator/Passes/MultipleInheritancePass.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ where property.IsDeclared
135135
Namespace = @interface,
136136
Name = "Dispose",
137137
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
138-
SynthKind = FunctionSynthKind.InterfaceDispose
138+
SynthKind = FunctionSynthKind.InterfaceDispose,
139+
Mangled = string.Empty
139140
};
140141

141142
@interface.Methods.Add(dispose);

tests/CSharp/CSharp.Tests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,14 @@ public void TestExtensionsOfSpecializationsAsSecondaryBases()
904904
}
905905
}
906906

907+
[Test]
908+
public void TestFieldWithDependentPointerType()
909+
{
910+
using (var dependentPointerFields = new DependentPointerFields<float>())
911+
{
912+
}
913+
}
914+
907915
[Test]
908916
public void TestAbstractImplementatonsInPrimaryAndSecondaryBases()
909917
{

tests/CSharp/CSharpTemplates.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,22 @@ class DLL_API DerivedFromSpecializationOfUnsupportedTemplate : public DependentV
211211
template <typename T>
212212
class DLL_API DependentPointerFields
213213
{
214-
private:
214+
public:
215+
DependentPointerFields();
216+
~DependentPointerFields();
215217
T* field;
216218
};
217219

220+
template <typename T>
221+
DependentPointerFields<T>::DependentPointerFields()
222+
{
223+
}
224+
225+
template <typename T>
226+
DependentPointerFields<T>::~DependentPointerFields()
227+
{
228+
}
229+
218230
template <typename K, typename V>
219231
class TwoTemplateArgs
220232
{
@@ -576,7 +588,8 @@ void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool>
576588
TemplateWithIndexer<int> _10, TemplateWithIndexer<T1> _11,
577589
TemplateWithIndexer<T2*> _12, TemplateWithIndexer<UsedInTemplatedIndexer> _13,
578590
TemplateDerivedFromRegularDynamic<RegularDynamic> _14,
579-
IndependentFields<OnlySpecialisedInTypeArg<double>> _15, std::string s);
591+
IndependentFields<OnlySpecialisedInTypeArg<double>> _15,
592+
DependentPointerFields<float> _16, std::string s);
580593

581594
void hasIgnoredParam(DependentValueFields<IndependentFields<Ignored>> ii);
582595

@@ -592,6 +605,7 @@ template class DLL_API IndependentFields<std::string>;
592605
template class DLL_API Base<int>;
593606
template class DLL_API DependentValueFields<int>;
594607
template class DLL_API DependentValueFields<float>;
608+
template class DLL_API DependentPointerFields<float>;
595609
template class DLL_API VirtualTemplate<int>;
596610
template class DLL_API VirtualTemplate<bool>;
597611
template class DLL_API HasDefaultTemplateArgument<int, int>;

0 commit comments

Comments
 (0)