Пример #1
0
 public override void RewriteChildren(AddressableExpression addressableExpression) {
   BoundField/*?*/ boundField;
   if (this.fieldForCapturedLocalOrParameter.TryGetValue(addressableExpression.Definition, out boundField)) {
     addressableExpression.Instance = new ThisReference();
     addressableExpression.Definition = iteratorClosure.GetReferenceOfFieldUsedByPeers(boundField.Field);
     addressableExpression.Type = boundField.Type;
     return;
   }
   base.RewriteChildren(addressableExpression);
 }
Пример #2
0
 private Expression ParseArrayElementAddres(IOperation currentOperation)
 {
     AddressOf result = new AddressOf();
       result.ObjectControlsMutability = this.sawReadonly;
       AddressableExpression addressableExpression = new AddressableExpression();
       result.Expression = addressableExpression;
       ArrayIndexer indexer = this.ParseArrayIndexer(currentOperation);
       addressableExpression.Definition = indexer;
       addressableExpression.Instance = indexer.IndexedObject;
       this.sawReadonly = false;
       return result;
 }
Пример #3
0
 private Expression ParseAddressOf(IOperation currentOperation)
 {
     AddressableExpression addressableExpression = new AddressableExpression();
       if (currentOperation.Value == null) {
     Debug.Assert(currentOperation.OperationCode == OperationCode.Ldarg || currentOperation.OperationCode == OperationCode.Ldarga_S);
     addressableExpression.Definition = new ThisReference();
       } else
     addressableExpression.Definition = currentOperation.Value;
       if (currentOperation.OperationCode == OperationCode.Ldflda || currentOperation.OperationCode == OperationCode.Ldvirtftn)
     addressableExpression.Instance = this.PopOperandStack();
       if (currentOperation.OperationCode == OperationCode.Ldloca || currentOperation.OperationCode == OperationCode.Ldloca_S) {
     var local = this.GetLocalWithSourceName((ILocalDefinition)currentOperation.Value);
     addressableExpression.Definition = local;
     this.numberOfReferences[local] = this.numberOfReferences.ContainsKey(local) ? this.numberOfReferences[local] + 1 : 1;
     //Treat this as an assignment as well, so that the local does not get deleted because it contains a constant and has only one assignment to it.
     this.numberOfAssignments[local] = this.numberOfAssignments.ContainsKey(local) ? this.numberOfAssignments[local] + 1 : 1;
       }
       return new AddressOf() { Expression = addressableExpression };
 }
 /// <summary>
 /// Rewrites the children of the given addressable expression.
 /// </summary>
 public override void RewriteChildren(AddressableExpression addressableExpression) {
   IFieldReference closureField;
   var map = this.isInsideAnonymousMethod ? this.fieldReferencesForUseInsideAnonymousMethods : this.fieldReferencesForUseInsideThisMethod;
   if (map.TryGetValue(addressableExpression.Definition, out closureField)) {
     addressableExpression.Instance = this.GetClosureObjectInstanceContaining(closureField);
     addressableExpression.Definition = closureField;
     addressableExpression.Type = closureField.Type;
     return;
   }
   base.RewriteChildren(addressableExpression);
 }
Пример #5
0
 private Expression ParseArrayElementAddres(IOperation currentOperation, ITypeReference elementType, bool treatArrayAsSingleDimensioned = false) {
   Contract.Requires(currentOperation != null);
   Contract.Requires(elementType != null);
   AddressOf result = new AddressOf();
   result.ObjectControlsMutability = this.sawReadonly;
   AddressableExpression addressableExpression = new AddressableExpression();
   result.Expression = addressableExpression;
   ArrayIndexer indexer = this.ParseArrayIndexer(currentOperation, elementType, treatArrayAsSingleDimensioned);
   addressableExpression.Definition = indexer;
   addressableExpression.Instance = indexer.IndexedObject;
   addressableExpression.Type = elementType;
   this.sawReadonly = false;
   return result;
 }
