1 // rust-linemap.h -- interface to location tracking -*- C++ -*-
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the '../go/gofrontend/LICENSE' file.
10 #define RUST_LINEMAP_H
12 #include "rust-system.h"
14 // The backend must define a type named Location which holds
15 // information about a location in a source file. The only thing the
16 // frontend does with instances of Location is pass them back to the
17 // backend interface. The Location type must be assignable, and it
18 // must be comparable: i.e., it must support operator= and operator<.
19 // The type is normally passed by value rather than by reference, and
20 // it should support that efficiently. The type should be defined in
22 #include "rust-location.h"
24 // The Linemap class is a pure abstract interface, plus some static
25 // convenience functions. The backend must implement the interface.
27 /* TODO: probably better to replace linemap implementation as pure abstract
28 * interface with some sort of compile-time switch (macros or maybe templates if
29 * doable without too much extra annoyance) as to the definition of the methods
30 * or whatever. This is to improve performance, as virtual function calls would
31 * otherwise have to be made in tight loops like in the lexer. */
38 // Only one instance of Linemap is allowed to exist.
39 rust_assert (Linemap::instance_
== NULL
);
40 Linemap::instance_
= this;
43 virtual ~Linemap () { Linemap::instance_
= NULL
; }
45 // Subsequent Location values will come from the file named
46 // FILE_NAME, starting at LINE_BEGIN. Normally LINE_BEGIN will be
47 // 0, but it will be non-zero if the Rust source has a //line comment.
48 virtual void start_file (const char *file_name
, unsigned int line_begin
) = 0;
50 // Subsequent Location values will come from the line LINE_NUMBER,
51 // in the current file. LINE_SIZE is the size of the line in bytes.
52 // This will normally be called for every line in a source file.
53 virtual void start_line (unsigned int line_number
, unsigned int line_size
)
56 // Get a Location representing column position COLUMN on the current
57 // line in the current file.
58 virtual Location
get_location (unsigned int column
) = 0;
60 // Stop generating Location values. This will be called after all
61 // input files have been read, in case any cleanup is required.
62 virtual void stop () = 0;
64 // Produce a human-readable description of a Location, e.g.
65 // "foo.rust:10". Returns an empty string for predeclared, builtin or
67 virtual std::string
to_string (Location
) = 0;
69 // Return the file name for a given location.
70 virtual std::string
location_file (Location
) = 0;
72 // Return the line number for a given location.
73 virtual int location_line (Location
) = 0;
75 // Return the column number for a given location.
76 virtual int location_column (Location
) = 0;
79 // Return a special Location used for predeclared identifiers. This
80 // Location should be different from that for any actual source
81 // file. This location will be used for various different types,
82 // functions, and objects created by the frontend.
83 virtual Location
get_predeclared_location () = 0;
85 // Return a special Location which indicates that no actual location
86 // is known. This is used for undefined objects and for errors.
87 virtual Location
get_unknown_location () = 0;
89 // Return whether the argument is the Location returned by
90 // get_predeclared_location.
91 virtual bool is_predeclared (Location
) = 0;
93 // Return whether the argument is the Location returned by
94 // get_unknown_location.
95 virtual bool is_unknown (Location
) = 0;
97 // The single existing instance of Linemap.
98 static Linemap
*instance_
;
101 // Following are convenience static functions, which allow us to
102 // access some virtual functions without explicitly passing around
103 // an instance of Linemap.
105 // Return the special Location used for predeclared identifiers.
106 static Location
predeclared_location ()
108 rust_assert (Linemap::instance_
!= NULL
);
109 return Linemap::instance_
->get_predeclared_location ();
112 // Return the special Location used when no location is known.
113 static Location
unknown_location ()
115 rust_assert (Linemap::instance_
!= NULL
);
116 return Linemap::instance_
->get_unknown_location ();
119 // Return whether the argument is the special location used for
120 // predeclared identifiers.
121 static bool is_predeclared_location (Location loc
)
123 rust_assert (Linemap::instance_
!= NULL
);
124 return Linemap::instance_
->is_predeclared (loc
);
127 // Return whether the argument is the special location used when no
128 // location is known.
129 static bool is_unknown_location (Location loc
)
131 rust_assert (Linemap::instance_
!= NULL
);
132 return Linemap::instance_
->is_unknown (loc
);
135 // Produce a human-readable description of a Location.
136 static std::string
location_to_string (Location loc
)
138 rust_assert (Linemap::instance_
!= NULL
);
139 return Linemap::instance_
->to_string (loc
);
142 // Return the file name of a location.
143 static std::string
location_to_file (Location loc
)
145 rust_assert (Linemap::instance_
!= NULL
);
146 return Linemap::instance_
->location_file (loc
);
149 // Return line number of a location.
150 static int location_to_line (Location loc
)
152 rust_assert (Linemap::instance_
!= NULL
);
153 return Linemap::instance_
->location_line (loc
);
156 static int location_to_column (Location loc
)
158 rust_assert (Linemap::instance_
!= NULL
);
159 return Linemap::instance_
->location_column (loc
);
163 #endif // !defined(RUST_LINEMAP_H)