public void SetDefaultParameterValue(int index, CType type, CONSTVAL cv)
        {
            Debug.Assert(_defaultParameterIndex != null);
            ConstValFactory factory = new ConstValFactory();

            _defaultParameterIndex[index]         = true;
            _defaultParameters[index]             = factory.Copy(type.constValKind(), cv);
            _defaultParameterConstValTypes[index] = type;
        }
Exemplo n.º 2
0
            private bool bindImplicitConversionToEnum(AggregateType aggTypeSrc)
            {
                // The spec states:
                // *****************
                // 13.1.3 Implicit enumeration conversions
                //
                // An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any
                // enum-type.
                // *****************
                // However, we actually allow any constant zero, not just the integer literal zero, to be converted
                // to enum.  The reason for this is for backwards compatibility with a premature optimization
                // that used to be in the binding layer.  We would optimize away expressions such as 0 | blah to be
                // just 0, but not erase the "is literal" bit.  This meant that expression such as 0 | 0 | E.X
                // would succeed rather than correctly producing an error pointing out that 0 | 0 is not a literal
                // zero and therefore does not convert to any enum.
                //
                // We have removed the premature optimization but want old code to continue to compile. Rather than
                // try to emulate the somewhat complex behaviour of the previous optimizer, it is easier to simply
                // say that any compile time constant zero is convertible to any enum.  This means unfortunately
                // expressions such as (7-7) * 12 are convertible to enum, but frankly, that's better than having
                // some terribly complex rule about what constitutes a legal zero and what doesn't.

                // Note: Don't use GetConst here since the conversion only applies to bona-fide compile time constants.
                if (
                    aggTypeSrc.getAggregate().GetPredefType() != PredefinedType.PT_BOOL &&
                    exprSrc != null &&
                    exprSrc.isZero() &&
                    exprSrc.type.isNumericType() &&
                    /*(exprSrc.flags & EXF_LITERALCONST) &&*/
                    0 == (flags & CONVERTTYPE.STANDARD))
                {
                    // NOTE: This allows conversions from uint, long, ulong, float, double, and hexadecimal int
                    // NOTE: This is for backwards compatibility with Everett

                    // REVIEW: : This is another place where we lose EXPR fidelity. We shouldn't fold this
                    // into a constant here - we should move this to a later pass.
                    if (needsExprDest)
                    {
                        exprDest = GetExprFactory().CreateConstant(typeDest, ConstValFactory.GetDefaultValue(typeDest.constValKind()));
                    }
                    return(true);
                }
                return(false);
            }
Exemplo n.º 3
0
        /////////////////////////////////////////////////////////////////////////////////

        private void SetParameterAttributes(MethodOrPropertySymbol methProp, ParameterInfo[] parameters, int i)
        {
            if (((parameters[i].Attributes & ParameterAttributes.Optional) != 0) &&
                !parameters[i].ParameterType.IsByRef)
            {
                methProp.SetOptionalParameter(i);
                PopulateSymbolTableWithName("Value", new Type[] { typeof(Missing) }, typeof(Missing)); // We might need this later
            }

            object[] attrs;

            // Get MarshalAsAttribute
            if ((parameters[i].Attributes & ParameterAttributes.HasFieldMarshal) != 0)
            {
                if ((attrs = parameters[i].GetCustomAttributes(typeof(MarshalAsAttribute), false).ToArray()) != null
                    && attrs.Length > 0)
                {
                    MarshalAsAttribute attr = (MarshalAsAttribute)attrs[0];
                    methProp.SetMarshalAsParameter(i, attr.Value);
                }
            }

            // Get the various kinds of default values
            if ((attrs = parameters[i].GetCustomAttributes(typeof(DateTimeConstantAttribute), false).ToArray()) != null
                && attrs.Length > 0)
            {
                // Get DateTimeConstant

                DateTimeConstantAttribute attr = (DateTimeConstantAttribute)attrs[0];

                ConstValFactory factory = new ConstValFactory();
                CONSTVAL cv = factory.Create(((System.DateTime)attr.Value).Ticks);
                CType cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_DATETIME);
                methProp.SetDefaultParameterValue(i, cvType, cv);
            }
            else if ((attrs = parameters[i].GetCustomAttributes(typeof(DecimalConstantAttribute), false).ToArray()) != null
                && attrs.Length > 0)
            {
                // Get DecimalConstant

                DecimalConstantAttribute attr = (DecimalConstantAttribute)attrs[0];

                ConstValFactory factory = new ConstValFactory();
                CONSTVAL cv = factory.Create(attr.Value);
                CType cvType = _semanticChecker.GetSymbolLoader().GetOptPredefType(PredefinedType.PT_DECIMAL);
                methProp.SetDefaultParameterValue(i, cvType, cv);
            }
            else if (((parameters[i].Attributes & ParameterAttributes.HasDefault) != 0) &&
                !parameters[i].ParameterType.IsByRef)
            {
                // Only set a default value if we have one, and the type that we're
                // looking at isn't a by ref type or a type parameter.

                ConstValFactory factory = new ConstValFactory();
                CONSTVAL cv = cv = ConstValFactory.GetNullRef();
                CType cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_OBJECT);

                // We need to use RawDefaultValue, because DefaultValue is too clever.
