/mcs/mcs/generic.cs
C# | 3508 lines | 2450 code | 602 blank | 456 comment | 798 complexity | 4c8c62f9060af98106273ee597c060b6 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- //
- // generic.cs: Generics support
- //
- // Authors: Martin Baulig (martin@ximian.com)
- // Miguel de Icaza (miguel@ximian.com)
- // Marek Safar (marek.safar@gmail.com)
- //
- // Dual licensed under the terms of the MIT X11 or GNU GPL
- //
- // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
- // Copyright 2004-2008 Novell, Inc
- // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
- //
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- #if STATIC
- using MetaType = IKVM.Reflection.Type;
- using IKVM.Reflection;
- using IKVM.Reflection.Emit;
- #else
- using MetaType = System.Type;
- using System.Reflection;
- using System.Reflection.Emit;
- #endif
- namespace Mono.CSharp {
- public enum Variance
- {
- //
- // Don't add or modify internal values, they are used as -/+ calculation signs
- //
- None = 0,
- Covariant = 1,
- Contravariant = -1
- }
- [Flags]
- public enum SpecialConstraint
- {
- None = 0,
- Constructor = 1 << 2,
- Class = 1 << 3,
- Struct = 1 << 4
- }
- public class SpecialContraintExpr : FullNamedExpression
- {
- public SpecialContraintExpr (SpecialConstraint constraint, Location loc)
- {
- this.loc = loc;
- this.Constraint = constraint;
- }
- public SpecialConstraint Constraint { get; private set; }
- protected override Expression DoResolve (ResolveContext rc)
- {
- throw new NotImplementedException ();
- }
- public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext ec)
- {
- throw new NotImplementedException ();
- }
- }
- //
- // A set of parsed constraints for a type parameter
- //
- public class Constraints
- {
- SimpleMemberName tparam;
- List<FullNamedExpression> constraints;
- Location loc;
- bool resolved;
- bool resolving;
-
- public Constraints (SimpleMemberName tparam, List<FullNamedExpression> constraints, Location loc)
- {
- this.tparam = tparam;
- this.constraints = constraints;
- this.loc = loc;
- }
- #region Properties
- public List<FullNamedExpression> TypeExpressions {
- get {
- return constraints;
- }
- }
- public Location Location {
- get {
- return loc;
- }
- }
- public SimpleMemberName TypeParameter {
- get {
- return tparam;
- }
- }
- #endregion
- public static bool CheckConflictingInheritedConstraint (TypeParameterSpec spec, TypeSpec bb, IMemberContext context, Location loc)
- {
- if (spec.HasSpecialClass && bb.IsStruct) {
- context.Module.Compiler.Report.Error (455, loc,
- "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
- spec.Name, "class", bb.GetSignatureForError ());
- return false;
- }
- return CheckConflictingInheritedConstraint (spec, spec.BaseType, bb, context, loc);
- }
- static bool CheckConflictingInheritedConstraint (TypeParameterSpec spec, TypeSpec ba, TypeSpec bb, IMemberContext context, Location loc)
- {
- if (ba == bb)
- return true;
- if (TypeSpec.IsBaseClass (ba, bb, false) || TypeSpec.IsBaseClass (bb, ba, false))
- return true;
- Error_ConflictingConstraints (context, spec, ba, bb, loc);
- return false;
- }
- public static void Error_ConflictingConstraints (IMemberContext context, TypeParameterSpec tp, TypeSpec ba, TypeSpec bb, Location loc)
- {
- context.Module.Compiler.Report.Error (455, loc,
- "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
- tp.Name, ba.GetSignatureForError (), bb.GetSignatureForError ());
- }
- public void CheckGenericConstraints (IMemberContext context, bool obsoleteCheck)
- {
- foreach (var c in constraints) {
- if (c == null)
- continue;
- var t = c.Type;
- if (t == null)
- continue;
- if (obsoleteCheck) {
- ObsoleteAttribute obsolete_attr = t.GetAttributeObsolete ();
- if (obsolete_attr != null)
- AttributeTester.Report_ObsoleteMessage (obsolete_attr, t.GetSignatureForError (), c.Location, context.Module.Compiler.Report);
- }
- ConstraintChecker.Check (context, t, c.Location);
- }
- }
- //
- // Resolve the constraints types with only possible early checks, return
- // value `false' is reserved for recursive failure
- //
- public bool Resolve (IMemberContext context, TypeParameter tp)
- {
- if (resolved)
- return true;
- if (resolving)
- return false;
- resolving = true;
- var spec = tp.Type;
- List<TypeParameterSpec> tparam_types = null;
- bool iface_found = false;
- spec.BaseType = context.Module.Compiler.BuiltinTypes.Object;
- for (int i = 0; i < constraints.Count; ++i) {
- var constraint = constraints[i];
- if (constraint is SpecialContraintExpr) {
- spec.SpecialConstraint |= ((SpecialContraintExpr) constraint).Constraint;
- if (spec.HasSpecialStruct)
- spec.BaseType = context.Module.Compiler.BuiltinTypes.ValueType;
- // Set to null as it does not have a type
- constraints[i] = null;
- continue;
- }
- var type = constraint.ResolveAsType (context);
- if (type == null)
- continue;
- if (type.Arity > 0 && ((InflatedTypeSpec) type).HasDynamicArgument ()) {
- context.Module.Compiler.Report.Error (1968, constraint.Location,
- "A constraint cannot be the dynamic type `{0}'", type.GetSignatureForError ());
- continue;
- }
- if (!context.CurrentMemberDefinition.IsAccessibleAs (type)) {
- context.Module.Compiler.Report.SymbolRelatedToPreviousError (type);
- context.Module.Compiler.Report.Error (703, loc,
- "Inconsistent accessibility: constraint type `{0}' is less accessible than `{1}'",
- type.GetSignatureForError (), context.GetSignatureForError ());
- }
- if (type.IsInterface) {
- if (!spec.AddInterface (type)) {
- context.Module.Compiler.Report.Error (405, constraint.Location,
- "Duplicate constraint `{0}' for type parameter `{1}'", type.GetSignatureForError (), tparam.Value);
- }
- iface_found = true;
- continue;
- }
- var constraint_tp = type as TypeParameterSpec;
- if (constraint_tp != null) {
- if (tparam_types == null) {
- tparam_types = new List<TypeParameterSpec> (2);
- } else if (tparam_types.Contains (constraint_tp)) {
- context.Module.Compiler.Report.Error (405, constraint.Location,
- "Duplicate constraint `{0}' for type parameter `{1}'", type.GetSignatureForError (), tparam.Value);
- continue;
- }
- //
- // Checks whether each generic method parameter constraint type
- // is valid with respect to T
- //
- if (tp.IsMethodTypeParameter) {
- TypeManager.CheckTypeVariance (type, Variance.Contravariant, context);
- }
- var tp_def = constraint_tp.MemberDefinition as TypeParameter;
- if (tp_def != null && !tp_def.ResolveConstraints (context)) {
- context.Module.Compiler.Report.Error (454, constraint.Location,
- "Circular constraint dependency involving `{0}' and `{1}'",
- constraint_tp.GetSignatureForError (), tp.GetSignatureForError ());
- continue;
- }
- //
- // Checks whether there are no conflicts between type parameter constraints
- //
- // class Foo<T, U>
- // where T : A
- // where U : B, T
- //
- // A and B are not convertible and only 1 class constraint is allowed
- //
- if (constraint_tp.HasTypeConstraint) {
- if (spec.HasTypeConstraint || spec.HasSpecialStruct) {
- if (!CheckConflictingInheritedConstraint (spec, constraint_tp.BaseType, context, constraint.Location))
- continue;
- } else {
- for (int ii = 0; ii < tparam_types.Count; ++ii) {
- if (!tparam_types[ii].HasTypeConstraint)
- continue;
- if (!CheckConflictingInheritedConstraint (spec, tparam_types[ii].BaseType, constraint_tp.BaseType, context, constraint.Location))
- break;
- }
- }
- }
- if (constraint_tp.HasSpecialStruct) {
- context.Module.Compiler.Report.Error (456, constraint.Location,
- "Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
- constraint_tp.GetSignatureForError (), tp.GetSignatureForError ());
- continue;
- }
- tparam_types.Add (constraint_tp);
- continue;
- }
- if (iface_found || spec.HasTypeConstraint) {
- context.Module.Compiler.Report.Error (406, constraint.Location,
- "The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
- type.GetSignatureForError ());
- }
- if (spec.HasSpecialStruct || spec.HasSpecialClass) {
- context.Module.Compiler.Report.Error (450, constraint.Location,
- "`{0}': cannot specify both a constraint class and the `class' or `struct' constraint",
- type.GetSignatureForError ());
- }
- switch (type.BuiltinType) {
- case BuiltinTypeSpec.Type.Array:
- case BuiltinTypeSpec.Type.Delegate:
- case BuiltinTypeSpec.Type.MulticastDelegate:
- case BuiltinTypeSpec.Type.Enum:
- case BuiltinTypeSpec.Type.ValueType:
- case BuiltinTypeSpec.Type.Object:
- context.Module.Compiler.Report.Error (702, constraint.Location,
- "A constraint cannot be special class `{0}'", type.GetSignatureForError ());
- continue;
- case BuiltinTypeSpec.Type.Dynamic:
- context.Module.Compiler.Report.Error (1967, constraint.Location,
- "A constraint cannot be the dynamic type");
- continue;
- }
- if (type.IsSealed || !type.IsClass) {
- context.Module.Compiler.Report.Error (701, loc,
- "`{0}' is not a valid constraint. A constraint must be an interface, a non-sealed class or a type parameter",
- TypeManager.CSharpName (type));
- continue;
- }
- if (type.IsStatic) {
- context.Module.Compiler.Report.Error (717, constraint.Location,
- "`{0}' is not a valid constraint. Static classes cannot be used as constraints",
- type.GetSignatureForError ());
- }
- spec.BaseType = type;
- }
- if (tparam_types != null)
- spec.TypeArguments = tparam_types.ToArray ();
- resolving = false;
- resolved = true;
- return true;
- }
- public void VerifyClsCompliance (Report report)
- {
- foreach (var c in constraints)
- {
- if (c == null)
- continue;
- if (!c.Type.IsCLSCompliant ()) {
- report.SymbolRelatedToPreviousError (c.Type);
- report.Warning (3024, 1, loc, "Constraint type `{0}' is not CLS-compliant",
- c.Type.GetSignatureForError ());
- }
- }
- }
- }
- //
- // A type parameter for a generic type or generic method definition
- //
- public class TypeParameter : MemberCore, ITypeDefinition
- {
- static readonly string[] attribute_target = new string [] { "type parameter" };
-
- Constraints constraints;
- GenericTypeParameterBuilder builder;
- readonly TypeParameterSpec spec;
- public TypeParameter (int index, MemberName name, Constraints constraints, Attributes attrs, Variance variance)
- : base (null, name, attrs)
- {
- this.constraints = constraints;
- this.spec = new TypeParameterSpec (null, index, this, SpecialConstraint.None, variance, null);
- }
- //
- // Used by parser
- //
- public TypeParameter (MemberName name, Attributes attrs, Variance variance)
- : base (null, name, attrs)
- {
- this.spec = new TypeParameterSpec (null, -1, this, SpecialConstraint.None, variance, null);
- }
-
- public TypeParameter (TypeParameterSpec spec, TypeSpec parentSpec, MemberName name, Attributes attrs)
- : base (null, name, attrs)
- {
- this.spec = new TypeParameterSpec (parentSpec, spec.DeclaredPosition, spec.MemberDefinition, spec.SpecialConstraint, spec.Variance, null) {
- BaseType = spec.BaseType,
- InterfacesDefined = spec.InterfacesDefined,
- TypeArguments = spec.TypeArguments
- };
- }
-
- #region Properties
- public override AttributeTargets AttributeTargets {
- get {
- return AttributeTargets.GenericParameter;
- }
- }
- public Constraints Constraints {
- get {
- return constraints;
- }
- set {
- constraints = value;
- }
- }
- public IAssemblyDefinition DeclaringAssembly {
- get {
- return Module.DeclaringAssembly;
- }
- }
- public override string DocCommentHeader {
- get {
- throw new InvalidOperationException (
- "Unexpected attempt to get doc comment from " + this.GetType ());
- }
- }
- bool ITypeDefinition.IsComImport {
- get {
- return false;
- }
- }
- bool ITypeDefinition.IsPartial {
- get {
- return false;
- }
- }
- public bool IsMethodTypeParameter {
- get {
- return spec.IsMethodOwned;
- }
- }
- bool ITypeDefinition.IsTypeForwarder {
- get {
- return false;
- }
- }
- public string Name {
- get {
- return MemberName.Name;
- }
- }
- public string Namespace {
- get {
- return null;
- }
- }
- public TypeParameterSpec Type {
- get {
- return spec;
- }
- }
- public int TypeParametersCount {
- get {
- return 0;
- }
- }
- public TypeParameterSpec[] TypeParameters {
- get {
- return null;
- }
- }
- public override string[] ValidAttributeTargets {
- get {
- return attribute_target;
- }
- }
- public Variance Variance {
- get {
- return spec.Variance;
- }
- }
- #endregion
- //
- // This is called for each part of a partial generic type definition.
- //
- // If partial type parameters constraints are not null and we don't
- // already have constraints they become our constraints. If we already
- // have constraints, we must check that they're the same.
- //
- public bool AddPartialConstraints (TypeDefinition part, TypeParameter tp)
- {
- if (builder == null)
- throw new InvalidOperationException ();
- var new_constraints = tp.constraints;
- if (new_constraints == null)
- return true;
- // TODO: could create spec only
- //tp.Define (null, -1, part.Definition);
- tp.spec.DeclaringType = part.Definition;
- if (!tp.ResolveConstraints (part))
- return false;
- if (constraints != null)
- return spec.HasSameConstraintsDefinition (tp.Type);
- // Copy constraint from resolved part to partial container
- spec.SpecialConstraint = tp.spec.SpecialConstraint;
- spec.Interfaces = tp.spec.Interfaces;
- spec.TypeArguments = tp.spec.TypeArguments;
- spec.BaseType = tp.spec.BaseType;
-
- return true;
- }
- public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
- {
- builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
- }
- public void CheckGenericConstraints (bool obsoleteCheck)
- {
- if (constraints != null)
- constraints.CheckGenericConstraints (this, obsoleteCheck);
- }
- public TypeParameter CreateHoistedCopy (TypeSpec declaringSpec)
- {
- return new TypeParameter (spec, declaringSpec, MemberName, null);
- }
- public override bool Define ()
- {
- return true;
- }
- //
- // This is the first method which is called during the resolving
- // process; we're called immediately after creating the type parameters
- // with SRE (by calling `DefineGenericParameters()' on the TypeBuilder /
- // MethodBuilder).
- //
- public void Define (GenericTypeParameterBuilder type, TypeSpec declaringType, TypeContainer parent)
- {
- if (builder != null)
- throw new InternalErrorException ();
- // Needed to get compiler reference
- this.Parent = parent;
- this.builder = type;
- spec.DeclaringType = declaringType;
- spec.SetMetaInfo (type);
- }
- public void EmitConstraints (GenericTypeParameterBuilder builder)
- {
- var attr = GenericParameterAttributes.None;
- if (spec.Variance == Variance.Contravariant)
- attr |= GenericParameterAttributes.Contravariant;
- else if (spec.Variance == Variance.Covariant)
- attr |= GenericParameterAttributes.Covariant;
- if (spec.HasSpecialClass)
- attr |= GenericParameterAttributes.ReferenceTypeConstraint;
- else if (spec.HasSpecialStruct)
- attr |= GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint;
- if (spec.HasSpecialConstructor)
- attr |= GenericParameterAttributes.DefaultConstructorConstraint;
- if (spec.BaseType.BuiltinType != BuiltinTypeSpec.Type.Object)
- builder.SetBaseTypeConstraint (spec.BaseType.GetMetaInfo ());
- if (spec.InterfacesDefined != null)
- builder.SetInterfaceConstraints (spec.InterfacesDefined.Select (l => l.GetMetaInfo ()).ToArray ());
- if (spec.TypeArguments != null)
- builder.SetInterfaceConstraints (spec.TypeArguments.Select (l => l.GetMetaInfo ()).ToArray ());
- builder.SetGenericParameterAttributes (attr);
- }
- public override void Emit ()
- {
- EmitConstraints (builder);
- if (OptAttributes != null)
- OptAttributes.Emit ();
- base.Emit ();
- }
- public void ErrorInvalidVariance (IMemberContext mc, Variance expected)
- {
- Report.SymbolRelatedToPreviousError (mc.CurrentMemberDefinition);
- string input_variance = Variance == Variance.Contravariant ? "contravariant" : "covariant";
- string gtype_variance;
- switch (expected) {
- case Variance.Contravariant: gtype_variance = "contravariantly"; break;
- case Variance.Covariant: gtype_variance = "covariantly"; break;
- default: gtype_variance = "invariantly"; break;
- }
- Delegate d = mc as Delegate;
- string parameters = d != null ? d.Parameters.GetSignatureForError () : "";
- Report.Error (1961, Location,
- "The {2} type parameter `{0}' must be {3} valid on `{1}{4}'",
- GetSignatureForError (), mc.GetSignatureForError (), input_variance, gtype_variance, parameters);
- }
- public TypeSpec GetAttributeCoClass ()
- {
- return null;
- }
- public string GetAttributeDefaultMember ()
- {
- throw new NotSupportedException ();
- }
- public AttributeUsageAttribute GetAttributeUsage (PredefinedAttribute pa)
- {
- throw new NotSupportedException ();
- }
- public override string GetSignatureForDocumentation ()
- {
- throw new NotImplementedException ();
- }
- public override string GetSignatureForError ()
- {
- return MemberName.Name;
- }
- bool ITypeDefinition.IsInternalAsPublic (IAssemblyDefinition assembly)
- {
- return spec.MemberDefinition.DeclaringAssembly == assembly;
- }
- public void LoadMembers (TypeSpec declaringType, bool onlyTypes, ref MemberCache cache)
- {
- throw new NotSupportedException ("Not supported for compiled definition");
- }
- //
- // Resolves all type parameter constraints
- //
- public bool ResolveConstraints (IMemberContext context)
- {
- if (constraints != null)
- return constraints.Resolve (context, this);
- if (spec.BaseType == null)
- spec.BaseType = context.Module.Compiler.BuiltinTypes.Object;
- return true;
- }
- public override bool IsClsComplianceRequired ()
- {
- return false;
- }
- public new void VerifyClsCompliance ()
- {
- if (constraints != null)
- constraints.VerifyClsCompliance (Report);
- }
- public void WarningParentNameConflict (TypeParameter conflict)
- {
- conflict.Report.SymbolRelatedToPreviousError (conflict.Location, null);
- conflict.Report.Warning (693, 3, Location,
- "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
- GetSignatureForError (), conflict.CurrentType.GetSignatureForError ());
- }
- }
- [System.Diagnostics.DebuggerDisplay ("{DisplayDebugInfo()}")]
- public class TypeParameterSpec : TypeSpec
- {
- public static readonly new TypeParameterSpec[] EmptyTypes = new TypeParameterSpec[0];
- Variance variance;
- SpecialConstraint spec;
- int tp_pos;
- TypeSpec[] targs;
- TypeSpec[] ifaces_defined;
- //
- // Creates type owned type parameter
- //
- public TypeParameterSpec (TypeSpec declaringType, int index, ITypeDefinition definition, SpecialConstraint spec, Variance variance, MetaType info)
- : base (MemberKind.TypeParameter, declaringType, definition, info, Modifiers.PUBLIC)
- {
- this.variance = variance;
- this.spec = spec;
- state &= ~StateFlags.Obsolete_Undetected;
- tp_pos = index;
- }
- //
- // Creates method owned type parameter
- //
- public TypeParameterSpec (int index, ITypeDefinition definition, SpecialConstraint spec, Variance variance, MetaType info)
- : this (null, index, definition, spec, variance, info)
- {
- }
- #region Properties
- public int DeclaredPosition {
- get {
- return tp_pos;
- }
- set {
- tp_pos = value;
- }
- }
- public bool HasSpecialConstructor {
- get {
- return (spec & SpecialConstraint.Constructor) != 0;
- }
- }
- public bool HasSpecialClass {
- get {
- return (spec & SpecialConstraint.Class) != 0;
- }
- }
- public bool HasSpecialStruct {
- get {
- return (spec & SpecialConstraint.Struct) != 0;
- }
- }
- public bool HasAnyTypeConstraint {
- get {
- return (spec & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0 || ifaces != null || targs != null || HasTypeConstraint;
- }
- }
- public bool HasTypeConstraint {
- get {
- var bt = BaseType.BuiltinType;
- return bt != BuiltinTypeSpec.Type.Object && bt != BuiltinTypeSpec.Type.ValueType;
- }
- }
- public override IList<TypeSpec> Interfaces {
- get {
- if ((state & StateFlags.InterfacesExpanded) == 0) {
- if (ifaces != null) {
- if (ifaces_defined == null)
- ifaces_defined = ifaces.ToArray ();
- for (int i = 0; i < ifaces_defined.Length; ++i ) {
- var iface_type = ifaces_defined[i];
- var td = iface_type.MemberDefinition as TypeDefinition;
- if (td != null)
- td.DoExpandBaseInterfaces ();
- if (iface_type.Interfaces != null) {
- for (int ii = 0; ii < iface_type.Interfaces.Count; ++ii) {
- var ii_iface_type = iface_type.Interfaces [ii];
- AddInterface (ii_iface_type);
- }
- }
- }
- }
- //
- // Include all base type interfaces too, see ImportTypeBase for details
- //
- if (BaseType != null) {
- var td = BaseType.MemberDefinition as TypeDefinition;
- if (td != null)
- td.DoExpandBaseInterfaces ();
- if (BaseType.Interfaces != null) {
- foreach (var iface in BaseType.Interfaces) {
- AddInterface (iface);
- }
- }
- }
- if (ifaces_defined == null)
- ifaces_defined = ifaces == null ? TypeSpec.EmptyTypes : ifaces.ToArray ();
- state |= StateFlags.InterfacesExpanded;
- }
- return ifaces;
- }
- }
- //
- // Unexpanded interfaces list
- //
- public TypeSpec[] InterfacesDefined {
- get {
- if (ifaces_defined == null) {
- if (ifaces == null)
- return null;
- ifaces_defined = ifaces.ToArray ();
- }
- return ifaces_defined.Length == 0 ? null : ifaces_defined;
- }
- set {
- ifaces_defined = value;
- if (value != null && value.Length != 0)
- ifaces = new List<TypeSpec> (value);
- }
- }
- public bool IsConstrained {
- get {
- return spec != SpecialConstraint.None || ifaces != null || targs != null || HasTypeConstraint;
- }
- }
- //
- // Returns whether the type parameter is known to be a reference type
- //
- public new bool IsReferenceType {
- get {
- if ((spec & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0)
- return (spec & SpecialConstraint.Class) != 0;
- //
- // Full check is needed (see IsValueType for details)
- //
- if (HasTypeConstraint && TypeSpec.IsReferenceType (BaseType))
- return true;
- if (targs != null) {
- foreach (var ta in targs) {
- //
- // Secondary special constraints are ignored (I am not sure why)
- //
- var tp = ta as TypeParameterSpec;
- if (tp != null && (tp.spec & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0)
- continue;
- if (TypeSpec.IsReferenceType (ta))
- return true;
- }
- }
- return false;
- }
- }
- //
- // Returns whether the type parameter is known to be a value type
- //
- public new bool IsValueType {
- get {
- //
- // Even if structs/enums cannot be used directly as constraints
- // they can apear as constraint type when inheriting base constraint
- // which has dependant type parameter constraint which has been
- // inflated using value type
- //
- // class A : B<int> { override void Foo<U> () {} }
- // class B<T> { virtual void Foo<U> () where U : T {} }
- //
- if (HasSpecialStruct)
- return true;
- if (targs != null) {
- foreach (var ta in targs) {
- if (TypeSpec.IsValueType (ta))
- return true;
- }
- }
- return false;
- }
- }
- public override string Name {
- get {
- return definition.Name;
- }
- }
- public bool IsMethodOwned {
- get {
- return DeclaringType == null;
- }
- }
- public SpecialConstraint SpecialConstraint {
- get {
- return spec;
- }
- set {
- spec = value;
- }
- }
- //
- // Types used to inflate the generic type
- //
- public new TypeSpec[] TypeArguments {
- get {
- return targs;
- }
- set {
- targs = value;
- }
- }
- public Variance Variance {
- get {
- return variance;
- }
- }
- #endregion
- public string DisplayDebugInfo ()
- {
- var s = GetSignatureForError ();
- return IsMethodOwned ? s + "!!" : s + "!";
- }
- //
- // Finds effective base class. The effective base class is always a class-type
- //
- public TypeSpec GetEffectiveBase ()
- {
- if (HasSpecialStruct)
- return BaseType;
- //
- // If T has a class-type constraint C but no type-parameter constraints, its effective base class is C
- //
- if (BaseType != null && targs == null) {
- //
- // If T has a constraint V that is a value-type, use instead the most specific base type of V that is a class-type.
- //
- // LAMESPEC: Is System.ValueType always the most specific base type in this case?
- //
- // Note: This can never happen in an explicitly given constraint, but may occur when the constraints of a generic method
- // are implicitly inherited by an overriding method declaration or an explicit implementation of an interface method.
- //
- return BaseType.IsStruct ? BaseType.BaseType : BaseType;
- }
- var types = targs;
- if (HasTypeConstraint) {
- Array.Resize (ref types, types.Length + 1);
- for (int i = 0; i < types.Length - 1; ++i) {
- types[i] = types[i].BaseType;
- }
- types[types.Length - 1] = BaseType;
- } else {
- types = types.Select (l => l.BaseType).ToArray ();
- }
- if (types != null)
- return Convert.FindMostEncompassedType (types);
- return BaseType;
- }
- public override string GetSignatureForDocumentation ()
- {
- var prefix = IsMethodOwned ? "``" : "`";
- return prefix + DeclaredPosition;
- }
- public override string GetSignatureForError ()
- {
- return Name;
- }
- //
- // Constraints have to match by definition but not position, used by
- // partial classes or methods
- //
- public bool HasSameConstraintsDefinition (TypeParameterSpec other)
- {
- if (spec != other.spec)
- return false;
- if (BaseType != other.BaseType)
- return false;
- if (!TypeSpecComparer.Override.IsSame (InterfacesDefined, other.InterfacesDefined))
- return false;
- if (!TypeSpecComparer.Override.IsSame (targs, other.targs))
- return false;
- return true;
- }
- //
- // Constraints have to match by using same set of types, used by
- // implicit interface implementation
- //
- public bool HasSameConstraintsImplementation (TypeParameterSpec other)
- {
- if (spec != other.spec)
- return false;
- //
- // It can be same base type or inflated type parameter
- //
- // interface I<T> { void Foo<U> where U : T; }
- // class A : I<int> { void Foo<X> where X : int {} }
- //
- bool found;
- if (!TypeSpecComparer.Override.IsEqual (BaseType, other.BaseType)) {
- if (other.targs == null)
- return false;
- found = false;
- foreach (var otarg in other.targs) {
- if (TypeSpecComparer.Override.IsEqual (BaseType, otarg)) {
- found = true;
- break;
- }
- }
- if (!found)
- return false;
- }
- // Check interfaces implementation -> definition
- if (InterfacesDefined != null) {
- //
- // Iterate over inflated interfaces
- //
- foreach (var iface in Interfaces) {
- found = false;
- if (other.InterfacesDefined != null) {
- foreach (var oiface in other.Interfaces) {
- if (TypeSpecComparer.Override.IsEqual (iface, oiface)) {
- found = true;
- break;
- }
- }
- }
- if (found)
- continue;
- if (other.targs != null) {
- foreach (var otarg in other.targs) {
- if (TypeSpecComparer.Override.IsEqual (BaseType, otarg)) {
- found = true;
- break;
- }
- }
- }
- if (!found)
- return false;
- }
- }
- // Check interfaces implementation <- definition
- if (other.InterfacesDefined != null) {
- if (InterfacesDefined == null)
- return false;
- //
- // Iterate over inflated interfaces
- //
- foreach (var oiface in other.Interfaces) {
- found = false;
- foreach (var iface in Interfaces) {
- if (TypeSpecComparer.Override.IsEqual (iface, oiface)) {
- found = true;
- break;
- }
- }
- if (!found)
- return false;
- }
- }
- // Check type parameters implementation -> definition
- if (targs != null) {
- if (other.targs == null)
- return false;
- foreach (var targ in targs) {
- found = false;
- foreach (var otarg in other.targs) {
- if (TypeSpecComparer.Override.IsEqual (targ, otarg)) {
- found = true;
- break;
- }
- }
- if (!found)
- return false;
- }
- }
- // Check type parameters implementation <- definition
- if (other.targs != null) {
- foreach (var otarg in other.targs) {
- // Ignore inflated type arguments, were checked above
- if (!otarg.IsGenericParameter)
- continue;
- if (targs == null)
- return false;
- found = false;
- foreach (var targ in targs) {
- if (TypeSpecComparer.Override.IsEqual (targ, otarg)) {
- found = true;
- break;
- }
- }
- if (!found)
- return false;
- }
- }
- return true;
- }
- public static TypeParameterSpec[] InflateConstraints (TypeParameterInflator inflator, TypeParameterSpec[] tparams)
- {
- return InflateConstraints (tparams, l => l, inflator);
- }
- public static TypeParameterSpec[] InflateConstraints<T> (TypeParameterSpec[] tparams, Func<T, TypeParameterInflator> inflatorFactory, T arg)
- {
- TypeParameterSpec[] constraints = null;
- TypeParameterInflator? inflator = null;
- for (int i = 0; i < tparams.Length; ++i) {
- var tp = tparams[i];
- if (tp.HasTypeConstraint || tp.InterfacesDefined != null || tp.TypeArguments != null) {
- if (constraints == null) {
- constraints = new TypeParameterSpec[tparams.Length];
- Array.Copy (tparams, constraints, constraints.Length);
- }
- //
- // Using a factory to avoid possibly expensive inflator build up
- //
- if (inflator == null)
- inflator = inflatorFactory (arg);
- constraints[i] = (TypeParameterSpec) constraints[i].InflateMember (inflator.Value);
- }
- }
- if (constraints == null)
- constraints = tparams;
- return constraints;
- }
- public void InflateConstraints (TypeParameterInflator inflator, TypeParameterSpec tps)
- {
- tps.BaseType = inflator.Inflate (BaseType);
- var defined = InterfacesDefined;
- if (defined != null) {
- tps.ifaces_defined = new TypeSpec[defined.Length];
- for (int i = 0; i < defined.Length; ++i)
- tps.ifaces_defined [i] = inflator.Inflate (defined[i]);
- }
- var ifaces = Interfaces;
- if (ifaces != null) {
- tps.ifaces = new List<TypeSpec> (ifaces.Count);
- for (int i = 0; i < ifaces.Count; ++i)
- tps.ifaces.Add (inflator.Inflate (ifaces[i]));
- }
- if (targs != null) {
- tps.targs = new TypeSpec[targs.Length];
- for (int i = 0; i < targs.Length; ++i)
- tps.targs[i] = inflator.Inflate (targs[i]);
- }
- }
- public override MemberSpec InflateMember (TypeParameterInflator inflator)
- {
- var tps = (TypeParameterSpec) MemberwiseClone ();
- InflateConstraints (inflator, tps);
- return tps;
- }
- //
- // Populates type parameter members using type parameter constraints
- // The trick here is to be called late enough but not too late to
- // populate member cache with all members from other types
- //
- protected override void InitializeMemberCache (bool onlyTypes)
- {
- cache = new MemberCache ();
- //
- // For a type parameter the membercache is the union of the sets of members of the types
- // specified as a primary constraint or secondary constraint
- //
- if (BaseType.BuiltinType != BuiltinTypeSpec.Type.Object && BaseType.BuiltinType != BuiltinTypeSpec.Type.ValueType)
- cache.AddBaseType (BaseType);
- if (InterfacesDefined != null) {
- foreach (var iface_type in InterfacesDefined) {
- cache.AddInterface (iface_type);
- }
- }
- if (targs != null) {
- foreach (var ta in targs) {
- var b_type = ta.BaseType;
- if (b_type.BuiltinType != BuiltinTypeSpec.Type.Object && b_type.BuiltinType != BuiltinTypeSpec.Type.ValueType)
- cache.AddBaseType (b_type);
- var tps = ta as TypeParameterSpec;
- var ifaces = tps != null ? tps.InterfacesDefined : ta.Interfaces;
- if (ifaces != null) {
- foreach (var iface_type in ifaces) {
- cache.AddInterface (iface_type);
- }
- }
- }
- }
- }
- public bool IsConvertibleToInterface (TypeSpec iface)
- {
- if (Interfaces != null) {
- foreach (var t in Interfaces) {
- if (t == iface)
- return true;
- }
- }
- if (TypeArguments != null) {
- foreach (var t in TypeArguments) {
- if (((TypeParameterSpec) t).IsConvertibleToInterface (iface))
- return true;
- }
- }
- return false;
- }
- public static bool HasAnyTypeParameterTypeConstrained (IGenericMethodDefinition md)
- {
- var tps = md.TypeParameters;
- for (int i = 0; i < md.TypeParametersCount; ++i) {
- if (tps[i].HasAnyTypeConstraint) {
- return true;
- }
- }
- return false;
- }
- public static bool HasAnyTypeParameterConstrained (IGenericMethodDefinition md)
- {
- var tps = md.TypeParameters;
- for (int i = 0; i < md.TypeParametersCount; ++i) {
- if (tps[i].IsConstrained) {
- return true;
- }
- }
- return false;
- }
- public bool HasDependencyOn (TypeSpec type)
- {
- if (TypeArguments != null) {
- foreach (var targ in TypeArguments) {
- if (TypeSpecComparer.Override.IsEqual (targ, type))
- return true;
- var tps = targ as TypeParameterSpec;
- if (tps != null && tps.HasDependencyOn (type))
- return true;
- }
- }
- return false;
- }
- public override TypeSpec Mutate (TypeParameterMutator mutator)
- {
- return mutator.Mutate (this);
- }
- }
- public struct TypeParameterInflator
- {
- readonly TypeSpec type;
- readonly TypeParameterSpec[] tparams;
- readonly TypeSpec[] targs;
- readonly IModuleContext context;
- public TypeParameterInflator (TypeParameterInflator nested, TypeSpec type)
- : this (nested.context, type, nested.tparams, nested.targs)
- {
- }
- public TypeParameterInflator (IModuleContext context, TypeSpec type, TypeParameterSpec[] tparams, TypeSpec[] targs)
- {
- if (tparams.Length != targs.Length)
- throw new ArgumentException ("Invalid arguments");
- this.context = context;
- this.tparams = tparams;
- this.targs = targs;
- this.type = type;
- }
- #region Properties
- public IModuleContext Context {
- get {
- return context;
- }
- }
- public TypeSpec TypeInstance {
- get {
- return type;
- }
- }
- //
- // Type parameters to inflate
- //
- public TypeParameterSpec[] TypeParameters {
- get {
- return tparams;
- }
- }
- #endregion
- public TypeSpec Inflate (TypeSpec type)
- {
- var tp = type as TypeParameterSpec;
- if (tp != null)
- return Inflate (tp);
- var ac = type as ArrayContainer;
- if (ac != null) {
- var et = Inflate (ac.Element);
- if (et != ac.Element)
- return ArrayContainer.MakeType (context.Module, et, ac.Rank);
- return ac;
- }
- //
- // When inflating a nested type, inflate its parent first
- // in case it's using same type parameters (was inflated within the type)
- //
- TypeSpec[] targs;
- int i = 0;
- if (type.IsNested) {
- var parent = Inflate (type.DeclaringType);
- //
- // Keep the inflated type arguments
- //
- targs = type.TypeArguments;
- //
- // When inflating imported nested type used inside same declaring type, we get TypeSpec
- // because the import cache helps us to catch it. However, that means we have to look at
- // type definition to get type argument (they are in fact type parameter in this case)
- //
- if (targs.Length == 0 && type.Arity > 0)
- targs = type.MemberDefinition.TypeParameters;
- //
- // Parent was inflated, find the same type on inflated type
- // to use same cache for nested types on same generic parent
- //
- type = MemberCache.FindNestedType (parent, type.Name, type.Arity);
- //
- // Handle the tricky case where parent shares local type arguments
- // which means inflating inflated type
- //
- // class Test<T> {
- // public static Nested<T> Foo () { return null; }
- //
- // public class Nested<U> {}
- // }
- //
- // return type of Test<string>.Foo() has to be Test<string>.Nested<string>
- //
- if (targs.Length > 0) {
- var inflated_targs = new TypeSpec[targs.Length];
- for (; i < targs.Length; ++i)
- inflated_targs[i] = Inflate (targs[i]);
- type = type.MakeGenericType (context, inflated_targs);
- }
- return type;
- }
- // Nothing to do for non-generic type
- if (type.Arity == 0)
- return type;
- targs = new TypeSpec[type.Arity];
- //
- // Inflating using outside type arguments, var v = new Foo<int> (), class Foo<T> {}
- //
- if (type is InflatedTypeSpec) {
- for (; i < targs.Length; ++i)
- targs[i] = Inflate (type.TypeArguments[i]);
- type = type.GetDefinition ();
- } else {
- //
- // Inflating parent using inside type arguments, class Foo<T> { ITest<T> foo; }
- //
- var args = type.MemberDefinition.TypeParameters;
- foreach (var ds_tp in args)
- targs[i++] = Inflate (ds_tp);
- }
- return type.MakeGenericType (context, targs);
- }
- public TypeSpec Inflate (TypeParameterSpec tp)
- {
- for (int i = 0; i < tparams.Length; ++i)
- if (tparams [i] == tp)
- return targs[i];
- // This can happen when inflating nested types
- // without type arguments specified
- return tp;
- }
- }
- //
- // Before emitting any code we have to change all MVAR references to VAR
- // when the method is of generic type and has hoisted variables
- //
- public class TypeParameterMutator
- {
- readonly TypeParameters mvar;
- readonly TypeParameters var;
- readonly TypeParameterSpec[] src;
- Dictionary<TypeSpec, TypeSpec> mutated_typespec;
- public TypeParameterMutator (TypeParameters mvar, TypeParameters var)
- {
- if (mvar.Count != var.Count)
- throw new ArgumentException ();
- this.mvar = mvar;
- this.var = var;
- }
- public TypeParameterMutator (TypeParameterSpec[] srcVar, TypeParameters destVar)
- {
- if (srcVar.Length != destVar.Count)
- throw new ArgumentException ();
- this.src = srcVar;
- this.var = destVar;
- }
- #region Properties
- public TypeParameters MethodTypeParameters {
- get {
- return mvar;
- }
- }
- #endregion
- public static TypeSpec GetMemberDeclaringType (TypeSpec type)
- {
- if (type is InflatedTypeSpec) {
- if (type.DeclaringType == null)
- return type.GetDefinition ();
- var parent = GetMemberDeclaringType (type.DeclaringType);
- type = MemberCache.GetMember<TypeSpec> (parent, type);
- }
- return type;
- }
- public TypeSpec Mutate (TypeSpec ts)
- {
- TypeSpec value;
- if (mutated_typespec != null && mutated_typespec.TryGetValue (ts, out value))
- return value;
- value = ts.Mutate (this);
- if (mutated_typespec == null)
- mutated_typespec = new Dictionary<TypeSpec, TypeSpec> ();
- mutated_typespec.Add (ts, value);
- return value;
- }
- public TypeParameterSpec Mutate (TypeParameterSpec tp)
- {
- if (mvar != null) {
- for (int i = 0; i < mvar.Count; ++i) {
- if (mvar[i].Type == tp)
- return var[i].Type;
- }
- } else {
- for (int i = 0; i < src.Length; ++i) {
- if (src[i] == tp)
- return var[i].Type;
- }
- }
- return tp;
- }
- public TypeSpec[] Mutate (TypeSpec[] targs)
- {
- TypeSpec[] mutated = new TypeSpec[targs.Length];
- bool changed = false;
- for (int i = 0; i < targs.Length; ++i) {
- mutated[i] = Mutate (targs[i]);
- changed |= targs[i] != mutated[i];
- }
- return changed ? mutated : targs;
- }
- }
- /// <summary>
- /// A TypeExpr which already resolved to a type parameter.
- /// </summary>
- public class TypeParameterExpr : TypeExpression
- {
- public TypeParameterExpr (TypeParameter type_parameter, Location loc)
- : base (type_parameter.Type, loc)
- {
- this.eclass = ExprClass.TypeParameter;
- }
- }
- public class InflatedTypeSpec : TypeSpec
- {
- TypeSpec[] targs;
- TypeParameterSpec[] constraints;
- readonly TypeSpec open_type;
- readonly IModuleContext context;
- public InflatedTypeSpec (IModuleContext context, TypeSpec openType, TypeSpec declaringType, TypeSpec[] targs)
- : base (openType.Kind, declaringType, openType.MemberDefinition, null, openType.Modifiers)
- {
- if (targs == null)
- throw new ArgumentNullException ("targs");
- this.state &= ~SharedStateFlags;
- this.state |= (openType.state & SharedStateFlags);
- this.context = context;
- this.open_type = openType;
- this.targs = targs;
- foreach (var arg in targs) {
- if (arg.HasDynamicElement || arg.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
- state |= StateFlags.HasDynamicElement;
- break;
- }
- }
- if (open_type.Kind == MemberKind.MissingType)
- MemberCache = MemberCache.Empty;
- if ((open_type.Modifiers & Modifiers.COMPILER_GENERATED) != 0)
- state |= StateFlags.ConstraintsChecked;
- }
- #region Properties
- public override TypeSpec BaseType {
- get {
- if (cache == null || (state & StateFlags.PendingBaseTypeInflate) != 0)
- InitializeMemberCache (true);
- return base.BaseType;
- }
- }
- //
- // Inflated type parameters with constraints array, mapping with type arguments is based on index
- //
- public TypeParameterSpec[] Constraints {
- get {
- if (constraints == null) {
- constraints = TypeParameterSpec.InflateConstraints (MemberDefinition.TypeParameters, l => l.CreateLocalInflator (context), this);
- }
- return constraints;
- }
- }
- //
- // Used to cache expensive constraints validation on constructed types
- //
- public bool HasConstraintsChecked {
- get {
- return (state & StateFlags.ConstraintsChecked) != 0;
- }
- set {
- state = value ? state | StateFlags.ConstraintsChecked : state & ~StateFlags.ConstraintsChecked;
- }
- }
- public override IList<TypeSpec> Interfaces {
- get {
- if (cache == null)
- InitializeMemberCache (true);
- return base.Interfaces;
- }
- }
- public override bool IsExpressionTreeType {
- get {
- return (open_type.state & StateFlags.InflatedExpressionType) != 0;
- }
- }
- public override bool IsGenericIterateInterface {
- get {
- return (open_type.state & StateFlags.GenericIterateInterface) != 0;
- }
- }
- public override bool IsGenericTask {
- get {
- return (open_type.state & StateFlags.GenericTask) != 0;
- }
- }
- public override bool IsNullableType {
- get {
- return (open_type.state & StateFlags.InflatedNullableType) != 0;
- }
- }
- //
- // Types used to inflate the generic type
- //
- public override TypeSpec[] TypeArguments {
- get {
- return targs;
- }
- }
- #endregion
- public override bool AddInterface (TypeSpec iface)
- {
- var inflator = CreateLocalInflator (context);
- iface = inflator.Inflate (iface);
- if (iface == null)
- return false;
- return base.AddInterface (iface);
- }
- public static bool ContainsTypeParameter (TypeSpec type)
- {
- if (type.Kind == MemberKind.TypeParameter)
- return true;
- var element_container = type as ElementTypeSpec;
- if (element_container != null)
- return ContainsTypeParameter (element_container.Element);
- foreach (var t in type.TypeArguments) {
- if (ContainsTypeParameter (t)) {
- return true;
- }
- }
- return false;
- }
- TypeParameterInflator CreateLocalInflator (IModuleContext context)
- {
- TypeParameterSpec[] tparams_full;
- TypeSpec[] targs_full = targs;
- if (IsNested) {
- //
- // Special case is needed when we are inflating an open type (nested type definition)
- // on inflated parent. Consider following case
- //
- // Foo<T>.Bar<U> => Foo<string>.Bar<U>
- //
- // Any later inflation of Foo<string>.Bar<U> has to also inflate T if used inside Bar<U>
- //
- List<TypeSpec> merged_targs = null;
- List<TypeParameterSpec> merged_tparams = null;
- var type = DeclaringType;
- do {
- if (type.TypeArguments.Length > 0) {
- if (merged_targs == null) {
- merged_targs = new List<TypeSpec> ();
- merged_tparams = new List<TypeParameterSpec> ();
- if (targs.Length > 0) {
- merged_targs.AddRange (targs);
- merged_tparams.AddRange (open_type.MemberDefinition.TypeParameters);
- }
- }
- merged_tparams.AddRange (type.MemberDefinition.TypeParameters);
- merged_targs.AddRange (type.TypeArguments);
- }
- type = type.DeclaringType;
- } while (type != null);
- if (merged_targs != null) {
- // Type arguments are not in the right order but it should not matter in this case
- targs_full = merged_targs.ToArray ();
- tparams_full = merged_tparams.ToArray ();
- } else if (targs.Length == 0) {
- tparams_full = TypeParameterSpec.EmptyTypes;
- } else {
- tparams_full = open_type.MemberDefinition.TypeParameters;
- }
- } else if (targs.Length == 0) {
- tparams_full = TypeParameterSpec.EmptyTypes;
- } else {
- tparams_full = open_type.MemberDefinition.TypeParameters;
- }
- return new TypeParameterInflator (context, this, tparams_full, targs_full);
- }
- MetaType CreateMetaInfo (TypeParameterMutator mutator)
- {
- //
- // Converts nested type arguments into right order
- // Foo<string, bool>.Bar<int> => string, bool, int
- //
- var all = new List<MetaType> ();
- TypeSpec type = this;
- TypeSpec definition = type;
- do {
- if (type.GetDefinition().IsGeneric) {
- all.InsertRange (0,
- type.TypeArguments != TypeSpec.EmptyTypes ?
- type.TypeArguments.Select (l => l.GetMetaInfo ()) :
- type.MemberDefinition.TypeParameters.Select (l => l.GetMetaInfo ()));
- }
- definition = definition.GetDefinition ();
- type = type.DeclaringType;
- } while (type != null);
- return definition.GetMetaInfo ().MakeGenericType (all.ToArray ());
- }
- public override ObsoleteAttribute GetAttributeObsolete ()
- {
- return open_type.GetAttributeObsolete ();
- }
- protected override bool IsNotCLSCompliant (out bool attrValue)
- {
- if (base.IsNotCLSCompliant (out attrValue))
- return true;
- foreach (var ta in TypeArguments) {
- if (ta.MemberDefinition.CLSAttributeValue == false)
- return true;
- }
- return false;
- }
- public override TypeSpec GetDefinition ()
- {
- return open_type;
- }
- public override MetaType GetMetaInfo ()
- {
- if (info == null)
- info = CreateMetaInfo (null);
- return info;
- }
- public override string GetSignatureForError ()
- {
- if (IsNullableType)
- return targs[0].GetSignatureForError () + "?";
- return base.GetSignatureForError ();
- }
- protected override string GetTypeNameSignature ()
- {
- if (targs.Length == 0 || MemberDefinition is AnonymousTypeClass)
- return null;
- return "<" + TypeManager.CSharpName (targs) + ">";
- }
- public bool HasDynamicArgument ()
- {
- for (int i = 0; i < targs.Length; ++i) {
- var item = targs[i];
- if (item.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
- return true;
- if (item is InflatedTypeSpec) {
- if (((InflatedTypeSpec) item).HasDynamicArgument ())
- return true;
- continue;
- }
- if (item.IsArray) {
- while (item.IsArray) {
- item = ((ArrayContainer) item).Element;
- }
- if (item.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
- return true;
- }
- }
- return false;
- }
- protected override void InitializeMemberCache (bool onlyTypes)
- {
- if (cache == null) {
- var open_cache = onlyTypes ? open_type.MemberCacheTypes : open_type.MemberCache;
- // Surprisingly, calling MemberCache on open type could meantime create cache on this type
- // for imported type parameter constraints referencing nested type of this declaration
- if (cache == null)
- cache = new MemberCache (open_cache);
- }
- var inflator = CreateLocalInflator (context);
- //
- // Two stage inflate due to possible nested types recursive
- // references
- //
- // class A<T> {
- // B b;
- // class B {
- // T Value;
- // }
- // }
- //
- // When resolving type of `b' members of `B' cannot be
- // inflated because are not yet available in membercache
- //
- if ((state & StateFlags.PendingMemberCacheMembers) == 0) {
- open_type.MemberCacheTypes.InflateTypes (cache, inflator);
- //
- // Inflate any implemented interfaces
- //
- if (open_type.Interfaces != null) {
- ifaces = new List<TypeSpec> (open_type.Interfaces.Count);
- foreach (var iface in open_type.Interfaces) {
- var iface_inflated = inflator.Inflate (iface);
- if (iface_inflated == null)
- continue;
- base.AddInterface (iface_inflated);
- }
- }
- //
- // Handles the tricky case of recursive nested base generic type
- //
- // class A<T> : Base<A<T>.Nested> {
- // class Nested {}
- // }
- //
- // When inflating A<T>. base type is not yet known, secondary
- // inflation is required (not common case) once base scope
- // is known
- //
- if (open_type.BaseType == null) {
- if (IsClass)
- state |= StateFlags.PendingBaseTypeInflate;
- } else {
- BaseType = inflator.Inflate (open_type.BaseType);
- }
- } else if ((state & StateFlags.PendingBaseTypeInflate) != 0) {
- //
- // It can happen when resolving base type without being defined
- // which is not allowed to happen and will always lead to an error
- //
- // class B { class N {} }
- // class A<T> : A<B.N> {}
- //
- if (open_type.BaseType == null)
- return;
- BaseType = inflator.Inflate (open_type.BaseType);
- state &= ~StateFlags.PendingBaseTypeInflate;
- }
- if (onlyTypes) {
- sta…
Large files files are truncated, but you can click here to view the full file