Пример #6
0
    private Expression ParseAddressOf(Instruction instruction) {
      Contract.Requires(instruction != null);

      var currentOperation = instruction.Operation;
      AddressableExpression addressableExpression = new AddressableExpression();
      if (currentOperation.Value == null) {
        Contract.Assume(currentOperation.OperationCode == OperationCode.Ldarga || currentOperation.OperationCode == OperationCode.Ldarga_S);
        var thisType = this.cdfg.MethodBody.MethodDefinition.ContainingTypeDefinition;
        addressableExpression.Definition = new ThisReference() { Type = thisType };
        addressableExpression.Type = thisType;
      } else {
        var definition = currentOperation.Value;
        Contract.Assume(definition is ILocalDefinition || definition is IParameterDefinition ||
          definition is IFieldReference || definition is IMethodReference || definition is IExpression);
        addressableExpression.Definition = definition;
        addressableExpression.Type = GetTypeFrom(definition);
      }
      if (currentOperation.OperationCode == OperationCode.Ldflda || currentOperation.OperationCode == OperationCode.Ldvirtftn)
        addressableExpression.Instance = this.PopOperandStack();
      if (currentOperation.OperationCode == OperationCode.Ldloca || currentOperation.OperationCode == OperationCode.Ldloca_S) {
        var local = this.GetLocalWithSourceName((ILocalDefinition)currentOperation.Value);
        addressableExpression.Definition = local;
        this.numberOfReferencesToLocal[local] = this.numberOfReferencesToLocal.ContainsKey(local) ? this.numberOfReferencesToLocal[local] + 1 : 1;
        //Treat this as an assignment as well, so that the local does not get deleted because it contains a constant and has only one assignment to it.
        this.numberOfAssignmentsToLocal[local] = this.numberOfAssignmentsToLocal.ContainsKey(local) ? this.numberOfAssignmentsToLocal[local] + 1 : 1;
        if (this.instructionsThatMakeALastUseOfALocalVersion.Contains(instruction)) {
          this.instructionsThatMakeALastUseOfALocalVersion.Remove(instruction);
          this.bindingsThatMakeALastUseOfALocalVersion.Add(addressableExpression);
        }
      }
      return new AddressOf() { Expression = addressableExpression };
    }
Пример #7
0
 /// <summary>
 /// Visits the specified addressable expression.
 /// </summary>
 /// <param name="addressableExpression">The addressable expression.</param>
 /// <returns></returns>
 protected virtual IAddressableExpression DeepCopy(AddressableExpression addressableExpression)
 {
     object def = addressableExpression.Definition;
       ILocalDefinition/*?*/ loc = def as ILocalDefinition;
       if (loc != null)
     addressableExpression.Definition = this.GetMutableCopyIfItExists(loc);
       else {
     IParameterDefinition/*?*/ par = def as IParameterDefinition;
     if (par != null)
       addressableExpression.Definition = this.GetMutableCopyIfItExists(par);
     else {
       IFieldReference/*?*/ field = def as IFieldReference;
       if (field != null)
     addressableExpression.Definition = this.Substitute(field);
       else {
     IArrayIndexer/*?*/ indexer = def as IArrayIndexer;
     if (indexer != null)
       addressableExpression.Definition = this.Substitute(indexer);
     else {
       IAddressDereference/*?*/ adr = def as IAddressDereference;
       if (adr != null)
         addressableExpression.Definition = this.Substitute(adr);
       else {
         IMethodReference/*?*/ meth = def as IMethodReference;
         if (meth != null)
           addressableExpression.Definition = this.Substitute(meth);
         else {
           IThisReference thisRef = (IThisReference)def;
           addressableExpression.Definition = this.Substitute(thisRef);
         }
       }
     }
       }
     }
       }
       if (addressableExpression.Instance != null)
     addressableExpression.Instance = this.Substitute(addressableExpression.Instance);
       addressableExpression.Type = this.Substitute(addressableExpression.Type);
       return addressableExpression;
 }
