Line data Source code
1 : // Copyright (C) 2020-2026 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 : #ifndef RUST_FINALIZED_NRCTX_H
20 : #define RUST_FINALIZED_NRCTX_H
21 :
22 : #include "rust-name-resolution-context.h"
23 : #include "rust-rib.h"
24 :
25 : namespace Rust {
26 : namespace Resolver2_0 {
27 :
28 : /**
29 : * Once the name resolution pass is complete, later passes of the compiler can
30 : * access it and still perform name resolution operations - for example, the
31 : * typechecker is able to insert method resolution into the name resolution
32 : * context.
33 : *
34 : * Using this type instead of `NameResolutionContext` provides better APIs which
35 : * are more suited to late passes, which shouldn't have to think about the inner
36 : * workings of name resolution too much.
37 : *
38 : * This class provides wrappers around all basic name resolution operations
39 : * (insertion, lookup) as well as an unsafe way to access the underlying name
40 : * resolution context.
41 : */
42 : class FinalizedNameResolutionContext
43 : {
44 : public:
45 : /**
46 : * Initialize the finalized name resolution context from a filled-out
47 : * `NameResolutionContext`. This function should be called after name
48 : * resolution is done, and `FinalizedNameResolutionContext` should be used
49 : * instead of `NameResolutionContext` by later passes of the pipeline.
50 : */
51 : static const FinalizedNameResolutionContext &
52 : init (NameResolutionContext &ctx);
53 :
54 : /**
55 : * Access the finalized name resolution context that you previously
56 : * initialized with `FinalizedNameResolutionContext::init`
57 : */
58 : static FinalizedNameResolutionContext &get ();
59 :
60 : /**
61 : * The exact same method as NameResolutionContext::map_usage, but this one
62 : * uses the leafmost definition by default - as we are past the name
63 : * resolution stage, every import chain has been resolved, and can be followed
64 : * to an actual definition instead of an import definition
65 : */
66 : void map_usage (Usage usage, Definition definition, Namespace ns);
67 :
68 : /**
69 : * Same as NameResolutionContext::lookup
70 : */
71 : tl::optional<NodeId> lookup (NodeId usage, Namespace ns) const;
72 : tl::optional<NameResolutionContext::NSLookup>
73 : lookup (NodeId usage, Namespace ns1, Namespace ns2) const;
74 : tl::optional<NameResolutionContext::NSLookup>
75 : lookup (NodeId usage, Namespace ns1, Namespace ns2, Namespace ns3) const;
76 :
77 : /**
78 : * Same as NameResolutionContext::to_canonical_path
79 : */
80 : Resolver::CanonicalPath to_canonical_path (NodeId id, Namespace ns) const;
81 :
82 : /**
83 : * Avoid using these and prefer adding wrapper methods to this class instead.
84 : *
85 : * Let's limit the number of times we use these, and eventually try to remove
86 : * them.
87 : */
88 14 : const NameResolutionContext &get_underlying () const { return ctx; }
89 52 : NameResolutionContext &get_underlying () { return ctx; }
90 :
91 : private:
92 : FinalizedNameResolutionContext (NameResolutionContext &ctx);
93 : FinalizedNameResolutionContext (const FinalizedNameResolutionContext &other)
94 : = default;
95 :
96 : NameResolutionContext &ctx;
97 : };
98 :
99 : } // namespace Resolver2_0
100 :
101 : } // namespace Rust
102 :
103 : #endif //! RUST_FINALIZED_NRCTX_H
|