Browse Source

2002-03-12 Miguel de Icaza <[email protected]>

	We could also solve this problem by having a separate path for
	performing type lookups, instead of DoResolve, we could have a
	ResolveType entry point, and only participating pieces of the
	production (simplename, deref, array) would implement this.

	* codegen.cs (EmitContext): New field OnlyLookupTypes used to
	signal SimpleName to only resolve type names and not attempt to
	resolve anything else.

	* expression.cs (Cast): Set the flag.

	* ecore.cs (SimpleName): Use the OnlyLookupTypes flag

	* Everywhere: Use ec.DeclSpace on calls to LookupType, as this
	makes the use more obvious of the DeclSpace.  The
	ec.TypeContainer.TypeBuilder is now only used to pull the
	TypeBuilder for it.

	My theory is that I can get rid of the TypeBuilder completely from
	the EmitContext, and have typecasts where it is used (from
	DeclSpace to where it matters).

	The only pending problem is that the code that implements Aliases
	is on TypeContainer, and probably should go in DeclSpace.

svn path=/trunk/mcs/; revision=3075
Miguel de Icaza 24 years ago
parent
commit
55cebe19cf
8 changed files with 119 additions and 40 deletions
  1. 25 0
      mcs/mcs/ChangeLog
  2. 11 0
      mcs/mcs/TODO
  3. 2 2
      mcs/mcs/attribute.cs
  4. 8 0
      mcs/mcs/codegen.cs
  5. 26 20
      mcs/mcs/ecore.cs
  6. 18 12
      mcs/mcs/expression.cs
  7. 6 6
      mcs/mcs/statement.cs
  8. 23 0
      mcs/tests/test-85.cs

+ 25 - 0
mcs/mcs/ChangeLog

@@ -1,5 +1,18 @@
 2002-03-12  Miguel de Icaza  <[email protected]>
 
+	We could also solve this problem by having a separate path for
+	performing type lookups, instead of DoResolve, we could have a
+	ResolveType entry point, and only participating pieces of the
+	production (simplename, deref, array) would implement this. 
+	
+	* codegen.cs (EmitContext): New field OnlyLookupTypes used to
+	signal SimpleName to only resolve type names and not attempt to
+	resolve anything else.
+
+	* expression.cs (Cast): Set the flag.
+
+	* ecore.cs (SimpleName): Use the OnlyLookupTypes flag
+
 	* class.cs: Only report 108 if there is no `new' modifier.
 
 	* cs-parser.jay: rework foreach statement to work with the new
@@ -11,6 +24,18 @@
 
 2002-03-11  Miguel de Icaza  <[email protected]>
 
+	* Everywhere: Use ec.DeclSpace on calls to LookupType, as this
+	makes the use more obvious of the DeclSpace.  The
+	ec.TypeContainer.TypeBuilder is now only used to pull the
+	TypeBuilder for it.
+
+	My theory is that I can get rid of the TypeBuilder completely from
+	the EmitContext, and have typecasts where it is used (from
+	DeclSpace to where it matters).  
+
+	The only pending problem is that the code that implements Aliases
+	is on TypeContainer, and probably should go in DeclSpace.
+
 	* ecore.cs (SimpleName.SimpleNameResolve): Perform local variable
 	lookups here, instead of doing that at parse time.  This means
 	that our grammar will not introduce `LocalVariableReferences' as

+ 11 - 0
mcs/mcs/TODO

@@ -183,9 +183,20 @@ PENDING TASKS
 * Maybe track event usage?  Currently I am not tracking these, although they
   are fields.
 
+
 OPTIMIZATIONS
 -------------
 
+* Dropping TypeContainer as an argument to EmitContext
+
+ 	My theory is that I can get rid of the TypeBuilder completely from
+	the EmitContext, and have typecasts where it is used (from
+	DeclSpace to where it matters).  
+
+	The only pending problem is that the code that implements Aliases
+	is on TypeContainer, and probably should go in DeclSpace.
+
+
 * Use of local temporary in UnaryMutator
 
 	We should get rid of the Localtemporary there for some cases

+ 2 - 2
mcs/mcs/attribute.cs

