Ejemplo n.º 1
0
        public override bool Validate(LogWriter logWriter)
        {
            logWriter.Member = Name;

            if (!_returnValue.Validate(logWriter) || !base.Validate(logWriter))
            {
                return(false);
            }

            if (Name == string.Empty || CName == string.Empty)
            {
                logWriter.Warn("Method has no name or cname.");
                return(false);
            }

            var parameters = Parameters;

            IsGetter =
                (parameters.IsAccessor && _returnValue.IsVoid || parameters.Count == 0 && !_returnValue.IsVoid) &&
                HasGetterName;
            IsSetter = (parameters.IsAccessor || parameters.VisibleCount == 1 && _returnValue.IsVoid) && HasSetterName;

            _call =
                $"({(IsStatic ? "" : $"{ContainerType.CallByName()}{(parameters.Count > 0 ? ", " : "")}")}{Body.GetCallString(IsSetter)})";

            return(true);
        }
Ejemplo n.º 2
0
        public void Generate(GenerationInfo generationInfo, ClassBase implementor)
        {
            Method complement = null;

            /* we are generated by the get Method, if there is one */
            if (IsSetter || IsGetter)
            {
                if (Modifiers != "new " && ContainerType.GetPropertyRecursively(Name.Substring(3)) != null)
                {
                    return;
                }

                complement = GetComplement();

                if (complement != null && IsSetter)
                {
                    if (Parameters.AccessorReturnType == complement.ReturnType)
                    {
                        return;
                    }

                    _call = string.Format("({0}{1})",
                                          IsStatic ? "" : ContainerType.CallByName() + (Parameters.Count > 0 ? ", " : ""),
                                          Body.GetCallString(false));

                    complement = null;
                    IsSetter   = false;
                }

                /* some setters take more than one arg */
                if (complement != null && !complement.IsSetter)
                {
                    complement = null;
                }
            }

            generationInfo.CurrentMember = Name;

            GenerateImport(generationInfo.Writer);
            if (complement != null && _returnValue.CsType == complement.Parameters.AccessorReturnType)
            {
                complement.GenerateImport(generationInfo.Writer);
            }

            if (IsDeprecated)
            {
                generationInfo.Writer.WriteLine("\t\t[Obsolete]");
            }
            generationInfo.Writer.Write("\t\t");
            if (Protection != "")
            {
                generationInfo.Writer.Write("{0} ", Protection);
            }
            GenerateDeclCommon(generationInfo.Writer, implementor);

            if (IsGetter || IsSetter)
            {
                generationInfo.Writer.Write("\t\t\t");
                generationInfo.Writer.Write(IsGetter ? "get" : "set");
                GenerateBody(generationInfo, implementor, "\t");
            }
            else
            {
                GenerateBody(generationInfo, implementor, "");
            }

            if (IsGetter || IsSetter)
            {
                if (complement != null && _returnValue.CsType == complement.Parameters.AccessorReturnType)
                {
                    generationInfo.Writer.WriteLine();
                    generationInfo.Writer.Write("\t\t\tset");
                    complement.GenerateBody(generationInfo, implementor, "\t");
                }

                generationInfo.Writer.WriteLine();
                generationInfo.Writer.WriteLine("\t\t}");
            }
            else
            {
                generationInfo.Writer.WriteLine();
            }

            if (Parameters.HasOptional && !(IsGetter || IsSetter))
            {
                GenerateOverloads(generationInfo.Writer);
            }

            generationInfo.Writer.WriteLine();

            Statistics.MethodCount++;
        }
