[build] Fixes Mono.Security dependency
[mono.git] / mcs / tools / corcompare / mono-api-html / Helpers.cs
index 098b29b29a0bb98ea297b89f9759c303fd33c7ef..8769785c47819f7ae5ed11cd3f283e8549f11c76 100644 (file)
@@ -2,7 +2,7 @@
 // Authors
 //    Sebastien Pouliot  <sebastien@xamarin.com>
 //
-// Copyright 2013 Xamarin Inc. http://www.xamarin.com
+// Copyright 2013-2014 Xamarin Inc. http://www.xamarin.com
 // 
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 //
 
 using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
 using System.Xml.Linq;
 
 namespace Xamarin.ApiDiff {
 
        public static class Helper {
-
                public static bool IsTrue (this XElement self, string name)
                {
                        return (self.GetAttribute (name) == "true");
@@ -66,6 +69,28 @@ namespace Xamarin.ApiDiff {
                        return null;
                }
 
+               public static IEnumerable<XElement> Descendants (this XElement self, params string[] names)
+               {
+                       XElement el = self;
+                       if (el == null)
+                               return null;
+
+                       for (int i = 0; i < names.Length - 1; i++) {
+                               el = el.Element (names [i]);
+                               if (el == null)
+                                       return null;
+                       }
+                       return el.Elements (names [names.Length - 1]);
+               }
+
+               public static List<XElement> DescendantList (this XElement self, params string[] names)
+               {
+                       var descendants = self.Descendants (names);
+                       if (descendants == null)
+                               return null;
+                       return descendants.ToList ();
+               }
+
                // make it beautiful (.NET -> C#)
                public static string GetTypeName (this XElement self, string name)
                {
@@ -73,15 +98,47 @@ namespace Xamarin.ApiDiff {
                        if (type == null)
                                return null;
 
-                       // inner types
-                       return GetTypeName (type.Replace ('+', '.'));
+                       StringBuilder sb = null;
+                       bool is_nullable = false;
+                       if (type.StartsWith ("System.Nullable`1[", StringComparison.Ordinal)) {
+                               is_nullable = true;
+                               sb = new StringBuilder (type, 18, type.Length - 19, 1024);
+                       } else {
+                               sb = new StringBuilder (type);
+                       }
+
+                       bool is_ref = (sb [sb.Length - 1] == '&');
+                       if (is_ref)
+                               sb.Remove (sb.Length - 1, 1);
+
+                       int array = 0;
+                       while ((sb [sb.Length - 1] == ']') && (sb [sb.Length - 2] == '[')) {
+                               sb.Remove (sb.Length - 2, 2);
+                               array++;
+                       }
+
+                       bool is_pointer = (sb [sb.Length - 1] == '*');
+                       if (is_pointer)
+                               sb.Remove (sb.Length - 1, 1);
+
+                       type = GetTypeName (sb.Replace ('+', '.').ToString ());
+                       sb.Length = 0;
+                       if (is_ref)
+                               sb.Append (self.GetAttribute ("direction")).Append (' ');
+
+                       sb.Append (type);
+
+                       while (array-- > 0)
+                               sb.Append ("[]");
+                       if (is_nullable)
+                               sb.Append ('?');
+                       if (is_pointer)
+                               sb.Append ('*');
+                       return sb.ToString ();
                }
 
                static string GetTypeName (string type)
                {
-                       if (type.StartsWith ("System.Nullable`1[", StringComparison.Ordinal))
-                               return type.Substring (18, type.Length - 19) + "?";
-
                        int pos = type.IndexOf ('`');
                        if (pos >= 0) {
                                int end = type.LastIndexOf (']');
@@ -120,11 +177,31 @@ namespace Xamarin.ApiDiff {
                                return "ushort";
                        case "System.Char":
                                return "char";
+                       case "System.nint":
+                               return "nint";
+                       case "System.nuint":
+                               return "uint";
+                       case "System.nfloat":
+                               return "nfloat";
+                       case "System.IntPtr":
+                               return "IntPtr";
                        default:
                                if (type.StartsWith (State.Namespace, StringComparison.Ordinal))
                                        type = type.Substring (State.Namespace.Length + 1);
                                return type;
                        }
                }
+
+               public static MethodAttributes GetMethodAttributes (this XElement element)
+               {
+                       var srcAttribs = element.Attribute ("attrib");
+                       return (MethodAttributes) (srcAttribs != null ? Int32.Parse (srcAttribs.Value) : 0);
+               }
+
+               public static FieldAttributes GetFieldAttributes (this XElement element)
+               {
+                       var srcAttribs = element.Attribute ("attrib");
+                       return (FieldAttributes) (srcAttribs != null ? Int32.Parse (srcAttribs.Value) : 0);
+               }
        }
 }
\ No newline at end of file