Branch data Line data Source code
1 : : // rust-linemap.h -- interface to location tracking -*- C++ -*-
2 : :
3 : : // Copyright 2011 The Go Authors. All rights reserved.
4 : : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
5 : :
6 : : // Use of this source code is governed by a BSD-style
7 : : // license that can be found in the '../go/gofrontend/LICENSE' file.
8 : :
9 : : #ifndef RUST_LINEMAP_H
10 : : #define RUST_LINEMAP_H
11 : :
12 : : #include "rust-system.h"
13 : :
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
21 : : // "rust-location.h".
22 : : #include "rust-location.h"
23 : :
24 : : // The Linemap class is a pure abstract interface, plus some static
25 : : // convenience functions. The backend must implement the interface.
26 : :
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. */
32 : :
33 : : class Linemap
34 : : {
35 : : public:
36 : 5120 : Linemap () : in_file_ (false)
37 : : {
38 : : // Only one instance of Linemap is allowed to exist.
39 : 5120 : rust_assert (Linemap::instance_ == NULL);
40 : 5120 : Linemap::instance_ = this;
41 : 5120 : }
42 : :
43 : : ~Linemap () { Linemap::instance_ = NULL; }
44 : :
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 : : void start_file (const char *file_name, unsigned int line_begin);
49 : :
50 : : // Stop generating Location values. This will be called after all
51 : : // input files have been read, in case any cleanup is required.
52 : : void stop ();
53 : :
54 : : protected:
55 : : // The single existing instance of Linemap.
56 : : static Linemap *instance_;
57 : :
58 : : public:
59 : : // Following are convenience static functions, which allow us to
60 : : // access some virtual functions without explicitly passing around
61 : : // an instance of Linemap.
62 : :
63 : : // Produce a human-readable description of a Location, e.g.
64 : : // "foo.rust:10". Returns an empty string for predeclared, builtin or
65 : : // unknown locations.
66 : : static std::string location_to_string (location_t loc);
67 : :
68 : : private:
69 : : // Whether we are currently reading a file.
70 : : bool in_file_;
71 : : };
72 : :
73 : : #endif // !defined(RUST_LINEMAP_H)
|