go-linemap.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // go-linemap.cc -- GCC implementation of Linemap.
  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. #include "go-linemap.h"
  6. // This class implements the Linemap interface defined by the
  7. // frontend.
  8. class Gcc_linemap : public Linemap
  9. {
  10. public:
  11. Gcc_linemap()
  12. : Linemap(),
  13. in_file_(false)
  14. { }
  15. void
  16. start_file(const char* file_name, unsigned int line_begin);
  17. void
  18. start_line(unsigned int line_number, unsigned int line_size);
  19. Location
  20. get_location(unsigned int column);
  21. void
  22. stop();
  23. protected:
  24. Location
  25. get_predeclared_location();
  26. Location
  27. get_unknown_location();
  28. bool
  29. is_predeclared(Location);
  30. bool
  31. is_unknown(Location);
  32. private:
  33. // Whether we are currently reading a file.
  34. bool in_file_;
  35. };
  36. Linemap* Linemap::instance_ = NULL;
  37. // Start getting locations from a new file.
  38. void
  39. Gcc_linemap::start_file(const char *file_name, unsigned line_begin)
  40. {
  41. if (this->in_file_)
  42. linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
  43. linemap_add(line_table, LC_ENTER, 0, file_name, line_begin);
  44. this->in_file_ = true;
  45. }
  46. // Stop getting locations.
  47. void
  48. Gcc_linemap::stop()
  49. {
  50. linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
  51. this->in_file_ = false;
  52. }
  53. // Start a new line.
  54. void
  55. Gcc_linemap::start_line(unsigned lineno, unsigned linesize)
  56. {
  57. linemap_line_start(line_table, lineno, linesize);
  58. }
  59. // Get a location.
  60. Location
  61. Gcc_linemap::get_location(unsigned column)
  62. {
  63. return Location(linemap_position_for_column(line_table, column));
  64. }
  65. // Get the unknown location.
  66. Location
  67. Gcc_linemap::get_unknown_location()
  68. {
  69. return Location(UNKNOWN_LOCATION);
  70. }
  71. // Get the predeclared location.
  72. Location
  73. Gcc_linemap::get_predeclared_location()
  74. {
  75. return Location(BUILTINS_LOCATION);
  76. }
  77. // Return whether a location is the predeclared location.
  78. bool
  79. Gcc_linemap::is_predeclared(Location loc)
  80. {
  81. return loc.gcc_location() == BUILTINS_LOCATION;
  82. }
  83. // Return whether a location is the unknown location.
  84. bool
  85. Gcc_linemap::is_unknown(Location loc)
  86. {
  87. return loc.gcc_location() == UNKNOWN_LOCATION;
  88. }
  89. // Return the Linemap to use for the gcc backend.
  90. Linemap*
  91. go_get_linemap()
  92. {
  93. return new Gcc_linemap;
  94. }