private bool?CheckForDefaultConstruct(Type desugared, Parameter parameter)
        {
            Method ctor = parameter.DefaultArgument.Declaration as Method;

            if (ctor == null || !ctor.IsConstructor)
            {
                return(false);
            }

            Type type;

            desugared.IsPointerTo(out type);
            type = type ?? desugared;
            Class decl;

            if (!type.TryGetClass(out decl))
            {
                return(false);
            }
            TypeMap typeMap;

            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                string mappedTo;
                if (Driver.Options.IsCSharpGenerator)
                {
                    var typePrinterContext = new CSharpTypePrinterContext
                    {
                        CSharpKind = CSharpTypePrinterContextKind.Managed,
                        Type       = type
                    };
                    mappedTo = typeMap.CSharpSignature(typePrinterContext);
                }
                else
                {
                    var typePrinterContext = new CLITypePrinterContext
                    {
                        Type = type
                    };
                    mappedTo = typeMap.CLISignature(typePrinterContext);
                }
                if (mappedTo == "string" && ctor.Parameters.Count == 0)
                {
                    parameter.DefaultArgument.String = "\"\"";
                    return(true);
                }
            }

            parameter.DefaultArgument.String = string.Format("new {0}", parameter.DefaultArgument.String);
            if (ctor.Parameters.Count > 0 && ctor.Parameters[0].OriginalName == "_0")
            {
                parameter.DefaultArgument.String = parameter.DefaultArgument.String.Replace("(0)", "()");
            }

            return(decl.IsValueType ? true : (bool?)null);
        }
Exemplo n.º 2
0
        public override TypePrinterResult VisitFunctionDecl(Function function)
        {
            string @class;

            switch (MethodScopeKind)
            {
            case TypePrintScopeKind.Qualified:
                @class = $"{function.Namespace.Visit(this)}::";
                break;

            case TypePrintScopeKind.GlobalQualified:
                @class = $"::{function.Namespace.Visit(this)}::";
                break;

            default:
                @class = string.Empty;
                break;
            }

            var @params = string.Join(", ", function.Parameters.Select(p => p.Visit(this)));
            var @const  = function is Method method && method.IsConst ? " const" : string.Empty;
            var name    = function.OperatorKind == CXXOperatorKind.Conversion ||
                          function.OperatorKind == CXXOperatorKind.ExplicitConversion ?
                          $"operator {function.OriginalReturnType.Visit(this)}" :
                          function.OriginalName;

            FunctionType functionType;

            CppSharp.AST.Type desugared = function.FunctionType.Type.Desugar();
            if (!desugared.IsPointerTo(out functionType))
            {
                functionType = (FunctionType)desugared;
            }
            string exceptionType = Print(functionType.ExceptionSpecType);

            var @return = function.OriginalReturnType.Visit(this);

            if (!string.IsNullOrEmpty(@return.Type))
            {
                @return.NamePrefix.Append(' ');
            }
            @return.Name = @class + name;
            @return.NameSuffix.Append('(').Append(@params)
            .Append(function.IsVariadic ? ", ..." : string.Empty).Append(')')
            .Append(@const).Append(exceptionType);
            return(@return);
        }
