Line data Source code
1 : /* rust-attribs.c -- Rust attributes handling.
2 : Copyright (C) 2015-2026 Free Software Foundation, Inc.
3 :
4 : GCC is free software; you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation; either version 3, or (at your option)
7 : any later version.
8 :
9 : GCC is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public License
15 : along with GCC; see the file COPYING3. If not see
16 : <http://www.gnu.org/licenses/>. */
17 :
18 : #include "config.h"
19 : #include "system.h"
20 : #include "coretypes.h"
21 :
22 : #include "tree.h"
23 : #include "diagnostic.h"
24 : #include "tm.h"
25 : #include "cgraph.h"
26 : #include "toplev.h"
27 : #include "target.h"
28 : #include "common/common-target.h"
29 : #include "stringpool.h"
30 : #include "attribs.h"
31 : #include "varasm.h"
32 : #include "fold-const.h"
33 : #include "opts.h"
34 : #include "attr-handlers.h"
35 :
36 : /* Heavily based on the D frontend Only a subset of the attributes found in the
37 : * D frontend have been pulled, the goal being to have the builtin function
38 : * correctly setup. It's possible we may need to add extra attributes in the
39 : * future.
40 : */
41 :
42 : extern const struct scoped_attribute_specs grs_langhook_gnu_attribute_table;
43 : extern const struct scoped_attribute_specs grs_langhook_common_attribute_table;
44 :
45 : /* Internal attribute handlers for built-in functions. */
46 : static tree rust_handle_noreturn_attribute (tree *, tree, tree, int, bool *);
47 : static tree rust_handle_leaf_attribute (tree *, tree, tree, int, bool *);
48 : static tree rust_handle_const_attribute (tree *, tree, tree, int, bool *);
49 : static tree rust_handle_malloc_attribute (tree *, tree, tree, int, bool *);
50 : static tree rust_handle_pure_attribute (tree *, tree, tree, int, bool *);
51 : static tree rust_handle_novops_attribute (tree *, tree, tree, int, bool *);
52 : static tree rust_handle_nonnull_attribute (tree *, tree, tree, int, bool *);
53 : static tree rust_handle_nothrow_attribute (tree *, tree, tree, int, bool *);
54 : static tree rust_handle_transaction_pure_attribute (tree *, tree, tree, int,
55 : bool *);
56 : static tree rust_handle_returns_twice_attribute (tree *, tree, tree, int,
57 : bool *);
58 : static tree rust_handle_omp_declare_simd_attribute (tree *, tree, tree, int,
59 : bool *);
60 :
61 : /* Rust attribute handlers for user defined attributes. */
62 : static tree rust_handle_cold_attribute (tree *, tree, tree, int, bool *);
63 : static tree rust_handle_hot_attribute (tree *, tree, tree, int, bool *);
64 :
65 : /* Helper to define attribute exclusions. */
66 : #define ATTR_EXCL(name, function, type, variable) \
67 : { \
68 : name, function, type, variable \
69 : }
70 :
71 : // clang-format off
72 : // Disabling clang-format because it insists in having several ATTR_EXCL() on a
73 : // single line.
74 :
75 : static const struct attribute_spec::exclusions rust_attr_noreturn_exclusions[]
76 : = {
77 : // ATTR_EXCL ("alloc_size", true, true, true),
78 : ATTR_EXCL ("const", true, true, true),
79 : // ATTR_EXCL ("malloc", true, true, true),
80 : ATTR_EXCL ("pure", true, true, true),
81 : ATTR_EXCL ("returns_twice", true, true, true),
82 : ATTR_EXCL (NULL, false, false, false),
83 : };
84 :
85 : static const struct attribute_spec::exclusions rust_attr_const_pure_exclusions[]
86 : = {
87 : // ATTR_EXCL ("alloc_size", true, true, true),
88 : ATTR_EXCL ("const", true, true, true),
89 : ATTR_EXCL ("noreturn", true, true, true),
90 : ATTR_EXCL ("pure", true, true, true),
91 : ATTR_EXCL (NULL, false, false, false)
92 : };
93 :
94 : // clang-format on
95 :
96 : /* Helper to define an attribute. */
97 : #define ATTR_SPEC(name, min_len, max_len, decl_req, type_req, fn_type_req, \
98 : affects_type_identity, handler, exclude) \
99 : { \
100 : name, min_len, max_len, decl_req, type_req, fn_type_req, \
101 : affects_type_identity, handler, exclude \
102 : }
103 :
104 : /* Table of machine-independent attributes.
105 : For internal use (marking of built-ins) only. */
106 : static const attribute_spec grs_langhook_common_attributes[] = {
107 : ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
108 : rust_handle_noreturn_attribute, rust_attr_noreturn_exclusions),
109 : ATTR_SPEC ("leaf", 0, 0, true, false, false, false,
110 : rust_handle_leaf_attribute, NULL),
111 : ATTR_SPEC ("const", 0, 0, true, false, false, false,
112 : rust_handle_const_attribute, rust_attr_const_pure_exclusions),
113 : ATTR_SPEC ("malloc", 0, 0, true, false, false, false,
114 : rust_handle_malloc_attribute, NULL),
115 : ATTR_SPEC ("returns_twice", 0, 0, true, false, false, false,
116 : rust_handle_returns_twice_attribute,
117 : attr_returns_twice_exclusions),
118 : ATTR_SPEC ("pure", 0, 0, true, false, false, false,
119 : rust_handle_pure_attribute, rust_attr_const_pure_exclusions),
120 : ATTR_SPEC ("nonnull", 0, -1, false, true, true, false,
121 : rust_handle_nonnull_attribute, NULL),
122 : ATTR_SPEC ("nothrow", 0, 0, true, false, false, false,
123 : rust_handle_nothrow_attribute, NULL),
124 : ATTR_SPEC ("transaction_pure", 0, 0, false, true, true, false,
125 : rust_handle_transaction_pure_attribute, NULL),
126 : ATTR_SPEC ("no vops", 0, 0, true, false, false, false,
127 : rust_handle_novops_attribute, NULL),
128 : ATTR_SPEC ("type generic", 0, 0, false, true, true, false,
129 : handle_type_generic_attribute, NULL),
130 : ATTR_SPEC ("fn spec", 1, 1, false, true, true, false, handle_fnspec_attribute,
131 : NULL),
132 : ATTR_SPEC ("omp declare simd", 0, -1, true, false, false, false,
133 : rust_handle_omp_declare_simd_attribute, NULL),
134 : };
135 :
136 : const scoped_attribute_specs grs_langhook_common_attribute_table
137 : = {"gnu", {grs_langhook_common_attributes}};
138 :
139 : static const attribute_spec grs_langhook_gnu_attributes[] = {
140 : ATTR_SPEC ("cold", 0, 0, true, false, false, false,
141 : rust_handle_cold_attribute, attr_cold_hot_exclusions),
142 : ATTR_SPEC ("hot", 0, 0, true, false, false, false, rust_handle_hot_attribute,
143 : attr_cold_hot_exclusions),
144 : };
145 :
146 : const scoped_attribute_specs grs_langhook_gnu_attribute_table
147 : = {"gnu", {grs_langhook_gnu_attributes}};
148 :
149 : /* Built-in attribute handlers.
150 : These functions take the arguments:
151 : (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
152 :
153 : /* Handle a "noreturn" attribute; arguments as in
154 : struct attribute_spec.handler. */
155 :
156 : static tree
157 54100 : rust_handle_noreturn_attribute (tree *node, tree, tree, int, bool *)
158 : {
159 54100 : tree type = TREE_TYPE (*node);
160 :
161 54100 : if (TREE_CODE (*node) == FUNCTION_DECL)
162 54100 : TREE_THIS_VOLATILE (*node) = 1;
163 0 : else if (TREE_CODE (type) == POINTER_TYPE
164 0 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
165 0 : TREE_TYPE (*node) = build_pointer_type (
166 0 : build_type_variant (TREE_TYPE (type), TYPE_READONLY (TREE_TYPE (type)),
167 : 1));
168 : else
169 0 : gcc_unreachable ();
170 :
171 54100 : return NULL_TREE;
172 : }
173 :
174 : /* Handle a "leaf" attribute; arguments as in
175 : struct attribute_spec.handler. */
176 :
177 : static tree
178 7325140 : rust_handle_leaf_attribute (tree *node, tree name, tree, int,
179 : bool *no_add_attrs)
180 : {
181 7325140 : if (TREE_CODE (*node) != FUNCTION_DECL)
182 : {
183 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
184 0 : *no_add_attrs = true;
185 : }
186 7325140 : if (!TREE_PUBLIC (*node))
187 : {
188 0 : warning (OPT_Wattributes, "%qE attribute has no effect", name);
189 0 : *no_add_attrs = true;
190 : }
191 :
192 7325140 : return NULL_TREE;
193 : }
194 :
195 : /* Handle a "const" attribute; arguments as in
196 : struct attribute_spec.handler. */
197 :
198 : static tree
199 3089110 : rust_handle_const_attribute (tree *node, tree, tree, int, bool *)
200 : {
201 3089110 : tree type = TREE_TYPE (*node);
202 :
203 3089110 : if (TREE_CODE (*node) == FUNCTION_DECL)
204 3089110 : TREE_READONLY (*node) = 1;
205 0 : else if (TREE_CODE (type) == POINTER_TYPE
206 0 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
207 0 : TREE_TYPE (*node) = build_pointer_type (
208 0 : build_type_variant (TREE_TYPE (type), 1,
209 : TREE_THIS_VOLATILE (TREE_TYPE (type))));
210 : else
211 0 : gcc_unreachable ();
212 :
213 3089110 : return NULL_TREE;
214 : }
215 :
216 : /* Handle a "malloc" attribute; arguments as in
217 : struct attribute_spec.handler. */
218 :
219 : static tree
220 32460 : rust_handle_malloc_attribute (tree *node, tree, tree, int, bool *)
221 : {
222 32460 : gcc_assert (TREE_CODE (*node) == FUNCTION_DECL
223 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))));
224 32460 : DECL_IS_MALLOC (*node) = 1;
225 32460 : return NULL_TREE;
226 : }
227 :
228 : /* Handle a "pure" attribute; arguments as in
229 : struct attribute_spec.handler. */
230 :
231 : static tree
232 254270 : rust_handle_pure_attribute (tree *node, tree name, tree, int,
233 : bool *no_add_attrs)
234 : {
235 254270 : if (TREE_CODE (*node) != FUNCTION_DECL)
236 : {
237 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
238 0 : *no_add_attrs = true;
239 : }
240 :
241 254270 : DECL_PURE_P (*node) = 1;
242 254270 : return NULL_TREE;
243 : }
244 :
245 : /* Handle a "no vops" attribute; arguments as in
246 : struct attribute_spec.handler. */
247 :
248 : static tree
249 43280 : rust_handle_novops_attribute (tree *node, tree name, tree, int,
250 : bool *no_add_attrs)
251 : {
252 43280 : if (TREE_CODE (*node) != FUNCTION_DECL)
253 : {
254 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
255 0 : *no_add_attrs = true;
256 : }
257 :
258 43280 : DECL_IS_NOVOPS (*node) = 1;
259 43280 : return NULL_TREE;
260 : }
261 :
262 : /* Helper for nonnull attribute handling; fetch the operand number
263 : from the attribute argument list. */
264 :
265 : static bool
266 227220 : get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
267 : {
268 : /* Verify the arg number is a constant. */
269 227220 : if (!tree_fits_uhwi_p (arg_num_expr))
270 : return false;
271 :
272 227220 : *valp = TREE_INT_CST_LOW (arg_num_expr);
273 227220 : return true;
274 : }
275 :
276 : /* Handle the "nonnull" attribute. */
277 :
278 : static tree
279 622150 : rust_handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
280 : {
281 622150 : tree type = *node;
282 :
283 : /* If no arguments are specified, all pointer arguments should be
284 : non-null. Verify a full prototype is given so that the arguments
285 : will have the correct types when we actually check them later.
286 : Avoid diagnosing type-generic built-ins since those have no
287 : prototype. */
288 622150 : if (!args)
289 : {
290 443620 : gcc_assert (prototype_p (type) || !TYPE_ATTRIBUTES (type)
291 : || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
292 :
293 : return NULL_TREE;
294 : }
295 :
296 : /* Argument list specified. Verify that each argument number references
297 : a pointer argument. */
298 405750 : for (; args; args = TREE_CHAIN (args))
299 : {
300 227220 : tree argument;
301 227220 : unsigned HOST_WIDE_INT arg_num = 0, ck_num;
302 :
303 227220 : if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
304 0 : gcc_unreachable ();
305 :
306 227220 : argument = TYPE_ARG_TYPES (type);
307 227220 : if (argument)
308 : {
309 243450 : for (ck_num = 1;; ck_num++)
310 : {
311 470670 : if (!argument || ck_num == arg_num)
312 : break;
313 243450 : argument = TREE_CHAIN (argument);
314 : }
315 :
316 227220 : gcc_assert (argument
317 : && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
318 : }
319 : }
320 :
321 : return NULL_TREE;
322 : }
323 :
324 : /* Handle a "nothrow" attribute; arguments as in
325 : struct attribute_spec.handler. */
326 :
327 : static tree
328 7454980 : rust_handle_nothrow_attribute (tree *node, tree name, tree, int,
329 : bool *no_add_attrs)
330 : {
331 7454980 : if (TREE_CODE (*node) != FUNCTION_DECL)
332 : {
333 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
334 0 : *no_add_attrs = true;
335 : }
336 :
337 7454980 : TREE_NOTHROW (*node) = 1;
338 7454980 : return NULL_TREE;
339 : }
340 :
341 : /* Handle a "transaction_pure" attribute; arguments as in
342 : struct attribute_spec.handler. */
343 :
344 : static tree
345 5410 : rust_handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
346 : {
347 : /* Ensure we have a function type. */
348 5410 : gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
349 :
350 5410 : return NULL_TREE;
351 : }
352 :
353 : /* Handle a "returns_twice" attribute; arguments as in
354 : struct attribute_spec.handler. */
355 :
356 : static tree
357 5410 : rust_handle_returns_twice_attribute (tree *node, tree name, tree, int,
358 : bool *no_add_attrs)
359 : {
360 5410 : if (TREE_CODE (*node) != FUNCTION_DECL)
361 : {
362 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
363 0 : *no_add_attrs = true;
364 : }
365 :
366 5410 : DECL_IS_RETURNS_TWICE (*node) = 1;
367 :
368 5410 : return NULL_TREE;
369 : }
370 :
371 : /* Handle an "omp declare simd" attribute; arguments as in
372 : struct attribute_spec.handler. */
373 :
374 : static tree
375 0 : rust_handle_omp_declare_simd_attribute (tree *node, tree name, tree, int,
376 : bool *no_add_attrs)
377 : {
378 0 : if (TREE_CODE (*node) != FUNCTION_DECL)
379 : {
380 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
381 0 : *no_add_attrs = true;
382 : }
383 :
384 0 : return NULL_TREE;
385 : }
386 :
387 : /* Language specific attribute handlers.
388 : These functions take the arguments:
389 : (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
390 :
391 : /* Handle a "cold" and attribute; arguments as in
392 : struct attribute_spec.handler. */
393 :
394 : static tree
395 21640 : rust_handle_cold_attribute (tree *node, tree name, tree, int,
396 : bool *no_add_attrs)
397 : {
398 21640 : if (TREE_CODE (*node) != FUNCTION_DECL)
399 : {
400 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
401 0 : *no_add_attrs = true;
402 : }
403 :
404 21640 : return NULL_TREE;
405 : }
406 :
407 : static tree
408 0 : rust_handle_hot_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
409 : {
410 0 : if (TREE_CODE (*node) != FUNCTION_DECL)
411 : {
412 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
413 0 : *no_add_attrs = true;
414 : }
415 :
416 0 : return NULL_TREE;
417 : }
|