Ejemplo n.º 1
0
        public string Finish(string indent)
        {
            string ret = "";

            foreach (Parameter p in parms.Keys)
            {
                if (parms [p] == false)
                {
                    continue;
                }

                IGeneratable igen = p.Generatable;

                if (igen is CallbackGen)
                {
                    continue;
                }
                else if (igen is StructBase || igen is ByRefGen)
                {
                    ret += indent + String.Format("if ({0} != IntPtr.Zero) System.Runtime.InteropServices.Marshal.StructureToPtr (my{0}, {0}, false);\n", p.Name);
                }
                else if (igen is IManualMarshaler)
                {
                    ret += String.Format("{0}{1} = {2};", indent, p.Name, (igen as IManualMarshaler).AllocNative("my" + p.Name));
                }
                else
                {
                    ret += indent + p.Name + " = " + igen.CallByName("my" + p.Name) + ";\n";
                }
            }

            return(ret);
        }
Ejemplo n.º 2
0
        string CallCount(string name)
        {
            string       result = CountCast + "(" + name + " == null ? 0 : " + name + ".Length)";
            IGeneratable gen    = SymbolTable.Table[count_elem.GetAttribute("type")];

            return(gen.CallByName(result));
        }
Ejemplo n.º 3
0
        public string CallByName(string c_type, string var_name)
        {
            IGeneratable gen = this[c_type];

            if (gen == null)
            {
                return("");
            }
            return(gen.CallByName(var_name));
        }
        public string Finish(string indent)
        {
            string ret = "";

            for (int i = 0; i < parms.Count; i++)
            {
                if ((bool)special[i] == false)
                {
                    continue;
                }

                Parameter    p    = parms [i] as Parameter;
                IGeneratable igen = p.Generatable;

                if (igen is CallbackGen)
                {
                    continue;
                }
                else if (igen is StructBase || igen is ByRefGen)
                {
                    ret += indent + String.Format("if ({0} != IntPtr.Zero) System.Runtime.InteropServices.Marshal.StructureToPtr (my{0}, {0}, false);\n", p.Name);
                }
                else if (igen is IManualMarshaler)
                {
                    ret += String.Format("{0}{1} = {2};", indent, p.Name, (igen as IManualMarshaler).AllocNative("my" + p.Name));
                }
                else if (p.Scope == "call")
                {
                    if (igen is ObjectBase || igen is OpaqueGen)
                    {
                        if (igen is ObjectBase)
                        {
                            ret += indent + "IntPtr dummy = my" + p.Name + ".OwnedHandle;\n";
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }

                        ret += indent + "my" + p.Name + ".Dispose();\n";
                    }
                }
                else
                {
                    ret += indent + p.Name + " = " + igen.CallByName("my" + p.Name) + ";\n";
                }
            }

            return(ret);
        }
Ejemplo n.º 5
0
        private string GenArgsInitialization(StreamWriter sw, IList <Parameter> dispose_params)
        {
            if (parms.Count > 0)
            {
                sw.WriteLine("\t\t\t\targs.Args = new object[" + parms.Count + "];");
            }
            string finish = "";

            for (int idx = 0; idx < parms.Count; idx++)
            {
                Parameter    p    = parms [idx];
                IGeneratable igen = p.Generatable;
                if (p.PassAs != "out")
                {
                    if (igen is ManualGen)
                    {
                        sw.WriteLine("\t\t\t\tif (arg{0} == IntPtr.Zero)", idx);
                        sw.WriteLine("\t\t\t\t\targs.Args[{0}] = null;", idx);
                        sw.WriteLine("\t\t\t\telse {");
                        sw.WriteLine("\t\t\t\t\targs.Args[" + idx + "] = " + p.FromNative("arg" + idx) + ";");
                        sw.WriteLine("\t\t\t\t}");
                    }
                    else if (dispose_params.Contains(p))
                    {
                        sw.WriteLine("\t\t\t\t" + p.Name + " = " + p.FromNative("arg" + idx) + ";");
                        sw.WriteLine("\t\t\t\targs.Args[" + idx + "] = " + p.Name + ";");
                    }
                    else
                    {
                        sw.WriteLine("\t\t\t\targs.Args[" + idx + "] = " + p.FromNative("arg" + idx) + ";");
                    }
                }
                if ((igen is StructBase || igen is ByRefGen) && p.PassAs != "")
                {
                    finish += "\t\t\t\tif (arg" + idx + " != IntPtr.Zero) System.Runtime.InteropServices.Marshal.StructureToPtr (args.Args[" + idx + "], arg" + idx + ", false);\n";
                }
                else if (igen is IManualMarshaler && p.PassAs != "")
                {
                    finish += string.Format("\t\t\t\targ{0} = {1};\n", idx, (igen as IManualMarshaler).AllocNative("args.Args[" + idx + "]"));
                }
                else if (p.PassAs != "")
                {
                    finish += "\t\t\t\targ" + idx + " = " + igen.CallByName("((" + p.CsType + ")args.Args[" + idx + "])") + ";\n";
                }
            }
            return(finish);
        }
