예제 #1
0
        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 + ";");
        }
예제 #2
0
        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);
        }
예제 #3
0
        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 + ";");
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, int arraySize)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " fixed " + type.Name + " " + name + "[" + arraySize + "];");
 }
예제 #6
0
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " " + type.Name + " " + name + ";");
 }
예제 #7
0
 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) + ";");
 }
예제 #8
0
 public ConstDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, object value)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " const " + type.Name + " " + name + " = " + GetValueInitializer(value) + ";");
 }
예제 #9
0
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, int arraySize)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " fixed " + type.Name + " " + name + "[" + arraySize + "];");
 }
예제 #10
0
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " " + type.Name + " " + name + ";");
 }
예제 #11
0
 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) + ";");
 }
예제 #12
0
 public ConstDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, object value)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " const " + type.Name + " " + name + " = " + GetValueInitializer(value) + ";");
 }
예제 #13
0
        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(() => "}");
        }