Merge pull request #733 from amoiseev-softheme/bugfix/monofix
[mono.git] / mcs / class / Mono.CompilerServices.SymbolWriter / MonoSymbolTable.cs
index 76f183767c58fb22f8a6be12f955f826795b0886..2b434426803c3ce8efe868be3205c9673400c49e 100644 (file)
@@ -30,7 +30,7 @@
 
 using System;
 using System.Security.Cryptography;
-using System.Collections;
+using System.Collections.Generic;
 using System.Text;
 using System.IO;
 
@@ -72,8 +72,8 @@ namespace Mono.CompilerServices.SymbolWriter
 {
        public class OffsetTable
        {
-               public const int  MajorVersion = 42;
-               public const int  MinorVersion = 6;
+               public const int  MajorVersion = 50;
+               public const int  MinorVersion = 0;
                public const long Magic        = 0x45e82623fd7fa614;
 
                #region This is actually written to the symbol file
@@ -94,7 +94,14 @@ namespace Mono.CompilerServices.SymbolWriter
                public int AnonymousScopeTableOffset;
                public int AnonymousScopeTableSize;
 
-               public bool IsAspxSource;
+               [Flags]
+               public enum Flags
+               {
+                       IsAspxSource            = 1,
+                       WindowsFileNames        = 2
+               }
+
+               public Flags FileFlags;
 
                public int LineNumberTable_LineBase = LineNumberTable.Default_LineBase;
                public int LineNumberTable_LineRange = LineNumberTable.Default_LineRange;
@@ -102,7 +109,11 @@ namespace Mono.CompilerServices.SymbolWriter
                #endregion
 
                internal OffsetTable ()
-               { }
+               {
+                       int platform = (int) Environment.OSVersion.Platform;
+                       if ((platform != 4) && (platform != 128))
+                               FileFlags |= Flags.WindowsFileNames;
+               }
 
                internal OffsetTable (BinaryReader reader, int major_version, int minor_version)
                {
@@ -128,7 +139,7 @@ namespace Mono.CompilerServices.SymbolWriter
                        LineNumberTable_LineRange = reader.ReadInt32 ();
                        LineNumberTable_OpcodeBase = reader.ReadInt32 ();
 
-                       IsAspxSource = reader.ReadInt32 () != 0;
+                       FileFlags = (Flags) reader.ReadInt32 ();
                }
 
                internal void Write (BinaryWriter bw, int major_version, int minor_version)
@@ -155,7 +166,7 @@ namespace Mono.CompilerServices.SymbolWriter
                        bw.Write (LineNumberTable_LineRange);
                        bw.Write (LineNumberTable_OpcodeBase);
 
-                       bw.Write ((int) (IsAspxSource ? 1 : 0));
+                       bw.Write ((int) FileFlags);
                }
 
                public override string ToString ()
@@ -168,67 +179,52 @@ namespace Mono.CompilerServices.SymbolWriter
                }
        }
 
