1*38fd1498Szrj /* Declarations and definitions dealing with attribute handling.
2*38fd1498Szrj Copyright (C) 2013-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj #ifndef GCC_ATTRIBS_H
21*38fd1498Szrj #define GCC_ATTRIBS_H
22*38fd1498Szrj
23*38fd1498Szrj extern const struct attribute_spec *lookup_attribute_spec (const_tree);
24*38fd1498Szrj extern void init_attributes (void);
25*38fd1498Szrj
26*38fd1498Szrj /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
27*38fd1498Szrj which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
28*38fd1498Szrj it should be modified in place; if a TYPE, a copy should be created
29*38fd1498Szrj unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
30*38fd1498Szrj information, in the form of a bitwise OR of flags in enum attribute_flags
31*38fd1498Szrj from tree.h. Depending on these flags, some attributes may be
32*38fd1498Szrj returned to be applied at a later stage (for example, to apply
33*38fd1498Szrj a decl attribute to the declaration rather than to its type). */
34*38fd1498Szrj extern tree decl_attributes (tree *, tree, int, tree = NULL_TREE);
35*38fd1498Szrj
36*38fd1498Szrj extern bool cxx11_attribute_p (const_tree);
37*38fd1498Szrj extern tree get_attribute_name (const_tree);
38*38fd1498Szrj extern void apply_tm_attr (tree, tree);
39*38fd1498Szrj extern tree make_attribute (const char *, const char *, tree);
40*38fd1498Szrj
41*38fd1498Szrj extern struct scoped_attributes* register_scoped_attributes (const struct attribute_spec *,
42*38fd1498Szrj const char *);
43*38fd1498Szrj
44*38fd1498Szrj extern char *sorted_attr_string (tree);
45*38fd1498Szrj extern bool common_function_versions (tree, tree);
46*38fd1498Szrj extern char *make_unique_name (tree, const char *, bool);
47*38fd1498Szrj extern tree make_dispatcher_decl (const tree);
48*38fd1498Szrj extern bool is_function_default_version (const tree);
49*38fd1498Szrj
50*38fd1498Szrj /* Return a type like TTYPE except that its TYPE_ATTRIBUTES
51*38fd1498Szrj is ATTRIBUTE.
52*38fd1498Szrj
53*38fd1498Szrj Such modified types already made are recorded so that duplicates
54*38fd1498Szrj are not made. */
55*38fd1498Szrj
56*38fd1498Szrj extern tree build_type_attribute_variant (tree, tree);
57*38fd1498Szrj extern tree build_decl_attribute_variant (tree, tree);
58*38fd1498Szrj extern tree build_type_attribute_qual_variant (tree, tree, int);
59*38fd1498Szrj
60*38fd1498Szrj extern bool attribute_value_equal (const_tree, const_tree);
61*38fd1498Szrj
62*38fd1498Szrj /* Return 0 if the attributes for two types are incompatible, 1 if they
63*38fd1498Szrj are compatible, and 2 if they are nearly compatible (which causes a
64*38fd1498Szrj warning to be generated). */
65*38fd1498Szrj extern int comp_type_attributes (const_tree, const_tree);
66*38fd1498Szrj
67*38fd1498Szrj /* Default versions of target-overridable functions. */
68*38fd1498Szrj extern tree merge_decl_attributes (tree, tree);
69*38fd1498Szrj extern tree merge_type_attributes (tree, tree);
70*38fd1498Szrj
71*38fd1498Szrj /* Remove any instances of attribute ATTR_NAME in LIST and return the
72*38fd1498Szrj modified list. */
73*38fd1498Szrj
74*38fd1498Szrj extern tree remove_attribute (const char *, tree);
75*38fd1498Szrj
76*38fd1498Szrj /* Given two attributes lists, return a list of their union. */
77*38fd1498Szrj
78*38fd1498Szrj extern tree merge_attributes (tree, tree);
79*38fd1498Szrj
80*38fd1498Szrj /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
81*38fd1498Szrj they are missing there. */
82*38fd1498Szrj
83*38fd1498Szrj extern void duplicate_one_attribute (tree *, tree, const char *);
84*38fd1498Szrj
85*38fd1498Szrj /* Duplicate all attributes from user DECL to the corresponding
86*38fd1498Szrj builtin that should be propagated. */
87*38fd1498Szrj
88*38fd1498Szrj extern void copy_attributes_to_builtin (tree);
89*38fd1498Szrj
90*38fd1498Szrj /* Given two Windows decl attributes lists, possibly including
91*38fd1498Szrj dllimport, return a list of their union . */
92*38fd1498Szrj extern tree merge_dllimport_decl_attributes (tree, tree);
93*38fd1498Szrj
94*38fd1498Szrj /* Handle a "dllimport" or "dllexport" attribute. */
95*38fd1498Szrj extern tree handle_dll_attribute (tree *, tree, tree, int, bool *);
96*38fd1498Szrj
97*38fd1498Szrj extern int attribute_list_equal (const_tree, const_tree);
98*38fd1498Szrj extern int attribute_list_contained (const_tree, const_tree);
99*38fd1498Szrj
100*38fd1498Szrj /* The backbone of lookup_attribute(). ATTR_LEN is the string length
101*38fd1498Szrj of ATTR_NAME, and LIST is not NULL_TREE.
102*38fd1498Szrj
103*38fd1498Szrj The function is called from lookup_attribute in order to optimize
104*38fd1498Szrj for size. */
105*38fd1498Szrj extern tree private_lookup_attribute (const char *attr_name, size_t attr_len,
106*38fd1498Szrj tree list);
107*38fd1498Szrj
108*38fd1498Szrj /* For a given IDENTIFIER_NODE, strip leading and trailing '_' characters
109*38fd1498Szrj so that we have a canonical form of attribute names. */
110*38fd1498Szrj
111*38fd1498Szrj static inline tree
canonicalize_attr_name(tree attr_name)112*38fd1498Szrj canonicalize_attr_name (tree attr_name)
113*38fd1498Szrj {
114*38fd1498Szrj const size_t l = IDENTIFIER_LENGTH (attr_name);
115*38fd1498Szrj const char *s = IDENTIFIER_POINTER (attr_name);
116*38fd1498Szrj
117*38fd1498Szrj if (l > 4 && s[0] == '_' && s[1] == '_' && s[l - 1] == '_' && s[l - 2] == '_')
118*38fd1498Szrj return get_identifier_with_length (s + 2, l - 4);
119*38fd1498Szrj
120*38fd1498Szrj return attr_name;
121*38fd1498Szrj }
122*38fd1498Szrj
123*38fd1498Szrj /* Compare attribute identifiers ATTR1 and ATTR2 with length ATTR1_LEN and
124*38fd1498Szrj ATTR2_LEN. */
125*38fd1498Szrj
126*38fd1498Szrj static inline bool
cmp_attribs(const char * attr1,size_t attr1_len,const char * attr2,size_t attr2_len)127*38fd1498Szrj cmp_attribs (const char *attr1, size_t attr1_len,
128*38fd1498Szrj const char *attr2, size_t attr2_len)
129*38fd1498Szrj {
130*38fd1498Szrj return attr1_len == attr2_len && strncmp (attr1, attr2, attr1_len) == 0;
131*38fd1498Szrj }
132*38fd1498Szrj
133*38fd1498Szrj /* Compare attribute identifiers ATTR1 and ATTR2. */
134*38fd1498Szrj
135*38fd1498Szrj static inline bool
cmp_attribs(const char * attr1,const char * attr2)136*38fd1498Szrj cmp_attribs (const char *attr1, const char *attr2)
137*38fd1498Szrj {
138*38fd1498Szrj return cmp_attribs (attr1, strlen (attr1), attr2, strlen (attr2));
139*38fd1498Szrj }
140*38fd1498Szrj
141*38fd1498Szrj /* Given an identifier node IDENT and a string ATTR_NAME, return true
142*38fd1498Szrj if the identifier node is a valid attribute name for the string. */
143*38fd1498Szrj
144*38fd1498Szrj static inline bool
is_attribute_p(const char * attr_name,const_tree ident)145*38fd1498Szrj is_attribute_p (const char *attr_name, const_tree ident)
146*38fd1498Szrj {
147*38fd1498Szrj return cmp_attribs (attr_name, strlen (attr_name),
148*38fd1498Szrj IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident));
149*38fd1498Szrj }
150*38fd1498Szrj
151*38fd1498Szrj /* Given an attribute name ATTR_NAME and a list of attributes LIST,
152*38fd1498Szrj return a pointer to the attribute's list element if the attribute
153*38fd1498Szrj is part of the list, or NULL_TREE if not found. If the attribute
154*38fd1498Szrj appears more than once, this only returns the first occurrence; the
155*38fd1498Szrj TREE_CHAIN of the return value should be passed back in if further
156*38fd1498Szrj occurrences are wanted. ATTR_NAME must be in the form 'text' (not
157*38fd1498Szrj '__text__'). */
158*38fd1498Szrj
159*38fd1498Szrj static inline tree
lookup_attribute(const char * attr_name,tree list)160*38fd1498Szrj lookup_attribute (const char *attr_name, tree list)
161*38fd1498Szrj {
162*38fd1498Szrj gcc_checking_assert (attr_name[0] != '_');
163*38fd1498Szrj /* In most cases, list is NULL_TREE. */
164*38fd1498Szrj if (list == NULL_TREE)
165*38fd1498Szrj return NULL_TREE;
166*38fd1498Szrj else
167*38fd1498Szrj {
168*38fd1498Szrj size_t attr_len = strlen (attr_name);
169*38fd1498Szrj /* Do the strlen() before calling the out-of-line implementation.
170*38fd1498Szrj In most cases attr_name is a string constant, and the compiler
171*38fd1498Szrj will optimize the strlen() away. */
172*38fd1498Szrj return private_lookup_attribute (attr_name, attr_len, list);
173*38fd1498Szrj }
174*38fd1498Szrj }
175*38fd1498Szrj
176*38fd1498Szrj /* Given an attribute name ATTR_NAME and a list of attributes LIST,
177*38fd1498Szrj return a pointer to the attribute's list first element if the attribute
178*38fd1498Szrj starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
179*38fd1498Szrj '__text__'). */
180*38fd1498Szrj
181*38fd1498Szrj static inline tree
lookup_attribute_by_prefix(const char * attr_name,tree list)182*38fd1498Szrj lookup_attribute_by_prefix (const char *attr_name, tree list)
183*38fd1498Szrj {
184*38fd1498Szrj gcc_checking_assert (attr_name[0] != '_');
185*38fd1498Szrj /* In most cases, list is NULL_TREE. */
186*38fd1498Szrj if (list == NULL_TREE)
187*38fd1498Szrj return NULL_TREE;
188*38fd1498Szrj else
189*38fd1498Szrj {
190*38fd1498Szrj size_t attr_len = strlen (attr_name);
191*38fd1498Szrj while (list)
192*38fd1498Szrj {
193*38fd1498Szrj size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
194*38fd1498Szrj
195*38fd1498Szrj if (attr_len > ident_len)
196*38fd1498Szrj {
197*38fd1498Szrj list = TREE_CHAIN (list);
198*38fd1498Szrj continue;
199*38fd1498Szrj }
200*38fd1498Szrj
201*38fd1498Szrj const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
202*38fd1498Szrj gcc_checking_assert (attr_len == 0 || p[0] != '_');
203*38fd1498Szrj
204*38fd1498Szrj if (strncmp (attr_name, p, attr_len) == 0)
205*38fd1498Szrj break;
206*38fd1498Szrj
207*38fd1498Szrj list = TREE_CHAIN (list);
208*38fd1498Szrj }
209*38fd1498Szrj
210*38fd1498Szrj return list;
211*38fd1498Szrj }
212*38fd1498Szrj }
213*38fd1498Szrj
214*38fd1498Szrj #endif // GCC_ATTRIBS_H
215