コード例 #1
0
        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);
        }
コード例 #2
0
        /// <summary>
        /// See if a type reference refers to a type definition that is compiler generated.
        /// </summary>
        /// <param name="typeReference"></param>
        /// <returns></returns>
        internal static bool IsCompilerGenerated(ITypeReference /*!*/ typeReference)
        {
            if (AttributeHelper.Contains(typeReference.ResolvedType.Attributes, typeReference.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return(true);
            }
            IGenericTypeInstanceReference genericTypeInstanceReference = typeReference as IGenericTypeInstanceReference;

            if (genericTypeInstanceReference != null && IsCompilerGenerated(genericTypeInstanceReference.GenericType))
            {
                return(true);
            }
            ISpecializedNestedTypeReference specializedNestedType = typeReference as ISpecializedNestedTypeReference;

            if (specializedNestedType != null && IsCompilerGenerated(specializedNestedType.UnspecializedVersion))
            {
                return(true);
            }
            ISpecializedNestedTypeDefinition specializedNestedTypeDefinition = typeReference as ISpecializedNestedTypeDefinition;

            if (specializedNestedTypeDefinition != null && IsCompilerGenerated(specializedNestedTypeDefinition.UnspecializedVersion))
            {
                return(true);
            }
            INestedTypeReference nestedTypeReference = UnspecializedMethods.AsUnspecializedNestedTypeReference(typeReference);

            if (nestedTypeReference != null)
            {
                return(IsCompilerGenerated(nestedTypeReference.ContainingType));
            }
            return(false);
        }
コード例 #3
0
        public override void TraverseChildren(ITypeDefinition typeDefinition)
        {
            if (AttributeHelper.Contains(typeDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return;
            }
            if (ContractHelper.IsContractClass(this.host, typeDefinition))
            {
                return;
            }
            if (typeDefinition.IsEnum)
            {
                return;
            }
            Console.WriteLine(TypeHelper.GetTypeName(typeDefinition, NameFormattingOptions.TypeParameters));
            this.indentLevel++;
            var unit = TypeHelper.GetDefiningUnit(typeDefinition);

            if (unit != null)
            {
                var ce = this.host.GetContractExtractor(unit.UnitIdentity);
                if (ce != null)
                {
                    PrintTypeContract(ce.GetTypeContractFor(typeDefinition));
                }
            }
            base.TraverseChildren(typeDefinition);
            this.indentLevel--;
        }
コード例 #4
0
        public override void TraverseChildren(IMethodDefinition methodDefinition)
        {
            if (AttributeHelper.Contains(methodDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return;
            }
            if (ContractHelper.IsInvariantMethod(this.host, methodDefinition))
            {
                return;
            }
            if (IsGetter(methodDefinition) || IsSetter(methodDefinition))
            {
                return;
            }
            IMethodContract methodContract;

            if (this.showInherited)
            {
                methodContract = ContractHelper.GetMethodContractForIncludingInheritedContracts(this.host, methodDefinition);
            }
            else
            {
                methodContract = ContractHelper.GetMethodContractFor(this.host, methodDefinition);
            }
            Indent();
            var methodSig = MemberHelper.GetMethodSignature(methodDefinition, NameFormattingOptions.Signature | NameFormattingOptions.ParameterName | NameFormattingOptions.ParameterModifiers);

            Console.WriteLine(methodSig);
            this.indentLevel++;
            PrintMethodContract(methodContract);
            this.indentLevel--;
        }
コード例 #5
0
 public override void TraverseChildren(INestedTypeDefinition nestedTypeDefinition)
 {
     if (!this.printCompilerGeneratedMembers && AttributeHelper.Contains(nestedTypeDefinition.Attributes, nestedTypeDefinition.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
     {
         return;
     }
     PrintTypeDefinition(nestedTypeDefinition as ITypeDefinition);
 }
コード例 #6
0
ファイル: Prune.cs プロジェクト: zhuyue1314/CodeContracts
        public override void RewriteChildren(MethodDefinition method)
        {
            if (method.Attributes != null && AttributeHelper.Contains(method.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return;
            }
            if (TypeHelper.IsCompilerGenerated(method.ContainingTypeDefinition))
            {
                return;
            }
            //if (Microsoft.Cci.MutableContracts.ContractHelper.IsContractClass(this.host, method.ContainingTypeDefinition)) return; // if a method in a contract class was considered reachable, then keep its method body

            #region Add attribute with its hash
            MethodHashAttribute        mhAttr;
            List <IMetadataExpression> args;
            Contract.Assume(this.copier.OriginalFor.ContainsKey(method));
            var originalMethod = (IMethodDefinition)this.copier.OriginalFor[method];
            if (this.methodHashAttributes.TryGetValue(originalMethod, out mhAttr))
            {
                args = new List <IMetadataExpression> {
                    new MetadataConstant()
                    {
                        Type = this.systemString, Value = mhAttr.Arg1,
                    },
                    new MetadataConstant()
                    {
                        Type = this.systemInt, Value = mhAttr.Arg2,
                    },
                };
            }
            else
            {
                // add attribute that says to *not* analyze this method: it is here only because it was discovered as being called somewhere
                args = new List <IMetadataExpression> {
                    new MetadataConstant()
                    {
                        Type = this.systemString, Value = null,
                    },
                    new MetadataConstant()
                    {
                        Type = this.systemInt, Value = ((int)MethodHashAttributeFlags.ForDependenceMethod),
                    },
                };
            }
            var attr = new CustomAttribute()
            {
                Constructor = this.methodHashAttributeCtor, Arguments = args,
            };
            if (method.Attributes == null)
            {
                method.Attributes = new List <ICustomAttribute>(1);
            }
            method.Attributes.Add(attr);
            #endregion
            base.RewriteChildren(method);
        }
コード例 #7
0
        public override void RewriteChildren(Assignment assignment)
        {
            base.RewriteChildren(assignment);
            var anonDel = assignment.Source as IAnonymousDelegate;

            if (anonDel != null)
            {
                var targetField = assignment.Target.Definition as IFieldReference;
                if (targetField != null && targetField.ResolvedField.IsStatic &&
                    (AttributeHelper.Contains(targetField.ResolvedField.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute) ||
                     AttributeHelper.Contains(targetField.ResolvedField.ContainingTypeDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute)))
                {
                    var specializedTargetField = targetField as ISpecializedFieldReference;
                    if (this.delegatesCachedInFields == null)
                    {
                        this.delegatesCachedInFields = new Hashtable <IAnonymousDelegate>();
                    }
                    this.delegatesCachedInFields[targetField.InternedKey] = anonDel;
                    if (this.sourceMethodBody.privateHelperFieldsToRemove == null)
                    {
                        this.sourceMethodBody.privateHelperFieldsToRemove = new List <IFieldDefinition>();
                    }
                    if (specializedTargetField != null)
                    {
                        targetField = specializedTargetField.UnspecializedVersion;
                    }
                    this.sourceMethodBody.privateHelperFieldsToRemove.Add(targetField.ResolvedField);
                }
                return;
            }
            if (this.closures != null && this.closures.Find(assignment.Source.Type.InternedKey) != null)
            {
                var targetField = assignment.Target.Definition as IFieldReference;
                if (targetField != null)
                {
                    var specializedTargetField = targetField as ISpecializedFieldReference;
                    if (this.sourceMethodBody.privateHelperFieldsToRemove == null)
                    {
                        this.sourceMethodBody.privateHelperFieldsToRemove = new List <IFieldDefinition>();
                    }
                    if (specializedTargetField != null)
                    {
                        targetField = specializedTargetField.UnspecializedVersion;
                    }
                    this.sourceMethodBody.privateHelperFieldsToRemove.Add(targetField.ResolvedField);
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Returns true if the given field reference refers to a field that is compiler generated.
        /// </summary>
        public static bool IsCompilerGenerated(IFieldReference /*!*/ fieldReference)
        {
            var specializedFieldReference = fieldReference as ISpecializedFieldReference;

            if (specializedFieldReference != null)
            {
                return(IsCompilerGenerated(specializedFieldReference.UnspecializedVersion));
            }
            if (IsCompilerGenerated(fieldReference.ContainingType))
            {
                return(true);
            }
            var field = fieldReference.ResolvedField; //This is unfortunate, but in the case of a (specialized) field reference where the containing type is a generic type instance

            //the unspecialized field is not a definition even if the generic type template is defined in the same assembly. Resolving seems to be the only way to tell.
            return(AttributeHelper.Contains(field.Attributes, field.ContainingType.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute));
        }
コード例 #9
0
        /// <summary>
        /// See if a method definition is compiler generated, or is inside a compiler generated type.
        /// </summary>
        /// <param name="methodDefinition"></param>
        /// <returns></returns>
        internal static bool IsCompilerGenerated(IMethodDefinition /*!*/ methodDefinition)
        {
            if (AttributeHelper.Contains(methodDefinition.Attributes, methodDefinition.ContainingType.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return(true);
            }
            IGenericMethodInstance genericMethodInstance = methodDefinition as IGenericMethodInstance;

            if (genericMethodInstance != null)
            {
                return(IsCompilerGenerated(genericMethodInstance.GenericMethod.ResolvedMethod));
            }
            if (methodDefinition.ContainingType == null)
            {
                return(false);
            }
            return(IsCompilerGenerated(methodDefinition.ContainingType));
        }
コード例 #10
0
ファイル: MoveNext.cs プロジェクト: xornand/cci
        private static IMethodBody /*?*/ FirstStatementIsIteratorCreation(IMetadataHost host, ISourceMethodBody possibleIterator, INameTable nameTable, IStatement statement)
        {
            ICreateObjectInstance createObjectInstance = GetICreateObjectInstance(statement);

            if (createObjectInstance == null)
            {
                // If the first statement in the method body is not the creation of iterator closure, return a dummy.
                // Possible corner case not handled: a local is used to hold the constant value for the initial state of the closure.
                return(null);
            }
            ITypeReference closureType /*?*/        = createObjectInstance.MethodToCall.ContainingType;
            ITypeReference unspecializedClosureType = ContractHelper.Unspecialized(closureType);

            if (!AttributeHelper.Contains(unspecializedClosureType.Attributes, host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return(null);
            }
            INestedTypeReference closureTypeAsNestedTypeReference = unspecializedClosureType as INestedTypeReference;

            if (closureTypeAsNestedTypeReference == null)
            {
                return(null);
            }
            ITypeReference unspecializedClosureContainingType = ContractHelper.Unspecialized(closureTypeAsNestedTypeReference.ContainingType);

            if (closureType != null && TypeHelper.TypesAreEquivalent(possibleIterator.MethodDefinition.ContainingTypeDefinition, unspecializedClosureContainingType))
            {
                IName MoveNextName = nameTable.GetNameFor("MoveNext");
                foreach (ITypeDefinitionMember member in closureType.ResolvedType.GetMembersNamed(MoveNextName, false))
                {
                    IMethodDefinition moveNext = member as IMethodDefinition;
                    if (moveNext != null)
                    {
                        ISpecializedMethodDefinition moveNextGeneric = moveNext as ISpecializedMethodDefinition;
                        if (moveNextGeneric != null)
                        {
                            moveNext = moveNextGeneric.UnspecializedVersion.ResolvedMethod;
                        }
                        return(moveNext.Body);
                    }
                }
            }
            return(null);
        }
コード例 #11
0
ファイル: ClosureFinder.cs プロジェクト: pkunze/visualmutator
        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);
            }
        }
コード例 #12
0
        internal int Run()
        {
            SecurityKeepOptions securityKeepOptions = options.onlySecurityTransparent ? SecurityKeepOptions.OnlyNonCritical : SecurityKeepOptions.All;

            if (options.doBreak)
            {
                System.Diagnostics.Debugger.Launch();
            }

            string assemblyName = null;

            if (options.GeneralArguments.Count == 1)
            {
                assemblyName = options.GeneralArguments[0];
            }
            if (assemblyName == null)
            {
                errorLogger("Must specify an input file.");
                return(1);
            }

            using (var host = new AsmMetaHostEnvironment(options.libPaths.ToArray(), options.searchGAC))
            {
                foreach (var p in options.resolvedPaths)
                {
                    host.AddResolvedPath(p);
                }

                IAssembly /*?*/ assembly = host.LoadUnitFrom(assemblyName) as IAssembly;
                if (assembly == null || assembly is Dummy)
                {
                    errorLogger(assemblyName + " is not a PE file containing a CLR assembly, or an error occurred when loading it.");
                    return(1);
                }

                var rewrittenAttribute = CreateTypeReference(host, assembly, "System.Diagnostics.Contracts.RuntimeContractsAttribute");
                if (AttributeHelper.Contains(assembly.Attributes, rewrittenAttribute))
                {
                    errorLogger(assemblyName + " is already rewritten, cannot generate a reference assembly from it.");
                    return(1);
                }

                if (options.backwardCompatibility)
                { // redundant because RemoveMethodBodies.ctor also does this when the flag is set
                    options.whatToEmit     = KeepOptions.ExtVis;
                    options.emitAttributes = false;
                }

                PdbReader /*?*/ pdbReader = null;
                if (options.includeSourceTextInContract)
                { // No point getting the PDB file unless we want to use it for source text
                    string pdbFile = Path.ChangeExtension(assembly.Location, "pdb");
                    if (File.Exists(pdbFile))
                    {
                        using (var pdbStream = File.OpenRead(pdbFile))
                        {
                            pdbReader = new PdbReader(pdbStream, host);
                        }
                    }
                    else
                    {
                        errorLogger("Could not load the PDB file for the assembly '" + assembly.Name.Value + "' . Source text will not be preserved in the reference assembly. Proceeding anyway.");
                    }
                }
                using (pdbReader)
                {
                    // We might be working on the assembly that defines the contract class and/or the type System.Void.
                    // (Or any other platform type!)
                    // But the host.PlatformType object has not been duplicated, so its properties will continue to be references to the immutable ones.
                    // This is OK, except if the assembly is being renamed.
                    this.compilerGeneratedAttributeType = host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute;
                    this.contractClassType   = host.PlatformType.SystemDiagnosticsContractsContract;
                    this.systemAttributeType = host.PlatformType.SystemAttribute;
                    this.systemBooleanType   = host.PlatformType.SystemBoolean;
                    this.systemStringType    = host.PlatformType.SystemString;
                    this.systemObjectType    = host.PlatformType.SystemObject;
                    this.systemVoidType      = host.PlatformType.SystemVoid;
                    //new FindPlatformTypes(this).Traverse(assembly); //update the above fields if any of those types are defined in mutable

                    Assembly mutable = new MetadataDeepCopier(host).Copy(assembly);

                    #region Rename the assembly in a separate pass because things done in later passes depend on interned keys that are computed based (in part) on the assembly identity

                    if (options.rename)
                    {
                        if (options.output != null)
                        {
                            mutable.Name = host.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(options.output));
                        }
                        else
                        {
                            mutable.Name = host.NameTable.GetNameFor(assembly.Name.Value + ".Contracts");
                        }
                        mutable.ModuleName = mutable.Name;
                        mutable.Kind       = ModuleKind.DynamicallyLinkedLibrary;
                        mutable            = (Assembly)RenameAssembly.ReparentAssemblyIdentity(host, assembly.AssemblyIdentity, mutable.AssemblyIdentity, mutable);
                    }

                    #endregion Rename the assembly in a separate pass because things done in later passes depend on interned keys that are computed based (in part) on the assembly identity
                    if (!options.renameOnly)
                    {
                        if (options.contracts)
                        {
                            if (options.rename)
                            {
                                // We might be working on the assembly that defines the contract class and/or the type System.Void.
                                // (Or any other platform type!)
                                // But the host.PlatformType object has not been duplicated, so its properties will continue to be references to the immutable ones.
                                // This is OK, except if the assembly is being renamed.

                                var systemNamespace = GetFirstMatchingNamespace(host, mutable.UnitNamespaceRoot, "System");
                                if (systemNamespace != null)
                                {
                                    var typeDefinition = GetFirstMatchingTypeDefinition(host, systemNamespace, "Attribute");
                                    if (typeDefinition != null)
                                    {
                                        this.systemAttributeType = typeDefinition;
                                    }
                                    typeDefinition = GetFirstMatchingTypeDefinition(host, systemNamespace, "Boolean");
                                    if (typeDefinition != null)
                                    {
                                        this.systemBooleanType = typeDefinition;
                                    }
                                    typeDefinition = GetFirstMatchingTypeDefinition(host, systemNamespace, "Object");
                                    if (typeDefinition != null)
                                    {
                                        this.systemObjectType = typeDefinition;
                                    }
                                    typeDefinition = GetFirstMatchingTypeDefinition(host, systemNamespace, "String");
                                    if (typeDefinition != null)
                                    {
                                        this.systemStringType = typeDefinition;
                                    }
                                    typeDefinition = GetFirstMatchingTypeDefinition(host, systemNamespace, "Void");
                                    if (typeDefinition != null)
                                    {
                                        this.systemVoidType = typeDefinition;
                                    }

                                    var systemRuntimeNamespace = GetFirstMatchingNamespace(host, systemNamespace, "Runtime");
                                    if (systemRuntimeNamespace != null)
                                    {
                                        var systemRuntimeCompilerServicesNamespace = GetFirstMatchingNamespace(host, systemRuntimeNamespace, "CompilerServices");
                                        if (systemRuntimeCompilerServicesNamespace != null)
                                        {
                                            typeDefinition = GetFirstMatchingTypeDefinition(host, systemRuntimeCompilerServicesNamespace, "CompilerGeneratedAttribute");
                                            if (typeDefinition != null)
                                            {
                                                this.compilerGeneratedAttributeType = typeDefinition;
                                            }
                                        }
                                    }

                                    var systemDiagnosticsNamespace = GetFirstMatchingNamespace(host, systemNamespace, "Diagnostics");
                                    if (systemDiagnosticsNamespace != null)
                                    {
                                        var systemDiagnosticsContractsNamespace = GetFirstMatchingNamespace(host, systemDiagnosticsNamespace, "Contracts");
                                        if (systemDiagnosticsContractsNamespace != null)
                                        {
                                            typeDefinition = GetFirstMatchingTypeDefinition(host, systemDiagnosticsContractsNamespace, "Contract");
                                            if (typeDefinition != null)
                                            {
                                                this.contractClassType = typeDefinition;
                                            }
                                        }
                                    }
                                }
                            }

                            mutable = AsmMetaRewriter.RewriteModule(host,
                                                                    pdbReader,
                                                                    mutable,
                                                                    contractClassType,
                                                                    compilerGeneratedAttributeType,
                                                                    systemAttributeType,
                                                                    systemBooleanType,
                                                                    systemObjectType,
                                                                    systemStringType,
                                                                    systemVoidType
                                                                    ) as Assembly;
                        }
                        #region Delete things that are not to be kept
                        if (options.backwardCompatibility || options.whatToEmit != KeepOptions.All || !options.emitAttributes || (options.attrs != null && 0 < options.attrs.Count))
                        {
                            DeleteThings thinner = new DeleteThings(host, options.whatToEmit, securityKeepOptions, options.emitAttributes, options.attrs.ToArray(), options.backwardCompatibility);
                            if (securityKeepOptions == SecurityKeepOptions.OnlyNonCritical && host.platformType.SystemSecuritySecurityCriticalAttribute.ResolvedType is Dummy)
                            {
                                errorLogger("You asked to remove security critical methods, but the version of mscorlib doesn't support the SecurityCriticalAttribute.");
                                return(1);
                            }
                            thinner.RewriteChildren(mutable);
                            #region Fix up dangling references to things that were deleted
                            FixUpReferences patcher = new FixUpReferences(host, thinner.WhackedMethods, thinner.WhackedTypes);
                            patcher.RewriteChildren(mutable);
                            #endregion Fix up dangling references to things that were deleted
                        }
                        #endregion Delete things that are not to be kept
                        #region Output is always a dll, so mark the assembly as that
                        mutable.EntryPoint = Dummy.MethodReference;
                        mutable.Kind       = ModuleKind.DynamicallyLinkedLibrary;
                        #endregion Output is always a dll, so mark the assembly as that
                    }

                    assembly = mutable;

                    string outputPath;
                    if (options.output != null) // user specified, they'd better make sure it doesn't conflict with anything
                    {
                        outputPath = options.output;
                    }
                    else if (options.rename) // A.dll ==> A.Contracts.dll (Always! Even if the input is an exe!)
                    {
                        outputPath = assembly.Name.Value + ".dll";
                    }
                    else // A.dll ==> A.dll.meta
                    {
                        outputPath = assembly.Name.Value + Path.GetExtension(assemblyName) + ".meta";
                    }
                    // NB: Do *not* pass a pdbWriter to WritePeToStream. No need to create a PDB file and if
                    // it is provided, then it might find things (like constants in a scope) that don't get
                    // visited and so don't have any type references modified as they should be.
                    using (var outputFile = File.Create(outputPath))
                    {
                        if (pdbReader != null && options.writePDB)
                        {
                            using (var pdbWriter = new PdbWriter(Path.ChangeExtension(outputPath, "pdb"), pdbReader))
                            {
                                // Need to not pass in a local scope provider until such time as we have one that will use the mutator
                                // to remap things (like the type of a scope constant) from the original assembly to the mutated one.
                                PeWriter.WritePeToStream(assembly, host, outputFile, pdbReader, null /*pdbReader*/, pdbWriter);
                            }
                        }
                        else
                        {
                            PeWriter.WritePeToStream(assembly, host, outputFile);
                        }
                    }
                }
            }
            return(0); // success
        }
コード例 #13
0
 private bool IsContractClass(ITypeDefinition typeDefinition)
 {
     Contract.Requires(typeDefinition != null);
     return(AttributeHelper.Contains(typeDefinition.Attributes, ContractClassForAttribute));
 }
コード例 #14
0
 private bool IsPure(IMethodDefinition methodDefinition)
 {
     Contract.Requires(methodDefinition != null);
     return(AttributeHelper.Contains(methodDefinition.Attributes, PureAttribute));
 }
コード例 #15
0
ファイル: FieldSourceEmitter.cs プロジェクト: xornand/cci
        public override void TraverseChildren(IFieldDefinition fieldDefinition)
        {
            if (fieldDefinition.ContainingType.IsEnum && fieldDefinition.IsRuntimeSpecial && fieldDefinition.IsSpecialName)
            {
                return; // implicit value field of an enum
            }
            if (!this.printCompilerGeneratedMembers &&
                AttributeHelper.Contains(fieldDefinition.Attributes, fieldDefinition.Type.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return; // eg. a cached anonymous delegate - may have invalid symbols
            }
            foreach (var e in fieldDefinition.ContainingTypeDefinition.Events)
            {
                if (e.Name == fieldDefinition.Name)
                {
                    return; // field is probably the implicit delegate backing the event
                }
            }

            // Figure out if this is a special fixed buffer field
            ICustomAttribute fixedBufferAttr = Utils.FindAttribute(fieldDefinition.Attributes, SpecialAttribute.FixedBuffer);

            if (fixedBufferAttr == null)
            {
                PrintAttributes(fieldDefinition);
            }

            if (fieldDefinition.ContainingTypeDefinition.Layout == LayoutKind.Explicit)
            {
                PrintPseudoCustomAttribute(fieldDefinition, "System.Runtime.InteropServices.FieldOffset", fieldDefinition.Offset.ToString(), true, null);
            }

            PrintToken(VBToken.Indent);

            if (fieldDefinition.IsCompileTimeConstant && fieldDefinition.ContainingType.IsEnum)
            {
                PrintFieldDefinitionEnumValue(fieldDefinition);
            }
            else
            {
                PrintFieldDefinitionVisibility(fieldDefinition);
                PrintFieldDefinitionModifiers(fieldDefinition);

                if (fixedBufferAttr == null)
                {
                    PrintToken(VBToken.Space);
                    PrintFieldDefinitionName(fieldDefinition);
                    if (fieldDefinition.IsCompileTimeConstant)
                    {
                        sourceEmitterOutput.Write(" = ");
                        PrintFieldDefinitionValue(fieldDefinition);
                    }
                }
                else
                {
                    PrintFieldDefinitionFixedBuffer(fieldDefinition, fixedBufferAttr);
                }
                PrintToken(VBToken.Space);
                PrintToken(VBToken.As);
                PrintToken(VBToken.Space);
                PrintFieldDefinitionType(fieldDefinition);
                //PrintToken(VBToken.Semicolon);
            }
        }
コード例 #16
0
 private bool IsCompilerGenerated(IMethodDefinition methodDefinition)
 {
     return(AttributeHelper.Contains(methodDefinition.Attributes, _host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute));
 }
コード例 #17
0
 // Does NOT include methods marked with SecuritySafeCriticalAttribute.
 private bool IsSecurityCritical(ITypeDefinitionMember member)
 {
     return(AttributeHelper.Contains(member.Attributes, this.asmMetaHostEnvironment.platformType.SystemSecuritySecurityCriticalAttribute));
 }
コード例 #18
0
        public override void TraverseChildren(IMethodDefinition methodDefinition)
        {
            if (!this.printCompilerGeneratedMembers)
            {
                if (methodDefinition.IsConstructor && methodDefinition.ParameterCount == 0 &&
                    AttributeHelper.Contains(methodDefinition.Attributes, methodDefinition.Type.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
                {
                    return;
                }

                // Skip if this is a method generated for use by a property or event
                foreach (var p in methodDefinition.ContainingTypeDefinition.Properties)
                {
                    if ((p.Getter != null && p.Getter.ResolvedMethod == methodDefinition) ||
                        (p.Setter != null && p.Setter.ResolvedMethod == methodDefinition))
                    {
                        return;
                    }
                }
                foreach (var e in methodDefinition.ContainingTypeDefinition.Events)
                {
                    if ((e.Adder != null && e.Adder.ResolvedMethod == methodDefinition) ||
                        (e.Remover != null && e.Remover.ResolvedMethod == methodDefinition))
                    {
                        return;
                    }
                }

                if (AttributeHelper.Contains(methodDefinition.Attributes, methodDefinition.Type.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
                {
                    return; // eg. an iterator helper - may have invalid identifier name
                }
            }

            // Cctors should probably be outputted in some cases
            if (methodDefinition.IsStaticConstructor)
            {
                return;
            }

            foreach (var ma in SortAttributes(methodDefinition.Attributes))
            {
                if (Utils.GetAttributeType(ma) != SpecialAttribute.Extension)
                {
                    PrintAttribute(methodDefinition, ma, true, null);
                }
            }

            foreach (var ra in SortAttributes(methodDefinition.ReturnValueAttributes))
            {
                PrintAttribute(methodDefinition, ra, true, "return");
            }

            PrintToken(CSharpToken.Indent);

            PrintMethodDefinitionVisibility(methodDefinition);
            PrintMethodDefinitionModifiers(methodDefinition);

            bool conversion = IsConversionOperator(methodDefinition);

            if (!conversion)
            {
                PrintMethodDefinitionReturnType(methodDefinition);
                if (!methodDefinition.IsConstructor && !IsDestructor(methodDefinition))
                {
                    PrintToken(CSharpToken.Space);
                }
            }
            PrintMethodDefinitionName(methodDefinition);
            if (conversion)
            {
                PrintMethodDefinitionReturnType(methodDefinition);
            }

            if (methodDefinition.IsGeneric)
            {
                Traverse(methodDefinition.GenericParameters);
            }
            Traverse(methodDefinition.Parameters);
            if (methodDefinition.IsGeneric)
            {
                PrintConstraints(methodDefinition.GenericParameters);
            }
            if (!methodDefinition.IsAbstract && !methodDefinition.IsExternal)
            {
                Traverse(methodDefinition.Body);
            }
            else
            {
                PrintToken(CSharpToken.Semicolon);
            }
        }