~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Mono.Cecil/Mono.Cecil/Mono.Cecil.Cil/Symbols.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Symbols.cs
 
3
//
 
4
// Author:
 
5
//   Jb Evain (jbevain@gmail.com)
 
6
//
 
7
// Copyright (c) 2008 - 2010 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
using System.IO;
 
31
using System.Runtime.InteropServices;
 
32
using SR = System.Reflection;
 
33
 
 
34
using Mono.Collections.Generic;
 
35
 
 
36
namespace Mono.Cecil.Cil {
 
37
 
 
38
        [StructLayout (LayoutKind.Sequential)]
 
39
        public struct ImageDebugDirectory {
 
40
                public int Characteristics;
 
41
                public int TimeDateStamp;
 
42
                public short MajorVersion;
 
43
                public short MinorVersion;
 
44
                public int Type;
 
45
                public int SizeOfData;
 
46
                public int AddressOfRawData;
 
47
                public int PointerToRawData;
 
48
        }
 
49
 
 
50
        public sealed class Scope : IVariableDefinitionProvider {
 
51
 
 
52
                Instruction start;
 
53
                Instruction end;
 
54
 
 
55
                Collection<Scope> scopes;
 
56
                Collection<VariableDefinition> variables;
 
57
 
 
58
                public Instruction Start {
 
59
                        get { return start; }
 
60
                        set { start = value; }
 
61
                }
 
62
 
 
63
                public Instruction End {
 
64
                        get { return end; }
 
65
                        set { end = value; }
 
66
                }
 
67
 
 
68
                public bool HasScopes {
 
69
                        get { return !scopes.IsNullOrEmpty (); }
 
70
                }
 
71
 
 
72
                public Collection<Scope> Scopes {
 
73
                        get {
 
74
                                if (scopes == null)
 
75
                                        scopes = new Collection<Scope> ();
 
76
 
 
77
                                return scopes;
 
78
                        }
 
79
                }
 
80
 
 
81
                public bool HasVariables {
 
82
                        get { return !variables.IsNullOrEmpty (); }
 
83
                }
 
84
 
 
85
                public Collection<VariableDefinition> Variables {
 
86
                        get {
 
87
                                if (variables == null)
 
88
                                        variables = new Collection<VariableDefinition> ();
 
89
 
 
90
                                return variables;
 
91
                        }
 
92
                }
 
93
        }
 
94
 
 
95
        public struct InstructionSymbol {
 
96
 
 
97
                public readonly int Offset;
 
98
                public readonly SequencePoint SequencePoint;
 
99
 
 
100
                public InstructionSymbol (int offset, SequencePoint sequencePoint)
 
101
                {
 
102
                        this.Offset = offset;
 
103
                        this.SequencePoint = sequencePoint;
 
104
                }
 
105
        }
 
106
 
 
107
        public sealed class MethodSymbols {
 
108
 
 
109
                internal int code_size;
 
110
                internal string method_name;
 
111
                internal MetadataToken method_token;
 
112
                internal MetadataToken local_var_token;
 
113
                internal Collection<VariableDefinition> variables;
 
114
                internal Collection<InstructionSymbol> instructions;
 
115
 
 
116
                public bool HasVariables {
 
117
                        get { return !variables.IsNullOrEmpty (); }
 
118
                }
 
119
 
 
120
                public Collection<VariableDefinition> Variables {
 
121
                        get {
 
122
                                if (variables == null)
 
123
                                        variables = new Collection<VariableDefinition> ();
 
124
 
 
125
                                return variables;
 
126
                        }
 
127
                }
 
128
 
 
129
                public Collection<InstructionSymbol> Instructions {
 
130
                        get {
 
131
                                if (instructions == null)
 
132
                                        instructions = new Collection<InstructionSymbol> ();
 
133
 
 
134
                                return instructions;
 
135
                        }
 
136
                }
 
137
 
 
138
                public int CodeSize {
 
139
                        get { return code_size; }
 
140
                }
 
141
 
 
142
                public string MethodName {
 
143
                        get { return method_name; }
 
144
                }
 
145
 
 
146
                public MetadataToken MethodToken {
 
147
                        get { return method_token; }
 
148
                }
 
149
 
 
150
                public MetadataToken LocalVarToken {
 
151
                        get { return local_var_token; }
 
152
                }
 
153
 
 
154
                public MethodSymbols (string methodName)
 
155
                {
 
156
                        this.method_name = methodName;
 
157
                }
 
158
        }
 
159
 
 
160
        public delegate Instruction InstructionMapper (int offset);
 
161
 
 
162
        public interface ISymbolReader : IDisposable {
 
163
 
 
164
                bool ProcessDebugHeader (ImageDebugDirectory directory, byte [] header);
 
165
                void Read (MethodBody body, InstructionMapper mapper);
 
166
                void Read (MethodSymbols symbols);
 
167
        }
 
168
 
 
169
        public interface ISymbolReaderProvider {
 
170
 
 
171
                ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName);
 
172
                ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream);
 
