just kick off another build
[moon.git] / tools / generators / MemberInfo.cs
blobf7d8beea599ef0a600015c6a8bfbe2f572249a55
1 /*
2 * MemberInfo.cs.
4 * Contact:
5 * Moonlight List (moonlight-list@lists.ximian.com)
7 * Copyright 2008 Novell, Inc. (http://www.novell.com)
9 * See the LICENSE file included with the distribution for details.
13 using System;
14 using System.Collections.Generic;
15 using System.IO;
16 using System.Text;
18 class MemberInfo {
19 public MemberInfo Parent;
20 public string Name;
22 public bool IsPublic;
23 public bool IsPrivate;
24 public bool IsProtected;
26 private string header; // The .h file where the member is defined
27 private Members children;
28 private Annotations annotations;
29 private string fullname;
30 private string managed_fullname;
31 private Nullable<int> silverlight_version;
32 private string managed_name;
34 public GlobalInfo GlobalInfo {
35 get {
36 GlobalInfo result = Parent as GlobalInfo;
38 if (result != null)
39 return result;
41 if (Parent == null)
42 return this as GlobalInfo;
44 return Parent.GlobalInfo;
48 public string ManagedName {
49 get {
50 if (managed_name == null) {
51 managed_name = Annotations.GetValue ("ManagedName");
52 if (string.IsNullOrEmpty (managed_name))
53 managed_name = Name;
55 return managed_name;
59 public TypeInfo ParentType {
60 get {
61 return (TypeInfo) Parent;
65 public void WriteVersionIf (StringBuilder text, bool end)
67 if (SilverlightVersion > 1) {
68 if (!end) {
69 text.AppendLine ("#if ");
70 Helper.WriteVersion (text, SilverlightVersion);
71 } else {
72 text.AppendLine ("#endif");
77 public bool IsPluginMember {
78 get {
79 if (Header == null || Header == string.Empty)
80 return false;
82 return Path.GetFileName (Path.GetDirectoryName (Header)) == "plugin";
86 public bool IsSrcMember {
87 get {
88 if (Header == null || Header == string.Empty)
89 return false;
91 return Path.GetFileName (Path.GetDirectoryName (Header)) == "src";
95 public virtual string Signature {
96 get { return Name; }
99 public Members Children {
100 get {
101 if (children == null)
102 children = new Members (this);
103 return children;
107 public Annotations Annotations {
108 get {
109 if (annotations == null)
110 annotations = new Annotations ();
111 return annotations;
113 set {
114 annotations = value;
118 public string Header {
119 get {
120 if (header == null) {
121 if (Parent != null)
122 header = Parent.Header;
123 else
124 header = string.Empty;
126 return header;
128 set {
129 header = value;
133 public string FullName {
134 get {
135 if (fullname == null) {
136 if (Parent != null && !string.IsNullOrEmpty (Parent.FullName)) {
137 fullname = Parent.FullName + "." + Name;
138 } else {
139 fullname = Name;
142 return fullname;
146 public string ManagedFullName {
147 get {
148 if (managed_fullname == null) {
149 if (Parent != null && !string.IsNullOrEmpty (Parent.ManagedFullName)) {
150 managed_fullname = Parent.ManagedFullName + "." + Name;
151 } else if (Namespace != null) {
152 managed_fullname = Namespace + "." + FullName;
153 } else {
154 managed_fullname = FullName;
157 return managed_fullname;
161 public string Namespace {
162 get { return Annotations.GetValue ("Namespace"); }
165 public int SilverlightVersion {
166 get {
167 string value = null;
168 Annotation property;
169 if (!silverlight_version.HasValue) {
170 if (Annotations.TryGetValue ("Version", out property)) {
171 value = property.Value;
172 } else if (Annotations.TryGetValue ("SilverlightVersion", out property)) {
173 value = property.Value;
176 if (value == null) {
177 if (Parent != null)
178 silverlight_version = new Nullable<int> (Parent.SilverlightVersion);
179 else
180 silverlight_version = new Nullable<int> (1);
181 } else {
182 if (value == "\"2\"" || value == "2" || value == "2.0")
183 silverlight_version = new Nullable<int> (2);
184 else if (value == "\"1\"" || value == "1" || value == "1.0")
185 silverlight_version = new Nullable<int> (1);
186 else
187 throw new Exception (string.Format ("Invalid Version/SilverlightVersion: '{0}'", value));
191 return silverlight_version.Value;
195 public void Dump (int ident)
197 if (annotations != null)
198 annotations.Dump ();
199 Console.Write (new string ('\t', ident));
200 Console.WriteLine ("{0} {1}", FullName, Header);
201 if (children != null)
202 foreach (MemberInfo info in children.Values)
203 info.Dump (ident + 1);
208 class Members : Dictionary <string, MemberInfo>{
209 private MemberInfo [] sorted;
210 private MemberInfo [] sorted_by_kind;
211 private List<TypeInfo> sorted_types_by_kind;
212 private StringBuilder kinds_for_enum;
213 private MemberInfo parent;
215 public Members (MemberInfo Parent) : base (StringComparer.Ordinal)
217 parent = Parent;
220 class TypeSortedByKind : IComparer <TypeInfo> {
221 public int Compare (TypeInfo a, TypeInfo b)
223 return string.Compare (a.KindName, b.KindName);
227 public class MembersSortedByName <T> : IComparer<T> where T : MemberInfo {
228 public int Compare (T a, T b)
230 return string.Compare (a.Name, b.Name);
234 public class MembersSortedByFullName <T> : IComparer<T> where T : MemberInfo {
235 public int Compare (T a, T b)
237 return string.Compare (a.FullName, b.FullName);
241 public class MembersSortedByManagedFullName <T> : IComparer<T> where T : MemberInfo {
242 public int Compare (T a, T b)
244 int result = string.Compare (a.Namespace, b.Namespace);
245 if (result != 0)
246 return result;
247 return string.Compare (a.ManagedName, b.ManagedName);
251 public IEnumerable <TypeInfo> SortedTypesByKind {
252 get {
253 if (sorted_types_by_kind == null) {
254 sorted_types_by_kind = new List<TypeInfo> ();
255 foreach (MemberInfo member in this.Values) {
256 TypeInfo type = member as TypeInfo;
257 if (type != null)
258 sorted_types_by_kind.Add (type);
260 sorted_types_by_kind.Sort (new TypeSortedByKind ());
262 return sorted_types_by_kind;
266 public IEnumerable <MemberInfo> SortedList {
267 get {
268 if (sorted == null) {
269 int i = 0;
270 sorted = new MemberInfo [Count];
271 foreach (MemberInfo type in this.Values)
272 sorted [i++] = type;
273 Array.Sort (sorted, delegate (MemberInfo x, MemberInfo y) {
274 return string.Compare (x.FullName, y.FullName);
277 return sorted;
280 public IEnumerable <MemberInfo> SortedByKindList {
281 get {
282 if (sorted_by_kind == null) {
283 int i = 0;
284 sorted_by_kind = new MemberInfo [Count];
285 foreach (MemberInfo type in this.Values) {
286 if (!(type is TypeInfo))
287 continue;
288 if (type == null)
289 continue;
290 sorted_by_kind [i++] = type;
293 Array.Sort (sorted_by_kind, delegate (MemberInfo x, MemberInfo y) {
294 if (x == null && y != null)
295 return 1;
296 else if (x != null && y == null)
297 return -1;
298 else if (x == null && y == null)
299 return 0;
300 else
301 return string.Compare ((x as TypeInfo).KindName, (y as TypeInfo).KindName);
304 return sorted_by_kind;
308 public MemberInfo Add (MemberInfo value)
310 int counter = 1;
311 string signature = value.Signature;
313 if (!base.ContainsKey (signature)) {
314 base.Add (signature, value);
315 } else if (value.Name == "<anonymous>") {
316 string tmp;
317 do {
318 tmp = "<anonymous>" + counter.ToString ();
319 counter++;
320 } while (base.ContainsKey (tmp));
321 value.Name = tmp;
322 base.Add (value.Name, value);
323 } else {
324 Console.WriteLine (string.Format ("Could not add the member: {0}.{1} in parent {3}: There already is a member with the same signature ({2}).", parent.Name, value.Name, signature, parent.GetType ().FullName));
326 return value;
329 public StringBuilder GetKindsForEnum ()
331 if (kinds_for_enum == null) {
332 kinds_for_enum = new StringBuilder ();
333 foreach (MemberInfo info in SortedByKindList) {
334 TypeInfo type = info as TypeInfo;
336 if (type == null)
337 continue;
339 if (!type.Annotations.ContainsKey ("IncludeInKinds")) {
340 continue;
343 kinds_for_enum.Append ("\t\t");
344 kinds_for_enum.Append (type.KindName);
345 kinds_for_enum.Append (",");
346 if (type.Annotations.ContainsKey ("SilverlightVersion"))// && type.Annotations ["SilverlightVersion"].Value == "\"2\"")
347 kinds_for_enum.Append ("// Silverlight 2.0 only");
348 kinds_for_enum.AppendLine ();
351 return kinds_for_enum;