2004-08-06 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mcs / mbas / const.cs
index e6643de5733908d9207b7bc7d2053abd80b0e1f2..9e197c55642ba293f8b0d497c0ce99ddfc55e2bb 100644 (file)
@@ -17,7 +17,8 @@
 //     function aborts because it requires its argument to be of the same type
 //
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
+
 
        using System;
        using System.Reflection;
@@ -36,11 +37,11 @@ namespace Mono.CSharp {
                bool in_transit = false;
 
                public const int AllowedModifiers =
-                       Modifiers.NEW |
                        Modifiers.PUBLIC |
                        Modifiers.PROTECTED |
                        Modifiers.INTERNAL |
-                       Modifiers.PRIVATE;
+                       Modifiers.PRIVATE |
+                       Modifiers.SHADOWS;
 
                public Const (Expression constant_type, string name, Expression expr, int mod_flags,
                              Attributes attrs, Location loc)
@@ -85,24 +86,75 @@ namespace Mono.CSharp {
                        if (!TypeManager.IsBuiltinType (type) &&
                            (!type.IsSubclassOf (TypeManager.enum_type))) {
                                Report.Error (
-                                       -3, Location,
+                                       30424, Location,
                                        "Constant type is not valid (only system types are allowed)");
                                return false;
                        }
 
+                       // if no type is declared expicitely 
+                       // set the expression type as the type of constant
+                       if (type == TypeManager.object_type){
+                               if (Expr is IntLiteral)
+                                       type = TypeManager.int32_type;
+                               else if (Expr is UIntLiteral)
+                                       type = TypeManager.uint32_type;
+                               else if (Expr is LongLiteral)
+                                       type = TypeManager.int64_type;
+                               else if (Expr is ULongLiteral)
+                                       type = TypeManager.uint64_type;
+                               else if (Expr is FloatLiteral)
+                                       type = TypeManager.float_type;
+                               else if (Expr is DoubleLiteral)
+                                       type = TypeManager.double_type;
+                               else if (Expr is StringLiteral)
+                                       type = TypeManager.string_type;
+                               else if (Expr is ShortLiteral)
+                                       type = TypeManager.short_type;
+                               else if (Expr is UShortConstant)
+                                       type = TypeManager.ushort_type;
+                               else if (Expr is SByteConstant)
+                                       type = TypeManager.sbyte_type;
+                               else if (Expr is ByteConstant)
+                                       type = TypeManager.byte_type;
+                               else if (Expr is CharConstant)
+                                       type = TypeManager.char_type;
+                               else if (Expr is BoolConstant)
+                                       type = TypeManager.bool_type;
+                               else if (Expr is DateConstant)
+                                       type = TypeManager.date_type;
+                       }
                        Type ptype = parent.TypeBuilder.BaseType;
 
                        if (ptype != null) {
-                               MemberInfo [] mi = TypeContainer.FindMembers (
+                               MemberList list = TypeContainer.FindMembers (
                                        ptype, MemberTypes.Field, BindingFlags.Public,
                                        Type.FilterName, Name);
                                
-                               if (mi == null || mi.Length == 0)
-                                       if ((ModFlags & Modifiers.NEW) != 0)
-                                               WarningNotHiding (parent);
+                               if ((list.Count > 0) && ((ModFlags & Modifiers.SHADOWS) == 0))
+                                       Report.Warning (
+                                               40004, 2, Location, 
+                                               "Const '" + Name + "' should be declared " +
+                                               "Shadows since the base type '" + ptype.Name + 
+                                               "' has a Const with same name");
+                               if (list.Count == 0) {
+                                       // if a member of module is not inherited from Object class
+                                       // can not be declared protected
+                                       if ((parent is Module) && ((ModFlags & Modifiers.PROTECTED) != 0))
+                                               Report.Error (30593, Location,
+                                                       "'Const' inside a 'Module' can not be " +
+                                                       "declared as 'Protected'");
+
+                                       /*if ((ModFlags & Modifiers.NEW) != 0)
+                                               WarningNotHiding (parent);*/
+                               }
+                       } 
+                       /*else if ((ModFlags & Modifiers.NEW) != 0)
+                               WarningNotHiding (parent);*/
 
-                       } else if ((ModFlags & Modifiers.NEW) != 0)
-                               WarningNotHiding (parent);
+                       if ((parent is Struct) && ((ModFlags & Modifiers.PROTECTED) != 0))
+                               Report.Error (30435, Location,
+                                       "'Const' inside a 'Structure' can not be " +
+                                       "declared as 'Protected'");
 
                        FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
 
@@ -136,7 +188,7 @@ namespace Mono.CSharp {
 
                        if (Expr == null) {
                                if (errors == Report.Errors)
-                                       Report.Error (150, Location, "A constant value is expected");
+                                       Report.Error (30059, Location, "A constant value is expected");
                                return null;
                        }
 
@@ -148,8 +200,9 @@ namespace Mono.CSharp {
                                        Expr = un_expr.Expr;
                                else if ((ch_expr != null) && (ch_expr.Expr is Constant))
                                        Expr = ch_expr.Expr;
-                               else {
-                                       Report.Error (150, Location, "A constant value is expected");
+                               else \r
+                               {
+                                       Report.Error (30059, Location, "A constant value is expected");
                                        return null;
                                }
                        }
@@ -211,7 +264,7 @@ namespace Mono.CSharp {
 
                        if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
                                return null;
-
+                       
                        return ConstantValue;
                }
                
@@ -228,5 +281,3 @@ namespace Mono.CSharp {
                }
        }
 }
-
-