Branch data Line data Source code
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 : 4490 : NameResolution::get ()
37 : : {
38 : 4490 : static NameResolution *instance;
39 : 4490 : if (instance == nullptr)
40 : 4466 : instance = new NameResolution ();
41 : :
42 : 4490 : return instance;
43 : : }
44 : :
45 : 4466 : NameResolution::NameResolution ()
46 : 4466 : : resolver (Resolver::get ()), mappings (Analysis::Mappings::get ())
47 : : {
48 : : // these are global
49 : 4466 : resolver->get_type_scope ().push (mappings.get_next_node_id ());
50 : 4466 : resolver->insert_builtin_types (resolver->get_type_scope ().peek ());
51 : 4466 : resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
52 : 4466 : }
53 : :
54 : : void
55 : 4490 : NameResolution::Resolve (AST::Crate &crate)
56 : : {
57 : 4490 : auto resolver = get ();
58 : 4490 : resolver->go (crate);
59 : 4489 : }
60 : :
61 : : void
62 : 4490 : NameResolution::go (AST::Crate &crate)
63 : : {
64 : : // lookup current crate name
65 : 4490 : 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 : 4490 : const auto crate_name = mappings.get_crate_name (cnum).value ();
70 : :
71 : : // setup the ribs
72 : 4490 : NodeId scope_node_id = crate.get_node_id ();
73 : 4490 : resolver->get_name_scope ().push (scope_node_id);
74 : 4490 : resolver->get_type_scope ().push (scope_node_id);
75 : 4490 : resolver->get_label_scope ().push (scope_node_id);
76 : 4490 : resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
77 : 4490 : resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
78 : 4490 : resolver->push_new_label_rib (resolver->get_label_scope ().peek ());
79 : :
80 : : // get the root segment
81 : 4490 : CanonicalPath crate_prefix
82 : 4490 : = CanonicalPath::new_seg (scope_node_id, crate_name);
83 : 4490 : crate_prefix.set_crate_num (cnum);
84 : :
85 : : // setup a dummy crate node
86 : 8980 : resolver->get_name_scope ().insert (
87 : 8980 : CanonicalPath::new_seg (crate.get_node_id (), "__$$crate__"),
88 : : crate.get_node_id (), UNDEF_LOCATION);
89 : :
90 : : // setup the root scope
91 : 4490 : 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 : 22263 : for (auto &item : crate.items)
97 : 17773 : ResolveTopLevel::go (*item, CanonicalPath::create_empty (), crate_prefix);
98 : :
99 : : // FIXME remove this
100 : 4490 : if (saw_errors ())
101 : : {
102 : 99 : resolver->pop_module_scope ();
103 : 99 : return;
104 : : }
105 : :
106 : : // next we can drill down into the items and their scopes
107 : 21835 : for (auto &item : crate.items)
108 : 17445 : ResolveItem::go (*item, CanonicalPath::create_empty (), crate_prefix);
109 : :
110 : : // done
111 : 4390 : resolver->pop_module_scope ();
112 : 4489 : }
113 : :
114 : : } // namespace Resolver
115 : : } // namespace Rust
|