2004-10-20 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / Mono.CSharp.Debugger / MonoSymbolTable.cs
index 43e06e03379fbaf981157b140f5f68aa206dfc0e..4fadaf1e18b467e5411801c0d61b7bc920e55401 100644 (file)
@@ -1,5 +1,5 @@
 //
-// System.Diagnostics.SymbolStore/MonoSymbolTable.cs
+// Mono.CSharp.Debugger/MonoSymbolTable.cs
 //
 // Author:
 //   Martin Baulig (martin@ximian.com)
@@ -7,9 +7,28 @@
 // (C) 2002 Ximian, Inc.  http://www.ximian.com
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
-using System.Reflection;
-using System.Reflection.Emit;
 using System.Collections;
 using System.Text;
 using System.IO;
@@ -48,11 +67,11 @@ using System.IO;
 // changing the file format.
 //
 
-namespace Mono.CSharp.Debugger
+namespace Mono.CompilerServices.SymbolWriter
 {
        public struct OffsetTable
        {
-               public const int  Version = 35;
+               public const int  Version = 38;
                public const long Magic   = 0x45e82623fd7fa614;
 
                #region This is actually written to the symbol file
@@ -188,7 +207,7 @@ namespace Mono.CSharp.Debugger
                        this.StartOffset = start_offset;
                }
 
-               internal LexicalBlockEntry (int index, BinaryReader reader)
+               internal LexicalBlockEntry (int index, MyBinaryReader reader)
                {
                        this.Index = index;
                        this.StartOffset = reader.ReadInt32 ();
@@ -200,7 +219,7 @@ namespace Mono.CSharp.Debugger
                        this.EndOffset = end_offset;
                }
 
-               internal void Write (BinaryWriter bw)
+               internal void Write (MyBinaryWriter bw)
                {
                        bw.Write (StartOffset);
                        bw.Write (EndOffset);
@@ -216,43 +235,36 @@ namespace Mono.CSharp.Debugger
        {
                #region This is actually written to the symbol file
                public readonly string Name;
-               public readonly FieldAttributes Attributes;
                public readonly byte[] Signature;
                public readonly int BlockIndex;
                #endregion
 
-               public LocalVariableEntry (string Name, FieldAttributes Attributes, byte[] Signature,
-                                          int BlockIndex)
+               public LocalVariableEntry (string Name, byte[] Signature, int BlockIndex)
                {
                        this.Name = Name;
-                       this.Attributes = Attributes;
                        this.Signature = Signature;
                        this.BlockIndex = BlockIndex;
                }
 
-               internal LocalVariableEntry (BinaryReader reader)
+               internal LocalVariableEntry (MyBinaryReader reader)
                {
-                       int name_length = reader.ReadInt32 ();
-                       byte[] name = reader.ReadBytes (name_length);
-                       Name = Encoding.UTF8.GetString (name);
-                       Attributes = (FieldAttributes) reader.ReadInt32 ();
-                       int sig_length = reader.ReadInt32 ();
+                       Name = reader.ReadString ();
+                       int sig_length = reader.ReadLeb128 ();
                        Signature = reader.ReadBytes (sig_length);
-                       BlockIndex = reader.ReadInt32 ();
+                       BlockIndex = reader.ReadLeb128 ();
                }
 
-               internal void Write (MonoSymbolFile file, BinaryWriter bw)
+               internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
                {
-                       file.WriteString (bw, Name);
-                       bw.Write ((int) Attributes);
-                       bw.Write ((int) Signature.Length);
+                       bw.Write (Name);
+                       bw.WriteLeb128 ((int) Signature.Length);
                        bw.Write (Signature);
-                       bw.Write (BlockIndex);
+                       bw.WriteLeb128 (BlockIndex);
                }
 
                public override string ToString ()
                {
-                       return String.Format ("[LocalVariable {0}:{1}]", Name, Attributes);
+                       return String.Format ("[LocalVariable {0}]", Name);
                }
        }
 
@@ -277,7 +289,7 @@ namespace Mono.CSharp.Debugger
                        get { return 24; }
                }
 
-               internal SourceFileEntry (MonoSymbolFile file, string file_name)
+               public SourceFileEntry (MonoSymbolFile file, string file_name)
                {
                        this.file = file;
                        this.file_name = file_name;
@@ -288,7 +300,7 @@ namespace Mono.CSharp.Debugger
                        namespaces = new ArrayList ();
                }
 
-               public void DefineMethod (MethodBase method, int token, LocalVariableEntry[] locals,
+               public void DefineMethod (string name, int token, LocalVariableEntry[] locals,
                                          LineNumberEntry[] lines, LexicalBlockEntry[] blocks,
                                          int start, int end, int namespace_id)
                {
@@ -296,7 +308,8 @@ namespace Mono.CSharp.Debugger
                                throw new InvalidOperationException ();
 
                        MethodEntry entry = new MethodEntry (
-                               file, this, method, token, locals, lines, blocks, start, end, namespace_id);
+                               file, this, name, (int) token, locals, lines, blocks,
+                               start, end, namespace_id);
 
                        methods.Add (entry);
                        file.AddMethod (entry);
@@ -313,10 +326,10 @@ namespace Mono.CSharp.Debugger
                        return index;
                }
 
-               internal void WriteData (BinaryWriter bw)
+               internal void WriteData (MyBinaryWriter bw)
                {
                        NameOffset = (int) bw.BaseStream.Position;
-                       file.WriteString (bw, file_name);
+                       bw.Write (file_name);
 
                        ArrayList list = new ArrayList ();
                        foreach (MethodEntry entry in methods)
@@ -382,6 +395,26 @@ namespace Mono.CSharp.Debugger
                        }
                }
 
