1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
|
//
// location.cs: Keeps track of the location of source code entity
//
// Author:
// Miguel de Icaza
// Atsushi Enomoto <atsushi@ximian.com>
// Marek Safar (marek.safar@gmail.com)
//
// Copyright 2001 Ximian, Inc.
// Copyright 2005 Novell, Inc.
//
using System;
using System.Collections.Generic;
using Mono.CompilerServices.SymbolWriter;
using System.Diagnostics;
using System.Linq;
namespace Mono.CSharp
{
//
// This is one single source file.
//
public class SourceFile : IEquatable<SourceFile>
{
//
// Used by #line directive to track hidden sequence point
// regions
//
struct LocationRegion : IComparable<LocationRegion>
{
public readonly Location Start;
public readonly Location End;
public LocationRegion (Location start, Location end)
{
this.Start = start;
this.End = end;
}
public int CompareTo (LocationRegion other)
{
if (Start.Row == other.Start.Row)
return Start.Column.CompareTo (other.Start.Column);
return Start.Row.CompareTo (other.Start.Row);
}
public override string ToString ()
{
return Start.ToString () + " - " + End.ToString ();
}
}
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 int Index;
public bool AutoGenerated;
SourceFileEntry file;
byte[] algGuid, checksum;
List<LocationRegion> hidden_lines;
public SourceFile (string name, string path, int index)
{
this.Index = index;
this.Name = name;
this.FullPathName = path;
}
public byte[] Checksum {
get {
return checksum;
}
}
public bool HasChecksum {
get {
return checksum != null;
}
}
public SourceFileEntry SourceFileEntry {
get {
return file;
}
}
public void SetChecksum (byte[] checksum)
{
SetChecksum (MD5Algorith, checksum);
}
public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
{
this.algGuid = algorithmGuid;
this.checksum = checksum;
}
public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter)
{
if (hidden_lines != null)
hidden_lines.Sort ();
file = new SourceFileEntry (symwriter, FullPathName, algGuid, checksum);
if (AutoGenerated)
file.SetAutoGenerated ();
return file;
}
public bool Equals (SourceFile other)
{
return FullPathName == other.FullPathName;
}
public bool IsHiddenLocation (Location loc)
{
if (hidden_lines == null)
return false;
int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
index = ~index;
if (index > 0) {
var found = hidden_lines[index - 1];
if (loc.Row < found.End.Row)
return true;
}
return false;
}
public void RegisterHiddenScope (Location start, Location end)
{
if (hidden_lines == null)
hidden_lines = new List<LocationRegion> ();
hidden_lines.Add (new LocationRegion (start, end));
}
public override string ToString ()
{
return String.Format ("SourceFile ({0}:{1}:{2})", Name, FullPathName, Index);
}
}
/// <summary>
/// Keeps track of the location in the program
/// </summary>
///
/// <remarks>
/// This uses a compact representation and a couple of auxiliary
/// structures to keep track of tokens to (file,line and column)
/// mappings. The usage of the bits is:
///
/// - 16 bits for "checkpoint" which is a mixed concept of
/// file and "line segment"
/// - 8 bits for line delta (offset) from the line segment
/// - 8 bits for column number.
///
/// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
/// </remarks>
public struct Location : IEquatable<Location>
{
struct Checkpoint {
public readonly int LineOffset;
public readonly int File;
public Checkpoint (int file, int line)
{
File = file;
LineOffset = line - (int) (line % (1 << line_delta_bits));
}
}
#if FULL_AST
readonly long token;
const int column_bits = 24;
const int line_delta_bits = 24;
#else
readonly int token;
const int column_bits = 8;
const int line_delta_bits = 8;
#endif
const int checkpoint_bits = 16;
const int column_mask = (1 << column_bits) - 1;
const int max_column = column_mask;
static List<SourceFile> source_list;
static Checkpoint [] checkpoints;
static int checkpoint_index;
public readonly static Location Null = new Location ();
public static bool InEmacs;
static Location ()
{
Reset ();
}
public static void Reset ()
{
source_list = new List<SourceFile> ();
checkpoint_index = 0;
}
public static void AddFile (SourceFile file)
{
source_list.Add (file);
}
// <summary>
// After adding all source files we want to compile with AddFile(), this method
// must be called to `reserve' an appropriate number of bits in the token for the
// source file. We reserve some extra space for files we encounter via #line
// directives while parsing.
// </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);
}
public Location (SourceFile file, int row, int column)
{
if (row <= 0)
token = 0;
else {
if (column > max_column)
column = max_column;
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;
for (int i = 0; i < max; i++) {
int offset = checkpoints [checkpoint_index - i].LineOffset;
delta = row - offset;
if (delta >= 0 &&
delta < (1 << line_delta_bits) &&
checkpoints[checkpoint_index - i].File == file_index) {
target = checkpoint_index - i;
break;
}
}
if (target == -1) {
AddCheckpoint (file_index, row);
target = checkpoint_index;
delta = row % (1 << line_delta_bits);
}
long l = column +
(delta << column_bits) +
(target << (line_delta_bits + column_bits));
#if FULL_AST
token = l;
#else
token = l > 0xFFFFFFFF ? 0 : (int) l;
#endif
}
}
public static Location operator - (Location loc, int columns)
{
return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
}
static void AddCheckpoint (int file, int row)
{
if (checkpoints.Length == ++checkpoint_index) {
Array.Resize (ref checkpoints, checkpoint_index * 2);
}
checkpoints [checkpoint_index] = new Checkpoint (file, row);
}
string FormatLocation (string fileName)
{
if (column_bits == 0 || InEmacs)
return fileName + "(" + Row.ToString () + "):";
return fileName + "(" + Row.ToString () + "," + Column.ToString () +
(Column == max_column ? "+):" : "):");
}
public override string ToString ()
{
return FormatLocation (Name);
}
public string ToStringFullName ()
{
return FormatLocation (NameFullPath);
}
/// <summary>
/// Whether the Location is Null
/// </summary>
public bool IsNull {
get { return token == 0; }
}
public string Name {
get {
int index = File;
if (token == 0 || index <= 0)
return null;
SourceFile file = source_list [index - 1];
return file.Name;
}
}
public string NameFullPath {
get {
int index = File;
if (token == 0 || index <= 0)
return null;
return source_list[index - 1].FullPathName;
}
}
int CheckpointIndex {
get {
const int checkpoint_mask = (1 << checkpoint_bits) - 1;
return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
}
}
public int Row {
get {
if (token == 0)
return 1;
int offset = checkpoints[CheckpointIndex].LineOffset;
const int line_delta_mask = (1 << column_bits) - 1;
return offset + (((int)(token >> column_bits)) & line_delta_mask);
}
}
public int Column {
get {
if (token == 0)
return 1;
return (int) (token & column_mask);
}
}
public int File {
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].File;
}
}
// 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;
if (index == 0)
return null;
return source_list [index - 1];
}
}
#region IEquatable<Location> Members
public bool Equals (Location other)
{
return this.token == other.token;
}
#endregion
}
public class SpecialsBag
{
public enum CommentType
{
Single,
Multi,
Documentation,
InactiveCode
}
public class SpecialVisitor
{
public virtual void Visit (Comment comment)
{
}
public virtual void Visit (NewLineToken newLineToken)
{
}
public virtual void Visit (PreProcessorDirective preProcessorDirective)
{
}
}
public abstract class SpecialBase
{
public abstract void Accept (SpecialVisitor visitor);
}
public class Comment : SpecialBase
{
public readonly CommentType CommentType;
public readonly bool StartsLine;
public readonly int Line;
public readonly int Col;
public readonly int EndLine;
public readonly int EndCol;
public readonly string Content;
public Comment (CommentType commentType, bool startsLine, int line, int col, int endLine, int endCol, string content)
{
this.CommentType = commentType;
this.StartsLine = startsLine;
this.Line = line;
this.Col = col;
this.EndLine = endLine;
this.EndCol = endCol;
this.Content = content;
}
public override string ToString ()
{
return string.Format ("[Comment: CommentType={0}, Line={1}, Col={2}, EndLine={3}, EndCol={4}, Content={5}]", CommentType, Line, Col, EndLine, EndCol, Content);
}
public override void Accept (SpecialVisitor visitor)
{
visitor.Visit (this);
}
}
public class NewLineToken : SpecialBase
{
public readonly int Line;
public readonly int Col;
public readonly NewLine NewLine;
public NewLineToken (int line, int col, NewLine newLine)
{
this.Line = line;
this.Col = col;
this.NewLine = newLine;
}
public override void Accept (SpecialVisitor visitor)
{
visitor.Visit (this);
}
}
public class PragmaPreProcessorDirective : PreProcessorDirective
{
public bool Disalbe { get; set; }
public List<int> Codes = new List<int> ();
public PragmaPreProcessorDirective (int line, int col, int endLine, int endCol, Tokenizer.PreprocessorDirective cmd, string arg) : base (line, col, endLine, endCol, cmd, arg)
{
}
}
public class LineProcessorDirective : PreProcessorDirective
{
public int LineNumber { get; set; }
public string FileName { get; set; }
public LineProcessorDirective (int line, int col, int endLine, int endCol, Tokenizer.PreprocessorDirective cmd, string arg) : base (line, col, endLine, endCol, cmd, arg)
{
}
}
public class PreProcessorDirective : SpecialBase
{
public readonly int Line;
public readonly int Col;
public readonly int EndLine;
public readonly int EndCol;
public readonly Tokenizer.PreprocessorDirective Cmd;
public readonly string Arg;
public bool Take = true;
public PreProcessorDirective (int line, int col, int endLine, int endCol, Tokenizer.PreprocessorDirective cmd, string arg)
{
this.Line = line;
this.Col = col;
this.EndLine = endLine;
this.EndCol = endCol;
this.Cmd = cmd;
this.Arg = arg;
}
public override void Accept (SpecialVisitor visitor)
{
visitor.Visit (this);
}
public override string ToString ()
{
return string.Format ("[PreProcessorDirective: Line={0}, Col={1}, EndLine={2}, EndCol={3}, Cmd={4}, Arg={5}]", Line, Col, EndLine, EndCol, Cmd, Arg);
}
}
public readonly List<SpecialBase> Specials = new List<SpecialBase> ();
CommentType curComment;
bool startsLine;
int startLine, startCol;
System.Text.StringBuilder contentBuilder = new System.Text.StringBuilder ();
[Conditional ("FULL_AST")]
public void StartComment (CommentType type, bool startsLine, int startLine, int startCol)
{
inComment = true;
curComment = type;
this.startsLine = startsLine;
this.startLine = startLine;
this.startCol = startCol;
contentBuilder.Length = 0;
}
[Conditional ("FULL_AST")]
public void PushCommentChar (int ch)
{
if (ch < 0)
return;
contentBuilder.Append ((char)ch);
}
[Conditional ("FULL_AST")]
public void PushCommentString (string str)
{
contentBuilder.Append (str);
}
bool inComment;
[Conditional ("FULL_AST")]
public void EndComment (int endLine, int endColumn)
{
if (!inComment)
return;
inComment = false;
Specials.Add (new Comment (curComment, startsLine, startLine, startCol, endLine, endColumn, contentBuilder.ToString ()));
}
[Conditional ("FULL_AST")]
public void AddPreProcessorDirective (int startLine, int startCol, int endLine, int endColumn, Tokenizer.PreprocessorDirective cmd, string arg)
{
if (inComment)
EndComment (startLine, startCol);
switch (cmd) {
case Tokenizer.PreprocessorDirective.Pragma:
Specials.Add (new PragmaPreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
break;
case Tokenizer.PreprocessorDirective.Line:
Specials.Add (new LineProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
break;
default:
Specials.Add (new PreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
break;
}
}
#if FULL_AST
public PragmaPreProcessorDirective SetPragmaDisable(bool disable)
{
var pragmaDirective = Specials [Specials.Count - 1] as PragmaPreProcessorDirective;
if (pragmaDirective == null)
return null;
pragmaDirective.Disalbe = disable;
return pragmaDirective;
}
#endif
public LineProcessorDirective GetCurrentLineProcessorDirective()
{
return Specials [Specials.Count - 1] as LineProcessorDirective;
}
public enum NewLine { Unix, Windows }
[Conditional ("FULL_AST")]
public void AddNewLine (int line, int col, NewLine newLine)
{
Specials.Add (new NewLineToken (line, col, newLine));
}
public void SkipIf ()
{
if (Specials.Count > 0) {
var directive = Specials[Specials.Count - 1] as PreProcessorDirective;
if (directive != null)
directive.Take = false;
}
}
}
//
// A bag of additional locations to support full ast tree
//
public class LocationsBag
{
public class MemberLocations
{
public IList<Tuple<Modifiers, Location>> Modifiers { get; internal set; }
List<Location> locations;
public MemberLocations (IList<Tuple<Modifiers, Location>> mods, IEnumerable<Location> locs)
{
Modifiers = mods;
locations = locs != null ? new List<Location> (locs) : null;
/*
public readonly IList<Tuple<Modifiers, Location>> Modifiers;
List<Location> locations;
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;*/
}
#region Properties
public Location this [int index] {
get {
return locations [index];
}
}
public int Count {
get {
return locations != null ? locations.Count : 0;
}
}
#endregion
public void AddLocations (Location loc)
{
if (locations == null) {
locations = new List<Location> ();
}
locations.Add (loc);
}
public void AddLocations (params Location[] additional)
{
AddLocations ((IEnumerable<Location>)additional);
}
public void AddLocations (IEnumerable<Location> additional)
{
if (additional == null)
return;
if (locations == null) {
locations = new List<Location> (additional);
} else {
locations.AddRange (additional);
}
}
}
public MemberCore LastMember {
get;
private set;
}
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)
{
AddLocation (element, (IEnumerable<Location>)locations);
}
[Conditional ("FULL_AST")]
public void AddLocation (object element, IEnumerable<Location> locations)
{
if (element == null || locations == null)
return;
List<Location> found;
if (!simple_locs.TryGetValue (element, out found)) {
simple_locs.Add (element, new List<Location> (locations));
return;
}
found.AddRange(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")]
public void AddStatement (object element, params Location[] locations)
{
if (element == null)
return;
if (locations.Length == 0)
throw new ArgumentException ("Statement is missing semicolon location");
simple_locs.Add (element, new List<Location>(locations));
}
[Conditional ("FULL_AST")]
public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
{
LastMember = member;
if (member == null)
return;
MemberLocations existing;
if (member_locs.TryGetValue (member, out existing)) {
existing.Modifiers = modLocations;
existing.AddLocations (locations);
return;
}
member_locs.Add (member, new MemberLocations (modLocations, locations));
}
[Conditional ("FULL_AST")]
public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, IEnumerable<Location> locations)
{
LastMember = member;
if (member == null)
return;
MemberLocations existing;
if (member_locs.TryGetValue (member, out existing)) {
existing.Modifiers = modLocations;
existing.AddLocations (locations);
return;
}
member_locs.Add (member, new MemberLocations (modLocations, locations));
}
[Conditional ("FULL_AST")]
public void AppendToMember (MemberCore existing, params Location[] locations)
{
AppendToMember (existing, (IEnumerable<Location>)locations);
}
[Conditional ("FULL_AST")]
public void AppendToMember (MemberCore existing, IEnumerable<Location> locations)
{
if (existing == null)
return;
MemberLocations member;
if (member_locs.TryGetValue (existing, out member)) {
member.AddLocations (locations);
return;
}
member_locs.Add (existing, new MemberLocations (null, locations));
}
public List<Location> GetLocations (object element)
{
if (element == null)
return null;
List<Location> found;
simple_locs.TryGetValue (element, out found);
return found;
}
public MemberLocations GetMemberLocation (MemberCore element)
{
MemberLocations found;
member_locs.TryGetValue (element, out found);
return found;
}
}
}
|