~ubuntu-branches/ubuntu/trusty/mono-addins/trusty-proposed

« back to all changes in this revision

Viewing changes to Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyDefinition.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-04-25 11:11:33 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110425111133-t05u5p7o5fxx70fu
Tags: 0.6-2
Upload to Unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// Author:
5
5
//   Jb Evain (jbevain@gmail.com)
6
6
//
7
 
// (C) 2005 Jb Evain
 
7
// Copyright (c) 2008 - 2010 Jb Evain
8
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining
10
10
// a copy of this software and associated documentation files (the
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using System.Text;
 
30
 
 
31
using Mono.Collections.Generic;
 
32
 
29
33
namespace Mono.Cecil {
30
34
 
31
 
        using System;
32
 
        using System.Text;
33
 
 
34
 
        public sealed class PropertyDefinition : PropertyReference,
35
 
                IMemberDefinition, ICustomAttributeProvider, IHasConstant {
36
 
 
37
 
                PropertyAttributes m_attributes;
38
 
 
39
 
                CustomAttributeCollection m_customAttrs;
40
 
 
41
 
                MethodDefinition m_getMeth;
42
 
                MethodDefinition m_setMeth;
43
 
 
44
 
                bool m_hasConstant;
45
 
                object m_const;
 
35
        public sealed class PropertyDefinition : PropertyReference, IMemberDefinition, IConstantProvider {
 
36
 
 
37
                bool? has_this;
 
38
                ushort attributes;
 
39
 
 
40
                Collection<CustomAttribute> custom_attributes;
 
41
 
 
42
                internal MethodDefinition get_method;
 
43
                internal MethodDefinition set_method;
 
44
                internal Collection<MethodDefinition> other_methods;
 
45
 
 
46
                object constant = Mixin.NotResolved;
46
47
 
47
48
                public PropertyAttributes Attributes {
48
 
                        get { return m_attributes; }
49
 
                        set { m_attributes = value; }
50
 
                }
51
 
 
52
 
                public CustomAttributeCollection CustomAttributes {
53
 
                        get {
54
 
                                if (m_customAttrs == null)
55
 
                                        m_customAttrs = new CustomAttributeCollection (this);
56
 
 
57
 
                                return m_customAttrs;
58
 
                        }
59
 
                }
60
 
 
61
 
                public override ParameterDefinitionCollection Parameters {
62
 
                        get {
63
 
                                if (this.GetMethod != null)
64
 
                                        return CloneParameterCollection (this.GetMethod.Parameters);
65
 
                                else if (this.SetMethod != null) {
66
 
                                        ParameterDefinitionCollection parameters =
67
 
                                                CloneParameterCollection (this.SetMethod.Parameters);
68
 
                                        if (parameters.Count > 0)
69
 
                                                parameters.RemoveAt (parameters.Count - 1);
70
 
                                        return parameters;
71
 
                                }
72
 
 
73
 
                                if (m_parameters == null)
74
 
                                        m_parameters = new ParameterDefinitionCollection (this);
75
 
 
76
 
                                return m_parameters;
77
 
                        }
 
49
                        get { return (PropertyAttributes) attributes; }
 
50
                        set { attributes = (ushort) value; }
 
51
                }
 
52
 
 
53
                public bool HasThis {
 
54
                        get {
 
55
                                if (has_this.HasValue)
 
56
                                        return has_this.Value;
 
57
 
 
58
                                if (GetMethod != null)
 
59
                                        return get_method.HasThis;
 
60
 
 
61
                                if (SetMethod != null)
 
62
                                        return set_method.HasThis;
 
63
 
 
64
                                return false;
 
65
                        }
 
66
                        set { has_this = value; }
 
67
                }
 
68
 
 
69
                public bool HasCustomAttributes {
 
70
                        get {
 
71
                                if (custom_attributes != null)
 
72
                                        return custom_attributes.Count > 0;
 
73
 
 
74
                                return this.GetHasCustomAttributes (Module);
 
75
                        }
 
76
                }
 
77
 
 
78
                public Collection<CustomAttribute> CustomAttributes {
 
79
                        get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
78
80
                }
79
81
 
80
82
                public MethodDefinition GetMethod {
81
 
                        get { return m_getMeth; }
82
 
                        set { m_getMeth = value; }
 
83
                        get {
 
84
                                if (get_method != null)
 
85
                                        return get_method;
 
86
 
 
87
                                InitializeMethods ();
 
88
                                return get_method;
 
89
                        }
 
90
                        set { get_method = value; }
83
91
                }
84
92
 
85
93
                public MethodDefinition SetMethod {
86
 
                        get { return m_setMeth; }
87
 
                        set { m_setMeth = value; }
88
 
                }
89
 
 
90
 
                ParameterDefinitionCollection CloneParameterCollection (ParameterDefinitionCollection original)
 
94
                        get {
 
95
                                if (set_method != null)
 
96
                                        return set_method;
 
97
 
 
98
                                InitializeMethods ();
 
99
                                return set_method;
 
100
                        }
 
101
                        set { set_method = value; }
 
102
                }
 
103
 
 
104
                public bool HasOtherMethods {
 
105
                        get {
 
106
                                if (other_methods != null)
 
107
                                        return other_methods.Count > 0;
 
108
 
 
109
                                InitializeMethods ();
 
110
                                return !other_methods.IsNullOrEmpty ();
 
111
                        }
 
112
                }
 
113
 
 
114
                public Collection<MethodDefinition> OtherMethods {
 
115
                        get {
 
116
                                if (other_methods != null)
 
117
                                        return other_methods;
 
118
 
 
119
                                InitializeMethods ();
 
120
 
 
121
                                if (other_methods != null)
 
122
                                        return other_methods;
 
123
 
 
124
                                return other_methods = new Collection<MethodDefinition> ();
 
125
                        }
 
126
                }
 
127
 
 
128
                public bool HasParameters {
 
129
                        get {
 
130
                                if (get_method != null)
 
131
                                        return get_method.HasParameters;
 
132
 
 
133
                                if (set_method != null)
 
134
                                        return set_method.HasParameters && set_method.Parameters.Count > 1;
 
135
 
 
136
                                return false;
 
137
                        }
 
138
                }
 
139
 
 
140
                public override Collection<ParameterDefinition> Parameters {
 
141
                        get {
 
142
                                InitializeMethods ();
 
143
 
 
144
                                if (get_method != null)
 
145
                                        return MirrorParameters (get_method, 0);
 
146
 
 
147
                                if (set_method != null)
 
148
                                        return MirrorParameters (set_method, 1);
 
149
 
 
150
                                return new Collection<ParameterDefinition> ();
 
151
                        }
 
152
                }
 
153
 
 
154
                static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
91
155
                {
92
 
                        ParameterDefinitionCollection clone = new ParameterDefinitionCollection (
93
 
                                original.Container);
94
 
                        foreach (ParameterDefinition param in original)
95
 
                                clone.Add (param);
96
 
                        return clone;
 
156
                        var parameters = new Collection<ParameterDefinition> ();
 
157
                        if (!method.HasParameters)
 
158
                                return parameters;
 
159
 
 
160
                        var original_parameters = method.Parameters;
 
161
                        var end = original_parameters.Count - bound;
 
162
 
 
163
                        for (int i = 0; i < end; i++)
 
164
                                parameters.Add (original_parameters [i]);
 
165
 
 
166
                        return parameters;
97
167
                }
98
168
 
99
169
                public bool HasConstant {
100
 
                        get { return m_hasConstant; }
 
170
                        get {
 
171
                                ResolveConstant ();
 
172
 
 
173
                                return constant != Mixin.NoValue;
 
174
                        }
 
175
                        set { if (!value) constant = Mixin.NoValue; }
101
176
                }
102
177
 
103
178
                public object Constant {
104
 
                        get { return m_const; }
105
 
                        set {
106
 
                                m_hasConstant = true;
107
 
                                m_const = value;
108
 
                        }
 
179
                        get { return HasConstant ? constant : null;     }
 
180
                        set { constant = value; }
 
181
                }
 
182
 
 
183
                void ResolveConstant ()
 
184
                {
 
185
                        if (constant != Mixin.NotResolved)
 
186
                                return;
 
187
 
 
188
                        this.ResolveConstant (ref constant, Module);
109
189
                }
110
190
 
111
191
                #region PropertyAttributes
112
192
 
113
193
                public bool IsSpecialName {
114
 
                        get { return (m_attributes & PropertyAttributes.SpecialName) != 0; }
115
 
                        set {
116
 
                                if (value)
117
 
                                        m_attributes |= PropertyAttributes.SpecialName;
118
 
                                else
119
 
                                        m_attributes &= ~PropertyAttributes.SpecialName;
120
 
                        }
 
194
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
 
195
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
121
196
                }
122
197
 
123
198
                public bool IsRuntimeSpecialName {
124
 
                        get { return (m_attributes & PropertyAttributes.RTSpecialName) != 0; }
125
 
                        set {
126
 
                                if (value)
127
 
                                        m_attributes |= PropertyAttributes.RTSpecialName;
128
 
                                else
129
 
                                        m_attributes &= ~PropertyAttributes.RTSpecialName;
130
 
                        }
 
199
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
 
200
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
131
201
                }
132
202
 
133
203
                public bool HasDefault {
134
 
                        get { return (m_attributes & PropertyAttributes.HasDefault) != 0; }
135
 
                        set {
136
 
                                if (value)
137
 
                                        m_attributes |= PropertyAttributes.HasDefault;
138
 
                                else
139
 
                                        m_attributes &= ~PropertyAttributes.HasDefault;
140
 
                        }
 
204
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
 
205
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
141
206
                }
142
207
 
143
208
                #endregion
144
209
 
145
 
                public PropertyDefinition (string name, TypeReference propertyType, PropertyAttributes attrs) : base (name, propertyType)
146
 
                {
147
 
                        m_attributes = attrs;
148
 
                }
149
 
 
150
 
                public static MethodDefinition CreateGetMethod (PropertyDefinition prop)
151
 
                {
152
 
                        MethodDefinition get = new MethodDefinition (
153
 
                                string.Concat ("get_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
154
 
                        prop.GetMethod = get;
155
 
                        return get;
156
 
                }
157
 
 
158
 
                public static MethodDefinition CreateSetMethod (PropertyDefinition prop)
159
 
                {
160
 
                        MethodDefinition set = new MethodDefinition (
161
 
                                string.Concat ("set_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
162
 
                        prop.SetMethod = set;
163
 
                        return set;
164
 
                }
165
 
 
166
 
                public PropertyDefinition Clone ()
167
 
                {
168
 
                        return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
169
 
                }
170
 
 
171
 
                internal static PropertyDefinition Clone (PropertyDefinition prop, ImportContext context)
172
 
                {
173
 
                        PropertyDefinition np = new PropertyDefinition (
174
 
                                prop.Name,
175
 
                                context.Import (prop.PropertyType),
176
 
                                prop.Attributes);
177
 
 
178
 
                        if (prop.HasConstant)
179
 
                                np.Constant = prop.Constant;
180
 
 
181
 
                        if (context.GenericContext.Type is TypeDefinition) {
182
 
                                TypeDefinition type = context.GenericContext.Type as TypeDefinition;
183
 
                                if (prop.SetMethod != null)
184
 
                                        np.SetMethod = type.Methods.GetMethod (prop.SetMethod.Name, prop.SetMethod.Parameters);
185
 
                                if (prop.GetMethod != null)
186
 
                                        np.GetMethod = type.Methods.GetMethod (prop.GetMethod.Name, prop.GetMethod.Parameters);
187
 
                        }
188
 
 
189
 
                        foreach (CustomAttribute ca in prop.CustomAttributes)
190
 
                                np.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
191
 
 
192
 
                        return np;
193
 
                }
194
 
 
195
 
                public override string ToString ()
196
 
                {
197
 
                        StringBuilder sb = new StringBuilder ();
198
 
                        sb.Append (PropertyType.ToString ());
199
 
                        sb.Append (' ');
200
 
 
201
 
                        if (this.DeclaringType != null) {
202
 
                                sb.Append (this.DeclaringType.ToString ());
203
 
                                sb.Append ("::");
204
 
                        }
205
 
 
206
 
                        sb.Append (this.Name);
207
 
                        sb.Append ('(');
208
 
                        ParameterDefinitionCollection parameters = this.Parameters;
209
 
                        for (int i = 0; i < parameters.Count; i++) {
210
 
                                if (i > 0)
211
 
                                        sb.Append (',');
212
 
                                sb.Append (parameters [i].ParameterType.ToString ());
213
 
                        }
214
 
                        sb.Append (')');
215
 
                        return sb.ToString ();
216
 
                }
217
 
 
218
 
                public override void Accept (IReflectionVisitor visitor)
219
 
                {
220
 
                        visitor.VisitPropertyDefinition (this);
221
 
 
222
 
                        this.CustomAttributes.Accept (visitor);
 
210
                public new TypeDefinition DeclaringType {
 
211
                        get { return (TypeDefinition) base.DeclaringType; }
 
212
                        set { base.DeclaringType = value; }
 
213
                }
 
214
 
 
215
                public override bool IsDefinition {
 
216
                        get { return true; }
 
217
                }
 
218
 
 
219
                public override string FullName {
 
220
                        get {
 
221
                                var builder = new StringBuilder ();
 
222
                                builder.Append (PropertyType.ToString ());
 
223
                                builder.Append (' ');
 
224
                                builder.Append (MemberFullName ());
 
225
                                builder.Append ('(');
 
226
                                if (HasParameters) {
 
227
                                        var parameters = Parameters;
 
228
                                        for (int i = 0; i < parameters.Count; i++) {
 
229
                                                if (i > 0)
 
230
                                                        builder.Append (',');
 
231
                                                builder.Append (parameters [i].ParameterType.FullName);
 
232
                                        }
 
233
                                }
 
234
                                builder.Append (')');
 
235
                                return builder.ToString ();
 
236
                        }
 
237
                }
 
238
 
 
239
                public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
 
240
                        : base (name, propertyType)
 
241
                {
 
242
                        this.attributes = (ushort) attributes;
 
243
                        this.token = new MetadataToken (TokenType.Property);
 
244
                }
 
245
 
 
246
                void InitializeMethods ()
 
247
                {
 
248
                        if (get_method != null || set_method != null)
 
249
                                return;
 
250
 
 
251
                        var module = this.Module;
 
252
                        if (!module.HasImage ())
 
253
                                return;
 
254
 
 
255
                        module.Read (this, (property, reader) => reader.ReadMethods (property));
223
256
                }
224
257
        }
225
258
}