Ejemplo n.º 3
0
        public virtual void Generate(GenerationInfo generationInfo, string indent)
        {
            if (Ignored || Hidden)
            {
                return;
            }

            CheckGlue(generationInfo);

            GenerateImports(generationInfo, indent);

            if (Getter == null && _getterName == null && _offsetName == null &&
                Setter == null && _setterName == null)
            {
                return;
            }

            var streamWriter = generationInfo.Writer;
            var modifiers    = Element.GetAttributeAsBoolean("new_flag") ? "new " : "";

            streamWriter.WriteLine($"{indent}public {modifiers}{CsType} {Name} {{");

            var table       = SymbolTable.Table;
            var generatable = table[CType];

            if (Getter != null)
            {
                streamWriter.Write($"{indent}\tget ");
                Getter.GenerateBody(generationInfo, ContainerType, "\t");
                streamWriter.WriteLine("");
            }
            else if (_getterName != null)
            {
                streamWriter.WriteLine($"{indent}\tget {{");
                ContainerType.Prepare(streamWriter, $"{indent}\t\t");

                streamWriter.WriteLine(indent + "\t\t" + CsType + " result = " +
                                       table.FromNative(CType, _getterName + " (" + ContainerType.CallByName() + ")") +
                                       ";");

                ContainerType.Finish(streamWriter, $"{indent}\t\t");
                streamWriter.WriteLine($"{indent}\t\treturn result;");
                streamWriter.WriteLine($"{indent}\t}}");
            }
            else if (Readable && _offsetName != null)
            {
                streamWriter.WriteLine($"{indent}\tget {{");
                streamWriter.WriteLine($"{indent}\t\tunsafe {{");

                if (generatable is CallbackGen)
                {
                    streamWriter.WriteLine(indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" +
                                           ContainerType.CallByName() +
                                           ") + " + _offsetName + ");");

                    streamWriter.WriteLine(
                        indent + "\t\t\t {0} del = ({0})Marshal.GetDelegateForFunctionPointer(*raw_ptr, typeof({0}));",
                        table.GetMarshalType(CType));

                    streamWriter.WriteLine($"{indent}\t\t\treturn {table.FromNative(CType, "(del)")};");
                }
                else
                {
                    streamWriter.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" +
                                           table.GetMarshalType(CType) + "*)(((byte*)" + ContainerType.CallByName() +
                                           ") + " +
                                           _offsetName + ");");

                    streamWriter.WriteLine($"{indent}\t\t\treturn {table.FromNative(CType, "(*raw_ptr)")};");
                }

                streamWriter.WriteLine($"{indent}\t\t}}");
                streamWriter.WriteLine($"{indent}\t}}");
            }

            var toNative = generatable is IManualMarshaler marshaler
                ? marshaler.AllocNative("value")
                : generatable.CallByName("value");

            if (Setter != null)
            {
                streamWriter.Write($"{indent}\tset ");
                Setter.GenerateBody(generationInfo, ContainerType, "\t");
                streamWriter.WriteLine("");
            }
            else if (_setterName != null)
            {
                streamWriter.WriteLine($"{indent}\tset {{");
                ContainerType.Prepare(streamWriter, $"{indent}\t\t");

                streamWriter.WriteLine(
                    $"{indent}\t\t{_setterName} ({ContainerType.CallByName()}, {toNative});");

                ContainerType.Finish(streamWriter, $"{indent}\t\t");
                streamWriter.WriteLine($"{indent}\t}}");
            }
            else if (Writeable && _offsetName != null)
            {
                streamWriter.WriteLine($"{indent}\tset {{");
                streamWriter.WriteLine($"{indent}\t\tunsafe {{");

                if (generatable is CallbackGen callbackGen)
                {
                    streamWriter.WriteLine(indent + "\t\t\t{0} wrapper = new {0} (value);", callbackGen.WrapperName);
                    streamWriter.WriteLine(
                        $"{indent}\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*){ContainerType.CallByName()}) + {_offsetName});");
                    streamWriter.WriteLine(
                        $"{indent}\t\t\t*raw_ptr = Marshal.GetFunctionPointerForDelegate (wrapper.NativeDelegate);");
                }
                else
                {
                    streamWriter.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" +
                                           table.GetMarshalType(CType) + "*)(((byte*)" + ContainerType.CallByName() +
                                           ") + " +
                                           _offsetName + ");");
                    streamWriter.WriteLine($"{indent}\t\t\t*raw_ptr = {toNative};");
                }

                streamWriter.WriteLine($"{indent}\t\t}}");
                streamWriter.WriteLine($"{indent}\t}}");
            }

            streamWriter.WriteLine($"{indent}}}");
            streamWriter.WriteLine("");

            if ((_getterName != null || _setterName != null || GetOffsetName != null) &&
                generationInfo.GlueWriter != null)
            {
                GenerateGlue(generationInfo);
            }
        }