173
        }
 
174
 
 
175
        static class SymbolProvider {
 
176
 
 
177
                static readonly string symbol_kind = Type.GetType ("Mono.Runtime") != null ? "Mdb" : "Pdb";
 
178
 
 
179
                static SR.AssemblyName GetPlatformSymbolAssemblyName ()
 
180
                {
 
181
                        var cecil_name = typeof (SymbolProvider).Assembly.GetName ();
 
182
 
 
183
                        var name = new SR.AssemblyName {
 
184
                                Name = "Mono.Cecil." + symbol_kind,
 
185
                                Version = cecil_name.Version,
 
186
                        };
 
187
 
 
188
                        name.SetPublicKeyToken (cecil_name.GetPublicKeyToken ());
 
189
 
 
190
                        return name;
 
191
                }
 
192
 
 
193
                static Type GetPlatformType (string fullname)
 
194
                {
 
195
                        var type = Type.GetType (fullname);
 
196
                        if (type != null)
 
197
                                return type;
 
198
 
 
199
                        var assembly_name = GetPlatformSymbolAssemblyName ();
 
200
 
 
201
                        type = Type.GetType (fullname + ", " + assembly_name.FullName);
 
202
                        if (type != null)
 
203
                                return type;
 
204
 
 
205
                        try {
 
206
                                var assembly = SR.Assembly.Load (assembly_name);
 
207
                                if (assembly != null)
 
208
                                        return assembly.GetType (fullname);
 
209
                        } catch (FileNotFoundException) {
 
210
#if !CF
 
211
                        } catch (FileLoadException) {
 
212
#endif
 
213
                        }
 
214
 
 
215
                        return null;
 
216
                }
 
217
 
 
218
                static ISymbolReaderProvider reader_provider;
 
219
 
 
220
                public static ISymbolReaderProvider GetPlatformReaderProvider ()
 
221
                {
 
222
                        if (reader_provider != null)
 
223
                                return reader_provider;
 
224
 
 
225
                        var type = GetPlatformType (GetProviderTypeName ("ReaderProvider"));
 
226
                        if (type == null)
 
227
                                return null;
 
228
 
 
229
                        return reader_provider = (ISymbolReaderProvider) Activator.CreateInstance (type);
 
230
                }
 
231
 
 
232
                static string GetProviderTypeName (string name)
 
233
                {
 
234
                        return "Mono.Cecil." + symbol_kind + "." + symbol_kind + name;
 
235
                }
 
236
 
 
237
#if !READ_ONLY
 
238
 
 
239
                static ISymbolWriterProvider writer_provider;
 
240
 
 
241
                public static ISymbolWriterProvider GetPlatformWriterProvider ()
 
242
                {
 
243
                        if (writer_provider != null)
 
244
                                return writer_provider;
 
245
 
 
246
                        var type = GetPlatformType (GetProviderTypeName ("WriterProvider"));
 
247
                        if (type == null)
 
248
                                return null;
 
249
 
 
250
                        return writer_provider = (ISymbolWriterProvider) Activator.CreateInstance (type);
 
251
                }
 
252
 
 
253
#endif
 
254
        }
 
255
 
 
256
#if !READ_ONLY
 
257
 
 
258
        public interface ISymbolWriter : IDisposable {
 
259
 
 
260
                bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header);
 
261
                void Write (MethodBody body);
 
262
                void Write (MethodSymbols symbols);
 
263
        }
 
264
 
 
265
        public interface ISymbolWriterProvider {
 
266
 
 
267
                ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName);
 
268
                ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream);
 
269
        }
 
270
 
 
271
#endif
 
272
}