Пример #1
0
        public AstParameterDefinition(string name, IAstTypeReference type)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);

            this.Name = name;
            this.Type = type;
        }
Пример #2
0
        private IAstTypeReference AdaptReferencedType(IAstTypeReference type)
        {
            var typeAsGeneric = type as AstGenericTypeWithArguments;

            if (typeAsGeneric == null)
            {
                return(type);
            }

            var arguments  = typeAsGeneric.TypeArguments.ToArray();
            var parameters = this.PrimaryType.GetTypeParameters().Select((p, index) => new { p.Name, index }).ToDictionary(p => p.Name, p => p.index);

            for (var i = 0; i < arguments.Length; i++)
            {
                var placeholder = arguments[i] as AstGenericPlaceholderType;
                if (placeholder == null)
                {
                    continue;
                }

                arguments[i] = this.TypeArguments[parameters[placeholder.Name]];
            }

            return(new AstGenericTypeWithArguments(typeAsGeneric.PrimaryType, arguments));
        }
Пример #3
0
        public IAstTypeReference RemapArgumentTypes(IAstTypeReference type, Func <IAstTypeReference, IAstTypeReference> remap)
        {
            if (type is AstGenericPlaceholderType)
            {
                return(remap(type));
            }

            var function = type as AstSpecifiedFunctionType;

            if (function != null)
            {
                return(RemapArgumentTypesForFunction(function, remap));
            }

            var generic = type as AstGenericTypeWithArguments;

            if (generic != null)
            {
                return(RemapArgumentTypesForGeneric(generic, remap));
            }

            var parameters = type.GetTypeParameters().ToArray();

            if (parameters.Any())
            {
                return(RemapArgumentTypesFromParameters(type, parameters, remap));
            }

            return(type);
        }
Пример #4
0
 protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     foreach (var child in base.VisitOrTransformChildren(transform)) {
         yield return child;
     }
     yield return this.ReturnType = (IAstTypeReference)transform(this.ReturnType);
 }
Пример #5
0
 public AstFunctionDefinition(string name, IEnumerable<AstParameterDefinition> parameters, IEnumerable<IAstStatement> body, IAstTypeReference returnType)
     : base(parameters, body)
 {
     Argument.RequireNotNullAndNotEmpty("name", name);
     this.Name = name;
     this.fixedReturnType = returnType;
 }
Пример #6
0
 public AstFunctionDefinition(string name, IEnumerable <AstParameterDefinition> parameters, IEnumerable <IAstStatement> body, IAstTypeReference returnType)
     : base(parameters, body)
 {
     Argument.RequireNotNullAndNotEmpty("name", name);
     this.Name            = name;
     this.fixedReturnType = returnType;
 }
Пример #7
0
        public AstParameterDefinition(string name, IAstTypeReference type)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);

            this.Name = name;
            this.Type = type;
        }
Пример #8
0
 protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     if (this.AssignedValue != null)
     {
         yield return(this.AssignedValue = (IAstExpression)transform(this.AssignedValue));
     }
     yield return(this.Type = (IAstTypeReference)transform(this.Type));
 }
Пример #9
0
        public AstPropertyDefinition(string name, IAstTypeReference type, IAstExpression assignedValue)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);

            this.Name          = name;
            this.Type          = type;
            this.AssignedValue = assignedValue;
        }
Пример #10
0
        public string Format(IAstTypeReference type)
        {
            var reflected = type as AstReflectedType;
            if (reflected != null)
                return Format(reflected.ActualType);

            return null;
        }
Пример #11
0
        public AstPropertyDefinition(string name, IAstTypeReference type, IAstExpression assignedValue)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);

            this.Name = name;
            this.Type = type;
            this.AssignedValue = assignedValue;
        }
Пример #12
0
 protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     foreach (var parameterType in this.ParameterTypes.Transform(transform))
     {
         yield return(parameterType);
     }
     yield return(this.ReturnType = (IAstTypeReference)transform(this.ReturnType));
 }
