Ejemplo n.º 1
0
        public void LeaveInvariant(HashSet <TKey> keys)
        {
#if DEBUG
            var liveKeys = GetLiveKeys();
            ContractHelpers.Assert(keys.IsSubsetOf(liveKeys));
#endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tells if a particular instruction is dominated by another instruction,
        /// that is, if control cannot flow to the instruction unless it first flowed
        /// through the dominator instruction.
        /// </summary>
        /// <param name="instruction">
        /// An instruction that might be dominated by <paramref name="dominator"/>.
        /// </param>
        /// <param name="dominator">
        /// An instruction that might dominate <paramref name="instruction"/>.
        /// </param>
        /// <returns>
        /// <c>true</c> if <paramref name="instruction"/> is strictly dominated by
        /// <paramref name="dominator"/> or <paramref name="instruction"/> equals
        /// <paramref name="dominator"/>; otherwise, <c>false</c>.
        /// </returns>
        public bool IsDominatedBy(NamedInstruction instruction, NamedInstruction dominator)
        {
            var graph = instruction.Block.Graph;

            ContractHelpers.Assert(graph == dominator.Block.Graph);
            return(IsDominatedBy(instruction, dominator, graph));
        }
Ejemplo n.º 3
0
        public override CompositeActivator RewriteActivator(
            TypeInfo partType,
            CompositeActivator activatorBody,
            IDictionary <string, object> partMetadata,
            IEnumerable <CompositionDependency> dependencies)
        {
            if (!ContractHelpers.IsShared(partMetadata))
            {
                return(activatorBody);
            }

            object sharingBoundaryMetadata;

            if (!partMetadata.TryGetValue(SharingBoundaryPartMetadataName, out sharingBoundaryMetadata))
            {
                sharingBoundaryMetadata = null;
            }

            var sharingBoundary = (string)sharingBoundaryMetadata;
            var sharingKey      = LifetimeContext.AllocateSharingId();

            return((c, o) =>
            {
                var scope = c.FindContextWithin(sharingBoundary);
                if (object.ReferenceEquals(scope, c))
                {
                    return scope.GetOrCreate(sharingKey, o, activatorBody);
                }
                else
                {
                    return CompositionOperation.Run(scope, (c1, o1) => c1.GetOrCreate(sharingKey, o1, activatorBody));
                }
            });
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Removes the instruction with a particular tag from
 /// this basic block. Returns a new basic block in a new
 /// control-flow graph.
 /// </summary>
 /// <param name="tag">The tag of the instruction to remove.</param>
 /// <returns>A new basic block in a new control-flow graph.</returns>
 public BasicBlock RemoveInstruction(ValueTag tag)
 {
     ContractHelpers.Assert(
         Graph.GetValueParent(tag).Tag == this.Tag,
         "Basic block does not define the instruction being removed.");
     return(Graph.RemoveInstruction(tag).GetBasicBlock(this.Tag));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets the call instruction prototype for a particular callee.
 /// </summary>
 /// <param name="callee">The method to call.</param>
 /// <param name="lookup">The method lookup strategy for the call.</param>
 /// <returns>A call instruction prototype.</returns>
 public static CallPrototype Create(IMethod callee, MethodLookup lookup)
 {
     ContractHelpers.Assert(
         lookup == MethodLookup.Static || !callee.IsStatic,
         "A static callee method cannot be resolved via virtual lookup.");
     return(instanceCache.Intern(new CallPrototype(callee, lookup)));
 }
Ejemplo n.º 6
0
        private IEnumerable <CompositionDependency> GetPartActivatorDependencies(DependencyAccessor definitionAccessor)
        {
            var partTypeAsType = _partType.AsType();

            if (_constructor == null)
            {
                foreach (var c in _partType.DeclaredConstructors.Where(ci => ci.IsPublic && !(ci.IsStatic)))
                {
                    if (_attributeContext.GetDeclaredAttribute <ImportingConstructorAttribute>(partTypeAsType, c) != null)
                    {
                        if (_constructor != null)
                        {
                            string message = SR.Format(SR.DiscoveredPart_MultipleImportingConstructorsFound, _partType);
                            throw new CompositionFailedException(message);
                        }

                        _constructor = c;
                    }
                }

                if (_constructor == null && _partType.IsGenericType)
                {
                    _constructor = GetConstructorInfoFromGenericType(_partType);
                }

                if (_constructor == null)
                {
                    _constructor = _partType.DeclaredConstructors
                                   .FirstOrDefault(ci => ci.IsPublic && !(ci.IsStatic || ci.GetParameters().Any()));
                }

                if (_constructor == null)
                {
                    string message = SR.Format(SR.DiscoveredPart_NoImportingConstructorsFound, _partType);
                    throw new CompositionFailedException(message);
                }
            }

            var cps = _constructor.GetParameters();

            for (var i = 0; i < cps.Length; ++i)
            {
                var pi   = cps[i];
                var site = new ParameterImportSite(pi);

                var importInfo = ContractHelpers.GetImportInfo(pi.ParameterType, _attributeContext.GetDeclaredAttributes(partTypeAsType, pi), site);
                if (!importInfo.AllowDefault)
                {
                    yield return(definitionAccessor.ResolveRequiredDependency(site, importInfo.Contract, true));
                }
                else
                {
                    CompositionDependency optional;
                    if (definitionAccessor.TryResolveOptionalDependency(site, importInfo.Contract, true, out optional))
                    {
                        yield return(optional);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Gets the constrained call instruction prototype for a particular callee.
 /// </summary>
 /// <param name="callee">The method to call.</param>
 /// <returns>A constrained call instruction prototype.</returns>
 public static ConstrainedCallPrototype Create(IMethod callee)
 {
     ContractHelpers.Assert(
         !callee.IsStatic,
         "Constrained calls cannot call static methods.");
     return(instanceCache.Intern(new ConstrainedCallPrototype(callee)));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates an integer value from the given integer and an integer spec.
        /// </summary>
        public IntegerConstant(BigInteger value, IntegerSpec spec)
        {
            ContractHelpers.Assert(spec != null);

            this.Value = value;
            this.Spec  = spec;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Reads out an integer spec attribute as an integer spec.
 /// </summary>
 /// <param name="attribute">The integer spec attribute to read.</param>
 /// <returns>The integer spec described by the attribute.</returns>
 public static IntegerSpec Read(IntrinsicAttribute attribute)
 {
     ContractHelpers.Assert(attribute.Name == AttributeName);
     ContractHelpers.Assert(attribute.Arguments.Count == 2);
     return(new IntegerSpec(
                ((IntegerConstant)attribute.Arguments[0]).ToInt32(),
                ((IntegerConstant)attribute.Arguments[1]).ToBoolean()));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Adds a generic parameter to this generic member.
        /// </summary>
        /// <param name="genericParameter">The generic parameter to add.</param>
        public void AddGenericParameter(IGenericParameter genericParameter)
        {
            ContractHelpers.Assert(
                object.Equals(this, genericParameter.ParentMember),
                "Generic parameters can only be added to their declaring member.");

            genericParamList.Add(genericParameter);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Casts this integer value to match the given spec.
        /// </summary>
        public IntegerConstant Cast(IntegerSpec newSpec)
        {
            var result = new IntegerConstant(newSpec.Cast(Value, Spec), newSpec);

            ContractHelpers.Assert(result.Spec.Equals(newSpec));
            AssertIsValid(result);
            return(result);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// "Normalizes" the given value, by casting it to this integer spec,
        /// from this integer spec. The result of this operation is always
        /// representible, even if the input value is not.
        /// </summary>
        public BigInteger Normalize(BigInteger value)
        {
            var result = Cast(value, this);

            AssertIsRepresentible(result);
            ContractHelpers.Assert(!IsRepresentible(value) || value.Equals(result));
            return(result);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Gets the new-object instruction prototype for a particular constructor.
 /// </summary>
 /// <param name="constructor">The constructor to initialize objects with.</param>
 /// <returns>A new-object instruction prototype.</returns>
 public static NewObjectPrototype Create(IMethod constructor)
 {
     ContractHelpers.Assert(
         constructor.IsConstructor && !constructor.IsStatic,
         "A new-object instruction prototype's constructor method " +
         "must be an instance constructor.");
     return(instanceCache.Intern(new NewObjectPrototype(constructor)));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Adds a nested type to this type.
        /// </summary>
        /// <param name="nestedType">The nested type to add.</param>
        public void AddNestedType(IType nestedType)
        {
            ContractHelpers.Assert(
                nestedType.Parent.IsType,
                "Cannot add a non-nested type as a nested type.");

            ContractHelpers.Assert(
                object.Equals(this, nestedType.Parent.Type),
                "A nested type can only be added to its defining type.");
            nestedTypeList.Add(nestedType);
        }
Ejemplo n.º 15
0
        static void AssumeInvariantTrue()
        {
            foreach (Host h in new ArrayList())
            {
                Contract.Assume(h != null);

                ContractHelpers.AssumeInvariant(h);

                Contract.Assert(h.Name != null);
            }
        }
Ejemplo n.º 16
0
        internal BasicBlock UpdateBasicBlockParameters(
            BasicBlockTag tag,
            ImmutableList <BlockParameter> parameters)
        {
            AssertContainsBasicBlock(tag);
            var oldBlock = blocks[tag];

            var newData = new BasicBlockData(
                parameters,
                oldBlock.InstructionTags,
                oldBlock.Flow);

            var oldData   = blocks[tag];
            var oldParams = oldData.Parameters;

            var newGraph = new FlowGraph(this, new BasicBlockParametersUpdate(tag));

            var paramTypeBuilder   = newGraph.blockParamTypes.ToBuilder();
            var valueParentBuilder = newGraph.valueParents.ToBuilder();

            // Remove the basic block's parameters from the value parent
            // and parameter type dictionaries.
            int oldParamCount = oldParams.Count;

            for (int i = 0; i < oldParamCount; i++)
            {
                paramTypeBuilder.Remove(oldParams[i].Tag);
                valueParentBuilder.Remove(oldParams[i].Tag);
            }

            // Add the new basic block parameters to the value parent and
            // parameter type dictionaries.
            int newParamCount = parameters.Count;

            for (int i = 0; i < newParamCount; i++)
            {
                var item = parameters[i];

                ContractHelpers.Assert(
                    !valueParentBuilder.ContainsKey(item.Tag),
                    "Value tag '" + item.Tag.Name + "' cannot appear twice in the same control-flow graph.");

                paramTypeBuilder.Add(item.Tag, item.Type);
                valueParentBuilder.Add(item.Tag, tag);
            }

            newGraph.blockParamTypes = paramTypeBuilder.ToImmutable();
            newGraph.valueParents    = valueParentBuilder.ToImmutable();
            newGraph.blocks          = newGraph.blocks.SetItem(tag, newData);

            return(new BasicBlock(newGraph, tag, newData));
        }
Ejemplo n.º 17
0
        /// <inheritdoc/>
        public override BlockFlow WithInstructions(IReadOnlyList <Instruction> instructions)
        {
            ContractHelpers.Assert(instructions.Count == 1, "Switch flow takes exactly one instruction.");
            var newSwitchValue = instructions[0];

            if (object.ReferenceEquals(newSwitchValue, SwitchValue))
            {
                return(this);
            }
            else
            {
                return(new SwitchFlow(newSwitchValue, Cases, DefaultBranch));
            }
        }
Ejemplo n.º 18
0
        /// <inheritdoc/>
        public override BlockFlow WithInstructions(IReadOnlyList <Instruction> instructions)
        {
            ContractHelpers.Assert(instructions.Count == 1, "Try flow takes exactly one instruction.");
            var insn = instructions[0];

            if (insn == Instruction)
            {
                return(this);
            }
            else
            {
                return(new TryFlow(insn, SuccessBranch, ExceptionBranch));
            }
        }
Ejemplo n.º 19
0
        /// <inheritdoc/>
        public override BlockFlow WithInstructions(IReadOnlyList <Instruction> instructions)
        {
            ContractHelpers.Assert(instructions.Count == 1, "Return flow takes exactly one instruction.");
            var newReturnValue = instructions[0];

            if (object.ReferenceEquals(newReturnValue, ReturnValue))
            {
                return(this);
            }
            else
            {
                return(new ReturnFlow(newReturnValue));
            }
        }
Ejemplo n.º 20
0
        /// <inheritdoc/>
        public override BlockFlow WithBranches(IReadOnlyList <Branch> branches)
        {
            ContractHelpers.Assert(branches.Count == 1, "Jump flow takes exactly one branch.");
            var newBranch = branches[0];

            if (object.ReferenceEquals(newBranch, Branch))
            {
                return(this);
            }
            else
            {
                return(new JumpFlow(newBranch));
            }
        }
Ejemplo n.º 21
0
        /// <inheritdoc/>
        public override BlockFlow WithBranches(IReadOnlyList <Branch> branches)
        {
            ContractHelpers.Assert(branches.Count == 2, "Try flow takes exactly two branches.");
            var success = branches[0];
            var exn     = branches[1];

            if (success == SuccessBranch && exn == ExceptionBranch)
            {
                return(this);
            }
            else
            {
                return(new TryFlow(Instruction, success, exn));
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Cleans up all buckets allocated to a particular concurrency
        /// domain.
        /// </summary>
        /// <param name="concurrencyDomain">
        /// The concurrency domain whose buckets are to be cleaned.
        /// </param>
        /// <remarks>
        /// This method is thread-safe within a single concurrency domain.
        /// </remarks>
        private void Cleanup(int concurrencyDomain)
        {
            for (int i = concurrencyDomain; i < buckets.Length; i += MaxConcurrency)
            {
                ContractHelpers.Assert(GetConcurrencyDomain(i) == concurrencyDomain);

                var  bucket   = buckets[i];
                bool wasEmpty = bucket.IsEmpty;
                bucket.Cleanup();
                if (!wasEmpty && bucket.IsEmpty)
                {
                    Interlocked.Decrement(ref initializedBucketCount);
                }
                buckets[i] = bucket;
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Casts the given unsigned integer to match this spec.
        /// </summary>
        private BigInteger CastUnsigned(BigInteger value)
        {
            // We're dealing with a positive integer, so first, we'll make
            // sure it fits in the number of bits we have.
            ContractHelpers.Assert(value.Sign >= 0);
            var remainder = BigInteger.Remainder(value, UnsignedModulus);

            if (remainder.CompareTo(MaxValue) > 0)
            {
                // We're dealing with two's complement here.
                return(BigInteger.Subtract(BigInteger.Remainder(remainder, Modulus), Modulus));
            }
            else
            {
                // Unsigned number. Just return the remainder.
                return(remainder);
            }
        }
        private static void SatisfyImportsInternal(this CompositionContext exportProvider, object objectWithLooseImports, AttributedModelProvider conventions)
        {
            if (exportProvider == null)
            {
                throw new ArgumentNullException(nameof(exportProvider));
            }
            if (objectWithLooseImports == null)
            {
                throw new ArgumentNullException(nameof(objectWithLooseImports));
            }
            if (conventions == null)
            {
                throw new ArgumentNullException(nameof(conventions));
            }

            var objType = objectWithLooseImports.GetType();

            foreach (var pi in objType.GetRuntimeProperties())
            {
                ImportInfo importInfo;
                var        site = new PropertyImportSite(pi);
                if (ContractHelpers.TryGetExplicitImportInfo(pi.PropertyType, conventions.GetDeclaredAttributes(pi.DeclaringType, pi), site, out importInfo))
                {
                    object value;
                    if (exportProvider.TryGetExport(importInfo.Contract, out value))
                    {
                        pi.SetValue(objectWithLooseImports, value);
                    }
                    else if (!importInfo.AllowDefault)
                    {
                        throw new CompositionFailedException(SR.Format(
                                                                 SR.CompositionContextExtensions_MissingDependency, pi.Name, objectWithLooseImports));
                    }
                }
            }

            var importsSatisfiedMethods = objectWithLooseImports.GetType().GetRuntimeMethods().Where(m =>
                                                                                                     m.CustomAttributes.Any(ca => ca.AttributeType == typeof(OnImportsSatisfiedAttribute)));

            foreach (var ois in importsSatisfiedMethods)
            {
                ois.Invoke(objectWithLooseImports, null);
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Creates an integer size from the given number of bits,
        /// and signedness.
        /// </summary>
        public IntegerSpec(int size, bool isSigned)
        {
            this.Size     = size;
            this.IsSigned = isSigned;

            ContractHelpers.Assert(size > 0);

            this.UnsignedModulus = BigInteger.Pow(new BigInteger((int)2), size);
            this.Modulus         = BigInteger.Pow(new BigInteger((int)2), DataSize);
            this.MaxValue        = BigInteger.Subtract(Modulus, BigInteger.One);

            if (isSigned)
            {
                this.MinValue = BigInteger.Negate(Modulus);
            }
            else
            {
                this.MinValue = BigInteger.Zero;
            }
        }
Ejemplo n.º 26
0
        /// <inheritdoc/>
        public override BlockFlow WithBranches(IReadOnlyList <Branch> branches)
        {
            int branchCount = branches.Count;
            int caseCount   = Cases.Count;

            ContractHelpers.Assert(
                branchCount == caseCount + 1,
                "Got '" + branchCount +
                "' branches when re-creating a switch statement, but expected '" +
                (caseCount + 1) + "'.");

            var newCases = new SwitchCase[caseCount];

            for (int i = 0; i < caseCount; i++)
            {
                newCases[i] = new SwitchCase(Cases[i].Values, branches[i]);
            }

            return(new SwitchFlow(
                       SwitchValue,
                       newCases,
                       branches[caseCount]));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Gets or creates a new-delegate instruction prototype.
        /// </summary>
        /// <param name="delegateType">
        /// The type of delegate produced by instances of the prototype.
        /// </param>
        /// <param name="callee">
        /// The method that is invoked when the delegates produced by instances
        /// of the prototype are called.
        /// </param>
        /// <param name="hasThisArgument">
        /// Tells if a 'this' argument is included in the delegate.
        /// </param>
        /// <param name="lookup">
        /// The method lookup strategy for the prototype.
        /// </param>
        /// <returns>A new-delegate instruction prototype.</returns>
        public static NewDelegatePrototype Create(
            IType delegateType,
            IMethod callee,
            bool hasThisArgument,
            MethodLookup lookup)
        {
            if (hasThisArgument)
            {
                ContractHelpers.Assert(
                    !callee.IsStatic || callee.Parameters.Count >= 1,
                    "A callee that is provided a 'this' argument must " +
                    "be an instance method or take at least one parameter.");
            }
            ContractHelpers.Assert(
                lookup == MethodLookup.Static || !callee.IsStatic,
                "A static callee method cannot be resolved via virtual lookup.");

            return(instanceCache.Intern(
                       new NewDelegatePrototype(
                           delegateType,
                           callee,
                           hasThisArgument,
                           lookup)));
        }
Ejemplo n.º 28
0
 /// <inheritdoc/>
 public override BlockFlow WithBranches(IReadOnlyList <Branch> branches)
 {
     ContractHelpers.Assert(branches.Count == 0, "Unreachable flow does not take any branches.");
     return(this);
 }
Ejemplo n.º 29
0
 /// <inheritdoc/>
 public override BlockFlow WithInstructions(IReadOnlyList <Instruction> instructions)
 {
     ContractHelpers.Assert(instructions.Count == 0, "Unreachable flow does not take any instructions.");
     return(this);
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Gets the 'this' argument in an instruction that conforms to
 /// this prototype.
 /// </summary>
 /// <param name="instruction">
 /// An instruction that conforms to this prototype.
 /// </param>
 /// <returns>The 'this' argument.</returns>
 public ValueTag GetThisArgument(Instruction instruction)
 {
     AssertIsPrototypeOf(instruction);
     ContractHelpers.Assert(HasThisArgument);
     return(instruction.Arguments[0]);
 }