Merge pull request #558 from mkorkalo/master
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / ContractHelper.cs
1 //
2 // ContractHelper.cs
3 //
4 // Authors:
5 //    Chris Bacon (chrisbacon76@gmail.com)
6 //
7 // Copyright 2010 Novell (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_4_5
30
31 using System;
32 using System.Text;
33 using System.Reflection;
34 using System.Runtime.ConstrainedExecution;
35 using System.Diagnostics.Contracts;
36 using System.Diagnostics;
37
38 namespace System.Runtime.CompilerServices
39 {
40         public static class ContractHelper
41         {
42                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
43                 [DebuggerNonUserCode]
44                 public static string RaiseContractFailedEvent (ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
45                 {
46
47                         StringBuilder msg = new StringBuilder (60);
48                         switch (failureKind) {
49                         case ContractFailureKind.Assert:
50                                 msg.Append ("Assertion failed");
51                                 break;
52                         case ContractFailureKind.Assume:
53                                 msg.Append ("Assumption failed");
54                                 break;
55                         case ContractFailureKind.Invariant:
56                                 msg.Append ("Invariant failed");
57                                 break;
58                         case ContractFailureKind.Postcondition:
59                                 msg.Append ("Postcondition failed");
60                                 break;
61                         case ContractFailureKind.PostconditionOnException:
62                                 msg.Append ("Postcondition failed after throwing an exception");
63                                 break;
64                         case ContractFailureKind.Precondition:
65                                 msg.Append ("Precondition failed");
66                                 break;
67                         default:
68                                 throw new NotSupportedException ("Not supported: " + failureKind);
69                         }
70                         if (conditionText != null) {
71                                 msg.Append (": ");
72                                 msg.Append (conditionText);
73                         } else {
74                                 msg.Append ('.');
75                         }
76                         if (userMessage != null) {
77                                 msg.Append ("  ");
78                                 msg.Append (userMessage);
79                         }
80                         string msgString = msg.ToString ();
81
82                         Exception handlerException = null;
83                         bool unwind = false, handled = false;
84
85                         var contractFailed = Contract.InternalContractFailedEvent;
86                         if (contractFailed != null) {
87                                 // Execute all event handlers
88                                 var handlers = contractFailed.GetInvocationList ();
89                                 var e = new ContractFailedEventArgs (failureKind, msgString, conditionText, innerException);
90                                 foreach (var handler in handlers) {
91                                         try {
92                                                 handler.DynamicInvoke (null, e);
93                                         } catch (Exception ex) {
94                                                 e.SetUnwind ();
95                                                 // If multiple handlers throw an exception then the specification states that it
96                                                 // is undetermined which one becomes the InnerException.
97                                                 handlerException = ex.InnerException;
98                                         }
99                                 }
100                                 unwind = e.Unwind;
101                                 handled = e.Handled;
102                         }
103
104                         if (unwind) {
105                                 Exception ex = innerException ?? handlerException;
106                                 throw new ContractException (msgString, failureKind, conditionText, userMessage, ex);
107                         }
108
109                         return handled ? null : msgString;
110                 }
111
112                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
113                 [DebuggerNonUserCode]
114                 public static void TriggerFailure (ContractFailureKind kind, string displayMessage, string userMessage, string conditionText, Exception innerException)
115                 {
116                         StringBuilder msg = new StringBuilder (50);
117
118                         if (conditionText != null) {
119                                 msg.Append ("Expression: ");
120                                 msg.AppendLine (conditionText);
121                         }
122                         msg.Append ("Description: ");
123                         if (displayMessage != null) {
124                                 msg.Append (displayMessage);
125                         }
126                         if (Environment.UserInteractive) {
127                                 // FIXME: This should trigger an assertion.
128                                 // But code will never get here at the moment, as Environment.UserInteractive currently
129                                 // always returns false.
130                                 throw new ContractShouldAssertException (msg.ToString ());
131                         } else {
132                                 // Note that FailFast() currently throws a NotImplementedException()
133                                 Environment.FailFast(msg.ToString()/*, new ExecutionEngineException()*/);
134                         }
135                 }
136
137         }
138
139 }
140
141 #endif