Пример #13
0
        public AstGenericTypeWithArguments(IAstTypeReference primaryType, IEnumerable<IAstTypeReference> typeArguments)
        {
            Argument.RequireNotNull("typeArguments", typeArguments);

            this.PrimaryType = primaryType;
            this.TypeArguments = typeArguments.ToArray();

            this.baseType = new Lazy<IAstTypeReference>(() => AdaptReferencedType(this.PrimaryType.BaseType));
        }
Пример #14
0
 public AstFunctionReferenceExpression(IAstElement target, IAstMethodReference reference)
 {
     this.Target         = target;
     this.Function       = reference;
     this.ExpressionType = new AstInferredFunctionType(
         () => this.Function.ParameterTypes,
         () => this.Function.ReturnType
         );
 }
Пример #15
0
        public AstGenericTypeWithArguments(IAstTypeReference primaryType, IEnumerable <IAstTypeReference> typeArguments)
        {
            Argument.RequireNotNull("typeArguments", typeArguments);

            this.PrimaryType   = primaryType;
            this.TypeArguments = typeArguments.ToArray();

            this.baseType = new Lazy <IAstTypeReference>(() => AdaptReferencedType(this.PrimaryType.BaseType));
        }
Пример #16
0
        public AstNewExpression(IAstTypeReference type, IEnumerable<IAstExpression> arguments, IAstElement initializer)
        {
            var argumentList = arguments.ToList();
            Argument.RequireNotNullAndNotContainsNull("arguments", argumentList);

            this.Type = type;
            this.Arguments = argumentList;
            this.Initializer = initializer;
        }
Пример #17
0
        public AstNewExpression(IAstTypeReference type, IEnumerable <IAstExpression> arguments, IAstElement initializer)
        {
            var argumentList = arguments.ToList();

            Argument.RequireNotNullAndNotContainsNull("arguments", argumentList);

            this.Type        = type;
            this.Arguments   = argumentList;
            this.Initializer = initializer;
        }
Пример #18
0
 private static Tuple<IList<IAstTypeReference>, bool> RemapAll(IList<IAstTypeReference> types, Func<IAstTypeReference, IAstTypeReference> remap)
 {
     var changed = false;
     var newTypes = new IAstTypeReference[types.Count];
     for (var i = 0; i < types.Count; i++) {
         newTypes[i] = remap(types[i]);
         changed = changed || newTypes[i] != types[i];
     }
     return Tuple.Create((IList<IAstTypeReference>)newTypes, changed);
 }
Пример #19
0
        public string Format(IAstTypeReference type)
        {
            var reflected = type as AstReflectedType;

            if (reflected != null)
            {
                return(Format(reflected.ActualType));
            }

            return(null);
        }
Пример #20
0
 protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     foreach (var child in base.VisitOrTransformChildren(transform))
     {
         yield return(child);
     }
     if (this.fixedReturnType != null)
     {
         yield return(this.fixedReturnType = (IAstTypeReference)transform(this.fixedReturnType));
     }
 }
Пример #21
0
        public IAstMemberReference Resolve(IAstTypeReference declaringType, string name, INameSource scope)
        {
            var resolved = declaringType.ResolveMember(name);
            if (resolved == null)
                resolved = ResolveMemberFromScope(name, scope);

            if (resolved == null)
                throw new NotImplementedException("MemberResolver: Failed to resolve " + name);

            return resolved;
        }
Пример #22
0
        public static IEnumerable <IAstTypeReference> GetAncestors(this IAstTypeReference type)
        {
            var @base = type.BaseType;

            while (@base != null)
            {
                yield return(@base);

                @base = @base.BaseType;
            }
        }
