public override ICatchClause Rewrite(ICatchClause catchClause) { _log.Info("Rewriting ITryCatchFinallyStatement: " + catchClause + " Pass: "******".ctor")); expression = new CreateObjectInstance() { MethodToCall = ctor, Type = catchClause.ExceptionContainer.Type }; } return new CatchClause(catchClause) { Body = new BlockStatement() { Statements = new ThrowStatement() { Exception = expression }.InList<IStatement>() } }; }
public CreateObjectInstance WithArguments(params IExpression[] arguments) { var createObjectInstance = new CreateObjectInstance(); createObjectInstance.Type = type.ResolvedType; createObjectInstance.MethodToCall = reflector.From(type).GetConstructor(constructorParameters); createObjectInstance.Arguments = new List<IExpression>(arguments); return createObjectInstance; }
private Expression ParseCreateObjectInstance(IOperation currentOperation) { CreateObjectInstance result = new CreateObjectInstance(); result.MethodToCall = (IMethodReference)currentOperation.Value; foreach (var par in result.MethodToCall.Parameters) result.Arguments.Add(this.PopOperandStack()); result.Arguments.Reverse(); return result; }
private Expression ParseCreateObjectInstance(IOperation currentOperation) { Contract.Requires(currentOperation != null); CreateObjectInstance result = new CreateObjectInstance(); Contract.Assume(currentOperation.Value is IMethodReference); result.MethodToCall = (IMethodReference)currentOperation.Value; result.Type = result.MethodToCall.ContainingType; foreach (var par in result.MethodToCall.Parameters) result.Arguments.Add(this.PopOperandStack()); result.Arguments.Reverse(); //Convert ints to bools and enums var i = 0; foreach (var par in result.MethodToCall.Parameters) { Contract.Assume(par != null); Contract.Assume(i < result.Arguments.Count); Contract.Assume(result.Arguments[i] != null); result.Arguments[i] = TypeInferencer.Convert(result.Arguments[i++], par.Type); } return result; }
/// <summary> /// Visits the specified create object instance. /// </summary> /// <param name="createObjectInstance">The create object instance.</param> /// <returns></returns> protected virtual IExpression DeepCopy(CreateObjectInstance createObjectInstance) { createObjectInstance.MethodToCall = this.Substitute(createObjectInstance.MethodToCall); createObjectInstance.Arguments = this.DeepCopy(createObjectInstance.Arguments); createObjectInstance.Type = this.Substitute(createObjectInstance.Type); return createObjectInstance; }
/// <summary> /// Visits the specified create object instance. /// </summary> /// <param name="createObjectInstance">The create object instance.</param> public override void Visit(ICreateObjectInstance createObjectInstance) { CreateObjectInstance mutableCreateObjectInstance = new CreateObjectInstance(createObjectInstance); this.resultExpression = this.myCodeCopier.DeepCopy(mutableCreateObjectInstance); }
public override IExpression Visit(CreateObjectInstance createObjectInstance) { if (createObjectInstance.Arguments.Count == 2) { AddressOf/*?*/ aexpr = createObjectInstance.Arguments[1] as AddressOf; if (aexpr != null && aexpr.Expression.Definition is IMethodReference) { CreateDelegateInstance createDel = new CreateDelegateInstance(); createDel.Instance = createObjectInstance.Arguments[0]; createDel.IsVirtualDelegate = aexpr.Expression.Instance != null; createDel.MethodToCallViaDelegate = (IMethodReference)aexpr.Expression.Definition; createDel.Locations = createObjectInstance.Locations; createDel.Type = createObjectInstance.Type; return this.Visit(createDel); } } return base.Visit(createObjectInstance); }
public override void TraverseChildren(IBlockStatement blockStatement) { base.TraverseChildren(blockStatement); Contract.Assume(blockStatement is BlockStatement); var block = this.currentBlock = (BlockStatement)blockStatement; var n = block.Statements.Count; int i; for (i = 0; i < n; i++) { var statement = block.Statements[i]; Contract.Assume(statement != null); if (statement is EmptyStatement) continue; var decl = statement as LocalDeclarationStatement; if (decl == null) break; this.declarationFor.Add(decl.LocalVariable, decl); } int unifications = 0; for (; i < n; i++) { var statement = block.Statements[i]; Contract.Assume(statement != null); var es = statement as ExpressionStatement; if (es == null) { if (statement is EmptyStatement || statement is LabeledStatement) continue; break; } ILocalDefinition local = null; var assignment = es.Expression as Assignment; IExpression initialValue = null; if (assignment != null) { initialValue = assignment.Source; local = assignment.Target.Definition as ILocalDefinition; if (local == null) { var addressDeref = assignment.Target.Definition as IAddressDereference; if (addressDeref == null) continue; var addressOf = addressDeref.Address as IAddressOf; if (addressOf == null) continue; var addressableExpression = addressOf.Expression as IAddressableExpression; if (addressableExpression == null) continue; local = addressableExpression.Definition as ILocalDefinition; if (local == null || this.firstReferenceToLocal[local] != addressableExpression) continue; } else { if (this.firstReferenceToLocal[local] != assignment.Target) continue; } } else { var mcall = es.Expression as MethodCall; if (mcall == null || mcall.IsStaticCall || mcall.IsJumpCall || mcall.MethodToCall.Type.TypeCode != PrimitiveTypeCode.Void || mcall.MethodToCall.Name.Value != ".ctor") continue; var addressOf = mcall.ThisArgument as IAddressOf; if (addressOf == null) continue; var addressableExpression = addressOf.Expression as IAddressableExpression; if (addressableExpression == null) continue; local = addressableExpression.Definition as ILocalDefinition; if (local == null || this.firstReferenceToLocal[local] != addressableExpression) continue; if (this.declarationFor.ContainsKey(local)) initialValue = new CreateObjectInstance() { Arguments = mcall.Arguments, Locations = mcall.Locations, MethodToCall = mcall.MethodToCall, Type = mcall.MethodToCall.ContainingType }; } if (local == null || initialValue == null) continue; LocalDeclarationStatement decl; if (!this.declarationFor.TryGetValue(local, out decl)) continue; Contract.Assume(decl != null); this.unifiedDeclarations.Add(decl); this.declarationFor.Remove(local); decl.InitialValue = initialValue; block.Statements[i] = decl; unifications++; } if (unifications == 0) return; var newStatements = new List<IStatement>(block.Statements.Count-unifications); for (i = 0; i < n; i++) { var statement = block.Statements[i]; var decl = statement as LocalDeclarationStatement; if (decl != null) { if (this.unifiedDeclarations.Contains(decl)) { this.unifiedDeclarations.Remove(decl); continue; } if (this.numberOfReferencesToLocal[decl.LocalVariable] == 0 && this.numberOfAssignmentsToLocal[decl.LocalVariable] == 0) continue; } newStatements.Add(statement); } block.Statements = newStatements; }
/// <summary> /// Create the new body of the iterator method. /// </summary> /// <remarks> /// Pseudo code: /// iteratorClosureLocal = new Closure(0); /// iteratorClosureLocal.field = parameter; // for each parameter including this. /// return iteratorClosureLocal; /// </remarks> private BlockStatement CreateNewIteratorMethodBody(IteratorClosureInformation iteratorClosure) { BlockStatement result = new BlockStatement(); // iteratorClosureLocal = new IteratorClosure(0); LocalDefinition localDefinition = new LocalDefinition() { Name = this.host.NameTable.GetNameFor("iteratorClosureLocal"), Type = GetClosureTypeReferenceFromIterator(iteratorClosure), }; CreateObjectInstance createObjectInstance = new CreateObjectInstance() { MethodToCall = GetMethodReference(iteratorClosure, iteratorClosure.Constructor), Type = localDefinition.Type }; // the start state depends on whether the iterator is an IEnumerable or an IEnumerator. For the former, // it must be created in state -2. Then it is the GetEnumerator method that puts it into its // "start" state, i.e., state 0. var startState = this.isEnumerable ? -2 : 0; createObjectInstance.Arguments.Add(new CompileTimeConstant() { Value = startState, Type = this.host.PlatformType.SystemInt32 }); LocalDeclarationStatement localDeclarationStatement = new LocalDeclarationStatement() { InitialValue = createObjectInstance, LocalVariable = localDefinition }; result.Statements.Add(localDeclarationStatement); // Generate assignments to closure instance's fields for each of the parameters captured by the closure. foreach (object capturedLocalOrParameter in FieldForCapturedLocalOrParameter.Keys) { BoundField boundField = FieldForCapturedLocalOrParameter[capturedLocalOrParameter]; Assignment assignment; ITypeReference localOrParameterType = GetLocalOrParameterType(capturedLocalOrParameter); if (capturedLocalOrParameter is ILocalDefinition) continue; if (capturedLocalOrParameter is IThisReference) { var thisR = new ThisReference(); IExpression thisValue = thisR; if (!this.method.ContainingTypeDefinition.IsClass) { thisValue = new AddressDereference() { Address = thisR, Type = this.method.ContainingTypeDefinition.IsGeneric ? (ITypeReference)this.method.ContainingTypeDefinition.InstanceType : (ITypeReference)this.method.ContainingTypeDefinition }; } assignment = new Assignment { Source = thisValue, Type = this.method.ContainingType, Target = new TargetExpression() { Definition = GetFieldReference(iteratorClosure, boundField.Field), Type = this.method.ContainingType, Instance = new BoundExpression() { Type = localDefinition.Type, Instance = null, Definition = localDefinition, IsVolatile = false } }, }; } else { assignment = new Assignment { Source = new BoundExpression() { Definition = capturedLocalOrParameter, Instance = null, IsVolatile = false, Type = localOrParameterType }, Type = localOrParameterType, Target = new TargetExpression() { Definition = GetFieldReference(iteratorClosure, boundField.Field), Type = localOrParameterType, Instance = new BoundExpression() { Type = localDefinition.Type, Instance = null, Definition = localDefinition, IsVolatile = false } }, }; } ExpressionStatement expressionStatement = new ExpressionStatement() { Expression = assignment }; result.Statements.Add(expressionStatement); } // Generate: return iteratorClosureLocal; result.Statements.Add(new ReturnStatement() { Expression = new BoundExpression() { Definition = localDeclarationStatement.LocalVariable, Instance = null, Type = localDeclarationStatement.LocalVariable.Type } }); return result; }
/// <summary> /// Rewrites the children of the given constructor call expression. /// </summary> public virtual void RewriteChildren(CreateObjectInstance createObjectInstance) { this.RewriteChildren((ConstructorOrMethodCall)createObjectInstance); }
/// <summary> /// Visits the specified create object instance. /// </summary> /// <param name="createObjectInstance">The create object instance.</param> /// <returns></returns> public virtual IExpression Visit(CreateObjectInstance createObjectInstance) { createObjectInstance.MethodToCall = this.Visit(createObjectInstance.MethodToCall); createObjectInstance.Arguments = Visit(createObjectInstance.Arguments); createObjectInstance.Type = this.Visit(createObjectInstance.Type); return createObjectInstance; }
/// <summary> /// Visits the specified create object instance. /// </summary> /// <param name="createObjectInstance">The create object instance.</param> public override void Visit(ICreateObjectInstance createObjectInstance) { CreateObjectInstance mutableCreateObjectInstance = createObjectInstance as CreateObjectInstance; if (alwaysMakeACopy || mutableCreateObjectInstance == null) mutableCreateObjectInstance = new CreateObjectInstance(createObjectInstance); this.resultExpression = this.myCodeMutator.Visit(mutableCreateObjectInstance); }