public override TypePrinterResult VisitFieldDecl(Field field) { var cSharpSourcesDummy = new CSharpSources(Context, new List <TranslationUnit>()); var safeIdentifier = cSharpSourcesDummy.SafeIdentifier(field.Name); if (safeIdentifier.All(c => c.Equals('_'))) { safeIdentifier = cSharpSourcesDummy.SafeIdentifier(field.Name); } PushMarshalKind(MarshalKind.NativeField); var fieldTypePrinted = field.QualifiedType.Visit(this); PopMarshalKind(); var returnTypePrinter = new TypePrinterResult(); if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix)) { returnTypePrinter.NameSuffix = fieldTypePrinted.NameSuffix; } returnTypePrinter.Type = $"{fieldTypePrinted.Type} {safeIdentifier}"; return(returnTypePrinter); }
public static void GenerateMember(this CSharpSources gen, Class @class, Action <Class> generate, bool isVoid = false) { if (@class != null && @class.IsDependent) { foreach (var parameter in @class.TemplateParameters) { gen.WriteLine($"var __{parameter.Name} = typeof({parameter.Name});"); } foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated)) { WriteTemplateSpecializationCheck(gen, @class, specialization); gen.WriteOpenBraceAndIndent(); generate(specialization); if (isVoid) { gen.WriteLine("return;"); } gen.UnindentAndWriteCloseBrace(); } ThrowException(gen, @class); } else { generate(@class); } }
private static void WriteTemplateSpecializationCheck(CSharpSources gen, Class @class, ClassTemplateSpecialization specialization) { gen.WriteLine("if ({0})", string.Join(" && ", Enumerable.Range(0, @class.TemplateParameters.Count).Select( i => string.Format("__{0}.IsAssignableFrom(typeof({1}))", @class.TemplateParameters[i].Name, specialization.Arguments[i].Type.Type.Desugar())))); }
public override List<Template> Generate(IEnumerable<TranslationUnit> units) { var outputs = new List<Template>(); var template = new CSharpSources(Driver, units, typePrinter, expressionPrinter); outputs.Add(template); return outputs; }
public override List <Template> Generate(IEnumerable <TranslationUnit> units) { var outputs = new List <Template>(); var template = new CSharpSources(Context, units, typePrinter, expressionPrinter); outputs.Add(template); return(outputs); }
private static void WriteTemplateSpecializationCheck(CSharpSources gen, Class @class, ClassTemplateSpecialization specialization) { gen.WriteLine("if ({0})", string.Join(" && ", Enumerable.Range(0, @class.TemplateParameters.Count).Select( i => { CppSharp.AST.Type type = specialization.Arguments[i].Type.Type; return(type.IsPointerToPrimitiveType() && !type.IsConstCharString() ? $"__{@class.TemplateParameters[i].Name}.FullName == \"System.IntPtr\"" : $"__{@class.TemplateParameters[i].Name}.IsAssignableFrom(typeof({type}))"); }))); }
public static void DisableTypeMap(this CSharpSources gen, Class @class) { var mapped = @class.OriginalClass ?? @class; DisableSingleTypeMap(mapped, gen.Context); if (mapped.IsDependent) { foreach (var specialization in mapped.Specializations) { DisableSingleTypeMap(specialization, gen.Context); } } }
public override List <CodeGenerator> Generate(IEnumerable <TranslationUnit> units) { var outputs = new List <CodeGenerator>(); var gen = new CSharpSources(Context, units) { TypePrinter = typePrinter }; outputs.Add(gen); return(outputs); }
private static void ThrowException(CSharpSources gen, Class @class) { var typePrinter = new CSharpTypePrinter(gen.Context); var supportedTypes = string.Join(", ", @class.Specializations.Where(s => !s.Ignore).Select(s => $@"<{string.Join(", ", s.Arguments.Select(a => typePrinter.VisitTemplateArgument(a)))}>")); var typeArguments = string.Join(", ", @class.TemplateParameters.Select(p => p.Name)); var managedTypes = string.Join(", ", @class.TemplateParameters.Select(p => $"typeof({p.Name}).FullName")); gen.WriteLine($"throw new ArgumentOutOfRangeException(\"{typeArguments}\", " + $@"string.Join("", "", new[] {{ {managedTypes} }}), " + $"\"{@class.Visit(typePrinter)} maps a C++ template class and therefore it only supports a limited set of types and their subclasses: {supportedTypes}.\");"); }
public static void GenerateNativeConstructorsByValue( this CSharpSources gen, Class @class) { var printedClass = @class.Visit(gen.TypePrinter); if (@class.IsDependent) { foreach (var specialization in @class.GetSpecializationsToGenerate().Where(s => !s.Ignore)) { gen.GenerateNativeConstructorByValue(specialization, printedClass.Type); } } else { gen.GenerateNativeConstructorByValue(@class, printedClass.Type); } }
public static void GenerateNativeConstructorsByValue( this CSharpSources gen, Class @class) { var printedClass = @class.Visit(gen.TypePrinter); if (@class.IsDependent) { foreach (var specialization in (from s in @class.GetSpecializedClassesToGenerate() where s.IsGenerated select s).KeepSingleAllPointersSpecialization()) { gen.GenerateNativeConstructorByValue(specialization, printedClass); } } else { gen.GenerateNativeConstructorByValue(@class, printedClass); } }
public static void GenerateField(this CSharpSources gen, Class @class, Field field, Action <Field, Class, QualifiedType> generate, bool isVoid) { if (@class.IsDependent) { if (@class.Fields.Any(f => f.Type.IsDependent)) { foreach (var parameter in @class.TemplateParameters) { gen.WriteLine($"var __{parameter.Name} = typeof({parameter.Name});"); } foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated)) { WriteTemplateSpecializationCheck(gen, @class, specialization); gen.WriteOpenBraceAndIndent(); var specializedField = specialization.Fields.First( f => f.OriginalName == field.OriginalName); generate(specializedField, specialization, field.QualifiedType); if (isVoid) { gen.WriteLine("return;"); } gen.UnindentAndWriteCloseBrace(); } ThrowException(gen, @class); } else { var specialization = @class.Specializations[0]; var specializedField = specialization.Fields.First( f => f.OriginalName == field.OriginalName); generate(specializedField, specialization, field.QualifiedType); } } else { generate(field, @class.IsDependent ? @class.Specializations[0] : @class, field.QualifiedType); } }
public static void GenerateNativeConstructorsByValue( this CSharpSources gen, Class @class) { var printedClass = @class.Visit(gen.TypePrinter); if (@class.IsDependent) { IEnumerable <Class> specializations = @class.GetSpecializedClassesToGenerate().Where(s => s.IsGenerated); if (@class.IsTemplate) { specializations = specializations.KeepSingleAllPointersSpecialization(); } foreach (var specialization in specializations) { gen.GenerateNativeConstructorByValue(specialization, printedClass); } } else { gen.GenerateNativeConstructorByValue(@class, printedClass); } }
private string SafeIdentifier(string id) => Options.IsCLIGenerator ? id : CSharpSources.SafeIdentifier(id);