Inflate default parameter expression without type checks. Fixes #13793
[mono.git] / mcs / mcs / constant.cs
index 1a70f55d8f83fdfece957397b44f6b02edefb7fb..f51bfab660900ca6aa8dab4783b4e446778341dc 100644 (file)
@@ -162,6 +162,88 @@ namespace Mono.CSharp {
 #endif
                }
 
+               //
+               // Returns a constant instance based on value and type. This is probing version of
+               // CreateConstantFromValue
+               //
+               public static Constant ExtractConstantFromValue (TypeSpec t, object v, Location loc)
+               {
+                       switch (t.BuiltinType) {
+                       case BuiltinTypeSpec.Type.Int:
+                               if (v is int)
+                                       return new IntConstant (t, (int) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.String:
+                               if (v is string)
+                                       return new StringConstant (t, (string) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.UInt:
+                               if (v is uint)
+                                       return new UIntConstant (t, (uint) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Long:
+                               if (v is long)
+                                       return new LongConstant (t, (long) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.ULong:
+                               if (v is ulong)
+                                       return new ULongConstant (t, (ulong) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Float:
+                               if (v is float)
+                                       return new FloatConstant (t, (float) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Double:
+                               if (v is double)
+                                       return new DoubleConstant (t, (double) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Short:
+                               if (v is short)
+                                       return new ShortConstant (t, (short) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.UShort:
+                               if (v is ushort)
+                                       return new UShortConstant (t, (ushort) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.SByte:
+                               if (v is sbyte)
+                                       return new SByteConstant (t, (sbyte) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Byte:
+                               if (v is byte)
+                                       return new ByteConstant (t, (byte) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Char:
+                               if (v is char)
+                                       return new CharConstant (t, (char) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Bool:
+                               if (v is bool)
+                                       return new BoolConstant (t, (bool) v, loc);
+                               break;
+                       case BuiltinTypeSpec.Type.Decimal:
+                               if (v is decimal)
+                                       return new DecimalConstant (t, (decimal) v, loc);
+                               break;
+                       }
+
+                       if (t.IsEnum) {
+                               var real_type = EnumSpec.GetUnderlyingType (t);
+                               return new EnumConstant (CreateConstantFromValue (real_type, v, loc), t);
+                       }
+
+                       if (v == null) {
+                               if (t.IsNullableType)
+                                       return Nullable.LiftedNull.Create (t, loc);
+
+                               if (TypeSpec.IsReferenceType (t))
+                                       return new NullConstant (t, loc);
+                       }
+
+                       return null;
+               }
+
+
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = new Arguments (2);