-       public struct LineNumberEntry
+       public class LineNumberEntry
        {
                #region This is actually written to the symbol file
                public readonly int Row;
+               public int Column;
                public readonly int File;
                public readonly int Offset;
-               public readonly bool IsHidden;
+               public readonly bool IsHidden;  // Obsolete is never used
                #endregion
 
-               public LineNumberEntry (int file, int row, int offset)
-                       : this (file, row, offset, false)
-               { }
-
-               public LineNumberEntry (int file, int row, int offset, bool is_hidden)
+               public sealed class LocationComparer : IComparer<LineNumberEntry>
                {
-                       this.File = file;
-                       this.Row = row;
-                       this.Offset = offset;
-                       this.IsHidden = is_hidden;
+                       public static readonly LocationComparer Default = new LocationComparer ();
+
+                       public int Compare (LineNumberEntry l1, LineNumberEntry l2)
+                       {
+                               return l1.Row == l2.Row ?
+                                       l1.Column.CompareTo (l2.Column) :
+                                       l1.Row.CompareTo (l2.Row);
+                       }
                }
 
-               public static LineNumberEntry Null = new LineNumberEntry (0, 0, 0);
+               public static readonly LineNumberEntry Null = new LineNumberEntry (0, 0, 0, 0);
 
-               private class OffsetComparerClass : IComparer
+               public LineNumberEntry (int file, int row, int column, int offset)
+                       : this (file, row, offset, column, false)
                {
-                       public int Compare (object a, object b)
-                       {
-                               LineNumberEntry l1 = (LineNumberEntry) a;
-                               LineNumberEntry l2 = (LineNumberEntry) b;
-
-                               if (l1.Offset < l2.Offset)
-                                       return -1;
-                               else if (l1.Offset > l2.Offset)
-                                       return 1;
-                               else
-                                       return 0;
-                       }
                }
 
-               private class RowComparerClass : IComparer
+               public LineNumberEntry (int file, int row, int offset)
+                       : this (file, row, -1, offset, false)
                {
-                       public int Compare (object a, object b)
-                       {
-                               LineNumberEntry l1 = (LineNumberEntry) a;
-                               LineNumberEntry l2 = (LineNumberEntry) b;
-
-                               if (l1.Row < l2.Row)
-                                       return -1;
-                               else if (l1.Row > l2.Row)
-                                       return 1;
-                               else
-                                       return 0;
-                       }
                }
 
-               public static readonly IComparer OffsetComparer = new OffsetComparerClass ();
-               public static readonly IComparer RowComparer = new RowComparerClass ();
+               public LineNumberEntry (int file, int row, int column, int offset, bool is_hidden)
+               {
+                       this.File = file;
+                       this.Row = row;
+                       this.Column = column;
+                       this.Offset = offset;
+                       this.IsHidden = is_hidden;
+               }
 
                public override string ToString ()
                {
-                       return String.Format ("[Line {0}:{1}:{2}]", File, Row, Offset);
+                       return String.Format ("[Line {0}:{1,2}:{3}]", File, Row, Column, Offset);
                }
        }
 
@@ -325,7 +321,7 @@ namespace Mono.CompilerServices.SymbolWriter
                public override string ToString ()
                {
                        return String.Format ("[LocalVariable {0}:{1}:{2}]",
-                                             Name, Index, BlockIndex);
+                                             Name, Index, BlockIndex - 1);
                }
        }
 
@@ -442,8 +438,8 @@ namespace Mono.CompilerServices.SymbolWriter
                public readonly int ID;
                #endregion
 
-               ArrayList captured_vars = new ArrayList ();
-               ArrayList captured_scopes = new ArrayList ();
+               List<CapturedVariable> captured_vars = new List<CapturedVariable> ();
+               List<CapturedScope> captured_scopes = new List<CapturedScope> ();
 
                public AnonymousScopeEntry (int id)
                {
@@ -518,8 +514,8 @@ namespace Mono.CompilerServices.SymbolWriter
 
                MonoSymbolFile file;
                SourceFileEntry source;
-               ArrayList include_files;
-               ArrayList namespaces;
+               List<SourceFileEntry> include_files;
+               List<NamespaceEntry> namespaces;
 
                bool creating;
 
@@ -539,7 +535,7 @@ namespace Mono.CompilerServices.SymbolWriter
                        this.Index = file.AddCompileUnit (this);
 
                        creating = true;
-                       namespaces = new ArrayList ();
+                       namespaces = new List<NamespaceEntry> ();
                }
 
                public void AddFile (SourceFileEntry file)
@@ -548,7 +544,7 @@ namespace Mono.CompilerServices.SymbolWriter
                                throw new InvalidOperationException ();
 
                        if (include_files == null)
-                               include_files = new ArrayList ();
+                               include_files = new List<SourceFileEntry> ();
 
                        include_files.Add (file);
                }
@@ -605,6 +601,11 @@ namespace Mono.CompilerServices.SymbolWriter
                        DataOffset = reader.ReadInt32 ();
                }
 