#if UNSUPPORTEDAPI
                if (parameters[i].RawDefaultValue != null)
                {
                    object defValue = parameters[i].RawDefaultValue;
#else
                if (parameters[i].DefaultValue != null)
                {
                    object defValue = parameters[i].DefaultValue;
#endif
                    Type defType = defValue.GetType();

                    if (defType == typeof(System.Byte))
                    {
                        cv = factory.Create((System.Byte)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_BYTE);
                    }
                    else if (defType == typeof(System.Int16))
                    {
                        cv = factory.Create((System.Int16)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_SHORT);
                    }
                    else if (defType == typeof(System.Int32))
                    {
                        cv = factory.Create((System.Int32)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_INT);
                    }
                    else if (defType == typeof(System.Int64))
                    {
                        cv = factory.Create((System.Int64)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_LONG);
                    }
                    else if (defType == typeof(System.Single))
                    {
                        cv = factory.Create((System.Single)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_FLOAT);
                    }
                    else if (defType == typeof(System.Double))
                    {
                        cv = factory.Create((System.Double)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_DOUBLE);
                    }
                    else if (defType == typeof(System.Decimal))
                    {
                        cv = factory.Create((System.Decimal)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_DECIMAL);
                    }
                    else if (defType == typeof(System.Char))
                    {
                        cv = factory.Create((System.Char)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_CHAR);
                    }
                    else if (defType == typeof(System.Boolean))
                    {
                        cv = factory.Create((System.Boolean)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_BOOL);
                    }
                    else if (defType == typeof(System.SByte))
                    {
                        cv = factory.Create((System.SByte)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_SBYTE);
                    }
                    else if (defType == typeof(System.UInt16))
                    {
                        cv = factory.Create((System.UInt16)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_USHORT);
                    }
                    else if (defType == typeof(System.UInt32))
                    {
                        cv = factory.Create((System.UInt32)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_UINT);
                    }
                    else if (defType == typeof(System.UInt64))
                    {
                        cv = factory.Create((System.UInt64)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_ULONG);
                    }
                    else if (defType == typeof(System.String))
                    {
                        cv = factory.Create((System.String)defValue);
                        cvType = _semanticChecker.GetSymbolLoader().GetReqPredefType(PredefinedType.PT_STRING);
                    }
                    // if we fall off the end of this cascading if, we get Object/null
                    // because that's how we initialized the constval.
                }
                methProp.SetDefaultParameterValue(i, cvType, cv);
            }
        }

        /////////////////////////////////////////////////////////////////////////////////

        private MethodSymbol FindMatchingMethod(MemberInfo method, AggregateSymbol callingAggregate)
        {
            MethodSymbol meth = _bsymmgr.LookupAggMember(GetName(method.Name), callingAggregate, symbmask_t.MASK_MethodSymbol).AsMethodSymbol();
            while (meth != null)
            {
                if (meth.AssociatedMemberInfo.IsEquivalentTo(method))
                {
                    return meth;
                }
                meth = BSYMMGR.LookupNextSym(meth, callingAggregate, symbmask_t.MASK_MethodSymbol).AsMethodSymbol();
            }
            return null;
        }
