2010-07-05 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / mcs / location.cs
index 19c246be8c067d3859310b47be43d309b02dbeb9..e904f132b3702994be23ec4d74d4a22fc916c9a1 100644 (file)
@@ -4,15 +4,18 @@
 // Author:
 //   Miguel de Icaza
 //   Atsushi Enomoto  <atsushi@ximian.com>
+//   Marek Safar (marek.safar@gmail.com)
 //
-// (C) 2001 Ximian, Inc.
-// (C) 2005 Novell, Inc.
+// Copyright 2001 Ximian, Inc.
+// Copyright 2005 Novell, Inc.
 //
 
 using System;
 using System.IO;
-using System.Collections;
+using System.Collections.Generic;
 using Mono.CompilerServices.SymbolWriter;
+using System.Diagnostics;
+using System.Linq;
 
 namespace Mono.CSharp {
        /// <summary>
@@ -22,22 +25,47 @@ namespace Mono.CSharp {
        ///   This is intentionally a class and not a struct since we need
        ///   to pass this by reference.
        /// </remarks>
-       public sealed class SourceFile : ISourceFile {
+       public class SourceFile : ISourceFile {
                public readonly string Name;
                public readonly string Path;
                public readonly int Index;
-               public SourceFileEntry SourceFileEntry;
-               public bool HasLineDirective;
+               public bool AutoGenerated;
+               public bool IsIncludeFile;
 
-               public SourceFile (string name, string path, int index)
+               SourceFileEntry file;
+               byte[] guid, checksum;
+
+               public SourceFile (string name, string path, int index, bool is_include)
                {
                        this.Index = index;
                        this.Name = name;
                        this.Path = path;
+                       this.IsIncludeFile = is_include;
+               }
+
+               public SourceFileEntry SourceFileEntry {
+                       get { return file; }
                }
 
                SourceFileEntry ISourceFile.Entry {
-                       get { return SourceFileEntry; }
+                       get { return file; }
+               }
+
+               public void SetChecksum (byte[] guid, byte[] checksum)
+               {
+                       this.guid = guid;
+                       this.checksum = checksum;
+               }
+
+               public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
+               {
+                       if (guid != null)
+                               file = symwriter.DefineDocument (Path, guid, checksum);
+                       else {
+                               file = symwriter.DefineDocument (Path);
+                               if (AutoGenerated)
+                                       file.SetAutoGenerated ();
+                       }
                }
 
                public override string ToString ()
@@ -47,6 +75,82 @@ namespace Mono.CSharp {
                }
        }
 
+       public class CompilationUnit : SourceFile, ICompileUnit
+       {
+               CompileUnitEntry comp_unit;
+               Dictionary<string, SourceFile> include_files;
+               Dictionary<string, bool> conditionals;
+
+               public CompilationUnit (string name, string path, int index)
+                       : base (name, path, index, false)
+               { }
+
+               public void AddFile (SourceFile file)
+               {
+                       if (file == this)
+                               return;
+                       
+                       if (include_files == null)
+                               include_files = new Dictionary<string, SourceFile> ();
+
+                       if (!include_files.ContainsKey (file.Path))
+                               include_files.Add (file.Path, file);
+               }
+
+               public void AddDefine (string value)
+               {
+                       if (conditionals == null)
+                               conditionals = new Dictionary<string, bool> (2);
+
+                       conditionals [value] = true;
+               }
+
+               public void AddUndefine (string value)
+               {
+                       if (conditionals == null)
+                               conditionals = new Dictionary<string, bool> (2);
+
+                       conditionals [value] = false;
+               }
+
+               CompileUnitEntry ICompileUnit.Entry {
+                       get { return comp_unit; }
+               }
+
+               public CompileUnitEntry CompileUnitEntry {
+                       get { return comp_unit; }
+               }
+
+               public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
+               {
+                       base.DefineSymbolInfo (symwriter);
+
+                       comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
+
+                       if (include_files != null) {
+                               foreach (SourceFile include in include_files.Values) {
+                                       include.DefineSymbolInfo (symwriter);
+                                       comp_unit.AddFile (include.SourceFileEntry);
+                               }
+                       }
+               }
+
+               public bool IsConditionalDefined (string value)
+               {
+                       if (conditionals != null) {
+                               bool res;
+                               if (conditionals.TryGetValue (value, out res))
+                                       return res;
+                               
+                               // When conditional was undefined
+                               if (conditionals.ContainsKey (value))
+                                       return false;                                   
+                       }
+
+                       return RootContext.IsConditionalDefined (value);
+               }
+       }
+
        /// <summary>
        ///   Keeps track of the location in the program
        /// </summary>
@@ -68,71 +172,75 @@ namespace Mono.CSharp {
 
                struct Checkpoint {
                        public readonly int LineOffset;
+                       public readonly int CompilationUnit;
                        public readonly int File;
 
-                       public Checkpoint (int file, int line)
+                       public Checkpoint (int compile_unit, int file, int line)
                        {
                                File = file;
+                               CompilationUnit = compile_unit;
                                LineOffset = line - (int) (line % (1 << line_delta_bits));
                        }
                }
 
-               static ArrayList source_list;
-               static Hashtable source_files;
+               static List<SourceFile> source_list;
+               static List<CompilationUnit> compile_units;
+               static Dictionary<string, int> source_files;
                static int checkpoint_bits;
                static int source_count;
                static int current_source;
+               static int current_compile_unit;
                static int line_delta_bits;
                static int line_delta_mask;
                static int column_bits;
                static int column_mask;
                static Checkpoint [] checkpoints;
                static int checkpoint_index;
-
+               
                public readonly static Location Null = new Location (-1);
+               public static bool InEmacs;
                
                static Location ()
                {
-                       source_files = new Hashtable ();
-                       source_list = new ArrayList ();
-                       current_source = 0;
+                       Reset ();
                        checkpoints = new Checkpoint [10];
                }
 
                public static void Reset ()
                {
-                       source_files = new Hashtable ();
-                       source_list = new ArrayList ();
+                       source_files = new Dictionary<string, int> ();
+                       source_list = new List<SourceFile> ();
+                       compile_units = new List<CompilationUnit> ();
                        current_source = 0;
+                       current_compile_unit = 0;
                        source_count = 0;
                }
 
                // <summary>
                //   This must be called before parsing/tokenizing any files.
                // </summary>
-               static public void AddFile (string name)
+               static public void AddFile (Report r, string name)
                {
                        string path = Path.GetFullPath (name);
-
-                       if (source_files.Contains (path)){
-                               int id = (int) source_files [path];
-                               string other_name = ((SourceFile) source_list [id - 1]).Name;
+                       int id;
+                       if (source_files.TryGetValue (path, out id)){
+                               string other_name = source_list [id - 1].Name;
                                if (name.Equals (other_name))
-                                       Report.Warning (2002, "Source file `{0}' specified multiple times", name);
+                                       r.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
                                else
-                                       Report.Warning (2002, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
+                                       r.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
                                return;
                        }
 
                        source_files.Add (path, ++source_count);
-                       source_list.Add (new SourceFile (name, path, source_count));
+                       CompilationUnit unit = new CompilationUnit (name, path, source_count);
+                       source_list.Add (unit);
+                       compile_units.Add (unit);
                }
 
-               static public SourceFile[] SourceFiles {
+               public static IList<CompilationUnit> SourceFiles {
                        get {
-                               SourceFile[] retval = new SourceFile [source_list.Count];
-                               source_list.CopyTo (retval, 0);
-                               return retval;
+                               return compile_units;
                        }
                }
 
@@ -146,7 +254,7 @@ namespace Mono.CSharp {
                {
                        checkpoints = new Checkpoint [source_list.Count * 2];
                        if (checkpoints.Length > 0)
-                               checkpoints [0] = new Checkpoint (0, 0);
+                               checkpoints [0] = new Checkpoint (0, 0, 0);
 
                        column_bits = 8;
                        column_mask = 0xFF;
@@ -159,16 +267,21 @@ namespace Mono.CSharp {
                // <remarks>
                //   This is used when we encounter a #line preprocessing directive.
                // </remarks>
-               static public SourceFile LookupFile (string name)
+               static public SourceFile LookupFile (CompilationUnit comp_unit, string name)
                {
-                       string path = name == "" ? "" : Path.GetFullPath (name);
-
-                       if (!source_files.Contains (path)) {
+                       string path;
+                       if (!Path.IsPathRooted (name)) {
+                               string root = Path.GetDirectoryName (comp_unit.Path);
+                               path = Path.Combine (root, name);
+                       } else
+                               path = name;
+
+                       if (!source_files.ContainsKey (path)) {
                                if (source_count >= (1 << checkpoint_bits))
-                                       return new SourceFile (name, path, 0);
+                                       return new SourceFile (name, path, 0, true);
 
                                source_files.Add (path, ++source_count);
-                               SourceFile retval = new SourceFile (name, path, source_count);
+                               SourceFile retval = new SourceFile (name, path, source_count, true);
                                source_list.Add (retval);
                                return retval;
                        }
@@ -177,9 +290,10 @@ namespace Mono.CSharp {
                        return (SourceFile) source_list [index - 1];
                }
 
-               static public void Push (SourceFile file, int line)
+               static public void Push (CompilationUnit compile_unit, SourceFile file)
                {
-                       current_source = file.Index;
+                       current_source = file != null ? file.Index : -1;
+                       current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
                        // File is always pushed before being changed.
                }
 
@@ -188,11 +302,10 @@ namespace Mono.CSharp {
                //   and code generation to register all the source files with the
                //   symbol writer.
                // </remarks>
-               static public void DefineSymbolDocuments (SymbolWriter symwriter)
+               static public void DefineSymbolDocuments (MonoSymbolWriter symwriter)
                {
-                       foreach (SourceFile file in source_list) {
-                               file.SourceFileEntry = symwriter.DefineDocument (file.Path);
-                       }
+                       foreach (CompilationUnit unit in compile_units)
+                               unit.DefineSymbolInfo (symwriter);
                }
                
                public Location (int row)
@@ -205,7 +318,9 @@ namespace Mono.CSharp {
                        if (row <= 0)
                                token = 0;
                        else {
-                               if (column > 255)
+                               if (column > 254)
+                                       column = 254;
+                               if (column < 0)
                                        column = 255;
                                int target = -1;
                                int delta = 0;
@@ -222,7 +337,7 @@ namespace Mono.CSharp {
                                        }
                                }
                                if (target == -1) {
-                                       AddCheckpoint (current_source, row);
+                                       AddCheckpoint (current_compile_unit, current_source, row);
                                        target = checkpoint_index;
                                        delta = row % (1 << line_delta_bits);
                                }
@@ -233,22 +348,27 @@ namespace Mono.CSharp {
                        }
                }
 
-               static void AddCheckpoint (int file, int row)
+               public static Location operator - (Location loc, int columns)
+               {
+                       return new Location (loc.Row, loc.Column - columns);
+               }
+
+               static void AddCheckpoint (int compile_unit, int file, int row)
                {
                        if (checkpoints.Length == ++checkpoint_index) {
                                Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
                                Array.Copy (checkpoints, tmp, checkpoints.Length);
                                checkpoints = tmp;
                        }
-                       checkpoints [checkpoint_index] = new Checkpoint (file, row);
+                       checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
                }
-
+               
                public override string ToString ()
                {
-                       if (column_bits == 0)
-                               return Name + "(" + Row + "):";
+                       if (column_bits == 0 || InEmacs)
+                               return Name + "(" + Row.ToString () + "):";
                        else
-                               return Name + "(" + Row + "," + Column +
+                               return Name + "(" + Row.ToString () + "," + Column.ToString () +
                                        (Column == column_mask ? "+):" : "):");
                }
                
@@ -286,7 +406,23 @@ namespace Mono.CSharp {
                        get {
                                if (token == 0)
                                        return 1;
-                               return (int) (token & column_mask);
+                               int col = (int) (token & column_mask);
+                               return col == 255 ? 1 : col;
+                       }
+               }
+
+               public bool Hidden {
+                       get {
+                               return (int) (token & column_mask) == 255;
+                       }
+               }
+
+               public int CompilationUnitIndex {
+                       get {
+                               if (token == 0)
+                                       return 0;
+if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("Should not happen. Token is {0:X04}, checkpoints are {1}, index is {2}", token, checkpoints.Length, CheckpointIndex));
+                               return checkpoints [CheckpointIndex].CompilationUnit;
                        }
                }
 
@@ -319,22 +455,117 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
                                return (SourceFile) source_list [index - 1];
                        }
                }
+
+               public CompilationUnit CompilationUnit {
+                       get {
+                               int index = CompilationUnitIndex;
+                               if (index == 0)
+                                       return null;
+                               return (CompilationUnit) source_list [index - 1];
+                       }
+               }
        }
 
-       public class LocatedToken
+       //
+       // A bag of additional locations to support full ast tree
+       //
+       public class LocationsBag
        {
-               public readonly Location Location;
-               public readonly string Value;
+               public class MemberLocations
+               {
+                       public readonly IList<Tuple<Modifiers, Location>> Modifiers;
+                       Location[] locations;
+
+                       public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
+                       {
+                               Modifiers = mods;
+                               locations = locs;
+                       }
+
+                       #region Properties
+
+                       public Location this [int index] {
+                               get {
+                                       return locations [index];
+                               }
+                       }
+                       
+                       public int Count {
+                               get {
+                                       return locations.Length;
+                               }
+                       }
+
+                       #endregion
 
-               public LocatedToken (Location loc, string value)
+                       public void AddLocations (params Location[] additional)
+                       {
+                               if (locations == null) {
+                                       locations = additional;
+                               } else {
+                                       int pos = locations.Length;
+                                       Array.Resize (ref locations, pos + additional.Length);
+                                       additional.CopyTo (locations, pos);
+                               }
+                       }
+               }
+
+               Dictionary<object, Location[]> simple_locs = new Dictionary<object, 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)
                {
-                       Location = loc;
-                       Value = value;
+                       simple_locs.Add (element, locations);
                }
 
-               public override string ToString ()
+               [Conditional ("FULL_AST")]
+               public void AddStatement (object element, params Location[] locations)
+               {
+                       if (locations.Length == 0)
+                               throw new ArgumentException ("Statement is missing semicolon location");
+
+                       simple_locs.Add (element, locations);
+               }
+
+               [Conditional ("FULL_AST")]
+               public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
+               {
+                       member_locs.Add (member, new MemberLocations (modLocations, locations));
+               }
+
+               [Conditional ("FULL_AST")]
+               public void AppendTo (object existing, params Location[] locations)
+               {
+                       Location[] locs;
+                       if (simple_locs.TryGetValue (existing, out locs)) {
+                               simple_locs [existing] = locs.Concat (locations).ToArray ();
+                               return;
+                       }
+               }
+
+               [Conditional ("FULL_AST")]
+               public void AppendToMember (MemberCore existing, params Location[] locations)
+               {
+                       MemberLocations member;
+                       if (member_locs.TryGetValue (existing, out member)) {
+                               member.AddLocations (locations);
+                               return;
+                       }
+               }
+
+               public Location[] GetLocations (object element)
+               {
+                       Location[] found;
+                       simple_locs.TryGetValue (element, out found);
+                       return found;
+               }
+
+               public MemberLocations GetMemberLocation (MemberCore element)
                {
-                       return Location.ToString () + Value;
+                       MemberLocations found;
+                       member_locs.TryGetValue (element, out found);
+                       return found;
                }
        }
 }