Exemplo n.º 3
0
        public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
        {
            if (!(typedef.Declaration.Type.Desugar(false) is TemplateParameterSubstitutionType) &&
                !VisitType(typedef, quals))
            {
                return(false);
            }

            var decl = typedef.Declaration;

            Type type         = decl.Type.Desugar();
            var  functionType = type as FunctionType;

            if (functionType == null && !type.IsPointerTo(out functionType))
            {
                return(decl.Type.Visit(this));
            }

            var ptrName = $@"{Generator.GeneratedIdentifier("ptr")}{
                Context.ParameterIndex}";

            Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};");

            var substitution = decl.Type.Desugar(false)
                               as TemplateParameterSubstitutionType;

            if (substitution != null)
            {
                Context.Return.Write($@"({
                    substitution.ReplacedParameter.Parameter.Name}) (object) (");
            }

            Context.Return.Write($@"{ptrName} == IntPtr.Zero? null : {
                (substitution == null ? $"({Context.ReturnType}) " :
                 string.Empty)}Marshal.GetDelegateForFunctionPointer({
                ptrName}, typeof({typedef}))");

            if (substitution != null)
            {
                Context.Return.Write(")");
            }
            return(true);
        }
        private bool?CheckForDefaultConstruct(Type desugared, Expression arg)
        {
            // Unwrapping the underlying type behind a possible pointer/reference
            Type type;

            desugared.IsPointerTo(out type);
            type = type ?? desugared;

            Class decl;

            if (!type.TryGetClass(out decl))
            {
                return(false);
            }

            var ctor = arg.Declaration as Method;

            TypeMap typeMap;

            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                Type   typeInSignature;
                string mappedTo;
                if (Driver.Options.IsCSharpGenerator)
                {
                    var typePrinterContext = new CSharpTypePrinterContext
                    {
                        CSharpKind = CSharpTypePrinterContextKind.Managed,
                        Type       = type
                    };
                    typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs();
                    mappedTo        = typeMap.CSharpSignature(typePrinterContext);
                }
                else
                {
                    var typePrinterContext = new CLITypePrinterContext
                    {
                        Type = type
                    };
                    typeInSignature = typeMap.CLISignatureType(typePrinterContext).SkipPointerRefs();
                    mappedTo        = typeMap.CLISignature(typePrinterContext);
                }
                Enumeration @enum;
                if (typeInSignature.TryGetEnum(out @enum))
                {
                    return(false);
                }

                if (ctor == null || !ctor.IsConstructor)
                {
                    return(false);
                }
                if (mappedTo == "string" && ctor.Parameters.Count == 0)
                {
                    arg.String = "\"\"";
                    return(true);
                }
            }

            if (regexCtor.IsMatch(arg.String))
            {
                arg.String = string.Format("new {0}", arg.String);
                if (ctor != null && ctor.Parameters.Count > 0 && ctor.Parameters[0].Type.IsAddress())
                {
                    arg.String = arg.String.Replace("(0)", "()");
                    return(decl.IsValueType ? true : (bool?)null);
                }
            }
            else
            {
                if (ctor != null && ctor.Parameters.Count > 0)
                {
                    var         finalPointee = ctor.Parameters[0].Type.SkipPointerRefs().Desugar();
                    Enumeration @enum;
                    if (finalPointee.TryGetEnum(out @enum))
                    {
                        TranslateEnumExpression(arg, finalPointee, arg.String);
                    }
                }
            }

            return(decl.IsValueType ? true : (bool?)null);
        }
        private bool? CheckForDefaultConstruct(Type desugared, Parameter parameter)
        {
            Method ctor = parameter.DefaultArgument.Declaration as Method;
            if (ctor == null || !ctor.IsConstructor)
                return false;

            Type type;
            desugared.IsPointerTo(out type);
            type = type ?? desugared;
            Class decl;
            if (!type.TryGetClass(out decl))
                return false;
            TypeMap typeMap;

            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                string mappedTo;
                if (Driver.Options.IsCSharpGenerator)
                {
                    var typePrinterContext = new CSharpTypePrinterContext
                    {
                        CSharpKind = CSharpTypePrinterContextKind.Managed,
                        Type = type
                    };
                    mappedTo = typeMap.CSharpSignature(typePrinterContext);
                }
                else
                {
                    var typePrinterContext = new CLITypePrinterContext
                    {
                        Type = type
                    };
                    mappedTo = typeMap.CLISignature(typePrinterContext);
                }
                if (mappedTo == "string" && ctor.Parameters.Count == 0)
                {
                    parameter.DefaultArgument.String = "\"\"";
                    return true;
                }
            }

            parameter.DefaultArgument.String = string.Format("new {0}", parameter.DefaultArgument.String);
            if (ctor.Parameters.Count > 0 && ctor.Parameters[0].OriginalName == "_0")
                parameter.DefaultArgument.String = parameter.DefaultArgument.String.Replace("(0)", "()");

            return decl.IsValueType ? true : (bool?) null;
        }
        private bool? CheckForDefaultConstruct(Type desugared, Expression arg)
        {
            // Unwrapping the underlying type behind a possible pointer/reference
            Type type;
            desugared.IsPointerTo(out type);
            type = type ?? desugared;

            Class decl;
            if (!type.TryGetClass(out decl))
                return false;

            var ctor = arg.Declaration as Method;

            TypeMap typeMap;
            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                Type typeInSignature;
                string mappedTo;
                if (Driver.Options.IsCSharpGenerator)
                {
                    var typePrinterContext = new CSharpTypePrinterContext
                    {
                        CSharpKind = CSharpTypePrinterContextKind.Managed,
                        Type = type
                    };
                    typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs();
                    mappedTo = typeMap.CSharpSignature(typePrinterContext);
                }
                else
                {
                    var typePrinterContext = new CLITypePrinterContext
                    {
                        Type = type
                    };
                    typeInSignature = typeMap.CLISignatureType(typePrinterContext).SkipPointerRefs();
                    mappedTo = typeMap.CLISignature(typePrinterContext);
                }
                Enumeration @enum;
                if (typeInSignature.TryGetEnum(out @enum))
                {
                    return false;
                }

                if (ctor == null || !ctor.IsConstructor)
                    return false;
                if (mappedTo == "string" && ctor.Parameters.Count == 0)
                {
                    arg.String = "\"\"";
                    return true;
                }
            }

            if (regexCtor.IsMatch(arg.String))
            {
                arg.String = string.Format("new {0}", arg.String);
                if (ctor != null && ctor.Parameters.Count > 0 && ctor.Parameters[0].Type.IsAddress())
                {
                    arg.String = arg.String.Replace("(0)", "()");
                    return decl.IsValueType ? true : (bool?) null;
                }
            }
            else
            {
                if (ctor != null && ctor.Parameters.Count > 0)
                {
                    var finalPointee = ctor.Parameters[0].Type.SkipPointerRefs().Desugar();
                    Enumeration @enum;
                    if (finalPointee.TryGetEnum(out @enum))
                        TranslateEnumExpression(arg, finalPointee, arg.String);
                }
            }

            return decl.IsValueType ? true : (bool?) null;
        }