]>
Commit | Line | Data |
---|---|---|
d8a2d370 | 1 | /* Declarations and definitions dealing with attribute handling. |
99dee823 | 2 | Copyright (C) 2013-2021 Free Software Foundation, Inc. |
d8a2d370 DN |
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 | |
8 | Software Foundation; either version 3, or (at your option) any later | |
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 | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #ifndef GCC_ATTRIBS_H | |
21 | #define GCC_ATTRIBS_H | |
22 | ||
23 | extern const struct attribute_spec *lookup_attribute_spec (const_tree); | |
a1ad0d84 | 24 | extern void free_attr_data (); |
d8a2d370 DN |
25 | extern void init_attributes (void); |
26 | ||
27 | /* Process the attributes listed in ATTRIBUTES and install them in *NODE, | |
28 | which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, | |
29 | it should be modified in place; if a TYPE, a copy should be created | |
30 | unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further | |
31 | information, in the form of a bitwise OR of flags in enum attribute_flags | |
32 | from tree.h. Depending on these flags, some attributes may be | |
33 | returned to be applied at a later stage (for example, to apply | |
34 | a decl attribute to the declaration rather than to its type). */ | |
5d9ae53d | 35 | extern tree decl_attributes (tree *, tree, int, tree = NULL_TREE); |
d8a2d370 DN |
36 | |
37 | extern bool cxx11_attribute_p (const_tree); | |
38 | extern tree get_attribute_name (const_tree); | |
1bf32c11 | 39 | extern tree get_attribute_namespace (const_tree); |
d8a2d370 | 40 | extern void apply_tm_attr (tree, tree); |
3b1661a9 | 41 | extern tree make_attribute (const char *, const char *, tree); |
d8a2d370 | 42 | |
d067e05f | 43 | extern struct scoped_attributes* register_scoped_attributes (const struct attribute_spec *, |
a1ad0d84 MP |
44 | const char *, |
45 | bool = false); | |
d067e05f | 46 | |
1b062c1a MM |
47 | extern char *sorted_attr_string (tree); |
48 | extern bool common_function_versions (tree, tree); | |
1b062c1a MM |
49 | extern tree make_dispatcher_decl (const tree); |
50 | extern bool is_function_default_version (const tree); | |
a1ad0d84 | 51 | extern void handle_ignored_attributes_option (vec<char *> *); |
1b062c1a | 52 | |
314e6352 ML |
53 | /* Return a type like TTYPE except that its TYPE_ATTRIBUTES |
54 | is ATTRIBUTE. | |
55 | ||
56 | Such modified types already made are recorded so that duplicates | |
57 | are not made. */ | |
58 | ||
59 | extern tree build_type_attribute_variant (tree, tree); | |
60 | extern tree build_decl_attribute_variant (tree, tree); | |
61 | extern tree build_type_attribute_qual_variant (tree, tree, int); | |
62 | ||
63 | extern bool attribute_value_equal (const_tree, const_tree); | |
64 | ||
65 | /* Return 0 if the attributes for two types are incompatible, 1 if they | |
66 | are compatible, and 2 if they are nearly compatible (which causes a | |
67 | warning to be generated). */ | |
68 | extern int comp_type_attributes (const_tree, const_tree); | |
69 | ||
a3317f7b | 70 | extern tree affects_type_identity_attributes (tree, bool = true); |
1696fc1e | 71 | extern tree restrict_type_identity_attributes_to (tree, tree); |
a3317f7b | 72 | |
314e6352 ML |
73 | /* Default versions of target-overridable functions. */ |
74 | extern tree merge_decl_attributes (tree, tree); | |
75 | extern tree merge_type_attributes (tree, tree); | |
76 | ||
77 | /* Remove any instances of attribute ATTR_NAME in LIST and return the | |
78 | modified list. */ | |
79 | ||
80 | extern tree remove_attribute (const char *, tree); | |
81 | ||
82 | /* Given two attributes lists, return a list of their union. */ | |
83 | ||
84 | extern tree merge_attributes (tree, tree); | |
85 | ||
bc1a75dd JJ |
86 | /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if |
87 | they are missing there. */ | |
88 | ||
89 | extern void duplicate_one_attribute (tree *, tree, const char *); | |
90 | ||
91 | /* Duplicate all attributes from user DECL to the corresponding | |
92 | builtin that should be propagated. */ | |
93 | ||
94 | extern void copy_attributes_to_builtin (tree); | |
95 | ||
314e6352 ML |
96 | /* Given two Windows decl attributes lists, possibly including |
97 | dllimport, return a list of their union . */ | |
98 | extern tree merge_dllimport_decl_attributes (tree, tree); | |
99 | ||
100 | /* Handle a "dllimport" or "dllexport" attribute. */ | |
101 | extern tree handle_dll_attribute (tree *, tree, tree, int, bool *); | |
102 | ||
103 | extern int attribute_list_equal (const_tree, const_tree); | |
104 | extern int attribute_list_contained (const_tree, const_tree); | |
105 | ||
13bdca74 ML |
106 | /* The backbone of lookup_attribute(). ATTR_LEN is the string length |
107 | of ATTR_NAME, and LIST is not NULL_TREE. | |
108 | ||
109 | The function is called from lookup_attribute in order to optimize | |
110 | for size. */ | |
111 | extern tree private_lookup_attribute (const char *attr_name, size_t attr_len, | |
112 | tree list); | |
113 | ||
79a2c428 MS |
114 | extern unsigned decls_mismatched_attributes (tree, tree, tree, |
115 | const char* const[], | |
116 | pretty_printer*); | |
117 | ||
118 | extern void maybe_diag_alias_attributes (tree, tree); | |
119 | ||
a1ad0d84 MP |
120 | /* For a given string S of length L, strip leading and trailing '_' characters |
121 | so that we have a canonical form of attribute names. NB: This function may | |
122 | change S and L. */ | |
123 | ||
124 | template <typename T> | |
125 | inline bool | |
126 | canonicalize_attr_name (const char *&s, T &l) | |
127 | { | |
128 | if (l > 4 && s[0] == '_' && s[1] == '_' && s[l - 1] == '_' && s[l - 2] == '_') | |
129 | { | |
130 | s += 2; | |
131 | l -= 4; | |
132 | return true; | |
133 | } | |
134 | return false; | |
135 | } | |
136 | ||
577eec56 ML |
137 | /* For a given IDENTIFIER_NODE, strip leading and trailing '_' characters |
138 | so that we have a canonical form of attribute names. */ | |
139 | ||
140 | static inline tree | |
141 | canonicalize_attr_name (tree attr_name) | |
142 | { | |
a1ad0d84 | 143 | size_t l = IDENTIFIER_LENGTH (attr_name); |
577eec56 ML |
144 | const char *s = IDENTIFIER_POINTER (attr_name); |
145 | ||
a1ad0d84 MP |
146 | if (canonicalize_attr_name (s, l)) |
147 | return get_identifier_with_length (s, l); | |
577eec56 ML |
148 | |
149 | return attr_name; | |
150 | } | |
151 | ||
152 | /* Compare attribute identifiers ATTR1 and ATTR2 with length ATTR1_LEN and | |
153 | ATTR2_LEN. */ | |
154 | ||
155 | static inline bool | |
156 | cmp_attribs (const char *attr1, size_t attr1_len, | |
157 | const char *attr2, size_t attr2_len) | |
158 | { | |
159 | return attr1_len == attr2_len && strncmp (attr1, attr2, attr1_len) == 0; | |
160 | } | |
161 | ||
162 | /* Compare attribute identifiers ATTR1 and ATTR2. */ | |
163 | ||
164 | static inline bool | |
165 | cmp_attribs (const char *attr1, const char *attr2) | |
166 | { | |
167 | return cmp_attribs (attr1, strlen (attr1), attr2, strlen (attr2)); | |
168 | } | |
169 | ||
170 | /* Given an identifier node IDENT and a string ATTR_NAME, return true | |
171 | if the identifier node is a valid attribute name for the string. */ | |
172 | ||
173 | static inline bool | |
174 | is_attribute_p (const char *attr_name, const_tree ident) | |
175 | { | |
176 | return cmp_attribs (attr_name, strlen (attr_name), | |
177 | IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident)); | |
178 | } | |
179 | ||
314e6352 ML |
180 | /* Given an attribute name ATTR_NAME and a list of attributes LIST, |
181 | return a pointer to the attribute's list element if the attribute | |
182 | is part of the list, or NULL_TREE if not found. If the attribute | |
183 | appears more than once, this only returns the first occurrence; the | |
184 | TREE_CHAIN of the return value should be passed back in if further | |
185 | occurrences are wanted. ATTR_NAME must be in the form 'text' (not | |
186 | '__text__'). */ | |
187 | ||
188 | static inline tree | |
189 | lookup_attribute (const char *attr_name, tree list) | |
190 | { | |
191 | gcc_checking_assert (attr_name[0] != '_'); | |
192 | /* In most cases, list is NULL_TREE. */ | |
193 | if (list == NULL_TREE) | |
194 | return NULL_TREE; | |
195 | else | |
196 | { | |
197 | size_t attr_len = strlen (attr_name); | |
198 | /* Do the strlen() before calling the out-of-line implementation. | |
199 | In most cases attr_name is a string constant, and the compiler | |
200 | will optimize the strlen() away. */ | |
13bdca74 | 201 | return private_lookup_attribute (attr_name, attr_len, list); |
314e6352 ML |
202 | } |
203 | } | |
204 | ||
205 | /* Given an attribute name ATTR_NAME and a list of attributes LIST, | |
206 | return a pointer to the attribute's list first element if the attribute | |
207 | starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not | |
208 | '__text__'). */ | |
209 | ||
210 | static inline tree | |
211 | lookup_attribute_by_prefix (const char *attr_name, tree list) | |
212 | { | |
213 | gcc_checking_assert (attr_name[0] != '_'); | |
214 | /* In most cases, list is NULL_TREE. */ | |
215 | if (list == NULL_TREE) | |
216 | return NULL_TREE; | |
217 | else | |
218 | { | |
219 | size_t attr_len = strlen (attr_name); | |
220 | while (list) | |
221 | { | |
222 | size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list)); | |
223 | ||
224 | if (attr_len > ident_len) | |
225 | { | |
226 | list = TREE_CHAIN (list); | |
227 | continue; | |
228 | } | |
229 | ||
230 | const char *p = IDENTIFIER_POINTER (get_attribute_name (list)); | |
231 | gcc_checking_assert (attr_len == 0 || p[0] != '_'); | |
232 | ||
233 | if (strncmp (attr_name, p, attr_len) == 0) | |
234 | break; | |
235 | ||
236 | list = TREE_CHAIN (list); | |
237 | } | |
238 | ||
239 | return list; | |
240 | } | |
241 | } | |
242 | ||
54aa6b58 MS |
243 | /* Description of a function argument declared with attribute access. |
244 | Used as an "iterator" over all such arguments in a function declaration | |
245 | or call. */ | |
246 | ||
247 | struct attr_access | |
248 | { | |
6450f073 MS |
249 | /* The beginning and end of the internal string representation. */ |
250 | const char *str, *end; | |
54aa6b58 MS |
251 | /* The attribute pointer argument. */ |
252 | tree ptr; | |
6450f073 MS |
253 | /* For a declaration, a TREE_CHAIN of VLA bound expressions stored |
254 | in TREE_VALUE and their positions in the argument list (stored | |
255 | in TREE_PURPOSE). Each expression may be a PARM_DECL or some | |
256 | other DECL (for ordinary variables), or an EXPR for other | |
257 | expressions (e.g., funcion calls). */ | |
54aa6b58 MS |
258 | tree size; |
259 | ||
6450f073 MS |
260 | /* The zero-based position of each of the formal function arguments. |
261 | For the optional SIZARG, UINT_MAX when not specified. For VLAs | |
262 | with multiple variable bounds, SIZARG is the position corresponding | |
263 | to the most significant bound in the argument list. Positions of | |
264 | subsequent bounds are in the TREE_PURPOSE field of the SIZE chain. */ | |
54aa6b58 MS |
265 | unsigned ptrarg; |
266 | unsigned sizarg; | |
6450f073 MS |
267 | /* For internal specifications only, the constant minimum size of |
268 | the array, zero if not specified, and HWI_M1U for the unspecified | |
269 | VLA [*] notation. Meaningless for external (explicit) access | |
270 | specifications. */ | |
271 | unsigned HOST_WIDE_INT minsize; | |
54aa6b58 MS |
272 | |
273 | /* The access mode. */ | |
54aa6b58 | 274 | access_mode mode; |
6450f073 MS |
275 | |
276 | /* Set for an attribute added internally rather than by an explicit | |
277 | declaration. */ | |
278 | bool internal_p; | |
279 | /* Set for the T[static MINSIZE] array notation for nonzero MINSIZE | |
280 | less than HWI_M1U. */ | |
281 | bool static_p; | |
282 | ||
283 | /* Return the number of specified VLA bounds. */ | |
284 | unsigned vla_bounds (unsigned *) const; | |
285 | ||
286 | /* Return internal representation as STRING_CST. */ | |
287 | tree to_internal_string () const; | |
288 | ||
289 | /* Return the human-readable representation of the external attribute | |
290 | specification (as it might appear in the source code) as STRING_CST. */ | |
291 | tree to_external_string () const; | |
292 | ||
293 | /* Return argument of array type formatted as a readable string. */ | |
294 | std::string array_as_string (tree) const; | |
295 | ||
296 | /* Return the access mode corresponding to the character code. */ | |
297 | static access_mode from_mode_char (char); | |
298 | ||
0718336a MS |
299 | /* Reset front end-specific attribute access data from attributes. */ |
300 | static void free_lang_data (tree); | |
301 | ||
6450f073 MS |
302 | /* The character codes corresponding to all the access modes. */ |
303 | static constexpr char mode_chars[5] = { '-', 'r', 'w', 'x', '^' }; | |
304 | ||
305 | /* The strings corresponding to just the external access modes. */ | |
306 | static constexpr char mode_names[4][11] = | |
307 | { | |
308 | "none", "read_only", "write_only", "read_write" | |
309 | }; | |
54aa6b58 MS |
310 | }; |
311 | ||
6450f073 MS |
312 | inline access_mode |
313 | attr_access::from_mode_char (char c) | |
314 | { | |
315 | switch (c) | |
316 | { | |
317 | case mode_chars[access_none]: return access_none; | |
318 | case mode_chars[access_read_only]: return access_read_only; | |
319 | case mode_chars[access_write_only]: return access_write_only; | |
320 | case mode_chars[access_read_write]: return access_read_write; | |
321 | case mode_chars[access_deferred]: return access_deferred; | |
322 | } | |
323 | gcc_unreachable (); | |
324 | } | |
325 | ||
b825a228 MS |
326 | /* Used to define rdwr_map below. */ |
327 | struct rdwr_access_hash: int_hash<int, -1> { }; | |
328 | ||
329 | /* A mapping between argument number corresponding to attribute access | |
330 | mode (read_only, write_only, or read_write) and operands. */ | |
331 | struct attr_access; | |
332 | typedef hash_map<rdwr_access_hash, attr_access> rdwr_map; | |
333 | ||
334 | extern void init_attr_rdwr_indices (rdwr_map *, tree); | |
6450f073 MS |
335 | extern attr_access *get_parm_access (rdwr_map &, tree, |
336 | tree = current_function_decl); | |
b825a228 | 337 | |
d8a2d370 | 338 | #endif // GCC_ATTRIBS_H |