File tree 5 files changed +33
-13
lines changed
5 files changed +33
-13
lines changed Original file line number Diff line number Diff line change 4
4
5
5
#include " toolchain/check/dump_id.h"
6
6
7
+ #include " common/check.h"
7
8
#include " llvm/Support/raw_ostream.h"
8
9
#include " toolchain/check/context.h"
9
10
#include " toolchain/lex/tokenized_buffer.h"
13
14
namespace Carbon ::Check::DumpIdOverloads {
14
15
15
16
auto DumpId (SemIR::LocId loc_id, const Context& context) -> void {
17
+ if (!loc_id.is_valid ()) {
18
+ llvm::errs () << " LocId(invalid)" ;
19
+ return ;
20
+ }
21
+
16
22
if (loc_id.is_node_id ()) {
17
23
auto token = context.parse_tree ().node_token (loc_id.node_id ());
18
24
auto line = context.tokens ().GetLineNumber (token);
19
25
auto col = context.tokens ().GetColumnNumber (token);
20
26
const char * implicit = loc_id.is_implicit () ? " implicit" : " " ;
21
27
llvm::errs () << " LocId(line: " << line << " , col: " << col << implicit
22
28
<< " )" ;
23
- } else if (loc_id.is_import_ir_inst_id ()) {
29
+ } else {
30
+ CARBON_CHECK (loc_id.is_import_ir_inst_id ());
31
+
24
32
auto import_ir_id = context.sem_ir ()
25
33
.import_ir_insts ()
26
34
.Get (loc_id.import_ir_inst_id ())
@@ -30,8 +38,6 @@ auto DumpId(SemIR::LocId loc_id, const Context& context) -> void {
30
38
llvm::errs () << " LocId(import from \" " ;
31
39
llvm::errs ().write_escaped (import_file->filename ());
32
40
llvm::errs () << " \" )" ;
33
- } else {
34
- llvm::errs () << " LocId(invalid)" ;
35
41
}
36
42
}
37
43
Original file line number Diff line number Diff line change 9
9
10
10
namespace Carbon ::Lex::DumpIdOverloads {
11
11
12
- auto DumpId (TokenIndex /* token*/ , const TokenizedBuffer& /* buffer*/ ) -> void {
13
- llvm::errs () << " TokenIndex(?)" ;
12
+ auto DumpId (TokenIndex token, const TokenizedBuffer& buffer) -> void {
13
+ if (!token.is_valid ()) {
14
+ llvm::errs () << " TokenIndex(invalid)" ;
15
+ return ;
16
+ }
17
+
18
+ auto kind = buffer.GetKind (token);
19
+ auto line = buffer.GetLineNumber (token);
20
+ auto col = buffer.GetColumnNumber (token);
21
+
22
+ llvm::errs () << " TokenIndex(kind: " ;
23
+ kind.Print (llvm::errs ());
24
+ llvm::errs () << " , line: " << line << " , col: " << col << " )" ;
14
25
}
15
26
16
27
} // namespace Carbon::Lex::DumpIdOverloads
Original file line number Diff line number Diff line change 5
5
#include " toolchain/parse/dump_id.h"
6
6
7
7
#include " llvm/Support/raw_ostream.h"
8
+ #include " toolchain/lex/dump_id.h"
8
9
#include " toolchain/parse/tree.h"
9
10
10
11
namespace Carbon ::Parse::DumpIdOverloads {
11
12
12
13
auto DumpId (NodeId node_id, const Tree& tree) -> void {
13
14
if (!node_id.is_valid ()) {
14
15
llvm::errs () << " NodeId(invalid)" ;
16
+ return ;
15
17
}
16
18
17
- llvm::errs () << " NodeId( " ;
19
+ auto kind = tree. node_kind (node_id) ;
18
20
auto token = tree.node_token (node_id);
19
- llvm::errs () << " token = " ;
20
- tree.DumpId (token);
21
- llvm::errs () << " )" ;
22
21
23
- llvm::errs () << " A node id" ;
22
+ llvm::errs () << " NodeId(kind: " ;
23
+ kind.Print (llvm::errs ());
24
+ llvm::errs () << " , token: " ;
25
+ Lex::DumpIdOverloads::DumpId (token, tree.tokens ());
26
+ llvm::errs () << " )" ;
24
27
}
25
28
26
29
} // namespace Carbon::Parse::DumpIdOverloads
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ class DumpIdMethods {
29
29
30
30
public:
31
31
LLVM_DUMP_METHOD auto DumpId (Lex::TokenIndex token) const -> void {
32
- static_cast <const Tree&>(*this ).tokens_ -> DumpId (token);
32
+ static_cast <const Tree&>(*this ).tokens (). DumpId (token);
33
33
}
34
34
LLVM_DUMP_METHOD auto DumpId (NodeId node_id) const -> void {
35
35
DumpIdOverloads::DumpId (node_id, static_cast <const Tree&>(*this ));
Original file line number Diff line number Diff line change @@ -81,8 +81,6 @@ struct File;
81
81
// The DumpIdMethods parent class provides a `DumpId(x)` method for many types
82
82
// across the toolchain.
83
83
class Tree : public Printable <Tree>, public DumpIdMethods<Tree> {
84
- friend class DumpIdMethods <Tree>; // Access to `tokens_`.
85
-
86
84
public:
87
85
class PostorderIterator ;
88
86
@@ -137,6 +135,8 @@ class Tree : public Printable<Tree>, public DumpIdMethods<Tree> {
137
135
// Returns the token the given parse tree node models.
138
136
auto node_token (NodeId n) const -> Lex::TokenIndex;
139
137
138
+ auto tokens () const -> const Lex::TokenizedBuffer& { return *tokens_; }
139
+
140
140
// Returns whether this node is a valid node of the specified type.
141
141
template <typename T>
142
142
auto IsValid (NodeId node_id) const -> bool {
You can’t perform that action at this time.
0 commit comments