Author: marek
Date: 2007-08-14 11:42:24 -0400 (Tue, 14 Aug 2007)
New Revision: 84087
Modified:
trunk/mcs/mcs/ChangeLog
trunk/mcs/mcs/assign.cs
trunk/mcs/mcs/codegen.cs
trunk/mcs/mcs/expression.cs
trunk/mcs/mcs/statement.cs
trunk/mcs/mcs/typemanager.cs
Log:
2007-08-14 Marek Safar <[EMAIL PROTECTED]>
** C# 3.0 Object and collection initializers (major re-write)
* assign.cs (DoResolve): Initializers are not assign related.
* codegen.cs (EmitContext.CurrentInitializerVariable): Holds a varible
used during collection or object initialization.
* expression.cs (Error_InvalidArguments): Add initializers specific
messages. More will come later because it requires some general
refactoring.
(New.DoResolve): Better error handling for unsafe types.
(EmptyExpressionStatement): New class.
(ElementInitializer): An object initializer expression.
(CollectionElementInitializer): A collection initializer expression.
(CollectionOrObjectInitializers): A block of object or collection
initializers.
(NewInitialize): New expression with element/object initializers.
* statement.cs: Reverted object/collection initializer hacks.
* typemanager.cs (CSharpName): Filter __arglist type.
Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog 2007-08-14 15:41:12 UTC (rev 84086)
+++ trunk/mcs/mcs/ChangeLog 2007-08-14 15:42:24 UTC (rev 84087)
@@ -1,3 +1,27 @@
+2007-08-14 Marek Safar <[EMAIL PROTECTED]>
+
+ ** C# 3.0 Object and collection initializers (major re-write)
+
+ * assign.cs (DoResolve): Initializers are not assign related.
+
+ * codegen.cs (EmitContext.CurrentInitializerVariable): Holds a varible
+ used during collection or object initialization.
+
+ * expression.cs (Error_InvalidArguments): Add initializers specific
+ messages. More will come later because it requires some general
+ refactoring.
+ (New.DoResolve): Better error handling for unsafe types.
+ (EmptyExpressionStatement): New class.
+ (ElementInitializer): An object initializer expression.
+ (CollectionElementInitializer): A collection initializer expression.
+ (CollectionOrObjectInitializers): A block of object or collection
+ initializers.
+ (NewInitialize): New expression with element/object initializers.
+
+ * statement.cs: Reverted object/collection initializer hacks.
+
+ * typemanager.cs (CSharpName): Filter __arglist type.
+
2007-08-09 Marek Safar <[EMAIL PROTECTED]>
** C# 3.0 Anonymous Types (update to the latest standard)
Modified: trunk/mcs/mcs/assign.cs
===================================================================
--- trunk/mcs/mcs/assign.cs 2007-08-14 15:41:12 UTC (rev 84086)
+++ trunk/mcs/mcs/assign.cs 2007-08-14 15:42:24 UTC (rev 84087)
@@ -366,11 +366,6 @@
if (target == null)
return null;
- // Handle initializations e.g. Person p = new Person ()
{ Name = "Scott" };
- IInitializable initializer = source as IInitializable;
- if (initializer != null && !initializer.Initialize (ec,
target))
- return null;
-
bool same_assignment = (embedded != null) ?
embedded.Target.Equals(target) : source.Equals (target);
if (same_assignment) {
Report.Warning (1717, 3, loc, "Assignment made
to same variable; did you mean to assign something else?");
Modified: trunk/mcs/mcs/codegen.cs
===================================================================
--- trunk/mcs/mcs/codegen.cs 2007-08-14 15:41:12 UTC (rev 84086)
+++ trunk/mcs/mcs/codegen.cs 2007-08-14 15:42:24 UTC (rev 84087)
@@ -220,6 +220,11 @@
/// </summary>
public class EmitContext : IResolveContext {
+ //
+ // Holds a varible used during collection or object
initialization.
+ //
+ public Expression CurrentInitializerVariable;
+
DeclSpace decl_space;
public DeclSpace TypeContainer;
@@ -685,6 +690,9 @@
bool unreachable;
if (ResolveTopBlock (null, block, md.ParameterInfo, md,
out unreachable)){
+ if (Report.Errors > 0)
+ return;
+
EmitMeta (block);
current_phase = Phase.Emitting;
Modified: trunk/mcs/mcs/expression.cs
===================================================================
--- trunk/mcs/mcs/expression.cs 2007-08-14 15:41:12 UTC (rev 84086)
+++ trunk/mcs/mcs/expression.cs 2007-08-14 15:42:24 UTC (rev 84087)
@@ -4286,10 +4286,20 @@
static void Error_InvalidArguments (Location loc, int idx,
MethodBase method,
Type delegate_type,
Argument a, ParameterData expected_par)
{
- if (delegate_type == null)
+ if (a is
CollectionElementInitializer.ElementInitializerArgument) {
+ Report.SymbolRelatedToPreviousError (method);
+ if ((expected_par.ParameterModifier (idx) &
Parameter.Modifier.ISBYREF) != 0) {
+ Report.Error (1954, loc, "The best
overloaded collection initalizer method `{0}' cannot have 'ref', or `out'
modifier",
+ TypeManager.CSharpSignature
(method));
+ return;
+ }
+ Report.Error (1950, loc, "The best overloaded
collection initalizer method `{0}' has some invalid arguments",
+ TypeManager.CSharpSignature (method));
+ } else if (delegate_type == null) {
+ Report.SymbolRelatedToPreviousError (method);
Report.Error (1502, loc, "The best overloaded
method match for `{0}' has some invalid arguments",
- TypeManager.CSharpSignature
(method));
- else
+ TypeManager.CSharpSignature
(method));
+ } else
Report.Error (1594, loc, "Delegate `{0}' has
some invalid arguments",
TypeManager.CSharpName (delegate_type));
@@ -5119,6 +5129,12 @@
return null;
}
+ if (type.IsPointer) {
+ Report.Error (1919, loc, "Unsafe type `{0}'
cannot be used in an object creation expression",
+ TypeManager.CSharpName (type));
+ return null;
+ }
+
if (Arguments == null) {
Expression c = Constantify (type);
if (c != null)
@@ -8199,7 +8215,37 @@
type = t;
}
}
+
+ //
+ // Empty statement expression
+ //
+ public sealed class EmptyExpressionStatement : ExpressionStatement
+ {
+ public static readonly EmptyExpressionStatement Instance = new
EmptyExpressionStatement ();
+ private EmptyExpressionStatement ()
+ {
+ type = TypeManager.object_type;
+ eclass = ExprClass.Value;
+ loc = Location.Null;
+ }
+
+ public override void EmitStatement (EmitContext ec)
+ {
+ // Do nothing
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ // Do nothing
+ }
+ }
+
public class UserCast : Expression {
MethodBase method;
Expression source;
@@ -8502,138 +8548,263 @@
target.t = t.Clone (clonectx);
}
}
-
- public interface IInitializable
+
+ //
+ // An object initializer expression
+ //
+ public class ElementInitializer : Expression
{
- bool Initialize (EmitContext ec, Expression target);
- }
-
- public class Initializer
- {
+ Expression initializer;
public readonly string Name;
- public readonly object Value;
- public Initializer (string name, Expression value)
+ public ElementInitializer (string name, Expression initializer,
Location loc)
{
- Name = name;
- Value = value;
+ this.Name = name;
+ this.initializer = initializer;
+ this.loc = loc;
}
- public Initializer (string name, IInitializable value)
+ protected override void CloneTo (CloneContext clonectx,
Expression t)
{
- Name = name;
- Value = value;
+ if (initializer == null)
+ return;
+
+ ElementInitializer target = (ElementInitializer) t;
+ target.initializer = initializer.Clone (clonectx);
}
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ if (initializer == null)
+ return EmptyExpressionStatement.Instance;
+
+ MemberExpr element_member = MemberLookupFinal (ec,
ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
+ Name, MemberTypes.Field | MemberTypes.Property,
BindingFlags.Public | BindingFlags.Instance, loc) as MemberExpr;
+
+ if (element_member == null)
+ return null;
+
+ element_member.InstanceExpression =
ec.CurrentInitializerVariable;
+
+ if (initializer is CollectionOrObjectInitializers) {
+ Expression previous =
ec.CurrentInitializerVariable;
+ ec.CurrentInitializerVariable = element_member;
+ initializer = initializer.Resolve (ec);
+ ec.CurrentInitializerVariable = previous;
+ return initializer;
+ }
+
+ return new Assign (element_member, initializer,
loc).Resolve (ec);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ throw new NotSupportedException ("Should not be
reached");
+ }
}
- public class ObjectInitializer : IInitializable
+ //
+ // A collection initializer expression
+ //
+ public class CollectionElementInitializer : Expression
{
- readonly ArrayList initializers;
- public ObjectInitializer (ArrayList initializers)
+ public class ElementInitializerArgument : Argument
{
- this.initializers = initializers;
+ public ElementInitializerArgument (Expression e)
+ : base (e)
+ {
+ }
}
+
+ ArrayList arguments;
+
+ public CollectionElementInitializer (Expression argument)
+ {
+ arguments = new ArrayList (1);
+ arguments.Add (argument);
+ this.loc = argument.Location;
+ }
+
+ public CollectionElementInitializer (ArrayList arguments,
Location loc)
+ {
+ this.arguments = arguments;
+ this.loc = loc;
+ }
+
+ protected override void CloneTo (CloneContext clonectx,
Expression t)
+ {
+ CollectionElementInitializer target =
(CollectionElementInitializer) t;
+ ArrayList t_arguments = target.arguments = new
ArrayList (arguments.Count);
+ foreach (Expression e in arguments)
+ t_arguments.Add (e.Clone (clonectx));
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ // TODO: We should call a constructor which takes
element counts argument,
+ // for know types like List<T>, Dictionary<T, U>
+
+ for (int i = 0; i < arguments.Count; ++i)
+ arguments [i] = new ElementInitializerArgument
((Expression)arguments [i]);
+
+ Expression add_method = new Invocation (
+ new MemberAccess
(ec.CurrentInitializerVariable, "Add", loc),
+ arguments);
+
+ add_method = add_method.Resolve (ec);
+
+ return add_method;
+ }
- public bool Initialize (EmitContext ec, Expression target)
+ public override void Emit (EmitContext ec)
{
- ArrayList initialized = new ArrayList
(initializers.Count);
- for (int i = initializers.Count - 1; i >= 0; i--) {
- Initializer initializer = initializers[i] as
Initializer;
- if (initialized.Contains (initializer.Name)) {
- //FIXME proper error
- Console.WriteLine ("Object member can
only be initialized once");
- return false;
- }
-
- MemberAccess ma = new MemberAccess (target,
initializer.Name);
- Expression expr = initializer.Value as
Expression;
- // If it's an expresison, append the assign.
- if (expr != null) {
- Assign a = new Assign (ma, expr);
-
ec.CurrentBlock.InsertStatementAfterCurrent (new StatementExpression (a));
- }
- // If it's another initializer (object or
collection), initialize it.
- else if
(!((IInitializable)initializer.Value).Initialize (ec, ma))
- return false;
-
- initialized.Add (initializer.Name);
- }
- return true;
+ throw new NotSupportedException ("Should not be
reached");
}
}
- public class CollectionInitializer : IInitializable
+ //
+ // A block of object or collection initializers
+ //
+ public class CollectionOrObjectInitializers : ExpressionStatement
{
- readonly ArrayList items;
- public CollectionInitializer (ArrayList items)
+ ArrayList initializers;
+
+ public static readonly CollectionOrObjectInitializers Empty =
+ new CollectionOrObjectInitializers (new ArrayList (0),
Location.Null);
+
+ public CollectionOrObjectInitializers (ArrayList initializers,
Location loc)
{
- this.items = items;
+ this.initializers = initializers;
+ this.loc = loc;
}
- bool CheckCollection (EmitContext ec, Expression e)
+ public bool IsEmpty {
+ get {
+ return initializers.Count == 0;
+ }
+ }
+
+ protected override void CloneTo (CloneContext clonectx,
Expression target)
{
- if (e == null || e.Type == null)
- return false;
- bool is_ienumerable = false;
- foreach (Type t in TypeManager.GetInterfaces (e.Type))
- if (t == typeof (IEnumerable)) {
- is_ienumerable = true;
- break;
- }
-
- if (!is_ienumerable)
- return false;
-
- MethodGroupExpr mg = Expression.MemberLookup (
- ec.ContainerType, e.Type, "Add",
MemberTypes.Method,
- Expression.AllBindingFlags, Location.Null) as
MethodGroupExpr;
+ CollectionOrObjectInitializers t =
(CollectionOrObjectInitializers) target;
- if (mg == null)
- return false;
-
- foreach (MethodInfo mi in mg.Methods) {
- if (TypeManager.GetParameterData (mi).Count !=
1)
- continue;
- if ((mi.Attributes & MethodAttributes.Public)
!= MethodAttributes.Public)
- continue;
- return true;
- }
- return false;
+ t.initializers = new ArrayList (initializers.Count);
+ foreach (Expression e in initializers)
+ t.initializers.Add (e.Clone (clonectx));
}
- public bool Initialize (EmitContext ec, Expression target)
+ public override Expression DoResolve (EmitContext ec)
{
- if (!CheckCollection (ec, target.Resolve (ec))) {
- // FIXME throw proper error
- Console.WriteLine ("Error: This is not a
collection");
- return false;
+ bool is_elements_initialization = false;
+ ArrayList element_names = null;
+ for (int i = 0; i < initializers.Count; ++i) {
+ Expression initializer = (Expression)
initializers [i];
+ ElementInitializer element_initializer =
initializer as ElementInitializer;
+
+ if (i == 0) {
+ if (element_initializer != null) {
+ is_elements_initialization =
true;
+ element_names = new ArrayList
(initializers.Count);
+ element_names.Add
(element_initializer.Name);
+ } else {
+ if
(!TypeManager.ImplementsInterface (ec.CurrentInitializerVariable.Type,
+
TypeManager.ienumerable_type)) {
+ Report.Error (1922,
loc, "A field or property `{0}' cannot be initialized with a collection " +
+ "object
initializer because type `{1}' does not implement `{2}' interface",
+
ec.CurrentInitializerVariable.GetSignatureForError (),
+
TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
+
TypeManager.CSharpName (TypeManager.ienumerable_type));
+ return null;
+ }
+ }
+ } else {
+ if (is_elements_initialization ==
(element_initializer == null)) {
+ Report.Error (747,
initializer.Location, "Inconsistent `{0}' member declaration",
+
is_elements_initialization ? "object initializer" : "collection initializer");
+ continue;
+ }
+
+ if (is_elements_initialization) {
+ if (element_names.Contains
(element_initializer.Name)) {
+ Report.Error (1912,
element_initializer.Location,
+ "An object
initializer includes more than one member `{0}' initialization",
+
element_initializer.Name);
+ } else {
+ element_names.Add
(element_initializer.Name);
+ }
+ }
+ }
+
+ initializers [i] = initializer.Resolve (ec);
}
-
- for (int i = items.Count - 1; i >= 0; i--) {
- MemberAccess ma = new MemberAccess (target,
"Add");
- ArrayList array = new ArrayList ();
- array.Add (new Argument ((Expression)items[i]));
- Invocation invoke = new Invocation (ma, array);
- ec.CurrentBlock.InsertStatementAfterCurrent
(new StatementExpression (invoke));
- }
- return true;
+
+ type = typeof (CollectionOrObjectInitializers);
+ eclass = ExprClass.Variable;
+ return this;
}
+
+ public override void Emit (EmitContext ec)
+ {
+ EmitStatement (ec);
+ }
+
+ public override void EmitStatement (EmitContext ec)
+ {
+ foreach (ExpressionStatement e in initializers)
+ e.EmitStatement (ec);
+ }
}
- public class NewInitialize : New, IInitializable
+ //
+ // New expression with element/object initializers
+ //
+ public class NewInitialize : New
{
- IInitializable initializer;
+ CollectionOrObjectInitializers initializers;
+ TemporaryVariable type_instance;
- public bool Initialize (EmitContext ec, Expression target)
+ public NewInitialize (Expression requested_type, ArrayList
arguments, CollectionOrObjectInitializers initializers, Location l)
+ : base (requested_type, arguments, l)
{
- return initializer.Initialize (ec, target);
+ this.initializers = initializers;
}
- public NewInitialize (Expression requested_type, ArrayList
arguments, IInitializable initializer, Location l)
- : base (requested_type, arguments, l)
+ protected override void CloneTo (CloneContext clonectx,
Expression t)
{
- this.initializer = initializer;
+ base.CloneTo (clonectx, t);
+
+ NewInitialize target = (NewInitialize) t;
+ target.initializers =
(CollectionOrObjectInitializers)initializers.Clone (clonectx);
}
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ Expression e = base.DoResolve (ec);
+ if (type == null)
+ return null;
+
+ // Empty initializer can be optimized to simple new
+ if (initializers.IsEmpty)
+ return e;
+
+ type_instance = new TemporaryVariable (type, loc);
+ type_instance =
(TemporaryVariable)type_instance.Resolve (ec);
+
+ Expression previous = ec.CurrentInitializerVariable;
+ ec.CurrentInitializerVariable = type_instance;
+ initializers.Resolve (ec);
+ ec.CurrentInitializerVariable = previous;
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ base.Emit (ec);
+
+ type_instance.EmitStore (ec);
+ initializers.Emit (ec);
+ type_instance.Emit (ec);
+ }
}
public class AnonymousTypeDeclaration : Expression
Modified: trunk/mcs/mcs/statement.cs
===================================================================
--- trunk/mcs/mcs/statement.cs 2007-08-14 15:41:12 UTC (rev 84086)
+++ trunk/mcs/mcs/statement.cs 2007-08-14 15:42:24 UTC (rev 84087)
@@ -1485,7 +1485,6 @@
// The statements in this block
//
protected ArrayList statements;
- protected int current_statement;
int num_statements;
//
@@ -1865,12 +1864,6 @@
statements.Add (s);
flags |= Flags.BlockUsed;
}
-
- public void InsertStatementAfterCurrent (Statement statement)
- {
- statements.Insert (current_statement + 1, statement);
- flags |= Flags.BlockUsed;
- }
public bool Used {
get { return (flags & Flags.BlockUsed) != 0; }
@@ -2111,12 +2104,13 @@
// from the beginning of the function. The outer
Resolve() that detected the unreachability is
// responsible for handling the situation.
//
- for (current_statement = 0; current_statement <
statements.Count; current_statement++) {
- Statement s = (Statement) statements
[current_statement];
+ int statement_count = statements.Count;
+ for (int ix = 0; ix < statement_count; ix++){
+ Statement s = (Statement) statements [ix];
// Check possible empty statement (CS0642)
if (Report.WarningLevel >= 3 &&
- current_statement + 1 <
statements.Count &&
- statements [current_statement +
1] is Block)
+ ix + 1 < statement_count &&
+ statements [ix + 1] is Block)
CheckPossibleMistakenEmptyStatement (s);
//
@@ -2148,14 +2142,14 @@
return false;
ok = false;
- statements [current_statement] =
EmptyStatement.Value;
+ statements [ix] = EmptyStatement.Value;
continue;
}
if (unreachable && !(s is LabeledStatement) &&
!(s is Block))
- statements [current_statement] =
EmptyStatement.Value;
+ statements [ix] = EmptyStatement.Value;
- num_statements = current_statement + 1;
+ num_statements = ix + 1;
unreachable =
ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
if (unreachable && s is LabeledStatement)
@@ -2163,7 +2157,7 @@
}
Report.Debug (4, "RESOLVE BLOCK DONE", StartLocation,
- ec.CurrentBranching, statements.Count,
num_statements);
+ ec.CurrentBranching, statement_count,
num_statements);
if (!ok)
return false;
@@ -5112,7 +5106,7 @@
TypeManager.CSharpName (expr.Type));
}
- public static bool IsOverride (MethodInfo m)
+ bool IsOverride (MethodInfo m)
{
m = (MethodInfo)
TypeManager.DropGenericMethodArguments (m);
Modified: trunk/mcs/mcs/typemanager.cs
===================================================================
--- trunk/mcs/mcs/typemanager.cs 2007-08-14 15:41:12 UTC (rev 84086)
+++ trunk/mcs/mcs/typemanager.cs 2007-08-14 15:42:24 UTC (rev 84087)
@@ -622,6 +622,9 @@
if (t == typeof(NullType))
return "null";
+ if (t == typeof (ArglistParameter))
+ return "__arglist";
+
return CSharpName (GetFullName (t));
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches