* *.cs: Add IResolveContext::CurrentTypeParameters.
svn path=/trunk/mcs/; revision=140046
+2009-08-17 Marek Safar <marek.safar@gmail.com>
+
+ * *.cs: Add IResolveContext::CurrentTypeParameters.
+
2009-08-14 Marek Safar <marek.safar@gmail.com>
* *.cs: Removed TypeContainer and ContainerType from EmitContext.
public AnonymousMethodStorey GetGenericStorey ()
{
DeclSpace storey = this;
- while (storey != null && storey.CurrentTypeParameters.Length == 0)
+ while (storey != null && storey.CurrentTypeParameters == null)
storey = storey.Parent;
return storey as AnonymousMethodStorey;
//
public Type MutateGenericArgument (Type type)
{
- foreach (TypeParameter tp in CurrentTypeParameters) {
- if (tp.Name == type.Name) {
+ if (CurrentTypeParameters != null) {
+ TypeParameter tp = TypeParameter.FindTypeParameter (CurrentTypeParameters, type.Name);
+ if (tp != null)
return tp.Type;
- }
}
return type;
new TypeExpression (ReturnType, Location), parameters);
ArrayList list = new ArrayList ();
- foreach (TypeParameter tparam in ((IMethodData)mc).GenericMethod.CurrentTypeParameters) {
+ foreach (TypeParameter tparam in ec.CurrentTypeParameters) {
if (tparam.Constraints != null)
list.Add (tparam.Constraints.Clone ());
}
get { return tc.Parent.CurrentType; }
}
+ public TypeParameter[] CurrentTypeParameters {
+ get { return tc.PartialContainer.CurrentTypeParameters; }
+ }
+
public TypeContainer CurrentTypeDefinition {
get { return tc.Parent.CurrentTypeDefinition; }
}
public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
{
- return tc.Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
- }
+ TypeParameter[] tp = CurrentTypeParameters;
+ if (tp != null) {
+ TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
+ if (t != null)
+ return new TypeParameterExpr (t, loc);
+ }
- public Type LookupTypeParameter (string name)
- {
- return tc.PartialContainer.LookupTypeParameter (name);
+ return tc.Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
}
public DeclSpace GenericDeclContainer {
#if GMCS_SOURCE
GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
- int offset = CountTypeParameters - CurrentTypeParameters.Length;
+ int offset = CountTypeParameters;
+ if (CurrentTypeParameters != null)
+ offset -= CurrentTypeParameters.Length;
+
if (offset > 0) {
nested_gen_params = new GenericTypeParameterBuilder [offset];
Array.Copy (gen_params, nested_gen_params, offset);
void UpdateTypeParameterConstraints (TypeContainer part)
{
- TypeParameter[] current_params = CurrentTypeParameters;
+ TypeParameter[] current_params = type_params;
for (int i = 0; i < current_params.Length; i++) {
- Constraints c = part.CurrentTypeParameters [i].Constraints;
+ Constraints c = part.type_params [i].Constraints;
if (c == null)
continue;
throw new InternalErrorException ();
TypeExpr current_type = null;
-
- foreach (TypeParameter type_param in CurrentTypeParameters) {
- if (!type_param.Resolve (this)) {
- error = true;
- return false;
+ if (CurrentTypeParameters != null) {
+ foreach (TypeParameter type_param in CurrentTypeParameters) {
+ if (!type_param.Resolve (this)) {
+ error = true;
+ return false;
+ }
}
- }
- if (partial_parts != null && is_generic) {
- foreach (TypeContainer part in partial_parts)
- UpdateTypeParameterConstraints (part);
+ if (partial_parts != null) {
+ foreach (TypeContainer part in partial_parts)
+ UpdateTypeParameterConstraints (part);
+ }
}
for (int i = 0; i < TypeParameters.Length; ++i) {
return true;
}
+ public override TypeParameter[] CurrentTypeParameters {
+ get {
+ return PartialContainer.type_params;
+ }
+ }
+
/// <summary>
/// Populates our TypeBuilder with fields and methods
/// </summary>
else if (Name != "System.Object")
base_class = TypeManager.system_object_expr;
} else {
- if (Kind == Kind.Class && base_class is TypeParameterExpr){
+ if (Kind == Kind.Class && TypeManager.IsGenericParameter (base_class.Type)){
Report.Error (
689, base_class.Location,
"Cannot derive from `{0}' because it is a type parameter",
(Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
}
+ public override FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
+ {
+ TypeParameter[] tp = CurrentTypeParameters;
+ if (tp != null) {
+ TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
+ if (t != null)
+ return new TypeParameterExpr (t, loc);
+ }
+
+ return base.LookupNamespaceOrType (name, loc, ignore_cs0104);
+ }
+
public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
{
if (a.Type == pa.Conditional) {
return true;
}
+ public override TypeParameter[] CurrentTypeParameters {
+ get {
+ if (GenericMethod != null)
+ return GenericMethod.CurrentTypeParameters;
+
+ return null;
+ }
+ }
+
//
// Creates the type
//
return mi;
}
- public override Type LookupTypeParameter (string name)
- {
- if (GenericMethod != null) {
- Type t = GenericMethod.LookupTypeParameter (name);
- if (t != null)
- return t;
- }
-
- return base.LookupTypeParameter (name);
- }
-
public void SetPartialDefinition (Method methodDefinition)
{
caching_flags |= Flags.PartialDefinitionExists;
/// </summary>
public interface IResolveContext
{
+ //
+ // A scope type context, it can be inflated for generic types
+ //
Type CurrentType { get; }
+
+ //
+ // A scope type parameters either VAR or MVAR
+ //
+ TypeParameter[] CurrentTypeParameters { get; }
+
+ //
+ // A type definition of the type context. For partial types definition use
+ // CurrentTypeDefinition.PartialContainer otherwise the context is local
+ //
+ // TODO: CurrentType.Definition
+ //
TypeContainer CurrentTypeDefinition { get; }
+
+ // Obsolete
DeclSpace DeclContainer { get; }
bool IsInObsoleteScope { get; }
ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc);
FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104);
- Type LookupTypeParameter (string name);
// the declcontainer to lookup for type-parameters. Should only use LookupGeneric on it.
//
get { return ResolveContext.CurrentType; }
}
+ public TypeParameter[] CurrentTypeParameters {
+ get { return ResolveContext.CurrentTypeParameters; }
+ }
+
public TypeContainer CurrentTypeDefinition {
get { return ResolveContext.CurrentTypeDefinition; }
}
return ResolveContext.LookupNamespaceOrType (name, loc, ignore_cs0104);
}
- public Type LookupTypeParameter (string name)
- {
- return ResolveContext.LookupTypeParameter (name);
- }
-
#endregion
}
get { return null; }
}
+ public TypeParameter[] CurrentTypeParameters {
+ get { return null; }
+ }
+
public TypeContainer CurrentTypeDefinition {
get { throw new InternalErrorException ("No TypeContainer in module context"); }
}
return RootContext.ToplevelTypes.LookupNamespaceOrType (name, loc, ignore_cs0104);
}
- public Type LookupTypeParameter (string name)
- {
- return null;
- }
-
#endregion
}
return Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
}
- public virtual Type LookupTypeParameter (string name)
- {
- return Parent.PartialContainer.LookupTypeParameter (name);
- }
-
/// <summary>
/// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
/// If no is attribute exists then assembly CLSCompliantAttribute is returned.
get { return Parent.CurrentTypeDefinition; }
}
+ public virtual TypeParameter[] CurrentTypeParameters {
+ get { return null; }
+ }
+
public DeclSpace DeclContainer {
get { return Parent; }
}
if (Cache.Contains (name))
return (FullNamedExpression) Cache [name];
- FullNamedExpression e;
+ FullNamedExpression e = null;
int errors = Report.Errors;
- Type t = LookupNestedTypeInHierarchy (name);
- if (t != null)
- e = new TypeExpression (t, Location.Null);
- else if (Parent != null)
- e = Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
- else
- e = NamespaceEntry.LookupNamespaceOrType (name, loc, ignore_cs0104);
-
- if (errors == Report.Errors)
- Cache [name] = e;
-
- return e;
- }
- public override Type LookupTypeParameter (string name)
- {
- if (type_params != null) {
- Type t = LookupLocalTypeParameter (name);
- if (t != null)
- return t;
+ TypeParameter[] tp = CurrentTypeParameters;
+ if (tp != null) {
+ TypeParameter tparam = TypeParameter.FindTypeParameter (tp, name);
+ if (tparam != null)
+ e = new TypeParameterExpr (tparam, Location.Null);
}
- if (Parent != null)
- return Parent.PartialContainer.LookupTypeParameter (name);
-
- return null;
- }
+ if (e == null) {
+ Type t = LookupNestedTypeInHierarchy (name);
- Type LookupLocalTypeParameter (string name)
- {
- foreach (var tp in type_params) {
- if (tp.Name == name)
- return tp.Type;
+ if (t != null)
+ e = new TypeExpression (t, Location.Null);
+ else if (Parent != null)
+ e = Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
+ else
+ e = NamespaceEntry.LookupNamespaceOrType (name, loc, ignore_cs0104);
}
- return null;
+ if (errors == Report.Errors)
+ Cache [name] = e;
+
+ return e;
}
/// <remarks>
ArrayList list = new ArrayList ();
if (the_parent != null && the_parent.IsGeneric) {
// FIXME: move generics info out of DeclSpace
- TypeParameter[] parent_params = the_parent.PartialContainer.TypeParameters;
+ TypeParameter[] parent_params = the_parent.TypeParameters;
list.AddRange (parent_params);
}
for (int i = 0; i < count; i++) {
TypeParameter param = type_params [i];
list.Add (param);
- if (Parent.IsGeneric) {
- foreach (TypeParameter tp in Parent.PartialContainer.CurrentTypeParameters) {
+ if (Parent.CurrentTypeParameters != null) {
+ foreach (TypeParameter tp in Parent.CurrentTypeParameters) {
if (tp.Name != param.Name)
continue;
get { return PartialContainer; }
}
- public TypeParameter[] CurrentTypeParameters {
- get {
- if (!IsGeneric)
- throw new InvalidOperationException ();
-
- // TODO: Something is seriously broken here
- if (type_params == null)
- return new TypeParameter [0];
-
- return type_params;
- }
- }
-
public int CountTypeParameters {
get {
return count_type_params;
base.ApplyAttributeBuilder (a, cb, pa);
}
+ public override TypeParameter[] CurrentTypeParameters {
+ get {
+ return base.type_params;
+ }
+ }
+
public override TypeBuilder DefineType ()
{
if (TypeBuilder != null)
GenericTypeParameterBuilder[] gen_params;
gen_params = TypeBuilder.DefineGenericParameters (param_names);
- int offset = CountTypeParameters - CurrentTypeParameters.Length;
+ int offset = CountTypeParameters;
+ if (CurrentTypeParameters != null)
+ offset -= CurrentTypeParameters.Length;
for (int i = offset; i < gen_params.Length; i++)
CurrentTypeParameters [i - offset].Define (gen_params [i]);
- foreach (TypeParameter type_param in CurrentTypeParameters) {
- if (!type_param.Resolve (this))
- return null;
+ if (CurrentTypeParameters != null) {
+ foreach (TypeParameter type_param in CurrentTypeParameters) {
+ if (!type_param.Resolve (this))
+ return null;
+ }
}
Expression current = new SimpleName (
public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
- Type t = ec.LookupTypeParameter (Name);
- if (t != null)
- return new TypeParameterExpr (TypeManager.LookupTypeParameter (t), loc).ResolveAsTypeStep (ec, false);
-
int errors = Report.Errors;
FullNamedExpression fne = ec.LookupNamespaceOrType (Name, loc, /*ignore_cs0104=*/ false);
Type etype = expr.Type;
if (!TypeManager.IsReferenceType (type) && !TypeManager.IsNullableType (type)) {
- if (probe_type_expr is TypeParameterExpr) {
+ if (TypeManager.IsGenericParameter (type)) {
Report.Error (413, loc,
"The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
probe_type_expr.GetSignatureForError ());
if (tnew_expr == null)
return null;
- if (tnew_expr is TypeParameterExpr) {
+ Type expr_type = tnew_expr.Type;
+ if (TypeManager.IsGenericParameter (expr_type)) {
Report.Error (704, loc, "A nested type cannot be specified through a type parameter `{0}'",
tnew_expr.GetSignatureForError ());
return null;
}
- Type expr_type = tnew_expr.Type;
Expression member_lookup = MemberLookup (
rc.DeclContainer.TypeBuilder, expr_type, expr_type, LookupIdentifier,
MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
public void ErrorInvalidVariance (MemberCore mc, Variance v)
{
}
+
+ public static TypeParameter FindTypeParameter (TypeParameter[] all, string name)
+ {
+ throw new NotImplementedException ();
+ }
//
// MemberContainer
return false;
}
- TypeParameterExpr texpr = expr as TypeParameterExpr;
- if (texpr != null)
+ if (TypeManager.IsGenericParameter (expr.Type))
type_param_constraints.Add (expr);
else if (expr.IsInterface)
iface_constraints.Add (expr);
list.Add (iface_constraint.Type);
}
- foreach (TypeParameterExpr expr in type_param_constraints) {
+ foreach (TypeExpr expr in type_param_constraints) {
foreach (Type type in list) {
if (!type.Equals (expr.Type))
continue;
return true;
}
- bool CheckTypeParameterConstraints (TypeParameter tparam, ref TypeExpr prevConstraint, ArrayList seen)
+ bool CheckTypeParameterConstraints (Type tparam, ref TypeExpr prevConstraint, ArrayList seen)
{
seen.Add (tparam);
- Constraints constraints = tparam.Constraints;
+ Constraints constraints = TypeManager.LookupTypeParameter (tparam).Constraints;
if (constraints == null)
return true;
if (constraints.type_param_constraints == null)
return true;
- foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
- if (seen.Contains (expr.TypeParameter)) {
+ foreach (TypeExpr expr in constraints.type_param_constraints) {
+ if (seen.Contains (expr.Type)) {
Report.Error (454, loc, "Circular constraint " +
"dependency involving `{0}' and `{1}'",
tparam.Name, expr.GetSignatureForError ());
return false;
}
- if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prevConstraint, seen))
+ if (!CheckTypeParameterConstraints (expr.Type, ref prevConstraint, seen))
return false;
}
if (type_param_constraints.Count != 0) {
ArrayList seen = new ArrayList ();
TypeExpr prev_constraint = class_constraint;
- foreach (TypeParameterExpr expr in type_param_constraints) {
- if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prev_constraint, seen))
+ foreach (TypeExpr expr in type_param_constraints) {
+ if (!CheckTypeParameterConstraints (expr.Type, ref prev_constraint, seen))
return false;
seen.Clear ();
}
return true;
}
+ public static TypeParameter FindTypeParameter (TypeParameter[] tparams, string name)
+ {
+ foreach (var tp in tparams) {
+ if (tp.Name == name)
+ return tp;
+ }
+
+ return null;
+ }
+
public void SetConstraints (GenericTypeParameterBuilder type)
{
GenericParameterAttributes attr = GenericParameterAttributes.None;
/// A TypeExpr which already resolved to a type parameter.
/// </summary>
public class TypeParameterExpr : TypeExpr {
- TypeParameter type_parameter;
-
- public TypeParameter TypeParameter {
- get {
- return type_parameter;
- }
- }
public TypeParameterExpr (TypeParameter type_parameter, Location loc)
{
- this.type_parameter = type_parameter;
+ this.type = type_parameter.Type;
+ this.eclass = ExprClass.TypeParameter;
this.loc = loc;
}
public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
- type = type_parameter.Type;
- eclass = ExprClass.TypeParameter;
return this;
}
this.parameters = parameters;
}
+ public override TypeParameter[] CurrentTypeParameters {
+ get {
+ return base.type_params;
+ }
+ }
+
public override TypeBuilder DefineType ()
{
throw new Exception ();
return resolved;
}
- public Type LookupTypeParameter (string name)
- {
- return null;
- }
-
public ICollection CompletionGetTypesStartingWith (string prefix)
{
Hashtable result = new Hashtable ();
get { return SlaveDeclSpace.CurrentTypeDefinition; }
}
+ public TypeParameter[] CurrentTypeParameters {
+ get { return SlaveDeclSpace.CurrentTypeParameters; }
+ }
+
public DeclSpace DeclContainer {
get { return SlaveDeclSpace; }
}
(modFlags & Parameter.Modifier.ISBYREF) != 0 ? Variance.None : Variance.Contravariant,
rc as MemberCore);
- if (texpr is TypeParameterExpr)
+ if (TypeManager.IsGenericParameter (parameter_type))
return parameter_type;
if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {