1 | // |
---|
2 | // GenericParameter.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Jb Evain (jbevain@gmail.com) |
---|
6 | // |
---|
7 | // Copyright (c) 2008 - 2011 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 | using System; |
---|
30 | |
---|
31 | using Mono.Collections.Generic; |
---|
32 | |
---|
33 | using Mono.Cecil.Metadata; |
---|
34 | |
---|
35 | namespace Mono.Cecil { |
---|
36 | |
---|
37 | public sealed class GenericParameter : TypeReference, ICustomAttributeProvider { |
---|
38 | |
---|
39 | readonly IGenericParameterProvider owner; |
---|
40 | |
---|
41 | ushort attributes; |
---|
42 | Collection<TypeReference> constraints; |
---|
43 | Collection<CustomAttribute> custom_attributes; |
---|
44 | |
---|
45 | public GenericParameterAttributes Attributes { |
---|
46 | get { return (GenericParameterAttributes) attributes; } |
---|
47 | set { attributes = (ushort) value; } |
---|
48 | } |
---|
49 | |
---|
50 | public int Position { |
---|
51 | get { |
---|
52 | if (owner == null) |
---|
53 | return -1; |
---|
54 | |
---|
55 | return owner.GenericParameters.IndexOf (this); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | public IGenericParameterProvider Owner { |
---|
60 | get { return owner; } |
---|
61 | } |
---|
62 | |
---|
63 | public bool HasConstraints { |
---|
64 | get { |
---|
65 | if (constraints != null) |
---|
66 | return constraints.Count > 0; |
---|
67 | |
---|
68 | if (HasImage) |
---|
69 | return Module.Read (this, (generic_parameter, reader) => reader.HasGenericConstraints (generic_parameter)); |
---|
70 | |
---|
71 | return false; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | public Collection<TypeReference> Constraints { |
---|
76 | get { |
---|
77 | if (constraints != null) |
---|
78 | return constraints; |
---|
79 | |
---|
80 | if (HasImage) |
---|
81 | return constraints = Module.Read (this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter)); |
---|
82 | |
---|
83 | return constraints = new Collection<TypeReference> (); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | public bool HasCustomAttributes { |
---|
88 | get { |
---|
89 | if (custom_attributes != null) |
---|
90 | return custom_attributes.Count > 0; |
---|
91 | |
---|
92 | return this.GetHasCustomAttributes (Module); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | public Collection<CustomAttribute> CustomAttributes { |
---|
97 | get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } |
---|
98 | } |
---|
99 | |
---|
100 | public override IMetadataScope Scope { |
---|
101 | get { |
---|
102 | if (owner.GenericParameterType == GenericParameterType.Method) |
---|
103 | return ((MethodReference) owner).DeclaringType.Scope; |
---|
104 | |
---|
105 | return ((TypeReference) owner).Scope; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | public override ModuleDefinition Module { |
---|
110 | get { return ((MemberReference) owner).Module; } |
---|
111 | } |
---|
112 | |
---|
113 | public override string Name { |
---|
114 | get { |
---|
115 | if (!string.IsNullOrEmpty (base.Name)) |
---|
116 | return base.Name; |
---|
117 | |
---|
118 | return base.Name = (owner.GenericParameterType == GenericParameterType.Type ? "!" : "!!") + Position; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | public override string Namespace { |
---|
123 | get { return string.Empty; } |
---|
124 | set { throw new InvalidOperationException (); } |
---|
125 | } |
---|
126 | |
---|
127 | public override string FullName { |
---|
128 | get { return Name; } |
---|
129 | } |
---|
130 | |
---|
131 | public override bool IsGenericParameter { |
---|
132 | get { return true; } |
---|
133 | } |
---|
134 | |
---|
135 | internal override bool ContainsGenericParameter { |
---|
136 | get { return true; } |
---|
137 | } |
---|
138 | |
---|
139 | public override MetadataType MetadataType { |
---|
140 | get { return (MetadataType) etype; } |
---|
141 | } |
---|
142 | |
---|
143 | #region GenericParameterAttributes |
---|
144 | |
---|
145 | public bool IsNonVariant { |
---|
146 | get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant); } |
---|
147 | set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant, value); } |
---|
148 | } |
---|
149 | |
---|
150 | public bool IsCovariant { |
---|
151 | get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant); } |
---|
152 | set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant, value); } |
---|
153 | } |
---|
154 | |
---|
155 | public bool IsContravariant { |
---|
156 | get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant); } |
---|
157 | set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant, value); } |
---|
158 | } |
---|
159 | |
---|
160 | public bool HasReferenceTypeConstraint { |
---|
161 | get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint); } |
---|
162 | set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint, value); } |
---|
163 | } |
---|
164 | |
---|
165 | public bool HasNotNullableValueTypeConstraint { |
---|
166 | get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint); } |
---|
167 | set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint, value); } |
---|
168 | } |
---|
169 | |
---|
170 | public bool HasDefaultConstructorConstraint { |
---|
171 | get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint); } |
---|
172 | set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint, value); } |
---|
173 | } |
---|
174 | |
---|
175 | #endregion |
---|
176 | |
---|
177 | public GenericParameter (IGenericParameterProvider owner) |
---|
178 | : this (string.Empty, owner) |
---|
179 | { |
---|
180 | } |
---|
181 | |
---|
182 | public GenericParameter (string name, IGenericParameterProvider owner) |
---|
183 | : base (string.Empty, name) |
---|
184 | { |
---|
185 | if (owner == null) |
---|
186 | throw new ArgumentNullException (); |
---|
187 | |
---|
188 | this.owner = owner; |
---|
189 | this.etype = owner.GenericParameterType == GenericParameterType.Type ? ElementType.Var : ElementType.MVar; |
---|
190 | } |
---|
191 | |
---|
192 | public override TypeDefinition Resolve () |
---|
193 | { |
---|
194 | return null; |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|