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 : 21536 : get_lang_item_attr (const T &maybe_lang_item)
35 : : {
36 : 35557 : for (const auto &attr : maybe_lang_item.get_outer_attrs ())
37 : : {
38 : 14021 : const auto &str_path = attr.get_path ().as_string ();
39 : 14021 : 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 : 14021 : bool is_lang_item = str_path == Values::Attributes::LANG
47 : 3150 : && attr.has_attr_input ()
48 : 17171 : && attr.get_attr_input ().get_attr_input_type ()
49 : : == AST::AttrInput::AttrInputType::LITERAL;
50 : :
51 : : if (is_lang_item)
52 : : {
53 : : auto &literal
54 : 3150 : = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
55 : 3150 : const auto &lang_item_type_str = literal.get_literal ().as_string ();
56 : :
57 : 3150 : return LangItem::Parse (lang_item_type_str);
58 : 3150 : }
59 : : }
60 : :
61 : 18386 : return tl::nullopt;
62 : : }
63 : :
64 : : template <typename T>
65 : : void
66 : 21536 : CollectLangItems::maybe_add_lang_item (const T &item)
67 : : {
68 : 21536 : if (auto lang_item = get_lang_item_attr (item))
69 : 3149 : mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
70 : 21536 : }
71 : :
72 : : void
73 : 3640 : CollectLangItems::visit (AST::Trait &item)
74 : : {
75 : 3640 : maybe_add_lang_item (item);
76 : :
77 : 3640 : DefaultASTVisitor::visit (item);
78 : 3640 : }
79 : :
80 : : void
81 : 725 : CollectLangItems::visit (AST::TraitItemType &item)
82 : : {
83 : 725 : maybe_add_lang_item (item);
84 : :
85 : 725 : DefaultASTVisitor::visit (item);
86 : 725 : }
87 : :
88 : : void
89 : 14483 : CollectLangItems::visit (AST::Function &item)
90 : : {
91 : 14483 : maybe_add_lang_item (item);
92 : :
93 : 14483 : DefaultASTVisitor::visit (item);
94 : 14483 : }
95 : :
96 : : void
97 : 1437 : CollectLangItems::visit (AST::StructStruct &item)
98 : : {
99 : 1437 : maybe_add_lang_item (item);
100 : :
101 : 1437 : DefaultASTVisitor::visit (item);
102 : 1437 : }
103 : :
104 : : void
105 : 449 : CollectLangItems::visit (AST::EnumItem &item)
106 : : {
107 : 449 : maybe_add_lang_item (item);
108 : :
109 : 449 : DefaultASTVisitor::visit (item);
110 : 449 : }
111 : :
112 : : void
113 : 433 : CollectLangItems::visit (AST::EnumItemTuple &item)
114 : : {
115 : 433 : maybe_add_lang_item (item);
116 : :
117 : 433 : DefaultASTVisitor::visit (item);
118 : 433 : }
119 : :
120 : : void
121 : 92 : CollectLangItems::visit (AST::EnumItemStruct &item)
122 : : {
123 : 92 : maybe_add_lang_item (item);
124 : :
125 : 92 : DefaultASTVisitor::visit (item);
126 : 92 : }
127 : :
128 : : void
129 : 277 : CollectLangItems::visit (AST::EnumItemDiscriminant &item)
130 : : {
131 : 277 : maybe_add_lang_item (item);
132 : :
133 : 277 : DefaultASTVisitor::visit (item);
134 : 277 : }
135 : :
136 : : } // namespace AST
137 : : } // namespace Rust
|