private static void WriteMember(CsCodeWriter cw, TypeNameMappings tnm, MemberSpec member, string nameSuffix) { if (!string.IsNullOrEmpty(member.Comment)) { cw.WriteLine($"///<summary>{member.Comment}</summary>"); } cw.WriteLine($"public {member.Type.MapTypeSpec(tnm)} {Util.NormalizeFieldName(member.Name)}{nameSuffix};"); }
private static void WriteMemberSymbolicCount(CsCodeWriter cw, TypeNameMappings tnm, MemberSpec member, ConstantDefinition[] constants) { if (!CanUseFixed(member.Type.MapTypeSpec(tnm))) { int count = GetSymbolValue(member.ElementCountSymbolic, constants); for (int i = 0; i < count; i++) { WriteMember(cw, tnm, member, "_" + i); } } else { if (!string.IsNullOrEmpty(member.Comment)) { cw.WriteLine($"///<summary>{member.Comment}</summary>"); } string mappedSymbolicName = EnumHelpers.GetPrettyEnumName(member.ElementCountSymbolic, "VK_"); cw.WriteLine($"public fixed {member.Type.MapTypeSpec(tnm)} {Util.NormalizeFieldName(member.Name)}[(int)VulkanNative.{mappedSymbolicName}];"); } }
private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { SpaceSeparatedList(cw, spec.Constants, constant => { ConstantHelpers.WriteConstant(cw, tnm, constant); }); cw.WriteLine(); SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { if (Configuration.GenerateCalliStubs) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } else { cw.WriteLine($"IntPtr {command.Name}_nativePtr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); using (cw.PushBlock($"if ({command.Name}_nativePtr != IntPtr.Zero)")) { cw.WriteLine($"{command.Name}_ptr = Marshal.GetDelegateForFunctionPointer<{command.Name}_delegate>({command.Name}_nativePtr);"); } using (cw.PushBlock("else")) { string invocation = string.Join(", ", command.Parameters.Select(pd => Util.NormalizeFieldName(pd.Name))); cw.WriteLine($"{command.Name}_ptr = ({invocation}) => {{ throw CreateMissingFunctionException(); }};"); } } } } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } } }
public static void WriteCommand(CsCodeWriter cw, TypeNameMappings tnm, CommandDefinition command) { if (!command.IsVariant) { if (Configuration.GenerateCalliStubs) { cw.WriteLine($"private static IntPtr {command.Name}_ptr;"); } else { cw.WriteLine($"private delegate {command.ReturnType.MapTypeSpec(tnm)} {command.Name}_delegate({command.GetParametersSignature(tnm)});"); cw.WriteLine($"private static {command.Name}_delegate {command.Name}_ptr;"); } } if (command.SuccessCodes.Length != 0) { cw.WriteLine($"///<remarks>Success codes:{string.Join(", ", command.SuccessCodes)}. Error codes:{string.Join(", ", command.ErrorCodes)}</remarks>"); } if (Configuration.GenerateCalliStubs) { cw.WriteLine("[Generator.CalliRewrite]"); } using (cw.PushBlock($"public static unsafe {command.ReturnType.MapTypeSpec(tnm)} {command.Name}({command.GetParametersSignature(tnm)})")) { if (Configuration.GenerateCalliStubs) { cw.WriteLine("throw new NotImplementedException();"); } else { string invocation = string.Join(", ", command.Parameters.Select(pd => $"{pd.GetModifierString()}{Util.NormalizeFieldName(pd.Name)}")); string ret = (command.ReturnType.Name == "void") ? "" : "return "; cw.WriteLine($"{ret}{command.Name}_ptr({invocation});"); } } }
public string GetMappedAndNormalizedString(TypeNameMappings tnm) { return $"{GetModifierString()}{Type.MapTypeSpec(tnm)} {Util.NormalizeFieldName(Name)}"; }