public override IExpression Rewrite(ICreateDelegateInstance createDelegateInstance)
        {
            IMethodDefinition delegateMethodDefinition = createDelegateInstance.MethodToCallViaDelegate.ResolvedMethod;

            if (this.closures != null && this.closures.Find(delegateMethodDefinition.ContainingTypeDefinition.InternedKey) != null)
            {
                Contract.Assume(!(delegateMethodDefinition is Dummy));
                return(this.ConvertToAnonymousDelegate(delegateMethodDefinition, createDelegateInstance.Type));
            }
            if (delegateMethodDefinition.Visibility == TypeMemberVisibility.Private &&
                AttributeHelper.Contains(delegateMethodDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                //The method that is enclosed in the delegate is private and it has been generated by a compiler.
                //We assume that means that this delegate is an anonymous delegate and we'll rewrite the expression as such.
                //The anonymous delegate will not have captured anything other than the this parameter, otherwise it would be an instance method on a closure class.
                if (this.containingMethod.IsGeneric)
                {
                    var genInst = delegateMethodDefinition as IGenericMethodInstance;
                    if (genInst == null)
                    {
                        return(createDelegateInstance);       //Anonymous delegates have to be generic in this case.
                    }
                    if (this.GenericArgumentsDoNotMatchGenericParameters(genInst.GenericArguments))
                    {
                        return(createDelegateInstance);
                    }
                }
                this.AddToPrivateHelperMethodsToRemove(MemberHelper.UninstantiateAndUnspecialize(delegateMethodDefinition));
                Contract.Assume(!(delegateMethodDefinition is Dummy));
                return(this.ConvertToAnonymousDelegate(delegateMethodDefinition, createDelegateInstance.Type));
            }
            return(createDelegateInstance);
        }
 public override void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     if (Process(createDelegateInstance))
     {
         visitor.Visit(createDelegateInstance);
     }
     base.Visit(createDelegateInstance);
 }
 public override IExpression Rewrite(ICreateDelegateInstance createDelegate)
 {
     var newDel = new CreateDelegateInstance(createDelegate);
     newDel.MethodToCallViaDelegate =
         CurrentMethod.ContainingTypeDefinition.Methods
         .First(m => m.ToString() == MutationTarget.PassInfo);
     return newDel;
 }
            public override IExpression Rewrite(ICreateDelegateInstance createDelegate)
            {
                var newDel = new CreateDelegateInstance(createDelegate);

                newDel.MethodToCallViaDelegate =
                    CurrentMethod.ContainingTypeDefinition.Methods
                    .First(m => m.ToString() == MutationTarget.PassInfo);
                return(newDel);
            }
            public override void Visit(ICreateDelegateInstance createDelegate)
            {
                var delegatedMethod = createDelegate.MethodToCallViaDelegate.ResolvedMethod;
                var otherMethod     = Parent.CurrentMethod.ContainingTypeDefinition.Methods
                                      .FirstOrDefault(m => m != delegatedMethod &&
                                                      TypeHelper.ParameterListsAreEquivalent(delegatedMethod.Parameters, m.Parameters) &&
                                                      m.Type == delegatedMethod.Type);

                if (otherMethod != null)
                {
                    MarkMutationTarget(createDelegate, otherMethod.ToString());
                }
            }
            public override void Visit(ICreateDelegateInstance createDelegate)
            {
                var delegatedMethod = createDelegate.MethodToCallViaDelegate.ResolvedMethod;
                var otherMethod = Parent.CurrentMethod.ContainingTypeDefinition.Methods
                    .FirstOrDefault(m => m != delegatedMethod
                    && TypeHelper.ParameterListsAreEquivalent(delegatedMethod.Parameters, m.Parameters)
                    && m.Type == delegatedMethod.Type);

                if (otherMethod != null)
                {
                    MarkMutationTarget(createDelegate, otherMethod.ToString());
                }
            }
Пример #7
0
        private HLLocation ProcessCreateDelegateInstanceExpression(ICreateDelegateInstance pExpression)
        {
            HLLocation locationInstance = null;

            if (pExpression.Instance != null)
            {
                locationInstance = ProcessExpression(pExpression.Instance);
            }
            HLMethod   methodCalled     = HLDomain.GetOrCreateMethod(pExpression.MethodToCallViaDelegate);
            HLLocation locationDelegate = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitNewDelegate(locationDelegate.Type, locationDelegate.AddressOf(), locationInstance, methodCalled, pExpression.IsVirtualDelegate);
            return(locationDelegate);
        }
