public override void RewriteChildren(GenericTypeInstanceReference genericTypeInstanceReference) { genericTypeInstanceReference.GenericType = TransformType(genericTypeInstanceReference.GenericType); base.RewriteChildren(genericTypeInstanceReference); genericTypeInstanceReference.GenericArguments = this.Rewrite(genericTypeInstanceReference.GenericArguments); genericTypeInstanceReference.GenericType = this.Rewrite(genericTypeInstanceReference.GenericType); }
private static void AnalyzeGenericVariable(GenericTypeInstanceReference variable) { Console.WriteLine("\t\t" + variable.GenericType.ToString()); foreach (var genericArg in variable.GenericArguments) { Console.WriteLine("\t\t\t" + genericArg.ToString()); } }
public override void BuildMethod() { AddStatement.DeclareInterceptedType(field.ContainingType.ResolvedType); Context.Log.WriteTrace(" Adding: var interceptedField = interceptedType.GetField('{0}');", field.Name.Value); Context.Block.Statements.Add( Declare.Variable<FieldInfo>("interceptedField").As( Call.VirtualMethod("GetField", typeof (string)).ThatReturns<FieldInfo>().WithArguments( Constant.Of(field.Name.Value)).On("interceptedType")) ); AddStatement.DeclareArgumentsList(); var funcT = SharpMockTypes.Functions[0]; var funcActualT = new GenericTypeInstanceReference(); funcActualT.GenericType = funcT; funcActualT.GenericArguments.Add(field.Type); var accessor = new AnonymousDelegate(); accessor.Type = funcActualT; accessor.ReturnType = field.Type; accessor.CallingConvention = CallingConvention.HasThis; var accessorBody = new BlockStatement(); var returnActualField = new ReturnStatement(); var actualField = new BoundExpression(); actualField.Type = field.Type; actualField.Definition = field; returnActualField.Expression = actualField; accessorBody.Statements.Add(returnActualField); accessor.Body = accessorBody; Context.Block.Statements.Add( Declare.Variable("local_0", funcActualT).As(accessor) ); AddStatement.DeclareRegistryInterceptor(); AddStatement.DeclareInvocation(); AddStatement.SetArgumentsOnInvocation(); AddStatement.SetOriginalCallOnInvocation(); AddStatement.SetTargetOnInvocationToNull(); Context.Block.Statements.Add( Do(Call.PropertySetter<MemberInfo>("OriginalCallInfo").WithArguments("interceptedField").On("invocation")) ); AddStatement.CallShouldInterceptOnInterceptor(); AddStatement.CallInterceptOnInterceptor(); Context.Block.Statements.Add( Declare.Variable("interceptionResult", field.Type).As( ChangeType.Convert(Call.PropertyGetter<object>("Return").On("invocation")).To(field.Type)) ); Context.Block.Statements.Add(Return.Variable(Locals["interceptionResult"])); }
static ITypeDefinition CreateGenericTypeInstance(IGenericTypeInstance gty, ITypeDefinition stubTemplate) { INamedTypeDefinition nStubTemplate = stubTemplate as INamedTypeDefinition; IEnumerable <ITypeReference> genArgs = gty.GenericArguments; GenericTypeInstanceReference stubbedGtyRef = new GenericTypeInstanceReference(stubTemplate as INamedTypeReference, genArgs, internFactory); ITypeDefinition stubbedGty = stubbedGtyRef.ResolvedType; NameToNameMap.Add(gty.FullName(), stubbedGty.FullName()); NameToTypeDefMap.Add(stubbedGty.FullName(), stubbedGty); return(stubbedGty); }
public override void BuildMethod() { AddStatement.DeclareInterceptedType(field.ContainingType.ResolvedType); Context.Log.WriteTrace(" Adding: var interceptedField = interceptedType.GetField('{0}');", field.Name.Value); Context.Block.Statements.Add( Declare.Variable<FieldInfo>("interceptedField").As( Call.VirtualMethod("GetField", typeof (string)).ThatReturns<FieldInfo>().WithArguments( Constant.Of(field.Name.Value)).On("interceptedType")) ); AddStatement.DeclareArgumentsList(); AddStatement.AddArgumentToList(Params["assignedValue"]); var actionT = SharpMockTypes.Actions[1]; var actionActualT = new GenericTypeInstanceReference(); actionActualT.GenericType = actionT; actionActualT.GenericArguments.Add(field.Type); var assignment = new AnonymousDelegate(); assignment.Type = actionActualT; assignment.ReturnType = Context.Host.PlatformType.SystemVoid; assignment.CallingConvention = CallingConvention.HasThis; var parameterDefinition = new ParameterDefinition(); parameterDefinition.Index = 0; parameterDefinition.Type = field.Type; parameterDefinition.Name = Context.Host.NameTable.GetNameFor("alteredValue"); parameterDefinition.ContainingSignature = assignment; assignment.Parameters.Add(parameterDefinition); var assignmentBody = new BlockStatement(); var assignActualField = new ExpressionStatement(); var actualField = new TargetExpression(); actualField.Type = field.Type; actualField.Definition = field; var value = new BoundExpression(); value.Type = field.Type; value.Definition = parameterDefinition; var assignValueToField = new Assignment(); assignValueToField.Source = value; assignValueToField.Target = actualField; assignValueToField.Type = field.Type; assignActualField.Expression = assignValueToField; actualField.Type = field.Type; actualField.Definition = field; assignmentBody.Statements.Add(assignActualField); assignmentBody.Statements.Add(new ReturnStatement()); assignment.Body = assignmentBody; Context.Block.Statements.Add( Declare.Variable("local_0", actionActualT).As(assignment) ); AddStatement.DeclareRegistryInterceptor(); AddStatement.DeclareInvocation(); AddStatement.SetArgumentsOnInvocation(); AddStatement.SetOriginalCallOnInvocation(); AddStatement.SetTargetOnInvocationToNull(); Context.Block.Statements.Add( Do(Call.PropertySetter<MemberInfo>("OriginalCallInfo").WithArguments("interceptedField").On("invocation")) ); AddStatement.CallShouldInterceptOnInterceptor(); AddStatement.CallInterceptOnInterceptor(); Context.Block.Statements.Add(Return.Void()); }
private void LoadSavedClasses(IMetadataHost host, string classesFN, IDictionary <string, IModule> moduleNameToModuleMap, IDictionary <string, ITypeDefinition> classNameToTypeMap, bool isEntryPt) { using (StreamReader sr = new StreamReader(classesFN)) { string line; // Processing of the lines in classesFN is stateful: The nested classes of generic type instances occur // immediately after the generic type instance itself. int remainingNestedClasses = 0; int state = 0; // 0 - non-nested processing, 1 - processing a gti with non-zero nested classes, // 2 - processing the nested classes themselves. ITypeDefinition genericTypeInst = null; IDictionary <string, ITypeDefinition> nestedTypeMap = new Dictionary <string, ITypeDefinition>(); while ((line = sr.ReadLine()) != null) { line = line.Trim(); state = 0; int ndxOfMod = line.IndexOf(" MODULE:"); int ndxOfNestingInfo = line.IndexOf(" NESTED_"); int modLen = (ndxOfNestingInfo > 0) ? ndxOfNestingInfo - ndxOfMod : line.Length - ndxOfMod; string modName = line.Substring(ndxOfMod, modLen).Split(':')[1]; string nestedClassName = ""; if (ndxOfNestingInfo > 0) { string nestingInfo = line.Substring(ndxOfNestingInfo); string[] parts = nestingInfo.Split(':'); if (parts[0].Contains("_CNT")) { if (remainingNestedClasses > 0) { Console.WriteLine("WARNING: LoadSavedClasses: Missing nested class."); } remainingNestedClasses = Int32.Parse(parts[1]); state = 1; } else { nestedClassName = parts[1]; remainingNestedClasses--; state = 2; } } string classAndArgs = line.Substring(0, ndxOfMod); int ndxOfArgs = classAndArgs.IndexOf(" ARGS:"); string className = classAndArgs.Substring(0, ndxOfArgs).Split(':')[1]; string args = classAndArgs.Substring(ndxOfArgs + 6); if (state == 0 && remainingNestedClasses > 0) { Console.WriteLine("WARNING: LoadSavedClasses: Missing nested class."); } IModule module = null; if (modName != "") { module = moduleNameToModuleMap[modName]; } ITypeDefinition clTypDefn = null; if (args.Length > 0 && module != null) { if (state == 0 || state == 1) { IList <ITypeReference> genArgs = getArgsList(args, classNameToTypeMap); if (genArgs == null) { continue; } // ASSUMPTION: for code in below line: a compiler-generated class that has the spl char '<' in its // name is never generic. int targNdx = className.IndexOf('<'); string clname = className.Substring(0, targNdx); clTypDefn = UnitHelper.FindType(host.NameTable, module, clname, genArgs.Count); GenericTypeInstanceReference gtyRef = new GenericTypeInstanceReference (clTypDefn as INamedTypeReference, genArgs, host.InternFactory); clTypDefn = gtyRef.ResolvedType; if (state == 1) { genericTypeInst = clTypDefn; FillNestedTypeMap(genericTypeInst, nestedTypeMap, "", true); } } else if (state == 2) { clTypDefn = null; if (nestedTypeMap.ContainsKey(nestedClassName)) { clTypDefn = nestedTypeMap[nestedClassName]; } if (remainingNestedClasses == 0) { nestedTypeMap.Clear(); } } } else if (className.EndsWith("[]") && module != null) { // TODO: Implement the multiple dimension case // string elemClassName = className.TrimEnd(']').TrimEnd('['); // ITypeDefinition elemTypDefn = UnitHelper.FindType(host.NameTable, module, elemClassName); // IArrayType aty = Matrix.GetMatrix(elemTypDefn, 1, host.InternFactory); // IArrayType aty1 = Matrix.GetMatrix(elemTypDefn, 1, null, null, host.InternFactory); // clTypDefn = aty; clTypDefn = null; } else { if (module != null) { clTypDefn = UnitHelper.FindType(host.NameTable, module, className); } else { clTypDefn = Dummy.TypeReference.ResolvedType; } } if (clTypDefn != null) { if (isEntryPt) { entryPtClasses.Add(clTypDefn); } classes.Add(clTypDefn); classNameToTypeMap[clTypDefn.FullName()] = clTypDefn; } } } }