Debian Codegraph @

mcs/tools/cil-strip/Mono.Cecil/ModuleDefinition.cs

raw
1 //
2 // ModuleDefinition.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
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 using System;
32 using SR = System.Reflection;
33 using SS = System.Security;
34 using SSP = System.Security.Permissions;
35 using System.Text;
36
37 using Mono.Cecil.Cil;
38 using Mono.Cecil.Binary;
39 using Mono.Cecil.Metadata;
40
41 internal sealed class ModuleDefinition : ModuleReference, ICustomAttributeProvider, IMetadataScope,
42 IReflectionStructureVisitable, IReflectionVisitable {
43
44 Guid m_mvid;
45 bool m_main;
46 bool m_manifestOnly;
47
48 AssemblyNameReferenceCollection m_asmRefs;
49 ModuleReferenceCollection m_modRefs;
50 ResourceCollection m_res;
51 TypeDefinitionCollection m_types;
52 TypeReferenceCollection m_refs;
53 ExternTypeCollection m_externs;
54 MemberReferenceCollection m_members;
55 CustomAttributeCollection m_customAttrs;
56
57 AssemblyDefinition m_asm;
58 Image m_image;
59
60 ImageReader m_imgReader;
61 ReflectionController m_controller;
62 MetadataResolver m_resolver;
63 SecurityDeclarationReader m_secReader;
64
65 public Guid Mvid {
66 get { return m_mvid; }
67 set { m_mvid = value; }
68 }
69
70 public bool Main {
71 get { return m_main; }
72 set { m_main = value; }
73 }
74
75 public AssemblyNameReferenceCollection AssemblyReferences {
76 get { return m_asmRefs; }
77 }
78
79 public ModuleReferenceCollection ModuleReferences {
80 get { return m_modRefs; }
81 }
82
83 public ResourceCollection Resources {
84 get { return m_res; }
85 }
86
87 public TypeDefinitionCollection Types {
88 get { return m_types; }
89 }
90
91 public TypeReferenceCollection TypeReferences {
92 get { return m_refs; }
93 }
94
95 public MemberReferenceCollection MemberReferences {
96 get { return m_members; }
97 }
98
99 public ExternTypeCollection ExternTypes {
100 get {
101 if (m_externs == null)
102 m_externs = new ExternTypeCollection (this);
103
104 return m_externs;
105 }
106 }
107
108 public bool HasCustomAttributes {
109 get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
110 }
111
112 public CustomAttributeCollection CustomAttributes {
113 get {
114 if (m_customAttrs == null)
115 m_customAttrs = new CustomAttributeCollection (this);
116
117 return m_customAttrs;
118 }
119 }
120
121 public AssemblyDefinition Assembly {
122 get { return m_asm; }
123 }
124
125 internal ReflectionController Controller {
126 get { return m_controller; }
127 }
128
129 internal MetadataResolver Resolver {
130 get { return m_resolver; }
131 }
132
133 internal ImageReader ImageReader {
134 get { return m_imgReader; }
135 }
136
137 public Image Image {
138 get { return m_image; }
139 set {
140 m_image = value;
141 m_secReader = null;
142 }
143 }
144
145 public ModuleDefinition (string name, AssemblyDefinition asm) :
146 this (name, asm, null, false)
147 {
148 }
149
150 public ModuleDefinition (string name, AssemblyDefinition asm, bool main) :
151 this (name, asm, null, main)
152 {
153 }
154
155 internal ModuleDefinition (string name, AssemblyDefinition asm, StructureReader reader, bool main) : base (name)
156 {
157 if (asm == null)
158 throw new ArgumentNullException ("asm");
159 if (name == null || name.Length == 0)
160 throw new ArgumentNullException ("name");
161
162 m_asm = asm;
163 m_main = main;
164 #if !CF_1_0
165 m_mvid = Guid.NewGuid ();
166 #endif
167 if (reader != null) {
168 m_image = reader.Image;
169 m_imgReader = reader.ImageReader;
170 m_manifestOnly = reader.ManifestOnly;
171 } else
172 m_image = Image.CreateImage ();
173
174 m_modRefs = new ModuleReferenceCollection (this);
175 m_asmRefs = new AssemblyNameReferenceCollection (this);
176 m_res = new ResourceCollection (this);
177 m_types = new TypeDefinitionCollection (this);
178 m_refs = new TypeReferenceCollection (this);
179 m_members = new MemberReferenceCollection (this);
180
181 m_controller = new ReflectionController (this);
182 m_resolver = new MetadataResolver (asm);
183 }
184
185 public IMetadataTokenProvider LookupByToken (MetadataToken token)
186 {
187 return m_controller.Reader.LookupByToken (token);
188 }
189
190 public IMetadataTokenProvider LookupByToken (TokenType table, int rid)
191 {
192 return LookupByToken (new MetadataToken (table, (uint) rid));
193 }
194
195 void CheckContext (TypeDefinition context)
196 {
197 if (context.Module != this)
198 throw new ArgumentException ("The context parameter does not belongs to this module");
199
200 CheckGenericParameterProvider (context);
201 }
202
203 void CheckContext (MethodDefinition context)
204 {
205 CheckGenericParameterProvider (context);
206 }
207
208 static void CheckGenericParameterProvider (IGenericParameterProvider context)
209 {
210 if (context == null)
211 throw new ArgumentNullException ("context");
212 if (context.GenericParameters.Count == 0)
213 throw new ArgumentException ("The context parameter is not a generic type");
214 }
215
216 ImportContext GetContext ()
217 {
218 return new ImportContext (m_controller.Importer);
219 }
220
221 static ImportContext GetContext (IImporter importer)
222 {
223 return new ImportContext (importer);
224 }
225
226 ImportContext GetContext (TypeDefinition context)
227 {
228 return new ImportContext (m_controller.Importer, context);
229 }
230
231 ImportContext GetContext (MethodDefinition context)
232 {
233 return new ImportContext (m_controller.Importer, context);
234 }
235
236 static ImportContext GetContext (IImporter importer, TypeDefinition context)
237 {
238 return new ImportContext (importer, context);
239 }
240
241 public TypeReference Import (Type type)
242 {
243 if (type == null)
244 throw new ArgumentNullException ("type");
245
246 return m_controller.Helper.ImportSystemType (type, GetContext ());
247 }
248
249 public TypeReference Import (Type type, TypeDefinition context)
250 {
251 if (type == null)
252 throw new ArgumentNullException ("type");
253 CheckContext (context);
254
255 return m_controller.Helper.ImportSystemType (type, GetContext (context));
256 }
257
258 public TypeReference Import (Type type, MethodDefinition context)
259 {
260 if (type == null)
261 throw new ArgumentNullException ("type");
262 CheckContext (context);
263
264 return m_controller.Helper.ImportSystemType (type, GetContext (context));
265 }
266
267 public MethodReference Import (SR.MethodBase meth)
268 {
269 if (meth == null)
270 throw new ArgumentNullException ("meth");
271
272 if (meth is SR.ConstructorInfo)
273 return m_controller.Helper.ImportConstructorInfo (
274 meth as SR.ConstructorInfo, GetContext ());
275 else
276 return m_controller.Helper.ImportMethodInfo (
277 meth as SR.MethodInfo, GetContext ());
278 }
279
280 public MethodReference Import (SR.MethodBase meth, TypeDefinition context)
281 {
282 if (meth == null)
283 throw new ArgumentNullException ("meth");
284 CheckContext (context);
285
286 ImportContext import_context = GetContext (context);
287
288 if (meth is SR.ConstructorInfo)
289 return m_controller.Helper.ImportConstructorInfo (
290 meth as SR.ConstructorInfo, import_context);
291 else
292 return m_controller.Helper.ImportMethodInfo (
293 meth as SR.MethodInfo, import_context);
294 }
295
296 public FieldReference Import (SR.FieldInfo field)
297 {
298 if (field == null)
299 throw new ArgumentNullException ("field");
300
301 return m_controller.Helper.ImportFieldInfo (field, GetContext ());
302 }
303
304 public FieldReference Import (SR.FieldInfo field, TypeDefinition context)
305 {
306 if (field == null)
307 throw new ArgumentNullException ("field");
308 CheckContext (context);
309
310 return m_controller.Helper.ImportFieldInfo (field, GetContext (context));
311 }
312
313 public FieldReference Import (SR.FieldInfo field, MethodDefinition context)
314 {
315 if (field == null)
316 throw new ArgumentNullException ("field");
317 CheckContext (context);
318
319 return m_controller.Helper.ImportFieldInfo (field, GetContext (context));
320 }
321
322 public TypeReference Import (TypeReference type)
323 {
324 if (type == null)
325 throw new ArgumentNullException ("type");
326
327 return m_controller.Importer.ImportTypeReference (type, GetContext ());
328 }
329
330 public TypeReference Import (TypeReference type, TypeDefinition context)
331 {
332 if (type == null)
333 throw new ArgumentNullException ("type");
334 CheckContext (context);
335
336 return m_controller.Importer.ImportTypeReference (type, GetContext (context));
337 }
338
339 public TypeReference Import (TypeReference type, MethodDefinition context)
340 {
341 if (type == null)
342 throw new ArgumentNullException ("type");
343 CheckContext (context);
344
345 return m_controller.Importer.ImportTypeReference (type, GetContext (context));
346 }
347
348 public MethodReference Import (MethodReference meth)
349 {
350 if (meth == null)
351 throw new ArgumentNullException ("meth");
352
353 return m_controller.Importer.ImportMethodReference (meth, GetContext ());
354 }
355
356 public MethodReference Import (MethodReference meth, TypeDefinition context)
357 {
358 if (meth == null)
359 throw new ArgumentNullException ("meth");
360 CheckContext (context);
361
362 return m_controller.Importer.ImportMethodReference (meth, GetContext (context));
363 }
364
365 public MethodReference Import (MethodReference meth, MethodDefinition context)
366 {
367 if (meth == null)
368 throw new ArgumentNullException ("meth");
369 CheckContext (context);
370
371 return m_controller.Importer.ImportMethodReference (meth, GetContext (context));
372 }
373
374 public FieldReference Import (FieldReference field)
375 {
376 if (field == null)
377 throw new ArgumentNullException ("field");
378
379 return m_controller.Importer.ImportFieldReference (field, GetContext ());
380 }
381
382 public FieldReference Import (FieldReference field, TypeDefinition context)
383 {
384 if (field == null)
385 throw new ArgumentNullException ("field");
386 CheckContext (context);
387
388 return m_controller.Importer.ImportFieldReference (field, GetContext (context));
389 }
390
391 public FieldReference Import (FieldReference field, MethodDefinition context)
392 {
393 if (field == null)
394 throw new ArgumentNullException ("field");
395 CheckContext (context);
396
397 return m_controller.Importer.ImportFieldReference (field, GetContext (context));
398 }
399
400 static FieldDefinition ImportFieldDefinition (FieldDefinition field, ImportContext context)
401 {
402 return FieldDefinition.Clone (field, context);
403 }
404
405 static MethodDefinition ImportMethodDefinition (MethodDefinition meth, ImportContext context)
406 {
407 return MethodDefinition.Clone (meth, context);
408 }
409
410 static TypeDefinition ImportTypeDefinition (TypeDefinition type, ImportContext context)
411 {
412 return TypeDefinition.Clone (type, context);
413 }
414
415 public TypeDefinition Inject (TypeDefinition type)
416 {
417 return Inject (type, m_controller.Importer);
418 }
419
420 public TypeDefinition Inject (TypeDefinition type, IImporter importer)
421 {
422 if (type == null)
423 throw new ArgumentNullException ("type");
424 if (importer == null)
425 throw new ArgumentNullException ("importer");
426
427 TypeDefinition definition = ImportTypeDefinition (type, GetContext (importer));
428 this.Types.Add (definition);
429 return definition;
430 }
431
432 public TypeDefinition Inject (TypeDefinition type, TypeDefinition context)
433 {
434 return Inject (type, context, m_controller.Importer);
435 }
436
437 public TypeDefinition Inject (TypeDefinition type, TypeDefinition context, IImporter importer)
438 {
439 Check (type, context, importer);
440
441 TypeDefinition definition = ImportTypeDefinition (type, GetContext (importer, context));
442 context.NestedTypes.Add (definition);
443 return definition;
444 }
445
446 public MethodDefinition Inject (MethodDefinition meth, TypeDefinition context)
447 {
448 return Inject (meth, context, m_controller.Importer);
449 }
450
451 void Check (IMemberDefinition definition, TypeDefinition context, IImporter importer)
452 {
453 if (definition == null)
454 throw new ArgumentNullException ("definition");
455 if (context == null)
456 throw new ArgumentNullException ("context");
457 if (importer == null)
458 throw new ArgumentNullException ("importer");
459 if (context.Module != this)
460 throw new ArgumentException ("The context parameter does not belongs to this module");
461 }
462
463 public MethodDefinition Inject (MethodDefinition meth, TypeDefinition context, IImporter importer)
464 {
465 Check (meth, context, importer);
466
467 MethodDefinition definition = ImportMethodDefinition (meth, GetContext (importer, context));
468 context.Methods.Add (definition);
469 return definition;
470 }
471
472 public FieldDefinition Inject (FieldDefinition field, TypeDefinition context)
473 {
474 return Inject (field, context, m_controller.Importer);
475 }
476
477 public FieldDefinition Inject (FieldDefinition field, TypeDefinition context, IImporter importer)
478 {
479 Check (field, context, importer);
480
481 FieldDefinition definition = ImportFieldDefinition (field, GetContext (importer, context));
482 context.Fields.Add (definition);
483 return definition;
484 }
485
486 public void FullLoad ()
487 {
488 if (m_manifestOnly)
489 m_controller.Reader.VisitModuleDefinition (this);
490
491 foreach (TypeDefinition type in this.Types) {
492 foreach (MethodDefinition meth in type.Methods)
493 meth.LoadBody ();
494 foreach (MethodDefinition ctor in type.Constructors)
495 ctor.LoadBody ();
496 }
497
498 if (m_controller.Reader.SymbolReader == null)
499 return;
500
501 m_controller.Reader.SymbolReader.Dispose ();
502 m_controller.Reader.SymbolReader = null;
503 }
504
505 public void LoadSymbols ()
506 {
507 m_controller.Reader.SymbolReader = SymbolStoreHelper.GetReader (this);
508 }
509
510 public void LoadSymbols (ISymbolReader reader)
511 {
512 m_controller.Reader.SymbolReader = reader;
513 }
514
515 public void SaveSymbols ()
516 {
517 m_controller.Writer.SaveSymbols = true;
518 }
519
520 public void SaveSymbols (ISymbolWriter writer)
521 {
522 SaveSymbols ();
523 m_controller.Writer.SymbolWriter = writer;
524 }
525
526 public void SaveSymbols (string outputDirectory)
527 {
528 SaveSymbols ();
529 m_controller.Writer.OutputFile = outputDirectory;
530 }
531
532 public void SaveSymbols (string outputDirectory, ISymbolWriter writer)
533 {
534 SaveSymbols (outputDirectory);
535 m_controller.Writer.SymbolWriter = writer;
536 }
537
538 public byte [] GetAsByteArray (CustomAttribute ca)
539 {
540 CustomAttribute customAttr = ca;
541 if (!ca.Resolved)
542 if (customAttr.Blob != null)
543 return customAttr.Blob;
544 else
545 return new byte [0];
546
547 return m_controller.Writer.SignatureWriter.CompressCustomAttribute (
548 ReflectionWriter.GetCustomAttributeSig (ca), ca.Constructor);
549 }
550
551 public byte [] GetAsByteArray (SecurityDeclaration dec)
552 {
553 // TODO - add support for 2.0 format
554 // note: the 1.x format is still supported in 2.0 so this isn't an immediate problem
555 if (!dec.Resolved)
556 return dec.Blob;
557
558 #if !CF_1_0 && !CF_2_0
559 if (dec.PermissionSet != null)
560 return Encoding.Unicode.GetBytes (dec.PermissionSet.ToXml ().ToString ());
561 #endif
562
563 return new byte [0];
564 }
565
566 public CustomAttribute FromByteArray (MethodReference ctor, byte [] data)
567 {
568 return m_controller.Reader.GetCustomAttribute (ctor, data);
569 }
570
571 public SecurityDeclaration FromByteArray (SecurityAction action, byte [] declaration)
572 {
573 if (m_secReader == null)
574 m_secReader = new SecurityDeclarationReader (Image.MetadataRoot, m_controller.Reader);
575 return m_secReader.FromByteArray (action, declaration);
576 }
577
578 public override void Accept (IReflectionStructureVisitor visitor)
579 {
580 visitor.VisitModuleDefinition (this);
581
582 this.AssemblyReferences.Accept (visitor);
583 this.ModuleReferences.Accept (visitor);
584 this.Resources.Accept (visitor);
585 }
586
587 public void Accept (IReflectionVisitor visitor)
588 {
589 visitor.VisitModuleDefinition (this);
590
591 this.Types.Accept (visitor);
592 this.TypeReferences.Accept (visitor);
593 }
594
595 public override string ToString ()
596 {
597 string s = (m_main ? "(main), Mvid=" : "Mvid=");
598 return s + m_mvid;
599 }
600 }
601 }