Author: marek
Date: 2007-06-20 14:14:49 -0400 (Wed, 20 Jun 2007)
New Revision: 80342
Modified:
trunk/mcs/mcs/ChangeLog
trunk/mcs/mcs/cs-tokenizer.cs
trunk/mcs/mcs/decl.cs
trunk/mcs/mcs/ecore.cs
trunk/mcs/mcs/expression.cs
trunk/mcs/mcs/statement.cs
Log:
2007-06-20 Marek Safar <[EMAIL PROTECTED]>
* cs-tokenizer.cs: Removed var related stuff.
* ecore.cs (Expression.ResolveAsContextualType): Introduced new method.
(VarExpr): Changed to derive from SimpleName. VarExpr now behaves as
a type and a keyword at same time.
* decl.cs (MembeName.GetTypeExpression): Create VarExpr when type name
matches to "var".
* expression.cs (ImplicitlyTypedArrayCreation): New empty class for
implicitly typed arrays, more changes will follow.
* statement.cs (LocalInfo.Resolve): Resolve type as contextual type.
Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog 2007-06-20 18:00:23 UTC (rev 80341)
+++ trunk/mcs/mcs/ChangeLog 2007-06-20 18:14:49 UTC (rev 80342)
@@ -1,3 +1,19 @@
+2007-06-20 Marek Safar <[EMAIL PROTECTED]>
+
+ * cs-tokenizer.cs: Removed var related stuff.
+
+ * ecore.cs (Expression.ResolveAsContextualType): Introduced new method.
+ (VarExpr): Changed to derive from SimpleName. VarExpr now behaves as
+ a type and a keyword at same time.
+
+ * decl.cs (MembeName.GetTypeExpression): Create VarExpr when type name
+ matches to "var".
+
+ * expression.cs (ImplicitlyTypedArrayCreation): New empty class for
+ implicitly typed arrays, more changes will follow.
+
+ * statement.cs (LocalInfo.Resolve): Resolve type as contextual type.
+
2007-06-19 Marek Safar <[EMAIL PROTECTED]>
* ecore.cs (VarExpr): Removed Handled field.
Modified: trunk/mcs/mcs/cs-tokenizer.cs
===================================================================
--- trunk/mcs/mcs/cs-tokenizer.cs 2007-06-20 18:00:23 UTC (rev 80341)
+++ trunk/mcs/mcs/cs-tokenizer.cs 2007-06-20 18:14:49 UTC (rev 80342)
@@ -40,7 +40,6 @@
bool handle_assembly = false;
bool handle_constraints = false;
bool handle_typeof = false;
- bool handle_var = false;
Location current_location;
Location current_comment_location = Location.Null;
ArrayList escaped_identifiers = new ArrayList ();
@@ -180,16 +179,6 @@
}
}
- public bool VarParsing {
- get {
- return handle_var;
- }
-
- set {
- handle_var = value;
- }
- }
-
public XmlCommentState doc_state {
get { return xml_doc_state; }
set {
@@ -428,7 +417,6 @@
AddKeyword ("partial", Token.PARTIAL);
#if GMCS_SOURCE
AddKeyword ("where", Token.WHERE);
- AddKeyword ("var", Token.VAR);
#endif
}
@@ -506,8 +494,6 @@
if (!handle_constraints && res == Token.WHERE)
return -1;
- if (res == Token.VAR && (RootContext.Version <
LanguageVersion.LINQ || !handle_var || peek_token() != Token.IDENTIFIER))
- return -1;
#endif
return res;
Modified: trunk/mcs/mcs/decl.cs
===================================================================
--- trunk/mcs/mcs/decl.cs 2007-06-20 18:00:23 UTC (rev 80341)
+++ trunk/mcs/mcs/decl.cs 2007-06-20 18:14:49 UTC (rev 80342)
@@ -134,8 +134,11 @@
if (Left == null) {
if (TypeArguments != null)
return new SimpleName (Basename,
TypeArguments, Location);
- else
- return new SimpleName (Name, Location);
+
+ if (RootContext.Version == LanguageVersion.LINQ
&& Name == "var")
+ return new VarExpr (Name, Location);
+
+ return new SimpleName (Name, Location);
}
if (is_double_colon) {
Modified: trunk/mcs/mcs/ecore.cs
===================================================================
--- trunk/mcs/mcs/ecore.cs 2007-06-20 18:00:23 UTC (rev 80341)
+++ trunk/mcs/mcs/ecore.cs 2007-06-20 18:14:49 UTC (rev 80342)
@@ -229,6 +229,15 @@
}
//
+ // C# 3.0 introduced contextual keywords (var) which behaves
like a type if type with
+ // same name exists or as a keyword when no type was found
+ //
+ public virtual TypeExpr ResolveAsContextualType
(IResolveContext rc, bool silent)
+ {
+ return ResolveAsTypeTerminal (rc, silent);
+ }
+
+ //
// This is used to resolve the expression as a type, a null
// value will be returned if the expression is not a type
// reference
@@ -4975,22 +4984,65 @@
EmitLoadAddress (ec);
}
}
-
- public sealed class VarExpr : Expression
+
+ ///
+ /// Handles `var' contextual keyword; var becomes a keyword only
+ /// if no type called var exists in a variable scope
+ ///
+ public class VarExpr : SimpleName
{
- public VarExpr (Location loc)
+ // Used for error reporting only
+ ArrayList initializer;
+
+ public VarExpr (string name, Location loc)
+ : base (name, loc)
{
- this.loc = loc;
}
-
- public override Expression DoResolve (EmitContext ec)
+
+ public ArrayList VariableInitializer {
+ set {
+ this.initializer = value;
+ }
+ }
+
+ public override Expression DoResolveLValue (EmitContext ec,
Expression right_side)
{
- return null;
+ if (type != null)
+ throw new InternalErrorException ("An
implicitly typed local variable could not be redefined");
+
+ type = right_side.Type;
+ if (type == TypeManager.null_type || type ==
TypeManager.void_type) {
+ Report.Error (815, loc, "An implicitly typed
local variable declaration cannot be initialized with `{0}'",
+ TypeManager.CSharpName (type));
+ return null;
+ }
+
+ return this;
}
-
- public override void Emit (EmitContext ec)
+
+ public override TypeExpr ResolveAsContextualType
(IResolveContext rc, bool silent)
{
+ TypeExpr te = base.ResolveAsContextualType (rc, true);
+ if (te != null)
+ return te;
+
+ if (initializer == null)
+ return null;
+
+ // TODO: refactor, the error is reported too many times
+ if (initializer.Count > 1) {
+ Location loc =
((Mono.CSharp.CSharpParser.VariableDeclaration)initializer [1]).Location;
+ Report.Error (819, loc, "An implicitly typed
local variable declaration cannot include multiple declarators");
+ return null;
+ }
+
+ Expression variable_initializer =
((Mono.CSharp.CSharpParser.VariableDeclaration)initializer
[0]).expression_or_array_initializer;
+ if (variable_initializer == null) {
+ Report.Error (818, loc, "An implicitly typed
local variable declarator must include an initializer");
+ return null;
+ }
+
+ return null;
}
}
-
}
Modified: trunk/mcs/mcs/expression.cs
===================================================================
--- trunk/mcs/mcs/expression.cs 2007-06-20 18:00:23 UTC (rev 80341)
+++ trunk/mcs/mcs/expression.cs 2007-06-20 18:14:49 UTC (rev 80342)
@@ -3659,10 +3659,8 @@
if (type == null) {
VarExpr ve = local_info.Type as VarExpr;
if (ve != null) {
- if (ve.Type != null)
- throw new
InternalErrorException ("Could not redefine implicitly typed local variable");
-
- type = local_info.VariableType =
right_side.Type;
+ ve.DoResolveLValue (ec, right_side);
+ type = local_info.VariableType =
ve.Type;
}
}
@@ -5696,6 +5694,11 @@
//
bool LookupType (EmitContext ec)
{
+ if (requested_base_type == null) {
+ Report.Error (622, loc, "Can only use array
initializer expressions to assign to array types. Try using a new expression
instead");
+ return false;
+ }
+
StringBuilder array_qualifier = new StringBuilder
(rank);
//
@@ -6199,6 +6202,14 @@
}
}
+ public class ImplicitlyTypedArrayCreation : ArrayCreation
+ {
+ public ImplicitlyTypedArrayCreation (string rank, ArrayList
initializers, Location loc)
+ : base (null, rank, initializers, loc)
+ {
+ }
+ }
+
public sealed class CompilerGeneratedThis : This
{
public static This Instance = new CompilerGeneratedThis ();
Modified: trunk/mcs/mcs/statement.cs
===================================================================
--- trunk/mcs/mcs/statement.cs 2007-06-20 18:00:23 UTC (rev 80341)
+++ trunk/mcs/mcs/statement.cs 2007-06-20 18:14:49 UTC (rev 80342)
@@ -1243,7 +1243,7 @@
public bool Resolve (EmitContext ec)
{
if (VariableType == null) {
- TypeExpr texpr = Type.ResolveAsTypeTerminal
(ec, false);
+ TypeExpr texpr = Type.ResolveAsContextualType
(ec, false);
if (texpr == null)
return false;
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches