Browse Source

2002-05-13 Miguel de Icaza <[email protected]>

	* ecore.cs (MemberLookupFinal): new version with all the
	parameters customizable.

	* expression.cs (New.DoResolve): Use MemberLookupFinal to locate
	constructors.  Return if the result value is null (as the error
	would have been flagged already by MemberLookupFinal)

	Do not allow instances of abstract classes or interfaces to be
	created.


	(Method.Emit): If the method is marked abstract and has a body,
	emit an error.

svn path=/trunk/mcs/; revision=4577
Miguel de Icaza 23 years ago
parent
commit
b2febae24c
7 changed files with 74 additions and 6 deletions
  1. 13 0
      mcs/errors/cs0144-2.cs
  2. 13 0
      mcs/errors/cs0144.cs
  3. 4 0
      mcs/errors/cs0500.cs
  4. 13 0
      mcs/mcs/ChangeLog
  5. 10 1
      mcs/mcs/class.cs
  6. 8 2
      mcs/mcs/ecore.cs
  7. 13 3
      mcs/mcs/expression.cs

+ 13 - 0
mcs/errors/cs0144-2.cs

@@ -0,0 +1,13 @@
+// cs0144.cs: can not create instances of abstract classes or interfaces
+// Line: 11
+interface X {
+	void A ();
+
+}
+
+class Demo {
+	static void Main ()
+	{
+		object x = new X ();
+	}
+}

+ 13 - 0
mcs/errors/cs0144.cs

@@ -0,0 +1,13 @@
+// cs0144.cs: can not create instances of abstract classes or interfaces
+// Line: 11
+abstract class X {
+	public abstract void B ();
+
+}
+
+class Demo {
+	static void Main ()
+	{
+		object x = new X ();
+	}
+}

+ 4 - 0
mcs/errors/cs0500.cs

@@ -0,0 +1,4 @@
+abstract class X {
+	public abstract void B () {
+	}
+}

+ 13 - 0
mcs/mcs/ChangeLog

@@ -1,5 +1,15 @@
 2002-05-13  Miguel de Icaza  <[email protected]>
 
+	* ecore.cs (MemberLookupFinal): new version with all the
+	parameters customizable.
+
+	* expression.cs (New.DoResolve): Use MemberLookupFinal to locate
+	constructors.  Return if the result value is null (as the error
+	would have been flagged already by MemberLookupFinal)
+
+	Do not allow instances of abstract classes or interfaces to be
+	created.
+	
 	* class.cs: (MethodSignature.InheritableMemberSignatureCompare):
 	We have to compare the assembly property here when dealing with
 	FamANDAssem and Assembly access modifiers, because we might be
@@ -7,6 +17,9 @@
 	getting TypeBuilders for types defined in other modules that are
 	part of this assembly).
 
+	(Method.Emit): If the method is marked abstract and has a body,
+	emit an error. 
+
 	(TypeContainer.DefineMembers): If both the defined member and the
 	parent name match are methods, then do not emit any warnings: let
 	the Method.Define routine take care of flagging warnings.  But if

+ 10 - 1
mcs/mcs/class.cs

@@ -2424,8 +2424,17 @@ namespace Mono.CSharp {
 			//
 			// abstract or extern methods have no bodies
 			//
-			if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
+			if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
+				if (Block != null){
+					if ((ModFlags & Modifiers.ABSTRACT) != 0){
+						Report.Error (
+							500, "Abstract method `" +
+							TypeManager.CSharpSignature (MethodBuilder) +
+							"' can not have a body");
+					}
+				}
 				return;
+			}
 
 			//
 			// Handle destructors specially

+ 8 - 2
mcs/mcs/ecore.cs

@@ -420,10 +420,16 @@ namespace Mono.CSharp {
 		/// </summary>
 		public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
 							    Location loc)
+		{
+			return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
+		}
+
+		public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
+							    MemberTypes mt, BindingFlags bf, Location loc)
 		{
 			Expression e;
 
-			e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+			e = MemberLookup (ec, t, name, mt, bf, loc);
 
 			if (e != null)
 				return e;
@@ -441,7 +447,7 @@ namespace Mono.CSharp {
 			}
 			
 			return null;
-		}
+		}			
 		
 		static EmptyExpression MyEmptyExpr;
 		static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)

+ 13 - 3
mcs/mcs/expression.cs

@@ -3894,6 +3894,13 @@ namespace Mono.CSharp {
 			
 			if (IsDelegate)
 				return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
+
+			if (type.IsInterface || type.IsAbstract){
+				Report.Error (
+					144, "It is not possible to create instances of interfaces " +
+					"or abstract classes");
+				return null;
+			}
 			
 			bool is_struct = false;
 			is_struct = type.IsSubclassOf (TypeManager.value_type);
@@ -3907,9 +3914,12 @@ namespace Mono.CSharp {
 				return this;
 			
 			Expression ml;
-			ml = MemberLookup (ec, type, ".ctor",
-					   MemberTypes.Constructor,
-					   AllBindingFlags | BindingFlags.DeclaredOnly, loc);
+			ml = MemberLookupFinal (ec, type, ".ctor",
+						MemberTypes.Constructor,
+						AllBindingFlags | BindingFlags.DeclaredOnly, loc);
+
+			if (ml == null)
+				return null;
 			
 			if (! (ml is MethodGroupExpr)){
 				if (!is_struct){