[sdb] Fix a deadlock in the assembly cache invalidation code.
[mono.git] / mcs / class / Mono.Debugger.Soft / Mono.Debugger.Soft / FieldInfoMirror.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Reflection;
5 using C = Mono.Cecil;
6 using Mono.Cecil.Metadata;
7
8 namespace Mono.Debugger.Soft
9 {
10         public class FieldInfoMirror : Mirror {
11
12                 TypeMirror parent;
13                 string name;
14                 TypeMirror type;
15                 FieldAttributes attrs;
16                 CustomAttributeDataMirror[] cattrs;
17                 C.FieldDefinition meta;
18                 bool inited;
19
20                 public FieldInfoMirror (TypeMirror parent, long id, string name, TypeMirror type, FieldAttributes attrs) : base (parent.VirtualMachine, id) {
21                         this.parent = parent;
22                         this.name = name;
23                         this.type = type;
24                         this.attrs = attrs;
25                         inited = true;
26                 }
27
28                 public FieldInfoMirror (VirtualMachine vm, long id) : base (vm, id) {
29                 }
30
31                 public TypeMirror DeclaringType {
32                         get {
33                                 if (!inited)
34                                         GetInfo ();
35                                 return parent;
36                         }
37                 }
38
39                 public string Name {
40                         get {
41                                 if (!inited)
42                                         GetInfo ();
43                                 return name;
44                         }
45                 }
46
47                 public TypeMirror FieldType {
48                         get {
49                                 if (!inited)
50                                         GetInfo ();
51                                 return type;
52                         }
53                 }
54
55                 public FieldAttributes Attributes {
56                         get {
57                                 if (!inited)
58                                         GetInfo ();
59                                 return attrs;
60                         }
61                 }
62
63                 void GetInfo () {
64                         if (inited)
65                                 return;
66                         var info = vm.conn.Field_GetInfo (id);
67                         name = info.Name;
68                         parent = vm.GetType (info.Parent);
69                         type = vm.GetType (info.TypeId);
70                         attrs = (FieldAttributes)info.Attrs;
71                         inited = true;
72                 }
73
74                 public bool IsLiteral
75                 {
76                         get {return (Attributes & FieldAttributes.Literal) != 0;}
77                 } 
78
79                 public bool IsStatic
80                 {
81                         get {return (Attributes & FieldAttributes.Static) != 0;}
82                 } 
83
84                 public bool IsInitOnly
85                 {
86                         get {return (Attributes & FieldAttributes.InitOnly) != 0;}
87                 }
88  
89                 public Boolean IsPublic
90                 { 
91                         get
92                         {
93                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
94                         }
95                 }
96
97                 public Boolean IsPrivate
98                 {
99                         get
100                         {
101                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
102                         }
103                 }
104
105                 public Boolean IsFamily
106                 {
107                         get
108                         {
109                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
110                         }
111                 }
112
113                 public Boolean IsAssembly
114                 {
115                         get
116                         {
117                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
118                         }
119                 }
120
121                 public Boolean IsFamilyAndAssembly
122                 {
123                         get {
124                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
125                         }
126                 }
127
128                 public Boolean IsFamilyOrAssembly
129                 {
130                         get
131                         {
132                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
133                         }
134                 }
135
136                 public Boolean IsPinvokeImpl
137                 {
138                         get
139                         {
140                                 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
141                         }
142                 }
143
144                 public Boolean IsSpecialName
145                 {
146                         get
147                         {
148                                 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
149                         }
150                 }
151
152                 public Boolean IsNotSerialized
153                 {
154                         get
155                         {
156                                 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
157                         }
158                 }
159
160                 public CustomAttributeDataMirror[] GetCustomAttributes (bool inherit) {
161                         return GetCAttrs (null, inherit);
162                 }
163
164                 public CustomAttributeDataMirror[] GetCustomAttributes (TypeMirror attributeType, bool inherit) {
165                         if (attributeType == null)
166                                 throw new ArgumentNullException ("attributeType");
167                         return GetCAttrs (attributeType, inherit);
168                 }
169
170                 public C.FieldDefinition Metadata {             
171                         get {
172                                 if (parent.Metadata == null)
173                                         return null;
174                                 // FIXME: Speed this up
175                                 foreach (var fd in parent.Metadata.Fields) {
176                                         if (fd.Name == Name) {
177                                                 meta = fd;
178                                                 break;
179                                         }
180                                 }
181                                 if (meta == null)
182                                         /* Shouldn't happen */
183                                         throw new NotImplementedException ();
184                                 return meta;
185                         }
186                 }
187
188                 CustomAttributeDataMirror[] GetCAttrs (TypeMirror type, bool inherit) {
189                         if (cattrs == null && Metadata != null && !Metadata.HasCustomAttributes)
190                                 cattrs = new CustomAttributeDataMirror [0];
191
192                         // FIXME: Handle inherit
193                         if (cattrs == null) {
194                                 CattrInfo[] info = vm.conn.Type_GetFieldCustomAttributes (DeclaringType.Id, id, 0, false);
195                                 cattrs = CustomAttributeDataMirror.Create (vm, info);
196                         }
197                         var res = new List<CustomAttributeDataMirror> ();
198                         foreach (var attr in cattrs)
199                                 if (type == null || attr.Constructor.DeclaringType == type)
200                                         res.Add (attr);
201                         return res.ToArray ();
202                 }
203         }
204 }
205