public AstParameterDefinition(string name, IAstTypeReference type) { Argument.RequireNotNullAndNotEmpty("name", name); this.Name = name; this.Type = type; }
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)); }
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); }
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); }
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; }
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; }
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)); }
public AstPropertyDefinition(string name, IAstTypeReference type, IAstExpression assignedValue) { Argument.RequireNotNullAndNotEmpty("name", name); this.Name = name; this.Type = type; this.AssignedValue = assignedValue; }
public string Format(IAstTypeReference type) { var reflected = type as AstReflectedType; if (reflected != null) return Format(reflected.ActualType); return null; }
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)); }
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)); }
public AstFunctionReferenceExpression(IAstElement target, IAstMethodReference reference) { this.Target = target; this.Function = reference; this.ExpressionType = new AstInferredFunctionType( () => this.Function.ParameterTypes, () => this.Function.ReturnType ); }
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)); }
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; }
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; }
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); }
public string Format(IAstTypeReference type) { var reflected = type as AstReflectedType; if (reflected != null) { return(Format(reflected.ActualType)); } return(null); }
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)); } }
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; }
public static IEnumerable <IAstTypeReference> GetAncestors(this IAstTypeReference type) { var @base = type.BaseType; while (@base != null) { yield return(@base); @base = @base.BaseType; } }
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(); }
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)); }
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(); }
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 ); }
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)); }
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); }
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); }
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; }
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]; }); }
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); }
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); }
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); }
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]; })); }
public bool Matches(IAstTypeReference other) { return(this.matches(other)); }
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); }
public AstSpecifiedFunctionType(IEnumerable<IAstTypeReference> parameterTypes, IAstTypeReference returnType) { Argument.RequireNotNull("parameterTypes", parameterTypes); this.ParameterTypes = parameterTypes.ToList(); this.ReturnType = returnType; }
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; }
public AstSpecifiedFunctionType(IEnumerable <IAstTypeReference> parameterTypes, IAstTypeReference returnType) { Argument.RequireNotNull("parameterTypes", parameterTypes); this.ParameterTypes = parameterTypes.ToList(); this.ReturnType = returnType; }
public TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false) { return(this.referenceContext.ConvertReference(type, returnNullIfFailed)); }
public VariableDefinition DefineVariable(string name, IAstTypeReference type) { return(DefineVariable(name, this.ConvertReference(type))); }
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)); }
public void SetDependentReturnType(Func<IAstTypeReference> dependentReturnType) { Argument.RequireNotNull("dependentReturnType", dependentReturnType); this.fixedReturnType = null; this.dependentReturnType = dependentReturnType; }
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; }
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; }
public void SetDependentReturnType(Func <IAstTypeReference> dependentReturnType) { Argument.RequireNotNull("dependentReturnType", dependentReturnType); this.fixedReturnType = null; this.dependentReturnType = dependentReturnType; }
public virtual TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false) { Argument.RequireNotNull("type", type); return((TypeReference)ConvertReference((IAstReference)type, returnNullIfFailed)); }
public AstVariableDefinition(string name, IAstTypeReference type, IAstExpression value) { this.Name = @name; this.Type = type; this.AssignedValue = value; }
public static bool IsVoid(this IAstTypeReference type) { return(type == AstVoidType.Instance); }
public virtual TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false) { Argument.RequireNotNull("type", type); return (TypeReference)ConvertReference((IAstReference)type, returnNullIfFailed); }
public static bool IsImplicit(this IAstTypeReference type) { return(type == AstImplicitType.Instance); }
public bool Matches(IAstTypeReference other) { return this.matches(other); }
public TypeReference ConvertReference(IAstTypeReference type, bool returnNullIfFailed = false) { return this.referenceContext.ConvertReference(type, returnNullIfFailed); }
public static bool IsUnknown(this IAstTypeReference type) { return(type is AstUnknownType); }
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; }
protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform) { yield return this.Type = (IAstTypeReference)transform(this.Type); }