Пример #8
0
 public override void RewriteChildren(AddressableExpression addressableExpression) {
   if (addressableExpression.Definition == this.local) {
     addressableExpression.Definition = this.expressionToSubstituteForLocal;
     this.numberOfAssignmentsToLocal[this.local]--;
     this.replacementHappened = true;
     return;
   }
   base.RewriteChildren(addressableExpression);
 }
Пример #9
0
 /// <summary>
 /// Visits the specified addressable expression.
 /// </summary>
 /// <param name="addressableExpression">The addressable expression.</param>
 public override void Visit(IAddressableExpression addressableExpression)
 {
     AddressableExpression mutableAddressableExpression = new AddressableExpression(addressableExpression);
     this.resultExpression = this.myCodeCopier.DeepCopy(mutableAddressableExpression);
 }
Пример #10
0
 public override void RewriteChildren(AddressableExpression addressableExpression) {
   IExpression capturedThing;
   var field = addressableExpression.Definition as IFieldReference;
   if (field != null) {
     var k = field.InternedKey;
     var spec = field as ISpecializedFieldReference;
     if (spec != null) k = spec.UnspecializedVersion.InternedKey;
     if (this.capturedThings.TryGetValue(k, out capturedThing)) {
       var be = capturedThing as IBoundExpression;
       if (be == null) {
         System.Diagnostics.Debug.Assert(false);
       }
       addressableExpression.Definition = be.Definition;
       addressableExpression.Instance = be.Instance;
       return;
     }
   }
   base.RewriteChildren(addressableExpression);
 }
Пример #11
0
 public override IAddressableExpression Visit(AddressableExpression addressableExpression)
 {
     var closureField = addressableExpression.Definition as IFieldReference;
       if (closureField != null) {
     var unspecializedClosureField = UnspecializedMethods.UnspecializedFieldReference(closureField);
     IBoundExpression binding = null;
     if (this.capturedBinding.TryGetValue(unspecializedClosureField.InternedKey, out binding)) {
       addressableExpression.Definition = binding.Definition;
       addressableExpression.Instance = binding.Instance;
       return addressableExpression;
     }
       }
       var parameter = addressableExpression.Definition as IParameterDefinition;
       if (parameter != null) {
     IParameterDefinition parToSubstitute;
     if (this.parameterMap.TryGetValue(parameter, out parToSubstitute)) {
       addressableExpression.Definition = parToSubstitute;
       return addressableExpression;
     }
       }
       return base.Visit(addressableExpression);
 }
Пример #12
0
 /// <summary>
 /// If the <paramref name="addressableExpression"/> represents a parameter of the target method,
 /// it is replaced with the equivalent parameter of the source method.
 /// </summary>
 /// <param name="addressableExpression">The addressable expression.</param>
 public override void RewriteChildren(AddressableExpression addressableExpression) {
   var/*?*/ par = addressableExpression.Definition as IParameterDefinition;
   if (par != null && par.ContainingSignature == this.targetMethod) {
     addressableExpression.Definition = this.parameters[par.Index];
     if (addressableExpression.Instance != null) {
       addressableExpression.Instance = this.Rewrite(addressableExpression.Instance);
     }
     addressableExpression.Type = this.Rewrite(addressableExpression.Type);
     return;
   } else {
     base.RewriteChildren(addressableExpression);
   }
 }
