private static bool CheckFloatSyntax(Type desugared, Statement statement, ref string result)
 {
     var builtin = desugared as BuiltinType;
     if (builtin != null)
     {
         switch (builtin.Type)
         {
             case PrimitiveType.Float:
                 if (statement.String.EndsWith(".F"))
                 {
                     result = statement.String.Replace(".F", ".0F");
                     return true;
                 }
                 break;
             case PrimitiveType.Double:
                 if (statement.String.EndsWith("."))
                 {
                     result = statement.String + '0';
                     return true;
                 }
                 break;
         }
     }
     return false;
 }
        private bool CheckForEnumValue(Type desugared, Statement statement,
            ref string result)
        {
            var enumItem = statement.Declaration as Enumeration.Item;
            if (enumItem != null)
            {
                if (desugared.IsPrimitiveType())
                {
                    statement.Declaration = null;
                    result = string.Format("(int) {0}.{1}",
                        new CSharpTypePrinter(Context).VisitEnumDecl(
                            (Enumeration) enumItem.Namespace), enumItem.Name);
                }
                else
                {
                    result = string.Format("{0}.{1}",
                        new CSharpTypePrinter(Context).VisitEnumDecl(
                            (Enumeration) enumItem.Namespace), enumItem.Name);
                }
                return true;
            }

            var call = statement.Declaration as Function;
            if (call != null && statement.String != "0")
            {
                var @params = regexFunctionParams.Match(statement.String).Groups[1].Value;
                result = TranslateEnumExpression(call, desugared, @params);
                return true;
            }

            return false;
        }
 private void TranslateEnumExpression(Function function, Statement arg,
     Type desugared, string @params)
 {
     TypeMap typeMap;
     if ((function.Parameters.Count == 0 ||
          HasSingleZeroExpression(function)) &&
         Driver.TypeDatabase.FindTypeMap(desugared, out typeMap))
     {
         var typeInSignature = typeMap.CSharpSignatureType(new CSharpTypePrinterContext
         {
             CSharpKind = CSharpTypePrinterContextKind.DefaultExpression,
             Type = desugared
         }).SkipPointerRefs().Desugar();
         Enumeration @enum;
         if (typeInSignature.TryGetEnum(out @enum))
         {
             arg.String = "0";
             return;
         }
     }
     if (@params.Contains("::"))
         arg.String = regexDoubleColon.Replace(@params, desugared + ".");
     else
         arg.String = regexName.Replace(@params, desugared + ".$1");
 }
        private bool? CheckForDefaultConstruct(Type desugared, Statement arg,
            TypeQualifiers qualifiers)
        {
            // Unwrapping the underlying type behind a possible pointer/reference
            Type type = desugared.GetFinalPointee() ?? desugared;

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

            var ctor = arg.Declaration as Method;

            TypeMap typeMap;
            var typePrinterContext = new CSharpTypePrinterContext
            {
                CSharpKind = CSharpTypePrinterContextKind.DefaultExpression,
                Type = type
            };

            string typePrinterResult = null;
            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                var typeInSignature = typeMap.CSharpSignatureType(
                    typePrinterContext).SkipPointerRefs().Desugar();
                Enumeration @enum;
                if (typeInSignature.TryGetEnum(out @enum))
                    return false;

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

                typePrinterResult = typeMap.CSharpSignature(typePrinterContext);
                if (typePrinterResult == "string" && ctor.Parameters.Count == 0)
                {
                    arg.String = "\"\"";
                    return true;
                }
            }

            var match = regexCtor.Match(arg.String);
            if (match.Success)
            {
                if (ctor != null)
                {
                    var templateSpecializationType = type as TemplateSpecializationType;
                    var typePrinter = new CSharpTypePrinter(Driver);
                    typePrinterResult = typePrinterResult ?? (templateSpecializationType != null
                        ? typePrinter.VisitTemplateSpecializationType(
                            templateSpecializationType, qualifiers)
                        : typePrinter.VisitClassDecl((Class) ctor.Namespace)).Type;

                    arg.String = string.Format("new {0}{1}", typePrinterResult,
                        match.Groups[2].Value);
                    if (ctor.Parameters.Count > 0 && ctor.Parameters[0].Type.IsAddress())
                        arg.String = arg.String.Replace("(0)", "()");
                }
                else
                    arg.String = string.Format("new {0}", arg.String);
            }
            else
            {
                if (ctor != null && ctor.Parameters.Count > 0)
                {
                    var finalPointee = ctor.Parameters[0].Type.SkipPointerRefs().Desugar();
                    Enumeration @enum;
                    if (finalPointee.TryGetEnum(out @enum))
                        TranslateEnumExpression(ctor, arg, finalPointee, arg.String);
                }
            }

            return decl.IsValueType ? true : (bool?) null;
        }
        private bool CheckForDefaultEmptyChar(Type desugared, Statement statement,
            ref string result)
        {
            if (statement.String == "0" &&
                Driver.Options.MarshalCharAsManagedChar &&
                desugared.IsPrimitiveType(PrimitiveType.Char))
            {
                result = "'\\0'";
                return true;
            }

            return false;
        }
        private bool? CheckForDefaultConstruct(Type desugared, Statement statement,
            ref string result)
        {
            var type = desugared.GetFinalPointee() ?? desugared;

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

            var ctor = statement as CXXConstructExpr;

            TypeMap typeMap;

            var typePrinter = new CSharpTypePrinter(Driver);
            typePrinter.PushContext(CSharpTypePrinterContextKind.DefaultExpression);
            var typePrinterResult = type.Visit(typePrinter).Type;
            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                var typeInSignature = typeMap.CSharpSignatureType(
                    typePrinter.Context).SkipPointerRefs().Desugar();
                Enumeration @enum;
                if (typeInSignature.TryGetEnum(out @enum))
                {
                    if (ctor != null &&
                        (ctor.Arguments.Count == 0 ||
                         HasSingleZeroArgExpression((Function) ctor.Declaration)))
                    {
                        result = "0";
                        return true;
                    }
                    return false;
                }

                if (ctor != null && typePrinterResult == "string" && ctor.Arguments.Count == 0)
                {
                    result = "\"\"";
                    return true;
                }
            }

            if (ctor == null)
                return decl.IsValueType ? (bool?) false : null;

            var method = (Method) statement.Declaration;
            var expressionSupported = decl.IsValueType && method.Parameters.Count == 0;

            if (statement.String.Contains('('))
            {
                var argsBuilder = new StringBuilder("new ");
                argsBuilder.Append(typePrinterResult);
                argsBuilder.Append('(');
                for (var i = 0; i < ctor.Arguments.Count; i++)
                {
                    var argument = ctor.Arguments[i];
                    var argResult = argument.String;
                    expressionSupported &= PrintExpression(method.Parameters[i].Type.Desugar(),
                        argument, ref argResult) ?? false;
                    argsBuilder.Append(argResult);
                    if (i < ctor.Arguments.Count - 1)
                        argsBuilder.Append(", ");
                }
                argsBuilder.Append(')');
                result = argsBuilder.ToString();
            }
            else
            {
                if (method.Parameters.Count > 0)
                {
                    var paramType = method.Parameters[0].Type.SkipPointerRefs().Desugar();
                    Enumeration @enum;
                    if (paramType.TryGetEnum(out @enum))
                        result = TranslateEnumExpression(method, paramType, statement.String);
                }
            }
            return expressionSupported ? true : (bool?) null;
        }