Пример #23
0
        public AstBuiltInOperator(string name, IAstTypeReference operandType, IAstTypeReference resultType)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);
            Argument.RequireNotNull("operandType", operandType);
            Argument.RequireNotNull("resultType", resultType);

            this.Name        = name;
            this.OperandType = operandType;
            this.ReturnType  = resultType;

            this.parameterTypes = new[] { this.OperandType, this.OperandType }.AsReadOnly();
        }
Пример #24
0
        private static Tuple <IList <IAstTypeReference>, bool> RemapAll(IList <IAstTypeReference> types, Func <IAstTypeReference, IAstTypeReference> remap)
        {
            var changed  = false;
            var newTypes = new IAstTypeReference[types.Count];

            for (var i = 0; i < types.Count; i++)
            {
                newTypes[i] = remap(types[i]);
                changed     = changed || newTypes[i] != types[i];
            }
            return(Tuple.Create((IList <IAstTypeReference>)newTypes, changed));
        }
Пример #25
0
        public AstBuiltInOperator(string name, IAstTypeReference operandType, IAstTypeReference resultType)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);
            Argument.RequireNotNull("operandType", operandType);
            Argument.RequireNotNull("resultType", resultType);

            this.Name = name;
            this.OperandType = operandType;
            this.ReturnType = resultType;

            this.parameterTypes = new[] {this.OperandType, this.OperandType}.AsReadOnly();
        }
Пример #26
0
        public AstLambdaExpression(IEnumerable <AstParameterDefinition> parameters, IAstElement body)
        {
            var parametersList = parameters.ToList();

            Argument.RequireNotNullAndNotContainsNull("parameters", parametersList);

            Parameters     = parametersList;
            Body           = body;
            ExpressionType = new AstInferredFunctionType(
                () => this.Parameters.Select(p => p.Type),
                () => this.ReturnType
                );
        }
Пример #27
0
        private void AddGenericTheory(Dictionary<IAstTypeReference, ISet<IAstTypeReference>> genericTheories, IAstTypeReference genericType, IAstTypeReference argumentType)
        {
            if (argumentType is AstGenericPlaceholderType || argumentType.IsImplicit())
                return;

            var set = genericTheories.GetValueOrDefault(genericType);
            if (set == null) {
                genericTheories.Add(genericType, new HashSet<IAstTypeReference> { argumentType });
                return;
            }

            set.Add(argumentType);
        }
        private IAstMethodReference CoerceToType(IAstMethodReference function, IAstFunctionTypeReference functionType)
        {
            var genericParameterTypes      = function.GetGenericParameterTypes().ToArray();
            var genericArgumentTypes       = new IAstTypeReference[genericParameterTypes.Length];
            var functionTypeParameterTypes = functionType.GetParameterTypes().ToArray();
            var functionParameterTypes     = (function.ParameterTypes as IList <IAstTypeReference>) ?? function.ParameterTypes.ToArray();

            for (var i = 0; i < genericArgumentTypes.Length; i++)
            {
                var parameterIndex = functionParameterTypes.IndexOf(genericParameterTypes[i]);
                genericArgumentTypes[i] = functionTypeParameterTypes[parameterIndex];
            }

            return(new AstGenericMethodWithTypeArguments(function, genericArgumentTypes, this.genericHelper));
        }
Пример #29
0
        public IAstMemberReference Resolve(IAstTypeReference declaringType, string name, INameSource scope)
        {
            var resolved = declaringType.ResolveMember(name);

            if (resolved == null)
            {
                resolved = ResolveMemberFromScope(name, scope);
            }

            if (resolved == null)
            {
                throw new NotImplementedException("MemberResolver: Failed to resolve " + name);
            }

            return(resolved);
        }
Пример #30
0
        public IAstTypeReference BuildType(IEnumerable<IAstTypeReference> parameterTypes, IAstTypeReference returnType)
        {
            var types = new List<IAstTypeReference>(parameterTypes);
            var delegateTypeName = "Action";
            if (!(returnType is AstVoidType)) {
                delegateTypeName = "Func";
                types.Add(returnType);
            }
            delegateTypeName += "`" + types.Count;

            var delegateType = Type.GetType("System." + delegateTypeName, true).MakeGenericType(
                types.Cast<AstReflectedType>().Select(t => t.ActualType).ToArray()
            );

            return new AstReflectedType(delegateType);
        }