+               public void ReadAll ()
+               {
+                       ReadData ();
+               }
+
                void ReadData ()
                {
                        if (creating)
@@ -624,15 +625,13 @@ namespace Mono.CompilerServices.SymbolWriter
 
                                int count_includes = reader.ReadLeb128 ();
                                if (count_includes > 0) {
-                                       include_files = new ArrayList ();
-                                       for (int i = 0; i < count_includes; i++) {
-                                               // FIXME: The debugger will need this later on.
-                                               reader.ReadLeb128 ();
-                                       }
+                                       include_files = new List<SourceFileEntry> ();
+                                       for (int i = 0; i < count_includes; i++)
+                                               include_files.Add (file.GetSourceFile (reader.ReadLeb128 ()));
                                }
 
                                int count_ns = reader.ReadLeb128 ();
-                               namespaces = new ArrayList ();
+                               namespaces = new List<NamespaceEntry> ();
                                for (int i = 0; i < count_ns; i ++)
                                        namespaces.Add (new NamespaceEntry (file, reader));
 
@@ -648,6 +647,18 @@ namespace Mono.CompilerServices.SymbolWriter
                                return retval;
                        }
                }
+
+               public SourceFileEntry[] IncludeFiles {
+                       get {
+                               ReadData ();
+                               if (include_files == null)
+                                       return new SourceFileEntry [0];
+
+                               SourceFileEntry[] retval = new SourceFileEntry [include_files.Count];
+                               include_files.CopyTo (retval, 0);
+                               return retval;
+                       }
+               }
        }
 
        public class SourceFileEntry
@@ -677,24 +688,35 @@ namespace Mono.CompilerServices.SymbolWriter
                        creating = true;
                }
 
-               public SourceFileEntry (MonoSymbolFile file, string file_name,
-                                       byte[] guid, byte[] checksum)
+               public SourceFileEntry (MonoSymbolFile file, string file_name, byte[] guid, byte[] checksum)
                        : this (file, file_name)
                {
                        this.guid = guid;
                        this.hash = checksum;
                }
 