Exemplo n.º 4
0
 public void SetDefaultParameterValue(int index, CType type, CONSTVAL cv)
 {
     Debug.Assert(_defaultParameterIndex != null);
     ConstValFactory factory = new ConstValFactory();
     _defaultParameterIndex[index] = true;
     _defaultParameters[index] = factory.Copy(type.constValKind(), cv);
     _defaultParameterConstValTypes[index] = type;
 }
 public EXPRCONSTANT CreateBoolConstant(bool b)
 {
     return(CreateConstant(GetTypes().GetReqPredefAgg(PredefinedType.PT_BOOL).getThisType(), ConstValFactory.GetBool(b)));
 }
 public EXPR CreateNull()
 {
     return(CreateConstant(GetTypes().GetNullType(), ConstValFactory.GetNullRef()));
 }
 public EXPRCONSTANT CreateIntegerConstant(int x)
 {
     return(CreateConstant(GetTypes().GetReqPredefAgg(PredefinedType.PT_INT).getThisType(), ConstValFactory.GetInt(x)));
 }
        private EXPR CreateZeroInit(EXPRTYPEORNAMESPACE pTypeExpr, EXPR pOptionalOriginalConstructorCall, bool isConstructor)
        {
            Debug.Assert(pTypeExpr != null);
            CType pType    = pTypeExpr.TypeOrNamespace.AsType();
            bool  bIsError = false;

            if (pType.isEnumType())
            {
                // For enum types, we create a constant that has the default value
                // as an object pointer.
                ConstValFactory factory = new ConstValFactory();
                EXPRCONSTANT    expr    = CreateConstant(pType, factory.Create(Activator.CreateInstance(pType.AssociatedSystemType)));
                return(expr);
            }

            switch (pType.fundType())
            {
            default:
                bIsError = true;
                break;

            case FUNDTYPE.FT_PTR:
            {
                CType nullType = GetTypes().GetNullType();

                // UNDONE:  I think this if is always false ...
                if (nullType.fundType() == pType.fundType())
                {
                    // Create a constant here.

                    EXPRCONSTANT expr = CreateConstant(pType, ConstValFactory.GetDefaultValue(ConstValKind.IntPtr));
                    return(expr);
                }

                // Just allocate a new node and fill it in.

                EXPRCAST cast = CreateCast(0, pTypeExpr, CreateNull());         // UNDONE:  should pTree be passed in here?
                return(cast);
            }

            case FUNDTYPE.FT_REF:
            case FUNDTYPE.FT_I1:
            case FUNDTYPE.FT_U1:
            case FUNDTYPE.FT_I2:
            case FUNDTYPE.FT_U2:
            case FUNDTYPE.FT_I4:
            case FUNDTYPE.FT_U4:
            case FUNDTYPE.FT_I8:
            case FUNDTYPE.FT_U8:
            case FUNDTYPE.FT_R4:
            case FUNDTYPE.FT_R8:
            {
                EXPRCONSTANT expr           = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                EXPRCONSTANT exprInOriginal = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                exprInOriginal.SetOptionalConstructorCall(pOptionalOriginalConstructorCall);
                return(expr);
                // UNDONE: Check other bogus casts
            }

            case FUNDTYPE.FT_STRUCT:
                if (pType.isPredefType(PredefinedType.PT_DECIMAL))
                {
                    EXPRCONSTANT expr         = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                    EXPRCONSTANT exprOriginal = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                    exprOriginal.SetOptionalConstructorCall(pOptionalOriginalConstructorCall);
                    return(expr);
                }
                break;

            case FUNDTYPE.FT_VAR:
                break;
            }

            EXPRZEROINIT rval = new EXPRZEROINIT();

            rval.kind  = ExpressionKind.EK_ZEROINIT;
            rval.type  = pType;
            rval.flags = 0;
            rval.OptionalConstructorCall = pOptionalOriginalConstructorCall;
            rval.IsConstructor           = isConstructor;

            if (bIsError)
            {
                rval.SetError();
            }

            Debug.Assert(rval != null);
            return(rval);
        }
 public ExprFactory(GlobalSymbolContext globalSymbolContext)
 {
     Debug.Assert(globalSymbolContext != null);
     m_globalSymbolContext = globalSymbolContext;
     m_constants           = new ConstValFactory();
 }