Пример #31
0
        public IAstTypeReference RemapArgumentTypes(IAstTypeReference type, Func<IAstTypeReference, IAstTypeReference> remap)
        {
            if (type is AstGenericPlaceholderType)
                return remap(type);

            var function = type as AstSpecifiedFunctionType;
            if (function != null)
                return RemapArgumentTypesForFunction(function, remap);

            var generic = type as AstGenericTypeWithArguments;
            if (generic != null)
                return RemapArgumentTypesForGeneric(generic, remap);

            var parameters = type.GetTypeParameters().ToArray();
            if (parameters.Any())
                return RemapArgumentTypesFromParameters(type, parameters, remap);

            return type;
        }
Пример #32
0
        private bool CanMatchPotentialGeneric(
            IAstTypeReference parameterType,
            IAstTypeReference argumentType,
            Func<IDictionary<IAstTypeReference, int>> getArgumentCompatibleTypes,
            Dictionary<IAstTypeReference, ISet<IAstTypeReference>> genericTheories
            )
        {
            if (parameterType is AstGenericPlaceholderType) {
                AddGenericTheory(genericTheories, parameterType, argumentType);
                return true;
            }

            var parameterTypeDeconstructed = DeconstructIfHasTypeArguments(parameterType);
            if (parameterTypeDeconstructed == null)
                return false;

            var parameterTypeArguments = parameterTypeDeconstructed.TypeArguments;
            var anyMatchesFound = false;
            foreach (var type in getArgumentCompatibleTypes().Keys) {
                if (!parameterTypeDeconstructed.Matches(type))
                    continue;

                var typeArguments = DeconstructIfHasTypeArguments(type).TypeArguments;
                for (var i = 0; i < typeArguments.Count; i++) {
                    if (Equals(typeArguments[i], parameterTypeArguments[i]))
                        continue;

                    if (!CanMatchPotentialGeneric(parameterTypeArguments[i], typeArguments[i], () => GetCompatibleTypes(typeArguments[i]), genericTheories))
                        return false;

                    anyMatchesFound = true;
                }
            }

            return anyMatchesFound;
        }
        private IAstTypeReference ApplyArgumentTypes(GenericTypeHelper genericHelper, IAstTypeReference type, IAstTypeReference[] genericParameterTypes)
        {
            return genericHelper.RemapArgumentTypes(type, t => {
                var parameterIndex = genericParameterTypes.IndexOf(t);
                if (parameterIndex < 0)
                    return t;

                return this.GenericArgumentTypes[parameterIndex];
            });
        }
Пример #34
0
 protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     if (this.AssignedValue != null)
         yield return this.AssignedValue = (IAstExpression)transform(this.AssignedValue);
     yield return this.Type = (IAstTypeReference)transform(this.Type);
 }
Пример #35
0
        private Tuple<IAstMethodReference, int> GetOrMakeMatch(IAstMethodReference method, IEnumerable<IAstTypeReference> parameterTypes, IEnumerable<IAstTypeReference> argumentTypes)
        {
            var genericTheories = new Dictionary<IAstTypeReference, ISet<IAstTypeReference>>();
            var distance = GetDistanceForAll(method, parameterTypes, argumentTypes, genericTheories);
            if (distance == -1)
                return null;

            if (method.IsGeneric) {
                var genericParameters = method.GetGenericParameterTypes().ToArray();
                var genericArguments = new IAstTypeReference[genericParameters.Length];
                for (var i = 0; i < genericParameters.Length; i++) {
                    var theories = genericTheories.GetValueOrDefault(genericParameters[i]);
                    if (theories == null)
                        return null;

                    var aggregate = ReconcileGenericTheories(theories, method);
                    if (aggregate == null)
                        return null;

                    genericArguments[i] = aggregate;
                }

                var generic = new AstGenericMethodWithTypeArguments(method, genericArguments, this.genericHelper);
                return Tuple.Create((IAstMethodReference)generic, distance);
            }

            return Tuple.Create(method, distance);
        }
