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 44359 : get_lang_item_attr (const T &maybe_lang_item)
35 : {
36 130137 : for (const auto &attr : maybe_lang_item.get_outer_attrs ())
37 : {
38 85778 : 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 85778 : bool is_lang_item = str_path == Values::Attributes::LANG;
44 :
45 85778 : if (is_lang_item)
46 : {
47 3869 : auto lang_item_type_str
48 : = Analysis::Attributes::extract_string_literal (attr);
49 :
50 3869 : rust_assert (lang_item_type_str.has_value ());
51 :
52 3869 : return LangItem::Parse (*lang_item_type_str);
53 3869 : }
54 : }
55 :
56 40490 : return tl::nullopt;
57 : }
58 :
59 : template <typename T>
60 : void
61 44359 : CollectLangItems::maybe_add_lang_item (const T &item)
62 : {
63 44359 : if (auto lang_item = get_lang_item_attr (item))
64 3868 : mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
65 44359 : }
66 :
67 : void
68 4490 : CollectLangItems::visit (AST::Trait &item)
69 : {
70 4490 : maybe_add_lang_item (item);
71 :
72 4490 : DefaultASTVisitor::visit (item);
73 4490 : }
74 :
75 : void
76 888 : CollectLangItems::visit (AST::TraitItemType &item)
77 : {
78 888 : maybe_add_lang_item (item);
79 :
80 888 : DefaultASTVisitor::visit (item);
81 888 : }
82 :
83 : void
84 34871 : CollectLangItems::visit (AST::Function &item)
85 : {
86 34871 : maybe_add_lang_item (item);
87 :
88 34871 : DefaultASTVisitor::visit (item);
89 34871 : }
90 :
91 : void
92 2603 : CollectLangItems::visit (AST::StructStruct &item)
93 : {
94 2603 : maybe_add_lang_item (item);
95 :
96 2603 : DefaultASTVisitor::visit (item);
97 2603 : }
98 :
99 : void
100 578 : CollectLangItems::visit (AST::EnumItem &item)
101 : {
102 578 : maybe_add_lang_item (item);
103 :
104 578 : DefaultASTVisitor::visit (item);
105 578 : }
106 :
107 : void
108 533 : CollectLangItems::visit (AST::EnumItemTuple &item)
109 : {
110 533 : maybe_add_lang_item (item);
111 :
112 533 : DefaultASTVisitor::visit (item);
113 533 : }
114 :
115 : void
116 100 : CollectLangItems::visit (AST::EnumItemStruct &item)
117 : {
118 100 : maybe_add_lang_item (item);
119 :
120 100 : DefaultASTVisitor::visit (item);
121 100 : }
122 :
123 : void
124 296 : CollectLangItems::visit (AST::EnumItemDiscriminant &item)
125 : {
126 296 : maybe_add_lang_item (item);
127 :
128 296 : DefaultASTVisitor::visit (item);
129 296 : }
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
|