]>
Commit | Line | Data |
---|---|---|
bb9f8221 | 1 | /* Functions dealing with attribute handling, used by most front ends. |
99dee823 | 2 | Copyright (C) 1992-2021 Free Software Foundation, Inc. |
bb9f8221 RK |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
bb9f8221 RK |
9 | version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
bb9f8221 | 19 | |
6450f073 | 20 | #define INCLUDE_STRING |
bb9f8221 RK |
21 | #include "config.h" |
22 | #include "system.h" | |
4977bab6 | 23 | #include "coretypes.h" |
957060b5 | 24 | #include "target.h" |
bb9f8221 | 25 | #include "tree.h" |
d8a2d370 | 26 | #include "stringpool.h" |
957060b5 | 27 | #include "diagnostic-core.h" |
d8a2d370 | 28 | #include "attribs.h" |
6450f073 | 29 | #include "fold-const.h" |
d8a2d370 | 30 | #include "stor-layout.h" |
7ffb4fd2 | 31 | #include "langhooks.h" |
d1c8e08a | 32 | #include "plugin.h" |
5d9ae53d MS |
33 | #include "selftest.h" |
34 | #include "hash-set.h" | |
79a2c428 MS |
35 | #include "diagnostic.h" |
36 | #include "pretty-print.h" | |
6450f073 | 37 | #include "tree-pretty-print.h" |
79a2c428 | 38 | #include "intl.h" |
bb9f8221 | 39 | |
349ae713 | 40 | /* Table of the tables of attributes (common, language, format, machine) |
bb9f8221 RK |
41 | searched. */ |
42 | static const struct attribute_spec *attribute_tables[4]; | |
43 | ||
23b43207 JH |
44 | /* Substring representation. */ |
45 | ||
46 | struct substring | |
47 | { | |
48 | const char *str; | |
49 | int length; | |
50 | }; | |
51 | ||
4a8fb1a1 LC |
52 | /* Simple hash function to avoid need to scan whole string. */ |
53 | ||
54 | static inline hashval_t | |
55 | substring_hash (const char *str, int l) | |
56 | { | |
57 | return str[0] + str[l - 1] * 256 + l * 65536; | |
58 | } | |
59 | ||
60 | /* Used for attribute_hash. */ | |
61 | ||
8d67ee55 | 62 | struct attribute_hasher : nofree_ptr_hash <attribute_spec> |
4a8fb1a1 | 63 | { |
67f58944 TS |
64 | typedef substring *compare_type; |
65 | static inline hashval_t hash (const attribute_spec *); | |
66 | static inline bool equal (const attribute_spec *, const substring *); | |
4a8fb1a1 LC |
67 | }; |
68 | ||
69 | inline hashval_t | |
67f58944 | 70 | attribute_hasher::hash (const attribute_spec *spec) |
4a8fb1a1 LC |
71 | { |
72 | const int l = strlen (spec->name); | |
73 | return substring_hash (spec->name, l); | |
74 | } | |
75 | ||
76 | inline bool | |
67f58944 | 77 | attribute_hasher::equal (const attribute_spec *spec, const substring *str) |
4a8fb1a1 LC |
78 | { |
79 | return (strncmp (spec->name, str->str, str->length) == 0 | |
80 | && !spec->name[str->length]); | |
81 | } | |
82 | ||
e28d52cf DS |
83 | /* Scoped attribute name representation. */ |
84 | ||
85 | struct scoped_attributes | |
86 | { | |
87 | const char *ns; | |
9771b263 | 88 | vec<attribute_spec> attributes; |
c203e8a7 | 89 | hash_table<attribute_hasher> *attribute_hash; |
e28d52cf DS |
90 | }; |
91 | ||
e28d52cf | 92 | /* The table of scope attributes. */ |
9771b263 | 93 | static vec<scoped_attributes> attributes_table; |
e28d52cf DS |
94 | |
95 | static scoped_attributes* find_attribute_namespace (const char*); | |
96 | static void register_scoped_attribute (const struct attribute_spec *, | |
97 | scoped_attributes *); | |
98 | ||
bb9f8221 RK |
99 | static bool attributes_initialized = false; |
100 | ||
bb9f8221 | 101 | /* Default empty table of attributes. */ |
23b43207 | 102 | |
bb9f8221 RK |
103 | static const struct attribute_spec empty_attribute_table[] = |
104 | { | |
4849deb1 | 105 | { NULL, 0, 0, false, false, false, false, NULL, NULL } |
bb9f8221 RK |
106 | }; |
107 | ||
23b43207 JH |
108 | /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'. |
109 | To avoid need for copying, we simply return length of the string. */ | |
110 | ||
111 | static void | |
112 | extract_attribute_substring (struct substring *str) | |
113 | { | |
114 | if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_' | |
115 | && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_') | |
116 | { | |
117 | str->length -= 4; | |
118 | str->str += 2; | |
119 | } | |
120 | } | |
121 | ||
e28d52cf DS |
122 | /* Insert an array of attributes ATTRIBUTES into a namespace. This |
123 | array must be NULL terminated. NS is the name of attribute | |
124 | namespace. The function returns the namespace into which the | |
125 | attributes have been registered. */ | |
126 | ||
4849deb1 JJ |
127 | scoped_attributes * |
128 | register_scoped_attributes (const struct attribute_spec *attributes, | |
129 | const char *ns) | |
e28d52cf DS |
130 | { |
131 | scoped_attributes *result = NULL; | |
132 | ||
133 | /* See if we already have attributes in the namespace NS. */ | |
134 | result = find_attribute_namespace (ns); | |
135 | ||
136 | if (result == NULL) | |
137 | { | |
138 | /* We don't have any namespace NS yet. Create one. */ | |
139 | scoped_attributes sa; | |
140 | ||
d067e05f | 141 | if (attributes_table.is_empty ()) |
9771b263 | 142 | attributes_table.create (64); |
e28d52cf DS |
143 | |
144 | memset (&sa, 0, sizeof (sa)); | |
145 | sa.ns = ns; | |
9771b263 DN |
146 | sa.attributes.create (64); |
147 | result = attributes_table.safe_push (sa); | |
c203e8a7 | 148 | result->attribute_hash = new hash_table<attribute_hasher> (200); |
e28d52cf DS |
149 | } |
150 | ||
151 | /* Really add the attributes to their namespace now. */ | |
152 | for (unsigned i = 0; attributes[i].name != NULL; ++i) | |
153 | { | |
9771b263 | 154 | result->attributes.safe_push (attributes[i]); |
e28d52cf DS |
155 | register_scoped_attribute (&attributes[i], result); |
156 | } | |
157 | ||
158 | gcc_assert (result != NULL); | |
159 | ||
160 | return result; | |
161 | } | |
162 | ||
163 | /* Return the namespace which name is NS, NULL if none exist. */ | |
164 | ||
165 | static scoped_attributes* | |
166 | find_attribute_namespace (const char* ns) | |
167 | { | |
3f207ab3 TS |
168 | for (scoped_attributes &iter : attributes_table) |
169 | if (ns == iter.ns | |
170 | || (iter.ns != NULL | |
e28d52cf | 171 | && ns != NULL |
3f207ab3 TS |
172 | && !strcmp (iter.ns, ns))) |
173 | return &iter; | |
e28d52cf DS |
174 | return NULL; |
175 | } | |
176 | ||
b2b29377 MM |
177 | /* Make some sanity checks on the attribute tables. */ |
178 | ||
179 | static void | |
180 | check_attribute_tables (void) | |
181 | { | |
182 | for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
183 | for (size_t j = 0; attribute_tables[i][j].name != NULL; j++) | |
184 | { | |
185 | /* The name must not begin and end with __. */ | |
186 | const char *name = attribute_tables[i][j].name; | |
187 | int len = strlen (name); | |
188 | ||
189 | gcc_assert (!(name[0] == '_' && name[1] == '_' | |
190 | && name[len - 1] == '_' && name[len - 2] == '_')); | |
191 | ||
192 | /* The minimum and maximum lengths must be consistent. */ | |
193 | gcc_assert (attribute_tables[i][j].min_length >= 0); | |
194 | ||
195 | gcc_assert (attribute_tables[i][j].max_length == -1 | |
196 | || (attribute_tables[i][j].max_length | |
197 | >= attribute_tables[i][j].min_length)); | |
198 | ||
199 | /* An attribute cannot require both a DECL and a TYPE. */ | |
200 | gcc_assert (!attribute_tables[i][j].decl_required | |
201 | || !attribute_tables[i][j].type_required); | |
202 | ||
203 | /* If an attribute requires a function type, in particular | |
204 | it requires a type. */ | |
205 | gcc_assert (!attribute_tables[i][j].function_type_required | |
206 | || attribute_tables[i][j].type_required); | |
207 | } | |
208 | ||
209 | /* Check that each name occurs just once in each table. */ | |
210 | for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
211 | for (size_t j = 0; attribute_tables[i][j].name != NULL; j++) | |
212 | for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++) | |
213 | gcc_assert (strcmp (attribute_tables[i][j].name, | |
214 | attribute_tables[i][k].name)); | |
215 | ||
216 | /* Check that no name occurs in more than one table. Names that | |
217 | begin with '*' are exempt, and may be overridden. */ | |
218 | for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
219 | for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++) | |
220 | for (size_t k = 0; attribute_tables[i][k].name != NULL; k++) | |
221 | for (size_t l = 0; attribute_tables[j][l].name != NULL; l++) | |
222 | gcc_assert (attribute_tables[i][k].name[0] == '*' | |
223 | || strcmp (attribute_tables[i][k].name, | |
224 | attribute_tables[j][l].name)); | |
225 | } | |
226 | ||
227 | /* Initialize attribute tables, and make some sanity checks if checking is | |
228 | enabled. */ | |
bb9f8221 | 229 | |
8dd00781 | 230 | void |
4682ae04 | 231 | init_attributes (void) |
bb9f8221 | 232 | { |
ca7558fc | 233 | size_t i; |
bb9f8221 | 234 | |
8dd00781 JJ |
235 | if (attributes_initialized) |
236 | return; | |
237 | ||
349ae713 NB |
238 | attribute_tables[0] = lang_hooks.common_attribute_table; |
239 | attribute_tables[1] = lang_hooks.attribute_table; | |
240 | attribute_tables[2] = lang_hooks.format_attribute_table; | |
bb9f8221 RK |
241 | attribute_tables[3] = targetm.attribute_table; |
242 | ||
349ae713 NB |
243 | /* Translate NULL pointers to pointers to the empty table. */ |
244 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
245 | if (attribute_tables[i] == NULL) | |
246 | attribute_tables[i] = empty_attribute_table; | |
247 | ||
b2b29377 MM |
248 | if (flag_checking) |
249 | check_attribute_tables (); | |
bb9f8221 | 250 | |
e28d52cf DS |
251 | for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i) |
252 | /* Put all the GNU attributes into the "gnu" namespace. */ | |
253 | register_scoped_attributes (attribute_tables[i], "gnu"); | |
254 | ||
d1c8e08a TG |
255 | invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL); |
256 | attributes_initialized = true; | |
257 | } | |
258 | ||
259 | /* Insert a single ATTR into the attribute table. */ | |
260 | ||
261 | void | |
b8698a0f | 262 | register_attribute (const struct attribute_spec *attr) |
e28d52cf DS |
263 | { |
264 | register_scoped_attribute (attr, find_attribute_namespace ("gnu")); | |
265 | } | |
266 | ||
267 | /* Insert a single attribute ATTR into a namespace of attributes. */ | |
268 | ||
269 | static void | |
270 | register_scoped_attribute (const struct attribute_spec *attr, | |
271 | scoped_attributes *name_space) | |
d1c8e08a | 272 | { |
16f7ad42 | 273 | struct substring str; |
4a8fb1a1 | 274 | attribute_spec **slot; |
16f7ad42 | 275 | |
e28d52cf DS |
276 | gcc_assert (attr != NULL && name_space != NULL); |
277 | ||
c203e8a7 | 278 | gcc_assert (name_space->attribute_hash); |
e28d52cf | 279 | |
16f7ad42 TG |
280 | str.str = attr->name; |
281 | str.length = strlen (str.str); | |
70e41a6a NP |
282 | |
283 | /* Attribute names in the table must be in the form 'text' and not | |
284 | in the form '__text__'. */ | |
285 | gcc_assert (str.length > 0 && str.str[0] != '_'); | |
286 | ||
4a8fb1a1 | 287 | slot = name_space->attribute_hash |
c203e8a7 TS |
288 | ->find_slot_with_hash (&str, substring_hash (str.str, str.length), |
289 | INSERT); | |
0a35513e | 290 | gcc_assert (!*slot || attr->name[0] == '*'); |
4a8fb1a1 | 291 | *slot = CONST_CAST (struct attribute_spec *, attr); |
bb9f8221 | 292 | } |
a7f6bc8c | 293 | |
e28d52cf DS |
294 | /* Return the spec for the scoped attribute with namespace NS and |
295 | name NAME. */ | |
a7f6bc8c | 296 | |
862d0b35 | 297 | static const struct attribute_spec * |
e28d52cf | 298 | lookup_scoped_attribute_spec (const_tree ns, const_tree name) |
a7f6bc8c JM |
299 | { |
300 | struct substring attr; | |
e28d52cf DS |
301 | scoped_attributes *attrs; |
302 | ||
303 | const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL; | |
304 | ||
305 | attrs = find_attribute_namespace (ns_str); | |
306 | ||
307 | if (attrs == NULL) | |
308 | return NULL; | |
a7f6bc8c JM |
309 | |
310 | attr.str = IDENTIFIER_POINTER (name); | |
311 | attr.length = IDENTIFIER_LENGTH (name); | |
312 | extract_attribute_substring (&attr); | |
c203e8a7 TS |
313 | return attrs->attribute_hash->find_with_hash (&attr, |
314 | substring_hash (attr.str, | |
315 | attr.length)); | |
a7f6bc8c | 316 | } |
e28d52cf | 317 | |
7dbb85a7 JM |
318 | /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST, |
319 | it also specifies the attribute namespace. */ | |
e28d52cf DS |
320 | |
321 | const struct attribute_spec * | |
322 | lookup_attribute_spec (const_tree name) | |
323 | { | |
7dbb85a7 JM |
324 | tree ns; |
325 | if (TREE_CODE (name) == TREE_LIST) | |
326 | { | |
327 | ns = TREE_PURPOSE (name); | |
328 | name = TREE_VALUE (name); | |
329 | } | |
330 | else | |
331 | ns = get_identifier ("gnu"); | |
332 | return lookup_scoped_attribute_spec (ns, name); | |
e28d52cf DS |
333 | } |
334 | ||
862d0b35 DN |
335 | |
336 | /* Return the namespace of the attribute ATTR. This accessor works on | |
337 | GNU and C++11 (scoped) attributes. On GNU attributes, | |
338 | it returns an identifier tree for the string "gnu". | |
339 | ||
340 | Please read the comments of cxx11_attribute_p to understand the | |
341 | format of attributes. */ | |
342 | ||
1bf32c11 | 343 | tree |
862d0b35 DN |
344 | get_attribute_namespace (const_tree attr) |
345 | { | |
346 | if (cxx11_attribute_p (attr)) | |
347 | return TREE_PURPOSE (TREE_PURPOSE (attr)); | |
348 | return get_identifier ("gnu"); | |
349 | } | |
350 | ||
5d9ae53d MS |
351 | /* Check LAST_DECL and NODE of the same symbol for attributes that are |
352 | recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose | |
353 | them, and return true if any have been found. NODE can be a DECL | |
354 | or a TYPE. */ | |
355 | ||
356 | static bool | |
357 | diag_attr_exclusions (tree last_decl, tree node, tree attrname, | |
358 | const attribute_spec *spec) | |
359 | { | |
360 | const attribute_spec::exclusions *excl = spec->exclude; | |
361 | ||
362 | tree_code code = TREE_CODE (node); | |
363 | ||
364 | if ((code == FUNCTION_DECL && !excl->function | |
365 | && (!excl->type || !spec->affects_type_identity)) | |
366 | || (code == VAR_DECL && !excl->variable | |
367 | && (!excl->type || !spec->affects_type_identity)) | |
368 | || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type))) | |
369 | return false; | |
370 | ||
371 | /* True if an attribute that's mutually exclusive with ATTRNAME | |
372 | has been found. */ | |
373 | bool found = false; | |
374 | ||
375 | if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node) | |
376 | { | |
377 | /* Check both the last DECL and its type for conflicts with | |
378 | the attribute being added to the current decl or type. */ | |
379 | found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec); | |
380 | tree decl_type = TREE_TYPE (last_decl); | |
381 | found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec); | |
382 | } | |
383 | ||
384 | /* NODE is either the current DECL to which the attribute is being | |
385 | applied or its TYPE. For the former, consider the attributes on | |
386 | both the DECL and its type. */ | |
387 | tree attrs[2]; | |
388 | ||
389 | if (DECL_P (node)) | |
390 | { | |
391 | attrs[0] = DECL_ATTRIBUTES (node); | |
392 | attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node)); | |
393 | } | |
394 | else | |
395 | { | |
396 | attrs[0] = TYPE_ATTRIBUTES (node); | |
397 | attrs[1] = NULL_TREE; | |
398 | } | |
399 | ||
400 | /* Iterate over the mutually exclusive attribute names and verify | |
401 | that the symbol doesn't contain it. */ | |
402 | for (unsigned i = 0; i != sizeof attrs / sizeof *attrs; ++i) | |
403 | { | |
404 | if (!attrs[i]) | |
405 | continue; | |
406 | ||
407 | for ( ; excl->name; ++excl) | |
408 | { | |
409 | /* Avoid checking the attribute against itself. */ | |
410 | if (is_attribute_p (excl->name, attrname)) | |
411 | continue; | |
412 | ||
413 | if (!lookup_attribute (excl->name, attrs[i])) | |
414 | continue; | |
415 | ||
fba303ed MS |
416 | /* An exclusion may apply either to a function declaration, |
417 | type declaration, or a field/variable declaration, or | |
418 | any subset of the three. */ | |
419 | if (TREE_CODE (node) == FUNCTION_DECL | |
420 | && !excl->function) | |
421 | continue; | |
422 | ||
423 | if (TREE_CODE (node) == TYPE_DECL | |
424 | && !excl->type) | |
425 | continue; | |
426 | ||
427 | if ((TREE_CODE (node) == FIELD_DECL | |
428 | || TREE_CODE (node) == VAR_DECL) | |
429 | && !excl->variable) | |
430 | continue; | |
431 | ||
5d9ae53d MS |
432 | found = true; |
433 | ||
434 | /* Print a note? */ | |
435 | bool note = last_decl != NULL_TREE; | |
097f82ec | 436 | auto_diagnostic_group d; |
5d9ae53d | 437 | if (TREE_CODE (node) == FUNCTION_DECL |
3d78e008 | 438 | && fndecl_built_in_p (node)) |
5d9ae53d MS |
439 | note &= warning (OPT_Wattributes, |
440 | "ignoring attribute %qE in declaration of " | |
441 | "a built-in function %qD because it conflicts " | |
442 | "with attribute %qs", | |
443 | attrname, node, excl->name); | |
444 | else | |
445 | note &= warning (OPT_Wattributes, | |
446 | "ignoring attribute %qE because " | |
447 | "it conflicts with attribute %qs", | |
448 | attrname, excl->name); | |
449 | ||
450 | if (note) | |
451 | inform (DECL_SOURCE_LOCATION (last_decl), | |
452 | "previous declaration here"); | |
453 | } | |
454 | } | |
455 | ||
456 | return found; | |
457 | } | |
862d0b35 | 458 | |
bb9f8221 RK |
459 | /* Process the attributes listed in ATTRIBUTES and install them in *NODE, |
460 | which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, | |
461 | it should be modified in place; if a TYPE, a copy should be created | |
462 | unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further | |
463 | information, in the form of a bitwise OR of flags in enum attribute_flags | |
464 | from tree.h. Depending on these flags, some attributes may be | |
465 | returned to be applied at a later stage (for example, to apply | |
03aa99d4 | 466 | a decl attribute to the declaration rather than to its type). */ |
bb9f8221 RK |
467 | |
468 | tree | |
5d9ae53d MS |
469 | decl_attributes (tree *node, tree attributes, int flags, |
470 | tree last_decl /* = NULL_TREE */) | |
bb9f8221 | 471 | { |
bb9f8221 RK |
472 | tree returned_attrs = NULL_TREE; |
473 | ||
e28d52cf | 474 | if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node) |
21516d64 VR |
475 | return NULL_TREE; |
476 | ||
bb9f8221 RK |
477 | if (!attributes_initialized) |
478 | init_attributes (); | |
479 | ||
ab442df7 MM |
480 | /* If this is a function and the user used #pragma GCC optimize, add the |
481 | options to the attribute((optimize(...))) list. */ | |
482 | if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma) | |
483 | { | |
484 | tree cur_attr = lookup_attribute ("optimize", attributes); | |
485 | tree opts = copy_list (current_optimize_pragma); | |
486 | ||
487 | if (! cur_attr) | |
488 | attributes | |
489 | = tree_cons (get_identifier ("optimize"), opts, attributes); | |
490 | else | |
491 | TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr)); | |
492 | } | |
493 | ||
494 | if (TREE_CODE (*node) == FUNCTION_DECL | |
495 | && optimization_current_node != optimization_default_node | |
496 | && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)) | |
497 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node; | |
498 | ||
5779e713 MM |
499 | /* If this is a function and the user used #pragma GCC target, add the |
500 | options to the attribute((target(...))) list. */ | |
ab442df7 | 501 | if (TREE_CODE (*node) == FUNCTION_DECL |
5779e713 | 502 | && current_target_pragma |
ab442df7 | 503 | && targetm.target_option.valid_attribute_p (*node, NULL_TREE, |
5779e713 | 504 | current_target_pragma, 0)) |
ab442df7 | 505 | { |
5779e713 MM |
506 | tree cur_attr = lookup_attribute ("target", attributes); |
507 | tree opts = copy_list (current_target_pragma); | |
ab442df7 MM |
508 | |
509 | if (! cur_attr) | |
5779e713 | 510 | attributes = tree_cons (get_identifier ("target"), opts, attributes); |
ab442df7 MM |
511 | else |
512 | TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr)); | |
513 | } | |
514 | ||
61044492 JZ |
515 | /* A "naked" function attribute implies "noinline" and "noclone" for |
516 | those targets that support it. */ | |
517 | if (TREE_CODE (*node) == FUNCTION_DECL | |
70e41a6a | 518 | && attributes |
036ea399 JJ |
519 | && lookup_attribute ("naked", attributes) != NULL |
520 | && lookup_attribute_spec (get_identifier ("naked"))) | |
61044492 JZ |
521 | { |
522 | if (lookup_attribute ("noinline", attributes) == NULL) | |
523 | attributes = tree_cons (get_identifier ("noinline"), NULL, attributes); | |
524 | ||
525 | if (lookup_attribute ("noclone", attributes) == NULL) | |
526 | attributes = tree_cons (get_identifier ("noclone"), NULL, attributes); | |
527 | } | |
528 | ||
036ea399 JJ |
529 | /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf" |
530 | for those targets that support it. */ | |
531 | if (TREE_CODE (*node) == FUNCTION_DECL | |
532 | && attributes | |
533 | && lookup_attribute ("noipa", attributes) != NULL | |
534 | && lookup_attribute_spec (get_identifier ("noipa"))) | |
535 | { | |
536 | if (lookup_attribute ("noinline", attributes) == NULL) | |
537 | attributes = tree_cons (get_identifier ("noinline"), NULL, attributes); | |
538 | ||
539 | if (lookup_attribute ("noclone", attributes) == NULL) | |
540 | attributes = tree_cons (get_identifier ("noclone"), NULL, attributes); | |
541 | ||
542 | if (lookup_attribute ("no_icf", attributes) == NULL) | |
543 | attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes); | |
544 | } | |
545 | ||
5fd9b178 | 546 | targetm.insert_attributes (*node, &attributes); |
bb9f8221 | 547 | |
5d9ae53d MS |
548 | /* Note that attributes on the same declaration are not necessarily |
549 | in the same order as in the source. */ | |
1bf32c11 | 550 | for (tree attr = attributes; attr; attr = TREE_CHAIN (attr)) |
bb9f8221 | 551 | { |
1bf32c11 MP |
552 | tree ns = get_attribute_namespace (attr); |
553 | tree name = get_attribute_name (attr); | |
554 | tree args = TREE_VALUE (attr); | |
bb9f8221 | 555 | tree *anode = node; |
4849deb1 JJ |
556 | const struct attribute_spec *spec |
557 | = lookup_scoped_attribute_spec (ns, name); | |
f9ceed32 | 558 | int fn_ptr_quals = 0; |
3acef2ae | 559 | tree fn_ptr_tmp = NULL_TREE; |
1bf32c11 | 560 | const bool cxx11_attr_p = cxx11_attribute_p (attr); |
bb9f8221 RK |
561 | |
562 | if (spec == NULL) | |
563 | { | |
e384e6b5 | 564 | if (!(flags & (int) ATTR_FLAG_BUILT_IN)) |
e28d52cf | 565 | { |
1bf32c11 | 566 | if (ns == NULL_TREE || !cxx11_attr_p) |
e28d52cf DS |
567 | warning (OPT_Wattributes, "%qE attribute directive ignored", |
568 | name); | |
569 | else | |
570 | warning (OPT_Wattributes, | |
571 | "%<%E::%E%> scoped attribute directive ignored", | |
572 | ns, name); | |
573 | } | |
bb9f8221 RK |
574 | continue; |
575 | } | |
54aa6b58 | 576 | else |
bb9f8221 | 577 | { |
54aa6b58 MS |
578 | int nargs = list_length (args); |
579 | if (nargs < spec->min_length | |
580 | || (spec->max_length >= 0 | |
581 | && nargs > spec->max_length)) | |
582 | { | |
583 | error ("wrong number of arguments specified for %qE attribute", | |
584 | name); | |
585 | if (spec->max_length < 0) | |
586 | inform (input_location, "expected %i or more, found %i", | |
587 | spec->min_length, nargs); | |
588 | else | |
589 | inform (input_location, "expected between %i and %i, found %i", | |
590 | spec->min_length, spec->max_length, nargs); | |
591 | continue; | |
592 | } | |
bb9f8221 | 593 | } |
23b43207 | 594 | gcc_assert (is_attribute_p (spec->name, name)); |
bb9f8221 RK |
595 | |
596 | if (spec->decl_required && !DECL_P (*anode)) | |
597 | { | |
598 | if (flags & ((int) ATTR_FLAG_DECL_NEXT | |
599 | | (int) ATTR_FLAG_FUNCTION_NEXT | |
600 | | (int) ATTR_FLAG_ARRAY_NEXT)) | |
601 | { | |
602 | /* Pass on this attribute to be tried again. */ | |
5d9ae53d MS |
603 | tree attr = tree_cons (name, args, NULL_TREE); |
604 | returned_attrs = chainon (returned_attrs, attr); | |
bb9f8221 RK |
605 | continue; |
606 | } | |
607 | else | |
608 | { | |
4f1e4960 JM |
609 | warning (OPT_Wattributes, "%qE attribute does not apply to types", |
610 | name); | |
bb9f8221 RK |
611 | continue; |
612 | } | |
613 | } | |
614 | ||
dd4dc3cd RK |
615 | /* If we require a type, but were passed a decl, set up to make a |
616 | new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE | |
617 | would have applied if we'd been passed a type, but we cannot modify | |
618 | the decl's type in place here. */ | |
bb9f8221 | 619 | if (spec->type_required && DECL_P (*anode)) |
dd4dc3cd RK |
620 | { |
621 | anode = &TREE_TYPE (*anode); | |
26c87b1a | 622 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; |
dd4dc3cd | 623 | } |
bb9f8221 RK |
624 | |
625 | if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE | |
626 | && TREE_CODE (*anode) != METHOD_TYPE) | |
627 | { | |
628 | if (TREE_CODE (*anode) == POINTER_TYPE | |
629 | && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE | |
630 | || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE)) | |
631 | { | |
3acef2ae JM |
632 | /* OK, this is a bit convoluted. We can't just make a copy |
633 | of the pointer type and modify its TREE_TYPE, because if | |
634 | we change the attributes of the target type the pointer | |
635 | type needs to have a different TYPE_MAIN_VARIANT. So we | |
636 | pull out the target type now, frob it as appropriate, and | |
637 | rebuild the pointer type later. | |
638 | ||
c22cacf3 MS |
639 | This would all be simpler if attributes were part of the |
640 | declarator, grumble grumble. */ | |
3acef2ae | 641 | fn_ptr_tmp = TREE_TYPE (*anode); |
f9ceed32 | 642 | fn_ptr_quals = TYPE_QUALS (*anode); |
3acef2ae JM |
643 | anode = &fn_ptr_tmp; |
644 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; | |
bb9f8221 RK |
645 | } |
646 | else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
647 | { | |
648 | /* Pass on this attribute to be tried again. */ | |
5d9ae53d MS |
649 | tree attr = tree_cons (name, args, NULL_TREE); |
650 | returned_attrs = chainon (returned_attrs, attr); | |
bb9f8221 RK |
651 | continue; |
652 | } | |
653 | ||
654 | if (TREE_CODE (*anode) != FUNCTION_TYPE | |
655 | && TREE_CODE (*anode) != METHOD_TYPE) | |
656 | { | |
5c498b10 | 657 | warning (OPT_Wattributes, |
4f1e4960 JM |
658 | "%qE attribute only applies to function types", |
659 | name); | |
bb9f8221 RK |
660 | continue; |
661 | } | |
662 | } | |
663 | ||
b9e75696 JM |
664 | if (TYPE_P (*anode) |
665 | && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) | |
666 | && TYPE_SIZE (*anode) != NULL_TREE) | |
667 | { | |
668 | warning (OPT_Wattributes, "type attributes ignored after type is already defined"); | |
669 | continue; | |
670 | } | |
671 | ||
5d9ae53d MS |
672 | bool no_add_attrs = false; |
673 | ||
bffc6270 MS |
674 | /* Check for exclusions with other attributes on the current |
675 | declation as well as the last declaration of the same | |
676 | symbol already processed (if one exists). Detect and | |
677 | reject incompatible attributes. */ | |
678 | bool built_in = flags & ATTR_FLAG_BUILT_IN; | |
679 | if (spec->exclude | |
e2a71816 JJ |
680 | && (flag_checking || !built_in) |
681 | && !error_operand_p (last_decl)) | |
bffc6270 MS |
682 | { |
683 | /* Always check attributes on user-defined functions. | |
684 | Check them on built-ins only when -fchecking is set. | |
685 | Ignore __builtin_unreachable -- it's both const and | |
686 | noreturn. */ | |
687 | ||
688 | if (!built_in | |
689 | || !DECL_P (*anode) | |
cb1180d5 | 690 | || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL |
bffc6270 MS |
691 | || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE |
692 | && (DECL_FUNCTION_CODE (*anode) | |
693 | != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE))) | |
694 | { | |
695 | bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec); | |
696 | if (!no_add && anode != node) | |
697 | no_add = diag_attr_exclusions (last_decl, *node, name, spec); | |
698 | no_add_attrs |= no_add; | |
699 | } | |
700 | } | |
701 | ||
702 | if (no_add_attrs) | |
703 | continue; | |
704 | ||
bb9f8221 | 705 | if (spec->handler != NULL) |
e28d52cf | 706 | { |
1bf32c11 | 707 | int cxx11_flag = (cxx11_attr_p ? ATTR_FLAG_CXX11 : 0); |
e28d52cf | 708 | |
5d9ae53d | 709 | /* Pass in an array of the current declaration followed |
6450f073 MS |
710 | by the last pushed/merged declaration if one exists. |
711 | For calls that modify the type attributes of a DECL | |
712 | and for which *ANODE is *NODE's type, also pass in | |
713 | the DECL as the third element to use in diagnostics. | |
5d9ae53d MS |
714 | If the handler changes CUR_AND_LAST_DECL[0] replace |
715 | *ANODE with its value. */ | |
6450f073 MS |
716 | tree cur_and_last_decl[3] = { *anode, last_decl }; |
717 | if (anode != node && DECL_P (*node)) | |
718 | cur_and_last_decl[2] = *node; | |
719 | ||
5d9ae53d MS |
720 | tree ret = (spec->handler) (cur_and_last_decl, name, args, |
721 | flags|cxx11_flag, &no_add_attrs); | |
722 | ||
723 | *anode = cur_and_last_decl[0]; | |
724 | if (ret == error_mark_node) | |
725 | { | |
726 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
727 | no_add_attrs = true; | |
728 | } | |
729 | else | |
730 | returned_attrs = chainon (ret, returned_attrs); | |
731 | } | |
732 | ||
1b9191d2 AH |
733 | /* Layout the decl in case anything changed. */ |
734 | if (spec->type_required && DECL_P (*node) | |
8813a647 | 735 | && (VAR_P (*node) |
67282790 JM |
736 | || TREE_CODE (*node) == PARM_DECL |
737 | || TREE_CODE (*node) == RESULT_DECL)) | |
d1838621 | 738 | relayout_decl (*node); |
1b9191d2 | 739 | |
bb9f8221 RK |
740 | if (!no_add_attrs) |
741 | { | |
742 | tree old_attrs; | |
743 | tree a; | |
744 | ||
745 | if (DECL_P (*anode)) | |
746 | old_attrs = DECL_ATTRIBUTES (*anode); | |
747 | else | |
748 | old_attrs = TYPE_ATTRIBUTES (*anode); | |
749 | ||
750 | for (a = lookup_attribute (spec->name, old_attrs); | |
751 | a != NULL_TREE; | |
752 | a = lookup_attribute (spec->name, TREE_CHAIN (a))) | |
753 | { | |
754 | if (simple_cst_equal (TREE_VALUE (a), args) == 1) | |
755 | break; | |
756 | } | |
757 | ||
758 | if (a == NULL_TREE) | |
759 | { | |
760 | /* This attribute isn't already in the list. */ | |
1bf32c11 MP |
761 | tree r; |
762 | /* Preserve the C++11 form. */ | |
763 | if (cxx11_attr_p) | |
764 | r = tree_cons (build_tree_list (ns, name), args, old_attrs); | |
765 | else | |
766 | r = tree_cons (name, args, old_attrs); | |
767 | ||
bb9f8221 | 768 | if (DECL_P (*anode)) |
1bf32c11 | 769 | DECL_ATTRIBUTES (*anode) = r; |
bb9f8221 | 770 | else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) |
67214984 | 771 | { |
1bf32c11 | 772 | TYPE_ATTRIBUTES (*anode) = r; |
67214984 JM |
773 | /* If this is the main variant, also push the attributes |
774 | out to the other variants. */ | |
775 | if (*anode == TYPE_MAIN_VARIANT (*anode)) | |
776 | { | |
1bf32c11 | 777 | for (tree variant = *anode; variant; |
67214984 JM |
778 | variant = TYPE_NEXT_VARIANT (variant)) |
779 | { | |
780 | if (TYPE_ATTRIBUTES (variant) == old_attrs) | |
781 | TYPE_ATTRIBUTES (variant) | |
782 | = TYPE_ATTRIBUTES (*anode); | |
783 | else if (!lookup_attribute | |
784 | (spec->name, TYPE_ATTRIBUTES (variant))) | |
785 | TYPE_ATTRIBUTES (variant) = tree_cons | |
786 | (name, args, TYPE_ATTRIBUTES (variant)); | |
787 | } | |
788 | } | |
789 | } | |
bb9f8221 | 790 | else |
1bf32c11 | 791 | *anode = build_type_attribute_variant (*anode, r); |
bb9f8221 RK |
792 | } |
793 | } | |
3acef2ae JM |
794 | |
795 | if (fn_ptr_tmp) | |
796 | { | |
797 | /* Rebuild the function pointer type and put it in the | |
798 | appropriate place. */ | |
799 | fn_ptr_tmp = build_pointer_type (fn_ptr_tmp); | |
f9ceed32 MM |
800 | if (fn_ptr_quals) |
801 | fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals); | |
3acef2ae JM |
802 | if (DECL_P (*node)) |
803 | TREE_TYPE (*node) = fn_ptr_tmp; | |
3acef2ae | 804 | else |
298e6adc NS |
805 | { |
806 | gcc_assert (TREE_CODE (*node) == POINTER_TYPE); | |
807 | *node = fn_ptr_tmp; | |
808 | } | |
3acef2ae | 809 | } |
bb9f8221 RK |
810 | } |
811 | ||
812 | return returned_attrs; | |
813 | } | |
0a35513e | 814 | |
e28d52cf DS |
815 | /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11 |
816 | attribute. | |
817 | ||
818 | When G++ parses a C++11 attribute, it is represented as | |
819 | a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE | |
820 | (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the | |
821 | TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please | |
822 | use get_attribute_namespace and get_attribute_name to retrieve the | |
823 | namespace and name of the attribute, as these accessors work with | |
824 | GNU attributes as well. */ | |
825 | ||
826 | bool | |
827 | cxx11_attribute_p (const_tree attr) | |
828 | { | |
829 | if (attr == NULL_TREE | |
830 | || TREE_CODE (attr) != TREE_LIST) | |
831 | return false; | |
832 | ||
833 | return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST); | |
834 | } | |
835 | ||
836 | /* Return the name of the attribute ATTR. This accessor works on GNU | |
837 | and C++11 (scoped) attributes. | |
838 | ||
839 | Please read the comments of cxx11_attribute_p to understand the | |
840 | format of attributes. */ | |
841 | ||
842 | tree | |
843 | get_attribute_name (const_tree attr) | |
844 | { | |
845 | if (cxx11_attribute_p (attr)) | |
846 | return TREE_VALUE (TREE_PURPOSE (attr)); | |
847 | return TREE_PURPOSE (attr); | |
848 | } | |
849 | ||
0a35513e AH |
850 | /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR |
851 | to the method FNDECL. */ | |
852 | ||
853 | void | |
854 | apply_tm_attr (tree fndecl, tree attr) | |
855 | { | |
856 | decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0); | |
857 | } | |
3b1661a9 ES |
858 | |
859 | /* Makes a function attribute of the form NAME(ARG_NAME) and chains | |
860 | it to CHAIN. */ | |
861 | ||
862 | tree | |
863 | make_attribute (const char *name, const char *arg_name, tree chain) | |
864 | { | |
865 | tree attr_name; | |
866 | tree attr_arg_name; | |
867 | tree attr_args; | |
868 | tree attr; | |
869 | ||
870 | attr_name = get_identifier (name); | |
871 | attr_arg_name = build_string (strlen (arg_name), arg_name); | |
872 | attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE); | |
873 | attr = tree_cons (attr_name, attr_args, chain); | |
874 | return attr; | |
875 | } | |
1b062c1a MM |
876 | |
877 | \f | |
878 | /* Common functions used for target clone support. */ | |
879 | ||
880 | /* Comparator function to be used in qsort routine to sort attribute | |
881 | specification strings to "target". */ | |
882 | ||
883 | static int | |
884 | attr_strcmp (const void *v1, const void *v2) | |
885 | { | |
886 | const char *c1 = *(char *const*)v1; | |
887 | const char *c2 = *(char *const*)v2; | |
888 | return strcmp (c1, c2); | |
889 | } | |
890 | ||
891 | /* ARGLIST is the argument to target attribute. This function tokenizes | |
892 | the comma separated arguments, sorts them and returns a string which | |
893 | is a unique identifier for the comma separated arguments. It also | |
894 | replaces non-identifier characters "=,-" with "_". */ | |
895 | ||
896 | char * | |
897 | sorted_attr_string (tree arglist) | |
898 | { | |
899 | tree arg; | |
900 | size_t str_len_sum = 0; | |
901 | char **args = NULL; | |
902 | char *attr_str, *ret_str; | |
903 | char *attr = NULL; | |
904 | unsigned int argnum = 1; | |
905 | unsigned int i; | |
906 | ||
907 | for (arg = arglist; arg; arg = TREE_CHAIN (arg)) | |
908 | { | |
909 | const char *str = TREE_STRING_POINTER (TREE_VALUE (arg)); | |
910 | size_t len = strlen (str); | |
911 | str_len_sum += len + 1; | |
912 | if (arg != arglist) | |
913 | argnum++; | |
914 | for (i = 0; i < strlen (str); i++) | |
915 | if (str[i] == ',') | |
916 | argnum++; | |
917 | } | |
918 | ||
919 | attr_str = XNEWVEC (char, str_len_sum); | |
920 | str_len_sum = 0; | |
921 | for (arg = arglist; arg; arg = TREE_CHAIN (arg)) | |
922 | { | |
923 | const char *str = TREE_STRING_POINTER (TREE_VALUE (arg)); | |
924 | size_t len = strlen (str); | |
925 | memcpy (attr_str + str_len_sum, str, len); | |
926 | attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0'; | |
927 | str_len_sum += len + 1; | |
928 | } | |
929 | ||
930 | /* Replace "=,-" with "_". */ | |
931 | for (i = 0; i < strlen (attr_str); i++) | |
932 | if (attr_str[i] == '=' || attr_str[i]== '-') | |
933 | attr_str[i] = '_'; | |
934 | ||
935 | if (argnum == 1) | |
936 | return attr_str; | |
937 | ||
938 | args = XNEWVEC (char *, argnum); | |
939 | ||
940 | i = 0; | |
941 | attr = strtok (attr_str, ","); | |
942 | while (attr != NULL) | |
943 | { | |
944 | args[i] = attr; | |
945 | i++; | |
946 | attr = strtok (NULL, ","); | |
947 | } | |
948 | ||
949 | qsort (args, argnum, sizeof (char *), attr_strcmp); | |
950 | ||
951 | ret_str = XNEWVEC (char, str_len_sum); | |
952 | str_len_sum = 0; | |
953 | for (i = 0; i < argnum; i++) | |
954 | { | |
955 | size_t len = strlen (args[i]); | |
956 | memcpy (ret_str + str_len_sum, args[i], len); | |
957 | ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0'; | |
958 | str_len_sum += len + 1; | |
959 | } | |
960 | ||
961 | XDELETEVEC (args); | |
962 | XDELETEVEC (attr_str); | |
963 | return ret_str; | |
964 | } | |
965 | ||
966 | ||
967 | /* This function returns true if FN1 and FN2 are versions of the same function, | |
968 | that is, the target strings of the function decls are different. This assumes | |
969 | that FN1 and FN2 have the same signature. */ | |
970 | ||
971 | bool | |
972 | common_function_versions (tree fn1, tree fn2) | |
973 | { | |
974 | tree attr1, attr2; | |
975 | char *target1, *target2; | |
976 | bool result; | |
977 | ||
978 | if (TREE_CODE (fn1) != FUNCTION_DECL | |
979 | || TREE_CODE (fn2) != FUNCTION_DECL) | |
980 | return false; | |
981 | ||
982 | attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1)); | |
983 | attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2)); | |
984 | ||
985 | /* At least one function decl should have the target attribute specified. */ | |
986 | if (attr1 == NULL_TREE && attr2 == NULL_TREE) | |
987 | return false; | |
988 | ||
989 | /* Diagnose missing target attribute if one of the decls is already | |
990 | multi-versioned. */ | |
991 | if (attr1 == NULL_TREE || attr2 == NULL_TREE) | |
992 | { | |
993 | if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2)) | |
994 | { | |
995 | if (attr2 != NULL_TREE) | |
996 | { | |
997 | std::swap (fn1, fn2); | |
998 | attr1 = attr2; | |
999 | } | |
1000 | error_at (DECL_SOURCE_LOCATION (fn2), | |
1001 | "missing %<target%> attribute for multi-versioned %qD", | |
1002 | fn2); | |
1003 | inform (DECL_SOURCE_LOCATION (fn1), | |
1004 | "previous declaration of %qD", fn1); | |
1005 | /* Prevent diagnosing of the same error multiple times. */ | |
1006 | DECL_ATTRIBUTES (fn2) | |
1007 | = tree_cons (get_identifier ("target"), | |
1008 | copy_node (TREE_VALUE (attr1)), | |
1009 | DECL_ATTRIBUTES (fn2)); | |
1010 | } | |
1011 | return false; | |
1012 | } | |
1013 | ||
1014 | target1 = sorted_attr_string (TREE_VALUE (attr1)); | |
1015 | target2 = sorted_attr_string (TREE_VALUE (attr2)); | |
1016 | ||
1017 | /* The sorted target strings must be different for fn1 and fn2 | |
1018 | to be versions. */ | |
1019 | if (strcmp (target1, target2) == 0) | |
1020 | result = false; | |
1021 | else | |
1022 | result = true; | |
1023 | ||
1024 | XDELETEVEC (target1); | |
1025 | XDELETEVEC (target2); | |
1026 | ||
1027 | return result; | |
1028 | } | |
1029 | ||
1030 | /* Return a new name by appending SUFFIX to the DECL name. If make_unique | |
1031 | is true, append the full path name of the source file. */ | |
1032 | ||
1033 | char * | |
1034 | make_unique_name (tree decl, const char *suffix, bool make_unique) | |
1035 | { | |
1036 | char *global_var_name; | |
1037 | int name_len; | |
1038 | const char *name; | |
1039 | const char *unique_name = NULL; | |
1040 | ||
1041 | name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); | |
1042 | ||
1043 | /* Get a unique name that can be used globally without any chances | |
1044 | of collision at link time. */ | |
1045 | if (make_unique) | |
1046 | unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0")); | |
1047 | ||
1048 | name_len = strlen (name) + strlen (suffix) + 2; | |
1049 | ||
1050 | if (make_unique) | |
1051 | name_len += strlen (unique_name) + 1; | |
1052 | global_var_name = XNEWVEC (char, name_len); | |
1053 | ||
1054 | /* Use '.' to concatenate names as it is demangler friendly. */ | |
1055 | if (make_unique) | |
1056 | snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name, | |
1057 | suffix); | |
1058 | else | |
1059 | snprintf (global_var_name, name_len, "%s.%s", name, suffix); | |
1060 | ||
1061 | return global_var_name; | |
1062 | } | |
1063 | ||
1064 | /* Make a dispatcher declaration for the multi-versioned function DECL. | |
1065 | Calls to DECL function will be replaced with calls to the dispatcher | |
1066 | by the front-end. Return the decl created. */ | |
1067 | ||
1068 | tree | |
1069 | make_dispatcher_decl (const tree decl) | |
1070 | { | |
1071 | tree func_decl; | |
1072 | char *func_name; | |
1073 | tree fn_type, func_type; | |
1b062c1a | 1074 | |
871cc215 | 1075 | func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))); |
1b062c1a MM |
1076 | |
1077 | fn_type = TREE_TYPE (decl); | |
1078 | func_type = build_function_type (TREE_TYPE (fn_type), | |
1079 | TYPE_ARG_TYPES (fn_type)); | |
1080 | ||
1081 | func_decl = build_fn_decl (func_name, func_type); | |
1082 | XDELETEVEC (func_name); | |
1083 | TREE_USED (func_decl) = 1; | |
1084 | DECL_CONTEXT (func_decl) = NULL_TREE; | |
1085 | DECL_INITIAL (func_decl) = error_mark_node; | |
1086 | DECL_ARTIFICIAL (func_decl) = 1; | |
1087 | /* Mark this func as external, the resolver will flip it again if | |
1088 | it gets generated. */ | |
1089 | DECL_EXTERNAL (func_decl) = 1; | |
1090 | /* This will be of type IFUNCs have to be externally visible. */ | |
1091 | TREE_PUBLIC (func_decl) = 1; | |
1092 | ||
1093 | return func_decl; | |
1094 | } | |
1095 | ||
1096 | /* Returns true if decl is multi-versioned and DECL is the default function, | |
1097 | that is it is not tagged with target specific optimization. */ | |
1098 | ||
1099 | bool | |
1100 | is_function_default_version (const tree decl) | |
1101 | { | |
1102 | if (TREE_CODE (decl) != FUNCTION_DECL | |
1103 | || !DECL_FUNCTION_VERSIONED (decl)) | |
1104 | return false; | |
1105 | tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl)); | |
1106 | gcc_assert (attr); | |
1107 | attr = TREE_VALUE (TREE_VALUE (attr)); | |
1108 | return (TREE_CODE (attr) == STRING_CST | |
1109 | && strcmp (TREE_STRING_POINTER (attr), "default") == 0); | |
1110 | } | |
314e6352 ML |
1111 | |
1112 | /* Return a declaration like DDECL except that its DECL_ATTRIBUTES | |
1113 | is ATTRIBUTE. */ | |
1114 | ||
1115 | tree | |
1116 | build_decl_attribute_variant (tree ddecl, tree attribute) | |
1117 | { | |
1118 | DECL_ATTRIBUTES (ddecl) = attribute; | |
1119 | return ddecl; | |
1120 | } | |
1121 | ||
1122 | /* Return a type like TTYPE except that its TYPE_ATTRIBUTE | |
1123 | is ATTRIBUTE and its qualifiers are QUALS. | |
1124 | ||
1125 | Record such modified types already made so we don't make duplicates. */ | |
1126 | ||
1127 | tree | |
27c825c5 | 1128 | build_type_attribute_qual_variant (tree otype, tree attribute, int quals) |
314e6352 | 1129 | { |
27c825c5 | 1130 | tree ttype = otype; |
314e6352 ML |
1131 | if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute)) |
1132 | { | |
1133 | tree ntype; | |
1134 | ||
1135 | /* Building a distinct copy of a tagged type is inappropriate; it | |
1136 | causes breakage in code that expects there to be a one-to-one | |
1137 | relationship between a struct and its fields. | |
1138 | build_duplicate_type is another solution (as used in | |
1139 | handle_transparent_union_attribute), but that doesn't play well | |
1140 | with the stronger C++ type identity model. */ | |
1141 | if (TREE_CODE (ttype) == RECORD_TYPE | |
1142 | || TREE_CODE (ttype) == UNION_TYPE | |
1143 | || TREE_CODE (ttype) == QUAL_UNION_TYPE | |
1144 | || TREE_CODE (ttype) == ENUMERAL_TYPE) | |
1145 | { | |
1146 | warning (OPT_Wattributes, | |
1147 | "ignoring attributes applied to %qT after definition", | |
1148 | TYPE_MAIN_VARIANT (ttype)); | |
1149 | return build_qualified_type (ttype, quals); | |
1150 | } | |
1151 | ||
1152 | ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED); | |
27c825c5 JM |
1153 | if (lang_hooks.types.copy_lang_qualifiers |
1154 | && otype != TYPE_MAIN_VARIANT (otype)) | |
1155 | ttype = (lang_hooks.types.copy_lang_qualifiers | |
1156 | (ttype, TYPE_MAIN_VARIANT (otype))); | |
1157 | ||
5cedffbc | 1158 | tree dtype = ntype = build_distinct_type_copy (ttype); |
314e6352 ML |
1159 | |
1160 | TYPE_ATTRIBUTES (ntype) = attribute; | |
1161 | ||
1162 | hashval_t hash = type_hash_canon_hash (ntype); | |
1163 | ntype = type_hash_canon (hash, ntype); | |
1164 | ||
5cedffbc JM |
1165 | if (ntype != dtype) |
1166 | /* This variant was already in the hash table, don't mess with | |
1167 | TYPE_CANONICAL. */; | |
1168 | else if (TYPE_STRUCTURAL_EQUALITY_P (ttype) | |
1169 | || !comp_type_attributes (ntype, ttype)) | |
a5e2a41f JM |
1170 | /* If the target-dependent attributes make NTYPE different from |
1171 | its canonical type, we will need to use structural equality | |
1172 | checks for this type. | |
1173 | ||
1174 | We shouldn't get here for stripping attributes from a type; | |
1175 | the no-attribute type might not need structural comparison. But | |
1176 | we can if was discarded from type_hash_table. */ | |
1177 | SET_TYPE_STRUCTURAL_EQUALITY (ntype); | |
314e6352 ML |
1178 | else if (TYPE_CANONICAL (ntype) == ntype) |
1179 | TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype); | |
1180 | ||
1181 | ttype = build_qualified_type (ntype, quals); | |
27c825c5 JM |
1182 | if (lang_hooks.types.copy_lang_qualifiers |
1183 | && otype != TYPE_MAIN_VARIANT (otype)) | |
1184 | ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype); | |
314e6352 ML |
1185 | } |
1186 | else if (TYPE_QUALS (ttype) != quals) | |
1187 | ttype = build_qualified_type (ttype, quals); | |
1188 | ||
1189 | return ttype; | |
1190 | } | |
1191 | ||
1192 | /* Compare two identifier nodes representing attributes. | |
1193 | Return true if they are the same, false otherwise. */ | |
1194 | ||
1195 | static bool | |
1196 | cmp_attrib_identifiers (const_tree attr1, const_tree attr2) | |
1197 | { | |
1198 | /* Make sure we're dealing with IDENTIFIER_NODEs. */ | |
1199 | gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE | |
1200 | && TREE_CODE (attr2) == IDENTIFIER_NODE); | |
1201 | ||
1202 | /* Identifiers can be compared directly for equality. */ | |
1203 | if (attr1 == attr2) | |
1204 | return true; | |
1205 | ||
1206 | return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1), | |
1207 | IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2)); | |
1208 | } | |
1209 | ||
1210 | /* Compare two constructor-element-type constants. Return 1 if the lists | |
1211 | are known to be equal; otherwise return 0. */ | |
1212 | ||
1213 | static bool | |
1214 | simple_cst_list_equal (const_tree l1, const_tree l2) | |
1215 | { | |
1216 | while (l1 != NULL_TREE && l2 != NULL_TREE) | |
1217 | { | |
1218 | if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1) | |
1219 | return false; | |
1220 | ||
1221 | l1 = TREE_CHAIN (l1); | |
1222 | l2 = TREE_CHAIN (l2); | |
1223 | } | |
1224 | ||
1225 | return l1 == l2; | |
1226 | } | |
1227 | ||
1228 | /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are | |
1229 | the same. */ | |
1230 | ||
1231 | static bool | |
1232 | omp_declare_simd_clauses_equal (tree clauses1, tree clauses2) | |
1233 | { | |
1234 | tree cl1, cl2; | |
1235 | for (cl1 = clauses1, cl2 = clauses2; | |
1236 | cl1 && cl2; | |
1237 | cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2)) | |
1238 | { | |
1239 | if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2)) | |
1240 | return false; | |
1241 | if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN) | |
1242 | { | |
1243 | if (simple_cst_equal (OMP_CLAUSE_DECL (cl1), | |
1244 | OMP_CLAUSE_DECL (cl2)) != 1) | |
1245 | return false; | |
1246 | } | |
1247 | switch (OMP_CLAUSE_CODE (cl1)) | |
1248 | { | |
1249 | case OMP_CLAUSE_ALIGNED: | |
1250 | if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1), | |
1251 | OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1) | |
1252 | return false; | |
1253 | break; | |
1254 | case OMP_CLAUSE_LINEAR: | |
1255 | if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1), | |
1256 | OMP_CLAUSE_LINEAR_STEP (cl2)) != 1) | |
1257 | return false; | |
1258 | break; | |
1259 | case OMP_CLAUSE_SIMDLEN: | |
1260 | if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1), | |
1261 | OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1) | |
1262 | return false; | |
1263 | default: | |
1264 | break; | |
1265 | } | |
1266 | } | |
1267 | return true; | |
1268 | } | |
1269 | ||
1270 | ||
1271 | /* Compare two attributes for their value identity. Return true if the | |
1272 | attribute values are known to be equal; otherwise return false. */ | |
1273 | ||
1274 | bool | |
1275 | attribute_value_equal (const_tree attr1, const_tree attr2) | |
1276 | { | |
1277 | if (TREE_VALUE (attr1) == TREE_VALUE (attr2)) | |
1278 | return true; | |
1279 | ||
1280 | if (TREE_VALUE (attr1) != NULL_TREE | |
1281 | && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST | |
1282 | && TREE_VALUE (attr2) != NULL_TREE | |
1283 | && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST) | |
1284 | { | |
1285 | /* Handle attribute format. */ | |
1286 | if (is_attribute_p ("format", get_attribute_name (attr1))) | |
1287 | { | |
1288 | attr1 = TREE_VALUE (attr1); | |
1289 | attr2 = TREE_VALUE (attr2); | |
1290 | /* Compare the archetypes (printf/scanf/strftime/...). */ | |
1291 | if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2))) | |
1292 | return false; | |
1293 | /* Archetypes are the same. Compare the rest. */ | |
1294 | return (simple_cst_list_equal (TREE_CHAIN (attr1), | |
1295 | TREE_CHAIN (attr2)) == 1); | |
1296 | } | |
1297 | return (simple_cst_list_equal (TREE_VALUE (attr1), | |
1298 | TREE_VALUE (attr2)) == 1); | |
1299 | } | |
1300 | ||
bc1a75dd | 1301 | if (TREE_VALUE (attr1) |
314e6352 | 1302 | && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE |
bc1a75dd | 1303 | && TREE_VALUE (attr2) |
314e6352 ML |
1304 | && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE) |
1305 | return omp_declare_simd_clauses_equal (TREE_VALUE (attr1), | |
1306 | TREE_VALUE (attr2)); | |
1307 | ||
1308 | return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1); | |
1309 | } | |
1310 | ||
1311 | /* Return 0 if the attributes for two types are incompatible, 1 if they | |
1312 | are compatible, and 2 if they are nearly compatible (which causes a | |
1313 | warning to be generated). */ | |
1314 | int | |
1315 | comp_type_attributes (const_tree type1, const_tree type2) | |
1316 | { | |
1317 | const_tree a1 = TYPE_ATTRIBUTES (type1); | |
1318 | const_tree a2 = TYPE_ATTRIBUTES (type2); | |
1319 | const_tree a; | |
1320 | ||
1321 | if (a1 == a2) | |
1322 | return 1; | |
1323 | for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a)) | |
1324 | { | |
1325 | const struct attribute_spec *as; | |
1326 | const_tree attr; | |
1327 | ||
1328 | as = lookup_attribute_spec (get_attribute_name (a)); | |
1329 | if (!as || as->affects_type_identity == false) | |
1330 | continue; | |
1331 | ||
1332 | attr = lookup_attribute (as->name, CONST_CAST_TREE (a2)); | |
1333 | if (!attr || !attribute_value_equal (a, attr)) | |
1334 | break; | |
1335 | } | |
1336 | if (!a) | |
1337 | { | |
1338 | for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a)) | |
1339 | { | |
1340 | const struct attribute_spec *as; | |
1341 | ||
1342 | as = lookup_attribute_spec (get_attribute_name (a)); | |
1343 | if (!as || as->affects_type_identity == false) | |
1344 | continue; | |
1345 | ||
1346 | if (!lookup_attribute (as->name, CONST_CAST_TREE (a1))) | |
1347 | break; | |
1348 | /* We don't need to compare trees again, as we did this | |
1349 | already in first loop. */ | |
1350 | } | |
1351 | /* All types - affecting identity - are equal, so | |
1352 | there is no need to call target hook for comparison. */ | |
1353 | if (!a) | |
1354 | return 1; | |
1355 | } | |
1356 | if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a))) | |
1357 | return 0; | |
5c5f0b65 IT |
1358 | if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL) |
1359 | ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL)) | |
1360 | return 0; | |
314e6352 ML |
1361 | /* As some type combinations - like default calling-convention - might |
1362 | be compatible, we have to call the target hook to get the final result. */ | |
1363 | return targetm.comp_type_attributes (type1, type2); | |
1364 | } | |
1365 | ||
a3317f7b RS |
1366 | /* PREDICATE acts as a function of type: |
1367 | ||
1368 | (const_tree attr, const attribute_spec *as) -> bool | |
1369 | ||
1370 | where ATTR is an attribute and AS is its possibly-null specification. | |
1371 | Return a list of every attribute in attribute list ATTRS for which | |
1372 | PREDICATE is true. Return ATTRS itself if PREDICATE returns true | |
1373 | for every attribute. */ | |
1374 | ||
1375 | template<typename Predicate> | |
1376 | tree | |
1377 | remove_attributes_matching (tree attrs, Predicate predicate) | |
1378 | { | |
1379 | tree new_attrs = NULL_TREE; | |
1380 | tree *ptr = &new_attrs; | |
1381 | const_tree start = attrs; | |
1382 | for (const_tree attr = attrs; attr; attr = TREE_CHAIN (attr)) | |
1383 | { | |
1384 | tree name = get_attribute_name (attr); | |
1385 | const attribute_spec *as = lookup_attribute_spec (name); | |
1386 | const_tree end; | |
1387 | if (!predicate (attr, as)) | |
1388 | end = attr; | |
1389 | else if (start == attrs) | |
1390 | continue; | |
1391 | else | |
1392 | end = TREE_CHAIN (attr); | |
1393 | ||
1394 | for (; start != end; start = TREE_CHAIN (start)) | |
1395 | { | |
1396 | *ptr = tree_cons (TREE_PURPOSE (start), | |
1397 | TREE_VALUE (start), NULL_TREE); | |
1398 | TREE_CHAIN (*ptr) = NULL_TREE; | |
1399 | ptr = &TREE_CHAIN (*ptr); | |
1400 | } | |
1401 | start = TREE_CHAIN (attr); | |
1402 | } | |
1403 | gcc_assert (!start || start == attrs); | |
1404 | return start ? attrs : new_attrs; | |
1405 | } | |
1406 | ||
1407 | /* If VALUE is true, return the subset of ATTRS that affect type identity, | |
1408 | otherwise return the subset of ATTRS that don't affect type identity. */ | |
1409 | ||
1410 | tree | |
1411 | affects_type_identity_attributes (tree attrs, bool value) | |
1412 | { | |
1413 | auto predicate = [value](const_tree, const attribute_spec *as) -> bool | |
1414 | { | |
1415 | return bool (as && as->affects_type_identity) == value; | |
1416 | }; | |
1417 | return remove_attributes_matching (attrs, predicate); | |
1418 | } | |
1419 | ||
1696fc1e RS |
1420 | /* Remove attributes that affect type identity from ATTRS unless the |
1421 | same attributes occur in OK_ATTRS. */ | |
1422 | ||
1423 | tree | |
1424 | restrict_type_identity_attributes_to (tree attrs, tree ok_attrs) | |
1425 | { | |
1426 | auto predicate = [ok_attrs](const_tree attr, | |
1427 | const attribute_spec *as) -> bool | |
1428 | { | |
1429 | if (!as || !as->affects_type_identity) | |
1430 | return true; | |
1431 | ||
1432 | for (tree ok_attr = lookup_attribute (as->name, ok_attrs); | |
1433 | ok_attr; | |
1434 | ok_attr = lookup_attribute (as->name, TREE_CHAIN (ok_attr))) | |
1435 | if (simple_cst_equal (TREE_VALUE (ok_attr), TREE_VALUE (attr)) == 1) | |
1436 | return true; | |
1437 | ||
1438 | return false; | |
1439 | }; | |
1440 | return remove_attributes_matching (attrs, predicate); | |
1441 | } | |
1442 | ||
314e6352 ML |
1443 | /* Return a type like TTYPE except that its TYPE_ATTRIBUTE |
1444 | is ATTRIBUTE. | |
1445 | ||
1446 | Record such modified types already made so we don't make duplicates. */ | |
1447 | ||
1448 | tree | |
1449 | build_type_attribute_variant (tree ttype, tree attribute) | |
1450 | { | |
1451 | return build_type_attribute_qual_variant (ttype, attribute, | |
1452 | TYPE_QUALS (ttype)); | |
1453 | } | |
1454 | \f | |
1455 | /* A variant of lookup_attribute() that can be used with an identifier | |
1456 | as the first argument, and where the identifier can be either | |
1457 | 'text' or '__text__'. | |
1458 | ||
1459 | Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST, | |
1460 | return a pointer to the attribute's list element if the attribute | |
1461 | is part of the list, or NULL_TREE if not found. If the attribute | |
1462 | appears more than once, this only returns the first occurrence; the | |
1463 | TREE_CHAIN of the return value should be passed back in if further | |
1464 | occurrences are wanted. ATTR_IDENTIFIER must be an identifier but | |
1465 | can be in the form 'text' or '__text__'. */ | |
1466 | static tree | |
1467 | lookup_ident_attribute (tree attr_identifier, tree list) | |
1468 | { | |
1469 | gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE); | |
1470 | ||
1471 | while (list) | |
1472 | { | |
1473 | gcc_checking_assert (TREE_CODE (get_attribute_name (list)) | |
1474 | == IDENTIFIER_NODE); | |
1475 | ||
1476 | if (cmp_attrib_identifiers (attr_identifier, | |
1477 | get_attribute_name (list))) | |
1478 | /* Found it. */ | |
1479 | break; | |
1480 | list = TREE_CHAIN (list); | |
1481 | } | |
1482 | ||
1483 | return list; | |
1484 | } | |
1485 | ||
1486 | /* Remove any instances of attribute ATTR_NAME in LIST and return the | |
1487 | modified list. */ | |
1488 | ||
1489 | tree | |
1490 | remove_attribute (const char *attr_name, tree list) | |
1491 | { | |
1492 | tree *p; | |
1493 | gcc_checking_assert (attr_name[0] != '_'); | |
1494 | ||
1495 | for (p = &list; *p;) | |
1496 | { | |
1497 | tree l = *p; | |
1498 | ||
1499 | tree attr = get_attribute_name (l); | |
1500 | if (is_attribute_p (attr_name, attr)) | |
1501 | *p = TREE_CHAIN (l); | |
1502 | else | |
1503 | p = &TREE_CHAIN (l); | |
1504 | } | |
1505 | ||
1506 | return list; | |
1507 | } | |
1508 | ||
1509 | /* Return an attribute list that is the union of a1 and a2. */ | |
1510 | ||
1511 | tree | |
1512 | merge_attributes (tree a1, tree a2) | |
1513 | { | |
1514 | tree attributes; | |
1515 | ||
1516 | /* Either one unset? Take the set one. */ | |
1517 | ||
1518 | if ((attributes = a1) == 0) | |
1519 | attributes = a2; | |
1520 | ||
1521 | /* One that completely contains the other? Take it. */ | |
1522 | ||
1523 | else if (a2 != 0 && ! attribute_list_contained (a1, a2)) | |
1524 | { | |
1525 | if (attribute_list_contained (a2, a1)) | |
1526 | attributes = a2; | |
1527 | else | |
1528 | { | |
1529 | /* Pick the longest list, and hang on the other list. */ | |
1530 | ||
1531 | if (list_length (a1) < list_length (a2)) | |
1532 | attributes = a2, a2 = a1; | |
1533 | ||
1534 | for (; a2 != 0; a2 = TREE_CHAIN (a2)) | |
1535 | { | |
1536 | tree a; | |
1537 | for (a = lookup_ident_attribute (get_attribute_name (a2), | |
1538 | attributes); | |
1539 | a != NULL_TREE && !attribute_value_equal (a, a2); | |
1540 | a = lookup_ident_attribute (get_attribute_name (a2), | |
1541 | TREE_CHAIN (a))) | |
1542 | ; | |
1543 | if (a == NULL_TREE) | |
1544 | { | |
1545 | a1 = copy_node (a2); | |
1546 | TREE_CHAIN (a1) = attributes; | |
1547 | attributes = a1; | |
1548 | } | |
1549 | } | |
1550 | } | |
1551 | } | |
1552 | return attributes; | |
1553 | } | |
1554 | ||
1555 | /* Given types T1 and T2, merge their attributes and return | |
1556 | the result. */ | |
1557 | ||
1558 | tree | |
1559 | merge_type_attributes (tree t1, tree t2) | |
1560 | { | |
1561 | return merge_attributes (TYPE_ATTRIBUTES (t1), | |
1562 | TYPE_ATTRIBUTES (t2)); | |
1563 | } | |
1564 | ||
1565 | /* Given decls OLDDECL and NEWDECL, merge their attributes and return | |
1566 | the result. */ | |
1567 | ||
1568 | tree | |
1569 | merge_decl_attributes (tree olddecl, tree newdecl) | |
1570 | { | |
1571 | return merge_attributes (DECL_ATTRIBUTES (olddecl), | |
1572 | DECL_ATTRIBUTES (newdecl)); | |
1573 | } | |
1574 | ||
bc1a75dd JJ |
1575 | /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if |
1576 | they are missing there. */ | |
1577 | ||
1578 | void | |
1579 | duplicate_one_attribute (tree *attrs, tree attr, const char *name) | |
1580 | { | |
1581 | attr = lookup_attribute (name, attr); | |
1582 | if (!attr) | |
1583 | return; | |
1584 | tree a = lookup_attribute (name, *attrs); | |
1585 | while (attr) | |
1586 | { | |
1587 | tree a2; | |
1588 | for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2))) | |
1589 | if (attribute_value_equal (attr, a2)) | |
1590 | break; | |
1591 | if (!a2) | |
1592 | { | |
1593 | a2 = copy_node (attr); | |
1594 | TREE_CHAIN (a2) = *attrs; | |
1595 | *attrs = a2; | |
1596 | } | |
1597 | attr = lookup_attribute (name, TREE_CHAIN (attr)); | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | /* Duplicate all attributes from user DECL to the corresponding | |
1602 | builtin that should be propagated. */ | |
1603 | ||
1604 | void | |
1605 | copy_attributes_to_builtin (tree decl) | |
1606 | { | |
1607 | tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl)); | |
1608 | if (b) | |
1609 | duplicate_one_attribute (&DECL_ATTRIBUTES (b), | |
1610 | DECL_ATTRIBUTES (decl), "omp declare simd"); | |
1611 | } | |
1612 | ||
314e6352 ML |
1613 | #if TARGET_DLLIMPORT_DECL_ATTRIBUTES |
1614 | ||
1615 | /* Specialization of merge_decl_attributes for various Windows targets. | |
1616 | ||
1617 | This handles the following situation: | |
1618 | ||
1619 | __declspec (dllimport) int foo; | |
1620 | int foo; | |
1621 | ||
1622 | The second instance of `foo' nullifies the dllimport. */ | |
1623 | ||
1624 | tree | |
1625 | merge_dllimport_decl_attributes (tree old, tree new_tree) | |
1626 | { | |
1627 | tree a; | |
1628 | int delete_dllimport_p = 1; | |
1629 | ||
1630 | /* What we need to do here is remove from `old' dllimport if it doesn't | |
1631 | appear in `new'. dllimport behaves like extern: if a declaration is | |
1632 | marked dllimport and a definition appears later, then the object | |
1633 | is not dllimport'd. We also remove a `new' dllimport if the old list | |
1634 | contains dllexport: dllexport always overrides dllimport, regardless | |
1635 | of the order of declaration. */ | |
1636 | if (!VAR_OR_FUNCTION_DECL_P (new_tree)) | |
1637 | delete_dllimport_p = 0; | |
1638 | else if (DECL_DLLIMPORT_P (new_tree) | |
1639 | && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old))) | |
1640 | { | |
1641 | DECL_DLLIMPORT_P (new_tree) = 0; | |
1642 | warning (OPT_Wattributes, "%q+D already declared with dllexport " | |
1643 | "attribute: dllimport ignored", new_tree); | |
1644 | } | |
1645 | else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree)) | |
1646 | { | |
1647 | /* Warn about overriding a symbol that has already been used, e.g.: | |
1648 | extern int __attribute__ ((dllimport)) foo; | |
1649 | int* bar () {return &foo;} | |
1650 | int foo; | |
1651 | */ | |
1652 | if (TREE_USED (old)) | |
1653 | { | |
1654 | warning (0, "%q+D redeclared without dllimport attribute " | |
1655 | "after being referenced with dll linkage", new_tree); | |
1656 | /* If we have used a variable's address with dllimport linkage, | |
1657 | keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the | |
1658 | decl may already have had TREE_CONSTANT computed. | |
1659 | We still remove the attribute so that assembler code refers | |
1660 | to '&foo rather than '_imp__foo'. */ | |
1661 | if (VAR_P (old) && TREE_ADDRESSABLE (old)) | |
1662 | DECL_DLLIMPORT_P (new_tree) = 1; | |
1663 | } | |
1664 | ||
1665 | /* Let an inline definition silently override the external reference, | |
1666 | but otherwise warn about attribute inconsistency. */ | |
1667 | else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree)) | |
1668 | warning (OPT_Wattributes, "%q+D redeclared without dllimport " | |
1669 | "attribute: previous dllimport ignored", new_tree); | |
1670 | } | |
1671 | else | |
1672 | delete_dllimport_p = 0; | |
1673 | ||
1674 | a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree)); | |
1675 | ||
1676 | if (delete_dllimport_p) | |
1677 | a = remove_attribute ("dllimport", a); | |
1678 | ||
1679 | return a; | |
1680 | } | |
1681 | ||
1682 | /* Handle a "dllimport" or "dllexport" attribute; arguments as in | |
1683 | struct attribute_spec.handler. */ | |
1684 | ||
1685 | tree | |
1686 | handle_dll_attribute (tree * pnode, tree name, tree args, int flags, | |
1687 | bool *no_add_attrs) | |
1688 | { | |
1689 | tree node = *pnode; | |
1690 | bool is_dllimport; | |
1691 | ||
1692 | /* These attributes may apply to structure and union types being created, | |
1693 | but otherwise should pass to the declaration involved. */ | |
1694 | if (!DECL_P (node)) | |
1695 | { | |
1696 | if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT | |
1697 | | (int) ATTR_FLAG_ARRAY_NEXT)) | |
1698 | { | |
1699 | *no_add_attrs = true; | |
1700 | return tree_cons (name, args, NULL_TREE); | |
1701 | } | |
1702 | if (TREE_CODE (node) == RECORD_TYPE | |
1703 | || TREE_CODE (node) == UNION_TYPE) | |
1704 | { | |
1705 | node = TYPE_NAME (node); | |
1706 | if (!node) | |
1707 | return NULL_TREE; | |
1708 | } | |
1709 | else | |
1710 | { | |
1711 | warning (OPT_Wattributes, "%qE attribute ignored", | |
1712 | name); | |
1713 | *no_add_attrs = true; | |
1714 | return NULL_TREE; | |
1715 | } | |
1716 | } | |
1717 | ||
1718 | if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL) | |
1719 | { | |
1720 | *no_add_attrs = true; | |
1721 | warning (OPT_Wattributes, "%qE attribute ignored", | |
1722 | name); | |
1723 | return NULL_TREE; | |
1724 | } | |
1725 | ||
1726 | if (TREE_CODE (node) == TYPE_DECL | |
1727 | && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE | |
1728 | && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE) | |
1729 | { | |
1730 | *no_add_attrs = true; | |
1731 | warning (OPT_Wattributes, "%qE attribute ignored", | |
1732 | name); | |
1733 | return NULL_TREE; | |
1734 | } | |
1735 | ||
1736 | is_dllimport = is_attribute_p ("dllimport", name); | |
1737 | ||
1738 | /* Report error on dllimport ambiguities seen now before they cause | |
1739 | any damage. */ | |
1740 | if (is_dllimport) | |
1741 | { | |
1742 | /* Honor any target-specific overrides. */ | |
1743 | if (!targetm.valid_dllimport_attribute_p (node)) | |
1744 | *no_add_attrs = true; | |
1745 | ||
1746 | else if (TREE_CODE (node) == FUNCTION_DECL | |
1747 | && DECL_DECLARED_INLINE_P (node)) | |
1748 | { | |
1749 | warning (OPT_Wattributes, "inline function %q+D declared as " | |
0d7bac69 | 1750 | "dllimport: attribute ignored", node); |
314e6352 ML |
1751 | *no_add_attrs = true; |
1752 | } | |
1753 | /* Like MS, treat definition of dllimported variables and | |
1754 | non-inlined functions on declaration as syntax errors. */ | |
1755 | else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node)) | |
1756 | { | |
1757 | error ("function %q+D definition is marked dllimport", node); | |
1758 | *no_add_attrs = true; | |
1759 | } | |
1760 | ||
1761 | else if (VAR_P (node)) | |
1762 | { | |
1763 | if (DECL_INITIAL (node)) | |
1764 | { | |
1765 | error ("variable %q+D definition is marked dllimport", | |
1766 | node); | |
1767 | *no_add_attrs = true; | |
1768 | } | |
1769 | ||
1770 | /* `extern' needn't be specified with dllimport. | |
1771 | Specify `extern' now and hope for the best. Sigh. */ | |
1772 | DECL_EXTERNAL (node) = 1; | |
1773 | /* Also, implicitly give dllimport'd variables declared within | |
1774 | a function global scope, unless declared static. */ | |
1775 | if (current_function_decl != NULL_TREE && !TREE_STATIC (node)) | |
1776 | TREE_PUBLIC (node) = 1; | |
3568d2d5 JJ |
1777 | /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless |
1778 | it is a C++ static data member. */ | |
1779 | if (DECL_CONTEXT (node) == NULL_TREE | |
1780 | || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node))) | |
1781 | TREE_STATIC (node) = 0; | |
314e6352 ML |
1782 | } |
1783 | ||
1784 | if (*no_add_attrs == false) | |
1785 | DECL_DLLIMPORT_P (node) = 1; | |
1786 | } | |
1787 | else if (TREE_CODE (node) == FUNCTION_DECL | |
1788 | && DECL_DECLARED_INLINE_P (node) | |
1789 | && flag_keep_inline_dllexport) | |
1790 | /* An exported function, even if inline, must be emitted. */ | |
1791 | DECL_EXTERNAL (node) = 0; | |
1792 | ||
1793 | /* Report error if symbol is not accessible at global scope. */ | |
1794 | if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node)) | |
1795 | { | |
1796 | error ("external linkage required for symbol %q+D because of " | |
1797 | "%qE attribute", node, name); | |
1798 | *no_add_attrs = true; | |
1799 | } | |
1800 | ||
1801 | /* A dllexport'd entity must have default visibility so that other | |
1802 | program units (shared libraries or the main executable) can see | |
1803 | it. A dllimport'd entity must have default visibility so that | |
1804 | the linker knows that undefined references within this program | |
1805 | unit can be resolved by the dynamic linker. */ | |
1806 | if (!*no_add_attrs) | |
1807 | { | |
1808 | if (DECL_VISIBILITY_SPECIFIED (node) | |
1809 | && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT) | |
1810 | error ("%qE implies default visibility, but %qD has already " | |
1811 | "been declared with a different visibility", | |
1812 | name, node); | |
1813 | DECL_VISIBILITY (node) = VISIBILITY_DEFAULT; | |
1814 | DECL_VISIBILITY_SPECIFIED (node) = 1; | |
1815 | } | |
1816 | ||
1817 | return NULL_TREE; | |
1818 | } | |
1819 | ||
1820 | #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */ | |
1821 | ||
1822 | /* Given two lists of attributes, return true if list l2 is | |
1823 | equivalent to l1. */ | |
1824 | ||
1825 | int | |
1826 | attribute_list_equal (const_tree l1, const_tree l2) | |
1827 | { | |
1828 | if (l1 == l2) | |
1829 | return 1; | |
1830 | ||
1831 | return attribute_list_contained (l1, l2) | |
1832 | && attribute_list_contained (l2, l1); | |
1833 | } | |
1834 | ||
1835 | /* Given two lists of attributes, return true if list L2 is | |
1836 | completely contained within L1. */ | |
1837 | /* ??? This would be faster if attribute names were stored in a canonicalized | |
1838 | form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method | |
1839 | must be used to show these elements are equivalent (which they are). */ | |
1840 | /* ??? It's not clear that attributes with arguments will always be handled | |
1841 | correctly. */ | |
1842 | ||
1843 | int | |
1844 | attribute_list_contained (const_tree l1, const_tree l2) | |
1845 | { | |
1846 | const_tree t1, t2; | |
1847 | ||
1848 | /* First check the obvious, maybe the lists are identical. */ | |
1849 | if (l1 == l2) | |
1850 | return 1; | |
1851 | ||
1852 | /* Maybe the lists are similar. */ | |
1853 | for (t1 = l1, t2 = l2; | |
1854 | t1 != 0 && t2 != 0 | |
1855 | && get_attribute_name (t1) == get_attribute_name (t2) | |
1856 | && TREE_VALUE (t1) == TREE_VALUE (t2); | |
1857 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) | |
1858 | ; | |
1859 | ||
1860 | /* Maybe the lists are equal. */ | |
1861 | if (t1 == 0 && t2 == 0) | |
1862 | return 1; | |
1863 | ||
1864 | for (; t2 != 0; t2 = TREE_CHAIN (t2)) | |
1865 | { | |
1866 | const_tree attr; | |
1867 | /* This CONST_CAST is okay because lookup_attribute does not | |
1868 | modify its argument and the return value is assigned to a | |
1869 | const_tree. */ | |
1870 | for (attr = lookup_ident_attribute (get_attribute_name (t2), | |
1871 | CONST_CAST_TREE (l1)); | |
1872 | attr != NULL_TREE && !attribute_value_equal (t2, attr); | |
1873 | attr = lookup_ident_attribute (get_attribute_name (t2), | |
1874 | TREE_CHAIN (attr))) | |
1875 | ; | |
1876 | ||
1877 | if (attr == NULL_TREE) | |
1878 | return 0; | |
1879 | } | |
1880 | ||
1881 | return 1; | |
1882 | } | |
13bdca74 ML |
1883 | |
1884 | /* The backbone of lookup_attribute(). ATTR_LEN is the string length | |
1885 | of ATTR_NAME, and LIST is not NULL_TREE. | |
1886 | ||
1887 | The function is called from lookup_attribute in order to optimize | |
1888 | for size. */ | |
1889 | ||
1890 | tree | |
1891 | private_lookup_attribute (const char *attr_name, size_t attr_len, tree list) | |
1892 | { | |
1893 | while (list) | |
1894 | { | |
1895 | tree attr = get_attribute_name (list); | |
1896 | size_t ident_len = IDENTIFIER_LENGTH (attr); | |
1897 | if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr), | |
1898 | ident_len)) | |
1899 | break; | |
1900 | list = TREE_CHAIN (list); | |
1901 | } | |
1902 | ||
1903 | return list; | |
1904 | } | |
5d9ae53d | 1905 | |
79a2c428 MS |
1906 | /* Return true if the function decl or type NODE has been declared |
1907 | with attribute ANAME among attributes ATTRS. */ | |
1908 | ||
1909 | static bool | |
1910 | has_attribute (tree node, tree attrs, const char *aname) | |
1911 | { | |
1912 | if (!strcmp (aname, "const")) | |
1913 | { | |
1914 | if (DECL_P (node) && TREE_READONLY (node)) | |
1915 | return true; | |
1916 | } | |
1917 | else if (!strcmp (aname, "malloc")) | |
1918 | { | |
1919 | if (DECL_P (node) && DECL_IS_MALLOC (node)) | |
1920 | return true; | |
1921 | } | |
1922 | else if (!strcmp (aname, "noreturn")) | |
1923 | { | |
1924 | if (DECL_P (node) && TREE_THIS_VOLATILE (node)) | |
1925 | return true; | |
1926 | } | |
1927 | else if (!strcmp (aname, "nothrow")) | |
1928 | { | |
1929 | if (TREE_NOTHROW (node)) | |
1930 | return true; | |
1931 | } | |
1932 | else if (!strcmp (aname, "pure")) | |
1933 | { | |
1934 | if (DECL_P (node) && DECL_PURE_P (node)) | |
1935 | return true; | |
1936 | } | |
1937 | ||
1938 | return lookup_attribute (aname, attrs); | |
1939 | } | |
1940 | ||
1941 | /* Return the number of mismatched function or type attributes between | |
1942 | the "template" function declaration TMPL and DECL. The word "template" | |
1943 | doesn't necessarily refer to a C++ template but rather a declaration | |
1944 | whose attributes should be matched by those on DECL. For a non-zero | |
1945 | return value set *ATTRSTR to a string representation of the list of | |
1946 | mismatched attributes with quoted names. | |
1947 | ATTRLIST is a list of additional attributes that SPEC should be | |
1948 | taken to ultimately be declared with. */ | |
1949 | ||
1950 | unsigned | |
1951 | decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist, | |
1952 | const char* const blacklist[], | |
1953 | pretty_printer *attrstr) | |
1954 | { | |
1955 | if (TREE_CODE (tmpl) != FUNCTION_DECL) | |
1956 | return 0; | |
1957 | ||
1958 | /* Avoid warning if either declaration or its type is deprecated. */ | |
1959 | if (TREE_DEPRECATED (tmpl) | |
1960 | || TREE_DEPRECATED (decl)) | |
1961 | return 0; | |
1962 | ||
1963 | const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) }; | |
1964 | const tree decls[] = { decl, TREE_TYPE (decl) }; | |
1965 | ||
1966 | if (TREE_DEPRECATED (tmpls[1]) | |
1967 | || TREE_DEPRECATED (decls[1]) | |
1968 | || TREE_DEPRECATED (TREE_TYPE (tmpls[1])) | |
1969 | || TREE_DEPRECATED (TREE_TYPE (decls[1]))) | |
1970 | return 0; | |
1971 | ||
1972 | tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) }; | |
1973 | tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) }; | |
1974 | ||
1975 | if (!decl_attrs[0]) | |
1976 | decl_attrs[0] = attrlist; | |
1977 | else if (!decl_attrs[1]) | |
1978 | decl_attrs[1] = attrlist; | |
1979 | ||
1980 | /* Avoid warning if the template has no attributes. */ | |
1981 | if (!tmpl_attrs[0] && !tmpl_attrs[1]) | |
1982 | return 0; | |
1983 | ||
1984 | /* Avoid warning if either declaration contains an attribute on | |
1985 | the white list below. */ | |
1986 | const char* const whitelist[] = { | |
1987 | "error", "warning" | |
1988 | }; | |
1989 | ||
1990 | for (unsigned i = 0; i != 2; ++i) | |
1991 | for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j) | |
1992 | if (lookup_attribute (whitelist[j], tmpl_attrs[i]) | |
1993 | || lookup_attribute (whitelist[j], decl_attrs[i])) | |
1994 | return 0; | |
1995 | ||
1996 | /* Put together a list of the black-listed attributes that the template | |
1997 | is declared with and the declaration is not, in case it's not apparent | |
1998 | from the most recent declaration of the template. */ | |
1999 | unsigned nattrs = 0; | |
2000 | ||
2001 | for (unsigned i = 0; blacklist[i]; ++i) | |
2002 | { | |
29d24852 MS |
2003 | /* Attribute leaf only applies to extern functions. Avoid mentioning |
2004 | it when it's missing from a static declaration. */ | |
2005 | if (!TREE_PUBLIC (decl) | |
2006 | && !strcmp ("leaf", blacklist[i])) | |
2007 | continue; | |
2008 | ||
79a2c428 MS |
2009 | for (unsigned j = 0; j != 2; ++j) |
2010 | { | |
2011 | if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i])) | |
2012 | continue; | |
2013 | ||
dea78431 | 2014 | bool found = false; |
79a2c428 MS |
2015 | unsigned kmax = 1 + !!decl_attrs[1]; |
2016 | for (unsigned k = 0; k != kmax; ++k) | |
2017 | { | |
2018 | if (has_attribute (decls[k], decl_attrs[k], blacklist[i])) | |
dea78431 AO |
2019 | { |
2020 | found = true; | |
2021 | break; | |
2022 | } | |
2023 | } | |
79a2c428 | 2024 | |
dea78431 AO |
2025 | if (!found) |
2026 | { | |
79a2c428 MS |
2027 | if (nattrs) |
2028 | pp_string (attrstr, ", "); | |
2029 | pp_begin_quote (attrstr, pp_show_color (global_dc->printer)); | |
2030 | pp_string (attrstr, blacklist[i]); | |
2031 | pp_end_quote (attrstr, pp_show_color (global_dc->printer)); | |
2032 | ++nattrs; | |
2033 | } | |
dea78431 AO |
2034 | |
2035 | break; | |
79a2c428 MS |
2036 | } |
2037 | } | |
2038 | ||
2039 | return nattrs; | |
2040 | } | |
2041 | ||
2042 | /* Issue a warning for the declaration ALIAS for TARGET where ALIAS | |
2043 | specifies either attributes that are incompatible with those of | |
2044 | TARGET, or attributes that are missing and that declaring ALIAS | |
2045 | with would benefit. */ | |
2046 | ||
2047 | void | |
2048 | maybe_diag_alias_attributes (tree alias, tree target) | |
2049 | { | |
2050 | /* Do not expect attributes to match between aliases and ifunc | |
2051 | resolvers. There is no obvious correspondence between them. */ | |
2052 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias))) | |
2053 | return; | |
2054 | ||
2055 | const char* const blacklist[] = { | |
2056 | "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc", | |
2057 | "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull", | |
2058 | "returns_twice", NULL | |
2059 | }; | |
2060 | ||
2061 | pretty_printer attrnames; | |
2062 | if (warn_attribute_alias > 1) | |
2063 | { | |
2064 | /* With -Wattribute-alias=2 detect alias declarations that are more | |
2065 | restrictive than their targets first. Those indicate potential | |
2066 | codegen bugs. */ | |
2067 | if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE, | |
2068 | blacklist, &attrnames)) | |
2069 | { | |
2070 | auto_diagnostic_group d; | |
2071 | if (warning_n (DECL_SOURCE_LOCATION (alias), | |
2072 | OPT_Wattribute_alias_, n, | |
2073 | "%qD specifies more restrictive attribute than " | |
2074 | "its target %qD: %s", | |
2075 | "%qD specifies more restrictive attributes than " | |
2076 | "its target %qD: %s", | |
2077 | alias, target, pp_formatted_text (&attrnames))) | |
2078 | inform (DECL_SOURCE_LOCATION (target), | |
2079 | "%qD target declared here", alias); | |
2080 | return; | |
2081 | } | |
2082 | } | |
2083 | ||
2084 | /* Detect alias declarations that are less restrictive than their | |
2085 | targets. Those suggest potential optimization opportunities | |
2086 | (solved by adding the missing attribute(s) to the alias). */ | |
2087 | if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE, | |
2088 | blacklist, &attrnames)) | |
2089 | { | |
2090 | auto_diagnostic_group d; | |
2091 | if (warning_n (DECL_SOURCE_LOCATION (alias), | |
2092 | OPT_Wmissing_attributes, n, | |
2093 | "%qD specifies less restrictive attribute than " | |
2094 | "its target %qD: %s", | |
2095 | "%qD specifies less restrictive attributes than " | |
2096 | "its target %qD: %s", | |
2097 | alias, target, pp_formatted_text (&attrnames))) | |
2098 | inform (DECL_SOURCE_LOCATION (target), | |
2099 | "%qD target declared here", alias); | |
2100 | } | |
2101 | } | |
2102 | ||
6450f073 MS |
2103 | /* Initialize a mapping RWM for a call to a function declared with |
2104 | attribute access in ATTRS. Each attribute positional operand | |
2105 | inserts one entry into the mapping with the operand number as | |
2106 | the key. */ | |
b825a228 MS |
2107 | |
2108 | void | |
6450f073 | 2109 | init_attr_rdwr_indices (rdwr_map *rwm, tree attrs) |
b825a228 | 2110 | { |
6450f073 | 2111 | if (!attrs) |
b825a228 MS |
2112 | return; |
2113 | ||
6450f073 | 2114 | for (tree access = attrs; |
b825a228 MS |
2115 | (access = lookup_attribute ("access", access)); |
2116 | access = TREE_CHAIN (access)) | |
2117 | { | |
2118 | /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE | |
2119 | is the attribute argument's value. */ | |
2120 | tree mode = TREE_VALUE (access); | |
6450f073 MS |
2121 | if (!mode) |
2122 | return; | |
2123 | ||
2124 | /* The (optional) list of VLA bounds. */ | |
2125 | tree vblist = TREE_CHAIN (mode); | |
b825a228 | 2126 | mode = TREE_VALUE (mode); |
6450f073 MS |
2127 | if (TREE_CODE (mode) != STRING_CST) |
2128 | continue; | |
b825a228 MS |
2129 | gcc_assert (TREE_CODE (mode) == STRING_CST); |
2130 | ||
c6503fa9 MS |
2131 | if (vblist) |
2132 | vblist = nreverse (copy_list (TREE_VALUE (vblist))); | |
2133 | ||
6450f073 | 2134 | for (const char *m = TREE_STRING_POINTER (mode); *m; ) |
b825a228 MS |
2135 | { |
2136 | attr_access acc = { }; | |
2137 | ||
6450f073 MS |
2138 | /* Skip the internal-only plus sign. */ |
2139 | if (*m == '+') | |
2140 | ++m; | |
2141 | ||
2142 | acc.str = m; | |
2143 | acc.mode = acc.from_mode_char (*m); | |
2144 | acc.sizarg = UINT_MAX; | |
2145 | ||
2146 | const char *end; | |
2147 | acc.ptrarg = strtoul (++m, const_cast<char**>(&end), 10); | |
2148 | m = end; | |
2149 | ||
2150 | if (*m == '[') | |
b825a228 | 2151 | { |
6450f073 MS |
2152 | /* Forms containing the square bracket are internal-only |
2153 | (not specified by an attribute declaration), and used | |
2154 | for various forms of array and VLA parameters. */ | |
2155 | acc.internal_p = true; | |
2156 | ||
2157 | /* Search to the closing bracket and look at the preceding | |
2158 | code: it determines the form of the most significant | |
2159 | bound of the array. Others prior to it encode the form | |
2160 | of interior VLA bounds. They're not of interest here. */ | |
2161 | end = strchr (m, ']'); | |
2162 | const char *p = end; | |
2163 | gcc_assert (p); | |
2164 | ||
2165 | while (ISDIGIT (p[-1])) | |
2166 | --p; | |
2167 | ||
2168 | if (ISDIGIT (*p)) | |
2169 | { | |
2170 | /* A digit denotes a constant bound (as in T[3]). */ | |
2171 | acc.static_p = p[-1] == 's'; | |
2172 | acc.minsize = strtoull (p, NULL, 10); | |
2173 | } | |
2174 | else if (' ' == p[-1]) | |
2175 | { | |
2176 | /* A space denotes an ordinary array of unspecified bound | |
2177 | (as in T[]). */ | |
2178 | acc.minsize = 0; | |
2179 | } | |
2180 | else if ('*' == p[-1] || '$' == p[-1]) | |
2181 | { | |
2182 | /* An asterisk denotes a VLA. When the closing bracket | |
2183 | is followed by a comma and a dollar sign its bound is | |
2184 | on the list. Otherwise it's a VLA with an unspecified | |
2185 | bound. */ | |
757ba665 | 2186 | acc.static_p = p[-2] == 's'; |
6450f073 MS |
2187 | acc.minsize = HOST_WIDE_INT_M1U; |
2188 | } | |
2189 | ||
2190 | m = end + 1; | |
b825a228 MS |
2191 | } |
2192 | ||
b825a228 MS |
2193 | if (*m == ',') |
2194 | { | |
6450f073 MS |
2195 | ++m; |
2196 | do | |
2197 | { | |
2198 | if (*m == '$') | |
2199 | { | |
2200 | ++m; | |
3599ecb6 | 2201 | if (!acc.size && vblist) |
6450f073 MS |
2202 | { |
2203 | /* Extract the list of VLA bounds for the current | |
2204 | parameter, store it in ACC.SIZE, and advance | |
2205 | to the list of bounds for the next VLA parameter. | |
2206 | */ | |
2207 | acc.size = TREE_VALUE (vblist); | |
2208 | vblist = TREE_CHAIN (vblist); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | if (ISDIGIT (*m)) | |
2213 | { | |
2214 | /* Extract the positional argument. It's absent | |
2215 | for VLAs whose bound doesn't name a function | |
2216 | parameter. */ | |
2217 | unsigned pos = strtoul (m, const_cast<char**>(&end), 10); | |
2218 | if (acc.sizarg == UINT_MAX) | |
2219 | acc.sizarg = pos; | |
2220 | m = end; | |
2221 | } | |
2222 | } | |
2223 | while (*m == '$'); | |
2224 | } | |
2225 | ||
2226 | acc.end = m; | |
2227 | ||
2228 | bool existing; | |
2229 | auto &ref = rwm->get_or_insert (acc.ptrarg, &existing); | |
2230 | if (existing) | |
2231 | { | |
2232 | /* Merge the new spec with the existing. */ | |
2233 | if (acc.minsize == HOST_WIDE_INT_M1U) | |
2234 | ref.minsize = HOST_WIDE_INT_M1U; | |
2235 | ||
2236 | if (acc.sizarg != UINT_MAX) | |
2237 | ref.sizarg = acc.sizarg; | |
2238 | ||
2239 | if (acc.mode) | |
2240 | ref.mode = acc.mode; | |
b825a228 MS |
2241 | } |
2242 | else | |
6450f073 | 2243 | ref = acc; |
b825a228 MS |
2244 | |
2245 | /* Unconditionally add an entry for the required pointer | |
2246 | operand of the attribute, and one for the optional size | |
2247 | operand when it's specified. */ | |
b825a228 MS |
2248 | if (acc.sizarg != UINT_MAX) |
2249 | rwm->put (acc.sizarg, acc); | |
2250 | } | |
2251 | } | |
2252 | } | |
2253 | ||
6450f073 MS |
2254 | /* Return the access specification for a function parameter PARM |
2255 | or null if the current function has no such specification. */ | |
2256 | ||
2257 | attr_access * | |
2258 | get_parm_access (rdwr_map &rdwr_idx, tree parm, | |
2259 | tree fndecl /* = current_function_decl */) | |
2260 | { | |
2261 | tree fntype = TREE_TYPE (fndecl); | |
2262 | init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype)); | |
2263 | ||
2264 | if (rdwr_idx.is_empty ()) | |
2265 | return NULL; | |
2266 | ||
2267 | unsigned argpos = 0; | |
2268 | tree fnargs = DECL_ARGUMENTS (fndecl); | |
2269 | for (tree arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos) | |
2270 | if (arg == parm) | |
2271 | return rdwr_idx.get (argpos); | |
2272 | ||
2273 | return NULL; | |
2274 | } | |
2275 | ||
2276 | /* Return the internal representation as STRING_CST. Internal positional | |
2277 | arguments are zero-based. */ | |
2278 | ||
2279 | tree | |
2280 | attr_access::to_internal_string () const | |
2281 | { | |
2282 | return build_string (end - str, str); | |
2283 | } | |
2284 | ||
2285 | /* Return the human-readable representation of the external attribute | |
2286 | specification (as it might appear in the source code) as STRING_CST. | |
2287 | External positional arguments are one-based. */ | |
2288 | ||
2289 | tree | |
2290 | attr_access::to_external_string () const | |
2291 | { | |
2292 | char buf[80]; | |
2293 | gcc_assert (mode != access_deferred); | |
2294 | int len = snprintf (buf, sizeof buf, "access (%s, %u", | |
2295 | mode_names[mode], ptrarg + 1); | |
2296 | if (sizarg != UINT_MAX) | |
2297 | len += snprintf (buf + len, sizeof buf - len, ", %u", sizarg + 1); | |
2298 | strcpy (buf + len, ")"); | |
2299 | return build_string (len + 2, buf); | |
2300 | } | |
2301 | ||
2302 | /* Return the number of specified VLA bounds and set *nunspec to | |
2303 | the number of unspecified ones (those designated by [*]). */ | |
2304 | ||
2305 | unsigned | |
2306 | attr_access::vla_bounds (unsigned *nunspec) const | |
2307 | { | |
c6503fa9 | 2308 | unsigned nbounds = 0; |
6450f073 | 2309 | *nunspec = 0; |
c6503fa9 MS |
2310 | /* STR points to the beginning of the specified string for the current |
2311 | argument that may be followed by the string for the next argument. */ | |
2312 | for (const char* p = strchr (str, ']'); p && *p != '['; --p) | |
2313 | { | |
2314 | if (*p == '*') | |
2315 | ++*nunspec; | |
2316 | else if (*p == '$') | |
2317 | ++nbounds; | |
2318 | } | |
2319 | return nbounds; | |
6450f073 MS |
2320 | } |
2321 | ||
0718336a MS |
2322 | /* Reset front end-specific attribute access data from ATTRS. |
2323 | Called from the free_lang_data pass. */ | |
2324 | ||
2325 | /* static */ void | |
2326 | attr_access::free_lang_data (tree attrs) | |
2327 | { | |
2328 | for (tree acs = attrs; (acs = lookup_attribute ("access", acs)); | |
2329 | acs = TREE_CHAIN (acs)) | |
2330 | { | |
2331 | tree vblist = TREE_VALUE (acs); | |
2332 | vblist = TREE_CHAIN (vblist); | |
2333 | if (!vblist) | |
2334 | continue; | |
2335 | ||
0718336a MS |
2336 | for (vblist = TREE_VALUE (vblist); vblist; vblist = TREE_CHAIN (vblist)) |
2337 | { | |
2338 | tree *pvbnd = &TREE_VALUE (vblist); | |
2339 | if (!*pvbnd || DECL_P (*pvbnd)) | |
2340 | continue; | |
2341 | ||
2342 | /* VLA bounds that are expressions as opposed to DECLs are | |
2343 | only used in the front end. Reset them to keep front end | |
2344 | trees leaking into the middle end (see pr97172) and to | |
2345 | free up memory. */ | |
2346 | *pvbnd = NULL_TREE; | |
2347 | } | |
2348 | } | |
ea5a82df MS |
2349 | |
2350 | for (tree argspec = attrs; (argspec = lookup_attribute ("arg spec", argspec)); | |
2351 | argspec = TREE_CHAIN (argspec)) | |
2352 | { | |
2353 | /* Same as above. */ | |
2354 | tree *pvblist = &TREE_VALUE (argspec); | |
2355 | *pvblist = NULL_TREE; | |
2356 | } | |
0718336a | 2357 | } |
6450f073 MS |
2358 | |
2359 | /* Defined in attr_access. */ | |
2360 | constexpr char attr_access::mode_chars[]; | |
2361 | constexpr char attr_access::mode_names[][11]; | |
2362 | ||
2363 | /* Format an array, including a VLA, pointed to by TYPE and used as | |
2364 | a function parameter as a human-readable string. ACC describes | |
2365 | an access to the parameter and is used to determine the outermost | |
2366 | form of the array including its bound which is otherwise obviated | |
2367 | by its decay to pointer. Return the formatted string. */ | |
2368 | ||
2369 | std::string | |
2370 | attr_access::array_as_string (tree type) const | |
2371 | { | |
2372 | std::string typstr; | |
2373 | ||
2374 | if (type == error_mark_node) | |
2375 | return std::string (); | |
2376 | ||
2377 | if (this->str) | |
2378 | { | |
e808f3fd MS |
2379 | /* For array parameters (but not pointers) create a temporary array |
2380 | type that corresponds to the form of the parameter including its | |
6450f073 MS |
2381 | qualifiers even though they apply to the pointer, not the array |
2382 | type. */ | |
2383 | const bool vla_p = minsize == HOST_WIDE_INT_M1U; | |
2384 | tree eltype = TREE_TYPE (type); | |
6450f073 | 2385 | tree index_type = NULL_TREE; |
e808f3fd | 2386 | |
6450f073 MS |
2387 | if (minsize == HOST_WIDE_INT_M1U) |
2388 | { | |
2389 | /* Determine if this is a VLA (an array whose most significant | |
2390 | bound is nonconstant and whose access string has "$]" in it) | |
2391 | extract the bound expression from SIZE. */ | |
2392 | const char *p = end; | |
7dbc7ad5 | 2393 | for ( ; p != str && *p-- != ']'; ); |
6450f073 | 2394 | if (*p == '$') |
2c8bffa1 MS |
2395 | /* SIZE may have been cleared. Use it with care. */ |
2396 | index_type = build_index_type (size ? TREE_VALUE (size) : size); | |
6450f073 | 2397 | } |
7dbc7ad5 | 2398 | else if (minsize) |
6450f073 MS |
2399 | index_type = build_index_type (size_int (minsize - 1)); |
2400 | ||
e808f3fd | 2401 | tree arat = NULL_TREE; |
6450f073 MS |
2402 | if (static_p || vla_p) |
2403 | { | |
2404 | tree flag = static_p ? integer_one_node : NULL_TREE; | |
2405 | /* Hack: there's no language-independent way to encode | |
2406 | the "static" specifier or the "*" notation in an array type. | |
e808f3fd MS |
2407 | Add a "fake" attribute to have the pretty-printer add "static" |
2408 | or "*". The "[static N]" notation is only valid in the most | |
2409 | significant bound but [*] can be used for any bound. Because | |
2410 | [*] is represented the same as [0] this hack only works for | |
2411 | the most significant bound like static and the others are | |
2412 | rendered as [0]. */ | |
2413 | arat = build_tree_list (get_identifier ("array"), flag); | |
6450f073 MS |
2414 | } |
2415 | ||
e808f3fd MS |
2416 | const int quals = TYPE_QUALS (type); |
2417 | type = build_array_type (eltype, index_type); | |
2418 | type = build_type_attribute_qual_variant (type, arat, quals); | |
6450f073 MS |
2419 | } |
2420 | ||
2421 | /* Format the type using the current pretty printer. The generic tree | |
2422 | printer does a terrible job. */ | |
2423 | pretty_printer *pp = global_dc->printer->clone (); | |
2424 | pp_printf (pp, "%qT", type); | |
2425 | typstr = pp_formatted_text (pp); | |
2426 | delete pp; | |
2427 | ||
6450f073 MS |
2428 | return typstr; |
2429 | } | |
79a2c428 | 2430 | |
5d9ae53d MS |
2431 | #if CHECKING_P |
2432 | ||
2433 | namespace selftest | |
2434 | { | |
2435 | ||
2436 | /* Helper types to verify the consistency attribute exclusions. */ | |
2437 | ||
2438 | typedef std::pair<const char *, const char *> excl_pair; | |
2439 | ||
2440 | struct excl_hash_traits: typed_noop_remove<excl_pair> | |
2441 | { | |
2442 | typedef excl_pair value_type; | |
2443 | typedef value_type compare_type; | |
2444 | ||
2445 | static hashval_t hash (const value_type &x) | |
2446 | { | |
2447 | hashval_t h1 = htab_hash_string (x.first); | |
2448 | hashval_t h2 = htab_hash_string (x.second); | |
2449 | return h1 ^ h2; | |
2450 | } | |
2451 | ||
2452 | static bool equal (const value_type &x, const value_type &y) | |
2453 | { | |
2454 | return !strcmp (x.first, y.first) && !strcmp (x.second, y.second); | |
2455 | } | |
2456 | ||
2457 | static void mark_deleted (value_type &x) | |
2458 | { | |
2459 | x = value_type (NULL, NULL); | |
2460 | } | |
2461 | ||
7ca50de0 DM |
2462 | static const bool empty_zero_p = false; |
2463 | ||
5d9ae53d MS |
2464 | static void mark_empty (value_type &x) |
2465 | { | |
2466 | x = value_type ("", ""); | |
2467 | } | |
2468 | ||
2469 | static bool is_deleted (const value_type &x) | |
2470 | { | |
2471 | return !x.first && !x.second; | |
2472 | } | |
2473 | ||
2474 | static bool is_empty (const value_type &x) | |
2475 | { | |
2476 | return !*x.first && !*x.second; | |
2477 | } | |
2478 | }; | |
2479 | ||
2480 | ||
2481 | /* Self-test to verify that each attribute exclusion is symmetric, | |
2482 | meaning that if attribute A is encoded as incompatible with | |
2483 | attribute B then the opposite relationship is also encoded. | |
2484 | This test also detects most cases of misspelled attribute names | |
2485 | in exclusions. */ | |
2486 | ||
2487 | static void | |
2488 | test_attribute_exclusions () | |
2489 | { | |
2490 | /* Iterate over the array of attribute tables first (with TI0 as | |
2491 | the index) and over the array of attribute_spec in each table | |
2492 | (with SI0 as the index). */ | |
2493 | const size_t ntables = ARRAY_SIZE (attribute_tables); | |
2494 | ||
2495 | /* Set of pairs of mutually exclusive attributes. */ | |
36a3a7a3 | 2496 | typedef hash_set<excl_pair, false, excl_hash_traits> exclusion_set; |
5d9ae53d MS |
2497 | exclusion_set excl_set; |
2498 | ||
2499 | for (size_t ti0 = 0; ti0 != ntables; ++ti0) | |
2500 | for (size_t s0 = 0; attribute_tables[ti0][s0].name; ++s0) | |
2501 | { | |
2502 | const attribute_spec::exclusions *excl | |
2503 | = attribute_tables[ti0][s0].exclude; | |
2504 | ||
2505 | /* Skip each attribute that doesn't define exclusions. */ | |
2506 | if (!excl) | |
2507 | continue; | |
2508 | ||
2509 | const char *attr_name = attribute_tables[ti0][s0].name; | |
2510 | ||
2511 | /* Iterate over the set of exclusions for every attribute | |
2512 | (with EI0 as the index) adding the exclusions defined | |
2513 | for each to the set. */ | |
2514 | for (size_t ei0 = 0; excl[ei0].name; ++ei0) | |
2515 | { | |
2516 | const char *excl_name = excl[ei0].name; | |
2517 | ||
2518 | if (!strcmp (attr_name, excl_name)) | |
2519 | continue; | |
2520 | ||
2521 | excl_set.add (excl_pair (attr_name, excl_name)); | |
2522 | } | |
2523 | } | |
2524 | ||
2525 | /* Traverse the set of mutually exclusive pairs of attributes | |
2526 | and verify that they are symmetric. */ | |
2527 | for (exclusion_set::iterator it = excl_set.begin (); | |
2528 | it != excl_set.end (); | |
2529 | ++it) | |
2530 | { | |
2531 | if (!excl_set.contains (excl_pair ((*it).second, (*it).first))) | |
2532 | { | |
2533 | /* An exclusion for an attribute has been found that | |
2534 | doesn't have a corresponding exclusion in the opposite | |
2535 | direction. */ | |
2536 | char desc[120]; | |
2537 | sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric", | |
2538 | (*it).first, (*it).second); | |
2539 | fail (SELFTEST_LOCATION, desc); | |
2540 | } | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | void | |
2545 | attribute_c_tests () | |
2546 | { | |
2547 | test_attribute_exclusions (); | |
2548 | } | |
2549 | ||
2550 | } /* namespace selftest */ | |
2551 | ||
2552 | #endif /* CHECKING_P */ |