Debian Codegraph @

gcc/rust/resolve/rust-ast-resolve.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include "rust-ast-resolve.h"
20 #include "rust-ast-full.h"
21 #include "rust-tyty.h"
22 #include "rust-ast-resolve-toplevel.h"
23 #include "rust-ast-resolve-item.h"
24 #include "rust-ast-resolve-expr.h"
25 #include "rust-ast-resolve-struct-expr-field.h"
26
27 extern bool
28 saw_errors (void);
29
30 namespace Rust {
31 namespace Resolver {
32
33 // NameResolution
34
35 NameResolution *
36 NameResolution::get ()
37 {
38 static NameResolution *instance;
39 if (instance == nullptr)
40 instance = new NameResolution ();
41
42 return instance;
43 }
44
45 NameResolution::NameResolution ()
46 : resolver (Resolver::get ()), mappings (Analysis::Mappings::get ())
47 {
48 // these are global
49 resolver->get_type_scope ().push (mappings.get_next_node_id ());
50 resolver->insert_builtin_types (resolver->get_type_scope ().peek ());
51 resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
52 }
53
54 void
55 NameResolution::Resolve (AST::Crate &crate)
56 {
57 auto resolver = get ();
58 resolver->go (crate);
59 }
60
61 void
62 NameResolution::go (AST::Crate &crate)
63 {
64 // lookup current crate name
65 CrateNum cnum = mappings.get_current_crate ();
66
67 // Clones the crate name instead of references due to gcc's possibly
68 // dangling references warnings
69 const auto crate_name = mappings.get_crate_name (cnum).value ();
70
71 // setup the ribs
72 NodeId scope_node_id = crate.get_node_id ();
73 resolver->get_name_scope ().push (scope_node_id);
74 resolver->get_type_scope ().push (scope_node_id);
75 resolver->get_label_scope ().push (scope_node_id);
76 resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
77 resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
78 resolver->push_new_label_rib (resolver->get_label_scope ().peek ());
79
80 // get the root segment
81 CanonicalPath crate_prefix
82 = CanonicalPath::new_seg (scope_node_id, crate_name);
83 crate_prefix.set_crate_num (cnum);
84
85 // setup a dummy crate node
86 resolver->get_name_scope ().insert (
87 CanonicalPath::new_seg (crate.get_node_id (), "__$$crate__"),
88 crate.get_node_id (), UNDEF_LOCATION);
89
90 // setup the root scope
91 resolver->push_new_module_scope (scope_node_id);
92
93 // first gather the top-level namespace names then we drill down so this
94 // allows for resolving forward declarations since an impl block might have
95 // a Self type Foo which is defined after the impl block for example.
96 for (auto &item : crate.items)
97 ResolveTopLevel::go (*item, CanonicalPath::create_empty (), crate_prefix);
98
99 // FIXME remove this
100 if (saw_errors ())
101 {
102 resolver->pop_module_scope ();
103 return;
104 }
105
106 // next we can drill down into the items and their scopes
107 for (auto &item : crate.items)
108 ResolveItem::go (*item, CanonicalPath::create_empty (), crate_prefix);
109
110 // done
111 resolver->pop_module_scope ();
112 }
113
114 } // namespace Resolver
115 } // namespace Rust