5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2011 Jb Evain
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
31
using Mono.Collections.Generic;
33
using Mono.Cecil.Metadata;
35
namespace Mono.Cecil {
37
public sealed class GenericParameter : TypeReference, ICustomAttributeProvider {
39
readonly IGenericParameterProvider owner;
42
Collection<TypeReference> constraints;
43
Collection<CustomAttribute> custom_attributes;
45
public GenericParameterAttributes Attributes {
46
get { return (GenericParameterAttributes) attributes; }
47
set { attributes = (ushort) value; }
55
return owner.GenericParameters.IndexOf (this);
59
public IGenericParameterProvider Owner {
63
public bool HasConstraints {
65
if (constraints != null)
66
return constraints.Count > 0;
69
return Module.Read (this, (generic_parameter, reader) => reader.HasGenericConstraints (generic_parameter));
75
public Collection<TypeReference> Constraints {
77
if (constraints != null)
81
return constraints = Module.Read (this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter));
83
return constraints = new Collection<TypeReference> ();
87
public bool HasCustomAttributes {
89
if (custom_attributes != null)
90
return custom_attributes.Count > 0;
92
return this.GetHasCustomAttributes (Module);
96
public Collection<CustomAttribute> CustomAttributes {
97
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
100
public override IMetadataScope Scope {
102
if (owner.GenericParameterType == GenericParameterType.Method)
103
return ((MethodReference) owner).DeclaringType.Scope;
105
return ((TypeReference) owner).Scope;
109
public override ModuleDefinition Module {
110
get { return ((MemberReference) owner).Module; }
113
public override string Name {
115
if (!string.IsNullOrEmpty (base.Name))
118
return base.Name = (owner.GenericParameterType == GenericParameterType.Type ? "!" : "!!") + Position;
122
public override string Namespace {
123
get { return string.Empty; }
124
set { throw new InvalidOperationException (); }
127
public override string FullName {
131
public override bool IsGenericParameter {
135
internal override bool ContainsGenericParameter {
139
public override MetadataType MetadataType {
140
get { return (MetadataType) etype; }
143
#region GenericParameterAttributes
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); }
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); }
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); }
160
public bool HasReferenceTypeConstraint {
161
get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint); }
162
set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint, value); }
165
public bool HasNotNullableValueTypeConstraint {
166
get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint); }
167
set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint, value); }
170
public bool HasDefaultConstructorConstraint {
171
get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint); }
172
set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint, value); }
177
public GenericParameter (IGenericParameterProvider owner)
178
: this (string.Empty, owner)
182
public GenericParameter (string name, IGenericParameterProvider owner)
183
: base (string.Empty, name)
186
throw new ArgumentNullException ();
189
this.etype = owner.GenericParameterType == GenericParameterType.Type ? ElementType.Var : ElementType.MVar;
192
public override TypeDefinition Resolve ()