attribs.h 7.4 KB

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