Branch data Line data Source code
1 : : // Copyright (C) 2024 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 : 38580 : get_lang_item_attr (const T &maybe_lang_item)
35 : : {
36 : 113468 : for (const auto &attr : maybe_lang_item.get_outer_attrs ())
37 : : {
38 : 74888 : const auto &str_path = attr.get_path ().as_string ();
39 : 74888 : if (!Analysis::Attributes::is_known (str_path))
40 : : {
41 : 0 : rust_error_at (attr.get_locus (), "unknown attribute %qs",
42 : : str_path.c_str ());
43 : : continue;
44 : : }
45 : :
46 : 74888 : bool is_lang_item = str_path == Values::Attributes::LANG;
47 : :
48 : 74888 : if (is_lang_item)
49 : : {
50 : 3304 : auto lang_item_type_str
51 : : = Analysis::Attributes::extract_string_literal (attr);
52 : :
53 : 3304 : rust_assert (lang_item_type_str.has_value ());
54 : :
55 : 3304 : return LangItem::Parse (*lang_item_type_str);
56 : 3304 : }
57 : : }
58 : :
59 : 35276 : return tl::nullopt;
60 : : }
61 : :
62 : : template <typename T>
63 : : void
64 : 38580 : CollectLangItems::maybe_add_lang_item (const T &item)
65 : : {
66 : 38580 : if (auto lang_item = get_lang_item_attr (item))
67 : 3276 : mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
68 : 38580 : }
69 : :
70 : : void
71 : 3846 : CollectLangItems::visit (AST::Trait &item)
72 : : {
73 : 3846 : maybe_add_lang_item (item);
74 : :
75 : 3846 : DefaultASTVisitor::visit (item);
76 : 3846 : }
77 : :
78 : : void
79 : 765 : CollectLangItems::visit (AST::TraitItemType &item)
80 : : {
81 : 765 : maybe_add_lang_item (item);
82 : :
83 : 765 : DefaultASTVisitor::visit (item);
84 : 765 : }
85 : :
86 : : void
87 : 30458 : CollectLangItems::visit (AST::Function &item)
88 : : {
89 : 30458 : maybe_add_lang_item (item);
90 : :
91 : 30458 : DefaultASTVisitor::visit (item);
92 : 30458 : }
93 : :
94 : : void
95 : 2162 : CollectLangItems::visit (AST::StructStruct &item)
96 : : {
97 : 2162 : maybe_add_lang_item (item);
98 : :
99 : 2162 : DefaultASTVisitor::visit (item);
100 : 2162 : }
101 : :
102 : : void
103 : 510 : CollectLangItems::visit (AST::EnumItem &item)
104 : : {
105 : 510 : maybe_add_lang_item (item);
106 : :
107 : 510 : DefaultASTVisitor::visit (item);
108 : 510 : }
109 : :
110 : : void
111 : 466 : CollectLangItems::visit (AST::EnumItemTuple &item)
112 : : {
113 : 466 : maybe_add_lang_item (item);
114 : :
115 : 466 : DefaultASTVisitor::visit (item);
116 : 466 : }
117 : :
118 : : void
119 : 92 : CollectLangItems::visit (AST::EnumItemStruct &item)
120 : : {
121 : 92 : maybe_add_lang_item (item);
122 : :
123 : 92 : DefaultASTVisitor::visit (item);
124 : 92 : }
125 : :
126 : : void
127 : 281 : CollectLangItems::visit (AST::EnumItemDiscriminant &item)
128 : : {
129 : 281 : maybe_add_lang_item (item);
130 : :
131 : 281 : DefaultASTVisitor::visit (item);
132 : 281 : }
133 : :
134 : : } // namespace AST
135 : : } // namespace Rust
|