Пример #13
0
 /// <summary>
 /// Visits the specified addressable expression.
 /// </summary>
 /// <param name="addressableExpression">The addressable expression.</param>
 /// <returns></returns>
 public virtual IAddressableExpression Visit(AddressableExpression addressableExpression)
 {
     object def = addressableExpression.Definition;
       ILocalDefinition/*?*/ loc = def as ILocalDefinition;
       if (loc != null)
     addressableExpression.Definition = this.VisitReferenceTo(loc);
       else {
     IParameterDefinition/*?*/ par = def as IParameterDefinition;
     if (par != null)
       addressableExpression.Definition = this.VisitReferenceTo(par);
     else {
       IFieldReference/*?*/ field = def as IFieldReference;
       if (field != null)
     addressableExpression.Definition = this.Visit(field);
       else {
     IArrayIndexer/*?*/ indexer = def as IArrayIndexer;
     if (indexer != null)
       addressableExpression.Definition = this.Visit(indexer);
     else {
       IAddressDereference/*?*/ adr = def as IAddressDereference;
       if (adr != null)
         addressableExpression.Definition = this.Visit(adr);
       else {
         IMethodReference/*?*/ meth = def as IMethodReference;
         if (meth != null)
           addressableExpression.Definition = this.Visit(meth);
         else {
           IThisReference thisRef = (IThisReference)def;
           addressableExpression.Definition = this.Visit(thisRef);
         }
       }
     }
       }
     }
       }
       if (addressableExpression.Instance != null)
     addressableExpression.Instance = this.Visit(addressableExpression.Instance);
       addressableExpression.Type = this.Visit(addressableExpression.Type);
       return addressableExpression;
 }
Пример #14
0
 /// <summary>
 /// Rewrites the children of the given addressable expression.
 /// </summary>
 /// <param name="addressableExpression"></param>
 public virtual void RewriteChildren(AddressableExpression addressableExpression)
 {
     this.RewriteChildren((Expression)addressableExpression);
       var local = addressableExpression.Definition as ILocalDefinition;
       if (local != null)
     addressableExpression.Definition = this.RewriteReference(local);
       else {
     var parameter = addressableExpression.Definition as IParameterDefinition;
     if (parameter != null)
       addressableExpression.Definition = this.RewriteReference(parameter);
     else {
       var fieldReference = addressableExpression.Definition as IFieldReference;
       if (fieldReference != null)
     addressableExpression.Definition = this.Rewrite(fieldReference);
       else {
     var arrayIndexer = addressableExpression.Definition as IArrayIndexer;
     if (arrayIndexer != null) {
       addressableExpression.Definition = this.Rewrite(arrayIndexer);
       return; //do not rewrite Instance again
     } else {
       var addressDereference = addressableExpression.Definition as IAddressDereference;
       if (addressDereference != null)
         addressableExpression.Definition = this.Rewrite(addressDereference);
       else {
         var methodReference = addressableExpression.Definition as IMethodReference;
         if (methodReference != null)
           addressableExpression.Definition = this.Rewrite(methodReference);
         else {
           var thisReference = (IThisReference)addressableExpression.Definition;
           addressableExpression.Definition = this.Rewrite(thisReference);
         }
       }
     }
       }
     }
       }
       if (addressableExpression.Instance != null)
     addressableExpression.Instance = this.Rewrite(addressableExpression.Instance);
 }
Пример #15
0
 /// <summary>
 /// Visits the specified addressable expression.
 /// </summary>
 /// <param name="addressableExpression">The addressable expression.</param>
 public override void Visit(IAddressableExpression addressableExpression)
 {
     AddressableExpression mutableAddressableExpression = addressableExpression as AddressableExpression;
     if (alwaysMakeACopy || mutableAddressableExpression == null) mutableAddressableExpression = new AddressableExpression(addressableExpression);
     this.resultExpression = this.myCodeMutator.Visit(mutableAddressableExpression);
 }
Пример #16
0
 /// <summary>
 /// Visits the specified addressable expression.
 /// </summary>
 /// <param name="addressableExpression">The addressable expression.</param>
 /// <returns></returns>
 public virtual IAddressableExpression Visit(IAddressableExpression addressableExpression)
 {
     AddressableExpression mutableAddressableExpression = addressableExpression as AddressableExpression;
       if (!this.copyOnlyIfNotAlreadyMutable || mutableAddressableExpression == null)
     mutableAddressableExpression = new AddressableExpression(addressableExpression);
       return Visit(mutableAddressableExpression);
 }