HowToUseAttributes.rst 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ``AttributeList``
  33. =================
  34. The ``AttributeList`` stores a collection of Attribute objects for each kind of
  35. object that may have an attribute associated with it: the function as a whole,
  36. the return type, or the function's parameters. A function's attributes are at
  37. index ``AttributeList::FunctionIndex``; the return type's attributes are at
  38. index ``AttributeList::ReturnIndex``; and the function's parameters' attributes
  39. are at indices 1, ..., n (where 'n' is the number of parameters). Most methods
  40. on the ``AttributeList`` class take an index parameter.
  41. An ``AttributeList`` is also a uniqued and immutable object. You create an
  42. ``AttributeList`` through the ``AttributeList::get`` methods. You can add and
  43. remove attributes, which result in the creation of a new ``AttributeList``.
  44. An ``AttributeList`` object is designed to be passed around by value.
  45. Note: It is advised that you do *not* use the ``AttributeList`` "introspection"
  46. methods (e.g. ``Raw``, ``getRawPointer``, etc.). These methods break
  47. encapsulation, and may be removed in a future release (i.e. LLVM 4.0).
  48. ``AttrBuilder``
  49. ===============
  50. Lastly, we have a "builder" class to help create the ``AttributeList`` object
  51. without having to create several different intermediate uniqued
  52. ``AttributeList`` objects. The ``AttrBuilder`` class allows you to add and
  53. remove attributes at will. The attributes won't be uniqued until you call the
  54. appropriate ``AttributeList::get`` method.
  55. An ``AttrBuilder`` object is *not* designed to be passed around by value. It
  56. should be passed by reference.
  57. Note: It is advised that you do *not* use the ``AttrBuilder::addRawValue()``
  58. method or the ``AttrBuilder(uint64_t Val)`` constructor. These are for
  59. backwards compatibility and may be removed in a future release (i.e. LLVM 4.0).
  60. And that's basically it! A lot of functionality is hidden behind these classes,
  61. but the interfaces are pretty straight forward.