Merge pull request #463 from strawd/concurrent-requests
[mono.git] / mcs / tools / cil-strip / Mono.Cecil / TypeReference.cs
1 //
2 // TypeReference.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         internal class TypeReference : MemberReference, IGenericParameterProvider, ICustomAttributeProvider {
32
33                 string m_namespace;
34                 bool m_fullNameDiscarded;
35                 string m_fullName;
36                 protected bool m_isValueType;
37                 IMetadataScope m_scope;
38                 ModuleDefinition m_module;
39
40                 CustomAttributeCollection m_customAttrs;
41                 GenericParameterCollection m_genparams;
42
43                 public override string Name {
44                         get { return base.Name; }
45                         set {
46                                 base.Name = value;
47                                 m_fullNameDiscarded = true;
48                         }
49                 }
50
51                 public virtual string Namespace {
52                         get { return m_namespace; }
53                         set {
54                                 m_namespace = value;
55                                 m_fullNameDiscarded = true;
56                         }
57                 }
58
59                 public virtual bool IsValueType {
60                         get { return m_isValueType; }
61                         set { m_isValueType = value; }
62                 }
63
64                 public virtual ModuleDefinition Module {
65                         get { return m_module; }
66                         set { m_module = value; }
67                 }
68
69                 public bool HasCustomAttributes {
70                         get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
71                 }
72
73                 public CustomAttributeCollection CustomAttributes {
74                         get {
75                                 if (m_customAttrs == null)
76                                         m_customAttrs = new CustomAttributeCollection (this);
77
78                                 return m_customAttrs;
79                         }
80                 }
81
82                 public bool HasGenericParameters {
83                         get { return (m_genparams == null) ? false : (m_genparams.Count > 0); }
84                 }
85
86                 public GenericParameterCollection GenericParameters {
87                         get {
88                                 if (m_genparams == null)
89                                         m_genparams = new GenericParameterCollection (this);
90                                 return m_genparams;
91                         }
92                 }
93
94                 public virtual IMetadataScope Scope {
95                         get {
96                                 if (this.DeclaringType != null)
97                                         return this.DeclaringType.Scope;
98
99                                 return m_scope;
100                         }
101                 }
102
103                 public bool IsNested {
104                         get { return this.DeclaringType != null; }
105                 }
106
107                 public virtual string FullName {
108                         get {
109                                 if (m_fullName != null && !m_fullNameDiscarded)
110                                         return m_fullName;
111
112                                 if (this.IsNested)
113                                         return string.Concat (this.DeclaringType.FullName, "/", this.Name);
114
115                                 if (m_namespace == null || m_namespace.Length == 0)
116                                         return this.Name;
117
118                                 m_fullName = string.Concat (m_namespace, ".", this.Name);
119                                 m_fullNameDiscarded = false;
120                                 return m_fullName;
121                         }
122                 }
123
124                 protected TypeReference (string name, string ns) : base (name)
125                 {
126                         m_namespace = ns;
127                         m_fullNameDiscarded = false;
128                 }
129
130                 internal TypeReference (string name, string ns, IMetadataScope scope) : this (name, ns)
131                 {
132                         m_scope = scope;
133                 }
134
135                 public TypeReference (string name, string ns, IMetadataScope scope, bool valueType) :
136                         this (name, ns, scope)
137                 {
138                         m_isValueType = valueType;
139                 }
140
141                 public virtual TypeDefinition Resolve ()
142                 {
143                         ModuleDefinition module = Module;
144                         if (module == null)
145                                 return null;
146
147                         return module.Resolver.Resolve (this);
148                 }
149
150                 public virtual TypeReference GetOriginalType ()
151                 {
152                         return this;
153                 }
154
155                 internal void AttachToScope (IMetadataScope scope)
156                 {
157                         m_scope = scope;
158                 }
159
160                 public override void Accept (IReflectionVisitor visitor)
161                 {
162                         visitor.VisitTypeReference (this);
163                 }
164
165                 public override string ToString ()
166                 {
167                         return this.FullName;
168                 }
169         }
170 }