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