[mcs] extend API for loading types and consuming source file from a stream (#4905)
[mono.git] / mcs / mcs / location.cs
index a6ae554ac05bea5afdc39d3c3bb9548e8370ae0f..287aac0797a375b222d27470769e692f0dabefe7 100644 (file)
@@ -15,6 +15,7 @@ using System.Collections.Generic;
 using Mono.CompilerServices.SymbolWriter;
 using System.Diagnostics;
 using System.Linq;
+using System.IO;
 
 namespace Mono.CSharp
 {
@@ -52,20 +53,40 @@ namespace Mono.CSharp
                        }
                }
 
+               static readonly byte[] MD5Algorith = { 96, 166, 110, 64, 207, 100, 130, 76, 182, 240, 66, 212, 129, 114, 167, 153 };
+
                public readonly string Name;
-               public readonly string FullPathName;
+               public readonly string OriginalFullPathName;
                public readonly int Index;
                public bool AutoGenerated;
+               public Func<SourceFile, SeekableStreamReader> GetInputStream;
 
                SourceFileEntry file;
-               byte[] guid, checksum;
+               byte[] algGuid, checksum;
                List<LocationRegion> hidden_lines;
 
                public SourceFile (string name, string path, int index)
                {
                        this.Index = index;
                        this.Name = name;
-                       this.FullPathName = path;
+                       this.OriginalFullPathName = path;
+               }
+
+               public SourceFile (string name, string path, int index, Func<SourceFile, SeekableStreamReader> inputStreamDelegate) : this (name, path, index)
+               {
+                       this.GetInputStream = inputStreamDelegate;
+               }
+
+               public byte[] Checksum {
+                       get {
+                               return checksum;
+                       }
+               }
+
+               public bool HasChecksum {
+                       get {
+                               return checksum != null;
+                       }
                }
 
                public SourceFileEntry SourceFileEntry {
@@ -74,31 +95,54 @@ namespace Mono.CSharp
                        }
                }
 
-               public void SetChecksum (byte[] guid, byte[] checksum)
+               public void SetChecksum (byte[] checksum)
+               {
+                       SetChecksum (MD5Algorith, checksum);
+               }
+
+               public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
                {
-                       this.guid = guid;
+                       this.algGuid = algorithmGuid;
                        this.checksum = checksum;
                }
 
-               public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter)
+               public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter, List<KeyValuePair<string, string>> pathMap)
                {
                        if (hidden_lines != null)
                                hidden_lines.Sort ();
 
-                       if (guid != null) {
-                               file = new SourceFileEntry (symwriter, FullPathName, guid, checksum);
-                       } else {
-                               file = new SourceFileEntry (symwriter, FullPathName);
-                               if (AutoGenerated)
-                                       file.SetAutoGenerated ();
-                       }
+                       file = new SourceFileEntry (symwriter, GetFullPathName (pathMap), OriginalFullPathName, algGuid, checksum);
+                       if (AutoGenerated)
+                               file.SetAutoGenerated ();
 
                        return file;
                }
 
+               public string GetFullPathName (List<KeyValuePair<string, string>> pathMap)
+               {
+                       var path = OriginalFullPathName;
+                       if (pathMap != null) {
+                               foreach (var map in pathMap) {
+                                       var prefix = map.Key;
+                                       if (path.Length <= prefix.Length)
+                                               continue;
+
+                                       if (path [prefix.Length] != Path.DirectorySeparatorChar)
+                                               continue;
+
+                                       if (!path.StartsWith (prefix, StringComparison.Ordinal))
+                                               continue;
+
+                                       path = map.Value + path.Substring (prefix.Length);
+                               }
+                       }
+
+                       return path;
+               }
+
                public bool Equals (SourceFile other)
                {
-                       return FullPathName == other.FullPathName;
+                       return OriginalFullPathName == other.OriginalFullPathName;
                }
 
                public bool IsHiddenLocation (Location loc)