Пример #36
0
 private IAstTypeReference RemapArgumentTypesFromParameters(IAstTypeReference type, IAstTypeReference[] parameters, Func<IAstTypeReference, IAstTypeReference> remap)
 {
     var remapped = RemapAll(parameters, remap);
     return !remapped.Item2 ? type : new AstGenericTypeWithArguments(type, remapped.Item1);
 }
Пример #37
0
        private IAstTypeReference ApplyArgumentTypes(GenericTypeHelper genericHelper, IAstTypeReference type, IAstTypeReference[] genericParameterTypes)
        {
            return(genericHelper.RemapArgumentTypes(type, t => {
                var parameterIndex = genericParameterTypes.IndexOf(t);
                if (parameterIndex < 0)
                {
                    return t;
                }

                return this.GenericArgumentTypes[parameterIndex];
            }));
        }
Пример #38
0
 public bool Matches(IAstTypeReference other)
 {
     return(this.matches(other));
 }
Пример #39
0
        private IAstTypeReference AdaptReferencedType(IAstTypeReference type)
        {
            var typeAsGeneric = type as AstGenericTypeWithArguments;
            if (typeAsGeneric == null)
                return type;

            var arguments = typeAsGeneric.TypeArguments.ToArray();
            var parameters = this.PrimaryType.GetTypeParameters().Select((p, index) => new {p.Name, index}).ToDictionary(p => p.Name, p => p.index);

            for (var i = 0; i < arguments.Length; i++) {
                var placeholder = arguments[i] as AstGenericPlaceholderType;
                if (placeholder == null)
                    continue;

                arguments[i] = this.TypeArguments[parameters[placeholder.Name]];
            }

            return new AstGenericTypeWithArguments(typeAsGeneric.PrimaryType, arguments);
        }
Пример #40
0
 public AstSpecifiedFunctionType(IEnumerable<IAstTypeReference> parameterTypes, IAstTypeReference returnType)
 {
     Argument.RequireNotNull("parameterTypes", parameterTypes);
     this.ParameterTypes = parameterTypes.ToList();
     this.ReturnType = returnType;
 }
Пример #41
0
        private int GetDistance(IAstTypeReference parameterType, IAstTypeReference argumentType, IAstMethodReference method, Dictionary<IAstTypeReference, ISet<IAstTypeReference>> genericTheories)
        {
            if (argumentType.IsImplicit())
                return 0;

            var argumentCompatibleTypes = GetCompatibleTypes(argumentType);
            var distance = argumentCompatibleTypes.GetValueOrDefault(parameterType, -1);
            if (distance != -1)
                return distance;

            if (!method.IsGeneric)
                return -1;

            if (CanMatchPotentialGeneric(parameterType, argumentType, () => argumentCompatibleTypes, genericTheories))
                return 0;

            return -1;
        }
Пример #42
0
 public AstSpecifiedFunctionType(IEnumerable <IAstTypeReference> parameterTypes, IAstTypeReference returnType)
 {
     Argument.RequireNotNull("parameterTypes", parameterTypes);
     this.ParameterTypes = parameterTypes.ToList();
     this.ReturnType     = returnType;
 }
Пример #43
0
 public TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false)
 {
     return(this.referenceContext.ConvertReference(type, returnNullIfFailed));
 }
Пример #44
0
 public VariableDefinition DefineVariable(string name, IAstTypeReference type)
 {
     return(DefineVariable(name, this.ConvertReference(type)));
 }
