]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/rust-linemap.h
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / rust-linemap.h
1 // rust-linemap.h -- interface to location tracking -*- C++ -*-
2
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Copyright (C) 2020-2023 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 Linemap ()
37 {
38 // Only one instance of Linemap is allowed to exist.
39 rust_assert (Linemap::instance_ == NULL);
40 Linemap::instance_ = this;
41 }
42
43 virtual ~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 virtual void start_file (const char *file_name, unsigned int line_begin) = 0;
49
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)
54 = 0;
55
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;
59
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;
63
64 // Produce a human-readable description of a Location, e.g.
65 // "foo.rust:10". Returns an empty string for predeclared, builtin or
66 // unknown locations.
67 virtual std::string to_string (Location) = 0;
68
69 // Return the file name for a given location.
70 virtual std::string location_file (Location) = 0;
71
72 // Return the line number for a given location.
73 virtual int location_line (Location) = 0;
74
75 // Return the column number for a given location.
76 virtual int location_column (Location) = 0;
77
78 protected:
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;
84
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;
88
89 // Return whether the argument is the Location returned by
90 // get_predeclared_location.
91 virtual bool is_predeclared (Location) = 0;
92
93 // Return whether the argument is the Location returned by
94 // get_unknown_location.
95 virtual bool is_unknown (Location) = 0;
96
97 // The single existing instance of Linemap.
98 static Linemap *instance_;
99
100 public:
101 // Following are convenience static functions, which allow us to
102 // access some virtual functions without explicitly passing around
103 // an instance of Linemap.
104
105 // Return the special Location used for predeclared identifiers.
106 static Location predeclared_location ()
107 {
108 rust_assert (Linemap::instance_ != NULL);
109 return Linemap::instance_->get_predeclared_location ();
110 }
111
112 // Return the special Location used when no location is known.
113 static Location unknown_location ()
114 {
115 rust_assert (Linemap::instance_ != NULL);
116 return Linemap::instance_->get_unknown_location ();
117 }
118
119 // Return whether the argument is the special location used for
120 // predeclared identifiers.
121 static bool is_predeclared_location (Location loc)
122 {
123 rust_assert (Linemap::instance_ != NULL);
124 return Linemap::instance_->is_predeclared (loc);
125 }
126
127 // Return whether the argument is the special location used when no
128 // location is known.
129 static bool is_unknown_location (Location loc)
130 {
131 rust_assert (Linemap::instance_ != NULL);
132 return Linemap::instance_->is_unknown (loc);
133 }
134
135 // Produce a human-readable description of a Location.
136 static std::string location_to_string (Location loc)
137 {
138 rust_assert (Linemap::instance_ != NULL);
139 return Linemap::instance_->to_string (loc);
140 }
141
142 // Return the file name of a location.
143 static std::string location_to_file (Location loc)
144 {
145 rust_assert (Linemap::instance_ != NULL);
146 return Linemap::instance_->location_file (loc);
147 }
148
149 // Return line number of a location.
150 static int location_to_line (Location loc)
151 {
152 rust_assert (Linemap::instance_ != NULL);
153 return Linemap::instance_->location_line (loc);
154 }
155
156 static int location_to_column (Location loc)
157 {
158 rust_assert (Linemap::instance_ != NULL);
159 return Linemap::instance_->location_column (loc);
160 }
161 };
162
163 #endif // !defined(RUST_LINEMAP_H)