+               public byte[] Checksum {
+                       get {
+                               return hash;
+                       }
+               }
+
                internal void WriteData (MyBinaryWriter bw)
                {
                        DataOffset = (int) bw.BaseStream.Position;
                        bw.Write (file_name);
 
-                       if (guid == null) {
-                               guid = Guid.NewGuid ().ToByteArray ();
-                               using (FileStream fs = new FileStream (file_name, FileMode.Open, FileAccess.Read)) {
-                                       MD5 md5 = MD5.Create ();
-                                       hash = md5.ComputeHash (fs);
+                       if (guid == null)
+                               guid = new byte[16];
+
+                       if (hash == null) {
+                               try {
+                                   using (FileStream fs = new FileStream (file_name, FileMode.Open, FileAccess.Read)) {
+                                       MD5 md5 = MD5.Create ();
+                                       hash = md5.ComputeHash (fs);
+                                   }
+                               } catch {
+                                       hash = new byte [16];
                                }
                        }
 
@@ -729,6 +751,7 @@ namespace Mono.CompilerServices.SymbolWriter
 
                public string FileName {
                        get { return file_name; }
+                       set { file_name = value; }
                }
 
                public bool AutoGenerated {
@@ -741,7 +764,7 @@ namespace Mono.CompilerServices.SymbolWriter
                                throw new InvalidOperationException ();
 
                        auto_generated = true;
-                       file.OffsetTable.IsAspxSource = true;
+                       file.OffsetTable.FileFlags |= OffsetTable.Flags.IsAspxSource;
                }
 
                public bool CheckChecksum ()
@@ -783,7 +806,6 @@ namespace Mono.CompilerServices.SymbolWriter
                public const int Default_LineRange = 8;
                public const byte Default_OpcodeBase = 9;
 
-               public const bool SuppressDuplicates = true;
 #endregion
 
                public const byte DW_LNS_copy = 1;
@@ -797,6 +819,9 @@ namespace Mono.CompilerServices.SymbolWriter
                // MONO extensions.
                public const byte DW_LNE_MONO_negate_is_hidden = 0x40;
 
+               internal const byte DW_LNE_MONO__extensions_start = 0x40;
+               internal const byte DW_LNE_MONO__extensions_end   = 0x7f;
+
                protected LineNumberTable (MonoSymbolFile file)
                {
                        this.LineBase = file.OffsetTable.LineNumberTable_LineBase;
@@ -811,7 +836,7 @@ namespace Mono.CompilerServices.SymbolWriter
                        this._line_numbers = lines;
                }
 
-               internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
+               internal void Write (MonoSymbolFile file, MyBinaryWriter bw, bool readColumnsInfo)
                {
                        int start = (int) bw.BaseStream.Position;
 
@@ -821,11 +846,6 @@ namespace Mono.CompilerServices.SymbolWriter
                                int line_inc = LineNumbers [i].Row - last_line;
                                int offset_inc = LineNumbers [i].Offset - last_offset;
 
-                               if (SuppressDuplicates && (i+1 < LineNumbers.Length)) {
-                                       if (LineNumbers [i+1].Equals (LineNumbers [i]))
-                                               continue;
-                               }
-
                                if (LineNumbers [i].File != last_file) {
                                        bw.Write (DW_LNS_set_file);
                                        bw.WriteLeb128 (LineNumbers [i].File);
@@ -873,19 +893,25 @@ namespace Mono.CompilerServices.SymbolWriter
                        bw.Write ((byte) 1);
                        bw.Write (DW_LNE_end_sequence);
 
+                       for (int i = 0; i < LineNumbers.Length; i++) {
+                               var ln = LineNumbers [i];
+                               if (ln.Row >= 0)
+                                       bw.WriteLeb128 (ln.Column);
+                       }
+
                        file.ExtendedLineNumberSize += (int) bw.BaseStream.Position - start;
                }
 
-               internal static LineNumberTable Read (MonoSymbolFile file, MyBinaryReader br)
+               internal static LineNumberTable Read (MonoSymbolFile file, MyBinaryReader br, bool readColumnsInfo)
                {
                        LineNumberTable lnt = new LineNumberTable (file);
-                       lnt.DoRead (file, br);
+                       lnt.DoRead (file, br, readColumnsInfo);
                        return lnt;
                }
 
-               void DoRead (MonoSymbolFile file, MyBinaryReader br)
+               void DoRead (MonoSymbolFile file, MyBinaryReader br, bool includesColumns)
                {
-                       ArrayList lines = new ArrayList ();
+                       var lines = new List<LineNumberEntry> ();
 
                        bool is_hidden = false, modified = false;
                        int stm_line = 1, stm_offset = 0, stm_file = 1;
@@ -900,15 +926,17 @@ namespace Mono.CompilerServices.SymbolWriter
                                        if (opcode == DW_LNE_end_sequence) {
                                                if (modified)
                                                        lines.Add (new LineNumberEntry (
-                                                               stm_file, stm_line, stm_offset, is_hidden));
+                                                               stm_file, stm_line, -1, stm_offset, is_hidden));
                                                break;
                                        } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
                                                is_hidden = !is_hidden;
                                                modified = true;
-                                       } else
-                                               throw new MonoSymbolFileException (
-                                                       "Unknown extended opcode {0:x} in LNT ({1})",
-                                                       opcode, file.FileName);
+                                       } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
+                                                  (opcode <= DW_LNE_MONO__extensions_end)) {
+                                               ; // reserved for future extensions
+                                       } else {
+                                               throw new MonoSymbolFileException ("Unknown extended opcode {0:x}", opcode);
+                                       }
 
                                        br.BaseStream.Position = end_pos;
                                        continue;
