namespace Mono.CSharp {
+ public abstract class GenericConstraints {
+ public abstract GenericParameterAttributes Attributes {
+ get;
+ }
+
+ public bool HasConstructorConstraint {
+ get { return (Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
+ }
+
+ public bool HasReferenceTypeConstraint {
+ get { return (Attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
+ }
+
+ public bool HasValueTypeConstraint {
+ get { return (Attributes & GenericParameterAttributes.ValueTypeConstraint) != 0; }
+ }
+
+ public virtual bool HasClassConstraint {
+ get { return ClassConstraint != null; }
+ }
+
+ public abstract Type ClassConstraint {
+ get;
+ }
+
+ public abstract Type[] InterfaceConstraints {
+ get;
+ }
+
+ public abstract Type EffectiveBaseClass {
+ get;
+ }
+
+ // <summary>
+ // Returns whether the type parameter is "known to be a reference type".
+ // </summary>
+ public virtual bool IsReferenceType {
+ get {
+ if (HasReferenceTypeConstraint)
+ return true;
+ if (HasValueTypeConstraint)
+ return false;
+
+ if (ClassConstraint != null) {
+ if (ClassConstraint.IsValueType)
+ return false;
+
+ if (ClassConstraint != TypeManager.object_type)
+ return true;
+ }
+
+ foreach (Type t in InterfaceConstraints) {
+ if (!t.IsGenericParameter)
+ continue;
+
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
+ if ((gc != null) && gc.IsReferenceType)
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+ // <summary>
+ // Returns whether the type parameter is "known to be a value type".
+ // </summary>
+ public virtual bool IsValueType {
+ get {
+ if (HasValueTypeConstraint)
+ return true;
+ if (HasReferenceTypeConstraint)
+ return false;
+
+ if (ClassConstraint != null) {
+ if (!ClassConstraint.IsValueType)
+ return false;
+
+ if (ClassConstraint != TypeManager.value_type)
+ return true;
+ }
+
+ foreach (Type t in InterfaceConstraints) {
+ if (!t.IsGenericParameter)
+ continue;
+
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
+ if ((gc != null) && gc.IsValueType)
+ return true;
+ }
+
+ return false;
+ }
+ }
+ }
+
public enum SpecialConstraint
{
Constructor,
}
}
- bool has_ctor_constraint;
- bool has_reference_type;
- bool has_value_type;
+ GenericParameterAttributes attrs;
TypeExpr class_constraint;
ArrayList iface_constraints;
ArrayList type_param_constraints;
int num_constraints, first_constraint;
Type class_constraint_type;
Type[] iface_constraint_types;
-
- public bool HasConstructorConstraint {
- get { return has_ctor_constraint; }
- }
+ Type effective_base_type;
public bool Resolve (DeclSpace ds)
{
type_param_constraints = new ArrayList ();
foreach (object obj in constraints) {
- if (has_ctor_constraint) {
+ if (HasConstructorConstraint) {
Report.Error (401, loc,
"The new() constraint must be last.");
return false;
SpecialConstraint sc = (SpecialConstraint) obj;
if (sc == SpecialConstraint.Constructor) {
- if (!has_value_type) {
- has_ctor_constraint = true;
+ if (!HasValueTypeConstraint) {
+ attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
continue;
}
return false;
}
- if ((num_constraints > 0) || has_reference_type ||
- has_value_type) {
+ if ((num_constraints > 0) || HasReferenceTypeConstraint || HasValueTypeConstraint) {
Report.Error (449, loc,
"The `class' or `struct' " +
"constraint must be first");
}
if (sc == SpecialConstraint.ReferenceType)
- has_reference_type = true;
+ attrs |= GenericParameterAttributes.ReferenceTypeConstraint;
else
- has_value_type = true;
+ attrs |= GenericParameterAttributes.ValueTypeConstraint;
continue;
}
- TypeExpr expr = ds.ResolveTypeExpr ((Expression) obj, false, loc);
+ TypeExpr expr = ds.ResolveTypeExpr ((Expression) obj, loc);
if (expr == null)
return false;
"must come before any other constraints.",
expr.Name, name);
return false;
- } else if (has_reference_type || has_value_type) {
+ } else if (HasReferenceTypeConstraint || HasValueTypeConstraint) {
Report.Error (450, loc, "`{0}': cannot specify both " +
"a constraint class and the `class' " +
"or `struct' constraint.", expr.Name);
if (constraints == null)
return true;
- if (constraints.IsValueType) {
+ if (constraints.HasValueTypeConstraint) {
Report.Error (456, loc, "Type parameter `{0}' has " +
"the `struct' constraint, so it cannot " +
"be used as a constraint for `{1}'",
}
}
- if (has_reference_type)
- class_constraint_type = TypeManager.object_type;
- else if (has_value_type)
- class_constraint_type = TypeManager.value_type;
+ if (class_constraint_type != null)
+ effective_base_type = class_constraint_type;
+ else if (HasValueTypeConstraint)
+ effective_base_type = TypeManager.value_type;
+ else
+ effective_base_type = TypeManager.object_type;
return true;
}
if (constraints == null)
return true;
- if (IsValueType && constraints.HasClassConstraint) {
+ if (HasValueTypeConstraint && constraints.HasClassConstraint) {
Report.Error (455, loc, "Type parameter `{0}' inherits " +
"conflicting constraints `{1}' and `{2}'",
name, constraints.ClassConstraint,
Type t2 = constraints.ClassConstraint;
TypeExpr e2 = constraints.class_constraint;
- if (!Convert.ImplicitReferenceConversionExists (e1, t2) &&
- !Convert.ImplicitReferenceConversionExists (e2, t1)) {
+ if (!Convert.ImplicitReferenceConversionExists (ec, e1, t2) &&
+ !Convert.ImplicitReferenceConversionExists (ec, e2, t1)) {
Report.Error (455, loc,
"Type parameter `{0}' inherits " +
"conflicting constraints `{1}' and `{2}'",
public void Define (GenericTypeParameterBuilder type)
{
- if (has_ctor_constraint)
- type.Mono_SetConstructorConstraint ();
- if (has_reference_type)
- type.Mono_SetReferenceTypeConstraint ();
- else if (has_value_type)
- type.Mono_SetValueTypeConstraint ();
- }
-
- public bool HasConstructor {
- get { return has_ctor_constraint; }
- }
-
- public bool IsReferenceType {
- get { return has_reference_type; }
+ type.SetGenericParameterAttributes (attrs);
}
- public bool IsValueType {
- get { return has_value_type; }
+ public override GenericParameterAttributes Attributes {
+ get { return attrs; }
}
- public bool HasClassConstraint {
- get { return class_constraint_type != null; }
+ public override bool HasClassConstraint {
+ get { return class_constraint != null; }
}
- public Type ClassConstraint {
+ public override Type ClassConstraint {
get { return class_constraint_type; }
}
- public Type[] InterfaceConstraints {
+ public override Type[] InterfaceConstraints {
get { return iface_constraint_types; }
}
+ public override Type EffectiveBaseClass {
+ get { return effective_base_type; }
+ }
+
internal bool IsSubclassOf (Type t)
{
if ((class_constraint_type != null) &&
if (!ResolveTypes (ec))
return false;
- if (gc.HasConstructor != HasConstructor)
- return false;
- if (gc.IsReferenceType != IsReferenceType)
- return false;
- if (gc.IsValueType != IsValueType)
- return false;
- if (gc.HasClassConstraint != HasClassConstraint)
+ if (gc.Attributes != attrs)
return false;
- if (HasClassConstraint && !gc.ClassConstraint.Equals (ClassConstraint))
+ if (HasClassConstraint != gc.HasClassConstraint)
+ return false;
+ if (HasClassConstraint && !TypeManager.IsEqual (gc.ClassConstraint, ClassConstraint))
return false;
int gc_icount = gc.InterfaceConstraints != null ?
foreach (Type iface in gc.InterfaceConstraints) {
bool ok = false;
foreach (Type check in InterfaceConstraints) {
- if (iface.Equals (check)) {
+ if (TypeManager.IsEqual (iface, check)) {
ok = true;
break;
}
//
// This type represents a generic type parameter
//
- public class TypeParameter : IMemberContainer {
+ public class TypeParameter : MemberCore, IMemberContainer {
string name;
+ GenericConstraints gc;
Constraints constraints;
Location loc;
GenericTypeParameterBuilder type;
- public TypeParameter (string name, Constraints constraints, Location loc)
+ public TypeParameter (TypeContainer parent, string name,
+ Constraints constraints, Location loc)
+ : base (parent, new MemberName (name), null, loc)
{
this.name = name;
this.constraints = constraints;
this.loc = loc;
}
- public string Name {
- get {
- return name;
- }
- }
-
- public Location Location {
+ public GenericConstraints GenericConstraints {
get {
- return loc;
+ return gc != null ? gc : constraints;
}
}
public void Define (GenericTypeParameterBuilder type)
{
+ if (this.type != null)
+ throw new InvalidOperationException ();
+
this.type = type;
- Type[] ifaces = null;
+ TypeManager.AddTypeParameter (type, this);
+ }
+
+ public void DefineConstraints ()
+ {
if (constraints != null)
constraints.Define (type);
- TypeManager.AddTypeParameter (type, this);
}
public bool DefineType (EmitContext ec)
public bool DefineType (EmitContext ec, MethodBuilder builder,
MethodInfo implementing, bool is_override)
{
- GenericConstraints gc;
-
if (implementing != null) {
if (is_override && (constraints != null)) {
Report.Error (
int pos = type.GenericParameterPosition;
ParameterData pd = Invocation.GetParameterData (mb);
- gc = pd.GenericConstraints (pos);
+ GenericConstraints temp_gc = pd.GenericConstraints (pos);
Type mparam = mb.GetGenericArguments () [pos];
- if (((constraints != null) && (gc == null)) ||
- ((constraints == null) && (gc != null)) ||
- ((constraints != null) &&
- !constraints.CheckInterfaceMethod (ec, gc))) {
+ if (temp_gc != null)
+ gc = new InflatedConstraints (temp_gc, implementing.DeclaringType);
+ else if (constraints != null)
+ gc = new InflatedConstraints (constraints, implementing.DeclaringType);
+
+ bool ok = true;
+ if (constraints != null) {
+ if (temp_gc == null)
+ ok = false;
+ else if (!constraints.CheckInterfaceMethod (ec, gc))
+ ok = false;
+ } else {
+ if (!is_override && (temp_gc != null))
+ ok = false;
+ }
+
+ if (!ok) {
Report.SymbolRelatedToPreviousError (implementing);
Report.Error (
return true;
}
+ //
+ // MemberContainer
+ //
+
+ public override bool Define ()
+ {
+ return true;
+ }
+
+ protected override void VerifyObsoleteAttribute ()
+ { }
+
+ public override void ApplyAttributeBuilder (Attribute a,
+ CustomAttributeBuilder cb)
+ { }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return (AttributeTargets) 0;
+ }
+ }
+
+ public override string[] ValidAttributeTargets {
+ get {
+ return new string [0];
+ }
+ }
+
//
// IMemberContainer
//
- IMemberContainer IMemberContainer.ParentContainer {
+ string IMemberContainer.Name {
+ get { return Name; }
+ }
+
+ MemberCache IMemberContainer.ParentCache {
get { return null; }
}
{
return "TypeParameter[" + name + "]";
}
+
+ protected class InflatedConstraints : GenericConstraints
+ {
+ GenericConstraints gc;
+ Type base_type;
+ Type class_constraint;
+ Type[] iface_constraints;
+ Type[] dargs;
+ Type declaring;
+
+ public InflatedConstraints (GenericConstraints gc, Type declaring)
+ {
+ this.gc = gc;
+ this.declaring = declaring;
+
+ dargs = TypeManager.GetTypeArguments (declaring);
+
+ ArrayList list = new ArrayList ();
+ if (gc.HasClassConstraint)
+ list.Add (inflate (gc.ClassConstraint));
+ foreach (Type iface in gc.InterfaceConstraints)
+ list.Add (inflate (iface));
+
+ bool has_class_constr = false;
+ if (list.Count > 0) {
+ Type first = (Type) list [0];
+ has_class_constr = !first.IsInterface && !first.IsGenericParameter;
+ }
+
+ if ((list.Count > 0) && has_class_constr) {
+ class_constraint = (Type) list [0];
+ iface_constraints = new Type [list.Count - 1];
+ list.CopyTo (1, iface_constraints, 0, list.Count - 1);
+ } else {
+ iface_constraints = new Type [list.Count];
+ list.CopyTo (iface_constraints, 0);
+ }
+
+ if (HasValueTypeConstraint)
+ base_type = TypeManager.value_type;
+ else if (class_constraint != null)
+ base_type = class_constraint;
+ else
+ base_type = TypeManager.object_type;
+ }
+
+ Type inflate (Type t)
+ {
+ if (t == null)
+ return null;
+ if (t.IsGenericParameter)
+ return dargs [t.GenericParameterPosition];
+ if (t.IsGenericInstance) {
+ t = t.GetGenericTypeDefinition ();
+ t = t.BindGenericParameters (dargs);
+ }
+
+ return t;
+ }
+
+ public override GenericParameterAttributes Attributes {
+ get { return gc.Attributes; }
+ }
+
+ public override Type ClassConstraint {
+ get { return class_constraint; }
+ }
+
+ public override Type EffectiveBaseClass {
+ get { return base_type; }
+ }
+
+ public override Type[] InterfaceConstraints {
+ get { return iface_constraints; }
+ }
+ }
}
//
get { return false; }
}
+ public override bool CheckAccessLevel (DeclSpace ds)
+ {
+ return true;
+ }
+
public void Error_CannotUseAsUnmanagedType (Location loc)
{
Report.Error (-203, loc, "Can not use type parameter as unamanged type");
atypes = new Type [count];
for (int i = 0; i < count; i++){
- TypeExpr te = ds.ResolveTypeExpr (
- (Expression) args [i], false, Location);
+ TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec);
if (te == null) {
ok = false;
continue;
return false;
}
- return Convert.ImplicitStandardConversionExists (expr, ctype);
+ return Convert.ImplicitStandardConversionExists (ec, expr, ctype);
}
protected bool CheckConstraints (EmitContext ec, int index)
Expression aexpr = new EmptyExpression (atype);
- Type parent = ptype.BaseType;
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
+ if (gc == null)
+ return true;
//
// First, check the `class' and `struct' constraints.
//
- if (parent == TypeManager.object_type) {
- if (!atype.IsClass) {
- Report.Error (452, loc, "The type `{0}' must be " +
- "a reference type in order to use it " +
- "as type parameter `{1}' in the " +
- "generic type or method `{2}'.",
- atype, ptype, DeclarationName);
- return false;
- }
- } else if (parent == TypeManager.value_type) {
- if (!atype.IsValueType) {
- Report.Error (453, loc, "The type `{0}' must be " +
- "a value type in order to use it " +
- "as type parameter `{1}' in the " +
- "generic type or method `{2}'.",
- atype, ptype, DeclarationName);
- return false;
- }
+ if (gc.HasReferenceTypeConstraint && !atype.IsClass) {
+ Report.Error (452, loc, "The type `{0}' must be " +
+ "a reference type in order to use it " +
+ "as type parameter `{1}' in the " +
+ "generic type or method `{2}'.",
+ atype, ptype, DeclarationName);
+ return false;
+ } else if (gc.HasValueTypeConstraint && !atype.IsValueType) {
+ Report.Error (453, loc, "The type `{0}' must be " +
+ "a value type in order to use it " +
+ "as type parameter `{1}' in the " +
+ "generic type or method `{2}'.",
+ atype, ptype, DeclarationName);
+ return false;
}
//
// The class constraint comes next.
//
- if ((parent != null) && (parent != TypeManager.object_type)) {
- if (!CheckConstraint (ec, ptype, aexpr, parent)) {
+ if (gc.HasClassConstraint) {
+ if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint)) {
Report.Error (309, loc, "The type `{0}' must be " +
"convertible to `{1}' in order to " +
"use it as parameter `{2}' in the " +
"generic type or method `{3}'",
- atype, parent, ptype, DeclarationName);
+ atype, gc.ClassConstraint, ptype, DeclarationName);
return false;
}
}
//
// Now, check the interface constraints.
//
- foreach (Type it in TypeManager.GetInterfaces (ptype)) {
+ foreach (Type it in gc.InterfaceConstraints) {
Type itype;
if (it.IsGenericParameter)
itype = atypes [it.GenericParameterPosition];
// Finally, check the constructor constraint.
//
- if (!TypeManager.HasConstructorConstraint (ptype))
+ if (!gc.HasConstructorConstraint)
return true;
if (TypeManager.IsBuiltinType (atype))
public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
{
- if (gt != null)
+ if (type != null)
return this;
+ if (gt != null)
+ return DoResolveType (ec);
//
// First, resolve the generic type.
gt = nested.GetGenericTypeDefinition ();
TypeArguments new_args = new TypeArguments (loc);
- foreach (TypeParameter param in ds.TypeParameters)
- new_args.Add (new TypeParameterExpr (param, loc));
+ if (ds.IsGeneric) {
+ foreach (TypeParameter param in ds.TypeParameters)
+ new_args.Add (new TypeParameterExpr (param, loc));
+ }
new_args.Add (args);
args = new_args;
- return this;
+ return DoResolveType (ec);
}
Type t;
SimpleName sn = new SimpleName (name, loc);
TypeExpr resolved = sn.ResolveAsTypeTerminal (ec);
- if ((resolved == null) || (resolved.Type == null)) {
- Report.Error (246, loc,
- "The type or namespace name `{0}<...>' "+
- "could not be found", Basename);
+ if (resolved == null)
return null;
- }
t = resolved.Type;
if (t == null) {
}
gt = t.GetGenericTypeDefinition ();
- return this;
+ return DoResolveType (ec);
}
- public override Type ResolveType (EmitContext ec)
+ TypeExpr DoResolveType (EmitContext ec)
{
- if (type != null)
- return type;
- if (DoResolveAsTypeStep (ec) == null)
- return null;
-
//
// Resolve the arguments.
//
// Now bind the parameters.
//
type = gt.BindGenericParameters (atypes);
- return type;
+ return this;
}
public Expression GetSimpleName (EmitContext ec)
return true;
}
- public bool Define (MethodBuilder mb)
+ public bool Define (MethodBuilder mb, Type return_type)
{
if (!Define ())
return false;
for (int i = 0; i < TypeParameters.Length; i++)
TypeParameters [i].Define (gen_params [i]);
+ ec = new EmitContext (
+ this, this, Location, null, return_type, ModFlags, false);
+
return true;
}
public override Expression DoResolve (EmitContext ec)
{
- type = ec.DeclSpace.ResolveType (expr, false, loc);
+ TypeExpr texpr = expr.ResolveAsTypeTerminal (ec);
+ if (texpr == null)
+ return null;
+
+ type = texpr.ResolveType (ec);
if (type == null)
return null;