[amd64] Save missing register
[mono.git] / mcs / tools / mono-api-html / ConstructorComparer.cs
1 // 
2 // Authors
3 //    Sebastien Pouliot  <sebastien@xamarin.com>
4 //
5 // Copyright 2013 Xamarin Inc. http://www.xamarin.com
6 // 
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 using System;
28 using System.Collections.Generic;
29 using System.Reflection;
30 using System.Text;
31 using System.Xml.Linq;
32
33 namespace Xamarin.ApiDiff {
34
35         // MethodComparer inherits from this one
36         public class ConstructorComparer : MemberComparer {
37
38                 public override string GroupName {
39                         get { return "constructors"; }
40                 }
41
42                 public override string ElementName {
43                         get { return "constructor"; }
44                 }
45
46                 public override bool Find (XElement e)
47                 {
48                         return (e.Attribute ("name").Value == Source.Attribute ("name").Value);
49                 }
50
51                 void RenderReturnType (XElement source, XElement target, ApiChange change)
52                 {
53                         var srcType = source.GetTypeName ("returntype");
54                         var tgtType = target.GetTypeName ("returntype");
55
56                         if (srcType != tgtType) {
57                                 change.AppendModified (srcType, tgtType, true);
58                                 change.Append (" ");
59                         } else if (srcType != null) {
60                                 // ctor don't have a return type
61                                 change.Append (srcType);
62                                 change.Append (" ");
63                         }
64                 }
65
66                 public override bool Equals (XElement source, XElement target, ApiChanges changes)
67                 {
68                         if (base.Equals (source, target, changes))
69                                 return true;
70                                 
71                         var change = new ApiChange ();
72                         change.Header = "Modified " + GroupName;
73                         RenderMethodAttributes (source, target, change);
74                         RenderReturnType (source, target, change);
75                         RenderName (source, target, change);
76                         RenderGenericParameters (source, target, change);
77                         RenderParameters (source, target, change);
78
79                         changes.Add (source, target, change);
80
81                         return false;
82                 }
83
84                 public override string GetDescription (XElement e)
85                 {
86                         var sb = new StringBuilder ();
87
88                         var attribs = e.Attribute ("attrib");
89                         if (attribs != null) {
90                                 var attr = (MethodAttributes) Int32.Parse (attribs.Value);
91                                 if ((attr & MethodAttributes.Public) != MethodAttributes.Public) {
92                                         sb.Append ("protected ");
93                                 } else {
94                                         sb.Append ("public ");
95                                 }
96
97                                 if ((attr & MethodAttributes.Static) != 0) {
98                                         sb.Append ("static ");
99                                 } else if ((attr & MethodAttributes.Virtual) != 0) {
100                                         if ((attr & MethodAttributes.VtableLayoutMask) == 0)
101                                                 sb.Append ("override ");
102                                         else
103                                                 sb.Append ("virtual ");
104                                 }
105                         }
106
107                         string name = e.GetAttribute ("name");
108
109                         var r = e.GetTypeName ("returntype");
110                         if (r != null) {
111                                 // ctor dont' have a return type
112                                 sb.Append (r).Append (' ');
113                         } else {
114                                 // show the constructor as it would be defined in C#
115                                 name = name.Replace (".ctor", State.Type);
116                         }
117
118                         // the XML file `name` does not contain parameter names, so we must process them ourselves
119                         // which gives us the opportunity to simplify type names
120                         sb.Append (name.Substring (0, name.IndexOf ('(')));
121
122                         var genericp = e.Element ("generic-parameters");
123                         if (genericp != null) {
124                                 var list = new List<string> ();
125                                 foreach (var p in genericp.Elements ("generic-parameter")) {
126                                         list.Add (p.GetTypeName ("name"));
127                                 }
128                                 sb.Append ("&lt;").Append (String.Join (", ", list)).Append ("&gt;");
129                         }
130
131                         sb.Append (" (");
132                         var parameters = e.Element ("parameters");
133                         if (parameters != null) {
134                                 var list = new List<string> ();
135                                 foreach (var p in parameters.Elements ("parameter")) {
136                                         var pTypeName   = p.GetTypeName ("type");
137                                         list.Add (State.IgnoreParameterNameChanges
138                                                 ? pTypeName
139                                                 : pTypeName + " " + p.GetAttribute ("name"));
140                                 }
141                                 sb.Append (String.Join (", ", list));
142                         }
143                         sb.Append (");");
144
145                         return sb.ToString ();
146                 }
147         }
148 }