+               public NamespaceEntry[] Namespaces {
+                       get {
+                               if (creating)
+                                       throw new InvalidOperationException ();
+
+                               MyBinaryReader reader = file.BinaryReader;
+                               int old_pos = (int) reader.BaseStream.Position;
+
+                               reader.BaseStream.Position = NamespaceTableOffset;
+                               ArrayList list = new ArrayList ();
+                               for (int i = 0; i < NamespaceCount; i ++)
+                                       list.Add (new NamespaceEntry (file, reader));
+                               reader.BaseStream.Position = old_pos;
+
+                               NamespaceEntry[] retval = new NamespaceEntry [list.Count];
+                               list.CopyTo (retval, 0);
+                               return retval;
+                       }
+               }
+
                public override string ToString ()
                {
                        return String.Format ("SourceFileEntry ({0}:{1}:{2})",
@@ -449,58 +482,51 @@ namespace Mono.CSharp.Debugger
        {
                #region This is actually written to the symbol file
                public readonly int FileOffset;
-               public readonly int FullNameOffset;
                public readonly int Token;
                #endregion
 
                public static int Size {
-                       get { return 12; }
+                       get { return 8; }
                }
 
-               public MethodIndexEntry (int offset, int name_offset, int token)
+               public MethodIndexEntry (int offset, int token)
                {
                        this.FileOffset = offset;
-                       this.FullNameOffset = name_offset;
                        this.Token = token;
                }
 
                internal MethodIndexEntry (BinaryReader reader)
                {
                        FileOffset = reader.ReadInt32 ();
-                       FullNameOffset = reader.ReadInt32 ();
                        Token = reader.ReadInt32 ();
                }
 
                internal void Write (BinaryWriter bw)
                {
                        bw.Write (FileOffset);
-                       bw.Write (FullNameOffset);
                        bw.Write (Token);
                }
 
                public override string ToString ()
                {
-                       return String.Format ("MethodIndexEntry ({0}:{1}:{2:x})",
-                                             FileOffset, FullNameOffset, Token);
+                       return String.Format ("MethodIndexEntry ({0}:{1:x})",
+                                             FileOffset, Token);
                }
        }
 
-       public class MethodEntry
+       public class MethodEntry : IComparable
        {
                #region This is actually written to the symbol file
                public readonly int SourceFileIndex;
                public readonly int Token;
                public readonly int StartRow;
                public readonly int EndRow;
-               public readonly int ClassTypeIndex;
-               public readonly int NumParameters;
                public readonly int NumLocals;
                public readonly int NumLineNumbers;
                public readonly int NamespaceID;
                public readonly bool LocalNamesAmbiguous;
 
                int NameOffset;
-               int FullNameOffset;
                int TypeIndexTableOffset;
                int LocalVariableTableOffset;
                int LineNumberTableOffset;
@@ -508,51 +534,37 @@ namespace Mono.CSharp.Debugger
                int LexicalBlockTableOffset;
                #endregion
 
+               int index;
                int file_offset;
-               string name;
-               string full_name;
 
-               public readonly int Index;
                public readonly SourceFileEntry SourceFile;
                public readonly LineNumberEntry[] LineNumbers;
-               public readonly int[] ParamTypeIndices;
                public readonly int[] LocalTypeIndices;
                public readonly LocalVariableEntry[] Locals;
-               public readonly Type[] LocalTypes;
                public readonly LexicalBlockEntry[] LexicalBlocks;
 
                public readonly MonoSymbolFile SymbolFile;
 
-               public static int Size {
-                       get { return 52; }
-               }
-
-               public string Name {
-                       get { return name; }
+               public int Index {
+                       get { return index; }
+                       set { index = value; }
                }
 
-               public string FullName {
-                       get { return full_name; }
-               }
-
-               public MethodBase MethodBase {
-                       get { return MonoDebuggerSupport.GetMethod (SymbolFile.Assembly, Token); }
+               public static int Size {
+                       get { return 52; }
                }
 
-               internal MethodEntry (MonoSymbolFile file, BinaryReader reader, int index)
+               internal MethodEntry (MonoSymbolFile file, MyBinaryReader reader, int index)
                {
                        this.SymbolFile = file;
-                       this.Index = index;
+                       this.index = index;
                        SourceFileIndex = reader.ReadInt32 ();
                        Token = reader.ReadInt32 ();
                        StartRow = reader.ReadInt32 ();
                        EndRow = reader.ReadInt32 ();
-                       ClassTypeIndex = reader.ReadInt32 ();
-                       NumParameters = reader.ReadInt32 ();
                        NumLocals = reader.ReadInt32 ();
                        NumLineNumbers = reader.ReadInt32 ();
                        NameOffset = reader.ReadInt32 ();
-                       FullNameOffset = reader.ReadInt32 ();
                        TypeIndexTableOffset = reader.ReadInt32 ();
                        LocalVariableTableOffset = reader.ReadInt32 ();
                        LineNumberTableOffset = reader.ReadInt32 ();
@@ -561,9 +573,6 @@ namespace Mono.CSharp.Debugger
                        NamespaceID = reader.ReadInt32 ();
                        LocalNamesAmbiguous = reader.ReadInt32 () != 0;
 
-                       name = file.ReadString (NameOffset);
-                       full_name = file.ReadString (FullNameOffset);
-
                        SourceFile = file.GetSourceFile (SourceFileIndex);
 
                        if (LineNumberTableOffset != 0) {
@@ -583,15 +592,9 @@ namespace Mono.CSharp.Debugger
                                reader.BaseStream.Position = LocalVariableTableOffset;
 
                                Locals = new LocalVariableEntry [NumLocals];
-                               LocalTypes = new Type [NumLocals];
-
-                               Assembly ass = file.Assembly;
 
-                               for (int i = 0; i < NumLocals; i++) {
+                               for (int i = 0; i < NumLocals; i++)
                                        Locals [i] = new LocalVariableEntry (reader);
-                                       LocalTypes [i] = MonoDebuggerSupport.GetLocalTypeFromSignature (
-                                               ass, Locals [i].Signature);
-                               }
 
                                reader.BaseStream.Position = old_pos;
                        }
@@ -600,11 +603,8 @@ namespace Mono.CSharp.Debugger
                                long old_pos = reader.BaseStream.Position;
                                reader.BaseStream.Position = TypeIndexTableOffset;
 
-                               ParamTypeIndices = new int [NumParameters];
                                LocalTypeIndices = new int [NumLocals];
 
-                               for (int i = 0; i < NumParameters; i++)
-                                       ParamTypeIndices [i] = reader.ReadInt32 ();
                                for (int i = 0; i < NumLocals; i++)
                                        LocalTypeIndices [i] = reader.ReadInt32 ();
 
@@ -623,13 +623,14 @@ namespace Mono.CSharp.Debugger
                        }
                }
 
-               internal MethodEntry (MonoSymbolFile file, SourceFileEntry source, MethodBase method,
-                                     int token, LocalVariableEntry[] locals, LineNumberEntry[] lines,
-                                     LexicalBlockEntry[] blocks, int start_row, int end_row,
-                                     int namespace_id)
+               internal MethodEntry (MonoSymbolFile file, SourceFileEntry source,
+                                     string name, int token, LocalVariableEntry[] locals,
+                                     LineNumberEntry[] lines, LexicalBlockEntry[] blocks,
+                                     int start_row, int end_row, int namespace_id)
                {
                        this.SymbolFile = file;
-                       Index = file.GetNextMethodIndex ();
+
+                       index = -1;
 
                        Token = token;
                        SourceFileIndex = source.Index;
@@ -638,44 +639,14 @@ namespace Mono.CSharp.Debugger
                        EndRow = end_row;
                        NamespaceID = namespace_id;
                        LexicalBlocks = blocks;
-                       NumLexicalBlocks = LexicalBlocks.Length;
+                       NumLexicalBlocks = LexicalBlocks != null ? LexicalBlocks.Length : 0;
 
                        LineNumbers = BuildLineNumberTable (lines);
                        NumLineNumbers = LineNumbers.Length;
 
-                       ParameterInfo[] parameters = method.GetParameters ();
-                       if (parameters == null)
-                               parameters = new ParameterInfo [0];
-                       
-                       if (parameters.Length == 0)
-                               full_name = method.DeclaringType.FullName + "." + method.Name + "()";
-                       else if (parameters.Length == 1)
-                               full_name = method.DeclaringType.FullName + "." + method.Name + "(" + parameters [0].ParameterType.FullName +  ")";
-                       else if (parameters.Length == 2)
-                               full_name = method.DeclaringType.FullName + "." + method.Name + "(" + parameters [0].ParameterType.FullName + "," + parameters [1].ParameterType.FullName + ")";
-                       else {
-                               StringBuilder sb = new StringBuilder ();
-                               sb.Append (method.DeclaringType.FullName);
-                               sb.Append (".");
-                               sb.Append (method.Name);
-                               sb.Append ("(");
-                               for (int i = 0; i < parameters.Length; i++) {
-                                       if (i > 0)
-                                               sb.Append (",");
-                                       sb.Append (parameters [i].ParameterType.FullName);
-                               }
-                               sb.Append (")");
-                               full_name = sb.ToString ();
-                       }
-
-                       name = method.Name;
-                       
-                       NumParameters = parameters.Length;
-                       ParamTypeIndices = new int [NumParameters];
-                       for (int i = 0; i < NumParameters; i++)
-                               ParamTypeIndices [i] = file.DefineType (parameters [i].ParameterType);
+                       file.NumLineNumbers += NumLineNumbers;
 
-                       NumLocals = locals.Length;
+                       NumLocals = locals != null ? locals.Length : 0;
                        Locals = locals;
 
                        if (NumLocals <= 32) {
@@ -709,9 +680,9 @@ namespace Mono.CSharp.Debugger
                        LocalTypeIndices = new int [NumLocals];
                        for (int i = 0; i < NumLocals; i++)
                                LocalTypeIndices [i] = file.GetNextTypeIndex ();
-
-                       ClassTypeIndex = file.DefineType (method.ReflectedType);
                }
+               
+               static LineNumberEntry [] tmp_buff = new LineNumberEntry [20];
 
                // BuildLineNumberTable() eliminates duplicate line numbers and ensures
                // we aren't going "backwards" since this would counfuse the runtime's
@@ -728,16 +699,22 @@ namespace Mono.CSharp.Debugger
                // simply be discarded).
                LineNumberEntry[] BuildLineNumberTable (LineNumberEntry[] line_numbers)
                {
-                       ArrayList list = new ArrayList ();
+                       int pos = 0;
                        int last_offset = -1;
                        int last_row = -1;
 
+                       if (line_numbers == null)
+                               return new LineNumberEntry [0];
+                       
+                       if (tmp_buff.Length < (line_numbers.Length + 1))
+                               tmp_buff = new LineNumberEntry [(line_numbers.Length + 1) * 2];
+
                        for (int i = 0; i < line_numbers.Length; i++) {
-                               LineNumberEntry line = (LineNumberEntry) line_numbers [i];
+                               LineNumberEntry line = line_numbers [i];
 
                                if (line.Offset > last_offset) {
                                        if (last_row >= 0)
-                                               list.Add (new LineNumberEntry (last_row, last_offset));
+                                               tmp_buff [pos ++] = new LineNumberEntry (last_row, last_offset);
                                        last_row = line.Row;
                                        last_offset = line.Offset;
                                } else if (line.Row > last_row) {
@@ -746,25 +723,22 @@ namespace Mono.CSharp.Debugger
                        }
 
                        if (last_row >= 0)
-                               list.Add (new LineNumberEntry (last_row, last_offset));
+                               tmp_buff [pos ++] = new LineNumberEntry (last_row, last_offset);
 
-                       LineNumberEntry[] retval = new LineNumberEntry [list.Count];
-                       list.CopyTo (retval, 0);
+                       LineNumberEntry [] retval = new LineNumberEntry [pos];
+                       Array.Copy (tmp_buff, retval, pos);
                        return retval;
                }
 
-               internal MethodSourceEntry Write (MonoSymbolFile file, BinaryWriter bw)
+               internal MethodSourceEntry Write (MonoSymbolFile file, MyBinaryWriter bw)
                {
-                       NameOffset = (int) bw.BaseStream.Position;
-                       file.WriteString (bw, name);
+                       if (index <= 0)
+                               throw new InvalidOperationException ();
 
-                       FullNameOffset = (int) bw.BaseStream.Position;
-                       file.WriteString (bw, full_name);
+                       NameOffset = (int) bw.BaseStream.Position;
 
                        TypeIndexTableOffset = (int) bw.BaseStream.Position;
 
-                       for (int i = 0; i < NumParameters; i++)
-                               bw.Write (ParamTypeIndices [i]);
                        for (int i = 0; i < NumLocals; i++)
                                bw.Write (LocalTypeIndices [i]);
 
@@ -787,12 +761,9 @@ namespace Mono.CSharp.Debugger
                        bw.Write (Token);
                        bw.Write (StartRow);
                        bw.Write (EndRow);
-                       bw.Write (ClassTypeIndex);
-                       bw.Write (NumParameters);
                        bw.Write (NumLocals);
                        bw.Write (NumLineNumbers);
                        bw.Write (NameOffset);
-                       bw.Write (FullNameOffset);
                        bw.Write (TypeIndexTableOffset);
                        bw.Write (LocalVariableTableOffset);
                        bw.Write (LineNumberTableOffset);
@@ -801,20 +772,31 @@ namespace Mono.CSharp.Debugger
                        bw.Write (NamespaceID);
                        bw.Write (LocalNamesAmbiguous ? 1 : 0);
 
-                       return new MethodSourceEntry (Index, file_offset, StartRow, EndRow);
+                       return new MethodSourceEntry (index, file_offset, StartRow, EndRow);
                }
 
                internal void WriteIndex (BinaryWriter bw)
                {
-                       new MethodIndexEntry (file_offset, FullNameOffset, Token).Write (bw);
+                       new MethodIndexEntry (file_offset, Token).Write (bw);
+               }
+
+               public int CompareTo (object obj)
+               {
+                       MethodEntry method = (MethodEntry) obj;
+
+                       if (method.Token < Token)
+                               return 1;
+                       else if (method.Token > Token)
+                               return -1;
+                       else
+                               return 0;
                }
 
                public override string ToString ()
                {
-                       return String.Format ("[Method {0}:{1}:{2}:{3}:{4} - {7}:{8}:{9}:{10} - {5} - {6}]",
-                                             Index, Token, SourceFileIndex, StartRow, EndRow,
-                                             SourceFile, FullName, ClassTypeIndex, NumParameters,
-                                             NumLocals, NumLineNumbers);
+                       return String.Format ("[Method {0}:{1}:{2}:{3}:{4} - {6}:{7} - {5}]",
+                                             index, Token, SourceFileIndex, StartRow, EndRow,
+                                             SourceFile, NumLocals, NumLineNumbers);
                }
        }
 
@@ -835,14 +817,26 @@ namespace Mono.CSharp.Debugger
                        this.UsingClauses = using_clauses != null ? using_clauses : new string [0];
                }
 
-               internal void Write (MonoSymbolFile file, BinaryWriter bw)
+               internal NamespaceEntry (MonoSymbolFile file, MyBinaryReader reader)
                {
-                       file.WriteString (bw, Name);
-                       bw.Write (Index);
-                       bw.Write (Parent);
-                       bw.Write (UsingClauses.Length);
+                       Name = reader.ReadString ();
+                       Index = reader.ReadLeb128 ();
+                       Parent = reader.ReadLeb128 ();
+
+                       int count = reader.ReadLeb128 ();
+                       UsingClauses = new string [count];
+                       for (int i = 0; i < count; i++)
+                               UsingClauses [i] = reader.ReadString ();
+               }
+
+               internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
+               {
+                       bw.Write (Name);
+                       bw.WriteLeb128 (Index);
+                       bw.WriteLeb128 (Parent);
+                       bw.WriteLeb128 (UsingClauses.Length);
                        foreach (string uc in UsingClauses)
-                               file.WriteString (bw, uc);
+                               bw.Write (uc);
                }
 
                public override string ToString ()