Line data Source code
1 : // Copyright (C) 2025-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_UNWRAP_SEGMENT_H
20 : #define RUST_UNWRAP_SEGMENT_H
21 :
22 : #include "rust-system.h"
23 : #include "optional.h"
24 : #include "rust-lang-item.h"
25 : #include "rust-mapping-common.h"
26 : #include <ast/rust-ast-full-decls.h>
27 :
28 : namespace Rust {
29 :
30 : /*
31 : * Used to convert different path segment object references
32 : * into SimplePathSegment/PathIdentSegment references
33 : *
34 : * unwrap_type_segment:
35 : * expands to a call to unwrap_type_segment_inner::unwrap,
36 : * used for type inference
37 : */
38 : #define unwrap_type_segment(x) \
39 : (unwrap_type_segment_inner<typename std::remove_const< \
40 : typename std::remove_reference<decltype (x)>::type>::type>::unwrap (x))
41 :
42 : template <class T> class unwrap_type_segment_inner;
43 :
44 : /* base case */
45 : template <> class unwrap_type_segment_inner<AST::SimplePathSegment>
46 : {
47 : public:
48 : /* The return type of unwrap */
49 : using ret = AST::SimplePathSegment;
50 :
51 : /* non-const qualified unwrap */
52 : static AST::SimplePathSegment &unwrap (AST::SimplePathSegment &x)
53 : {
54 : return x;
55 : }
56 :
57 : /* const qualified unwrap */
58 26288 : static const AST::SimplePathSegment &unwrap (const AST::SimplePathSegment &x)
59 : {
60 26288 : return x;
61 : }
62 : };
63 :
64 : /* case which dereferences unique_ptr */
65 : template <class T> class unwrap_type_segment_inner<std::unique_ptr<T>>
66 : {
67 : public:
68 : using ret = typename unwrap_type_segment_inner<T>::ret;
69 :
70 52933 : static ret &unwrap (std::unique_ptr<T> &x)
71 : {
72 52933 : return unwrap_type_segment (*x);
73 : }
74 56215 : static const ret &unwrap (const std::unique_ptr<T> &x)
75 : {
76 56215 : return unwrap_type_segment (*x);
77 : }
78 : };
79 :
80 : /* case which handles objects with a get_ident_segment member function */
81 : template <class T> class unwrap_type_segment_inner
82 : {
83 : public:
84 : using ret = AST::PathIdentSegment;
85 :
86 109220 : static ret &unwrap (T &x) { return x.get_ident_segment (); }
87 34848 : static const ret &unwrap (const T &x) { return x.get_ident_segment (); }
88 : };
89 :
90 : /*
91 : * Used to get the node id of a path segment object
92 : */
93 : NodeId unwrap_segment_node_id (const AST::TypePathSegment &seg);
94 :
95 : NodeId unwrap_segment_node_id (const AST::SimplePathSegment &seg);
96 :
97 : NodeId unwrap_segment_node_id (const AST::PathExprSegment &seg);
98 :
99 : template <class T>
100 : NodeId
101 57646 : unwrap_segment_node_id (const std::unique_ptr<T> &ptr)
102 : {
103 57646 : return unwrap_segment_node_id (*ptr);
104 : }
105 :
106 : /**
107 : * Used to check if a path segment is associated with a lang item
108 : */
109 : tl::optional<LangItem::Kind>
110 : unwrap_segment_get_lang_item (const AST::TypePathSegment &seg);
111 :
112 : tl::optional<LangItem::Kind>
113 : unwrap_segment_get_lang_item (const AST::SimplePathSegment &seg);
114 :
115 : tl::optional<LangItem::Kind>
116 : unwrap_segment_get_lang_item (const AST::PathExprSegment &seg);
117 :
118 : template <class T>
119 : tl::optional<LangItem::Kind>
120 109932 : unwrap_segment_get_lang_item (const std::unique_ptr<T> &ptr)
121 : {
122 109932 : return unwrap_segment_get_lang_item (*ptr);
123 : }
124 :
125 : /**
126 : * Used to output a path in error messages
127 : */
128 :
129 : inline static std::string
130 26 : unwrap_segment_error_string (const AST::TypePath &path)
131 : {
132 26 : return path.make_debug_string ();
133 : }
134 :
135 : inline static std::string
136 0 : unwrap_segment_error_string (const AST::PathInExpression &path)
137 : {
138 0 : return path.as_simple_path ().as_string ();
139 : }
140 :
141 : } // namespace Rust
142 :
143 : #endif // RUST_UNWRAP_SEGMENT_H
|