Пример #8
0
 public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance) {
   base.TraverseChildren(createDelegateInstance);
   var delegateMethodDefinition = createDelegateInstance.MethodToCallViaDelegate.ResolvedMethod;
   var containingTypeDefinition = delegateMethodDefinition.ContainingTypeDefinition;
   if (TypeHelper.IsCompilerGenerated(containingTypeDefinition)) {
     if (!containingTypeDefinition.IsValueType) { //If it is a value type, is is probably the state class of an async method.
       if (this.closures == null) this.closures = new Hashtable<object>();
       this.closures.Add(containingTypeDefinition.InternedKey, containingTypeDefinition);
       this.AddOuterClosures(containingTypeDefinition);
       this.sawAnonymousDelegate = true;
     }
   } else {
     this.sawAnonymousDelegate |= AttributeHelper.Contains(delegateMethodDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute);
   }
 }
        public override void TraverseChildren(IMethodCall methodCall)
        {
            if (PhoneCodeHelper.instance().PhoneFeedbackToggled)
            {
                // check for handlers we do not wish to add feedback checks to
                if (methodCall.MethodToCall.Name.Value.StartsWith("add_"))
                {
                    string eventName = methodCall.MethodToCall.Name.Value.Remove(0, "add_".Length);
                    if (PhoneCodeHelper.IgnoredEvents.Contains(eventName))
                    {
                        IMethodReference eventHandler = null;
                        foreach (IExpression arg in methodCall.Arguments)
                        {
                            ICreateDelegateInstance createDelegate = arg as ICreateDelegateInstance;
                            if (createDelegate == null)
                            {
                                continue;
                            }

                            ITypeReference typeRef = createDelegate.Type;
                            if (!typeRef.isRoutedEventHandlerClass(host))
                            {
                                continue;
                            }

                            eventHandler = createDelegate.MethodToCallViaDelegate;
                            break;
                        }

                        if (eventHandler != null)
                        {
                            INamespaceTypeReference namedType = eventHandler.ContainingType.ResolvedType as INamespaceTypeReference;
                            if (namedType != null)
                            {
                                INamespaceTypeDefinition namedTypeDef = namedType.ResolvedType;
                                if (namedTypeDef != null)
                                {
                                    PhoneCodeHelper.instance().ignoreEventHandler(namedTypeDef.ContainingUnitNamespace.Name + "." + namedTypeDef.Name + "." + eventHandler.Name);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #10
0
        public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
        {
            base.TraverseChildren(createDelegateInstance);
            var delegateMethodDefinition = createDelegateInstance.MethodToCallViaDelegate.ResolvedMethod;
            var containingTypeDefinition = delegateMethodDefinition.ContainingTypeDefinition;

            if (TypeHelper.IsCompilerGenerated(containingTypeDefinition))
            {
                if (!containingTypeDefinition.IsValueType) //If it is a value type, is is probably the state class of an async method.
                {
                    if (this.closures == null)
                    {
                        this.closures = new Hashtable <object>();
                    }
                    this.closures.Add(containingTypeDefinition.InternedKey, containingTypeDefinition);
                    this.AddOuterClosures(containingTypeDefinition);
                    this.sawAnonymousDelegate = true;
                }
            }
            else
            {
                this.sawAnonymousDelegate |= AttributeHelper.Contains(delegateMethodDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute);
            }
        }
Пример #11
0
 //^ ensures this.path.Count == old(this.path.Count);
 /// <summary>
 /// Traverses the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public virtual void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     if (this.stopTraversal) return;
       //^ int oldCount = this.path.Count;
       this.path.Push(createDelegateInstance);
       if (createDelegateInstance.Instance != null)
     this.Visit(createDelegateInstance.Instance);
       //^ assume this.path.Count == oldCount+1; //True because all of the virtual methods of this class promise not to decrease this.path.Count.
       this.path.Pop();
 }
Пример #12
0
 public void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     Contract.Requires(createDelegateInstance != null);
       throw new NotImplementedException();
 }
Пример #13
0
 public void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     this.traverser.Traverse(createDelegateInstance);
 }
Пример #14
0
 /// <summary>
 /// Traverses the delegate creation expression.
 /// </summary>
 public void Traverse(ICreateDelegateInstance createDelegateInstance)
 {
     Contract.Requires(createDelegateInstance != null);
       if (this.preorderVisitor != null) this.preorderVisitor.Visit(createDelegateInstance);
       if (this.StopTraversal) return;
       this.TraverseChildren(createDelegateInstance);
       if (this.StopTraversal) return;
       if (this.postorderVisitor != null) this.postorderVisitor.Visit(createDelegateInstance);
 }
Пример #15
0
        public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
{ MethodEnter(createDelegateInstance);
            base.TraverseChildren(createDelegateInstance);
     MethodExit();   }
        public override void TraverseChildren(IMethodCall methodCall)
        {
            if (methodCall.MethodToCall.ResolvedMethod.IsSpecialName && methodCall.MethodToCall.Name.Value == "add_BackKeyPress")
            {
                // check if it is a back key handler and if it is...
                // NAVIGATION TODO this only catches really locally delegate expressions. If it is created before, we see it as a BoundExpression
                // NAVIGATION TODO and need (again) data flow analysis
                bool delegateIsIdentified = false;
                bool delegateIsAnonymous  = false;
                PhoneCodeHelper.instance().OnBackKeyPressOverriden = true;
                IBlockStatement   delegateBody      = null;
                IMethodDefinition delegateMethodRef = null;
                if (methodCall.Arguments.Count() == 1)
                {
                    IExpression             delegateArg   = methodCall.Arguments.First();
                    ICreateDelegateInstance localDelegate = delegateArg as ICreateDelegateInstance;
                    if (localDelegate != null)
                    {
                        delegateIsIdentified = true;
                        delegateMethodRef    = localDelegate.MethodToCallViaDelegate.ResolvedMethod;
                        SourceMethodBody body = delegateMethodRef.Body as SourceMethodBody;
                        if (body != null)
                        {
                            delegateBody = body.Block;
                        }

                        PhoneCodeHelper.instance().KnownBackKeyHandlers.Add(delegateMethodRef);
                    }

                    AnonymousDelegate anonDelegate = delegateArg as AnonymousDelegate;
                    if (anonDelegate != null)
                    {
                        delegateIsIdentified = true;
                        delegateIsAnonymous  = true;
                        delegateBody         = anonDelegate.Body;
                    }

                    // NAVIGATION TODO what if it has no body?
                    if (delegateBody != null)
                    {
                        bool navigates = false, cancelsNav = false;
                        ICollection <string> navTargets;
                        parseBlockForNavigation(delegateBody, out navigates, out navTargets);
                        if (navigates)
                        {
                            ICollection <Tuple <IMethodReference, string> > targets = null;
                            if (!PhoneCodeHelper.instance().BackKeyNavigatingOffenders.TryGetValue(typeBeingTraversed, out targets))
                            {
                                targets = new HashSet <Tuple <IMethodReference, string> >();
                            }

                            foreach (string tgt in navTargets)
                            {
                                IMethodReference dummyRef = null;
                                if (delegateIsAnonymous)
                                {
                                    dummyRef = new Microsoft.Cci.MutableCodeModel.MethodReference();
                                    (dummyRef as Microsoft.Cci.MutableCodeModel.MethodReference).ContainingType = typeBeingTraversed;
                                    (dummyRef as Microsoft.Cci.MutableCodeModel.MethodReference).Name           = Dummy.Name;
                                }
                                targets.Add(Tuple.Create <IMethodReference, string>((delegateIsAnonymous ? dummyRef : delegateMethodRef), "\"" + tgt + "\""));
                            }

                            PhoneCodeHelper.instance().BackKeyNavigatingOffenders[typeBeingTraversed] = targets;
                        }

                        parseBlockForEventCancellation(delegateBody, out cancelsNav);
                        if (cancelsNav)
                        {
                            string reason = "(via delegate ";
                            if (delegateIsIdentified)
                            {
                                reason += delegateMethodRef.ContainingType.ToString() + "." + delegateMethodRef.Name.Value;
                            }
                            else
                            {
                                reason += "anonymous";
                            }
                            reason += ")";
                            PhoneCodeHelper.instance().BackKeyCancellingOffenders.Add(Tuple.Create <ITypeReference, string>(typeBeingTraversed, reason));
                        }
                    }
                }

                if (!delegateIsIdentified)
                {
                    PhoneCodeHelper.instance().BackKeyHandlerOverridenByUnknownDelegate = true;
                    PhoneCodeHelper.instance().BackKeyUnknownDelegateOffenders.Add(typeBeingTraversed);
                }
            }
            base.TraverseChildren(methodCall);
        }
Пример #17
0
 public void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     this.result = this.copier.Copy(createDelegateInstance);
 }
Пример #18
0
 public override void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     allElements.Add(new InvokInfo(Traverser, "ICreateDelegateInstance", createDelegateInstance));
 }
Пример #19
0
 public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
 {
     //The type should already be filled in
 }
Пример #20
0
 /// <summary>
 /// Generates IL for the specified create delegate instance.
 /// </summary>
 /// <param name="createDelegateInstance">The create delegate instance.</param>
 public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
 {
     IPlatformType platformType = createDelegateInstance.Type.PlatformType;
       MethodReference constructor = new MethodReference(this.host, createDelegateInstance.Type, CallingConvention.Default|CallingConvention.HasThis,
     platformType.SystemVoid, this.host.NameTable.Ctor, 0, platformType.SystemObject, platformType.SystemIntPtr);
       if (createDelegateInstance.Instance != null) {
     this.Traverse(createDelegateInstance.Instance);
     if (createDelegateInstance.IsVirtualDelegate) {
       this.generator.Emit(OperationCode.Dup);
       this.StackSize++;
       this.generator.Emit(OperationCode.Ldvirtftn, createDelegateInstance.MethodToCallViaDelegate);
       this.StackSize--;
     } else
       this.generator.Emit(OperationCode.Ldftn, createDelegateInstance.MethodToCallViaDelegate);
     this.StackSize++;
       } else {
     this.generator.Emit(OperationCode.Ldnull);
     this.generator.Emit(OperationCode.Ldftn, createDelegateInstance.MethodToCallViaDelegate);
     this.StackSize+=2;
       }
       this.generator.Emit(OperationCode.Newobj, constructor);
       this.StackSize--;
 }
 public override void TraverseChildren(ICreateDelegateInstance/*!*/ createDelegateInstance) {
   if (createDelegateInstance.Instance != null) {
     ICompileTimeConstant constant = createDelegateInstance.Instance as ICompileTimeConstant;
     if (constant == null || constant.Value != null) {
       this.Traverse(createDelegateInstance.Instance);
       this.PrintToken(CSharpToken.Dot);
     }
   }
   this.PrintMethodDefinitionName(createDelegateInstance.MethodToCallViaDelegate.ResolvedMethod);
   //base.TraverseChildren(createDelegateInstance);
 }
 /// <summary>
 /// Rewrites the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public virtual IExpression Rewrite(ICreateDelegateInstance createDelegateInstance)
 {
     return createDelegateInstance;
 }
Пример #23
0
 public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance) {
   //The type should already be filled in
 }
    public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
    {
      if (createDelegateInstance.Instance == null) {
        TranslatedExpressions.Push(Bpl.Expr.Literal(0));
      }
      else {
        this.Traverse(createDelegateInstance.Instance);
      }

      TranslateDelegateCreation(createDelegateInstance.MethodToCallViaDelegate, createDelegateInstance.Type, createDelegateInstance);
    }
Пример #25
0
    /// <summary>
    /// Returns a deep copy of the anonymous object creation expression.
    /// </summary>
    /// <param name="createDelegateInstance"></param>
    public CreateDelegateInstance Copy(ICreateDelegateInstance createDelegateInstance) {
      Contract.Requires(createDelegateInstance != null);
      Contract.Ensures(Contract.Result<CreateDelegateInstance>() != null);

      var mutableCopy = this.shallowCopier.Copy(createDelegateInstance);
      this.CopyChildren((Expression)mutableCopy);
      if (mutableCopy.Instance != null)
        mutableCopy.Instance = this.Copy(mutableCopy.Instance);
      mutableCopy.MethodToCallViaDelegate = this.Copy(mutableCopy.MethodToCallViaDelegate);
      return mutableCopy;
    }
Пример #26
0
 public virtual void onASTElement(ICreateDelegateInstance createDelegateInstance)
 {
 }
Пример #27
0
    /// <summary>
    /// Returns a shallow copy of the anonymous object creation expression.
    /// </summary>
    /// <param name="createDelegateInstance"></param>
    public CreateDelegateInstance Copy(ICreateDelegateInstance createDelegateInstance) {
      Contract.Requires(createDelegateInstance != null);
      Contract.Ensures(Contract.Result<CreateDelegateInstance>() != null);

      return new CreateDelegateInstance(createDelegateInstance);
    }
Пример #28
0
 /// <summary>
 /// Performs some computation with the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public virtual void Visit(ICreateDelegateInstance createDelegateInstance)
 {
 }
 /// <summary>
 /// Performs some computation with the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public virtual void Visit(ICreateDelegateInstance createDelegateInstance)
 {
 }
Пример #30
0
 /// <summary>
 /// Traverses the children the delegate instance creation expression.
 /// </summary>
 public virtual void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
 {
     Contract.Requires(createDelegateInstance != null);
       this.TraverseChildren((IExpression)createDelegateInstance);
       if (this.StopTraversal) return;
       if (createDelegateInstance.Instance != null)
     this.Traverse(createDelegateInstance.Instance);
       if (this.StopTraversal) return;
       this.Traverse(createDelegateInstance.MethodToCallViaDelegate);
 }
Пример #31
0
 public virtual void onASTElement(ICreateDelegateInstance createDelegateInstance) { }
Пример #32
0
 /// <summary>
 /// Performs some computation with the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public virtual void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     this.Visit((IExpression)createDelegateInstance);
 }
