Line data Source code
1 : // Copyright (C) 2024-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-collect-lang-items.h"
20 : #include "optional.h"
21 : #include "rust-ast-collector.h"
22 : #include "rust-ast-visitor.h"
23 : #include "rust-ast.h"
24 : #include "rust-attribute-values.h"
25 : #include "rust-attributes.h"
26 : #include "rust-hir-map.h"
27 : #include "rust-item.h"
28 :
29 : namespace Rust {
30 : namespace AST {
31 :
32 : template <typename T>
33 : tl::optional<LangItem::Kind>
34 40402 : get_lang_item_attr (const T &maybe_lang_item)
35 : {
36 114067 : for (const auto &attr : maybe_lang_item.get_outer_attrs ())
37 : {
38 73665 : const auto &str_path = attr.get_path ().as_string ();
39 :
40 : // Attribute checking is done elsewhere, we can just check whether or not
41 : // we're dealing with a lang item here
42 :
43 73665 : bool is_lang_item = str_path == Values::Attributes::LANG;
44 :
45 73665 : if (is_lang_item)
46 : {
47 3661 : auto lang_item_type_str
48 : = Analysis::Attributes::extract_string_literal (attr);
49 :
50 3661 : rust_assert (lang_item_type_str.has_value ());
51 :
52 3661 : return LangItem::Parse (*lang_item_type_str);
53 3661 : }
54 : }
55 :
56 36741 : return tl::nullopt;
57 : }
58 :
59 : template <typename T>
60 : void
61 40402 : CollectLangItems::maybe_add_lang_item (const T &item)
62 : {
63 40402 : if (auto lang_item = get_lang_item_attr (item))
64 3660 : mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
65 40402 : }
66 :
67 : void
68 4219 : CollectLangItems::visit (AST::Trait &item)
69 : {
70 4219 : maybe_add_lang_item (item);
71 :
72 4219 : DefaultASTVisitor::visit (item);
73 4219 : }
74 :
75 : void
76 788 : CollectLangItems::visit (AST::TraitItemType &item)
77 : {
78 788 : maybe_add_lang_item (item);
79 :
80 788 : DefaultASTVisitor::visit (item);
81 788 : }
82 :
83 : void
84 31642 : CollectLangItems::visit (AST::Function &item)
85 : {
86 31642 : maybe_add_lang_item (item);
87 :
88 31642 : DefaultASTVisitor::visit (item);
89 31642 : }
90 :
91 : void
92 2350 : CollectLangItems::visit (AST::StructStruct &item)
93 : {
94 2350 : maybe_add_lang_item (item);
95 :
96 2350 : DefaultASTVisitor::visit (item);
97 2350 : }
98 :
99 : void
100 543 : CollectLangItems::visit (AST::EnumItem &item)
101 : {
102 543 : maybe_add_lang_item (item);
103 :
104 543 : DefaultASTVisitor::visit (item);
105 543 : }
106 :
107 : void
108 475 : CollectLangItems::visit (AST::EnumItemTuple &item)
109 : {
110 475 : maybe_add_lang_item (item);
111 :
112 475 : DefaultASTVisitor::visit (item);
113 475 : }
114 :
115 : void
116 95 : CollectLangItems::visit (AST::EnumItemStruct &item)
117 : {
118 95 : maybe_add_lang_item (item);
119 :
120 95 : DefaultASTVisitor::visit (item);
121 95 : }
122 :
123 : void
124 290 : CollectLangItems::visit (AST::EnumItemDiscriminant &item)
125 : {
126 290 : maybe_add_lang_item (item);
127 :
128 290 : DefaultASTVisitor::visit (item);
129 290 : }
130 :
131 : void
132 68 : CollectLangItems::visit (AST::ExternCrate &extern_crate)
133 : {
134 68 : auto &mappings = Analysis::Mappings::get ();
135 68 : auto crate_num
136 68 : = mappings.lookup_crate_name (extern_crate.get_referenced_crate ());
137 68 : if (crate_num)
138 : {
139 64 : visit (mappings.get_ast_crate (*crate_num));
140 : }
141 :
142 68 : DefaultASTVisitor::visit (extern_crate);
143 68 : }
144 :
145 : } // namespace AST
146 : } // namespace Rust
|