Пример #45
0
        private IAstTypeReference RemapArgumentTypesFromParameters(IAstTypeReference type, IAstTypeReference[] parameters, Func <IAstTypeReference, IAstTypeReference> remap)
        {
            var remapped = RemapAll(parameters, remap);

            return(!remapped.Item2 ? type : new AstGenericTypeWithArguments(type, remapped.Item1));
        }
Пример #46
0
 public void SetDependentReturnType(Func<IAstTypeReference> dependentReturnType)
 {
     Argument.RequireNotNull("dependentReturnType", dependentReturnType);
     this.fixedReturnType = null;
     this.dependentReturnType = dependentReturnType;
 }
Пример #47
0
        private DeconstructingAdapter DeconstructIfHasTypeArguments(IAstTypeReference type)
        {
            var generic = type as AstGenericTypeWithArguments;
            if (generic != null) {
                return new DeconstructingAdapter(other => {
                    var otherAsGeneric = other as AstGenericTypeWithArguments;
                    return otherAsGeneric != null && Equals(otherAsGeneric.PrimaryType, generic.PrimaryType);
                }, generic.TypeArguments);
            }

            var function = type as IAstFunctionTypeReference;
            if (function != null) {
                return new DeconstructingAdapter(
                    other => other is IAstFunctionTypeReference,
                    function.GetParameterTypes().Concat(new[] { function.ReturnType }).ToArray()
                );
            }

            return null;
        }
Пример #48
0
        public VariableDefinition DefineVariable(string name, IAstTypeReference type)
        {
            var variableDefinition = new VariableDefinition(name, this.ConvertReference(type));

            this.Method.Body.InitLocals = true;
            this.Method.Body.Variables.Add(variableDefinition);

            return variableDefinition;
        }
Пример #49
0
 public void SetDependentReturnType(Func <IAstTypeReference> dependentReturnType)
 {
     Argument.RequireNotNull("dependentReturnType", dependentReturnType);
     this.fixedReturnType     = null;
     this.dependentReturnType = dependentReturnType;
 }
Пример #50
0
 public virtual TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false)
 {
     Argument.RequireNotNull("type", type);
     return((TypeReference)ConvertReference((IAstReference)type, returnNullIfFailed));
 }
Пример #51
0
 public AstVariableDefinition(string name, IAstTypeReference type, IAstExpression value)
 {
     this.Name          = @name;
     this.Type          = type;
     this.AssignedValue = value;
 }
Пример #52
0
 public static bool IsVoid(this IAstTypeReference type)
 {
     return(type == AstVoidType.Instance);
 }
Пример #53
0
 public virtual TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false)
 {
     Argument.RequireNotNull("type", type);
     return (TypeReference)ConvertReference((IAstReference)type, returnNullIfFailed);
 }
Пример #54
0
 public AstVariableDefinition(string name, IAstTypeReference type, IAstExpression value)
 {
     this.Name = @name;
     this.Type = type;
     this.AssignedValue = value;
 }
Пример #55
0
 public static bool IsImplicit(this IAstTypeReference type)
 {
     return(type == AstImplicitType.Instance);
 }
Пример #56
0
 public bool Matches(IAstTypeReference other)
 {
     return this.matches(other);
 }
Пример #57
0
 public TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false)
 {
     return this.referenceContext.ConvertReference(type, returnNullIfFailed);
 }
Пример #58
0
 public static bool IsUnknown(this IAstTypeReference type)
 {
     return(type is AstUnknownType);
 }
Пример #59
0
        private IDictionary<IAstTypeReference, int> GetCompatibleTypes(IAstTypeReference type)
        {
            var dictionary = new Dictionary<IAstTypeReference, int> { { type, 0 } };
            var distance = 1;

            foreach (var ancestor in type.GetAncestors()) {
                dictionary.Add(ancestor, distance);
                distance += 1;
            }

            foreach (var @interface in type.GetInterfaces()) {
                dictionary.Add(@interface, 1);
            }

            return dictionary;
        }
Пример #60
0
 protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     yield return this.Type = (IAstTypeReference)transform(this.Type);
 }