openmp: Implement OpenMP 5.0 base-pointer attachement and clause ordering
[gcc.git] / gcc / attr-fnspec.h
1 /* Handling of fnspec attribute specifiers
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
3 Contributed by Richard Guenther <rguenther@suse.de>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* Parse string of attribute "fn spec". This is an internal attribute
22 describing side effects of a function as follows:
23
24 character 0 specifies properties of return values as follows:
25 '1'...'4' specifies number of argument function returns (as in memset)
26 'm' specifies that returned value is noalias (as in malloc)
27 '.' specifies that nothing is known.
28 character 1 specifies additional function properties
29 ' ' specifies that nothing is known
30 'p' or 'P' specifies that function is pure except for described side
31 effects.
32 'c' or 'C' specifies that function is const except for described side
33 effects.
34 The uppercase letter in addition specifies that function clobbers errno.
35
36 character 2+2i specifies properties of argument number i as follows:
37 'x' or 'X' specifies that parameter is unused.
38 'r' or 'R' specifies that the memory pointed to by the parameter is only
39 read and does not escape
40 'o' or 'O' specifies that the memory pointed to by the parameter is only
41 written and does not escape
42 'w' or 'W' specifies that the memory pointed to by the parameter does not
43 escape
44 '.' specifies that nothing is known.
45 The uppercase letter in addition specifies that the memory pointed to
46 by the parameter is not dereferenced. For 'r' only read applies
47 transitively to pointers read from the pointed-to memory.
48
49 character 3+2i specifies additional properties of argument number i
50 as follows:
51 ' ' nothing is known
52 't' the size of value written/read corresponds to the size of
53 of the pointed-to type of the argument type
54 '1'...'9' the size of value written/read is given by the specified
55 argument
56 */
57
58 #ifndef ATTR_FNSPEC_H
59 #define ATTR_FNSPEC_H
60
61 class attr_fnspec
62 {
63 private:
64 /* fn spec attribute string. */
65 const char *str;
66 /* length of the fn spec string. */
67 const unsigned len;
68 /* Number of characters specifying return value. */
69 const unsigned int return_desc_size = 2;
70 /* Number of characters specifying size. */
71 const unsigned int arg_desc_size = 2;
72
73 /* Return start of specifier of arg i. */
74 unsigned int arg_idx (int i)
75 {
76 return return_desc_size + arg_desc_size * i;
77 }
78
79 public:
80 attr_fnspec (const char *str, unsigned len)
81 : str (str), len (len)
82 {
83 if (flag_checking)
84 verify ();
85 }
86 attr_fnspec (const char *str)
87 : str (str), len (strlen (str))
88 {
89 if (flag_checking)
90 verify ();
91 }
92 attr_fnspec (const_tree identifier)
93 : str (TREE_STRING_POINTER (identifier)),
94 len (TREE_STRING_LENGTH (identifier))
95 {
96 if (flag_checking)
97 verify ();
98 }
99 attr_fnspec ()
100 : str (NULL), len (0)
101 {
102 }
103
104 /* Return true if fn spec is known. */
105 bool
106 known_p ()
107 {
108 return len;
109 }
110
111 /* Return true if arg I is specified. */
112 bool
113 arg_specified_p (unsigned int i)
114 {
115 return len >= arg_idx (i + 1);
116 }
117
118 /* True if the argument is not dereferenced recursively, thus only
119 directly reachable memory is read or written. */
120 bool
121 arg_direct_p (unsigned int i)
122 {
123 unsigned int idx = arg_idx (i);
124 gcc_checking_assert (arg_specified_p (i));
125 return str[idx] == 'R' || str[idx] == 'O' || str[idx] == 'W';
126 }
127
128 /* True if argument is used. */
129 bool
130 arg_used_p (unsigned int i)
131 {
132 unsigned int idx = arg_idx (i);
133 gcc_checking_assert (arg_specified_p (i));
134 return str[idx] != 'x' && str[idx] != 'X';
135 }
136
137 /* True if memory reached by the argument is readonly (not clobbered). */
138 bool
139 arg_readonly_p (unsigned int i)
140 {
141 unsigned int idx = arg_idx (i);
142 gcc_checking_assert (arg_specified_p (i));
143 return str[idx] == 'r' || str[idx] == 'R';
144 }
145
146 /* True if memory reached by the argument is read (directly or indirectly) */
147 bool
148 arg_maybe_read_p (unsigned int i)
149 {
150 unsigned int idx = arg_idx (i);
151 gcc_checking_assert (arg_specified_p (i));
152 return str[idx] != 'o' && str[idx] != 'O'
153 && str[idx] != 'x' && str[idx] != 'X';
154 }
155
156 /* True if memory reached by the argument is written.
157 (directly or indirectly) */
158 bool
159 arg_maybe_written_p (unsigned int i)
160 {
161 unsigned int idx = arg_idx (i);
162 gcc_checking_assert (arg_specified_p (i));
163 return str[idx] != 'r' && str[idx] != 'R'
164 && str[idx] != 'x' && str[idx] != 'X';
165 }
166
167 /* Return true if load of memory pointed to by argument I is specified
168 by another argument. In this case set ARG. */
169 bool
170 arg_max_access_size_given_by_arg_p (unsigned int i, unsigned int *arg)
171 {
172 unsigned int idx = arg_idx (i);
173 gcc_checking_assert (arg_specified_p (i));
174 if (str[idx + 1] >= '1' && str[idx + 1] <= '9')
175 {
176 *arg = str[idx + 1] - '1';
177 return true;
178 }
179 else
180 return false;
181 }
182
183 /* Return true if the pointed-to type of the argument correspond to the
184 size of the memory acccess. */
185 bool
186 arg_access_size_given_by_type_p (unsigned int i)
187 {
188 unsigned int idx = arg_idx (i);
189 gcc_checking_assert (arg_specified_p (i));
190 return str[idx + 1] == 't';
191 }
192
193 /* True if the argument does not escape. */
194 bool
195 arg_noescape_p (unsigned int i)
196 {
197 unsigned int idx = arg_idx (i);
198 gcc_checking_assert (arg_specified_p (i));
199 return str[idx] == 'w' || str[idx] == 'W'
200 || str[idx] == 'r' || str[idx] == 'R'
201 || str[idx] == 'o' || str[idx] == 'O';
202 }
203
204 /* Return true if function returns value of its parameter. If ARG_NO is
205 non-NULL return initialize it to the argument returned. */
206 bool
207 returns_arg (unsigned int *arg_no)
208 {
209 if (str[0] >= '1' && str[0] <= '4')
210 {
211 if (arg_no)
212 *arg_no = str[0] - '1';
213 return true;
214 }
215 return false;
216 }
217
218 /* Nonzero if the return value does not alias with anything. Functions
219 with the malloc attribute have this set on their return value. */
220 bool
221 returns_noalias_p ()
222 {
223 return str[0] == 'm';
224 }
225
226 /* Return true if all memory read by the function is specified by fnspec. */
227 bool
228 global_memory_read_p ()
229 {
230 return str[1] != 'c' && str[1] != 'C';
231 }
232
233 /* Return true if all memory written by the function
234 is specified by fnspec. */
235 bool
236 global_memory_written_p ()
237 {
238 return str[1] != 'c' && str[1] != 'C' && str[1] != 'p' && str[1] != 'P';
239 }
240
241 bool
242 errno_maybe_written_p ()
243 {
244 return str[1] == 'C' || str[1] == 'P';
245 }
246
247 /* Check validity of the string. */
248 void verify ();
249
250 /* Return the fnspec string. */
251 const char *
252 get_str ()
253 {
254 return str;
255 }
256 };
257
258 extern attr_fnspec gimple_call_fnspec (const gcall *stmt);
259 extern attr_fnspec builtin_fnspec (tree);
260
261 #endif /* ATTR_FNSPEC_H */