1 // rust-linemap.h -- interface to location tracking -*- C++ -*-
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Copyright (C) 2020-2024 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. */
36 Linemap () : in_file_ (false)
38 // Only one instance of Linemap is allowed to exist.
39 rust_assert (Linemap::instance_
== NULL
);
40 Linemap::instance_
= this;
43 ~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 void start_file (const char *file_name
, unsigned int line_begin
);
50 // Stop generating Location values. This will be called after all
51 // input files have been read, in case any cleanup is required.
55 // The single existing instance of Linemap.
56 static Linemap
*instance_
;
59 // Following are convenience static functions, which allow us to
60 // access some virtual functions without explicitly passing around
61 // an instance of Linemap.
63 // Produce a human-readable description of a Location, e.g.
64 // "foo.rust:10". Returns an empty string for predeclared, builtin or
66 static std::string
location_to_string (location_t loc
);
69 // Whether we are currently reading a file.
73 #endif // !defined(RUST_LINEMAP_H)