HowToUseAttributes.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. =====================
  2. How To Use Attributes
  3. =====================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. Attributes in LLVM have changed in some fundamental ways. It was necessary to
  9. do this to support expanding the attributes to encompass more than a handful of
  10. attributes --- e.g. command line options. The old way of handling attributes
  11. consisted of representing them as a bit mask of values. This bit mask was
  12. stored in a "list" structure that was reference counted. The advantage of this
  13. was that attributes could be manipulated with 'or's and 'and's. The
  14. disadvantage of this was that there was limited room for expansion, and
  15. virtually no support for attribute-value pairs other than alignment.
  16. In the new scheme, an ``Attribute`` object represents a single attribute that's
  17. uniqued. You use the ``Attribute::get`` methods to create a new ``Attribute``
  18. object. An attribute can be a single "enum" value (the enum being the
  19. ``Attribute::AttrKind`` enum), a string representing a target-dependent
  20. attribute, or an attribute-value pair. Some examples:
  21. * Target-independent: ``noinline``, ``zext``
  22. * Target-dependent: ``"no-sse"``, ``"thumb2"``
  23. * Attribute-value pair: ``"cpu" = "cortex-a8"``, ``align = 4``
  24. Note: for an attribute value pair, we expect a target-dependent attribute to
  25. have a string for the value.
  26. ``Attribute``
  27. =============
  28. An ``Attribute`` object is designed to be passed around by value.
  29. Because attributes are no longer represented as a bit mask, you will need to
  30. convert any code which does treat them as a bit mask to use the new query
  31. methods on the Attribute class.
  32. ``AttributeSet``
  33. ================
  34. The ``AttributeSet`` class replaces the old ``AttributeList`` class. The
  35. ``AttributeSet`` stores a collection of Attribute objects for each kind of
  36. object that may have an attribute associated with it: the function as a
  37. whole, the return type, or the function's parameters. A function's attributes
  38. are at index ``AttributeSet::FunctionIndex``; the return type's attributes are
  39. at index ``AttributeSet::ReturnIndex``; and the function's parameters'
  40. attributes are at indices 1, ..., n (where 'n' is the number of parameters).
  41. Most methods on the ``AttributeSet`` class take an index parameter.
  42. An ``AttributeSet`` is also a uniqued and immutable object. You create an
  43. ``AttributeSet`` through the ``AttributeSet::get`` methods. You can add and
  44. remove attributes, which result in the creation of a new ``AttributeSet``.
  45. An ``AttributeSet`` object is designed to be passed around by value.
  46. Note: It is advised that you do *not* use the ``AttributeSet`` "introspection"
  47. methods (e.g. ``Raw``, ``getRawPointer``, etc.). These methods break
  48. encapsulation, and may be removed in a future release (i.e. LLVM 4.0).
  49. ``AttrBuilder``
  50. ===============
  51. Lastly, we have a "builder" class to help create the ``AttributeSet`` object
  52. without having to create several different intermediate uniqued
  53. ``AttributeSet`` objects. The ``AttrBuilder`` class allows you to add and
  54. remove attributes at will. The attributes won't be uniqued until you call the
  55. appropriate ``AttributeSet::get`` method.
  56. An ``AttrBuilder`` object is *not* designed to be passed around by value. It
  57. should be passed by reference.
  58. Note: It is advised that you do *not* use the ``AttrBuilder::addRawValue()``
  59. method or the ``AttrBuilder(uint64_t Val)`` constructor. These are for
  60. backwards compatibility and may be removed in a future release (i.e. LLVM 4.0).
  61. And that's basically it! A lot of functionality is hidden behind these classes,
  62. but the interfaces are pretty straight forward.