public InstantiatedParameterType (ProjectDom dom, ITypeParameterMember typeParameterMember, ITypeParameter tp) { IType outerType = typeParameterMember as IType ?? typeParameterMember.DeclaringType; typeparam = tp; compilationUnit = outerType.CompilationUnit; ClassType = ClassType.Class; Modifiers = Modifiers.Public; Name = tp.Name; Namespace = outerType.DecoratedFullName; Location = outerType.Location; DeclaringType = outerType; if (tp.Constraints.Count > 0) ClassType = ClassType.Interface; foreach (IReturnType rt in tp.Constraints) { if (FindCyclicReference (new HashSet<ITypeParameter> () { tp }, outerType, ((DomReturnType)rt).DecoratedFullName)) continue; IType bt = dom.SearchType (typeParameterMember, rt); IReturnType resolvedType = rt; if (bt != null) { resolvedType = new DomReturnType (bt); if (bt.ClassType == ClassType.Interface || BaseType != null) { AddInterfaceImplementation (resolvedType); } else { ClassType = bt.ClassType; BaseType = resolvedType; } } else { AddInterfaceImplementation (resolvedType); } } if (BaseType == null) BaseType = DomReturnType.Object; }
public override IType VisitTypeParameter(ITypeParameter type) { // TODO: how to map type parameters? // It might have constraints, and those constraints might be mutually recursive. // Maybe reintroduce ITypeParameter.Owner? throw new NotImplementedException(); }
public TypeParameterCompletionData(ITypeParameter typeParameter) { TypeParameter = typeParameter; SetDefaultText(typeParameter.Name); DeclarationCategory = DeclarationCategory.Type_Parameter; //Documentation = typeParameter.GetDefinition().Documentation; }
public override IType VisitTypeParameter(ITypeParameter type) { if (type.OwnerType == SymbolKind.TypeDefinition) { return DummyTypeParameter.GetClassTypeParameter(type.Index); } else { return base.VisitTypeParameter(type); } }
public override IType VisitTypeParameter(ITypeParameter type) { if (type.OwnerType == SymbolKind.Method) { return DummyTypeParameter.GetMethodTypeParameter(type.Index); } else { return base.VisitTypeParameter(type); } }
ITypeParameter GetBaseTypeParameter() { ITypeParameter baseTP = this.baseTypeParameter; if (baseTP == null) { // ResolveBaseTypeParameter() is idempotent, so this is thread-safe. this.baseTypeParameter = baseTP = ResolveBaseTypeParameter((IMethod)this.Owner, this.Index); } return baseTP; }
static void AddConstraintsFromType(ITypeParameter tp, GenericParameter g) { foreach (TypeReference constraint in g.Constraints) { if (tp.Method != null) { tp.Constraints.Add(CreateType(tp.Class.ProjectContent, tp.Method, constraint)); } else { tp.Constraints.Add(CreateType(tp.Class.ProjectContent, tp.Class, constraint)); } } }
void CheckContstraints(IMethod omethod, ITypeParameter p1, ITypeParameter p2, ref AbiCompatibility compatibility) { if (p1.DirectBaseTypes.Count () != p2.DirectBaseTypes.Count () || p1.HasReferenceTypeConstraint != p2.HasReferenceTypeConstraint || p1.HasValueTypeConstraint != p2.HasValueTypeConstraint || p1.HasDefaultConstructorConstraint != p2.HasDefaultConstructorConstraint) { OnIncompatibilityFound (new AbiEventArgs (string.Format (TranslateString ("Type parameter constraints of method {0} have changed."), omethod.FullName))); compatibility = AbiCompatibility.Incompatible; } }
public BoundTypeParameter(ITypeParameter baseTypeParameter, IClass owningClass, IMethod owningMethod) { if (owningClass == null) throw new ArgumentNullException("owningClass"); if (baseTypeParameter == null) throw new ArgumentNullException("baseTypeParameter"); this.baseTypeParameter = baseTypeParameter; this.owningMethod = owningMethod; this.owningClass = owningClass; }
static bool CheckTypeParameterConstraints (IType type, IEnumerable<IType> baseTypes, ITypeParameter typeParameter) { if (!typeParameter.DirectBaseTypes.All (t => baseTypes.Any (t2 => t2.Equals (t)))) return false; if (typeParameter.HasDefaultConstructorConstraint && !type.GetConstructors (c => c.IsPublic && c.Parameters.Count == 0).Any ()) return false; return true; }
protected static bool FindUsage (BaseRefactoringContext context, SyntaxTree unit, ITypeParameter typaParameter, AstNode declaration) { var found = false; refFinder.FindTypeParameterReferences (typaParameter, context.UnresolvedFile, unit, context.Compilation, (node, resolveResult) => { found = found || node != declaration; }, context.CancellationToken); return found; }
public override IType VisitTypeParameter(ITypeParameter type) { int index = type.Index; if (type.OwnerType == EntityType.Method) { if (index >= 0 && index < typeArguments.Count) return typeArguments[index]; else return SharedTypes.UnknownType; } else { return base.VisitTypeParameter(type); } }
public override IType VisitTypeParameter(ITypeParameter type) { isOpen = true; // If both classes and methods, or different classes (nested types) // are involved, find the most specific one int newNestingLevel = GetNestingLevel(type.Owner); if (newNestingLevel > typeParameterOwnerNestingLevel) { typeParameterOwner = type.Owner; typeParameterOwnerNestingLevel = newNestingLevel; } return(base.VisitTypeParameter(type)); }
TP GetTPForType(IType v) { ITypeParameter p = v as ITypeParameter; if (p != null) { int index = p.Index; if (index < typeParameters.Length && typeParameters[index].TypeParameter == p) { return(typeParameters[index]); } } return(null); }
internal void AddConstraintsFromType(ITypeParameter tp, Type type) { foreach (Type constraint in type.GetGenericParameterConstraints()) { if (tp.Method != null) { tp.Constraints.Add(ReflectionReturnType.Create(tp.Method, constraint, false)); } else { tp.Constraints.Add(ReflectionReturnType.Create(tp.Class, constraint, false)); } } }
bool IdentityOrVarianceConversion(IType s, IType t, int subtypeCheckNestingDepth) { ITypeDefinition def = s.GetDefinition(); if (def != null && def.Equals(t.GetDefinition())) { ParameterizedType ps = s as ParameterizedType; ParameterizedType pt = t as ParameterizedType; if (ps != null && pt != null) { // C# 4.0 spec: §13.1.3.2 Variance Conversion for (int i = 0; i < def.TypeParameters.Count; i++) { IType si = ps.GetTypeArgument(i); IType ti = pt.GetTypeArgument(i); if (IdentityConversion(si, ti)) { continue; } ITypeParameter xi = def.TypeParameters[i]; switch (xi.Variance) { case VarianceModifier.Covariant: if (!ImplicitReferenceConversion(si, ti, subtypeCheckNestingDepth)) { return(false); } break; case VarianceModifier.Contravariant: if (!ImplicitReferenceConversion(ti, si, subtypeCheckNestingDepth)) { return(false); } break; default: return(false); } } } else if (ps != null || pt != null) { return(false); // only of of them is parameterized, or counts don't match? -> not valid conversion } return(true); } return(false); }
protected virtual ITypeParameter Visit(ITypeParameter type, T data) { TypeParameter tp = new TypeParameter(type.Name); tp.TypeParameterModifier = type.TypeParameterModifier; foreach (IAttribute attr in type.Attributes) { tp.AddAttribute((IAttribute)attr.AcceptVisitor(this, data)); } foreach (IReturnType rt in type.Constraints) { tp.AddConstraint((IReturnType)rt.AcceptVisitor(this, data)); } return(tp); }
public override IType VisitTypeParameter(ITypeParameter type) { if (type.OwnerType == SymbolKind.Method && ReplaceMethodTypeParametersWithDummy) { return(DummyTypeParameter.GetMethodTypeParameter(type.Index)); } else if (type.OwnerType == SymbolKind.TypeDefinition && ReplaceClassTypeParametersWithDummy) { return(DummyTypeParameter.GetClassTypeParameter(type.Index)); } else { return(base.VisitTypeParameter(type)); } }
protected static bool FindUsage(BaseRefactoringContext context, SyntaxTree unit, ITypeParameter typeParameter, AstNode declaration) { var found = false; var searchScopes = refFinder.GetSearchScopes(typeParameter); refFinder.FindReferencesInFile(searchScopes, context.Resolver, (node, resolveResult) => { if (node != declaration) { found = true; } }, context.CancellationToken); return(found); }
public static ITypeParameter[] Create(MetadataModule module, IEntity owner, GenericParameterHandleCollection handles) { if (handles.Count == 0) { return(Empty <ITypeParameter> .Array); } var tps = new ITypeParameter[handles.Count]; int i = 0; foreach (var handle in handles) { tps[i] = Create(module, owner, i, handle); i++; } return(tps); }
/// <summary> /// Validates whether the given type argument satisfies the constraints for the given type parameter. /// </summary> /// <param name="typeParameter">The type parameter.</param> /// <param name="typeArgument">The type argument.</param> /// <param name="substitution">The substitution that defines how type parameters are replaced with type arguments. /// The substitution is used to check constraints that depend on other type parameters (or recursively on the same type parameter).</param> /// <returns>True if the constraints are satisfied; false otherwise.</returns> public static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution) { if (typeParameter == null) { throw new ArgumentNullException("typeParameter"); } if (typeParameter.Owner == null) { throw new ArgumentNullException("typeParameter.Owner"); } if (typeArgument == null) { throw new ArgumentNullException("typeArgument"); } return(ValidateConstraints(typeParameter, typeArgument, substitution, Conversions.Get(typeParameter.Owner.Compilation))); }
static void CreateInstantiatedSubtypes(InstantiatedType result, IType curType, IList <IReturnType> genericArguments, GenericTypeInstanceResolver resolver) { foreach (IType innerType in curType.InnerTypes) { List <IReturnType> newArguments = new List <IReturnType> (); List <int> removeInheritedArguments = new List <int> (); for (int i = 0; i < innerType.TypeParameters.Count; i++) { ITypeParameter curParameter = innerType.TypeParameters [i]; bool found = false; for (int j = curType.TypeParameters.Count - 1; j >= 0; j--) { if (curType.TypeParameters [j].Name == curParameter.Name) { removeInheritedArguments.Add(newArguments.Count); newArguments.Add(genericArguments [j]); found = true; break; } } if (!found) { newArguments.Add(new DomReturnType(curParameter.Name)); } } InstantiatedType innerInstantiatedType = (InstantiatedType)CreateInstantiatedGenericTypeInternal(innerType, newArguments, resolver); for (int i = 0, j = 0; i < innerInstantiatedType.TypeParameters.Count && j < innerInstantiatedType.TypeParameters.Count; i++, j++) { if (curType.TypeParameters [i].Name == innerInstantiatedType.TypeParameters [j].Name) { innerInstantiatedType.typeParameters.RemoveAt(j); j--; } } result.Add(innerInstantiatedType); CreateInstantiatedSubtypes(innerInstantiatedType, innerType, newArguments, resolver); foreach (int i in removeInheritedArguments) { if (i >= 0 && i < newArguments.Count) { newArguments.RemoveAt(i); } } } }
static void AppendTypeName(StringBuilder b, IType type) { switch (type.Kind) { case TypeKind.Dynamic: b.Append("System.Object"); break; case TypeKind.TypeParameter: ITypeParameter tp = (ITypeParameter)type; b.Append('`'); if (tp.OwnerType == EntityType.Method) b.Append('`'); b.Append(tp.Index); break; case TypeKind.Array: ArrayType array = (ArrayType)type; AppendTypeName(b, array.ElementType); b.Append('['); if (array.Dimensions > 1) { for (int i = 0; i < array.Dimensions; i++) { if (i > 0) b.Append(','); b.Append("0:"); } } b.Append(']'); break; case TypeKind.Pointer: AppendTypeName(b, ((PointerType)type).ElementType); b.Append('*'); break; case TypeKind.ByReference: AppendTypeName(b, ((ByReferenceType)type).ElementType); b.Append('@'); break; default: IType declType = type.DeclaringType; if (declType != null) { AppendTypeName(b, declType); b.Append('.'); b.Append(type.Name); AppendTypeParameters(b, type, declType.TypeParameterCount); } else { b.Append(type.FullName); AppendTypeParameters(b, type, 0); } break; } }
protected virtual ITypeParameter Visit(ITypeParameter type, T data) { TypeParameter tp = new TypeParameter(type.Name); tp.ClassRequired = type.ClassRequired; tp.ValueTypeRequired = type.ValueTypeRequired; tp.ConstructorRequired = type.ConstructorRequired; foreach (IAttribute attr in type.Attributes) { tp.AddAttribute((IAttribute)attr.AcceptVisitor(this, data)); } foreach (IReturnType rt in type.Constraints) { tp.AddConstraint((IReturnType)rt.AcceptVisitor(this, data)); } return(tp); }
public override IType VisitTypeParameter(ITypeParameter type) { int index = type.Index; if (classTypeArguments != null && type.OwnerType == EntityType.TypeDefinition) { if (index >= 0 && index < classTypeArguments.Count) return classTypeArguments[index]; else return SharedTypes.UnknownType; } else if (methodTypeArguments != null && type.OwnerType == EntityType.Method) { if (index >= 0 && index < methodTypeArguments.Count) return methodTypeArguments[index]; else return SharedTypes.UnknownType; } else { return base.VisitTypeParameter(type); } }
public void Can_remove_type_params_from_class() { string csharpCode = @" public class Bar<T1, T2, T3> { public string FooBar(int bar1, string bar2) {} } "; IRoot root = RDom.CSharp.Load(csharpCode); RDomClass class1 = root.RootClasses.First() as RDomClass; ITypeParameter param = class1.TypeParameters.Skip(1).First(); Assert.AreEqual(3, class1.TypeParameters.Count()); class1.TypeParameters.Remove(param); Assert.AreEqual(2, class1.TypeParameters.Count()); Assert.AreEqual("T1", class1.TypeParameters.First().Name); Assert.AreEqual("T3", class1.TypeParameters.Last().Name); }
private static ITypeName GetName(this ITypeParameter typeParameter, ISubstitution substitution, IDictionary <DeclaredElementInstance, IName> seenElements) { string id; if (typeParameter.IsBound(substitution)) { var type = substitution[typeParameter]; var target = type.GetName(seenElements).Identifier; id = "{0} -> {1}".FormatEx(typeParameter.ShortName, target); } else { id = typeParameter.ShortName; } return(Names.Type(id)); }
public override IType VisitTypeParameter(ITypeParameter type) { if (type.OwnerType == SymbolKind.Method && ReplaceMethodTypeParametersWithDummy) { return(DummyTypeParameter.GetMethodTypeParameter(type.Index)); } else if (type.OwnerType == SymbolKind.TypeDefinition && ReplaceClassTypeParametersWithDummy) { return(DummyTypeParameter.GetClassTypeParameter(type.Index)); } else if (RemoveNullability && type is NullabilityAnnotatedTypeParameter natp) { return(natp.TypeWithoutAnnotation.AcceptVisitor(this)); } else { return(base.VisitTypeParameter(type)); } }
bool FindCyclicReference(HashSet <ITypeParameter> visited, IType outerType, string sourceParamName) { // Normalize the param name if (sourceParamName.StartsWith(((DomType)outerType).DecoratedFullName + ".")) { sourceParamName = sourceParamName.Substring(sourceParamName.LastIndexOf('.') + 1); } else if (sourceParamName.IndexOf('.') != -1) { return(false); } ITypeParameter targetParam = null; foreach (ITypeParameter tp in outerType.TypeParameters) { if (tp.Name == sourceParamName) { targetParam = tp; break; } } if (targetParam == null) { return(false); } if (!visited.Add(targetParam)) { return(true); } foreach (IReturnType rt in targetParam.Constraints) { if (FindCyclicReference(visited, outerType, ((DomReturnType)rt).DecoratedFullName)) { return(true); } } return(false); }
public static void Write(BinaryWriter writer, INameEncoder nameTable, ITypeParameter typeParameter) { WriteString(typeParameter.Name, writer, nameTable); // Flags byte f = 0; if (typeParameter.ClassRequired) { f |= 1; } if (typeParameter.ValueTypeRequired) { f |= 2; } if (typeParameter.ConstructorRequired) { f |= 4; } writer.Write(f); // Variance writer.Write((byte)typeParameter.Variance); // Constraints writer.Write(typeParameter.Constraints.Count()); foreach (IReturnType rt in typeParameter.Constraints) { Write(writer, nameTable, rt); } // Attributes writer.Write(typeParameter.Attributes.Count()); foreach (IAttribute attr in typeParameter.Attributes) { Write(writer, nameTable, attr); } }
public override IType VisitTypeParameter(ITypeParameter type) { int index = type.Index; if (type.ParentMethod != null) { if (index >= 0 && index < typeArguments.Count) { return(typeArguments[index]); } else { return(SharedTypes.UnknownType); } } else { return(base.VisitTypeParameter(type)); } }
public override IType VisitTypeParameter(ITypeParameter type) { int index = type.Index; if (type.OwnerType == EntityType.TypeDefinition) { if (index >= 0 && index < typeArguments.Length) { return(typeArguments[index]); } else { return(SharedTypes.UnknownType); } } else { return(base.VisitTypeParameter(type)); } }
private Dictionary <IMethod, ITypeParameter> UpdateDeclarations(IEnumerable <IMethod> methods) { int index = Method.Parameters.IndexOf(Parameter); var map = new Dictionary <IMethod, ITypeParameter>(); foreach (IMethod method in methods) { ITypeParameter parameter = null; foreach (IDeclaration declaration in method.GetDeclarations()) { // methods can have multiple declarations (partial methods). Exec[declaration.Language].RemoveParameter(declaration, index); parameter = Exec[declaration.Language].AddTypeParameter(declaration); } if (parameter != null) { map.Add(method, parameter); } } return(map); }
public static JsExpression ResolveTypeParameter(ITypeParameter tp, ITypeDefinition currentType, IMethod currentMethod, IMetadataImporter metadataImporter, IErrorReporter errorReporter, INamer namer) { bool unusable = false; switch (tp.OwnerType) { case EntityType.TypeDefinition: unusable = metadataImporter.GetTypeSemantics(currentType).IgnoreGenericArguments; break; case EntityType.Method: { var sem = metadataImporter.GetMethodSemantics(currentMethod); unusable = sem.Type != MethodScriptSemantics.ImplType.InlineCode && metadataImporter.GetMethodSemantics(currentMethod).IgnoreGenericArguments; break; } default: errorReporter.InternalError("Invalid owner " + tp.OwnerType + " for type parameter " + tp); return JsExpression.Null; } if (unusable) { errorReporter.Message(Messages._7536, tp.Name, tp.OwnerType == EntityType.TypeDefinition ? "type" : "method", tp.OwnerType == EntityType.TypeDefinition ? currentType.FullName : currentMethod.FullName); return JsExpression.Null; } return JsExpression.Identifier(namer.GetTypeParameterName(tp)); }
Constraint ConvertTypeParameterConstraint(ITypeParameter tp) { if (!tp.HasDefaultConstructorConstraint && !tp.HasReferenceTypeConstraint && !tp.HasValueTypeConstraint && tp.DirectBaseTypes.All(IsObjectOrValueType)) { return null; } Constraint c = new Constraint(); c.TypeParameter = new SimpleType (tp.Name); if (tp.HasReferenceTypeConstraint) { c.BaseTypes.Add(new PrimitiveType("class")); } else if (tp.HasValueTypeConstraint) { c.BaseTypes.Add(new PrimitiveType("struct")); } foreach (IType t in tp.DirectBaseTypes) { if (!IsObjectOrValueType(t)) c.BaseTypes.Add(ConvertType(t)); } if (tp.HasDefaultConstructorConstraint && !tp.HasValueTypeConstraint) { c.BaseTypes.Add(new PrimitiveType("new")); } return c; }
private IClassDeclaration GenerateGenericContractClassDeclaration(string contractClassName) { // This solution was found at CreateDerivedTypeAction.cs from decompiled R# SDK var baseTypeElement = _addContractForAvailability.DeclaredType.GetTypeElement(); Contract.Assert(baseTypeElement != null); string typeDeclaration = baseTypeElement.TypeParameters .AggregateString(",", (builder, parameter) => builder.Append(parameter.ShortName)); typeDeclaration = "<" + typeDeclaration + ">"; var classDeclaration = (IClassDeclaration)_factory.CreateTypeMemberDeclaration( "abstract class $0 " + typeDeclaration + " : $1" + typeDeclaration + " {}", new object[] { contractClassName, baseTypeElement }); var map = new Dictionary <ITypeParameter, IType>(); for (int i = 0; i < baseTypeElement.TypeParameters.Count; i++) { ITypeParameterOfTypeDeclaration declaration3 = classDeclaration.TypeParameters[i]; ITypeParameter key = baseTypeElement.TypeParameters[i]; map.Add(key, TypeFactory.CreateType(declaration3.DeclaredElement)); } ISubstitution substitution = EmptySubstitution.INSTANCE.Extend(map); for (int j = 0; j < baseTypeElement.TypeParameters.Count; j++) { ITypeParameter typeParameter = baseTypeElement.TypeParameters[j]; ITypeParameterOfTypeDeclaration declaration4 = classDeclaration.TypeParameters[j]; ITypeParameterConstraintsClause clause = _factory.CreateTypeParameterConstraintsClause(typeParameter, substitution, declaration4.DeclaredName); if (clause != null) { classDeclaration.AddTypeParameterConstraintsClauseBefore(clause, null); } } return(classDeclaration); }
private void AppendTypeParameters([NotNull] ITypeElement typeElement, [NotNull] ISubstitution substitution) { IList <ITypeParameter> typeParameters = typeElement.TypeParameters; int typeParameterCount = typeParameters.Count; if (typeParameterCount == 0) { return; } AppendText("<", VsHighlightingAttributeIds.Operator); for (int i = 0; i < typeParameterCount; ++i) { if (i > 0) { AppendText(",", null); } ITypeParameter typeParameter = typeParameters[i]; AppendType(substitution.Apply(typeParameter), NamespaceDisplays.TypeParameters); } AppendText(">", VsHighlightingAttributeIds.Operator); }
private static bool IsBound(this ITypeParameter typeParameter, ISubstitution substitution) { if (!substitution.Domain.Contains(typeParameter)) { return(false); } if (substitution.IsId()) { return(false); } var targetType = substitution[typeParameter]; var targetTypeParameter = targetType.GetTypeElement <ITypeParameter>(); if (targetTypeParameter == null) { return(true); } var o1 = typeParameter.Owner; var o2 = targetTypeParameter.Owner; return(!o1.Equals(o2)); }
private void AppendTypeParameters([NotNull] ITypeElement typeElement, [NotNull] ISubstitution substitution, Context context) { IList <ITypeParameter> typeParameters = typeElement.TypeParameters; int typeParameterCount = typeParameters.Count; if (typeParameterCount == 0) { return; } AppendText("<", _highlighterIdProvider.Operator); for (int i = 0; i < typeParameterCount; ++i) { if (i > 0) { AppendText(",", null); } ITypeParameter typeParameter = typeParameters[i]; AppendTypeWithoutModule(substitution.Apply(typeParameter), QualifierDisplays.TypeParameters, context); } AppendText(">", _highlighterIdProvider.Operator); }
static ITypeParameter GetTypeParameter(ref ITypeParameter[] typeParameters, SymbolKind symbolKind, int index) { ITypeParameter[] tps = typeParameters; while (index >= tps.Length) { // We don't have a normal type parameter for this index, so we need to extend our array. // Because the array can be used concurrently from multiple threads, we have to use // Interlocked.CompareExchange. ITypeParameter[] newTps = new ITypeParameter[index + 1]; tps.CopyTo(newTps, 0); for (int i = tps.Length; i < newTps.Length; i++) { newTps[i] = new DummyTypeParameter(symbolKind, i); } ITypeParameter[] oldTps = Interlocked.CompareExchange(ref typeParameters, newTps, tps); if (oldTps == tps) { // exchange successful tps = newTps; } else { // exchange not successful tps = oldTps; } } return tps[index]; }
public SpecializedMethod(IMethod methodDefinition, TypeParameterSubstitution substitution) : base(methodDefinition) { // The base ctor might have unpacked a SpecializedMember // (in case we are specializing an already-specialized method) methodDefinition = (IMethod)base.MemberDefinition; this.methodDefinition = methodDefinition; if (methodDefinition.TypeParameters.Any(ConstraintNeedsSpecialization)) { // The method is generic, and we need to specialize the type parameters specializedTypeParameters = new ITypeParameter[methodDefinition.TypeParameters.Count]; for (int i = 0; i < specializedTypeParameters.Length; i++) { ITypeParameter tp = methodDefinition.TypeParameters[i]; if (ConstraintNeedsSpecialization(tp)) { tp = new SpecializedTypeParameter(tp, this); } specializedTypeParameters[i] = tp; } // add substitution that replaces the base method's type parameters with our specialized version AddSubstitution(new TypeParameterSubstitution(null, specializedTypeParameters)); } // Add the main substitution after the method type parameter specialization. AddSubstitution(substitution); if (specializedTypeParameters != null) { // Set the substitution on the type parameters to the final composed substitution foreach (var tp in specializedTypeParameters.OfType <SpecializedTypeParameter>()) { if (tp.Owner == this) { tp.substitution = base.Substitution; } } } }
internal static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution, Conversions conversions) { switch (typeArgument.Kind) { // void, null, and pointers cannot be used as type arguments case TypeKind.Void: case TypeKind.Null: case TypeKind.Pointer: return false; } if (typeParameter.HasReferenceTypeConstraint) { if (typeArgument.IsReferenceType != true) return false; } if (typeParameter.HasValueTypeConstraint) { if (!NullableType.IsNonNullableValueType(typeArgument)) return false; } if (typeParameter.HasDefaultConstructorConstraint) { ITypeDefinition def = typeArgument.GetDefinition(); if (def != null && def.IsAbstract) return false; var ctors = typeArgument.GetConstructors( m => m.Parameters.Count == 0 && m.Accessibility == Accessibility.Public, GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions ); if (!ctors.Any()) return false; } foreach (IType constraintType in typeParameter.DirectBaseTypes) { IType c = constraintType; if (substitution != null) c = c.AcceptVisitor(substitution); if (!conversions.IsConstraintConvertible(typeArgument, c)) return false; } return true; }
public static ITypeParameter[] Create(MetadataModule module, ITypeDefinition copyFromOuter, IEntity owner, GenericParameterHandleCollection handles) { if (handles.Count == 0) { return(Empty <ITypeParameter> .Array); } var outerTps = copyFromOuter.TypeParameters; var tps = new ITypeParameter[handles.Count]; int i = 0; foreach (var handle in handles) { if (i < outerTps.Count) { tps[i] = outerTps[i]; } else { tps[i] = Create(module, owner, i, handle); } i++; } return(tps); }
string ConvertTypeParameter(ITypeParameter tp) { if (tp.BoundTo != null) return Convert(tp.BoundTo); else return tp.Name; }
public override IType VisitTypeParameter(ITypeParameter type) { isOpen = true; return base.VisitTypeParameter(type); }
public override IType VisitTypeParameter(ITypeParameter type) { isOpen = true; // If both classes and methods, or different classes (nested types) // are involved, find the most specific one int newNestingLevel = GetNestingLevel(type.Owner); if (newNestingLevel > typeParameterOwnerNestingLevel) { typeParameterOwner = type.Owner; typeParameterOwnerNestingLevel = newNestingLevel; } return base.VisitTypeParameter(type); }
private StaticGenericParameterWrapper MakeGenericParameterType(ITypeParameter typeParameterHandle) { ITypeElement declaringTypeHandle = typeParameterHandle.OwnerType; if (declaringTypeHandle != null) { return StaticGenericParameterWrapper.CreateGenericTypeParameter(this, typeParameterHandle, MakeDeclaredTypeWithoutSubstitution(declaringTypeHandle)); } else { return StaticGenericParameterWrapper.CreateGenericMethodParameter(this, typeParameterHandle, Wrap(typeParameterHandle.OwnerMethod)); } }
JsExpression IRuntimeContext.ResolveTypeParameter(ITypeParameter tp) { return Utils.ResolveTypeParameter(tp, _typeBeingCompiled, _methodBeingCompiled, _metadataImporter, _errorReporter, _namer); }
Constraint ConvertTypeParameterConstraint(ITypeParameter tp) { if (!tp.HasDefaultConstructorConstraint && !tp.HasReferenceTypeConstraint && !tp.HasValueTypeConstraint && tp.DirectBaseTypes.All(IsObjectOrValueType)) { return null; } Constraint c = new Constraint(); c.TypeParameter = new SimpleType (tp.Name); if (tp.HasReferenceTypeConstraint) { c.BaseTypes.Add(new PrimitiveType("class")); } else if (tp.HasValueTypeConstraint) { c.BaseTypes.Add(new PrimitiveType("struct")); } foreach (IType t in tp.DirectBaseTypes) { if (!IsObjectOrValueType(t)) c.BaseTypes.Add(ConvertType(t)); } if (tp.HasDefaultConstructorConstraint) { c.BaseTypes.Add(new PrimitiveType("new")); } return c; }
TypeParameterDeclaration ConvertTypeParameter(ITypeParameter tp) { TypeParameterDeclaration decl = new TypeParameterDeclaration(); decl.Variance = tp.Variance; decl.Name = tp.Name; return decl; }
ICompletionData ICompletionDataFactory.CreateVariableCompletionData(ITypeParameter parameter) { return new CompletionData(parameter.Name); }
public ICompletionData CreateVariableCompletionData(ITypeParameter parameter) { return new CompletionData(parameter.Name); }
public ICompletionData CreateVariableCompletionData(ITypeParameter parameter) { return(new CompletionData(parameter.Name)); }
public virtual IType VisitTypeParameter(ITypeParameter type) { return type.VisitChildren(this); }
public SpecializedTypeParameter(ITypeParameter baseTp, IMethod specializedOwner) : base(specializedOwner, baseTp.Index, baseTp.Name, baseTp.Variance, baseTp.Attributes, baseTp.Region) { // We don't have to consider already-specialized baseTps because // we read the baseTp directly from the unpacked memberDefinition. this.baseTp = baseTp; }
public FindTypeParameterReferencesNavigator(ITypeParameter typeParameter) { this.typeParameter = typeParameter; }
public override IType VisitTypeParameter(ITypeParameter type) { return NullableType.Create(compilation, type); }
/// <summary> /// Validates whether the given type argument satisfies the constraints for the given type parameter. /// </summary> /// <param name="typeParameter">The type parameter.</param> /// <param name="typeArgument">The type argument.</param> /// <param name="substitution">The substitution that defines how type parameters are replaced with type arguments. /// The substitution is used to check constraints that depend on other type parameters (or recursively on the same type parameter).</param> /// <returns>True if the constraints are satisfied; false otherwise.</returns> public static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution) { if (typeParameter == null) throw new ArgumentNullException("typeParameter"); if (typeParameter.Owner == null) throw new ArgumentNullException("typeParameter.Owner"); if (typeArgument == null) throw new ArgumentNullException("typeArgument"); return ValidateConstraints(typeParameter, typeArgument, substitution, Conversions.Get(typeParameter.Owner.Compilation)); }