43572742d16dd71dc1e2cbab91dbd6d953531bd6
[mono.git] / mcs / tools / cil-strip / Mono.Cecil / PropertyDefinition.cs
1 //
2 // PropertyDefinition.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace Mono.Cecil {
30
31         using System;
32         using System.Text;
33
34         internal 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;
46
47                 public PropertyAttributes Attributes {
48                         get { return m_attributes; }
49                         set { m_attributes = value; }
50                 }
51
52                 public bool HasCustomAttributes {
53                         get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
54                 }
55
56                 public CustomAttributeCollection CustomAttributes {
57                         get {
58                                 if (m_customAttrs == null)
59                                         m_customAttrs = new CustomAttributeCollection (this);
60
61                                 return m_customAttrs;
62                         }
63                 }
64
65                 public override bool HasParameters {
66                         get {
67                                 if (m_getMeth != null)
68                                         return m_getMeth.HasParameters;
69                                 else if (m_setMeth != null)
70                                         return m_setMeth.HasParameters;
71                                 else if (m_parameters == null)
72                                         return false;
73                                 else
74                                         return m_parameters.Count > 0;
75                         }
76                 }
77
78                 public override ParameterDefinitionCollection Parameters {
79                         get {
80                                 if (this.GetMethod != null)
81                                         return CloneParameterCollection (this.GetMethod.Parameters);
82                                 else if (this.SetMethod != null) {
83                                         ParameterDefinitionCollection parameters =
84                                                 CloneParameterCollection (this.SetMethod.Parameters);
85                                         if (parameters.Count > 0)
86                                                 parameters.RemoveAt (parameters.Count - 1);
87                                         return parameters;
88                                 }
89
90                                 if (m_parameters == null)
91                                         m_parameters = new ParameterDefinitionCollection (this);
92
93                                 return m_parameters;
94                         }
95                 }
96
97                 public MethodDefinition GetMethod {
98                         get { return m_getMeth; }
99                         set { m_getMeth = value; }
100                 }
101
102                 public MethodDefinition SetMethod {
103                         get { return m_setMeth; }
104                         set { m_setMeth = value; }
105                 }
106
107                 ParameterDefinitionCollection CloneParameterCollection (ParameterDefinitionCollection original)
108                 {
109                         ParameterDefinitionCollection clone = new ParameterDefinitionCollection (
110                                 original.Container);
111                         foreach (ParameterDefinition param in original)
112                                 clone.Add (param);
113                         return clone;
114                 }
115
116                 public bool HasConstant {
117                         get { return m_hasConstant; }
118                 }
119
120                 public object Constant {
121                         get { return m_const; }
122                         set {
123                                 m_hasConstant = true;
124                                 m_const = value;
125                         }
126                 }
127
128                 #region PropertyAttributes
129
130                 public bool IsSpecialName {
131                         get { return (m_attributes & PropertyAttributes.SpecialName) != 0; }
132                         set {
133                                 if (value)
134                                         m_attributes |= PropertyAttributes.SpecialName;
135                                 else
136                                         m_attributes &= ~PropertyAttributes.SpecialName;
137                         }
138                 }
139
140                 public bool IsRuntimeSpecialName {
141                         get { return (m_attributes & PropertyAttributes.RTSpecialName) != 0; }
142                         set {
143                                 if (value)
144                                         m_attributes |= PropertyAttributes.RTSpecialName;
145                                 else
146                                         m_attributes &= ~PropertyAttributes.RTSpecialName;
147                         }
148                 }
149
150                 public bool HasDefault {
151                         get { return (m_attributes & PropertyAttributes.HasDefault) != 0; }
152                         set {
153                                 if (value)
154                                         m_attributes |= PropertyAttributes.HasDefault;
155                                 else
156                                         m_attributes &= ~PropertyAttributes.HasDefault;
157                         }
158                 }
159
160                 #endregion
161
162                 public new TypeDefinition DeclaringType {
163                         get { return (TypeDefinition) base.DeclaringType; }
164                         set { base.DeclaringType = value; }
165                 }
166
167                 public PropertyDefinition (string name, TypeReference propertyType, PropertyAttributes attrs) : base (name, propertyType)
168                 {
169                         m_attributes = attrs;
170                 }
171
172                 public override PropertyDefinition Resolve ()
173                 {
174                         return this;
175                 }
176
177                 public static MethodDefinition CreateGetMethod (PropertyDefinition prop)
178                 {
179                         MethodDefinition get = new MethodDefinition (
180                                 string.Concat ("get_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
181                         prop.GetMethod = get;
182                         return get;
183                 }
184
185                 public static MethodDefinition CreateSetMethod (PropertyDefinition prop)
186                 {
187                         MethodDefinition set = new MethodDefinition (
188                                 string.Concat ("set_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
189                         prop.SetMethod = set;
190                         return set;
191                 }
192
193                 public PropertyDefinition Clone ()
194                 {
195                         return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
196                 }
197
198                 internal static PropertyDefinition Clone (PropertyDefinition prop, ImportContext context)
199                 {
200                         PropertyDefinition np = new PropertyDefinition (
201                                 prop.Name,
202                                 context.Import (prop.PropertyType),
203                                 prop.Attributes);
204
205                         if (prop.HasConstant)
206                                 np.Constant = prop.Constant;
207
208                         if (context.GenericContext.Type is TypeDefinition) {
209                                 TypeDefinition type = context.GenericContext.Type as TypeDefinition;
210                                 if (prop.SetMethod != null)
211                                         np.SetMethod = type.Methods.GetMethod (prop.SetMethod.Name, prop.SetMethod.Parameters);
212                                 if (prop.GetMethod != null)
213                                         np.GetMethod = type.Methods.GetMethod (prop.GetMethod.Name, prop.GetMethod.Parameters);
214                         }
215
216                         foreach (CustomAttribute ca in prop.CustomAttributes)
217                                 np.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
218
219                         return np;
220                 }
221
222                 public override string ToString ()
223                 {
224                         StringBuilder sb = new StringBuilder ();
225                         sb.Append (PropertyType.ToString ());
226                         sb.Append (' ');
227
228                         if (this.DeclaringType != null) {
229                                 sb.Append (this.DeclaringType.ToString ());
230                                 sb.Append ("::");
231                         }
232
233                         sb.Append (this.Name);
234                         sb.Append ('(');
235                         ParameterDefinitionCollection parameters = this.Parameters;
236                         for (int i = 0; i < parameters.Count; i++) {
237                                 if (i > 0)
238                                         sb.Append (',');
239                                 sb.Append (parameters [i].ParameterType.ToString ());
240                         }
241                         sb.Append (')');
242                         return sb.ToString ();
243                 }
244
245                 public override void Accept (IReflectionVisitor visitor)
246                 {
247                         visitor.VisitPropertyDefinition (this);
248
249                         this.CustomAttributes.Accept (visitor);
250                 }
251         }
252 }