Debian Codegraph @

gcc/rust/util/rust-unwrap-segment.h

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