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);
			}
		}
Exemplo n.º 2
0
		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 };
				}
			}
		}
Exemplo n.º 5
0
		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);
		}
Exemplo n.º 6
0
		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;
			}
		}
Exemplo n.º 7
0
 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;
     }
 }
Exemplo n.º 8
0
		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);
		}
Exemplo n.º 9
0
 public virtual IType AcceptVisitor(TypeVisitor visitor)
 {
     return visitor.VisitOtherType(this);
 }
Exemplo n.º 10
0
 // 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);
Exemplo n.º 11
0
 internal static IType Substitute(IType type, TypeVisitor substitution)
 {
     if (substitution == null)
         return type;
     else
         return type.AcceptVisitor(substitution);
 }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
		public override IType AcceptVisitor(TypeVisitor visitor)
		{
			return visitor.VisitTypeParameter(this);
		}
Exemplo n.º 15
0
 internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution)
     : base(declaringType, eventDefinition)
 {
     this.eventDefinition = eventDefinition;
     Initialize(substitution);
 }
Exemplo n.º 16
0
		internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution)
			: base(declaringType, eventDefinition)
		{
			this.eventDefinition = eventDefinition;
			Initialize(substitution);
		}
Exemplo n.º 17
0
 internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution, ITypeResolveContext context)
     : base(declaringType, eventDefinition, substitution, context)
 {
     this.eventDefinition = eventDefinition;
 }
Exemplo n.º 18
0
        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();
                    }
                }
            }
        }
Exemplo n.º 19
0
		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);
			}
		}
Exemplo n.º 20
0
 internal SpecializedField(IType declaringType, IField fieldDefinition, TypeVisitor substitution)
     : base(declaringType, fieldDefinition)
 {
     this.fieldDefinition = fieldDefinition;
     Initialize(substitution);
 }
Exemplo n.º 21
0
        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;
            }
        }
Exemplo n.º 22
0
 public bool Equals(IMember obj, TypeVisitor typeNormalization) =>
 this.Name == obj.Name &&
 this.DeclaringType.AcceptVisitor(typeNormalization).Equals(
     obj.DeclaringType.AcceptVisitor(typeNormalization));
Exemplo n.º 23
0
        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));
 }
Exemplo n.º 25
0
		public IType VisitChildren(TypeVisitor visitor)
		{
			return this;
		}
Exemplo n.º 26
0
		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);
			}
		}
Exemplo n.º 27
0
 internal SpecializedField(IType declaringType, IField fieldDefinition, TypeVisitor substitution, ITypeResolveContext context)
     : base(declaringType, fieldDefinition, substitution, context)
 {
     this.fieldDefinition = fieldDefinition;
 }
Exemplo n.º 28
0
 public ArrowTypeFlatbufferBuilder(FlatBufferBuilder builder)
 {
     _visitor = new TypeVisitor(builder);
 }
Exemplo n.º 29
0
			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;
			}
Exemplo n.º 30
0
 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)));
 }
Exemplo n.º 31
0
 protected virtual void Initialize(TypeVisitor substitution)
 {
     this.returnType = Substitute(memberDefinition.ReturnType, substitution);
 }
Exemplo n.º 32
0
 protected virtual void Initialize(TypeVisitor substitution)
 {
     this.returnType = Substitute(memberDefinition.ReturnType, substitution);
 }
Exemplo n.º 33
0
 /// <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)));
 }
Exemplo n.º 34
0
 public bool Equals(IMember obj, TypeVisitor typeNormalization) =>
 this.DeclaringTypeDefinition.AcceptVisitor(typeNormalization).Equals(
     obj.DeclaringTypeDefinition.AcceptVisitor(typeNormalization)) &&
 Name == Name;
Exemplo n.º 35
0
 bool IMember.Equals(IMember obj, TypeVisitor typeNormalization)
 {
     return(Equals(obj));
 }
Exemplo n.º 36
0
 public virtual IType VisitChildren(TypeVisitor visitor)
 {
     return this;
 }
Exemplo n.º 37
0
 IType IType.AcceptVisitor(TypeVisitor visitor)
 {
     return(visitor.VisitTypeDefinition(this));
 }
Exemplo n.º 38
0
		// 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);
Exemplo n.º 39
0
		internal SpecializedField(IType declaringType, IField fieldDefinition, TypeVisitor substitution, ITypeResolveContext context)
			: base(declaringType, fieldDefinition, substitution, context)
		{
			this.fieldDefinition = fieldDefinition;
		}
Exemplo n.º 40
0
 public override void visit(TypeVisitor v)
 {
     v.visit(this);
 }
Exemplo n.º 41
0
 ////////////////////////////////////////////////////////////////////
 public void visit(TypeVisitor v)
 {
     v.visit(this);
 }
Exemplo n.º 42
0
 public override IType AcceptVisitor(TypeVisitor visitor)
 {
     return(visitor.VisitNullabilityAnnotatedType(this));
 }
Exemplo n.º 43
0
 public virtual IType AcceptVisitor(TypeVisitor visitor)
 {
     return(visitor.VisitOtherType(this));
 }
Exemplo n.º 44
0
 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);
 }
Exemplo n.º 45
0
 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));
        }
Exemplo n.º 47
0
 internal SpecializedProperty(IType declaringType, IProperty propertyDefinition, TypeVisitor substitution)
     : base(declaringType, propertyDefinition)
 {
     this.propertyDefinition = propertyDefinition;
     Initialize(substitution);
 }
Exemplo n.º 48
0
 IType IType.VisitChildren(TypeVisitor visitor)
 {
     return(this);
 }
Exemplo n.º 49
0
 public abstract IType VisitChildren(TypeVisitor visitor);
 public IType VisitChildren(TypeVisitor visitor)
 {
     return(this);
 }
Exemplo n.º 51
0
 public abstract IType AcceptVisitor(TypeVisitor visitor);
Exemplo n.º 52
0
		internal SpecializedProperty(IType declaringType, IProperty propertyDefinition, TypeVisitor substitution)
			: base(declaringType, propertyDefinition)
		{
			this.propertyDefinition = propertyDefinition;
			Initialize(substitution);
		}
Exemplo n.º 53
0
		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);
			}
		}
Exemplo n.º 54
0
		internal SpecializedEvent(IType declaringType, IEvent eventDefinition, TypeVisitor substitution, ITypeResolveContext context)
			: base(declaringType, eventDefinition, substitution, context)
		{
			this.eventDefinition = eventDefinition;
		}
Exemplo n.º 55
0
		internal SpecializedProperty(IType declaringType, IProperty propertyDefinition, TypeVisitor substitution, ITypeResolveContext context)
			: base(declaringType, propertyDefinition, substitution, context)
		{
			this.propertyDefinition = propertyDefinition;
		}