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 :
35 : /* Heavily based on the D frontend Only a subset of the attributes found in the
36 : * D frontend have been pulled, the goal being to have the builtin function
37 : * correctly setup. It's possible we may need to add extra attributes in the
38 : * future.
39 : */
40 :
41 : extern const struct scoped_attribute_specs grs_langhook_gnu_attribute_table;
42 : extern const struct scoped_attribute_specs grs_langhook_common_attribute_table;
43 :
44 : /* Internal attribute handlers for built-in functions. */
45 : static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
46 : static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
47 : static tree handle_const_attribute (tree *, tree, tree, int, bool *);
48 : static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
49 : static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
50 : static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
51 : static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
52 : static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
53 : static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
54 : static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
55 : static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
56 : static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
57 : static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *);
58 :
59 : /* Rust attribute handlers for user defined attributes. */
60 : static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
61 : static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
62 :
63 : /* Helper to define attribute exclusions. */
64 : #define ATTR_EXCL(name, function, type, variable) \
65 : { \
66 : name, function, type, variable \
67 : }
68 :
69 : // clang-format off
70 : // Disabling clang-format because it insists in having several ATTR_EXCL() on a
71 : // single line.
72 :
73 : static const struct attribute_spec::exclusions attr_noreturn_exclusions[] = {
74 : // ATTR_EXCL ("alloc_size", true, true, true),
75 : ATTR_EXCL ("const", true, true, true),
76 : // ATTR_EXCL ("malloc", true, true, true),
77 : ATTR_EXCL ("pure", true, true, true),
78 : ATTR_EXCL ("returns_twice", true, true, true),
79 : ATTR_EXCL (NULL, false, false, false),
80 : };
81 :
82 : static const struct attribute_spec::exclusions attr_returns_twice_exclusions[]
83 : = {
84 : ATTR_EXCL ("noreturn", true, true, true),
85 : ATTR_EXCL (NULL, false, false, false),
86 : };
87 :
88 : extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] = {
89 :
90 : ATTR_EXCL ("cold", true, true, true),
91 : ATTR_EXCL ("hot", true, true, true),
92 : ATTR_EXCL (NULL, false, false, false)
93 : };
94 :
95 : static const struct attribute_spec::exclusions attr_const_pure_exclusions[] = {
96 : // ATTR_EXCL ("alloc_size", true, true, true),
97 : ATTR_EXCL ("const", true, true, true),
98 : ATTR_EXCL ("noreturn", true, true, true),
99 : ATTR_EXCL ("pure", true, true, true),
100 : ATTR_EXCL (NULL, false, false, false)
101 : };
102 :
103 : // clang-format on
104 :
105 : /* Helper to define an attribute. */
106 : #define ATTR_SPEC(name, min_len, max_len, decl_req, type_req, fn_type_req, \
107 : affects_type_identity, handler, exclude) \
108 : { \
109 : name, min_len, max_len, decl_req, type_req, fn_type_req, \
110 : affects_type_identity, handler, exclude \
111 : }
112 :
113 : /* Table of machine-independent attributes.
114 : For internal use (marking of built-ins) only. */
115 : static const attribute_spec grs_langhook_common_attributes[] = {
116 : ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
117 : handle_noreturn_attribute, attr_noreturn_exclusions),
118 : ATTR_SPEC ("leaf", 0, 0, true, false, false, false, handle_leaf_attribute,
119 : NULL),
120 : ATTR_SPEC ("const", 0, 0, true, false, false, false, handle_const_attribute,
121 : attr_const_pure_exclusions),
122 : ATTR_SPEC ("malloc", 0, 0, true, false, false, false, handle_malloc_attribute,
123 : NULL),
124 : ATTR_SPEC ("returns_twice", 0, 0, true, false, false, false,
125 : handle_returns_twice_attribute, attr_returns_twice_exclusions),
126 : ATTR_SPEC ("pure", 0, 0, true, false, false, false, handle_pure_attribute,
127 : attr_const_pure_exclusions),
128 : ATTR_SPEC ("nonnull", 0, -1, false, true, true, false,
129 : handle_nonnull_attribute, NULL),
130 : ATTR_SPEC ("nothrow", 0, 0, true, false, false, false,
131 : handle_nothrow_attribute, NULL),
132 : ATTR_SPEC ("transaction_pure", 0, 0, false, true, true, false,
133 : handle_transaction_pure_attribute, NULL),
134 : ATTR_SPEC ("no vops", 0, 0, true, false, false, false,
135 : handle_novops_attribute, NULL),
136 : ATTR_SPEC ("type generic", 0, 0, false, true, true, false,
137 : handle_type_generic_attribute, NULL),
138 : ATTR_SPEC ("fn spec", 1, 1, false, true, true, false, handle_fnspec_attribute,
139 : NULL),
140 : ATTR_SPEC ("omp declare simd", 0, -1, true, false, false, false,
141 : handle_omp_declare_simd_attribute, NULL),
142 : };
143 :
144 : const scoped_attribute_specs grs_langhook_common_attribute_table
145 : = {"gnu", {grs_langhook_common_attributes}};
146 :
147 : static const attribute_spec grs_langhook_gnu_attributes[] = {
148 : ATTR_SPEC ("cold", 0, 0, true, false, false, false, handle_cold_attribute,
149 : attr_cold_hot_exclusions),
150 : ATTR_SPEC ("hot", 0, 0, true, false, false, false, handle_hot_attribute,
151 : attr_cold_hot_exclusions),
152 : };
153 :
154 : const scoped_attribute_specs grs_langhook_gnu_attribute_table
155 : = {"gnu", {grs_langhook_gnu_attributes}};
156 :
157 : /* Built-in attribute handlers.
158 : These functions take the arguments:
159 : (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
160 :
161 : /* Handle a "noreturn" attribute; arguments as in
162 : struct attribute_spec.handler. */
163 :
164 : static tree
165 13640 : handle_noreturn_attribute (tree *node, tree, tree, int, bool *)
166 : {
167 13640 : tree type = TREE_TYPE (*node);
168 :
169 13640 : if (TREE_CODE (*node) == FUNCTION_DECL)
170 13640 : TREE_THIS_VOLATILE (*node) = 1;
171 0 : else if (TREE_CODE (type) == POINTER_TYPE
172 0 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
173 0 : TREE_TYPE (*node) = build_pointer_type (
174 0 : build_type_variant (TREE_TYPE (type), TYPE_READONLY (TREE_TYPE (type)),
175 : 1));
176 : else
177 0 : gcc_unreachable ();
178 :
179 13640 : return NULL_TREE;
180 : }
181 :
182 : /* Handle a "leaf" attribute; arguments as in
183 : struct attribute_spec.handler. */
184 :
185 : static tree
186 1831852 : handle_leaf_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
187 : {
188 1831852 : if (TREE_CODE (*node) != FUNCTION_DECL)
189 : {
190 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
191 0 : *no_add_attrs = true;
192 : }
193 1831852 : if (!TREE_PUBLIC (*node))
194 : {
195 0 : warning (OPT_Wattributes, "%qE attribute has no effect", name);
196 0 : *no_add_attrs = true;
197 : }
198 :
199 1831852 : return NULL_TREE;
200 : }
201 :
202 : /* Handle a "const" attribute; arguments as in
203 : struct attribute_spec.handler. */
204 :
205 : static tree
206 772024 : handle_const_attribute (tree *node, tree, tree, int, bool *)
207 : {
208 772024 : tree type = TREE_TYPE (*node);
209 :
210 772024 : if (TREE_CODE (*node) == FUNCTION_DECL)
211 772024 : TREE_READONLY (*node) = 1;
212 0 : else if (TREE_CODE (type) == POINTER_TYPE
213 0 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
214 0 : TREE_TYPE (*node) = build_pointer_type (
215 0 : build_type_variant (TREE_TYPE (type), 1,
216 : TREE_THIS_VOLATILE (TREE_TYPE (type))));
217 : else
218 0 : gcc_unreachable ();
219 :
220 772024 : return NULL_TREE;
221 : }
222 :
223 : /* Handle a "malloc" attribute; arguments as in
224 : struct attribute_spec.handler. */
225 :
226 : static tree
227 8184 : handle_malloc_attribute (tree *node, tree, tree, int, bool *)
228 : {
229 8184 : gcc_assert (TREE_CODE (*node) == FUNCTION_DECL
230 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))));
231 8184 : DECL_IS_MALLOC (*node) = 1;
232 8184 : return NULL_TREE;
233 : }
234 :
235 : /* Handle a "pure" attribute; arguments as in
236 : struct attribute_spec.handler. */
237 :
238 : static tree
239 64108 : handle_pure_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
240 : {
241 64108 : if (TREE_CODE (*node) != FUNCTION_DECL)
242 : {
243 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
244 0 : *no_add_attrs = true;
245 : }
246 :
247 64108 : DECL_PURE_P (*node) = 1;
248 64108 : return NULL_TREE;
249 : }
250 :
251 : /* Handle a "no vops" attribute; arguments as in
252 : struct attribute_spec.handler. */
253 :
254 : static tree
255 10912 : handle_novops_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
256 : {
257 10912 : if (TREE_CODE (*node) != FUNCTION_DECL)
258 : {
259 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
260 0 : *no_add_attrs = true;
261 : }
262 :
263 10912 : DECL_IS_NOVOPS (*node) = 1;
264 10912 : return NULL_TREE;
265 : }
266 :
267 : /* Helper for nonnull attribute handling; fetch the operand number
268 : from the attribute argument list. */
269 :
270 : static bool
271 57288 : get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
272 : {
273 : /* Verify the arg number is a constant. */
274 57288 : if (!tree_fits_uhwi_p (arg_num_expr))
275 : return false;
276 :
277 57288 : *valp = TREE_INT_CST_LOW (arg_num_expr);
278 57288 : return true;
279 : }
280 :
281 : /* Handle the "nonnull" attribute. */
282 :
283 : static tree
284 156860 : handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
285 : {
286 156860 : tree type = *node;
287 :
288 : /* If no arguments are specified, all pointer arguments should be
289 : non-null. Verify a full prototype is given so that the arguments
290 : will have the correct types when we actually check them later.
291 : Avoid diagnosing type-generic built-ins since those have no
292 : prototype. */
293 156860 : if (!args)
294 : {
295 111848 : gcc_assert (prototype_p (type) || !TYPE_ATTRIBUTES (type)
296 : || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
297 :
298 111848 : return NULL_TREE;
299 : }
300 :
301 : /* Argument list specified. Verify that each argument number references
302 : a pointer argument. */
303 102300 : for (; args; args = TREE_CHAIN (args))
304 : {
305 57288 : tree argument;
306 57288 : unsigned HOST_WIDE_INT arg_num = 0, ck_num;
307 :
308 57288 : if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
309 0 : gcc_unreachable ();
310 :
311 57288 : argument = TYPE_ARG_TYPES (type);
312 57288 : if (argument)
313 : {
314 61380 : for (ck_num = 1;; ck_num++)
315 : {
316 118668 : if (!argument || ck_num == arg_num)
317 : break;
318 61380 : argument = TREE_CHAIN (argument);
319 : }
320 :
321 57288 : gcc_assert (argument
322 : && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
323 : }
324 : }
325 :
326 : return NULL_TREE;
327 : }
328 :
329 : /* Handle a "nothrow" attribute; arguments as in
330 : struct attribute_spec.handler. */
331 :
332 : static tree
333 1864588 : handle_nothrow_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
334 : {
335 1864588 : if (TREE_CODE (*node) != FUNCTION_DECL)
336 : {
337 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
338 0 : *no_add_attrs = true;
339 : }
340 :
341 1864588 : TREE_NOTHROW (*node) = 1;
342 1864588 : return NULL_TREE;
343 : }
344 :
345 : /* Handle a "type generic" attribute; arguments as in
346 : struct attribute_spec.handler. */
347 :
348 : static tree
349 38192 : handle_type_generic_attribute (tree *node, tree, tree, int, bool *)
350 : {
351 : /* Ensure we have a function type. */
352 38192 : gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
353 :
354 : /* Ensure we have a variadic function. */
355 38192 : gcc_assert (!prototype_p (*node) || stdarg_p (*node));
356 :
357 38192 : return NULL_TREE;
358 : }
359 :
360 : /* Handle a "transaction_pure" attribute; arguments as in
361 : struct attribute_spec.handler. */
362 :
363 : static tree
364 1364 : handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
365 : {
366 : /* Ensure we have a function type. */
367 1364 : gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
368 :
369 1364 : return NULL_TREE;
370 : }
371 :
372 : /* Handle a "returns_twice" attribute; arguments as in
373 : struct attribute_spec.handler. */
374 :
375 : static tree
376 1364 : handle_returns_twice_attribute (tree *node, tree name, tree, int,
377 : bool *no_add_attrs)
378 : {
379 1364 : if (TREE_CODE (*node) != FUNCTION_DECL)
380 : {
381 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
382 0 : *no_add_attrs = true;
383 : }
384 :
385 1364 : DECL_IS_RETURNS_TWICE (*node) = 1;
386 :
387 1364 : return NULL_TREE;
388 : }
389 :
390 : /* Handle a "fn spec" attribute; arguments as in
391 : struct attribute_spec.handler. */
392 :
393 : static tree
394 531960 : handle_fnspec_attribute (tree *, tree, tree args, int, bool *)
395 : {
396 1063920 : gcc_assert (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST
397 : && !TREE_CHAIN (args));
398 531960 : return NULL_TREE;
399 : }
400 :
401 : /* Handle an "omp declare simd" attribute; arguments as in
402 : struct attribute_spec.handler. */
403 :
404 : static tree
405 0 : handle_omp_declare_simd_attribute (tree *node, tree name, tree, int,
406 : bool *no_add_attrs)
407 : {
408 0 : if (TREE_CODE (*node) != FUNCTION_DECL)
409 : {
410 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
411 0 : *no_add_attrs = true;
412 : }
413 :
414 0 : return NULL_TREE;
415 : }
416 :
417 : /* Language specific attribute handlers.
418 : These functions take the arguments:
419 : (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
420 :
421 : /* Handle a "cold" and attribute; arguments as in
422 : struct attribute_spec.handler. */
423 :
424 : static tree
425 5456 : handle_cold_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
426 : {
427 5456 : if (TREE_CODE (*node) != FUNCTION_DECL)
428 : {
429 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
430 0 : *no_add_attrs = true;
431 : }
432 :
433 5456 : return NULL_TREE;
434 : }
435 :
436 : static tree
437 0 : handle_hot_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
438 : {
439 0 : if (TREE_CODE (*node) != FUNCTION_DECL)
440 : {
441 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
442 0 : *no_add_attrs = true;
443 : }
444 :
445 0 : return NULL_TREE;
446 : }
|