public FieldDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(container, 0) { if (parameter.Hidden) accessModifier = AccessModifierType.Private; string fieldType; if (parameter.ArraySizes.Any()) fieldType = new ArrayWrapperDeclararation(parameter, container, accessModifier).TypeName; else fieldType = parameter.TypeDeclaration; WriteLine(() => accessModifier + " " + fieldType + " " + parameter.Name + ";"); }
public MethodDeclaration(MethodDesc method, BaseTypeDeclaration container, AccessModifierType accessModifier, bool isNew = false, TextBlock code = null, string nameOverride = null) : base(container, 0) { string additionalModifier = (accessModifier == AccessModifierType.Empty ? "" : " ") + ((method.Dll != null) ? "static extern " : "") + "unsafe " + (isNew ? "new " : ""); if (code == null) { if (method.Dll != null) WriteLine(() => "[DllImport(\"" + method.Dll + "\", EntryPoint = \"" + method.EntryPoint + "\", CallingConvention = CallingConvention." + method.Convention.ToString() + ", CharSet = CharSet.Unicode, SetLastError = true)]"); else WriteLine(() => "[PreserveSig]"); } WriteLine(() => accessModifier + additionalModifier + GetReturnValue(method) + " " + ((nameOverride == null) ? method.Name : nameOverride) + "(" + GetParameters(method) + ")" + ((code == null) ? ";" : "")); if (code != null) AddTextBlock(code); }
public FieldDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(container, 0) { if (parameter.Hidden) { accessModifier = AccessModifierType.Private; } string fieldType; if (parameter.ArraySizes.Any()) { fieldType = new ArrayWrapperDeclararation(parameter, container, accessModifier).TypeName; } else { fieldType = parameter.TypeDeclaration; } WriteLine(() => accessModifier + " " + fieldType + " " + parameter.Name + ";"); }
public MethodDeclaration(MethodDesc method, BaseTypeDeclaration container, AccessModifierType accessModifier, bool isNew = false, TextBlock code = null, string nameOverride = null) : base(container, 0) { string additionalModifier = (accessModifier == AccessModifierType.Empty ? "" : " ") + ((method.Dll != null) ? "static extern " : "") + "unsafe " + (isNew ? "new " : ""); if (code == null) { if (method.Dll != null) { WriteLine(() => "[DllImport(\"" + method.Dll + "\", EntryPoint = \"" + method.EntryPoint + "\", CallingConvention = CallingConvention." + method.Convention.ToString() + ", CharSet = CharSet.Unicode, SetLastError = true)]"); } else { WriteLine(() => "[PreserveSig]"); } } WriteLine(() => accessModifier + additionalModifier + GetReturnValue(method) + " " + ((nameOverride == null) ? method.Name : nameOverride) + "(" + GetParameters(method) + ")" + ((code == null) ? ";" : "")); if (code != null) { AddTextBlock(code); } }
public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, int arraySize) : base(container, 0) { WriteLine(() => accessModifier + " fixed " + type.Name + " " + name + "[" + arraySize + "];"); }
public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(container, 0) { WriteLine(() => accessModifier + " " + type.Name + " " + name + ";"); }
public ConstDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(container, 0) { WriteLine(() => accessModifier + " const " + ((parameter.Value is string) ? "string" : parameter.TypeDeclaration) + " " + parameter.Name + " = " + GetValueInitializer(parameter.Value) + ";"); }
public ConstDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, object value) : base(container, 0) { WriteLine(() => accessModifier + " const " + type.Name + " " + name + " = " + GetValueInitializer(value) + ";"); }
public ArrayWrapperDeclararation(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(parameter.Type, container, accessModifier) { this.arraySizes = parameter.ArraySizes; int totalLength = 1; int[] sizes = arraySizes.ToArray(); List <string> lengthConstants = new List <string>(); List <string> indexerParameters = new List <string>(); for (int i = 0; i < sizes.Length; i++) { totalLength *= sizes[i]; lengthConstants.Add("length" + i.ToString()); indexerParameters.Add("index" + i.ToString()); } List <string> parts = new List <string>(); for (int i = 0; i < sizes.Length; i++) { List <string> multipliers = new List <string>(); multipliers.Add(indexerParameters[i]); for (int j = i; j < (sizes.Length - 1); j++) { multipliers.Add(lengthConstants[j]); } parts.Add(string.Join(" * ", multipliers)); } string index = string.Join(" + ", parts); bool canBeFixedArray = IsPrimitiveType(parameter.Type.Name) && (parameter.IndirectionLevel == 0); WriteBaseLine(() => "{"); for (int i = 0; i < sizes.Length; i++) { AddTextBlock(new ConstDeclaration(new TypeDesc("int"), lengthConstants[i], this, AccessModifierType.Private, sizes[i])); } WriteLine(); if (!canBeFixedArray) { for (int i = 0; i < totalLength; i++) { AddTextBlock(new FieldDeclaration(parameter.Type, "data" + i.ToString(), this, AccessModifierType.Private)); } } else { AddTextBlock(new FieldDeclaration(parameter.Type, "data", this, AccessModifierType.Private, totalLength)); } WriteLine(); WriteLine(() => AccessModifier + " unsafe " + parameter.Type.Name + " this[" + string.Join(", ", indexerParameters.Select(x => "int " + x)) + "]"); WriteLine(() => "{"); WriteLine(() => " get"); WriteLine(() => " {"); WriteLine(() => " fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")"); WriteLine(() => " {"); WriteLine(() => " return pointer[" + index + "];"); WriteLine(() => " }"); WriteLine(() => " }"); WriteLine(() => " set"); WriteLine(() => " {"); WriteLine(() => " fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")"); WriteLine(() => " {"); WriteLine(() => " pointer[" + index + "] = value;"); WriteLine(() => " }"); WriteLine(() => " }"); WriteLine(() => "}"); WriteLine(); WriteLine(() => "public static unsafe implicit operator " + parameter.Type.Name + "[](" + TypeName + " value)"); WriteLine(() => "{"); WriteLine(() => " " + parameter.Type.Name + "[] result = new " + parameter.Type.Name + "[" + totalLength + "];"); WriteLine(() => " " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "value.data" : "&value.data0") + ";"); WriteLine(() => " {"); WriteLine(() => " for (int i = 0; i < " + totalLength + "; i++)"); WriteLine(() => " result[i] = pointer[i];"); WriteLine(() => " }"); WriteLine(() => " return result;"); WriteLine(() => "}"); WriteLine(); WriteLine(() => "public static unsafe implicit operator " + TypeName + "(" + parameter.Type.Name + "[] value)"); WriteLine(() => "{"); WriteLine(() => " if (value == null)"); WriteLine(() => " throw new ArgumentNullException(\"value\");"); WriteLine(() => " " + TypeName + " result = new " + TypeName + "();"); WriteLine(() => " " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "result.data" : "&result.data0") + ";"); WriteLine(() => " for (int i = 0; i < ((value.Length < " + totalLength + ") ? value.Length : " + totalLength + "); i++)"); WriteLine(() => " pointer[i] = value[i];"); WriteLine(() => " return result;"); WriteLine(() => "}"); WriteBaseLine(() => "}"); }
public ArrayWrapperDeclararation(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(parameter.Type, container, accessModifier) { this.arraySizes = parameter.ArraySizes; int totalLength = 1; int[] sizes = arraySizes.ToArray(); List<string> lengthConstants = new List<string>(); List<string> indexerParameters = new List<string>(); for(int i = 0; i < sizes.Length; i++) { totalLength *= sizes[i]; lengthConstants.Add("length" + i.ToString()); indexerParameters.Add("index" + i.ToString()); } List<string> parts = new List<string>(); for (int i = 0; i < sizes.Length; i++) { List<string> multipliers = new List<string>(); multipliers.Add(indexerParameters[i]); for (int j = i; j < (sizes.Length - 1); j++) multipliers.Add(lengthConstants[j]); parts.Add(string.Join(" * ", multipliers)); } string index = string.Join(" + ", parts); bool canBeFixedArray = IsPrimitiveType(parameter.Type.Name) && (parameter.IndirectionLevel == 0); WriteBaseLine(() => "{"); for (int i = 0; i < sizes.Length; i++) AddTextBlock(new ConstDeclaration(new TypeDesc("int"), lengthConstants[i], this, AccessModifierType.Private, sizes[i])); WriteLine(); if (!canBeFixedArray) { for (int i = 0; i < totalLength; i++) AddTextBlock(new FieldDeclaration(parameter.Type, "data" + i.ToString(), this, AccessModifierType.Private)); } else AddTextBlock(new FieldDeclaration(parameter.Type, "data", this, AccessModifierType.Private, totalLength)); WriteLine(); WriteLine(() => AccessModifier + " unsafe " + parameter.Type.Name + " this[" + string.Join(", ", indexerParameters.Select(x => "int " + x)) + "]"); WriteLine(() => "{"); WriteLine(() => " get"); WriteLine(() => " {"); WriteLine(() => " fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")"); WriteLine(() => " {"); WriteLine(() => " return pointer[" + index + "];"); WriteLine(() => " }"); WriteLine(() => " }"); WriteLine(() => " set"); WriteLine(() => " {"); WriteLine(() => " fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")"); WriteLine(() => " {"); WriteLine(() => " pointer[" + index + "] = value;"); WriteLine(() => " }"); WriteLine(() => " }"); WriteLine(() => "}"); WriteLine(); WriteLine(() => "public static unsafe implicit operator " + parameter.Type.Name + "[](" + TypeName + " value)"); WriteLine(() => "{"); WriteLine(() => " " + parameter.Type.Name + "[] result = new " + parameter.Type.Name + "[" + totalLength + "];"); WriteLine(() => " " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "value.data" : "&value.data0") + ";"); WriteLine(() => " {"); WriteLine(() => " for (int i = 0; i < " + totalLength + "; i++)"); WriteLine(() => " result[i] = pointer[i];"); WriteLine(() => " }"); WriteLine(() => " return result;"); WriteLine(() => "}"); WriteLine(); WriteLine(() => "public static unsafe implicit operator " + TypeName + "(" + parameter.Type.Name + "[] value)"); WriteLine(() => "{"); WriteLine(() => " if (value == null)"); WriteLine(() => " throw new ArgumentNullException(\"value\");"); WriteLine(() => " " + TypeName + " result = new " + TypeName + "();"); WriteLine(() => " " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "result.data" : "&result.data0") + ";"); WriteLine(() => " for (int i = 0; i < ((value.Length < " + totalLength + ") ? value.Length : " + totalLength + "); i++)"); WriteLine(() => " pointer[i] = value[i];"); WriteLine(() => " return result;"); WriteLine(() => "}"); WriteBaseLine(() => "}"); }