BooClassBuilder CreateClosureClass(ForeignReferenceCollector collector, InternalMethod closure) { Method method = closure.Method; TypeDefinition parent = method.DeclaringType; parent.Members.Remove(method); BooClassBuilder builder = collector.CreateSkeletonClass(closure.Name, method.LexicalInfo); parent.Members.Add(builder.ClassDefinition); builder.ClassDefinition.Members.Add(method); method.Name = "Invoke"; if (method.IsStatic) { // need to adjust paremeter indexes (parameter 0 is now self) foreach (ParameterDeclaration parameter in method.Parameters) { ((InternalParameter)parameter.Entity).Index += 1; } } method.Modifiers = TypeMemberModifiers.Public; var coll = new GenericTypeCollector(CodeBuilder); coll.Process(builder.ClassDefinition); return builder; }
Method CreateBeginInvokeExtension(ICallableType anonymousType, Method beginInvoke, out MethodInvocationExpression mie) { InternalMethod beginInvokeEntity = (InternalMethod)beginInvoke.Entity; Method extension = CodeBuilder.CreateMethod("BeginInvoke", TypeSystemServices.Map(typeof(IAsyncResult)), TypeMemberModifiers.Public | TypeMemberModifiers.Static); extension.Attributes.Add(CodeBuilder.CreateAttribute(Types.ExtensionAttribute)); ParameterDeclaration self = CodeBuilder.CreateParameterDeclaration(0, "self", beginInvokeEntity.DeclaringType); extension.Parameters.Add(self); CodeBuilder.DeclareParameters(extension, 1, anonymousType.GetSignature().Parameters); mie = CodeBuilder.CreateMethodInvocation( CodeBuilder.CreateReference(self), beginInvokeEntity); ParameterDeclarationCollection parameters = extension.Parameters; for (int i = 1; i < parameters.Count; ++i) { mie.Arguments.Add(CodeBuilder.CreateReference(parameters[i])); } extension.Body.Add(new ReturnStatement(mie)); return(extension); }
IType InferGeneratorItemTypeFor(InternalMethod generator) { if (TypeSystemServices.IsGenericGeneratorReturnType(generator.ReturnType)) return generator.ReturnType.ConstructedInfo.GenericArguments[0]; var yieldExpressions = generator.YieldExpressions; return yieldExpressions.Count > 0 ? TypeSystemServices.GetMostGenericType(yieldExpressions) : TypeSystemServices.ObjectType; }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List(); _mapping = new Hashtable(); _generator = method; _generatorItemType = (IType)_generator.Method["GeneratorItemType"]; _enumerable = (BooClassBuilder)_generator.Method["GeneratorClassBuilder"]; Debug.Assert(null != _generatorItemType); Debug.Assert(null != _enumerable); Initialize(context); }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List(); _mapping = new Hashtable(); _generator = method; GeneratorSkeleton skeleton = context.Provide<GeneratorSkeletonBuilder>().SkeletonFor(method); _generatorItemType = skeleton.GeneratorItemType; _enumerable = skeleton.GeneratorClassBuilder; _getEnumeratorBuilder = skeleton.GetEnumeratorBuilder; Initialize(context); }
public override void Initialize(CompilerContext context) { base.Initialize(context); _currentModule = null; _currentMethod = null; _methodStack = new Stack<InternalMethod>(); _memberStack = new Stack(); _callableResolutionService = new EnvironmentProvision<CallableResolutionService>(); _invocationTypeReferenceRules = new EnvironmentProvision<InvocationTypeInferenceRules>(); _typeChecker = new EnvironmentProvision<TypeChecker>(); _methodCache = new EnvironmentProvision<RuntimeMethodCache>(); }
public GeneratorMethodProcessor(CompilerContext context, InternalMethod method) { _labels = new List<LabelStatement>(); _mapping = new Hashtable(); _generator = method; var skeleton = My<GeneratorSkeletonBuilder>.Instance.SkeletonFor(method); _generatorItemType = skeleton.GeneratorItemType; _enumerable = skeleton.GeneratorClassBuilder; _getEnumeratorBuilder = skeleton.GetEnumeratorBuilder; Initialize(context); }
Method CreateBeginInvokeOverload(ICallableType anonymousType, Method beginInvoke, out MethodInvocationExpression mie) { InternalMethod beginInvokeEntity = (InternalMethod)beginInvoke.Entity; Method overload = CodeBuilder.CreateMethod("BeginInvoke", Map(typeof(IAsyncResult)), TypeMemberModifiers.Public | TypeMemberModifiers.Virtual); CodeBuilder.DeclareParameters(overload, 1, anonymousType.GetSignature().Parameters); mie = CodeBuilder.CreateMethodInvocation( CodeBuilder.CreateSelfReference(beginInvokeEntity.DeclaringType), beginInvokeEntity); foreach (ParameterDeclaration parameter in overload.Parameters) { mie.Arguments.Add(CodeBuilder.CreateReference(parameter)); } overload.Body.Add(new ReturnStatement(mie)); return(overload); }
void ResolveReturnType(InternalMethod entity) { var method = entity.Method; method.ReturnType = entity.ReturnExpressions == null ? CodeBuilder.CreateTypeReference(TypeSystemServices.VoidType) : GetMostGenericTypeReference(entity.ReturnExpressions); TraceReturnType(method, entity); }
void TryToResolveReturnType(InternalMethod entity) { if (entity.IsGenerator) { ResolveGeneratorReturnType(entity); return; } if (CanResolveReturnType(entity)) ResolveReturnType(entity); }
void ProcessNodeInMethodContext(InternalMethod entity, INamespace ns, Node node) { PushMethodInfo(entity); EnterNamespace(ns); try { Visit(node); } finally { LeaveNamespace(); PopMethodInfo(); } }
void ResolveGeneratorReturnType(InternalMethod entity) { IType returnType = GetGeneratorReturnType(entity); entity.Method.ReturnType = CodeBuilder.CreateTypeReference(returnType); }
void ProcessMethodBody(InternalMethod entity, INamespace ns) { ProcessNodeInMethodContext(entity, ns, entity.Method.Body); }
void PopMethodInfo() { _currentMethod = _methodStack.Pop(); }
private string GetMostSimilarBaseMethodName(InternalMethod entity) { return NameResolutionService.GetMostSimilarMemberName(entity.DeclaringType.BaseType, entity.Name, EntityType.Method); }
IMethod FindMethodOverridenBy(InternalMethod entity) { var method = entity.Method; if (method.ParentNode.NodeType == NodeType.Property) return FindPropertyAccessorOverridenBy((Property)method.ParentNode, method); var baseType = entity.DeclaringType.BaseType; var candidates = NameResolutionService.Resolve(baseType, entity.Name, EntityType.Method); if (candidates == null) return null; var baseMethod = FindMethodOverridenBy(entity, candidates); if (baseMethod != null) EnsureRelatedNodeWasVisited(method, baseMethod); return baseMethod; }
public InternalGenericParameter(TypeSystemServices tss, GenericParameterDeclaration declaration, InternalMethod declaringMethod, int position) : this(tss, declaration, position) { _declaringMethod = declaringMethod; }
private IType GeneratorItemTypeFor(InternalMethod generator) { return My<GeneratorItemTypeInferrer>.Instance.GeneratorItemTypeFor(generator); }
public override void Dispose() { base.Dispose(); _currentModule = null; _currentMethod = null; _methodStack = null; _memberStack = null; }
private void MethodHidesInheritedNonVirtual(InternalMethod hidingMethod, IMethod hiddenMethod) { Warnings.Add(CompilerWarningFactory.MethodHidesInheritedNonVirtual(hidingMethod.Method, hidingMethod, hiddenMethod)); }
/// <summary> /// Allows a different language to use custom rules for generator /// return types. /// </summary> /// <param name="generator"></param> /// <returns></returns> protected virtual IType GetGeneratorReturnType(InternalMethod generator) { // Make method return a generic IEnumerable IType itemType = GeneratorItemTypeFor(generator); if (TypeSystemServices.VoidType == itemType) // circunvent exception in MakeGenericType return TypeSystemServices.ErrorEntity; IType enumerableType = TypeSystemServices.IEnumerableGenericType; return enumerableType.GenericInfo.ConstructType(itemType); }
void ProcessMethodBody(InternalMethod entity) { ProcessMethodBody(entity, entity); }
static bool CanResolveReturnType(InternalMethod method) { var expressions = method.ReturnExpressions; if (null != expressions) { foreach (var expression in expressions) { IType type = expression.ExpressionType; if (type == null || TypeSystemServices.IsUnknown(type)) return false; } } return true; }
void ProcessMethodOverride(InternalMethod entity, IMethod baseMethod) { CallableSignature baseSignature = TypeSystemServices.GetOverriddenSignature(baseMethod, entity); if (TypeSystemServices.IsUnknown(entity.ReturnType)) { entity.Method.ReturnType = CodeBuilder.CreateTypeReference(entity.Method.LexicalInfo, baseSignature.ReturnType); } else if (baseSignature.ReturnType != entity.ReturnType) { Error(CompilerErrorFactory.InvalidOverrideReturnType( entity.Method.ReturnType, baseMethod, baseMethod.ReturnType, entity.ReturnType)); } SetOverride(entity, baseMethod); }
private static IMethod FindMethodOverridenBy(InternalMethod entity, IEntity candidates) { if (EntityType.Method == candidates.EntityType) { var candidate = (IMethod)candidates; if (TypeSystemServices.CheckOverrideSignature(entity, candidate)) return candidate; } if (EntityType.Ambiguous == candidates.EntityType) foreach (IMethod candidate in ((Ambiguous) candidates).Entities) if (TypeSystemServices.CheckOverrideSignature(entity, candidate)) return candidate; return null; }
void PushMethodInfo(InternalMethod entity) { _methodStack.Push(_currentMethod); _currentMethod = entity; }
void AddInitializerToStaticConstructor(TypeDefinition type, InternalMethod initializer) { GetStaticConstructor(type).Body.Insert(0, CodeBuilder.CreateMethodInvocation(initializer)); }
void ResolveMethodOverride(InternalMethod entity) { var baseMethod = FindMethodOverridenBy(entity); if (baseMethod == null) { var suggestion = GetMostSimilarBaseMethodName(entity); if (suggestion == entity.Name) //same name => incompatible signature Error(CompilerErrorFactory.NoMethodToOverride(entity.Method, entity, true)); else //suggestion (or null) Error(CompilerErrorFactory.NoMethodToOverride(entity.Method, entity, suggestion)); } else ValidateOverride(entity, baseMethod); }
void CheckGeneratorCantReturnValues(InternalMethod entity) { if (!entity.IsGenerator) return; if (null == entity.ReturnExpressions) return; foreach (Expression e in entity.ReturnExpressions) { Error(CompilerErrorFactory.GeneratorCantReturnValue(e)); } }
void SetOverride(InternalMethod entity, IMethod baseMethod) { TraceOverride(entity.Method, baseMethod); entity.Overriden = baseMethod; entity.Method.Modifiers |= TypeMemberModifiers.Override; }
void CheckGeneratorYieldType(InternalMethod method, IType returnType) { if (!TypeSystemServices.IsGenericGeneratorReturnType(returnType)) return; IType returnElementType = returnType.ConstructedInfo.GenericArguments[0]; foreach (var yieldExpression in method.YieldExpressions) { var yieldType = yieldExpression.ExpressionType; if (!IsAssignableFrom(returnElementType, yieldType) && !TypeSystemServices.CanBeReachedByDownCastOrPromotion(returnElementType, yieldType)) { Error(CompilerErrorFactory.YieldTypeDoesNotMatchReturnType( yieldExpression, yieldType, returnElementType)); } } }
private void ValidateOverride(InternalMethod entity, IMethod baseMethod) { if (CanBeOverriden(baseMethod)) ProcessMethodOverride(entity, baseMethod); else CantOverrideNonVirtual(entity.Method, baseMethod); }
void CheckIfIsMethodOverride(InternalMethod method) { if (method.IsStatic) return; if (method.IsNew) return; var overriden = FindMethodOverridenBy(method); if (overriden == null) return; if (CanBeOverriden(overriden)) ProcessMethodOverride(method, overriden); else if (InStrictMode()) CantOverrideNonVirtual(method.Method, overriden); else MethodHidesInheritedNonVirtual(method, overriden); }