From 409ebb6a2ee08734c182bb9a90d54f9a55c6a20b Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Wed, 19 Nov 2008 16:09:41 +0000 Subject: [PATCH] 2008-11-19 Marek Safar * generic.cs, lambda.cs, linq.cs, iterators.cs, anonymous.cs, nullable.cs, expression.cs, statement.cs, ecore.cs, cs-parser.jay generic-mcs.cs: Small cleanup of TypeArguments. svn path=/trunk/mcs/; revision=119324 --- mcs/mcs/ChangeLog | 6 ++++ mcs/mcs/anonymous.cs | 8 ++--- mcs/mcs/cs-parser.jay | 52 ++++++++++++++--------------- mcs/mcs/ecore.cs | 16 +++------ mcs/mcs/expression.cs | 6 ++-- mcs/mcs/generic-mcs.cs | 28 +++------------- mcs/mcs/generic.cs | 76 +++++++++++++----------------------------- mcs/mcs/iterators.cs | 3 +- mcs/mcs/lambda.cs | 4 +-- mcs/mcs/linq.cs | 6 ++-- mcs/mcs/nullable.cs | 8 ++--- mcs/mcs/statement.cs | 2 +- 12 files changed, 81 insertions(+), 134 deletions(-) diff --git a/mcs/mcs/ChangeLog b/mcs/mcs/ChangeLog index c3adbf821e4..39f3c13aeff 100644 --- a/mcs/mcs/ChangeLog +++ b/mcs/mcs/ChangeLog @@ -1,3 +1,9 @@ +2008-11-19 Marek Safar + + * generic.cs, lambda.cs, linq.cs, iterators.cs, anonymous.cs, + nullable.cs, expression.cs, statement.cs, ecore.cs, cs-parser.jay + generic-mcs.cs: Small cleanup of TypeArguments. + 2008-11-18 Marek Safar * generic.cs, iterators.cs, anonymous.cs, nullable.cs, ecore.cs, diff --git a/mcs/mcs/anonymous.cs b/mcs/mcs/anonymous.cs index 6bd1bc43f58..6c3807fdcb0 100644 --- a/mcs/mcs/anonymous.cs +++ b/mcs/mcs/anonymous.cs @@ -132,7 +132,7 @@ namespace Mono.CSharp { string tname = MakeName (host_name, "c", name, unique_id); TypeArguments args = null; if (generic != null) { - args = new TypeArguments (loc); + args = new TypeArguments (); foreach (TypeParameter tparam in generic.CurrentTypeParameters) args.Add (new TypeParameterName (tparam.Name, null, loc)); } @@ -269,7 +269,7 @@ namespace Mono.CSharp { ec.CurrentAnonymousMethod.Storey.TypeParameters : ec.GenericDeclContainer.TypeParameters; - TypeArguments targs = new TypeArguments (Location); + TypeArguments targs = new TypeArguments (); if (tparams.Length < CountTypeParameters) { TypeParameter[] parent_tparams = ec.DeclContainer.Parent.CurrentTypeParameters; @@ -1655,7 +1655,7 @@ namespace Mono.CSharp { // named upon properties names // AnonymousTypeClass a_type = new AnonymousTypeClass (parent.NamespaceEntry.SlaveDeclSpace, - new MemberName (name, new TypeArguments (loc, t_params), loc), parameters, loc); + new MemberName (name, new TypeArguments (t_params), loc), parameters, loc); if (parameters.Count > 0) a_type.SetParameterInfo (null); @@ -1753,7 +1753,7 @@ namespace Mono.CSharp { MemberAccess equality_comparer = new MemberAccess (new MemberAccess ( system_collections_generic, "EqualityComparer", - new TypeArguments (loc, new SimpleName (TypeParameters [i].Name, loc)), loc), + new TypeArguments (new SimpleName (TypeParameters [i].Name, loc)), loc), "Default", loc); ArrayList arguments_equal = new ArrayList (2); diff --git a/mcs/mcs/cs-parser.jay b/mcs/mcs/cs-parser.jay index 9d2b12f77ae..1f90627393d 100644 --- a/mcs/mcs/cs-parser.jay +++ b/mcs/mcs/cs-parser.jay @@ -2632,21 +2632,21 @@ opt_type_argument_list | OP_GENERICS_LT error { Error_TypeExpected (lexer.Location); - $$ = new TypeArguments (lexer.Location); + $$ = new TypeArguments (); } ; type_arguments : type { - TypeArguments type_args = new TypeArguments (lexer.Location); - type_args.Add ((Expression) $1); + TypeArguments type_args = new TypeArguments (); + type_args.Add ((FullNamedExpression) $1); $$ = type_args; } | type_arguments COMMA type { TypeArguments type_args = (TypeArguments) $1; - type_args.Add ((Expression) $3); + type_args.Add ((FullNamedExpression) $3); $$ = type_args; } ; @@ -2734,14 +2734,14 @@ opt_type_parameter_list type_parameters : type_parameter { - TypeArguments type_args = new TypeArguments (lexer.Location); - type_args.Add ((Expression)$1); + TypeArguments type_args = new TypeArguments (); + type_args.Add ((FullNamedExpression)$1); $$ = type_args; } | type_parameters COMMA type_parameter { TypeArguments type_args = (TypeArguments) $1; - type_args.Add ((Expression)$3); + type_args.Add ((FullNamedExpression)$3); $$ = type_args; } ; @@ -3507,44 +3507,44 @@ unbound_type_name : IDENTIFIER generic_dimension { LocatedToken lt = (LocatedToken) $1; - TypeArguments ta = (TypeArguments)$2; - $$ = new SimpleName (MemberName.MakeName (lt.Value, ta), lt.Location); + $$ = new SimpleName (MemberName.MakeName (lt.Value, (int)$2), lt.Location); } | qualified_alias_member IDENTIFIER generic_dimension { LocatedToken lt1 = (LocatedToken) $1; LocatedToken lt2 = (LocatedToken) $2; - $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (TypeArguments) $3), lt1.Location); + $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (int) $3), lt1.Location); } - | unbound_type_name DOT IDENTIFIER opt_generic_dimension + | unbound_type_name DOT IDENTIFIER { LocatedToken lt = (LocatedToken) $3; - $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (TypeArguments) $4), lt.Location); + $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location); + } + | unbound_type_name DOT IDENTIFIER generic_dimension + { + LocatedToken lt = (LocatedToken) $3; + + $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (int) $4), lt.Location); } | namespace_or_type_name DOT IDENTIFIER generic_dimension { LocatedToken lt = (LocatedToken) $3; MemberName name = (MemberName) $1; - $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (TypeArguments) $4), lt.Location); + $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (int) $4), lt.Location); } ; -opt_generic_dimension - : /* empty */ - | generic_dimension - ; - generic_dimension : GENERIC_DIMENSION { if (RootContext.Version < LanguageVersion.ISO_2) Report.FeatureIsNotAvailable (lexer.Location, "generics"); - $$ = new TypeArguments ((int)$1, lexer.Location); + $$ = $1; } ; @@ -3644,11 +3644,11 @@ unary_expression cast_expression : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression { - $$ = new Cast ((Expression) $2, (Expression) $4, GetLocation ($1)); + $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1)); } | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression { - $$ = new Cast ((Expression) $2, (Expression) $4, GetLocation ($1)); + $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1)); } ; @@ -5206,7 +5206,7 @@ first_from_clause current_block = new Linq.QueryBlock (current_block, GetLocation ($1)); LocatedToken lt = (LocatedToken) $3; - Expression type = (Expression)$2; + FullNamedExpression type = (FullNamedExpression)$2; current_block.AddVariable (type, lt.Value, lt.Location); $$ = new Linq.QueryExpression (lt, new Linq.Cast (type, (Expression)$5)); } @@ -5226,9 +5226,9 @@ from_clause { LocatedToken lt = (LocatedToken) $3; - Expression type = (Expression)$2; + FullNamedExpression type = (FullNamedExpression)$2; current_block.AddVariable (type, lt.Value, lt.Location); - $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (Expression)$5)); + $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (FullNamedExpression)$5)); } ; @@ -5321,9 +5321,9 @@ join_clause { Location loc = GetLocation ($1); LocatedToken lt = (LocatedToken) $3; - current_block.AddVariable ((Expression)$2, lt.Value, lt.Location); + current_block.AddVariable ((FullNamedExpression)$2, lt.Value, lt.Location); - Linq.Cast cast = new Linq.Cast ((Expression)$2, (Expression)$5); + Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$5); if ($10 == null) { $$ = new Linq.Join (lt, cast, (Expression)$7, (Expression)$9, loc); diff --git a/mcs/mcs/ecore.cs b/mcs/mcs/ecore.cs index eb51da851ff..29f19886fd4 100644 --- a/mcs/mcs/ecore.cs +++ b/mcs/mcs/ecore.cs @@ -2358,7 +2358,7 @@ namespace Mono.CSharp { public SimpleName (string name, TypeParameter[] type_params, Location l) : base (name, l) { - targs = new TypeArguments (l); + targs = new TypeArguments (); foreach (TypeParameter type_param in type_params) targs.Add (new TypeParameterExpr (type_param, l)); } @@ -2461,7 +2461,7 @@ namespace Mono.CSharp { for (; (ds != null) && ds.IsGeneric; ds = ds.Parent) { if (arg_count + ds.CountTypeParameters == gen_params.Length) { - TypeArguments new_args = new TypeArguments (loc); + TypeArguments new_args = new TypeArguments (); foreach (TypeParameter param in ds.TypeParameters) new_args.Add (new TypeParameterExpr (param, loc)); @@ -2656,6 +2656,9 @@ namespace Mono.CSharp { } else if (e is EventExpr) { if (((EventExpr) e).IsAccessibleFrom (ec.ContainerType)) break; + } else if (targs != null && e is TypeExpression) { + e = new GenericTypeExpr (e.Type, targs, loc).ResolveAsTypeStep (ec, false); + break; } else { break; } @@ -2705,15 +2708,6 @@ namespace Mono.CSharp { return null; } - if (e is TypeExpr) { - if (targs == null) - return e; - - GenericTypeExpr ct = new GenericTypeExpr ( - e.Type, targs, loc); - return ct.ResolveAsTypeStep (ec, false); - } - if (e is MemberExpr) { MemberExpr me = (MemberExpr) e; diff --git a/mcs/mcs/expression.cs b/mcs/mcs/expression.cs index 9d64a123d52..a890818b228 100644 --- a/mcs/mcs/expression.cs +++ b/mcs/mcs/expression.cs @@ -7521,7 +7521,7 @@ namespace Mono.CSharp { expr_type = expr_type.BaseType; } - TypeArguments new_args = new TypeArguments (loc); + TypeArguments new_args = new TypeArguments (); foreach (Type decl in TypeManager.GetTypeArguments (expr_type)) new_args.Add (new TypeExpression (decl, loc)); @@ -8931,7 +8931,7 @@ namespace Mono.CSharp { Type ltype = lexpr.Type; #if GMCS_SOURCE if ((dim.Length > 0) && (dim [0] == '?')) { - TypeExpr nullable = new Nullable.NullableType (left, loc); + TypeExpr nullable = new Nullable.NullableType (lexpr, loc); if (dim.Length > 1) nullable = new ComposedCast (nullable, dim.Substring (1), loc); return nullable.ResolveAsTypeTerminal (ec, false); @@ -9730,7 +9730,7 @@ namespace Mono.CSharp { return null; GenericTypeExpr te = new GenericTypeExpr (anonymous_type.TypeBuilder, - new TypeArguments (loc, t_args), loc); + new TypeArguments (t_args), loc); return new New (te, arguments, loc).Resolve (ec); } diff --git a/mcs/mcs/generic-mcs.cs b/mcs/mcs/generic-mcs.cs index 6da073a861e..dd80095e7c8 100644 --- a/mcs/mcs/generic-mcs.cs +++ b/mcs/mcs/generic-mcs.cs @@ -288,31 +288,18 @@ namespace Mono.CSharp public class TypeArguments { - public readonly Location Location; ArrayList args; - //Type[] atypes; - int dimension; - //bool has_type_args; - //bool created; - public TypeArguments (Location loc) + public TypeArguments () { args = new ArrayList (); - this.Location = loc; } - public TypeArguments (Location loc, params Expression[] types) + public TypeArguments (params Expression[] types) { - this.Location = loc; - this.args = new ArrayList (types); + args = new ArrayList (types); } - public TypeArguments (int dimension, Location loc) - { - this.dimension = dimension; - this.Location = loc; - } - public void Add (Expression type) { } @@ -332,17 +319,10 @@ namespace Mono.CSharp public int Count { get { - if (dimension > 0) - return dimension; - else - return args.Count; + return args.Count; } } - public bool IsUnbound { - get { throw new NotImplementedException (); } - } - public TypeParameterName[] GetDeclarations () { throw new NotImplementedException (); diff --git a/mcs/mcs/generic.cs b/mcs/mcs/generic.cs index 5dcd5915c09..0f05ee2d469 100644 --- a/mcs/mcs/generic.cs +++ b/mcs/mcs/generic.cs @@ -1064,36 +1064,25 @@ namespace Mono.CSharp { } } - /// - /// Tracks the type arguments when instantiating a generic type. We're used in - /// ConstructedType. - /// + // + // Tracks the type arguments when instantiating a generic type. It's used + // by both type arguments and type parameters + // public class TypeArguments { - public readonly Location Location; ArrayList args; Type[] atypes; - int dimension; - bool has_type_args; - public TypeArguments (Location loc) + public TypeArguments () { args = new ArrayList (); - this.Location = loc; } - public TypeArguments (Location loc, params Expression[] types) + public TypeArguments (params FullNamedExpression[] types) { - this.Location = loc; this.args = new ArrayList (types); } - - public TypeArguments (int dimension, Location loc) - { - this.dimension = dimension; - this.Location = loc; - } - public void Add (Expression type) + public void Add (FullNamedExpression type) { args.Add (type); } @@ -1119,24 +1108,9 @@ namespace Mono.CSharp { } } - public bool HasTypeArguments { - get { - return has_type_args; - } - } - public int Count { get { - if (dimension > 0) - return dimension; - else - return args.Count; - } - } - - public bool IsUnbound { - get { - return dimension > 0; + return args.Count; } } @@ -1158,41 +1132,34 @@ namespace Mono.CSharp { /// public bool Resolve (IResolveContext ec) { + if (atypes != null) + return true; + int count = args.Count; bool ok = true; atypes = new Type [count]; for (int i = 0; i < count; i++){ - TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec, false); + TypeExpr te = ((FullNamedExpression) args[i]).ResolveAsTypeTerminal (ec, false); if (te == null) { ok = false; continue; } atypes[i] = te.Type; - if (te.Type.IsGenericParameter) { - if (te is TypeParameterExpr) - has_type_args = true; - continue; - } if (te.Type.IsSealed && te.Type.IsAbstract) { - Report.Error (718, Location, "`{0}': static classes cannot be used as generic arguments", + Report.Error (718, te.Location, "`{0}': static classes cannot be used as generic arguments", te.GetSignatureForError ()); - return false; + ok = false; } if (te.Type.IsPointer || TypeManager.IsSpecialType (te.Type)) { - Report.Error (306, Location, + Report.Error (306, te.Location, "The type `{0}' may not be used as a type argument", - TypeManager.CSharpName (te.Type)); - return false; - } - - if (te.Type == TypeManager.void_type) { - Expression.Error_VoidInvalidInTheContext (Location); - return false; + te.GetSignatureForError ()); + ok = false; } } return ok; @@ -1200,7 +1167,7 @@ namespace Mono.CSharp { public TypeArguments Clone () { - TypeArguments copy = new TypeArguments (Location); + TypeArguments copy = new TypeArguments (); foreach (Expression ta in args) copy.args.Add (ta); @@ -1244,7 +1211,7 @@ namespace Mono.CSharp { { open_type = gType.TypeBuilder.GetGenericTypeDefinition (); - args = new TypeArguments (l); + args = new TypeArguments (); foreach (TypeParameter type_param in gType.TypeParameters) args.Add (new TypeParameterExpr (type_param, l)); @@ -1275,6 +1242,9 @@ namespace Mono.CSharp { protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec) { + if (eclass != ExprClass.Invalid) + return this; + if (!args.Resolve (ec)) return null; @@ -1485,7 +1455,7 @@ namespace Mono.CSharp { if (TypeManager.HasGenericArguments (ctype)) { Type[] types = TypeManager.GetTypeArguments (ctype); - TypeArguments new_args = new TypeArguments (loc); + TypeArguments new_args = new TypeArguments (); for (int i = 0; i < types.Length; i++) { Type t = types [i]; diff --git a/mcs/mcs/iterators.cs b/mcs/mcs/iterators.cs index 73a3e861a3c..7cdb19d1a3c 100644 --- a/mcs/mcs/iterators.cs +++ b/mcs/mcs/iterators.cs @@ -421,8 +421,7 @@ namespace Mono.CSharp { iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location); #if GMCS_SOURCE - generic_args = new TypeArguments (Location); - generic_args.Add (iterator_type_expr); + generic_args = new TypeArguments (iterator_type_expr); #endif ArrayList list = new ArrayList (); diff --git a/mcs/mcs/lambda.cs b/mcs/mcs/lambda.cs index 794572c1b87..0de65784b78 100644 --- a/mcs/mcs/lambda.cs +++ b/mcs/mcs/lambda.cs @@ -45,7 +45,7 @@ namespace Mono.CSharp { arguments.Add (new Argument (expr)); arguments.Add (new Argument (args)); return CreateExpressionFactoryCall ("Lambda", - new TypeArguments (loc, new TypeExpression (delegate_type, loc)), + new TypeArguments (new TypeExpression (delegate_type, loc)), arguments); } @@ -156,7 +156,7 @@ namespace Mono.CSharp { arguments.Add (new Argument (expr)); arguments.Add (new Argument (args)); return CreateExpressionFactoryCall ("Lambda", - new TypeArguments (loc, new TypeExpression (type, loc)), + new TypeArguments (new TypeExpression (type, loc)), arguments); } } diff --git a/mcs/mcs/linq.cs b/mcs/mcs/linq.cs index 610f0f499fc..5ab7b2eff57 100644 --- a/mcs/mcs/linq.cs +++ b/mcs/mcs/linq.cs @@ -347,9 +347,9 @@ namespace Mono.CSharp.Linq public class Cast : QueryStartClause { // We don't have to clone cast type - readonly Expression type_expr; + readonly FullNamedExpression type_expr; - public Cast (Expression type, Expression expr) + public Cast (FullNamedExpression type, Expression expr) : base (expr) { this.type_expr = type; @@ -357,7 +357,7 @@ namespace Mono.CSharp.Linq public override Expression BuildQueryClause (EmitContext ec, Expression lSide, Parameter parameter, TransparentIdentifiersScope ti) { - lSide = CreateQueryExpression (expr, new TypeArguments (loc, type_expr), null); + lSide = CreateQueryExpression (expr, new TypeArguments (type_expr), null); if (next != null) return next.BuildQueryClause (ec, lSide, parameter, ti); diff --git a/mcs/mcs/nullable.cs b/mcs/mcs/nullable.cs index d8a1d4b82f2..b70b3d8b371 100644 --- a/mcs/mcs/nullable.cs +++ b/mcs/mcs/nullable.cs @@ -20,9 +20,9 @@ namespace Mono.CSharp.Nullable { public class NullableType : TypeExpr { - Expression underlying; + TypeExpr underlying; - public NullableType (Expression underlying, Location l) + public NullableType (TypeExpr underlying, Location l) { this.underlying = underlying; loc = l; @@ -36,14 +36,12 @@ namespace Mono.CSharp.Nullable protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec) { - TypeArguments args = new TypeArguments (loc); - args.Add (underlying); - if (TypeManager.generic_nullable_type == null) { TypeManager.generic_nullable_type = TypeManager.CoreLookupType ( "System", "Nullable`1", Kind.Struct, true); } + TypeArguments args = new TypeArguments (underlying); GenericTypeExpr ctype = new GenericTypeExpr (TypeManager.generic_nullable_type, args, loc); return ctype.ResolveAsTypeTerminal (ec, false); } diff --git a/mcs/mcs/statement.cs b/mcs/mcs/statement.cs index 63052ea9896..2cea11aa576 100644 --- a/mcs/mcs/statement.cs +++ b/mcs/mcs/statement.cs @@ -3658,7 +3658,7 @@ namespace Mono.CSharp { new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Collections", loc), "Generic", loc); string_dictionary_type = new MemberAccess (system_collections_generic, "Dictionary", - new TypeArguments (loc, + new TypeArguments ( new TypeExpression (TypeManager.string_type, loc), new TypeExpression (TypeManager.int32_type, loc)), loc); #else -- 2.25.1