2004-07-30 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / Mono.CSharp.Debugger / MonoSymbolTable.cs
1 //
2 // Mono.CSharp.Debugger/MonoSymbolTable.cs
3 //
4 // Author:
5 //   Martin Baulig (martin@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Text;
34 using System.IO;
35
36 //
37 // Parts which are actually written into the symbol file are marked with
38 //
39 //         #region This is actually written to the symbol file
40 //         #endregion
41 //
42 // Please do not modify these regions without previously talking to me.
43 //
44 // All changes to the file format must be synchronized in several places:
45 //
46 // a) The fields in these regions (and their order) must match the actual
47 //    contents of the symbol file.
48 //
49 //    This helps people to understand the symbol file format without reading
50 //    too much source code, ie. you look at the appropriate region and then
51 //    you know what's actually in the file.
52 //
53 //    It is also required to help me enforce b).
54 //
55 // b) The regions must be kept in sync with the unmanaged code in
56 //    mono/metadata/debug-mono-symfile.h
57 //
58 // When making changes to the file format, you must also increase two version
59 // numbers:
60 //
61 // i)  OffsetTable.Version in this file.
62 // ii) MONO_SYMBOL_FILE_VERSION in mono/metadata/debug-mono-symfile.h
63 //
64 // After doing so, recompile everything, including the debugger.  Symbol files
65 // with different versions are incompatible to each other and the debugger and
66 // the runtime enfore this, so you need to recompile all your assemblies after
67 // changing the file format.
68 //
69
70 namespace Mono.CompilerServices.SymbolWriter
71 {
72         public struct OffsetTable
73         {
74                 public const int  Version = 38;
75                 public const long Magic   = 0x45e82623fd7fa614;
76
77                 #region This is actually written to the symbol file
78                 public int TotalFileSize;
79                 public int DataSectionOffset;
80                 public int DataSectionSize;
81                 public int SourceCount;
82                 public int SourceTableOffset;
83                 public int SourceTableSize;
84                 public int MethodCount;
85                 public int MethodTableOffset;
86                 public int MethodTableSize;
87                 public int TypeCount;
88                 #endregion
89
90                 internal OffsetTable (BinaryReader reader)
91                 {
92                         TotalFileSize = reader.ReadInt32 ();
93                         DataSectionOffset = reader.ReadInt32 ();
94                         DataSectionSize = reader.ReadInt32 ();
95                         SourceCount = reader.ReadInt32 ();
96                         SourceTableOffset = reader.ReadInt32 ();
97                         SourceTableSize = reader.ReadInt32 ();
98                         MethodCount = reader.ReadInt32 ();
99                         MethodTableOffset = reader.ReadInt32 ();
100                         MethodTableSize = reader.ReadInt32 ();
101                         TypeCount = reader.ReadInt32 ();
102                 }
103
104                 internal void Write (BinaryWriter bw)
105                 {
106                         bw.Write (TotalFileSize);
107                         bw.Write (DataSectionOffset);
108                         bw.Write (DataSectionSize);
109                         bw.Write (SourceCount);
110                         bw.Write (SourceTableOffset);
111                         bw.Write (SourceTableSize);
112                         bw.Write (MethodCount);
113                         bw.Write (MethodTableOffset);
114                         bw.Write (MethodTableSize);
115                         bw.Write (TypeCount);
116                 }
117
118                 public override string ToString ()
119                 {
120                         return String.Format (
121                                 "OffsetTable [{0} - {1}:{2} - {3}:{4}:{5} - {6}:{7}:{8} - {9}]",
122                                 TotalFileSize, DataSectionOffset, DataSectionSize, SourceCount,
123                                 SourceTableOffset, SourceTableSize, MethodCount, MethodTableOffset,
124                                 MethodTableSize, TypeCount);
125                 }
126         }
127
128         public struct LineNumberEntry
129         {
130                 #region This is actually written to the symbol file
131                 public readonly int Row;
132                 public readonly int Offset;
133                 #endregion
134
135                 public LineNumberEntry (int row, int offset)
136                 {
137                         this.Row = row;
138                         this.Offset = offset;
139                 }
140
141                 public static LineNumberEntry Null = new LineNumberEntry (0, 0);
142
143                 internal LineNumberEntry (BinaryReader reader)
144                 {
145                         Row = reader.ReadInt32 ();
146                         Offset = reader.ReadInt32 ();
147                 }
148
149                 internal void Write (BinaryWriter bw)
150                 {
151                         bw.Write (Row);
152                         bw.Write (Offset);
153                 }
154
155                 private class OffsetComparerClass : IComparer
156                 {
157                         public int Compare (object a, object b)
158                         {
159                                 LineNumberEntry l1 = (LineNumberEntry) a;
160                                 LineNumberEntry l2 = (LineNumberEntry) b;
161
162                                 if (l1.Offset < l2.Offset)
163                                         return -1;
164                                 else if (l1.Offset > l2.Offset)
165                                         return 1;
166                                 else
167                                         return 0;
168                         }
169                 }
170
171                 private class RowComparerClass : IComparer
172                 {
173                         public int Compare (object a, object b)
174                         {
175                                 LineNumberEntry l1 = (LineNumberEntry) a;
176                                 LineNumberEntry l2 = (LineNumberEntry) b;
177
178                                 if (l1.Row < l2.Row)
179                                         return -1;
180                                 else if (l1.Row > l2.Row)
181                                         return 1;
182                                 else
183                                         return 0;
184                         }
185                 }
186
187                 public static readonly IComparer OffsetComparer = new OffsetComparerClass ();
188                 public static readonly IComparer RowComparer = new RowComparerClass ();
189
190                 public override string ToString ()
191                 {
192                         return String.Format ("[Line {0}:{1}]", Row, Offset);
193                 }
194         }
195
196         public class LexicalBlockEntry
197         {
198                 public int Index;
199                 #region This is actually written to the symbol file
200                 public int StartOffset;
201                 public int EndOffset;
202                 #endregion
203
204                 public LexicalBlockEntry (int index, int start_offset)
205                 {
206                         this.Index = index;
207                         this.StartOffset = start_offset;
208                 }
209
210                 internal LexicalBlockEntry (int index, MyBinaryReader reader)
211                 {
212                         this.Index = index;
213                         this.StartOffset = reader.ReadInt32 ();
214                         this.EndOffset = reader.ReadInt32 ();
215                 }
216
217                 public void Close (int end_offset)
218                 {
219                         this.EndOffset = end_offset;
220                 }
221
222                 internal void Write (MyBinaryWriter bw)
223                 {
224                         bw.Write (StartOffset);
225                         bw.Write (EndOffset);
226                 }
227
228                 public override string ToString ()
229                 {
230                         return String.Format ("[LexicalBlock {0}:{1}]", StartOffset, EndOffset);
231                 }
232         }
233
234         public struct LocalVariableEntry
235         {
236                 #region This is actually written to the symbol file
237                 public readonly string Name;
238                 public readonly byte[] Signature;
239                 public readonly int BlockIndex;
240                 #endregion
241
242                 public LocalVariableEntry (string Name, byte[] Signature, int BlockIndex)
243                 {
244                         this.Name = Name;
245                         this.Signature = Signature;
246                         this.BlockIndex = BlockIndex;
247                 }
248
249                 internal LocalVariableEntry (MyBinaryReader reader)
250                 {
251                         Name = reader.ReadString ();
252                         int sig_length = reader.ReadLeb128 ();
253                         Signature = reader.ReadBytes (sig_length);
254                         BlockIndex = reader.ReadLeb128 ();
255                 }
256
257                 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
258                 {
259                         bw.Write (Name);
260                         bw.WriteLeb128 ((int) Signature.Length);
261                         bw.Write (Signature);
262                         bw.WriteLeb128 (BlockIndex);
263                 }
264
265                 public override string ToString ()
266                 {
267                         return String.Format ("[LocalVariable {0}]", Name);
268                 }
269         }
270
271         public class SourceFileEntry
272         {
273                 #region This is actually written to the symbol file
274                 public readonly int Index;
275                 int Count;
276                 int NamespaceCount;
277                 int NameOffset;
278                 int MethodOffset;
279                 int NamespaceTableOffset;
280                 #endregion
281
282                 MonoSymbolFile file;
283                 string file_name;
284                 ArrayList methods;
285                 ArrayList namespaces;
286                 bool creating;
287
288                 public static int Size {
289                         get { return 24; }
290                 }
291
292                 public SourceFileEntry (MonoSymbolFile file, string file_name)
293                 {
294                         this.file = file;
295                         this.file_name = file_name;
296                         this.Index = file.AddSource (this);
297
298                         creating = true;
299                         methods = new ArrayList ();
300                         namespaces = new ArrayList ();
301                 }
302
303                 public void DefineMethod (string name, int token, LocalVariableEntry[] locals,
304                                           LineNumberEntry[] lines, LexicalBlockEntry[] blocks,
305                                           int start, int end, int namespace_id)
306                 {
307                         if (!creating)
308                                 throw new InvalidOperationException ();
309
310                         MethodEntry entry = new MethodEntry (
311                                 file, this, name, (int) token, locals, lines, blocks,
312                                 start, end, namespace_id);
313
314                         methods.Add (entry);
315                         file.AddMethod (entry);
316                 }
317
318                 public int DefineNamespace (string name, string[] using_clauses, int parent)
319                 {
320                         if (!creating)
321                                 throw new InvalidOperationException ();
322
323                         int index = file.GetNextNamespaceIndex ();
324                         NamespaceEntry ns = new NamespaceEntry (name, index, using_clauses, parent);
325                         namespaces.Add (ns);
326                         return index;
327                 }
328
329                 internal void WriteData (MyBinaryWriter bw)
330                 {
331                         NameOffset = (int) bw.BaseStream.Position;
332                         bw.Write (file_name);
333
334                         ArrayList list = new ArrayList ();
335                         foreach (MethodEntry entry in methods)
336                                 list.Add (entry.Write (file, bw));
337                         list.Sort ();
338                         Count = list.Count;
339
340                         MethodOffset = (int) bw.BaseStream.Position;
341                         foreach (MethodSourceEntry method in list)
342                                 method.Write (bw);
343
344                         NamespaceCount = namespaces.Count;
345                         NamespaceTableOffset = (int) bw.BaseStream.Position;
346                         foreach (NamespaceEntry ns in namespaces)
347                                 ns.Write (file, bw);
348                 }
349
350                 internal void Write (BinaryWriter bw)
351                 {
352                         bw.Write (Index);
353                         bw.Write (Count);
354                         bw.Write (NamespaceCount);
355                         bw.Write (NameOffset);
356                         bw.Write (MethodOffset);
357                         bw.Write (NamespaceTableOffset);
358                 }
359
360                 internal SourceFileEntry (MonoSymbolFile file, BinaryReader reader)
361                 {
362                         this.file = file;
363
364                         Index = reader.ReadInt32 ();
365                         Count = reader.ReadInt32 ();
366                         NamespaceCount = reader.ReadInt32 ();
367                         NameOffset = reader.ReadInt32 ();
368                         MethodOffset = reader.ReadInt32 ();
369                         NamespaceTableOffset = reader.ReadInt32 ();
370
371                         file_name = file.ReadString (NameOffset);
372                 }
373
374                 public string FileName {
375                         get { return file_name; }
376                 }
377
378                 public MethodSourceEntry[] Methods {
379                         get {
380                                 if (creating)
381                                         throw new InvalidOperationException ();
382
383                                 BinaryReader reader = file.BinaryReader;
384                                 int old_pos = (int) reader.BaseStream.Position;
385
386                                 reader.BaseStream.Position = MethodOffset;
387                                 ArrayList list = new ArrayList ();
388                                 for (int i = 0; i < Count; i ++)
389                                         list.Add (new MethodSourceEntry (reader));
390                                 reader.BaseStream.Position = old_pos;
391
392                                 MethodSourceEntry[] retval = new MethodSourceEntry [Count];
393                                 list.CopyTo (retval, 0);
394                                 return retval;
395                         }
396                 }
397
398                 public NamespaceEntry[] Namespaces {
399                         get {
400                                 if (creating)
401                                         throw new InvalidOperationException ();
402
403                                 MyBinaryReader reader = file.BinaryReader;
404                                 int old_pos = (int) reader.BaseStream.Position;
405
406                                 reader.BaseStream.Position = NamespaceTableOffset;
407                                 ArrayList list = new ArrayList ();
408                                 for (int i = 0; i < NamespaceCount; i ++)
409                                         list.Add (new NamespaceEntry (file, reader));
410                                 reader.BaseStream.Position = old_pos;
411
412                                 NamespaceEntry[] retval = new NamespaceEntry [list.Count];
413                                 list.CopyTo (retval, 0);
414                                 return retval;
415                         }
416                 }
417
418                 public override string ToString ()
419                 {
420                         return String.Format ("SourceFileEntry ({0}:{1}:{2})",
421                                               Index, file_name, Count);
422                 }
423         }
424
425         public struct MethodSourceEntry : IComparable
426         {
427                 #region This is actually written to the symbol file
428                 public readonly int Index;
429                 public readonly int FileOffset;
430                 public readonly int StartRow;
431                 public readonly int EndRow;
432                 #endregion
433
434                 public MethodSourceEntry (int index, int file_offset, int start, int end)
435                 {
436                         this.Index = index;
437                         this.FileOffset = file_offset;
438                         this.StartRow = start;
439                         this.EndRow = end;
440                 }
441
442                 internal MethodSourceEntry (BinaryReader reader)
443                 {
444                         Index = reader.ReadInt32 ();
445                         FileOffset = reader.ReadInt32 ();
446                         StartRow = reader.ReadInt32 ();
447                         EndRow = reader.ReadInt32 ();
448                 }
449
450                 public static int Size {
451                         get { return 16; }
452                 }
453
454                 internal void Write (BinaryWriter bw)
455                 {
456                         bw.Write (Index);
457                         bw.Write (FileOffset);
458                         bw.Write (StartRow);
459                         bw.Write (EndRow);
460                 }
461
462                 public int CompareTo (object obj)
463                 {
464                         MethodSourceEntry method = (MethodSourceEntry) obj;
465
466                         if (method.StartRow < StartRow)
467                                 return -1;
468                         else if (method.StartRow > StartRow)
469                                 return 1;
470                         else
471                                 return 0;
472                 }
473
474                 public override string ToString ()
475                 {
476                         return String.Format ("MethodSourceEntry ({0}:{1}:{2}:{3})",
477                                               Index, FileOffset, StartRow, EndRow);
478                 }
479         }
480
481         public struct MethodIndexEntry
482         {
483                 #region This is actually written to the symbol file
484                 public readonly int FileOffset;
485                 public readonly int Token;
486                 #endregion
487
488                 public static int Size {
489                         get { return 8; }
490                 }
491
492                 public MethodIndexEntry (int offset, int token)
493                 {
494                         this.FileOffset = offset;
495                         this.Token = token;
496                 }
497
498                 internal MethodIndexEntry (BinaryReader reader)
499                 {
500                         FileOffset = reader.ReadInt32 ();
501                         Token = reader.ReadInt32 ();
502                 }
503
504                 internal void Write (BinaryWriter bw)
505                 {
506                         bw.Write (FileOffset);
507                         bw.Write (Token);
508                 }
509
510                 public override string ToString ()
511                 {
512                         return String.Format ("MethodIndexEntry ({0}:{1:x})",
513                                               FileOffset, Token);
514                 }
515         }
516
517         public class MethodEntry : IComparable
518         {
519                 #region This is actually written to the symbol file
520                 public readonly int SourceFileIndex;
521                 public readonly int Token;
522                 public readonly int StartRow;
523                 public readonly int EndRow;
524                 public readonly int NumLocals;
525                 public readonly int NumLineNumbers;
526                 public readonly int NamespaceID;
527                 public readonly bool LocalNamesAmbiguous;
528
529                 int NameOffset;
530                 int TypeIndexTableOffset;
531                 int LocalVariableTableOffset;
532                 int LineNumberTableOffset;
533                 int NumLexicalBlocks;
534                 int LexicalBlockTableOffset;
535                 #endregion
536
537                 int file_offset;
538
539                 public readonly int Index;
540                 public readonly SourceFileEntry SourceFile;
541                 public readonly LineNumberEntry[] LineNumbers;
542                 public readonly int[] LocalTypeIndices;
543                 public readonly LocalVariableEntry[] Locals;
544                 public readonly LexicalBlockEntry[] LexicalBlocks;
545
546                 public readonly MonoSymbolFile SymbolFile;
547
548                 public static int Size {
549                         get { return 52; }
550                 }
551
552                 internal MethodEntry (MonoSymbolFile file, MyBinaryReader reader, int index)
553                 {
554                         this.SymbolFile = file;
555                         this.Index = index;
556                         SourceFileIndex = reader.ReadInt32 ();
557                         Token = reader.ReadInt32 ();
558                         StartRow = reader.ReadInt32 ();
559                         EndRow = reader.ReadInt32 ();
560                         NumLocals = reader.ReadInt32 ();
561                         NumLineNumbers = reader.ReadInt32 ();
562                         NameOffset = reader.ReadInt32 ();
563                         TypeIndexTableOffset = reader.ReadInt32 ();
564                         LocalVariableTableOffset = reader.ReadInt32 ();
565                         LineNumberTableOffset = reader.ReadInt32 ();
566                         NumLexicalBlocks = reader.ReadInt32 ();
567                         LexicalBlockTableOffset = reader.ReadInt32 ();
568                         NamespaceID = reader.ReadInt32 ();
569                         LocalNamesAmbiguous = reader.ReadInt32 () != 0;
570
571                         SourceFile = file.GetSourceFile (SourceFileIndex);
572
573                         if (LineNumberTableOffset != 0) {
574                                 long old_pos = reader.BaseStream.Position;
575                                 reader.BaseStream.Position = LineNumberTableOffset;
576
577                                 LineNumbers = new LineNumberEntry [NumLineNumbers];
578
579                                 for (int i = 0; i < NumLineNumbers; i++)
580                                         LineNumbers [i] = new LineNumberEntry (reader);
581
582                                 reader.BaseStream.Position = old_pos;
583                         }
584
585                         if (LocalVariableTableOffset != 0) {
586                                 long old_pos = reader.BaseStream.Position;
587                                 reader.BaseStream.Position = LocalVariableTableOffset;
588
589                                 Locals = new LocalVariableEntry [NumLocals];
590
591                                 for (int i = 0; i < NumLocals; i++)
592                                         Locals [i] = new LocalVariableEntry (reader);
593
594                                 reader.BaseStream.Position = old_pos;
595                         }
596
597                         if (TypeIndexTableOffset != 0) {
598                                 long old_pos = reader.BaseStream.Position;
599                                 reader.BaseStream.Position = TypeIndexTableOffset;
600
601                                 LocalTypeIndices = new int [NumLocals];
602
603                                 for (int i = 0; i < NumLocals; i++)
604                                         LocalTypeIndices [i] = reader.ReadInt32 ();
605
606                                 reader.BaseStream.Position = old_pos;
607                         }
608
609                         if (LexicalBlockTableOffset != 0) {
610                                 long old_pos = reader.BaseStream.Position;
611                                 reader.BaseStream.Position = LexicalBlockTableOffset;
612
613                                 LexicalBlocks = new LexicalBlockEntry [NumLexicalBlocks];
614                                 for (int i = 0; i < NumLexicalBlocks; i++)
615                                         LexicalBlocks [i] = new LexicalBlockEntry (i, reader);
616
617                                 reader.BaseStream.Position = old_pos;
618                         }
619                 }
620
621                 internal MethodEntry (MonoSymbolFile file, SourceFileEntry source,
622                                       string name, int token, LocalVariableEntry[] locals,
623                                       LineNumberEntry[] lines, LexicalBlockEntry[] blocks,
624                                       int start_row, int end_row, int namespace_id)
625                 {
626                         this.SymbolFile = file;
627
628                         Index = file.GetNextMethodIndex ();
629
630                         Token = token;
631                         SourceFileIndex = source.Index;
632                         SourceFile = source;
633                         StartRow = start_row;
634                         EndRow = end_row;
635                         NamespaceID = namespace_id;
636                         LexicalBlocks = blocks;
637                         NumLexicalBlocks = LexicalBlocks != null ? LexicalBlocks.Length : 0;
638
639                         LineNumbers = BuildLineNumberTable (lines);
640                         NumLineNumbers = LineNumbers.Length;
641
642                         file.NumLineNumbers += NumLineNumbers;
643
644                         NumLocals = locals != null ? locals.Length : 0;
645                         Locals = locals;
646
647                         if (NumLocals <= 32) {
648                                 // Most of the time, the O(n^2) factor is actually
649                                 // less than the cost of allocating the hash table,
650                                 // 32 is a rough number obtained through some testing.
651                                 
652                                 for (int i = 0; i < NumLocals; i ++) {
653                                         string nm = locals [i].Name;
654                                         
655                                         for (int j = i + 1; j < NumLocals; j ++) {
656                                                 if (locals [j].Name == nm) {
657                                                         LocalNamesAmbiguous = true;
658                                                         goto locals_check_done;
659                                                 }
660                                         }
661                                 }
662                         locals_check_done :
663                                 ;
664                         } else {
665                                 Hashtable local_names = new Hashtable ();
666                                 foreach (LocalVariableEntry local in locals) {
667                                         if (local_names.Contains (local.Name)) {
668                                                 LocalNamesAmbiguous = true;
669                                                 break;
670                                         }
671                                         local_names.Add (local.Name, local);
672                                 }
673                         }
674
675                         LocalTypeIndices = new int [NumLocals];
676                         for (int i = 0; i < NumLocals; i++)
677                                 LocalTypeIndices [i] = file.GetNextTypeIndex ();
678                 }
679                 
680                 static LineNumberEntry [] tmp_buff = new LineNumberEntry [20];
681
682                 // BuildLineNumberTable() eliminates duplicate line numbers and ensures
683                 // we aren't going "backwards" since this would counfuse the runtime's
684                 // debugging code (and the debugger).
685                 //
686                 // In the line number table, the "offset" field most be strictly
687                 // monotonic increasing; that is, the next entry must not have an offset
688                 // which is equal to or less than the current one.
689                 //
690                 // The most common case is that our input (ie. the line number table as
691                 // we get it from mcs) contains several entries with the same offset
692                 // (and different line numbers) - but it may also happen that the offset
693                 // is decreasing (this can be considered as an exception, such lines will
694                 // simply be discarded).
695                 LineNumberEntry[] BuildLineNumberTable (LineNumberEntry[] line_numbers)
696                 {
697                         int pos = 0;
698                         int last_offset = -1;
699                         int last_row = -1;
700
701                         if (line_numbers == null)
702                                 return new LineNumberEntry [0];
703                         
704                         if (tmp_buff.Length < (line_numbers.Length + 1))
705                                 tmp_buff = new LineNumberEntry [(line_numbers.Length + 1) * 2];
706
707                         for (int i = 0; i < line_numbers.Length; i++) {
708                                 LineNumberEntry line = line_numbers [i];
709
710                                 if (line.Offset > last_offset) {
711                                         if (last_row >= 0)
712                                                 tmp_buff [pos ++] = new LineNumberEntry (last_row, last_offset);
713                                         last_row = line.Row;
714                                         last_offset = line.Offset;
715                                 } else if (line.Row > last_row) {
716                                         last_row = line.Row;
717                                 }
718                         }
719
720                         if (last_row >= 0)
721                                 tmp_buff [pos ++] = new LineNumberEntry (last_row, last_offset);
722
723                         LineNumberEntry [] retval = new LineNumberEntry [pos];
724                         Array.Copy (tmp_buff, retval, pos);
725                         return retval;
726                 }
727
728                 internal MethodSourceEntry Write (MonoSymbolFile file, MyBinaryWriter bw)
729                 {
730                         NameOffset = (int) bw.BaseStream.Position;
731
732                         TypeIndexTableOffset = (int) bw.BaseStream.Position;
733
734                         for (int i = 0; i < NumLocals; i++)
735                                 bw.Write (LocalTypeIndices [i]);
736
737                         LocalVariableTableOffset = (int) bw.BaseStream.Position;
738                         for (int i = 0; i < NumLocals; i++)
739                                 Locals [i].Write (file, bw);
740                         file.LocalCount += NumLocals;
741
742                         LineNumberTableOffset = (int) bw.BaseStream.Position;
743                         for (int i = 0; i < NumLineNumbers; i++)
744                                 LineNumbers [i].Write (bw);
745                         file.LineNumberCount += NumLineNumbers;
746
747                         LexicalBlockTableOffset = (int) bw.BaseStream.Position;
748                         for (int i = 0; i < NumLexicalBlocks; i++)
749                                 LexicalBlocks [i].Write (bw);
750                         file_offset = (int) bw.BaseStream.Position;
751
752                         bw.Write (SourceFileIndex);
753                         bw.Write (Token);
754                         bw.Write (StartRow);
755                         bw.Write (EndRow);
756                         bw.Write (NumLocals);
757                         bw.Write (NumLineNumbers);
758                         bw.Write (NameOffset);
759                         bw.Write (TypeIndexTableOffset);
760                         bw.Write (LocalVariableTableOffset);
761                         bw.Write (LineNumberTableOffset);
762                         bw.Write (NumLexicalBlocks);
763                         bw.Write (LexicalBlockTableOffset);
764                         bw.Write (NamespaceID);
765                         bw.Write (LocalNamesAmbiguous ? 1 : 0);
766
767                         return new MethodSourceEntry (Index, file_offset, StartRow, EndRow);
768                 }
769
770                 internal void WriteIndex (BinaryWriter bw)
771                 {
772                         new MethodIndexEntry (file_offset, Token).Write (bw);
773                 }
774
775                 public int CompareTo (object obj)
776                 {
777                         MethodEntry method = (MethodEntry) obj;
778
779                         if (method.Token < Token)
780                                 return 1;
781                         else if (method.Token > Token)
782                                 return -1;
783                         else
784                                 return 0;
785                 }
786
787                 public override string ToString ()
788                 {
789                         return String.Format ("[Method {0}:{1}:{2}:{3}:{4} - {6}:{7} - {5}]",
790                                               Index, Token, SourceFileIndex, StartRow, EndRow,
791                                               SourceFile, NumLocals, NumLineNumbers);
792                 }
793         }
794
795         public struct NamespaceEntry
796         {
797                 #region This is actually written to the symbol file
798                 public readonly string Name;
799                 public readonly int Index;
800                 public readonly int Parent;
801                 public readonly string[] UsingClauses;
802                 #endregion
803
804                 public NamespaceEntry (string name, int index, string[] using_clauses, int parent)
805                 {
806                         this.Name = name;
807                         this.Index = index;
808                         this.Parent = parent;
809                         this.UsingClauses = using_clauses != null ? using_clauses : new string [0];
810                 }
811
812                 internal NamespaceEntry (MonoSymbolFile file, MyBinaryReader reader)
813                 {
814                         Name = reader.ReadString ();
815                         Index = reader.ReadLeb128 ();
816                         Parent = reader.ReadLeb128 ();
817
818                         int count = reader.ReadLeb128 ();
819                         UsingClauses = new string [count];
820                         for (int i = 0; i < count; i++)
821                                 UsingClauses [i] = reader.ReadString ();
822                 }
823
824                 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
825                 {
826                         bw.Write (Name);
827                         bw.WriteLeb128 (Index);
828                         bw.WriteLeb128 (Parent);
829                         bw.WriteLeb128 (UsingClauses.Length);
830                         foreach (string uc in UsingClauses)
831                                 bw.Write (uc);
832                 }
833
834                 public override string ToString ()
835                 {
836                         return String.Format ("[Namespace {0}:{1}:{2}]", Name, Index, Parent);
837                 }
838         }
839 }