1 //===--- AttributeList.h - Parsed attribute sets ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the AttributeList class, which is used to collect
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SEMA_ATTRLIST_H
16 #define LLVM_CLANG_SEMA_ATTRLIST_H
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/VersionTuple.h"
29 /// \brief Represents information about a change in availability for
30 /// an entity, which is part of the encoding of the 'availability'
32 struct AvailabilityChange {
33 /// \brief The location of the keyword indicating the kind of change.
34 SourceLocation KeywordLoc;
36 /// \brief The version number at which the change occurred.
39 /// \brief The source range covering the version number.
40 SourceRange VersionRange;
42 /// \brief Determine whether this availability change is valid.
43 bool isValid() const { return !Version.empty(); }
46 /// AttributeList - Represents GCC's __attribute__ declaration. There are
47 /// 4 forms of this construct...they are:
49 /// 1: __attribute__(( const )). ParmName/Args/NumArgs will all be unused.
50 /// 2: __attribute__(( mode(byte) )). ParmName used, Args/NumArgs unused.
51 /// 3: __attribute__(( format(printf, 1, 2) )). ParmName/Args/NumArgs all used.
52 /// 4: __attribute__(( aligned(16) )). ParmName is unused, Args/Num used.
54 class AttributeList { // TODO: This should really be called ParsedAttribute
56 IdentifierInfo *AttrName;
57 IdentifierInfo *ScopeName;
58 IdentifierInfo *ParmName;
59 SourceLocation AttrLoc;
60 SourceLocation ScopeLoc;
61 SourceLocation ParmLoc;
63 /// The number of expression arguments this attribute has.
64 /// The expressions themselves are stored after the object.
65 unsigned NumArgs : 16;
67 /// True if Microsoft style: declspec(foo).
68 unsigned DeclspecAttribute : 1;
70 /// True if C++0x-style: [[foo]].
71 unsigned CXX0XAttribute : 1;
73 /// True if already diagnosed as invalid.
74 mutable unsigned Invalid : 1;
76 /// True if this has the extra information associated with an
77 /// availability attribute.
78 unsigned IsAvailability : 1;
80 unsigned AttrKind : 8;
82 /// \brief The location of the 'unavailable' keyword in an
83 /// availability attribute.
84 SourceLocation UnavailableLoc;
86 /// The next attribute in the current position.
87 AttributeList *NextInPosition;
89 /// The next attribute allocated in the current Pool.
90 AttributeList *NextInPool;
92 Expr **getArgsBuffer() {
93 return reinterpret_cast<Expr**>(this+1);
95 Expr * const *getArgsBuffer() const {
96 return reinterpret_cast<Expr* const *>(this+1);
99 enum AvailabilitySlot {
100 IntroducedSlot, DeprecatedSlot, ObsoletedSlot
103 AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) {
104 return reinterpret_cast<AvailabilityChange*>(this+1)[index];
106 const AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) const {
107 return reinterpret_cast<const AvailabilityChange*>(this+1)[index];
110 AttributeList(const AttributeList &); // DO NOT IMPLEMENT
111 void operator=(const AttributeList &); // DO NOT IMPLEMENT
112 void operator delete(void *); // DO NOT IMPLEMENT
113 ~AttributeList(); // DO NOT IMPLEMENT
115 size_t allocated_size() const;
117 AttributeList(IdentifierInfo *attrName, SourceLocation attrLoc,
118 IdentifierInfo *scopeName, SourceLocation scopeLoc,
119 IdentifierInfo *parmName, SourceLocation parmLoc,
120 Expr **args, unsigned numArgs,
121 bool declspec, bool cxx0x)
122 : AttrName(attrName), ScopeName(scopeName), ParmName(parmName),
123 AttrLoc(attrLoc), ScopeLoc(scopeLoc), ParmLoc(parmLoc),
125 DeclspecAttribute(declspec), CXX0XAttribute(cxx0x), Invalid(false),
126 IsAvailability(false), NextInPosition(0), NextInPool(0) {
127 if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(Expr*));
128 AttrKind = getKind(getName());
131 AttributeList(IdentifierInfo *attrName, SourceLocation attrLoc,
132 IdentifierInfo *scopeName, SourceLocation scopeLoc,
133 IdentifierInfo *parmName, SourceLocation parmLoc,
134 const AvailabilityChange &introduced,
135 const AvailabilityChange &deprecated,
136 const AvailabilityChange &obsoleted,
137 SourceLocation unavailable,
138 bool declspec, bool cxx0x)
139 : AttrName(attrName), ScopeName(scopeName), ParmName(parmName),
140 AttrLoc(attrLoc), ScopeLoc(scopeLoc), ParmLoc(parmLoc),
141 NumArgs(0), DeclspecAttribute(declspec), CXX0XAttribute(cxx0x),
142 Invalid(false), IsAvailability(true), UnavailableLoc(unavailable),
143 NextInPosition(0), NextInPool(0) {
144 new (&getAvailabilitySlot(IntroducedSlot)) AvailabilityChange(introduced);
145 new (&getAvailabilitySlot(DeprecatedSlot)) AvailabilityChange(deprecated);
146 new (&getAvailabilitySlot(ObsoletedSlot)) AvailabilityChange(obsoleted);
147 AttrKind = getKind(getName());
150 friend class AttributePool;
151 friend class AttributeFactory;
154 enum Kind { // Please keep this list alphabetized.
159 AT_analyzer_noreturn,
161 AT_arc_weakref_unavailable,
162 AT_availability, // Clang-specific
165 AT_carries_dependency,
167 AT_cf_consumed, // Clang-specific.
168 AT_cf_returns_autoreleased, // Clang-specific.
169 AT_cf_returns_not_retained, // Clang-specific.
170 AT_cf_returns_retained, // Clang-specific.
188 AT_IBAction, // Clang-specific.
189 AT_IBOutlet, // Clang-specific.
190 AT_IBOutletCollection, // Clang-specific.
198 AT_neon_polyvector_type, // Clang-specific.
199 AT_neon_vector_type, // Clang-specific.
200 AT_no_instrument_function,
207 AT_ns_consumed, // Clang-specific.
208 AT_ns_consumes_self, // Clang-specific.
209 AT_ns_returns_autoreleased, // Clang-specific.
210 AT_ns_returns_not_retained, // Clang-specific.
211 AT_ns_returns_retained, // Clang-specific.
215 AT_objc_method_family,
216 AT_objc_ownership, // Clang-specific.
217 AT_objc_precise_lifetime, // Clang-specific.
218 AT_opencl_image_access, // OpenCL-specific.
219 AT_opencl_kernel_function, // OpenCL-specific.
220 AT_overloadable, // Clang-specific.
221 AT_ownership_holds, // Clang-specific.
222 AT_ownership_returns, // Clang-specific.
223 AT_ownership_takes, // Clang-specific.
226 AT_pcs, // ARM specific
235 AT_transparent_union,
240 AT_vecreturn, // PS3 PPU-specific.
243 AT_warn_unused_result,
251 IdentifierInfo *getName() const { return AttrName; }
252 SourceLocation getLoc() const { return AttrLoc; }
254 bool hasScope() const { return ScopeName; }
255 IdentifierInfo *getScopeName() const { return ScopeName; }
256 SourceLocation getScopeLoc() const { return ScopeLoc; }
258 IdentifierInfo *getParameterName() const { return ParmName; }
259 SourceLocation getParameterLoc() const { return ParmLoc; }
261 bool isDeclspecAttribute() const { return DeclspecAttribute; }
262 bool isCXX0XAttribute() const { return CXX0XAttribute; }
264 bool isInvalid() const { return Invalid; }
265 void setInvalid(bool b = true) const { Invalid = b; }
267 Kind getKind() const { return Kind(AttrKind); }
268 static Kind getKind(const IdentifierInfo *Name);
270 AttributeList *getNext() const { return NextInPosition; }
271 void setNext(AttributeList *N) { NextInPosition = N; }
273 /// getNumArgs - Return the number of actual arguments to this attribute.
274 unsigned getNumArgs() const { return NumArgs; }
276 /// hasParameterOrArguments - Return true if this attribute has a parameter,
277 /// or has a non empty argument expression list.
278 bool hasParameterOrArguments() const { return ParmName || NumArgs; }
280 /// getArg - Return the specified argument.
281 Expr *getArg(unsigned Arg) const {
282 assert(Arg < NumArgs && "Arg access out of range!");
283 return getArgsBuffer()[Arg];
290 arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {}
292 arg_iterator& operator++() {
297 bool operator==(const arg_iterator& I) const {
299 "compared arg_iterators are for different argument lists");
303 bool operator!=(const arg_iterator& I) const {
304 return !operator==(I);
307 Expr* operator*() const {
311 unsigned getArgNum() const {
316 arg_iterator arg_begin() const {
317 return arg_iterator(getArgsBuffer(), 0);
320 arg_iterator arg_end() const {
321 return arg_iterator(getArgsBuffer(), NumArgs);
324 const AvailabilityChange &getAvailabilityIntroduced() const {
325 assert(getKind() == AT_availability && "Not an availability attribute");
326 return getAvailabilitySlot(IntroducedSlot);
329 const AvailabilityChange &getAvailabilityDeprecated() const {
330 assert(getKind() == AT_availability && "Not an availability attribute");
331 return getAvailabilitySlot(DeprecatedSlot);
334 const AvailabilityChange &getAvailabilityObsoleted() const {
335 assert(getKind() == AT_availability && "Not an availability attribute");
336 return getAvailabilitySlot(ObsoletedSlot);
339 SourceLocation getUnavailableLoc() const {
340 assert(getKind() == AT_availability && "Not an availability attribute");
341 return UnavailableLoc;
345 /// A factory, from which one makes pools, from which one creates
346 /// individual attributes which are deallocated with the pool.
348 /// Note that it's tolerably cheap to create and destroy one of
349 /// these as long as you don't actually allocate anything in it.
350 class AttributeFactory {
353 /// The required allocation size of an availability attribute,
354 /// which we want to ensure is a multiple of sizeof(void*).
355 AvailabilityAllocSize =
356 sizeof(AttributeList)
357 + ((3 * sizeof(AvailabilityChange) + sizeof(void*) - 1)
358 / sizeof(void*) * sizeof(void*))
363 /// The number of free lists we want to be sure to support
364 /// inline. This is just enough that availability attributes
365 /// don't surpass it. It's actually very unlikely we'll see an
366 /// attribute that needs more than that; on x86-64 you'd need 10
367 /// expression arguments, and on i386 you'd need 19.
368 InlineFreeListsCapacity =
369 1 + (AvailabilityAllocSize - sizeof(AttributeList)) / sizeof(void*)
372 llvm::BumpPtrAllocator Alloc;
374 /// Free lists. The index is determined by the following formula:
375 /// (size - sizeof(AttributeList)) / sizeof(void*)
376 llvm::SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists;
378 // The following are the private interface used by AttributePool.
379 friend class AttributePool;
381 /// Allocate an attribute of the given size.
382 void *allocate(size_t size);
384 /// Reclaim all the attributes in the given pool chain, which is
385 /// non-empty. Note that the current implementation is safe
386 /// against reclaiming things which were not actually allocated
387 /// with the allocator, although of course it's important to make
388 /// sure that their allocator lives at least as long as this one.
389 void reclaimPool(AttributeList *head);
396 class AttributePool {
397 AttributeFactory &Factory;
400 void *allocate(size_t size) {
401 return Factory.allocate(size);
404 AttributeList *add(AttributeList *attr) {
405 // We don't care about the order of the pool.
406 attr->NextInPool = Head;
411 void takePool(AttributeList *pool);
414 /// Create a new pool for a factory.
415 AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {}
417 /// Move the given pool's allocations to this pool.
418 AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) {
422 AttributeFactory &getFactory() const { return Factory; }
426 Factory.reclaimPool(Head);
431 /// Take the given pool's allocations and add them to this pool.
432 void takeAllFrom(AttributePool &pool) {
440 if (Head) Factory.reclaimPool(Head);
443 AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc,
444 IdentifierInfo *scopeName, SourceLocation scopeLoc,
445 IdentifierInfo *parmName, SourceLocation parmLoc,
446 Expr **args, unsigned numArgs,
447 bool declspec = false, bool cxx0x = false) {
448 void *memory = allocate(sizeof(AttributeList)
449 + numArgs * sizeof(Expr*));
450 return add(new (memory) AttributeList(attrName, attrLoc,
457 AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc,
458 IdentifierInfo *scopeName, SourceLocation scopeLoc,
459 IdentifierInfo *parmName, SourceLocation parmLoc,
460 const AvailabilityChange &introduced,
461 const AvailabilityChange &deprecated,
462 const AvailabilityChange &obsoleted,
463 SourceLocation unavailable,
464 bool declspec = false, bool cxx0x = false) {
465 void *memory = allocate(AttributeFactory::AvailabilityAllocSize);
466 return add(new (memory) AttributeList(attrName, attrLoc,
469 introduced, deprecated, obsoleted,
474 AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
475 SourceLocation TokLoc, int Arg);
478 /// addAttributeLists - Add two AttributeLists together
479 /// The right-hand list is appended to the left-hand list, if any
480 /// A pointer to the joined list is returned.
481 /// Note: the lists are not left unmodified.
482 inline AttributeList *addAttributeLists(AttributeList *Left,
483 AttributeList *Right) {
487 AttributeList *next = Left, *prev;
490 next = next->getNext();
492 prev->setNext(Right);
496 /// CXX0XAttributeList - A wrapper around a C++0x attribute list.
497 /// Stores, in addition to the list proper, whether or not an actual list was
498 /// (as opposed to an empty list, which may be ill-formed in some places) and
499 /// the source range of the list.
500 struct CXX0XAttributeList {
501 AttributeList *AttrList;
504 CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
505 : AttrList(attrList), Range(range), HasAttr (hasAttr) {
507 CXX0XAttributeList ()
508 : AttrList(0), Range(), HasAttr(false) {
512 /// ParsedAttributes - A collection of parsed attributes. Currently
513 /// we don't differentiate between the various attribute syntaxes,
514 /// which is basically silly.
516 /// Right now this is a very lightweight container, but the expectation
517 /// is that this will become significantly more serious.
518 class ParsedAttributes {
520 ParsedAttributes(AttributeFactory &factory)
521 : pool(factory), list(0) {
524 ParsedAttributes(ParsedAttributes &attrs)
525 : pool(attrs.pool), list(attrs.list) {
529 AttributePool &getPool() const { return pool; }
531 bool empty() const { return list == 0; }
533 void add(AttributeList *newAttr) {
535 assert(newAttr->getNext() == 0);
536 newAttr->setNext(list);
540 void addAll(AttributeList *newList) {
541 if (!newList) return;
543 AttributeList *lastInNewList = newList;
544 while (AttributeList *next = lastInNewList->getNext())
545 lastInNewList = next;
547 lastInNewList->setNext(list);
551 void set(AttributeList *newList) {
555 void takeAllFrom(ParsedAttributes &attrs) {
558 pool.takeAllFrom(attrs.pool);
561 void clear() { list = 0; pool.clear(); }
562 AttributeList *getList() const { return list; }
564 /// Returns a reference to the attribute list. Try not to introduce
565 /// dependencies on this method, it may not be long-lived.
566 AttributeList *&getListRef() { return list; }
569 AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc,
570 IdentifierInfo *scopeName, SourceLocation scopeLoc,
571 IdentifierInfo *parmName, SourceLocation parmLoc,
572 Expr **args, unsigned numArgs,
573 bool declspec = false, bool cxx0x = false) {
574 AttributeList *attr =
575 pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc,
576 args, numArgs, declspec, cxx0x);
581 AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc,
582 IdentifierInfo *scopeName, SourceLocation scopeLoc,
583 IdentifierInfo *parmName, SourceLocation parmLoc,
584 const AvailabilityChange &introduced,
585 const AvailabilityChange &deprecated,
586 const AvailabilityChange &obsoleted,
587 SourceLocation unavailable,
588 bool declspec = false, bool cxx0x = false) {
589 AttributeList *attr =
590 pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc,
591 introduced, deprecated, obsoleted, unavailable,
597 AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name,
598 SourceLocation loc, int arg) {
599 AttributeList *attr =
600 pool.createIntegerAttribute(C, name, loc, arg);
607 mutable AttributePool pool;
611 } // end namespace clang