annotate gcc/go/gofrontend/go-linemap.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // go-linemap.h -- interface to location tracking -*- C++ -*-
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 // Copyright 2011 The Go Authors. All rights reserved.
kono
parents:
diff changeset
4 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
5 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 #ifndef GO_LINEMAP_H
kono
parents:
diff changeset
8 #define GO_LINEMAP_H
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 #include "go-system.h"
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 // The backend must define a type named Location which holds
kono
parents:
diff changeset
13 // information about a location in a source file. The only thing the
kono
parents:
diff changeset
14 // frontend does with instances of Location is pass them back to the
kono
parents:
diff changeset
15 // backend interface. The Location type must be assignable, and it
kono
parents:
diff changeset
16 // must be comparable: i.e., it must support operator= and operator<.
kono
parents:
diff changeset
17 // The type is normally passed by value rather than by reference, and
kono
parents:
diff changeset
18 // it should support that efficiently. The type should be defined in
kono
parents:
diff changeset
19 // "go-location.h".
kono
parents:
diff changeset
20 #include "go-location.h"
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 // The Linemap class is a pure abstract interface, plus some static
kono
parents:
diff changeset
23 // convenience functions. The backend must implement the interface.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 class Linemap
kono
parents:
diff changeset
26 {
kono
parents:
diff changeset
27 public:
kono
parents:
diff changeset
28 Linemap()
kono
parents:
diff changeset
29 {
kono
parents:
diff changeset
30 // Only one instance of Linemap is allowed to exist.
kono
parents:
diff changeset
31 go_assert(Linemap::instance_ == NULL);
kono
parents:
diff changeset
32 Linemap::instance_ = this;
kono
parents:
diff changeset
33 }
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 virtual
kono
parents:
diff changeset
36 ~Linemap() { Linemap::instance_ = NULL; }
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 // Subsequent Location values will come from the file named
kono
parents:
diff changeset
39 // FILE_NAME, starting at LINE_BEGIN. Normally LINE_BEGIN will be
kono
parents:
diff changeset
40 // 0, but it will be non-zero if the Go source has a //line comment.
kono
parents:
diff changeset
41 virtual void
kono
parents:
diff changeset
42 start_file(const char* file_name, unsigned int line_begin) = 0;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 // Subsequent Location values will come from the line LINE_NUMBER,
kono
parents:
diff changeset
45 // in the current file. LINE_SIZE is the size of the line in bytes.
kono
parents:
diff changeset
46 // This will normally be called for every line in a source file.
kono
parents:
diff changeset
47 virtual void
kono
parents:
diff changeset
48 start_line(unsigned int line_number, unsigned int line_size) = 0;
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 // Get a Location representing column position COLUMN on the current
kono
parents:
diff changeset
51 // line in the current file.
kono
parents:
diff changeset
52 virtual Location
kono
parents:
diff changeset
53 get_location(unsigned int column) = 0;
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 // Stop generating Location values. This will be called after all
kono
parents:
diff changeset
56 // input files have been read, in case any cleanup is required.
kono
parents:
diff changeset
57 virtual void
kono
parents:
diff changeset
58 stop() = 0;
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 // Produce a human-readable description of a Location, e.g.
kono
parents:
diff changeset
61 // "foo.go:10". Returns an empty string for predeclared, builtin or
kono
parents:
diff changeset
62 // unknown locations.
kono
parents:
diff changeset
63 virtual std::string
kono
parents:
diff changeset
64 to_string(Location) = 0;
kono
parents:
diff changeset
65
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
66 // Return the file name for a given location.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
67 virtual std::string
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
68 location_file(Location) = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
69
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
70 // Return the line number for a given location.
111
kono
parents:
diff changeset
71 virtual int
kono
parents:
diff changeset
72 location_line(Location) = 0;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 protected:
kono
parents:
diff changeset
75 // Return a special Location used for predeclared identifiers. This
kono
parents:
diff changeset
76 // Location should be different from that for any actual source
kono
parents:
diff changeset
77 // file. This location will be used for various different types,
kono
parents:
diff changeset
78 // functions, and objects created by the frontend.
kono
parents:
diff changeset
79 virtual Location
kono
parents:
diff changeset
80 get_predeclared_location() = 0;
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 // Return a special Location which indicates that no actual location
kono
parents:
diff changeset
83 // is known. This is used for undefined objects and for errors.
kono
parents:
diff changeset
84 virtual Location
kono
parents:
diff changeset
85 get_unknown_location() = 0;
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 // Return whether the argument is the Location returned by
kono
parents:
diff changeset
88 // get_predeclared_location.
kono
parents:
diff changeset
89 virtual bool
kono
parents:
diff changeset
90 is_predeclared(Location) = 0;
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 // Return whether the argument is the Location returned by
kono
parents:
diff changeset
93 // get_unknown_location.
kono
parents:
diff changeset
94 virtual bool
kono
parents:
diff changeset
95 is_unknown(Location) = 0;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 // The single existing instance of Linemap.
kono
parents:
diff changeset
98 static Linemap *instance_;
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 public:
kono
parents:
diff changeset
101 // Following are convenience static functions, which allow us to
kono
parents:
diff changeset
102 // access some virtual functions without explicitly passing around
kono
parents:
diff changeset
103 // an instance of Linemap.
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 // Return the special Location used for predeclared identifiers.
kono
parents:
diff changeset
106 static Location
kono
parents:
diff changeset
107 predeclared_location()
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 go_assert(Linemap::instance_ != NULL);
kono
parents:
diff changeset
110 return Linemap::instance_->get_predeclared_location();
kono
parents:
diff changeset
111 }
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 // Return the special Location used when no location is known.
kono
parents:
diff changeset
114 static Location
kono
parents:
diff changeset
115 unknown_location()
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 go_assert(Linemap::instance_ != NULL);
kono
parents:
diff changeset
118 return Linemap::instance_->get_unknown_location();
kono
parents:
diff changeset
119 }
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 // Return whether the argument is the special location used for
kono
parents:
diff changeset
122 // predeclared identifiers.
kono
parents:
diff changeset
123 static bool
kono
parents:
diff changeset
124 is_predeclared_location(Location loc)
kono
parents:
diff changeset
125 {
kono
parents:
diff changeset
126 go_assert(Linemap::instance_ != NULL);
kono
parents:
diff changeset
127 return Linemap::instance_->is_predeclared(loc);
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 // Return whether the argument is the special location used when no
kono
parents:
diff changeset
131 // location is known.
kono
parents:
diff changeset
132 static bool
kono
parents:
diff changeset
133 is_unknown_location(Location loc)
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 go_assert(Linemap::instance_ != NULL);
kono
parents:
diff changeset
136 return Linemap::instance_->is_unknown(loc);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 // Produce a human-readable description of a Location.
kono
parents:
diff changeset
140 static std::string
kono
parents:
diff changeset
141 location_to_string(Location loc)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 go_assert(Linemap::instance_ != NULL);
kono
parents:
diff changeset
144 return Linemap::instance_->to_string(loc);
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
147 // Return the file name of a location.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
148 static std::string
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
149 location_to_file(Location loc)
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
150 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
151 go_assert(Linemap::instance_ != NULL);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
152 return Linemap::instance_->location_file(loc);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
153 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
154
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
155 // Return line number of a location.
111
kono
parents:
diff changeset
156 static int
kono
parents:
diff changeset
157 location_to_line(Location loc)
kono
parents:
diff changeset
158 {
kono
parents:
diff changeset
159 go_assert(Linemap::instance_ != NULL);
kono
parents:
diff changeset
160 return Linemap::instance_->location_line(loc);
kono
parents:
diff changeset
161 }
kono
parents:
diff changeset
162 };
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 #endif // !defined(GO_LINEMAP_H)