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 138590 : get_lang_item_attr (const T &maybe_lang_item)
35 : {
36 636157 : for (const auto &attr : maybe_lang_item.get_outer_attrs ())
37 : {
38 497567 : 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 497567 : bool is_lang_item = str_path == Values::Attributes::LANG;
44 :
45 497567 : if (is_lang_item)
46 : {
47 4849 : auto lang_item_type_str
48 : = Analysis::Attributes::extract_string_literal (attr);
49 :
50 4849 : rust_assert (lang_item_type_str.has_value ());
51 :
52 4849 : return LangItem::Parse (*lang_item_type_str);
53 4849 : }
54 : }
55 :
56 133741 : return tl::nullopt;
57 : }
58 :
59 : template <typename T>
60 : void
61 138590 : CollectLangItems::maybe_add_lang_item (const T &item)
62 : {
63 138590 : if (auto lang_item = get_lang_item_attr (item))
64 4841 : mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
65 138590 : }
66 :
67 : void
68 5791 : CollectLangItems::visit (AST::Trait &item)
69 : {
70 5791 : maybe_add_lang_item (item);
71 :
72 5791 : DefaultASTVisitor::visit (item);
73 5791 : }
74 :
75 : void
76 1178 : CollectLangItems::visit (AST::TraitItemType &item)
77 : {
78 1178 : maybe_add_lang_item (item);
79 :
80 1178 : DefaultASTVisitor::visit (item);
81 1178 : }
82 :
83 : void
84 122636 : CollectLangItems::visit (AST::Function &item)
85 : {
86 122636 : maybe_add_lang_item (item);
87 :
88 122636 : DefaultASTVisitor::visit (item);
89 122636 : }
90 :
91 : void
92 6777 : CollectLangItems::visit (AST::StructStruct &item)
93 : {
94 6777 : maybe_add_lang_item (item);
95 :
96 6777 : DefaultASTVisitor::visit (item);
97 6777 : }
98 :
99 : void
100 1018 : CollectLangItems::visit (AST::EnumItem &item)
101 : {
102 1018 : maybe_add_lang_item (item);
103 :
104 1018 : DefaultASTVisitor::visit (item);
105 1018 : }
106 :
107 : void
108 767 : CollectLangItems::visit (AST::EnumItemTuple &item)
109 : {
110 767 : maybe_add_lang_item (item);
111 :
112 767 : DefaultASTVisitor::visit (item);
113 767 : }
114 :
115 : void
116 103 : CollectLangItems::visit (AST::EnumItemStruct &item)
117 : {
118 103 : maybe_add_lang_item (item);
119 :
120 103 : DefaultASTVisitor::visit (item);
121 103 : }
122 :
123 : void
124 320 : CollectLangItems::visit (AST::EnumItemDiscriminant &item)
125 : {
126 320 : maybe_add_lang_item (item);
127 :
128 320 : DefaultASTVisitor::visit (item);
129 320 : }
130 :
131 : void
132 89 : CollectLangItems::visit (AST::ExternCrate &extern_crate)
133 : {
134 89 : auto &crate_mappings = Analysis::Mappings::get ().crate;
135 89 : auto crate_num
136 89 : = crate_mappings.lookup_crate_name (extern_crate.get_referenced_crate ());
137 89 : if (crate_num)
138 : {
139 85 : visit (crate_mappings.get_ast_crate (*crate_num));
140 : }
141 :
142 89 : DefaultASTVisitor::visit (extern_crate);
143 89 : }
144 :
145 : } // namespace AST
146 : } // namespace Rust
|