@@ -916,7 +944,7 @@ namespace Mono.CompilerServices.SymbolWriter
                                        switch (opcode) {
                                        case DW_LNS_copy:
                                                lines.Add (new LineNumberEntry (
-                                                       stm_file, stm_line, stm_offset, is_hidden));
+                                                       stm_file, stm_line, -1, stm_offset, is_hidden));
                                                modified = false;
                                                break;
                                        case DW_LNS_advance_pc:
@@ -946,13 +974,20 @@ namespace Mono.CompilerServices.SymbolWriter
                                        stm_offset += opcode / LineRange;
                                        stm_line += LineBase + (opcode % LineRange);
                                        lines.Add (new LineNumberEntry (
-                                               stm_file, stm_line, stm_offset, is_hidden));
+                                               stm_file, stm_line, -1, stm_offset, is_hidden));
                                        modified = false;
                                }
                        }
 
-                       _line_numbers = new LineNumberEntry [lines.Count];
-                       lines.CopyTo (_line_numbers, 0);
+                       _line_numbers = lines.ToArray ();
+
+                       if (includesColumns) {
+                               for (int i = 0; i < _line_numbers.Length; ++i) {
+                                       var ln = _line_numbers[i];
+                                       if (ln.Row >= 0)
+                                               ln.Column = br.ReadLeb128 ();
+                               }
+                       }
                }
 
                public bool GetMethodBounds (out LineNumberEntry start, out LineNumberEntry end)
@@ -1010,7 +1045,7 @@ namespace Mono.CompilerServices.SymbolWriter
                public enum Flags
                {
                        LocalNamesAmbiguous     = 1,
-                       IsCompilerGenerated     = 2
+                       ColumnsInfoIncluded = 1 << 1
                }
 
                public const int Size = 12;
@@ -1087,9 +1122,9 @@ namespace Mono.CompilerServices.SymbolWriter
                        locals_check_done :
                                ;
                        } else {
-                               Hashtable local_names = new Hashtable ();
+                               var local_names = new Dictionary<string, LocalVariableEntry> ();
                                foreach (LocalVariableEntry local in locals) {
-                                       if (local_names.Contains (local.Name)) {
+                                       if (local_names.ContainsKey (local.Name)) {
                                                flags |= Flags.LocalNamesAmbiguous;
                                                break;
                                        }
@@ -1098,7 +1133,7 @@ namespace Mono.CompilerServices.SymbolWriter
                        }
                }
                
-               void CheckLineNumberTable (LineNumberEntry[] line_numbers)
+               static void CheckLineNumberTable (LineNumberEntry[] line_numbers)
                {
                        int last_offset = -1;
                        int last_row = -1;
@@ -1164,7 +1199,7 @@ namespace Mono.CompilerServices.SymbolWriter
                        }
 
                        LineNumberTableOffset = (int) bw.BaseStream.Position;
-                       lnt.Write (file, bw);
+                       lnt.Write (file, bw, (flags & Flags.ColumnsInfoIncluded) != 0);
 
                        DataOffset = (int) bw.BaseStream.Position;
 
@@ -1179,6 +1214,15 @@ namespace Mono.CompilerServices.SymbolWriter
                        bw.WriteLeb128 ((int) flags);
                }
 
+               public void ReadAll ()
+               {
+                       GetLineNumberTable ();
+                       GetLocals ();
+                       GetCodeBlocks ();
+                       GetScopeVariables ();
+                       GetRealName ();
+               }
+
                public LineNumberTable GetLineNumberTable ()
                {
                        lock (SymbolFile) {
@@ -1192,7 +1236,7 @@ namespace Mono.CompilerServices.SymbolWriter
                                long old_pos = reader.BaseStream.Position;
                                reader.BaseStream.Position = LineNumberTableOffset;
 
-                               lnt = LineNumberTable.Read (SymbolFile, reader);
+                               lnt = LineNumberTable.Read (SymbolFile, reader, (flags & Flags.ColumnsInfoIncluded) != 0);
 
                                reader.BaseStream.Position = old_pos;
                                return lnt;