Exemplo n.º 10
0
        private EXPR CreateZeroInit(EXPRTYPEORNAMESPACE pTypeExpr, EXPR pOptionalOriginalConstructorCall, bool isConstructor)
        {
            Debug.Assert(pTypeExpr != null);
            CType pType = pTypeExpr.TypeOrNamespace.AsType();
            bool bIsError = false;

            if (pType.isEnumType())
            {
                // For enum types, we create a constant that has the default value
                // as an object pointer.
                ConstValFactory factory = new ConstValFactory();
                EXPRCONSTANT expr = CreateConstant(pType, factory.Create(Activator.CreateInstance(pType.AssociatedSystemType)));
                return expr;
            }

            switch (pType.fundType())
            {
                default:
                    bIsError = true;
                    break;

                case FUNDTYPE.FT_PTR:
                    {
                        CType nullType = GetTypes().GetNullType();

                        // It looks like this if is always false ...
                        if (nullType.fundType() == pType.fundType())
                        {
                            // Create a constant here.

                            EXPRCONSTANT expr = CreateConstant(pType, ConstValFactory.GetDefaultValue(ConstValKind.IntPtr));
                            return (expr);
                        }

                        // Just allocate a new node and fill it in.

                        EXPRCAST cast = CreateCast(0, pTypeExpr, CreateNull());
                        return (cast);
                    }

                case FUNDTYPE.FT_REF:
                case FUNDTYPE.FT_I1:
                case FUNDTYPE.FT_U1:
                case FUNDTYPE.FT_I2:
                case FUNDTYPE.FT_U2:
                case FUNDTYPE.FT_I4:
                case FUNDTYPE.FT_U4:
                case FUNDTYPE.FT_I8:
                case FUNDTYPE.FT_U8:
                case FUNDTYPE.FT_R4:
                case FUNDTYPE.FT_R8:
                    {
                        EXPRCONSTANT expr = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                        EXPRCONSTANT exprInOriginal = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                        exprInOriginal.SetOptionalConstructorCall(pOptionalOriginalConstructorCall);
                        return expr;
                    }
                case FUNDTYPE.FT_STRUCT:
                    if (pType.isPredefType(PredefinedType.PT_DECIMAL))
                    {
                        EXPRCONSTANT expr = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                        EXPRCONSTANT exprOriginal = CreateConstant(pType, ConstValFactory.GetDefaultValue(pType.constValKind()));
                        exprOriginal.SetOptionalConstructorCall(pOptionalOriginalConstructorCall);
                        return expr;
                    }
                    break;

                case FUNDTYPE.FT_VAR:
                    break;
            }

            EXPRZEROINIT rval = new EXPRZEROINIT();
            rval.kind = ExpressionKind.EK_ZEROINIT;
            rval.type = pType;
            rval.flags = 0;
            rval.OptionalConstructorCall = pOptionalOriginalConstructorCall;
            rval.IsConstructor = isConstructor;

            if (bIsError)
            {
                rval.SetError();
            }

            Debug.Assert(rval != null);
            return (rval);
        }
Exemplo n.º 11
0
 public ExprFactory(GlobalSymbolContext globalSymbolContext)
 {
     Debug.Assert(globalSymbolContext != null);
     _globalSymbolContext = globalSymbolContext;
     _constants = new ConstValFactory();
 }