Пример #33
0
 private HLLocation ProcessCreateDelegateInstanceExpression(ICreateDelegateInstance pExpression)
 {
     HLLocation locationInstance = null;
     if (pExpression.Instance != null) locationInstance = ProcessExpression(pExpression.Instance);
     HLMethod methodCalled = HLDomain.GetOrCreateMethod(pExpression.MethodToCallViaDelegate);
     HLLocation locationDelegate = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));
     mCurrentBlock.EmitNewDelegate(locationDelegate.Type, locationDelegate.AddressOf(), locationInstance, methodCalled, pExpression.IsVirtualDelegate);
     return locationDelegate;
 }
Пример #34
0
 public void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     throw new NotImplementedException();
 }
Пример #35
0
 /// <summary>
 /// Rewrites the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public virtual IExpression Rewrite(ICreateDelegateInstance createDelegateInstance)
 {
     var mutableCreateDelegateInstance = createDelegateInstance as CreateDelegateInstance;
       if (mutableCreateDelegateInstance == null) return createDelegateInstance;
       this.RewriteChildren(mutableCreateDelegateInstance);
       return mutableCreateDelegateInstance;
 }
Пример #36
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public CreateDelegateInstance(ICreateDelegateInstance createDelegateInstance)
     : base(createDelegateInstance)
 {
     this.instance = createDelegateInstance.Instance;
       this.isVirtualDelegate = createDelegateInstance.IsVirtualDelegate;
       this.methodToCallViaDelegate = createDelegateInstance.MethodToCallViaDelegate;
 }
