importing messaging-2008 branch to trunk [continued]
[mono.git] / mcs / tools / mdoc / Mono.Documentation / exceptions.cs
1 //
2 // Mono.Documentation/exceptions.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2008 Novell, Inc.
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 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Linq;
32
33 using Mono.Cecil;
34 using Mono.Cecil.Cil;
35
36 namespace Mono.Documentation {
37
38         [Flags]
39         public enum ExceptionLocations {
40                 Member              = 0x0,
41                 Assembly            = 0x1,
42                 DependentAssemblies = 0x2,
43                 All = Assembly | DependentAssemblies
44         }
45          
46         public class ExceptionSources {
47                 internal ExceptionSources (TypeReference exception)
48                 {
49                         Exception = exception;
50                         SourcesList = new List<IMemberReference> ();
51                         Sources = new ReadOnlyCollection<IMemberReference> (SourcesList);
52                 }
53
54                 public TypeReference Exception { get; private set; }
55                 public ReadOnlyCollection<IMemberReference> Sources { get; private set; }
56                 internal List<IMemberReference> SourcesList;
57         }
58
59
60         public class ExceptionLookup {
61
62                 SlashDocMemberFormatter xdoc = new SlashDocMemberFormatter ();
63
64                 // xdoc(MemberRef) -> xdoc(TypeRef) -> ExceptionSource
65                 //   where ExceptionSource.Exception == xdoc(TypeRef)
66                 Dictionary<string, Dictionary<string, ExceptionSources>> db = new Dictionary<string, Dictionary<string, ExceptionSources>> ();
67
68                 ExceptionLocations locations;
69
70                 public ExceptionLookup (ExceptionLocations locations)
71                 {
72                         this.locations = locations;
73                 }
74
75                 public IEnumerable<ExceptionSources> this [IMemberReference member] {
76                         get {
77                                 if (member == null)
78                                         throw new ArgumentNullException ("member");
79                                 string memberDecl = xdoc.GetDeclaration (member.GetDefinition ());
80                                 Dictionary<string, ExceptionSources> e;
81                                 if (!db.TryGetValue (memberDecl, out e)) {
82                                         e = new Dictionary<string, ExceptionSources> ();
83                                         var bodies = GetMethodBodies (member);
84                                         foreach (var body in bodies) {
85                                                 if (body == null)
86                                                         continue;
87                                                 FillExceptions (body, e);
88                                         }
89                                         db.Add (memberDecl, e);
90                                 }
91                                 return e.Values;
92                         }
93                 }
94
95                 MethodBody[] GetMethodBodies (IMemberReference member)
96                 {
97                         if (member is MethodReference) {
98                                 return new[]{ ((MethodDefinition) member.GetDefinition ()).Body };
99                         }
100                         if (member is PropertyReference) {
101                                 PropertyDefinition prop = (PropertyDefinition) member.GetDefinition ();
102                                 return new[]{
103                                         prop.GetMethod != null ? prop.GetMethod.Body : null,
104                                         prop.SetMethod != null ? prop.SetMethod.Body : null,
105                                 };
106                         }
107                         if (member is FieldReference)
108                                 return new MethodBody[]{};
109                         if (member is EventReference) {
110                                 EventDefinition ev = (EventDefinition) member.GetDefinition ();
111                                 return new[]{
112                                         ev.AddMethod != null ? ev.AddMethod.Body : null,
113                                         ev.InvokeMethod != null ? ev.InvokeMethod.Body : null, 
114                                         ev.RemoveMethod != null ? ev.RemoveMethod.Body : null,
115                                 };
116                         }
117                         throw new NotSupportedException ("Unsupported member type: " + member.GetType().FullName);
118                 }
119
120                 void FillExceptions (MethodBody body, Dictionary<string, ExceptionSources> exceptions)
121                 {
122                         for (int i = 0; i < body.Instructions.Count; ++i) {
123                                 Instruction instruction = body.Instructions [i];
124                                 switch (instruction.OpCode.Code) {
125                                         case Code.Call:
126                                         case Code.Callvirt: {
127                                                 if ((locations & ExceptionLocations.Assembly) == 0 && 
128                                                                 (locations & ExceptionLocations.DependentAssemblies) == 0)
129                                                         break;
130                                                 IMemberReference memberRef = ((IMemberReference) instruction.Operand);
131                                                 if (((locations & ExceptionLocations.Assembly) != 0 && 
132                                                                         body.Method.DeclaringType.Scope.Name == memberRef.DeclaringType.Scope.Name) ||
133                                                                 ((locations & ExceptionLocations.DependentAssemblies) != 0 && 
134                                                                         body.Method.DeclaringType.Scope.Name != memberRef.DeclaringType.Scope.Name)) {
135                                                         IEnumerable<ExceptionSources> memberExceptions = this [memberRef];
136                                                         AddExceptions (body, instruction, 
137                                                                         memberExceptions.Select (es => es.Exception),
138                                                                         memberExceptions.SelectMany (es => es.Sources),
139                                                                         exceptions);
140                                                 }
141                                                 break;
142                                         }
143                                         case Code.Newobj: {
144                                                 MethodReference ctor = (MethodReference) instruction.Operand;
145                                                 if (IsExceptionConstructor (ctor)) {
146                                                         AddExceptions (body, instruction,
147                                                                         new TypeReference[]{ctor.DeclaringType},
148                                                                         new IMemberReference[]{body.Method},
149                                                                         exceptions);
150                                                 }
151                                                 break;
152                                         }
153                                 }
154                         }
155                 }
156
157                 void AddExceptions (MethodBody body, Instruction instruction, IEnumerable<TypeReference> add, IEnumerable<IMemberReference> sources,
158                                 Dictionary<string, ExceptionSources> exceptions)
159                 {
160                         var handlers = body.ExceptionHandlers.Cast<ExceptionHandler> ()
161                                 .Where (eh => instruction.Offset >= eh.TryStart.Offset && 
162                                                 instruction.Offset <= eh.TryEnd.Offset);
163                         foreach (var ex in add) {
164                                 if (!handlers.Any (h => IsExceptionCaught (ex, h.CatchType))) {
165                                         ExceptionSources s;
166                                         string eName = xdoc.GetDeclaration (ex);
167                                         if (!exceptions.TryGetValue (eName, out s)) {
168                                                 s = new ExceptionSources (ex);
169                                                 exceptions.Add (eName, s);
170                                         }
171                                         s.SourcesList.AddRange (sources);
172                                 }
173                         }
174                 }
175
176                 static bool IsExceptionConstructor (MethodReference ctor)
177                 {
178                         return GetBases (ctor.DeclaringType)
179                                 .Any (t => t.FullName == "System.Exception");
180                 }
181
182                 bool IsExceptionCaught (TypeReference exception, TypeReference catcher)
183                 {
184                         return GetBases (exception).Select (e => xdoc.GetDeclaration (e))
185                                 .Union (GetBases (catcher).Select (e => xdoc.GetDeclaration (e)))
186                                 .Any ();
187                 }
188
189                 static IEnumerable<TypeReference> GetBases (TypeReference type)
190                 {
191                         yield return type;
192                         TypeDefinition def = DocUtils.GetTypeDefinition (type);
193                         while (def != null && def.BaseType != null) {
194                                 yield return def.BaseType;
195                                 def = DocUtils.GetTypeDefinition (def.BaseType);
196                         }
197                 }
198         }
199 }