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