attribs.h 8.0 KB

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