1 /* Declarations and definitions dealing with attribute handling.
2    Copyright (C) 2013-2019 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 void apply_tm_attr (tree, tree);
39 extern tree make_attribute (const char *, const char *, tree);
40 
41 extern struct scoped_attributes* register_scoped_attributes (const struct attribute_spec *,
42 							     const char *);
43 
44 extern char *sorted_attr_string (tree);
45 extern bool common_function_versions (tree, tree);
46 extern char *make_unique_name (tree, const char *, bool);
47 extern tree make_dispatcher_decl (const tree);
48 extern bool is_function_default_version (const tree);
49 
50 /* Return a type like TTYPE except that its TYPE_ATTRIBUTES
51    is ATTRIBUTE.
52 
53    Such modified types already made are recorded so that duplicates
54    are not made.  */
55 
56 extern tree build_type_attribute_variant (tree, tree);
57 extern tree build_decl_attribute_variant (tree, tree);
58 extern tree build_type_attribute_qual_variant (tree, tree, int);
59 
60 extern bool attribute_value_equal (const_tree, const_tree);
61 
62 /* Return 0 if the attributes for two types are incompatible, 1 if they
63    are compatible, and 2 if they are nearly compatible (which causes a
64    warning to be generated).  */
65 extern int comp_type_attributes (const_tree, const_tree);
66 
67 /* Default versions of target-overridable functions.  */
68 extern tree merge_decl_attributes (tree, tree);
69 extern tree merge_type_attributes (tree, tree);
70 
71 /* Remove any instances of attribute ATTR_NAME in LIST and return the
72    modified list.  */
73 
74 extern tree remove_attribute (const char *, tree);
75 
76 /* Given two attributes lists, return a list of their union.  */
77 
78 extern tree merge_attributes (tree, tree);
79 
80 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
81    they are missing there.  */
82 
83 extern void duplicate_one_attribute (tree *, tree, const char *);
84 
85 /* Duplicate all attributes from user DECL to the corresponding
86    builtin that should be propagated.  */
87 
88 extern void copy_attributes_to_builtin (tree);
89 
90 /* Given two Windows decl attributes lists, possibly including
91    dllimport, return a list of their union .  */
92 extern tree merge_dllimport_decl_attributes (tree, tree);
93 
94 /* Handle a "dllimport" or "dllexport" attribute.  */
95 extern tree handle_dll_attribute (tree *, tree, tree, int, bool *);
96 
97 extern int attribute_list_equal (const_tree, const_tree);
98 extern int attribute_list_contained (const_tree, const_tree);
99 
100 /* The backbone of lookup_attribute().  ATTR_LEN is the string length
101    of ATTR_NAME, and LIST is not NULL_TREE.
102 
103    The function is called from lookup_attribute in order to optimize
104    for size.  */
105 extern tree private_lookup_attribute (const char *attr_name, size_t attr_len,
106 				      tree list);
107 
108 extern unsigned decls_mismatched_attributes (tree, tree, tree,
109 					     const char* const[],
110 					     pretty_printer*);
111 
112 extern void maybe_diag_alias_attributes (tree, tree);
113 
114 /* For a given IDENTIFIER_NODE, strip leading and trailing '_' characters
115    so that we have a canonical form of attribute names.  */
116 
117 static inline tree
canonicalize_attr_name(tree attr_name)118 canonicalize_attr_name (tree attr_name)
119 {
120   const size_t l = IDENTIFIER_LENGTH (attr_name);
121   const char *s = IDENTIFIER_POINTER (attr_name);
122 
123   if (l > 4 && s[0] == '_' && s[1] == '_' && s[l - 1] == '_' && s[l - 2] == '_')
124     return get_identifier_with_length (s + 2, l - 4);
125 
126   return attr_name;
127 }
128 
129 /* Compare attribute identifiers ATTR1 and ATTR2 with length ATTR1_LEN and
130    ATTR2_LEN.  */
131 
132 static inline bool
cmp_attribs(const char * attr1,size_t attr1_len,const char * attr2,size_t attr2_len)133 cmp_attribs (const char *attr1, size_t attr1_len,
134 	     const char *attr2, size_t attr2_len)
135 {
136   return attr1_len == attr2_len && strncmp (attr1, attr2, attr1_len) == 0;
137 }
138 
139 /* Compare attribute identifiers ATTR1 and ATTR2.  */
140 
141 static inline bool
cmp_attribs(const char * attr1,const char * attr2)142 cmp_attribs (const char *attr1, const char *attr2)
143 {
144   return cmp_attribs (attr1, strlen (attr1), attr2, strlen (attr2));
145 }
146 
147 /* Given an identifier node IDENT and a string ATTR_NAME, return true
148    if the identifier node is a valid attribute name for the string.  */
149 
150 static inline bool
is_attribute_p(const char * attr_name,const_tree ident)151 is_attribute_p (const char *attr_name, const_tree ident)
152 {
153   return cmp_attribs (attr_name, strlen (attr_name),
154 		      IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident));
155 }
156 
157 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
158    return a pointer to the attribute's list element if the attribute
159    is part of the list, or NULL_TREE if not found.  If the attribute
160    appears more than once, this only returns the first occurrence; the
161    TREE_CHAIN of the return value should be passed back in if further
162    occurrences are wanted.  ATTR_NAME must be in the form 'text' (not
163    '__text__').  */
164 
165 static inline tree
lookup_attribute(const char * attr_name,tree list)166 lookup_attribute (const char *attr_name, tree list)
167 {
168   gcc_checking_assert (attr_name[0] != '_');
169   /* In most cases, list is NULL_TREE.  */
170   if (list == NULL_TREE)
171     return NULL_TREE;
172   else
173     {
174       size_t attr_len = strlen (attr_name);
175       /* Do the strlen() before calling the out-of-line implementation.
176 	 In most cases attr_name is a string constant, and the compiler
177 	 will optimize the strlen() away.  */
178       return private_lookup_attribute (attr_name, attr_len, list);
179     }
180 }
181 
182 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
183    return a pointer to the attribute's list first element if the attribute
184    starts with ATTR_NAME.  ATTR_NAME must be in the form 'text' (not
185    '__text__').  */
186 
187 static inline tree
lookup_attribute_by_prefix(const char * attr_name,tree list)188 lookup_attribute_by_prefix (const char *attr_name, tree list)
189 {
190   gcc_checking_assert (attr_name[0] != '_');
191   /* In most cases, list is NULL_TREE.  */
192   if (list == NULL_TREE)
193     return NULL_TREE;
194   else
195     {
196       size_t attr_len = strlen (attr_name);
197       while (list)
198 	{
199 	  size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
200 
201 	  if (attr_len > ident_len)
202 	    {
203 	      list = TREE_CHAIN (list);
204 	      continue;
205 	    }
206 
207 	  const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
208 	  gcc_checking_assert (attr_len == 0 || p[0] != '_');
209 
210 	  if (strncmp (attr_name, p, attr_len) == 0)
211 	    break;
212 
213 	  list = TREE_CHAIN (list);
214 	}
215 
216       return list;
217     }
218 }
219 
220 #endif // GCC_ATTRIBS_H
221