Example #1
0
        private bool WriteCast(CCodeWriterBase c, out bool parenthesis)
        {
            parenthesis = false;

            switch (this.ConversionKind)
            {
            case ConversionKind.MethodGroup:
                var newDelegate = new DelegateCreationExpression {
                    Type = Type, MethodOwner = MethodOwner
                };
                newDelegate.Arguments.Add(this.Operand);
                newDelegate.WriteTo(c);
                return(false);

            case ConversionKind.NullToPointer:
                // The null pointer is represented as 0u.
                c.TextSpan("(");
                c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                c.TextSpan(")");
                c.TextSpan("nullptr");
                return(false);

            case ConversionKind.Boxing:
                c.TextSpan("__box");
                if (Cs2CGenerator.DebugOutput)
                {
                    c.TextSpan("_debug");
                }

                break;

            case ConversionKind.Unboxing:
                c.TextSpan("__unbox<");
                c.WriteType(Type, true, false, true, containingNamespace: MethodOwner?.ContainingNamespace);
                c.TextSpan(">");
                break;

            case ConversionKind.ExplicitReference:
            case ConversionKind.ImplicitReference:

                if (Type.TypeKind == TypeKind.TypeParameter)
                {
                    c.TextSpan("cast<");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(">");
                }
                else if (Type.TypeKind == TypeKind.Interface && this.TypeSource.AllInterfaces.Contains((INamedTypeSymbol)Type))
                {
                    c.TextSpan("interface_cast<");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(">");
                }
                else if (Type.TypeKind == TypeKind.Interface)
                {
                    c.TextSpan("dynamic_interface_cast_or_throw<");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(">");
                }
                else if (this.TypeSource.IsDerivedFrom(Type))
                {
                    c.TextSpan("static_cast<");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(">");
                }
                else if (this.TypeSource.TypeKind == TypeKind.Interface && Type.SpecialType == SpecialType.System_Object)
                {
                    c.TextSpan("object_cast");
                }
                else if (this.TypeSource.TypeKind == TypeKind.Array && Type.TypeKind == TypeKind.Array)
                {
                    c.TextSpan("reinterpret_cast<");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(">");
                }
                else
                {
                    c.TextSpan("cast<");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(">");
                }

                break;

            case ConversionKind.PointerToInteger:
            case ConversionKind.IntegerToPointer:
            case ConversionKind.PointerToPointer:

                if (!Type.IsIntPtrType())
                {
                    c.TextSpan("(");
                    c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                    c.TextSpan(")");

                    parenthesis = true;
                }

                break;

            case ConversionKind.Identity:
                // for string
                if (this.TypeSource.SpecialType == SpecialType.System_String && Type.TypeKind == TypeKind.Pointer)
                {
                    c.TextSpan("&");
                    this.Operand.WriteTo(c);
                    c.TextSpan("->m_firstChar");
                    return(false);
                }

                return(true);

            default:
                if (this.Checked)
                {
                    c.TextSpan("checked_");
                }

                c.TextSpan("static_cast<");
                c.WriteType(Type, containingNamespace: MethodOwner?.ContainingNamespace);
                c.TextSpan(">");
                break;
            }

            return(true);
        }