Пример #37
0
 /// <summary>
 /// Returns a shallow copy of the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public CreateDelegateInstance Copy(ICreateDelegateInstance createDelegateInstance)
 {
     return new CreateDelegateInstance(createDelegateInstance);
 }
Пример #38
0
 public void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     this.result = this.rewriter.Rewrite(createDelegateInstance);
 }
 public override void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     if(Process(createDelegateInstance)){visitor.Visit(createDelegateInstance);}
     base.Visit(createDelegateInstance);
 }
Пример #40
0
 public override void TraverseChildren(ICreateDelegateInstance createDelegateInstance)
 {
     MethodEnter(createDelegateInstance);
     base.TraverseChildren(createDelegateInstance);
     MethodExit();
 }
Пример #41
0
 /// <summary>
 /// Visits the specified create delegate instance.
 /// </summary>
 /// <param name="createDelegateInstance">The create delegate instance.</param>
 public override void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     CreateDelegateInstance mutableCreateDelegateInstance = new CreateDelegateInstance(createDelegateInstance);
     this.resultExpression = this.myCodeCopier.DeepCopy(mutableCreateDelegateInstance);
 }
Пример #42
0
 /// <summary>
 /// Returns a deep copy of the anonymous object creation expression.
 /// </summary>
 /// <param name="createDelegateInstance"></param>
 public CreateDelegateInstance Copy(ICreateDelegateInstance createDelegateInstance)
 {
     var mutableCopy = this.shallowCopier.Copy(createDelegateInstance);
       this.CopyChildren((Expression)mutableCopy);
       if (mutableCopy.Instance != null)
     mutableCopy.Instance = this.Copy(mutableCopy.Instance);
       mutableCopy.MethodToCallViaDelegate = this.Copy(mutableCopy.MethodToCallViaDelegate);
       return mutableCopy;
 }
Пример #43
0
 /// <summary>
 /// Visits the specified create delegate instance.
 /// </summary>
 /// <param name="createDelegateInstance">The create delegate instance.</param>
 public override void Visit(ICreateDelegateInstance createDelegateInstance)
 {
     CreateDelegateInstance mutableCreateDelegateInstance = createDelegateInstance as CreateDelegateInstance;
     if (alwaysMakeACopy || mutableCreateDelegateInstance == null) mutableCreateDelegateInstance = new CreateDelegateInstance(createDelegateInstance);
     this.resultExpression = this.myCodeMutator.Visit(mutableCreateDelegateInstance);
 }