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 saw_errors (void);
28 : :
29 : : namespace Rust {
30 : : namespace Resolver {
31 : :
32 : : // NameResolution
33 : :
34 : : NameResolution *
35 : 0 : NameResolution::get ()
36 : : {
37 : 0 : static NameResolution *instance;
38 : 0 : if (instance == nullptr)
39 : 0 : instance = new NameResolution ();
40 : :
41 : 0 : return instance;
42 : : }
43 : :
44 : 0 : NameResolution::NameResolution ()
45 : 0 : : resolver (Resolver::get ()), mappings (Analysis::Mappings::get ())
46 : : {
47 : : // these are global
48 : 0 : resolver->get_type_scope ().push (mappings.get_next_node_id ());
49 : 0 : resolver->insert_builtin_types (resolver->get_type_scope ().peek ());
50 : 0 : resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
51 : 0 : }
52 : :
53 : : void
54 : 0 : NameResolution::Resolve (AST::Crate &crate)
55 : : {
56 : 0 : auto resolver = get ();
57 : 0 : resolver->go (crate);
58 : 0 : }
59 : :
60 : : void
61 : 0 : NameResolution::go (AST::Crate &crate)
62 : : {
63 : : // lookup current crate name
64 : 0 : CrateNum cnum = mappings.get_current_crate ();
65 : :
66 : : // Clones the crate name instead of references due to gcc's possibly
67 : : // dangling references warnings
68 : 0 : const auto crate_name = mappings.get_crate_name (cnum).value ();
69 : :
70 : : // setup the ribs
71 : 0 : NodeId scope_node_id = crate.get_node_id ();
72 : 0 : resolver->get_name_scope ().push (scope_node_id);
73 : 0 : resolver->get_type_scope ().push (scope_node_id);
74 : 0 : resolver->get_label_scope ().push (scope_node_id);
75 : 0 : resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
76 : 0 : resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
77 : 0 : resolver->push_new_label_rib (resolver->get_label_scope ().peek ());
78 : :
79 : : // get the root segment
80 : 0 : CanonicalPath crate_prefix
81 : 0 : = CanonicalPath::new_seg (scope_node_id, crate_name);
82 : 0 : crate_prefix.set_crate_num (cnum);
83 : :
84 : : // setup a dummy crate node
85 : 0 : resolver->get_name_scope ().insert (
86 : 0 : CanonicalPath::new_seg (crate.get_node_id (), "__$$crate__"),
87 : : crate.get_node_id (), UNDEF_LOCATION);
88 : :
89 : : // setup the root scope
90 : 0 : resolver->push_new_module_scope (scope_node_id);
91 : :
92 : : // first gather the top-level namespace names then we drill down so this
93 : : // allows for resolving forward declarations since an impl block might have
94 : : // a Self type Foo which is defined after the impl block for example.
95 : 0 : for (auto &item : crate.items)
96 : 0 : ResolveTopLevel::go (*item, CanonicalPath::create_empty (), crate_prefix);
97 : :
98 : : // FIXME remove this
99 : 0 : if (saw_errors ())
100 : : {
101 : 0 : resolver->pop_module_scope ();
102 : 0 : return;
103 : : }
104 : :
105 : : // next we can drill down into the items and their scopes
106 : 0 : for (auto &item : crate.items)
107 : 0 : ResolveItem::go (*item, CanonicalPath::create_empty (), crate_prefix);
108 : :
109 : : // done
110 : 0 : resolver->pop_module_scope ();
111 : 0 : }
112 : :
113 : : } // namespace Resolver
114 : : } // namespace Rust
|