@@ -75,7 +75,7 @@ namespace Mono.CSharp {
 			else if (Name.LastIndexOf ("Attribute") == 0)
 				name = Name + "Attribute";
 
-			Type = RootContext.LookupType (ec.TypeContainer, name, false, Location);
+			Type = RootContext.LookupType (ec.DeclSpace, name, false, Location);
 
 			if (Type == null) {
 				Report.Error (
@@ -519,7 +519,7 @@ namespace Mono.CSharp {
 			else if (Name.LastIndexOf ("Attribute") == 0)
 				attr_name = Name + "Attribute";
 			
-			Type = RootContext.LookupType (ec.TypeContainer, attr_name, false, Location);
+			Type = RootContext.LookupType (ec.DeclSpace, attr_name, false, Location);
 
 			if (Type == null) {
 				Report.Error (246, Location, "Could not find attribute '" + Name + "' (are you" +

+ 8 - 0
mcs/mcs/codegen.cs

@@ -183,6 +183,13 @@ namespace Mono.CSharp {
 		///   Location for this EmitContext
 		/// </summary>
 		public Location loc;
+
+		/// <summary>
+		///   Used to "flag" the resolution process to only lookup types,
+		///   and nothing else.  This is an out-of-band communication
+		///   path to SimpleName from the cast operation.
+		/// </summary>
+		public bool OnlyLookupTypes;
 		
 		public EmitContext (TypeContainer parent, DeclSpace ds, Location l, ILGenerator ig,
 				    Type return_type, int code_flags, bool is_constructor)
@@ -200,6 +207,7 @@ namespace Mono.CSharp {
 			CurrentBlock = null;
 			ContainerType = parent.TypeBuilder;
 			InUnsafe = ((parent.ModFlags | code_flags) & Modifiers.UNSAFE) != 0;
+			OnlyLookupTypes = false;
 			loc = l;
 			
 			if (ReturnType == TypeManager.void_type)

+ 26 - 20
mcs/mcs/ecore.cs

@@ -2971,28 +2971,31 @@ namespace Mono.CSharp {
 			//
 			// Stage 1: Performed by the parser (binding to locals or parameters).
 			//
-			Block current_block = ec.CurrentBlock;
-			if (current_block != null && current_block.IsVariableDefined (Name)){
-				LocalVariableReference var;
-				
-				var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
-
-				return var.Resolve (ec);
-			}
+			if (!ec.OnlyLookupTypes){
+				Block current_block = ec.CurrentBlock;
+				if (current_block != null && current_block.IsVariableDefined (Name)){
+					LocalVariableReference var;
+					
+					var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
+					
+					return var.Resolve (ec);
+				}
 			
-			//
-			// Stage 2: Lookup members 
-			//
-
-			//
-			// For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
-			// Hence we have two different cases
-			//
-			e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
+				//
+				// Stage 2: Lookup members 
+				//
 
-			if (e == null && ec.TypeContainer.TypeBuilder != null)
-				e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
+				//
+				// For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
+				// Hence we have two different cases
+				//
+				e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
+				
+				if (e == null && ec.TypeContainer.TypeBuilder != null)
+					e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
+			}
 
+			// Continuation of stage 2
 			if (e == null){
 				//
 				// Stage 3: Lookup symbol in the various namespaces. 
@@ -3000,7 +3003,7 @@ namespace Mono.CSharp {
 				DeclSpace ds = ec.DeclSpace;
 				Type t;
 				string alias_value;
-				
+
 				if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
 					return new TypeExpr (t);
 				
@@ -3033,6 +3036,9 @@ namespace Mono.CSharp {
 			if (e is TypeExpr)
 				return e;
 
+			if (ec.OnlyLookupTypes)
+				return null;
+			
 			if (e is FieldExpr){
 				FieldExpr fe = (FieldExpr) e;
 				FieldInfo fi = fe.FieldInfo;

+ 18 - 12
mcs/mcs/expression.cs

@@ -844,7 +844,7 @@ namespace Mono.CSharp {
 
 		public override Expression DoResolve (EmitContext ec)
 		{
-			probe_type = RootContext.LookupType (ec.TypeContainer, ProbeType, false, loc);
+			probe_type = RootContext.LookupType (ec.DeclSpace, ProbeType, false, loc);
 
 			if (probe_type == null)
 				return null;
@@ -1205,9 +1205,15 @@ namespace Mono.CSharp {
 			if (expr == null)
 				return null;
 
+			bool old_state = ec.OnlyLookupTypes;
+			ec.OnlyLookupTypes = true;
 			target_type = target_type.Resolve (ec);
-			if (target_type == null)
+			ec.OnlyLookupTypes = old_state;
+			
+			if (target_type == null){
+				Report.Error (-10, loc, "Can not resolve type");
 				return null;
+			}
 
 			if (target_type.eclass != ExprClass.Type){
 				report118 (loc, target_type, "class");
@@ -2560,7 +2566,7 @@ namespace Mono.CSharp {
 		//
 		public override Expression DoResolve (EmitContext ec)
 		{
-			type = pars.GetParameterInfo (ec.TypeContainer, idx, out is_ref);
+			type = pars.GetParameterInfo (ec.DeclSpace, idx, out is_ref);
 			eclass = ExprClass.Variable;
 
 			return this;
@@ -3717,7 +3723,7 @@ namespace Mono.CSharp {
 
 		public override Expression DoResolve (EmitContext ec)
 		{
-			type = RootContext.LookupType (ec.TypeContainer, RequestedType, false, loc);
+			type = RootContext.LookupType (ec.DeclSpace, RequestedType, false, loc);
 			
 			if (type == null)
 				return null;
@@ -4033,7 +4039,7 @@ namespace Mono.CSharp {
 			}
 			
 			underlying_type = RootContext.LookupType (
-				ec.TypeContainer, RequestedType, false, loc);
+				ec.DeclSpace, RequestedType, false, loc);
 			
 			//
 			// We use this to store all the date values in the order in which we
@@ -4094,10 +4100,10 @@ namespace Mono.CSharp {
 			string array_type = FormArrayType (RequestedType, arg_count, Rank);
 			string element_type = FormElementType (RequestedType, arg_count, Rank);
 
-			type = RootContext.LookupType (ec.TypeContainer, array_type, false, loc);
+			type = RootContext.LookupType (ec.DeclSpace, array_type, false, loc);
 			
 			array_element_type = RootContext.LookupType (
-				ec.TypeContainer, element_type, false, loc);
+				ec.DeclSpace, element_type, false, loc);
 			
 			if (type == null)
 				return null;
@@ -4524,7 +4530,7 @@ namespace Mono.CSharp {
 		public override Expression DoResolve (EmitContext ec)
 		{
 			typearg = RootContext.LookupType (
-				ec.TypeContainer, QueriedType, false, loc);
+				ec.DeclSpace, QueriedType, false, loc);
 
 			if (typearg == null)
 				return null;
@@ -4558,7 +4564,7 @@ namespace Mono.CSharp {
 		public override Expression DoResolve (EmitContext ec)
 		{
 			type_queried = RootContext.LookupType (
-				ec.TypeContainer, QueriedType, false, loc);
+				ec.DeclSpace, QueriedType, false, loc);
 			if (type_queried == null)
 				return null;
 
@@ -4618,7 +4624,7 @@ namespace Mono.CSharp {
 
 			SimpleName sn = (SimpleName) left_original;
 
-			Type t = RootContext.LookupType (ec.TypeContainer, sn.Name, true, loc);
+			Type t = RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc);
 			if (t != null)
 				return true;
 
@@ -5709,7 +5715,7 @@ namespace Mono.CSharp {
 			}
 			
 			type = RootContext.LookupType (
-				ec.TypeContainer, left.Type.FullName + dim, false, loc);
+				ec.DeclSpace, left.Type.FullName + dim, false, loc);
 			if (type == null)
 				return null;
 
@@ -5837,7 +5843,7 @@ namespace Mono.CSharp {
 				return null;
 			}
 			
-			otype = RootContext.LookupType (ec.TypeContainer, t, false, loc);
+			otype = RootContext.LookupType (ec.DeclSpace, t, false, loc);
 
 			if (otype == null)
 				return null;

+ 6 - 6
mcs/mcs/statement.cs

@@ -971,7 +971,7 @@ namespace Mono.CSharp {
 		/// </remarks>
 		public int EmitMeta (EmitContext ec, Block toplevel, int count)
 		{
-			TypeContainer tc = ec.TypeContainer;
+			DeclSpace ds = ec.DeclSpace;
 			ILGenerator ig = ec.ig;
 				
 			//
@@ -985,7 +985,7 @@ namespace Mono.CSharp {
 					VariableInfo vi = (VariableInfo) de.Value;
 					Type t;
 
-					t = RootContext.LookupType (tc, vi.Type, false, vi.Location);
+					t = RootContext.LookupType (ds, vi.Type, false, vi.Location);
 					if (t == null)
 						continue;
 
@@ -1748,7 +1748,7 @@ namespace Mono.CSharp {
 			ILGenerator ig = ec.ig;
 			Type t;
 			
-			t = RootContext.LookupType (ec.TypeContainer, type, false, loc);
+			t = RootContext.LookupType (ec.DeclSpace, type, false, loc);
 			if (t == null)
 				return false;
 
@@ -1939,7 +1939,7 @@ namespace Mono.CSharp {
 
 			bool old_in_catch = ec.InCatch;
 			ec.InCatch = true;
-			DeclSpace ds = ec.TypeContainer;
+			DeclSpace ds = ec.DeclSpace;
 			
 			foreach (Catch c in Specific){
 				Type catch_type = RootContext.LookupType (ds, c.Type, false, c.Location);
@@ -2015,7 +2015,7 @@ namespace Mono.CSharp {
 			ILGenerator ig = ec.ig;
 			Expression [] converted_vars;
 			bool need_conv = false;
-			Type type = RootContext.LookupType (ec.TypeContainer, type_name, false, loc);
+			Type type = RootContext.LookupType (ec.DeclSpace, type_name, false, loc);
 			int i = 0;
 
 			if (type == null)
@@ -2620,7 +2620,7 @@ namespace Mono.CSharp {
 			if (expr == null)
 				return false;
 
-			var_type = RootContext.LookupType (ec.TypeContainer, type, false, loc);
+			var_type = RootContext.LookupType (ec.DeclSpace, type, false, loc);
 			if (var_type == null)
 				return false;
 			

+ 23 - 0
mcs/tests/test-85.cs

@@ -0,0 +1,23 @@
+//
+// This test declares a field variable called `UnmanagedType' of type
+// UnmanagedType.
+//
+// The test is used to test the cast of 0 to UnmanagedType, as before
+// that would have been resolved to a variable instead of a type.
+//
+using System.Runtime.InteropServices;
+
+class X {
+	static UnmanagedType UnmanagedType;
+
+	static int Main ()
+	{
+		UnmanagedType = (UnmanagedType) 0;
+
+		if (UnmanagedType != 0)
+			return 1;
+
+		return 0;
+	}
+}
+