@@ -127,7 +171,7 @@ namespace Mono.CSharp
 
                public override string ToString ()
                {
-                       return String.Format ("SourceFile ({0}:{1}:{2})", Name, FullPathName, Index);
+                       return String.Format ("SourceFile ({0}:{1}:{2})", Name, OriginalFullPathName, Index);
                }
        }
 
@@ -177,7 +221,6 @@ namespace Mono.CSharp
                const int max_column = column_mask;
 
                static List<SourceFile> source_list;
-               static int current_source;
                static Checkpoint [] checkpoints;
                static int checkpoint_index;
                
@@ -192,7 +235,6 @@ namespace Mono.CSharp
                public static void Reset ()
                {
                        source_list = new List<SourceFile> ();
-                       current_source = 0;
                        checkpoint_index = 0;
                }
 
@@ -209,24 +251,14 @@ namespace Mono.CSharp
                // </summary>
                static public void Initialize (List<SourceFile> files)
                {
-#if NET_4_0 || MONODROID
                        source_list.AddRange (files);
-#else
-                       source_list.AddRange (files.ToArray ());
-#endif
 
                        checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
                        if (checkpoints.Length > 0)
                                checkpoints [0] = new Checkpoint (0, 0);
                }
 
-               static public void Push (SourceFile file)
-               {
-                       current_source = file != null ? file.Index : -1;
-                       // File is always pushed before being changed.
-               }
-               
-               public Location (int row, int column)
+               public Location (SourceFile file, int row, int column)
                {
                        if (row <= 0)
                                token = 0;
@@ -237,6 +269,9 @@ namespace Mono.CSharp
                                long target = -1;
                                long delta = 0;
 
+                               // TODO: For eval only, need better handling of empty
+                               int file_index = file == null ? 0 : file.Index;
+
                                // FIXME: This value is certainly wrong but what was the intension
                                int max = checkpoint_index < 10 ?
                                        checkpoint_index : 10;
@@ -245,13 +280,13 @@ namespace Mono.CSharp
                                        delta = row - offset;
                                        if (delta >= 0 &&
                                                delta < (1 << line_delta_bits) &&
-                                               checkpoints [checkpoint_index - i].File == current_source) {
+                                               checkpoints[checkpoint_index - i].File == file_index) {
                                                target = checkpoint_index - i;
                                                break;
                                        }
                                }
                                if (target == -1) {
-                                       AddCheckpoint (current_source, row);
+                                       AddCheckpoint (file_index, row);
                                        target = checkpoint_index;
                                        delta = row % (1 << line_delta_bits);
                                }
@@ -269,7 +304,7 @@ namespace Mono.CSharp
 
                public static Location operator - (Location loc, int columns)
                {
-                       return new Location (loc.Row, loc.Column - columns);
+                       return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
                }
 
                static void AddCheckpoint (int file, int row)
@@ -320,10 +355,10 @@ namespace Mono.CSharp
                public string NameFullPath {
                        get {
                                int index = File;
-                               if (token == 0 || index <= 0)
+                               if (index <= 0)
                                        return null;
 
-                               return source_list[index - 1].FullPathName;
+                               return source_list[index - 1].OriginalFullPathName;
                        }
                }
 
@@ -363,18 +398,6 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                        }
                }
 
