1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using Microsoft
.Scripting
.Ast
;
19 using System
.Linq
.Expressions
;
25 using System
.Collections
.Generic
;
26 using System
.Diagnostics
;
28 namespace System
.Dynamic
.Utils
{
30 // Will be replaced with CLRv4 managed contracts
31 internal static class ContractUtils
{
33 internal static Exception Unreachable
{
35 Debug
.Assert(false, "Unreachable");
36 return new InvalidOperationException("Code supposed to be unreachable");
40 internal static void Requires(bool precondition
) {
42 throw new ArgumentException(Strings
.MethodPreconditionViolated
);
46 internal static void Requires(bool precondition
, string paramName
) {
47 Debug
.Assert(!string.IsNullOrEmpty(paramName
));
50 throw new ArgumentException(Strings
.InvalidArgumentValue
, paramName
);
54 internal static void RequiresNotNull(object value, string paramName
) {
55 Debug
.Assert(!string.IsNullOrEmpty(paramName
));
58 throw new ArgumentNullException(paramName
);
62 internal static void RequiresNotEmpty
<T
>(ICollection
<T
> collection
, string paramName
) {
63 RequiresNotNull(collection
, paramName
);
64 if (collection
.Count
== 0) {
65 throw new ArgumentException(Strings
.NonEmptyCollectionRequired
, paramName
);
70 /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
72 /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
73 /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
74 internal static void RequiresArrayRange
<T
>(IList
<T
> array
, int offset
, int count
, string offsetName
, string countName
) {
75 Debug
.Assert(!string.IsNullOrEmpty(offsetName
));
76 Debug
.Assert(!string.IsNullOrEmpty(countName
));
77 Debug
.Assert(array
!= null);
79 if (count
< 0) throw new ArgumentOutOfRangeException(countName
);
80 if (offset
< 0 || array
.Count
- offset
< count
) throw new ArgumentOutOfRangeException(offsetName
);
84 /// Requires the array and all its items to be non-null.
86 internal static void RequiresNotNullItems
<T
>(IList
<T
> array
, string arrayName
) {
87 Debug
.Assert(arrayName
!= null);
88 RequiresNotNull(array
, arrayName
);
90 for (int i
= 0; i
< array
.Count
; i
++) {
91 if (array
[i
] == null) {
92 throw new ArgumentNullException(string.Format(System
.Globalization
.CultureInfo
.CurrentCulture
, "{0}[{1}]", arrayName
, i
));