Exemple #1
0
 public MyILRewriter(IMetadataHost host, ILocalScopeProvider localScopeProvider, ISourceLocationProvider sourceLocationProvider)
     : base(host, localScopeProvider, sourceLocationProvider)
 {
     #region Get reference to Console.WriteLine
     var nameTable            = host.NameTable;
     var platformType         = host.PlatformType;
     var systemString         = platformType.SystemString;
     var systemVoid           = platformType.SystemVoid;
     var SystemDotConsoleType =
         new Microsoft.Cci.Immutable.NamespaceTypeReference(
             host,
             systemString.ContainingUnitNamespace,
             nameTable.GetNameFor("Console"),
             0, false, false, true, PrimitiveTypeCode.NotPrimitive);
     this.consoleDotWriteLine = new Microsoft.Cci.MethodReference(
         host, SystemDotConsoleType,
         CallingConvention.Default,
         systemVoid,
         nameTable.GetNameFor("WriteLine"),
         0, systemString);
     #endregion Get reference to Console.WriteLine
 }
Exemple #2
0
 public ILMutator(IMetadataHost host, PdbReader /*?*/ pdbReader)
     : base(host)
 {
     this.pdbReader = pdbReader;
     #region Get reference to Console.WriteLine
     var nameTable            = host.NameTable;
     var platformType         = host.PlatformType;
     var systemString         = platformType.SystemString;
     var systemVoid           = platformType.SystemVoid;
     var SystemDotConsoleType =
         new Microsoft.Cci.Immutable.NamespaceTypeReference(
             host,
             systemString.ContainingUnitNamespace,
             nameTable.GetNameFor("Console"),
             0, false, false, PrimitiveTypeCode.NotPrimitive);
     this.consoleDotWriteLine = new Microsoft.Cci.MethodReference(
         this.host, SystemDotConsoleType,
         CallingConvention.Default,
         systemVoid,
         nameTable.GetNameFor("WriteLine"),
         0, systemString);
     #endregion Get reference to Console.WriteLine
 }
Exemple #3
0
        /// <summary>
        ///     If the <paramref name="typeDefinition" /> has a type contract, generate a
        ///     contract invariant method and add it to the Methods of the <paramref name="typeDefinition" />.
        /// </summary>
        protected void VisitTypeDefinition(ITypeDefinition typeDefinition, ITypeContract typeContract)
        {
            if (typeContract == null)
            {
                return;
            }

            #region Define the method

            var statements = new List <IStatement>();
            var methodBody = new SourceMethodBody(host)
            {
                LocalsAreZeroed = true,
                Block           = new BlockStatement {
                    Statements = statements
                }
            };
            var attributes = new List <ICustomAttribute>();
            var m          = new MethodDefinition
            {
                Attributes               = attributes,
                Body                     = methodBody,
                CallingConvention        = CallingConvention.HasThis,
                ContainingTypeDefinition = typeDefinition,
                InternFactory            = host.InternFactory,
                IsStatic                 = false,
                Name                     = host.NameTable.GetNameFor("$InvariantMethod$"),
                Type                     = systemVoid,
                Visibility               = TypeMemberVisibility.Private
            };
            methodBody.MethodDefinition = m;

            #region Add calls to Contract.Invariant

            foreach (var inv in typeContract.Invariants)
            {
                var methodCall = new MethodCall
                {
                    Arguments = new List <IExpression> {
                        inv.Condition
                    },
                    IsStaticCall = true,
                    MethodToCall = contractProvider.ContractMethods.Invariant,
                    Type         = systemVoid,
                    Locations    = new List <ILocation>(inv.Locations)
                };
                var es = new ExpressionStatement
                {
                    Expression = methodCall
                };
                statements.Add(es);
            }
            statements.Add(new ReturnStatement());

            #endregion Add calls to Contract.Invariant

            #region Add [ContractInvariantMethod]

            var contractInvariantMethodType = new NamespaceTypeReference(
                host,
                host.PlatformType.SystemDiagnosticsContractsContract.ContainingUnitNamespace,
                host.NameTable.GetNameFor("ContractInvariantMethodAttribute"),
                0,
                false,
                false,
                true,
                PrimitiveTypeCode.NotPrimitive
                );
            var contractInvariantMethodCtor = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                CallingConvention     = CallingConvention.HasThis,
                ContainingType        = contractInvariantMethodType,
                GenericParameterCount = 0,
                InternFactory         = host.InternFactory,
                Name = host.NameTable.Ctor,
                Type = host.PlatformType.SystemVoid
            };
            var contractInvariantMethodAttribute = new CustomAttribute();
            contractInvariantMethodAttribute.Constructor = contractInvariantMethodCtor;
            attributes.Add(contractInvariantMethodAttribute);

            #endregion Add [ContractInvariantMethod]

            var namedTypeDefinition = (NamedTypeDefinition)typeDefinition;

            var newMethods = new List <IMethodDefinition>(namedTypeDefinition.Methods == null ? 1 : namedTypeDefinition.Methods.Count() + 1);
            if (namedTypeDefinition.Methods != null)
            {
                foreach (var meth in namedTypeDefinition.Methods)
                {
                    if (!ContractHelper.IsInvariantMethod(host, meth))
                    {
                        newMethods.Add(meth);
                    }
                }
            }
            namedTypeDefinition.Methods = newMethods;
            namedTypeDefinition.Methods.Add(m);

            #endregion Define the method
        }
 public ILMutator(IMetadataHost host, PdbReader/*?*/ pdbReader)
   : base(host) {
   this.pdbReader = pdbReader;
   #region Get reference to Console.WriteLine
   var nameTable = host.NameTable;
   var platformType = host.PlatformType;
   var systemString = platformType.SystemString;
   var systemVoid = platformType.SystemVoid;
   var SystemDotConsoleType =
     new Microsoft.Cci.Immutable.NamespaceTypeReference(
       host,
       systemString.ContainingUnitNamespace,
       nameTable.GetNameFor("Console"),
       0, false, false, PrimitiveTypeCode.NotPrimitive);
   this.consoleDotWriteLine = new Microsoft.Cci.MethodReference(
     this.host, SystemDotConsoleType,
     CallingConvention.Default,
     systemVoid,
     nameTable.GetNameFor("WriteLine"),
     0, systemString);
   #endregion Get reference to Console.WriteLine
 }
Exemple #5
0
 public MyILRewriter(IMetadataHost host, ILocalScopeProvider localScopeProvider, ISourceLocationProvider sourceLocationProvider)
   : base(host, localScopeProvider, sourceLocationProvider) {
   #region Get reference to Console.WriteLine
   var nameTable = host.NameTable;
   var platformType = host.PlatformType;
   var systemString = platformType.SystemString;
   var systemVoid = platformType.SystemVoid;
   var SystemDotConsoleType =
     new Microsoft.Cci.Immutable.NamespaceTypeReference(
       host,
       systemString.ContainingUnitNamespace,
       nameTable.GetNameFor("Console"),
       0, false, false, true, PrimitiveTypeCode.NotPrimitive);
   this.consoleDotWriteLine = new Microsoft.Cci.MethodReference(
     host, SystemDotConsoleType,
     CallingConvention.Default,
     systemVoid,
     nameTable.GetNameFor("WriteLine"),
     0, systemString);
   #endregion Get reference to Console.WriteLine
 }