1 //
2 // ExportedType.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 namespace Mono.Cecil {
32 
33 	public class ExportedType : IMetadataTokenProvider {
34 
35 		string @namespace;
36 		string name;
37 		uint attributes;
38 		IMetadataScope scope;
39 		ModuleDefinition module;
40 		int identifier;
41 		ExportedType declaring_type;
42 		internal MetadataToken token;
43 
44 		public string Namespace {
45 			get { return @namespace; }
46 			set { @namespace = value; }
47 		}
48 
49 		public string Name {
50 			get { return name; }
51 			set { name = value; }
52 		}
53 
54 		public TypeAttributes Attributes {
55 			get { return (TypeAttributes) attributes; }
56 			set { attributes = (uint) value; }
57 		}
58 
59 		public IMetadataScope Scope {
60 			get {
61 				if (declaring_type != null)
62 					return declaring_type.Scope;
63 
64 				return scope;
65 			}
66 		}
67 
68 		public ExportedType DeclaringType {
69 			get { return declaring_type; }
70 			set { declaring_type = value; }
71 		}
72 
73 		public MetadataToken MetadataToken {
74 			get { return token; }
75 			set { token = value; }
76 		}
77 
78 		public int Identifier {
79 			get { return identifier; }
80 			set { identifier = value; }
81 		}
82 
83 		#region TypeAttributes
84 
85 		public bool IsNotPublic {
86 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NotPublic); }
87 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NotPublic, value); }
88 		}
89 
90 		public bool IsPublic {
91 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.Public); }
92 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.Public, value); }
93 		}
94 
95 		public bool IsNestedPublic {
96 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPublic); }
97 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPublic, value); }
98 		}
99 
100 		public bool IsNestedPrivate {
101 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPrivate); }
102 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPrivate, value); }
103 		}
104 
105 		public bool IsNestedFamily {
106 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamily); }
107 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamily, value); }
108 		}
109 
110 		public bool IsNestedAssembly {
111 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedAssembly); }
112 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedAssembly, value); }
113 		}
114 
115 		public bool IsNestedFamilyAndAssembly {
116 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamANDAssem); }
117 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamANDAssem, value); }
118 		}
119 
120 		public bool IsNestedFamilyOrAssembly {
121 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamORAssem); }
122 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamORAssem, value); }
123 		}
124 
125 		public bool IsAutoLayout {
126 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.AutoLayout); }
127 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.AutoLayout, value); }
128 		}
129 
130 		public bool IsSequentialLayout {
131 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.SequentialLayout); }
132 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.SequentialLayout, value); }
133 		}
134 
135 		public bool IsExplicitLayout {
136 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.ExplicitLayout); }
137 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.ExplicitLayout, value); }
138 		}
139 
140 		public bool IsClass {
141 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Class); }
142 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Class, value); }
143 		}
144 
145 		public bool IsInterface {
146 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Interface); }
147 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Interface, value); }
148 		}
149 
150 		public bool IsAbstract {
151 			get { return attributes.GetAttributes ((uint) TypeAttributes.Abstract); }
152 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Abstract, value); }
153 		}
154 
155 		public bool IsSealed {
156 			get { return attributes.GetAttributes ((uint) TypeAttributes.Sealed); }
157 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Sealed, value); }
158 		}
159 
160 		public bool IsSpecialName {
161 			get { return attributes.GetAttributes ((uint) TypeAttributes.SpecialName); }
162 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.SpecialName, value); }
163 		}
164 
165 		public bool IsImport {
166 			get { return attributes.GetAttributes ((uint) TypeAttributes.Import); }
167 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Import, value); }
168 		}
169 
170 		public bool IsSerializable {
171 			get { return attributes.GetAttributes ((uint) TypeAttributes.Serializable); }
172 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Serializable, value); }
173 		}
174 
175 		public bool IsAnsiClass {
176 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass); }
177 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass, value); }
178 		}
179 
180 		public bool IsUnicodeClass {
181 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.UnicodeClass); }
182 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.UnicodeClass, value); }
183 		}
184 
185 		public bool IsAutoClass {
186 			get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AutoClass); }
187 			set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AutoClass, value); }
188 		}
189 
190 		public bool IsBeforeFieldInit {
191 			get { return attributes.GetAttributes ((uint) TypeAttributes.BeforeFieldInit); }
192 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.BeforeFieldInit, value); }
193 		}
194 
195 		public bool IsRuntimeSpecialName {
196 			get { return attributes.GetAttributes ((uint) TypeAttributes.RTSpecialName); }
197 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.RTSpecialName, value); }
198 		}
199 
200 		public bool HasSecurity {
201 			get { return attributes.GetAttributes ((uint) TypeAttributes.HasSecurity); }
202 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.HasSecurity, value); }
203 		}
204 
205 		#endregion
206 
207 		public bool IsForwarder {
208 			get { return attributes.GetAttributes ((uint) TypeAttributes.Forwarder); }
209 			set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Forwarder, value); }
210 		}
211 
212 		public string FullName {
213 			get {
214 				if (declaring_type != null)
215 					return declaring_type.FullName + "/" + name;
216 
217 				if (string.IsNullOrEmpty (@namespace))
218 					return name;
219 
220 				return @namespace + "." + name;
221 			}
222 		}
223 
ExportedType(string @namespace, string name, ModuleDefinition module, IMetadataScope scope)224 		public ExportedType (string @namespace, string name, ModuleDefinition module, IMetadataScope scope)
225 		{
226 			this.@namespace = @namespace;
227 			this.name = name;
228 			this.scope = scope;
229 			this.module = module;
230 		}
231 
ToString()232 		public override string ToString ()
233 		{
234 			return FullName;
235 		}
236 
Resolve()237 		public TypeDefinition Resolve ()
238 		{
239 			return module.Resolve (CreateReference ());
240 		}
241 
CreateReference()242 		internal TypeReference CreateReference ()
243 		{
244 			return new TypeReference (@namespace, name, module, scope) {
245 				DeclaringType = declaring_type != null ? declaring_type.CreateReference () : null,
246 			};
247 		}
248 	}
249 }
250