-               // The ISymbolDocumentWriter interface is used by the symbol writer to
-               // describe a single source file - for each source file there's exactly
-               // one corresponding ISymbolDocumentWriter instance.
-               //
-               // This class has an internal hash table mapping source document names
-               // to such ISymbolDocumentWriter instances - so there's exactly one
-               // instance per document.
-               //
-               // This property returns the ISymbolDocumentWriter instance which belongs
-               // to the location's source file.
-               //
-               // If we don't have a symbol writer, this property is always null.
                public SourceFile SourceFile {
                        get {
                                int index = File;
@@ -402,11 +425,28 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                public class MemberLocations
                {
                        public readonly IList<Tuple<Modifiers, Location>> Modifiers;
-                       Location[] locations;
+                       List<Location> locations;
 
-                       public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
+                       public MemberLocations (IList<Tuple<Modifiers, Location>> mods)
                        {
                                Modifiers = mods;
+                       }
+
+                       public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location loc)
+                               : this (mods)
+                       {
+                               AddLocations (loc);
+                       }
+
+                       public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
+                               : this (mods)
+                       {
+                               AddLocations (locs);
+                       }
+
+                       public MemberLocations (IList<Tuple<Modifiers, Location>> mods, List<Location> locs)
+                               : this (mods)
+                       {
                                locations = locs;
                        }
 
@@ -420,31 +460,50 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                        
                        public int Count {
                                get {
-                                       return locations.Length;
+                                       return locations.Count;
                                }
                        }
 
                        #endregion
 
+                       public void AddLocations (Location loc)
+                       {
+                               if (locations == null) {
+                                       locations = new List<Location> ();
+                               }
+
+                               locations.Add (loc);
+                       }
+
                        public void AddLocations (params Location[] additional)
                        {
                                if (locations == null) {
-                                       locations = additional;
+                                       locations = new List<Location> (additional);
                                } else {
-                                       int pos = locations.Length;
-                                       Array.Resize (ref locations, pos + additional.Length);
-                                       additional.CopyTo (locations, pos);
+                                       locations.AddRange (additional);
                                }
                        }
                }
 
-               Dictionary<object, Location[]> simple_locs = new Dictionary<object, Location[]> (ReferenceEquality<object>.Default);
+               Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
                Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
 
                [Conditional ("FULL_AST")]
                public void AddLocation (object element, params Location[] locations)
                {
-                       simple_locs.Add (element, locations);
+                       simple_locs.Add (element, new List<Location> (locations));
+               }
+
+               [Conditional ("FULL_AST")]
+               public void InsertLocation (object element, int index, Location location)
+               {
+                       List<Location> found;
+                       if (!simple_locs.TryGetValue (element, out found)) {
+                               found = new List<Location> ();
+                               simple_locs.Add (element, found);
+                       }
+
+                       found.Insert (index, location);
                }
 
                [Conditional ("FULL_AST")]
@@ -453,7 +512,19 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                        if (locations.Length == 0)
                                throw new ArgumentException ("Statement is missing semicolon location");
 
-                       simple_locs.Add (element, locations);
+                       AddLocation (element, locations);
+               }
+
+               [Conditional ("FULL_AST")]
+               public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations)
+               {
+                       member_locs.Add (member, new MemberLocations (modLocations));
+               }
+
+               [Conditional ("FULL_AST")]
+               public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, Location location)
+               {
+                       member_locs.Add (member, new MemberLocations (modLocations, location));
                }
 
                [Conditional ("FULL_AST")]
@@ -463,13 +534,21 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                }
 
                [Conditional ("FULL_AST")]
-               public void AppendTo (object existing, params Location[] locations)
+               public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, List<Location> locations)
                {
-                       Location[] locs;
-                       if (simple_locs.TryGetValue (existing, out locs)) {
-                               simple_locs [existing] = locs.Concat (locations).ToArray ();
-                               return;
+                       member_locs.Add (member, new MemberLocations (modLocations, locations));
+               }
+
+               [Conditional ("FULL_AST")]
+               public void AppendTo (object element, Location location)
+               {
+                       List<Location> found;
+                       if (!simple_locs.TryGetValue (element, out found)) {
+                               found = new List<Location> ();
+                               simple_locs.Add (element, found);
                        }
+
+                       found.Add (location);
                }
 
                [Conditional ("FULL_AST")]
@@ -482,9 +561,9 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                        }
                }
 
-               public Location[] GetLocations (object element)
+               public List<Location> GetLocations (object element)
                {
-                       Location[] found;
+                       List<Location> found;
                        simple_locs.TryGetValue (element, out found);
                        return found;
                }