[Mono-list] [patch] mcs - using aliases

Radek Doulík rodo@ximian.com
08 Dec 2001 19:15:40 -0500


--=-L4AD3eIHbPDmankCvvgL
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


Hi all!

I am attaching my first contribution to mcs. Following patch implements
using aliases directive. I am looking forward to hear from you. Also
test-54.cs is attached.

Cheers
Radek

--=-L4AD3eIHbPDmankCvvgL
Content-Disposition: attachment; filename=diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-2

? box.cs
? diff
? log
? ua.cs
? ua1.cs
Index: TODO
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/mcs/TODO,v
retrieving revision 1.47
diff -u -p -r1.47 TODO
--- TODO	2001/12/07 04:52:18	1.47
+++ TODO	2001/12/08 20:14:48
@@ -155,33 +155,6 @@ PENDING TASKS
   properties, operators and indexers (currently we only do this
   on methods).
=20
-
-* Using Alias
-
-	Need to reset the aliases for each compilation unit, so an
-	alias defined in a file does not have any effect on another one:
-
-	File.cs
-	=3D=3D=3D=3D=3D=3D=3D
-	namespace A {
-		using X =3D Blah;
-
-		class Z : X {           <-- This X is `Blah'=20
-	}
-
-	File2.cs
-	namespace {
-		class Y : X {		<-- This X Is not `Blah'=20
-		}
-	}
-
-	I think we can implement Aliases by having an `Alias' context in all
-	the toplevel TypeContainers of a compilation unit.  The children typecont=
ainers
-	just chain to the parents to resolve the information.
-
-	The driver advances the Alias for each file compiled, so that each file
-	has its own alias set.
-
 * Handle volatile
=20
 * Support Re-Throw exceptions:
Index: class.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/mcs/class.cs,v
retrieving revision 1.110
diff -u -p -r1.110 class.cs
--- class.cs	2001/12/07 04:52:18	1.110
+++ class.cs	2001/12/08 20:14:48
@@ -1073,11 +1073,10 @@ namespace Mono.CSharp {
 		/// </summary>
 		public string LookupAlias (string name)
 		{
-			//
-			// Read the comments on `mcs/mcs/TODO' for details
-			//
-
-			return null;
+			if (my_namespace !=3D null)
+				return my_namespace.LookupAlias (name);
+			else
+				return null;
 		}
 	=09
 		/// <summary>
Index: cs-parser.jay
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/mcs/cs-parser.jay,v
retrieving revision 1.103
diff -u -p -r1.103 cs-parser.jay
--- cs-parser.jay	2001/12/07 04:52:18	1.103
+++ cs-parser.jay	2001/12/08 20:14:48
@@ -280,7 +280,7 @@ using_alias_directive
 	: USING IDENTIFIER ASSIGN=20
 	  namespace_or_type_name SEMICOLON
 	  {
-		  // FIXME : Need to implement actual action.
+		  current_namespace.UsingAlias ((string) $2, (string) $4);
 	  }
 	;
=20
Index: ecore.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/mcs/ecore.cs,v
retrieving revision 1.20
diff -u -p -r1.20 ecore.cs
--- ecore.cs	2001/12/05 13:11:27	1.20
+++ ecore.cs	2001/12/08 20:14:48
@@ -2435,12 +2435,13 @@ namespace Mono.CSharp {
 			// Stage 2: Lookup members
 			//
 			e =3D MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Locat=
ion);
-			if (e =3D=3D null){
+			if (e =3D=3D null) {
 				//
 				// Stage 3: Lookup symbol in the various namespaces.=20
 				//=20
 				Type t;
-			=09
+				string alias_value;
+
 				if ((t =3D ec.TypeContainer.LookupType (Name, true)) !=3D null)
 					return new TypeExpr (t);
=20
@@ -2451,11 +2452,16 @@ namespace Mono.CSharp {
 				// Since we are cheating: we only do the Alias lookup for
 				// namespaces if the name does not include any dots in it
 				//
-			=09
-				// IMPLEMENT ME.  Read mcs/mcs/TODO for ideas, or rewrite
-				// using NamespaceExprs (dunno how that fixes the alias
-				// per-file though).
-			=09
+
+				if (Name.IndexOf ('.') =3D=3D -1 && (alias_value =3D ec.TypeContainer.=
LookupAlias (Name)) !=3D null) {
+					// System.Console.WriteLine (Name + " --> " + alias_value);
+					if ((t =3D ec.TypeContainer.LookupType (alias_value, true)) !=3D null=
)
+						return new TypeExpr (t);
+
+					// we have alias value, but it isn't Type, so try if it's namespace
+					return new SimpleName (alias_value, Location);
+				}
+
 				// No match, maybe our parent can compose us
 				// into something meaningful.
 				//
Index: namespace.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/mcs/namespace.cs,v
retrieving revision 1.4
diff -u -p -r1.4 namespace.cs
--- namespace.cs	2001/11/19 00:44:09	1.4
+++ namespace.cs	2001/12/08 20:14:48
@@ -18,6 +18,7 @@ namespace Mono.CSharp {
 		Namespace parent;
 		string name;
 		ArrayList using_clauses;
+		Hashtable aliases;
 		bool decl_found =3D false;
 	=09
 		/// <summary>
@@ -85,6 +86,25 @@ namespace Mono.CSharp {
 			get {
 				return using_clauses;
 			}
+		}
+
+		public void UsingAlias (string alias, string namespace_or_type) {
+			if (aliases =3D=3D null)
+				aliases =3D new Hashtable ();
+			aliases [alias] =3D namespace_or_type;
+		}
+
+		public string LookupAlias (string alias) {
+			string value =3D null;
+
+			// System.Console.WriteLine ("Lookup " + alias + " in " + name);
+
+			if (aliases !=3D null)
+				value =3D (string) (aliases [alias]);
+			if (value =3D=3D null && Parent !=3D null)
+				value =3D Parent.LookupAlias (alias);
+
+			return value;
 		}
=20
 		/// <summary>

--=-L4AD3eIHbPDmankCvvgL--