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