public static ITypeReference Create(ITypeReference baseTypeReference, TypeVisitor substitution) { IType baseType = baseTypeReference as IType; if (baseType != null && substitution != null) { return baseType.AcceptVisitor(substitution); } else { return new SubstitutionTypeReference(baseTypeReference, substitution); } }
internal static ITypeReference Substitute(ITypeReference type, TypeVisitor substitution, ITypeResolveContext context) { if (substitution == null) return type; if (context != null) return type.Resolve(context).AcceptVisitor(substitution); else return SubstitutionTypeReference.Create(type, substitution); }
public SubstitutionTypeReference(ITypeReference baseTypeReference, TypeVisitor substitution) { if (baseTypeReference == null) throw new ArgumentNullException("baseTypeReference"); if (substitution == null) throw new ArgumentNullException("substitution"); this.baseTypeReference = baseTypeReference; this.substitution = substitution; }
/// <summary> /// Performs type substitution in parameter types and in the return type. /// </summary> public void SubstituteTypes(ITypeResolveContext context, TypeVisitor substitution) { this.ReturnType = this.ReturnType.Resolve(context).AcceptVisitor(substitution); var p = this.Parameters; for (int i = 0; i < p.Count; i++) { IType newType = p[i].Type.Resolve(context).AcceptVisitor(substitution); if (newType != p[i].Type) { p[i] = new DefaultParameter(p[i]) { Type = newType }; } } }
internal SpecializedMember(IType declaringType, IMember memberDefinition, TypeVisitor substitution, ITypeResolveContext context) { if (declaringType == null) throw new ArgumentNullException("declaringType"); if (memberDefinition == null) throw new ArgumentNullException("memberDefinition"); this.declaringType = declaringType; this.memberDefinition = memberDefinition; this.returnType = Substitute(memberDefinition.ReturnType, substitution, context); }
internal SpecializedMethod(IType declaringType, IMethod methodDefinition, IList<IType> typeArguments, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, methodDefinition, substitution, context) { this.methodDefinition = methodDefinition; if (typeArguments != null) { if (typeArguments.Count != methodDefinition.TypeParameters.Count) throw new ArgumentException("Number of type arguments does not match number of type parameters"); this.typeArguments = typeArguments; } else { this.typeArguments = EmptyList<IType>.Instance; } }
public static void Generate(string assemblyFile, string docdir) { if (assemblyFile == null) { throw new ArgumentNullException("assemblyFile"); } if (assemblyFile.Length == 0) { throw new ArgumentException("assemblyFile is empty."); } if (docdir == null) { throw new ArgumentNullException("docdir"); } if (docdir.Length == 0) { throw new ArgumentException("docdir is empty."); } var directory = Path.GetFullPath(docdir); assemblyFile = Path.GetFullPath(assemblyFile); Directory.SetCurrentDirectory(Path.GetDirectoryName(assemblyFile)); if (!File.Exists(assemblyFile)) { // Exit early, not found return; } var assembly = Assembly.LoadFrom(assemblyFile); Directory.CreateDirectory(directory); try { var members = DocReader.Read(assembly); var oldWriter = Console.Out; var visitor = new TypeVisitor(directory); members.Accept(visitor); visitor.Finish(); using ( var writer = new StreamWriter( Path.Combine(directory, "APIDocs.md"), false, Encoding.UTF8)) { var visitor2 = new SummaryVisitor(writer); members.Accept(visitor2); visitor2.Finish(); } } catch (IOException ex) { Console.WriteLine(ex.Message); return; } }
internal protected SpecializedMethod(IType declaringType, IMethod methodDefinition, IList<IType> typeArguments, TypeVisitor substitution) : base(declaringType, methodDefinition) { if (declaringType == null) throw new ArgumentNullException("declaringType"); if (methodDefinition == null) throw new ArgumentNullException("methodDefinition"); this.methodDefinition = methodDefinition; this.typeArguments = typeArguments ?? EmptyList<IType>.Instance; 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.Count; i++) { ITypeParameter tp = methodDefinition.TypeParameters[i]; if (ConstraintNeedsSpecialization(tp)) tp = new SpecializedTypeParameter(tp, this, substitution); specializedTypeParameters[i] = tp; } } if (typeArguments != null && typeArguments.Count > 0) { if (typeArguments.Count != methodDefinition.TypeParameters.Count) throw new ArgumentException("Incorrect number of type arguments"); } else if (specializedTypeParameters != null) { // No type arguments were specified, but the method is generic. // -> substitute original type parameters with the specialized ones substitution = GetSubstitution(declaringType, specializedTypeParameters.ToArray<IType>()); for (int i = 0; i < specializedTypeParameters.Count; i++) { if (ConstraintNeedsSpecialization(methodDefinition.TypeParameters[i])) { ((SpecializedTypeParameter)specializedTypeParameters[i]).substitution = substitution; } } } Initialize(substitution); }
public virtual IType AcceptVisitor(TypeVisitor visitor) { return visitor.VisitOtherType(this); }
// Force concrete implementations to override VisitChildren - the base implementation // in AbstractType assumes there are no children, but we know there is (at least) 1. public abstract override IType VisitChildren(TypeVisitor visitor);
internal static IType Substitute(IType type, TypeVisitor substitution) { if (substitution == null) return type; else return type.AcceptVisitor(substitution); }
protected override void Initialize(TypeVisitor substitution) { base.Initialize(substitution); var paramDefs = ((IParameterizedMember)this.MemberDefinition).Parameters; if (paramDefs.Count == 0) { this.parameters = EmptyList<IParameter>.Instance; } else { var parameters = new IParameter[paramDefs.Count]; for (int i = 0; i < parameters.Length; i++) { IType newType = Substitute(paramDefs[i].Type, substitution); if (newType != paramDefs[i].Type) { parameters[i] = new SpecializedParameter(paramDefs[i], newType); } else { parameters[i] = paramDefs[i]; } } this.parameters = Array.AsReadOnly(parameters); } }
internal protected SpecializedMethod(IType declaringType, IMethod methodDefinition, IList <IType> typeArguments, TypeVisitor substitution) : base(declaringType, methodDefinition) { if (declaringType == null) { throw new ArgumentNullException("declaringType"); } if (methodDefinition == null) { throw new ArgumentNullException("methodDefinition"); } this.methodDefinition = methodDefinition; this.typeArguments = typeArguments ?? EmptyList <IType> .Instance; 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.Count; i++) { ITypeParameter tp = methodDefinition.TypeParameters[i]; if (ConstraintNeedsSpecialization(tp)) { tp = new SpecializedTypeParameter(tp, this, substitution); } specializedTypeParameters[i] = tp; } } if (typeArguments != null && typeArguments.Count > 0) { if (typeArguments.Count != methodDefinition.TypeParameters.Count) { throw new ArgumentException("Incorrect number of type arguments"); } } else if (specializedTypeParameters != null) { // No type arguments were specified, but the method is generic. // -> substitute original type parameters with the specialized ones substitution = GetSubstitution(declaringType, specializedTypeParameters.ToArray <IType>()); for (int i = 0; i < specializedTypeParameters.Count; i++) { if (ConstraintNeedsSpecialization(methodDefinition.TypeParameters[i])) { ((SpecializedTypeParameter)specializedTypeParameters[i]).substitution = substitution; } } } Initialize(substitution); }
public override IType AcceptVisitor(TypeVisitor visitor) { return visitor.VisitTypeParameter(this); }
internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution) : base(declaringType, eventDefinition) { this.eventDefinition = eventDefinition; Initialize(substitution); }
internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution) : base(declaringType, eventDefinition) { this.eventDefinition = eventDefinition; Initialize(substitution); }
internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, eventDefinition, substitution, context) { this.eventDefinition = eventDefinition; }
public void TransformIL(TypeDefinition t) { TypeVisitor.ReplaceTypeRefs(t, x => { if (x.IsArray) { return(GetArrayType(GetArraySize(x))); } return(x); } ); foreach (var m in t.Methods) { if (!m.HasBody) { continue; } var ilp = m.Body.GetILProcessor(); for (int idx = 0; idx < m.Body.Instructions.Count; idx++) { var inst = m.Body.Instructions[idx]; if (IsLdelm(inst.OpCode)) { var at = GetArrayType(GetArraySize(inst.OpCode)); var getter = at.Methods.Single(x => x.Name == "Get"); ilp.Replace(inst, ilp.Create(OpCodes.Call, getter)); } if (IsStelm(inst.OpCode)) { var at = GetArrayType(GetArraySize(inst.OpCode)); var setter = at.Methods.Single(x => x.Name == "Set"); ilp.Replace(inst, ilp.Create(OpCodes.Call, setter)); } if (inst.OpCode == OpCodes.Ldlen) { // todo add Array_GetLength function var at = GetArrayType(EArrayType._1); var length = at.Methods.Single(x => x.Name == "GetLength"); ilp.Replace(inst, ilp.Create(OpCodes.Call, length)); } if (inst.OpCode == OpCodes.Newarr) { var elemType = (inst.Operand as TypeReference).Resolve(); var arrayType = GetArrayType(GetArraySize(elemType)); var newRef = arrayType.Methods.Single(x => x.Name == "new"); ilp.Replace(inst, ilp.Create(OpCodes.Call, newRef)); } if (inst.OpCode == OpCodes.Ldelema || inst.OpCode == OpCodes.Ldelem_Any || inst.OpCode == OpCodes.Stelem_Any) { throw new NotImplementedException(); } } } }
internal SpecializedParameterizedMember(IType declaringType, IParameterizedMember memberDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, memberDefinition, substitution, context) { var paramDefs = memberDefinition.Parameters; if (paramDefs.Count == 0) { this.parameters = EmptyList<IParameter>.Instance; } else { var parameters = new IParameter[paramDefs.Count]; for (int i = 0; i < parameters.Length; i++) { ITypeReference newType = Substitute(paramDefs[i].Type, substitution, context); if (newType != paramDefs[i].Type) { parameters[i] = new DefaultParameter(paramDefs[i]) { Type = newType }; } else { parameters[i] = paramDefs[i]; } } this.parameters = Array.AsReadOnly(parameters); } }
internal SpecializedField(IType declaringType, IField fieldDefinition, TypeVisitor substitution) : base(declaringType, fieldDefinition) { this.fieldDefinition = fieldDefinition; Initialize(substitution); }
internal SpecializedMethod(IType declaringType, IMethod methodDefinition, IList <IType> typeArguments, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, methodDefinition, substitution, context) { this.methodDefinition = methodDefinition; if (typeArguments != null) { if (typeArguments.Count != methodDefinition.TypeParameters.Count) { throw new ArgumentException("Number of type arguments does not match number of type parameters"); } this.typeArguments = typeArguments; } else { this.typeArguments = EmptyList <IType> .Instance; } }
public bool Equals(IMember obj, TypeVisitor typeNormalization) => this.Name == obj.Name && this.DeclaringType.AcceptVisitor(typeNormalization).Equals( obj.DeclaringType.AcceptVisitor(typeNormalization));
internal SpecializedParameterizedMember(IType declaringType, IParameterizedMember memberDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, memberDefinition, substitution, context) { var paramDefs = memberDefinition.Parameters; if (paramDefs.Count == 0) { this.parameters = EmptyList <IParameter> .Instance; } else { var parameters = new IParameter[paramDefs.Count]; for (int i = 0; i < parameters.Length; i++) { ITypeReference newType = Substitute(paramDefs[i].Type, substitution, context); if (newType != paramDefs[i].Type) { parameters[i] = new DefaultParameter(paramDefs[i]) { Type = newType }; } else { parameters[i] = paramDefs[i]; } } this.parameters = Array.AsReadOnly(parameters); } }
public IType AcceptVisitor(TypeVisitor visitor) { return(visitor.VisitTypeParameter(this)); }
public IType VisitChildren(TypeVisitor visitor) { return this; }
protected IList<IParameter> CreateParameters(TypeVisitor substitution) { var paramDefs = ((IParameterizedMember)this.baseMember).Parameters; if (paramDefs.Count == 0) { return EmptyList<IParameter>.Instance; } else { var parameters = new IParameter[paramDefs.Count]; for (int i = 0; i < parameters.Length; i++) { var p = paramDefs[i]; IType newType = p.Type.AcceptVisitor(substitution); parameters[i] = new DefaultParameter( newType, p.Name, this, p.Region, p.Attributes, p.IsRef, p.IsOut, p.IsParams, p.IsOptional, p.ConstantValue ); } return Array.AsReadOnly(parameters); } }
internal SpecializedField(IType declaringType, IField fieldDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, fieldDefinition, substitution, context) { this.fieldDefinition = fieldDefinition; }
public ArrowTypeFlatbufferBuilder(FlatBufferBuilder builder) { _visitor = new TypeVisitor(builder); }
public SpecializedTypeParameter(ITypeParameter baseTp, IMethod specializedOwner, TypeVisitor substitution) : base(specializedOwner, baseTp.Index, baseTp.Name, baseTp.Variance, baseTp.Attributes, baseTp.Region) { this.baseTp = baseTp; this.substitution = substitution; }
bool IMember.Equals(IMember obj, TypeVisitor typeNormalization) { return(obj is SyntheticRangeIndexAccessor g && this.underlyingMethod.Equals(g.underlyingMethod, typeNormalization) && this.indexOrRangeType.AcceptVisitor(typeNormalization).Equals(g.indexOrRangeType.AcceptVisitor(typeNormalization))); }
protected virtual void Initialize(TypeVisitor substitution) { this.returnType = Substitute(memberDefinition.ReturnType, substitution); }
protected virtual void Initialize(TypeVisitor substitution) { this.returnType = Substitute(memberDefinition.ReturnType, substitution); }
/// <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). /// May be null if no substitution should be used.</param> /// <returns>True if the constraints are satisfied; false otherwise.</returns> public static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution = null) { if (typeParameter == null) { throw new ArgumentNullException("typeParameter"); } if (typeArgument == null) { throw new ArgumentNullException("typeArgument"); } return(ValidateConstraints(typeParameter, typeArgument, substitution, CSharpConversions.Get(typeParameter.Owner.Compilation))); }
public bool Equals(IMember obj, TypeVisitor typeNormalization) => this.DeclaringTypeDefinition.AcceptVisitor(typeNormalization).Equals( obj.DeclaringTypeDefinition.AcceptVisitor(typeNormalization)) && Name == Name;
bool IMember.Equals(IMember obj, TypeVisitor typeNormalization) { return(Equals(obj)); }
public virtual IType VisitChildren(TypeVisitor visitor) { return this; }
IType IType.AcceptVisitor(TypeVisitor visitor) { return(visitor.VisitTypeDefinition(this)); }
// Force concrete implementations to override VisitChildren - the base implementation // in AbstractType assumes there are no children, but we know there is (at least) 1. public abstract override IType VisitChildren(TypeVisitor visitor);
internal SpecializedField(IType declaringType, IField fieldDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, fieldDefinition, substitution, context) { this.fieldDefinition = fieldDefinition; }
public override void visit(TypeVisitor v) { v.visit(this); }
//////////////////////////////////////////////////////////////////// public void visit(TypeVisitor v) { v.visit(this); }
public override IType AcceptVisitor(TypeVisitor visitor) { return(visitor.VisitNullabilityAnnotatedType(this)); }
public virtual IType AcceptVisitor(TypeVisitor visitor) { return(visitor.VisitOtherType(this)); }
internal static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution, CSharpConversions 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 IType VisitChildren(TypeVisitor visitor) => this;
/// <summary> /// Check that expressions are used on correct types. /// </summary> internal static AstData TypeCheck(AstData tree, SideeffectHelper notused) { var newTree = new TypeVisitor().Visit(tree.Tree.RootNode).OwningTree; return(new AstData(tree.TokenStream, tree.Filename, newTree)); }
internal SpecializedProperty(IType declaringType, IProperty propertyDefinition, TypeVisitor substitution) : base(declaringType, propertyDefinition) { this.propertyDefinition = propertyDefinition; Initialize(substitution); }
IType IType.VisitChildren(TypeVisitor visitor) { return(this); }
public abstract IType VisitChildren(TypeVisitor visitor);
public IType VisitChildren(TypeVisitor visitor) { return(this); }
public abstract IType AcceptVisitor(TypeVisitor visitor);
internal SpecializedProperty(IType declaringType, IProperty propertyDefinition, TypeVisitor substitution) : base(declaringType, propertyDefinition) { this.propertyDefinition = propertyDefinition; Initialize(substitution); }
protected IList<IParameter> CreateParameters(TypeVisitor substitution) { var paramDefs = ((IParameterizedMember)this.baseMember).Parameters; if (paramDefs.Count == 0) { return EmptyList<IParameter>.Instance; } else { var parameters = new IParameter[paramDefs.Count]; for (int i = 0; i < parameters.Length; i++) { IType newType = paramDefs[i].Type.AcceptVisitor(substitution); if (newType != paramDefs[i].Type) { parameters[i] = new SpecializedParameter(paramDefs[i], newType); } else { parameters[i] = paramDefs[i]; } } return Array.AsReadOnly(parameters); } }
internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, eventDefinition, substitution, context) { this.eventDefinition = eventDefinition; }
internal SpecializedProperty(IType declaringType, IProperty propertyDefinition, TypeVisitor substitution, ITypeResolveContext context) : base(declaringType, propertyDefinition, substitution, context) { this.propertyDefinition = propertyDefinition; }