attribs.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Declarations and definitions dealing with attribute handling.
  2. Copyright (C) 2013-2018 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. /* For a given IDENTIFIER_NODE, strip leading and trailing '_' characters
  79. so that we have a canonical form of attribute names. */
  80. static inline tree
  81. canonicalize_attr_name (tree attr_name)
  82. {
  83. const size_t l = IDENTIFIER_LENGTH (attr_name);
  84. const char *s = IDENTIFIER_POINTER (attr_name);
  85. if (l > 4 && s[0] == '_' && s[1] == '_' && s[l - 1] == '_' && s[l - 2] == '_')
  86. return get_identifier_with_length (s + 2, l - 4);
  87. return attr_name;
  88. }
  89. /* Compare attribute identifiers ATTR1 and ATTR2 with length ATTR1_LEN and
  90. ATTR2_LEN. */
  91. static inline bool
  92. cmp_attribs (const char *attr1, size_t attr1_len,
  93. const char *attr2, size_t attr2_len)
  94. {
  95. return attr1_len == attr2_len && strncmp (attr1, attr2, attr1_len) == 0;
  96. }
  97. /* Compare attribute identifiers ATTR1 and ATTR2. */
  98. static inline bool
  99. cmp_attribs (const char *attr1, const char *attr2)
  100. {
  101. return cmp_attribs (attr1, strlen (attr1), attr2, strlen (attr2));
  102. }
  103. /* Given an identifier node IDENT and a string ATTR_NAME, return true
  104. if the identifier node is a valid attribute name for the string. */
  105. static inline bool
  106. is_attribute_p (const char *attr_name, const_tree ident)
  107. {
  108. return cmp_attribs (attr_name, strlen (attr_name),
  109. IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident));
  110. }
  111. /* Given an attribute name ATTR_NAME and a list of attributes LIST,
  112. return a pointer to the attribute's list element if the attribute
  113. is part of the list, or NULL_TREE if not found. If the attribute
  114. appears more than once, this only returns the first occurrence; the
  115. TREE_CHAIN of the return value should be passed back in if further
  116. occurrences are wanted. ATTR_NAME must be in the form 'text' (not
  117. '__text__'). */
  118. static inline tree
  119. lookup_attribute (const char *attr_name, tree list)
  120. {
  121. gcc_checking_assert (attr_name[0] != '_');
  122. /* In most cases, list is NULL_TREE. */
  123. if (list == NULL_TREE)
  124. return NULL_TREE;
  125. else
  126. {
  127. size_t attr_len = strlen (attr_name);
  128. /* Do the strlen() before calling the out-of-line implementation.
  129. In most cases attr_name is a string constant, and the compiler
  130. will optimize the strlen() away. */
  131. return private_lookup_attribute (attr_name, attr_len, list);
  132. }
  133. }
  134. /* Given an attribute name ATTR_NAME and a list of attributes LIST,
  135. return a pointer to the attribute's list first element if the attribute
  136. starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
  137. '__text__'). */
  138. static inline tree
  139. lookup_attribute_by_prefix (const char *attr_name, tree list)
  140. {
  141. gcc_checking_assert (attr_name[0] != '_');
  142. /* In most cases, list is NULL_TREE. */
  143. if (list == NULL_TREE)
  144. return NULL_TREE;
  145. else
  146. {
  147. size_t attr_len = strlen (attr_name);
  148. while (list)
  149. {
  150. size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
  151. if (attr_len > ident_len)
  152. {
  153. list = TREE_CHAIN (list);
  154. continue;
  155. }
  156. const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
  157. gcc_checking_assert (attr_len == 0 || p[0] != '_');
  158. if (strncmp (attr_name, p, attr_len) == 0)
  159. break;
  160. list = TREE_CHAIN (list);
  161. }
  162. return list;
  163. }
  164. }
  165. #endif // GCC_ATTRIBS_H