Ejemplo n.º 6
0
        public string GetCallString(bool is_set)
        {
            if (parameters.Count == 0)
            {
                return(String.Empty);
            }

            string[] result = new string [parameters.Count];
            for (int i = 0; i < parameters.Count; i++)
            {
                Parameter    p    = parameters [i];
                IGeneratable igen = p.Generatable;

                bool is_prop = is_set && i == 0;

                if (i > 0 && parameters [i - 1].IsString && p.IsLength && p.PassAs == String.Empty)
                {
                    string string_name = (i == 1 && is_set) ? "value" : parameters [i - 1].Name;
                    result[i] = igen.CallByName(CastFromInt(p.CSType) + "System.Text.Encoding.UTF8.GetByteCount (" + string_name + ")");
                    continue;
                }

                if (is_prop)
                {
                    p.CallName = "value";
                }
                else
                {
                    p.CallName = p.Name;
                }
                string call_parm = p.CallString;

                if (p.IsUserData && parameters.IsHidden(p) && !parameters.HideData &&
                    (i == 0 || parameters [i - 1].Scope != "notified"))
                {
                    call_parm = "IntPtr.Zero";
                }

                result [i] += call_parm;
            }

            return(String.Join(", ", result));
        }
Ejemplo n.º 7
0
        public virtual void Generate(GenerationInfo gen_info, string indent)
        {
            if (Ignored || Hidden)
            {
                return;
            }

            CheckGlue();
            if ((getterName != null || setterName != null || getOffsetName != null) && gen_info.GlueWriter == null)
            {
                LogWriter log = new LogWriter(container_type.QualifiedName);
                log.Member = Name;
                log.Warn("needs glue for field access.  Specify --glue-filename");
                return;
            }

            GenerateImports(gen_info, indent);

            SymbolTable  table     = SymbolTable.Table;
            StreamWriter sw        = gen_info.Writer;
            string       modifiers = elem.HasAttribute("new_flag") ? "new " : "";
            bool         is_struct = table.IsStruct(CType) || table.IsBoxed(CType);

            sw.WriteLine(indent + "public " + modifiers + CSType + " " + Name + " {");

            if (Getter != null)
            {
                sw.Write(indent + "\tget ");
                Getter.GenerateBody(gen_info, container_type, "\t");
                sw.WriteLine("");
            }
            else if (getterName != null)
            {
                sw.WriteLine(indent + "\tget {");
                container_type.Prepare(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t\t" + CSType + " result = " + table.FromNative(ctype, getterName + " (" + container_type.CallByName() + ")") + ";");
                container_type.Finish(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t\treturn result;");
                sw.WriteLine(indent + "\t}");
            }
            else if (Readable && offsetName != null)
            {
                sw.WriteLine(indent + "\tget {");
                sw.WriteLine(indent + "\t\tunsafe {");
                if (is_struct)
                {
                    sw.WriteLine(indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\treturn *raw_ptr;");
                }
                else
                {
                    sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\treturn " + table.FromNative(ctype, "(*raw_ptr)") + ";");
                }
                sw.WriteLine(indent + "\t\t}");
                sw.WriteLine(indent + "\t}");
            }

            IGeneratable gen       = table [CType];
            string       to_native = (gen is IManualMarshaler) ? (gen as IManualMarshaler).AllocNative("value") : gen.CallByName("value");

            if (Setter != null)
            {
                sw.Write(indent + "\tset ");
                Setter.GenerateBody(gen_info, container_type, "\t");
                sw.WriteLine("");
            }
            else if (setterName != null)
            {
                sw.WriteLine(indent + "\tset {");
                container_type.Prepare(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t\t" + setterName + " (" + container_type.CallByName() + ", " + to_native + ");");
                container_type.Finish(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t}");
            }
            else if (Writable && offsetName != null)
            {
                sw.WriteLine(indent + "\tset {");
                sw.WriteLine(indent + "\t\tunsafe {");
                if (is_struct)
                {
                    sw.WriteLine(indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\t*raw_ptr = value;");
                }
                else
                {
                    sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\t*raw_ptr = " + to_native + ";");
                }
                sw.WriteLine(indent + "\t\t}");
                sw.WriteLine(indent + "\t}");
            }

            sw.WriteLine(indent + "}");
            sw.WriteLine("");

            if (getterName != null || setterName != null || getOffsetName != null)
            {
                GenerateGlue(gen_info);
            }
        }
Ejemplo n.º 8
0
        public virtual void Generate(GenerationInfo gen_info, string indent)
        {
            if (Ignored || Hidden)
            {
                return;
            }

            CheckGlue();
            if ((getterName != null || setterName != null || getOffsetName != null) && gen_info.GlueWriter == null)
            {
                LogWriter log = new LogWriter(container_type.QualifiedName);
                log.Member = Name;
                log.Warn("needs glue for field access.  Specify --glue-filename");
                return;
            }

            GenerateImports(gen_info, indent);

            SymbolTable  table     = SymbolTable.Table;
            IGeneratable gen       = table [CType];
            StreamWriter sw        = gen_info.Writer;
            string       modifiers = elem.GetAttributeAsBoolean("new_flag") ? "new " : "";

            sw.WriteLine(indent + "public " + modifiers + CSType + " " + Name + " {");

            if (Getter != null)
            {
                sw.Write(indent + "\tget ");
                Getter.GenerateBody(gen_info, container_type, "\t");
                sw.WriteLine("");
            }
            else if (getterName != null)
            {
                sw.WriteLine(indent + "\tget {");
                container_type.Prepare(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t\t" + CSType + " result = " + table.FromNative(ctype, getterName + " (" + container_type.CallByName() + ")") + ";");
                container_type.Finish(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t\treturn result;");
                sw.WriteLine(indent + "\t}");
            }
            else if (Readable && offsetName != null)
            {
                sw.WriteLine(indent + "\tget {");
                sw.WriteLine(indent + "\t\tunsafe {");
                if (gen is CallbackGen)
                {
                    sw.WriteLine(indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\t {0} del = ({0})Marshal.GetDelegateForFunctionPointer(*raw_ptr, typeof({0}));", table.GetMarshalType(CType));
                    sw.WriteLine(indent + "\t\t\treturn " + table.FromNative(ctype, "(del)") + ";");
                }
                else
                {
                    sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\treturn " + table.FromNative(ctype, "(*raw_ptr)") + ";");
                }
                sw.WriteLine(indent + "\t\t}");
                sw.WriteLine(indent + "\t}");
            }

            string to_native = (gen is IManualMarshaler) ? (gen as IManualMarshaler).AllocNative("value") : gen.CallByName("value");

            if (Setter != null)
            {
                sw.Write(indent + "\tset ");
                Setter.GenerateBody(gen_info, container_type, "\t");
                sw.WriteLine("");
            }
            else if (setterName != null)
            {
                sw.WriteLine(indent + "\tset {");
                container_type.Prepare(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t\t" + setterName + " (" + container_type.CallByName() + ", " + to_native + ");");
                container_type.Finish(sw, indent + "\t\t");
                sw.WriteLine(indent + "\t}");
            }
            else if (Writable && offsetName != null)
            {
                sw.WriteLine(indent + "\tset {");
                sw.WriteLine(indent + "\t\tunsafe {");
                if (gen is CallbackGen)
                {
                    sw.WriteLine(indent + "\t\t\t{0} wrapper = new {0} (value);", ((CallbackGen)gen).WrapperName);
                    sw.WriteLine(indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\t*raw_ptr = Marshal.GetFunctionPointerForDelegate (wrapper.NativeDelegate);");
                }
                else
                {
                    sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");");
                    sw.WriteLine(indent + "\t\t\t*raw_ptr = " + to_native + ";");
                }
                sw.WriteLine(indent + "\t\t}");
                sw.WriteLine(indent + "\t}");
            }

            sw.WriteLine(indent + "}");
            sw.WriteLine("");

            if (getterName != null || setterName != null || getOffsetName != null)
            {
                GenerateGlue(gen_info);
            }
        }