go-linemap.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // go-linemap.h -- interface to location tracking -*- C++ -*-
  2. // Copyright 2011 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. #ifndef GO_LINEMAP_H
  6. #define GO_LINEMAP_H
  7. #include "go-system.h"
  8. // The backend must define a type named Location which holds
  9. // information about a location in a source file. The only thing the
  10. // frontend does with instances of Location is pass them back to the
  11. // backend interface. The Location type must be assignable, and it
  12. // must be comparable: i.e., it must support operator= and operator<.
  13. // The type is normally passed by value rather than by reference, and
  14. // it should support that efficiently. The type should be defined in
  15. // "go-location.h".
  16. #include "go-location.h"
  17. // The Linemap class is a pure abstract interface, plus some static
  18. // convenience functions. The backend must implement the interface.
  19. class Linemap
  20. {
  21. public:
  22. Linemap()
  23. {
  24. // Only one instance of Linemap is allowed to exist.
  25. go_assert(Linemap::instance_ == NULL);
  26. Linemap::instance_ = this;
  27. }
  28. virtual
  29. ~Linemap() { Linemap::instance_ = NULL; }
  30. // Subsequent Location values will come from the file named
  31. // FILE_NAME, starting at LINE_BEGIN. Normally LINE_BEGIN will be
  32. // 0, but it will be non-zero if the Go source has a //line comment.
  33. virtual void
  34. start_file(const char* file_name, unsigned int line_begin) = 0;
  35. // Subsequent Location values will come from the line LINE_NUMBER,
  36. // in the current file. LINE_SIZE is the size of the line in bytes.
  37. // This will normally be called for every line in a source file.
  38. virtual void
  39. start_line(unsigned int line_number, unsigned int line_size) = 0;
  40. // Get a Location representing column position COLUMN on the current
  41. // line in the current file.
  42. virtual Location
  43. get_location(unsigned int column) = 0;
  44. // Stop generating Location values. This will be called after all
  45. // input files have been read, in case any cleanup is required.
  46. virtual void
  47. stop() = 0;
  48. protected:
  49. // Return a special Location used for predeclared identifiers. This
  50. // Location should be different from that for any actual source
  51. // file. This location will be used for various different types,
  52. // functions, and objects created by the frontend.
  53. virtual Location
  54. get_predeclared_location() = 0;
  55. // Return a special Location which indicates that no actual location
  56. // is known. This is used for undefined objects and for errors.
  57. virtual Location
  58. get_unknown_location() = 0;
  59. // Return whether the argument is the Location returned by
  60. // get_predeclared_location.
  61. virtual bool
  62. is_predeclared(Location) = 0;
  63. // Return whether the argument is the Location returned by
  64. // get_unknown_location.
  65. virtual bool
  66. is_unknown(Location) = 0;
  67. // The single existing instance of Linemap.
  68. static Linemap *instance_;
  69. public:
  70. // Following are convenience static functions, which allow us to
  71. // access some virtual functions without explicitly passing around
  72. // an instance of Linemap.
  73. // Return the special Location used for predeclared identifiers.
  74. static Location
  75. predeclared_location()
  76. {
  77. go_assert(Linemap::instance_ != NULL);
  78. return Linemap::instance_->get_predeclared_location();
  79. }
  80. // Return the special Location used when no location is known.
  81. static Location
  82. unknown_location()
  83. {
  84. go_assert(Linemap::instance_ != NULL);
  85. return Linemap::instance_->get_unknown_location();
  86. }
  87. // Return whether the argument is the special location used for
  88. // predeclared identifiers.
  89. static bool
  90. is_predeclared_location(Location loc)
  91. {
  92. go_assert(Linemap::instance_ != NULL);
  93. return Linemap::instance_->is_predeclared(loc);
  94. }
  95. // Return whether the argument is the special location used when no
  96. // location is known.
  97. static bool
  98. is_unknown_location(Location loc)
  99. {
  100. go_assert(Linemap::instance_ != NULL);
  101. return Linemap::instance_->is_unknown(loc);
  102. }
  103. };
  104. // The backend interface must define this function. It should return
  105. // a fully implemented instance of Linemap.
  106. extern Linemap* go_get_linemap();
  107. #endif // !defined(GO_LINEMAP_H)