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 SourceRange AttrRange;
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 attribute was used as a type attribute.
77 mutable unsigned UsedAsTypeAttr : 1;
79 /// True if this has the extra information associated with an
80 /// availability attribute.
81 unsigned IsAvailability : 1;
83 unsigned AttrKind : 8;
85 /// \brief The location of the 'unavailable' keyword in an
86 /// availability attribute.
87 SourceLocation UnavailableLoc;
89 /// The next attribute in the current position.
90 AttributeList *NextInPosition;
92 /// The next attribute allocated in the current Pool.
93 AttributeList *NextInPool;
95 Expr **getArgsBuffer() {
96 return reinterpret_cast<Expr**>(this+1);
98 Expr * const *getArgsBuffer() const {
99 return reinterpret_cast<Expr* const *>(this+1);
102 enum AvailabilitySlot {
103 IntroducedSlot, DeprecatedSlot, ObsoletedSlot
106 AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) {
107 return reinterpret_cast<AvailabilityChange*>(this+1)[index];
109 const AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) const {
110 return reinterpret_cast<const AvailabilityChange*>(this+1)[index];
113 AttributeList(const AttributeList &); // DO NOT IMPLEMENT
114 void operator=(const AttributeList &); // DO NOT IMPLEMENT
115 void operator delete(void *); // DO NOT IMPLEMENT
116 ~AttributeList(); // DO NOT IMPLEMENT
118 size_t allocated_size() const;
120 AttributeList(IdentifierInfo *attrName, SourceRange attrRange,
121 IdentifierInfo *scopeName, SourceLocation scopeLoc,
122 IdentifierInfo *parmName, SourceLocation parmLoc,
123 Expr **args, unsigned numArgs,
124 bool declspec, bool cxx0x)
125 : AttrName(attrName), ScopeName(scopeName), ParmName(parmName),
126 AttrRange(attrRange), ScopeLoc(scopeLoc), ParmLoc(parmLoc),
128 DeclspecAttribute(declspec), CXX0XAttribute(cxx0x), Invalid(false),
129 UsedAsTypeAttr(false), IsAvailability(false),
130 NextInPosition(0), NextInPool(0) {
131 if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(Expr*));
132 AttrKind = getKind(getName());
135 AttributeList(IdentifierInfo *attrName, SourceRange attrRange,
136 IdentifierInfo *scopeName, SourceLocation scopeLoc,
137 IdentifierInfo *parmName, SourceLocation parmLoc,
138 const AvailabilityChange &introduced,
139 const AvailabilityChange &deprecated,
140 const AvailabilityChange &obsoleted,
141 SourceLocation unavailable,
142 bool declspec, bool cxx0x)
143 : AttrName(attrName), ScopeName(scopeName), ParmName(parmName),
144 AttrRange(attrRange), ScopeLoc(scopeLoc), ParmLoc(parmLoc),
145 NumArgs(0), DeclspecAttribute(declspec), CXX0XAttribute(cxx0x),
146 Invalid(false), UsedAsTypeAttr(false), IsAvailability(true),
147 UnavailableLoc(unavailable), NextInPosition(0), NextInPool(0) {
148 new (&getAvailabilitySlot(IntroducedSlot)) AvailabilityChange(introduced);
149 new (&getAvailabilitySlot(DeprecatedSlot)) AvailabilityChange(deprecated);
150 new (&getAvailabilitySlot(ObsoletedSlot)) AvailabilityChange(obsoleted);
151 AttrKind = getKind(getName());
154 friend class AttributePool;
155 friend class AttributeFactory;
158 enum Kind { // Please keep this list alphabetized.
165 AT_analyzer_noreturn,
167 AT_arc_weakref_unavailable,
168 AT_availability, // Clang-specific
171 AT_carries_dependency,
173 AT_cf_audited_transfer, // Clang-specific.
174 AT_cf_consumed, // Clang-specific.
175 AT_cf_returns_autoreleased, // Clang-specific.
176 AT_cf_returns_not_retained, // Clang-specific.
177 AT_cf_returns_retained, // Clang-specific.
178 AT_cf_unknown_transfer, // Clang-specific.
189 AT_exclusive_lock_function,
190 AT_exclusive_locks_required,
191 AT_exclusive_trylock_function,
201 AT_IBAction, // Clang-specific.
202 AT_IBOutlet, // Clang-specific.
203 AT_IBOutletCollection, // Clang-specific.
214 AT_neon_polyvector_type, // Clang-specific.
215 AT_neon_vector_type, // Clang-specific.
216 AT_no_instrument_function,
217 AT_no_thread_safety_analysis,
224 AT_ns_bridged, // Clang-specific.
225 AT_ns_consumed, // Clang-specific.
226 AT_ns_consumes_self, // Clang-specific.
227 AT_ns_returns_autoreleased, // Clang-specific.
228 AT_ns_returns_not_retained, // Clang-specific.
229 AT_ns_returns_retained, // Clang-specific.
233 AT_objc_method_family,
234 AT_objc_ownership, // Clang-specific.
235 AT_objc_precise_lifetime, // Clang-specific.
236 AT_objc_returns_inner_pointer, // Clang-specific.
237 AT_opencl_image_access, // OpenCL-specific.
238 AT_opencl_kernel_function, // OpenCL-specific.
239 AT_overloadable, // Clang-specific.
240 AT_ownership_holds, // Clang-specific.
241 AT_ownership_returns, // Clang-specific.
242 AT_ownership_takes, // Clang-specific.
245 AT_pcs, // ARM specific
255 AT_shared_lock_function,
256 AT_shared_locks_required,
257 AT_shared_trylock_function,
260 AT_transparent_union,
266 AT_vecreturn, // PS3 PPU-specific.
269 AT_warn_unused_result,
278 IdentifierInfo *getName() const { return AttrName; }
279 SourceLocation getLoc() const { return AttrRange.getBegin(); }
280 SourceRange getRange() const { return AttrRange; }
282 bool hasScope() const { return ScopeName; }
283 IdentifierInfo *getScopeName() const { return ScopeName; }
284 SourceLocation getScopeLoc() const { return ScopeLoc; }
286 IdentifierInfo *getParameterName() const { return ParmName; }
287 SourceLocation getParameterLoc() const { return ParmLoc; }
289 bool isDeclspecAttribute() const { return DeclspecAttribute; }
290 bool isCXX0XAttribute() const { return CXX0XAttribute; }
292 bool isInvalid() const { return Invalid; }
293 void setInvalid(bool b = true) const { Invalid = b; }
295 bool isUsedAsTypeAttr() const { return UsedAsTypeAttr; }
296 void setUsedAsTypeAttr() { UsedAsTypeAttr = true; }
298 Kind getKind() const { return Kind(AttrKind); }
299 static Kind getKind(const IdentifierInfo *Name);
301 AttributeList *getNext() const { return NextInPosition; }
302 void setNext(AttributeList *N) { NextInPosition = N; }
304 /// getNumArgs - Return the number of actual arguments to this attribute.
305 unsigned getNumArgs() const { return NumArgs; }
307 /// hasParameterOrArguments - Return true if this attribute has a parameter,
308 /// or has a non empty argument expression list.
309 bool hasParameterOrArguments() const { return ParmName || NumArgs; }
311 /// getArg - Return the specified argument.
312 Expr *getArg(unsigned Arg) const {
313 assert(Arg < NumArgs && "Arg access out of range!");
314 return getArgsBuffer()[Arg];
321 arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {}
323 arg_iterator& operator++() {
328 bool operator==(const arg_iterator& I) const {
330 "compared arg_iterators are for different argument lists");
334 bool operator!=(const arg_iterator& I) const {
335 return !operator==(I);
338 Expr* operator*() const {
342 unsigned getArgNum() const {
347 arg_iterator arg_begin() const {
348 return arg_iterator(getArgsBuffer(), 0);
351 arg_iterator arg_end() const {
352 return arg_iterator(getArgsBuffer(), NumArgs);
355 const AvailabilityChange &getAvailabilityIntroduced() const {
356 assert(getKind() == AT_availability && "Not an availability attribute");
357 return getAvailabilitySlot(IntroducedSlot);
360 const AvailabilityChange &getAvailabilityDeprecated() const {
361 assert(getKind() == AT_availability && "Not an availability attribute");
362 return getAvailabilitySlot(DeprecatedSlot);
365 const AvailabilityChange &getAvailabilityObsoleted() const {
366 assert(getKind() == AT_availability && "Not an availability attribute");
367 return getAvailabilitySlot(ObsoletedSlot);
370 SourceLocation getUnavailableLoc() const {
371 assert(getKind() == AT_availability && "Not an availability attribute");
372 return UnavailableLoc;
376 /// A factory, from which one makes pools, from which one creates
377 /// individual attributes which are deallocated with the pool.
379 /// Note that it's tolerably cheap to create and destroy one of
380 /// these as long as you don't actually allocate anything in it.
381 class AttributeFactory {
384 /// The required allocation size of an availability attribute,
385 /// which we want to ensure is a multiple of sizeof(void*).
386 AvailabilityAllocSize =
387 sizeof(AttributeList)
388 + ((3 * sizeof(AvailabilityChange) + sizeof(void*) - 1)
389 / sizeof(void*) * sizeof(void*))
394 /// The number of free lists we want to be sure to support
395 /// inline. This is just enough that availability attributes
396 /// don't surpass it. It's actually very unlikely we'll see an
397 /// attribute that needs more than that; on x86-64 you'd need 10
398 /// expression arguments, and on i386 you'd need 19.
399 InlineFreeListsCapacity =
400 1 + (AvailabilityAllocSize - sizeof(AttributeList)) / sizeof(void*)
403 llvm::BumpPtrAllocator Alloc;
405 /// Free lists. The index is determined by the following formula:
406 /// (size - sizeof(AttributeList)) / sizeof(void*)
407 SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists;
409 // The following are the private interface used by AttributePool.
410 friend class AttributePool;
412 /// Allocate an attribute of the given size.
413 void *allocate(size_t size);
415 /// Reclaim all the attributes in the given pool chain, which is
416 /// non-empty. Note that the current implementation is safe
417 /// against reclaiming things which were not actually allocated
418 /// with the allocator, although of course it's important to make
419 /// sure that their allocator lives at least as long as this one.
420 void reclaimPool(AttributeList *head);
427 class AttributePool {
428 AttributeFactory &Factory;
431 void *allocate(size_t size) {
432 return Factory.allocate(size);
435 AttributeList *add(AttributeList *attr) {
436 // We don't care about the order of the pool.
437 attr->NextInPool = Head;
442 void takePool(AttributeList *pool);
445 /// Create a new pool for a factory.
446 AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {}
448 /// Move the given pool's allocations to this pool.
449 AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) {
453 AttributeFactory &getFactory() const { return Factory; }
457 Factory.reclaimPool(Head);
462 /// Take the given pool's allocations and add them to this pool.
463 void takeAllFrom(AttributePool &pool) {
471 if (Head) Factory.reclaimPool(Head);
474 AttributeList *create(IdentifierInfo *attrName, SourceRange attrRange,
475 IdentifierInfo *scopeName, SourceLocation scopeLoc,
476 IdentifierInfo *parmName, SourceLocation parmLoc,
477 Expr **args, unsigned numArgs,
478 bool declspec = false, bool cxx0x = false) {
479 void *memory = allocate(sizeof(AttributeList)
480 + numArgs * sizeof(Expr*));
481 return add(new (memory) AttributeList(attrName, attrRange,
488 AttributeList *create(IdentifierInfo *attrName, SourceRange attrRange,
489 IdentifierInfo *scopeName, SourceLocation scopeLoc,
490 IdentifierInfo *parmName, SourceLocation parmLoc,
491 const AvailabilityChange &introduced,
492 const AvailabilityChange &deprecated,
493 const AvailabilityChange &obsoleted,
494 SourceLocation unavailable,
495 bool declspec = false, bool cxx0x = false) {
496 void *memory = allocate(AttributeFactory::AvailabilityAllocSize);
497 return add(new (memory) AttributeList(attrName, attrRange,
500 introduced, deprecated, obsoleted,
505 AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
506 SourceLocation TokLoc, int Arg);
509 /// addAttributeLists - Add two AttributeLists together
510 /// The right-hand list is appended to the left-hand list, if any
511 /// A pointer to the joined list is returned.
512 /// Note: the lists are not left unmodified.
513 inline AttributeList *addAttributeLists(AttributeList *Left,
514 AttributeList *Right) {
518 AttributeList *next = Left, *prev;
521 next = next->getNext();
523 prev->setNext(Right);
527 /// CXX0XAttributeList - A wrapper around a C++0x attribute list.
528 /// Stores, in addition to the list proper, whether or not an actual list was
529 /// (as opposed to an empty list, which may be ill-formed in some places) and
530 /// the source range of the list.
531 struct CXX0XAttributeList {
532 AttributeList *AttrList;
535 CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
536 : AttrList(attrList), Range(range), HasAttr (hasAttr) {
538 CXX0XAttributeList ()
539 : AttrList(0), Range(), HasAttr(false) {
543 /// ParsedAttributes - A collection of parsed attributes. Currently
544 /// we don't differentiate between the various attribute syntaxes,
545 /// which is basically silly.
547 /// Right now this is a very lightweight container, but the expectation
548 /// is that this will become significantly more serious.
549 class ParsedAttributes {
551 ParsedAttributes(AttributeFactory &factory)
552 : pool(factory), list(0) {
555 ParsedAttributes(ParsedAttributes &attrs)
556 : pool(attrs.pool), list(attrs.list) {
560 AttributePool &getPool() const { return pool; }
562 bool empty() const { return list == 0; }
564 void add(AttributeList *newAttr) {
566 assert(newAttr->getNext() == 0);
567 newAttr->setNext(list);
571 void addAll(AttributeList *newList) {
572 if (!newList) return;
574 AttributeList *lastInNewList = newList;
575 while (AttributeList *next = lastInNewList->getNext())
576 lastInNewList = next;
578 lastInNewList->setNext(list);
582 void set(AttributeList *newList) {
586 void takeAllFrom(ParsedAttributes &attrs) {
589 pool.takeAllFrom(attrs.pool);
592 void clear() { list = 0; pool.clear(); }
593 AttributeList *getList() const { return list; }
595 /// Returns a reference to the attribute list. Try not to introduce
596 /// dependencies on this method, it may not be long-lived.
597 AttributeList *&getListRef() { return list; }
600 AttributeList *addNew(IdentifierInfo *attrName, SourceRange attrRange,
601 IdentifierInfo *scopeName, SourceLocation scopeLoc,
602 IdentifierInfo *parmName, SourceLocation parmLoc,
603 Expr **args, unsigned numArgs,
604 bool declspec = false, bool cxx0x = false) {
605 AttributeList *attr =
606 pool.create(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc,
607 args, numArgs, declspec, cxx0x);
612 AttributeList *addNew(IdentifierInfo *attrName, SourceRange attrRange,
613 IdentifierInfo *scopeName, SourceLocation scopeLoc,
614 IdentifierInfo *parmName, SourceLocation parmLoc,
615 const AvailabilityChange &introduced,
616 const AvailabilityChange &deprecated,
617 const AvailabilityChange &obsoleted,
618 SourceLocation unavailable,
619 bool declspec = false, bool cxx0x = false) {
620 AttributeList *attr =
621 pool.create(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc,
622 introduced, deprecated, obsoleted, unavailable,
628 AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name,
629 SourceLocation loc, int arg) {
630 AttributeList *attr =
631 pool.createIntegerAttribute(C, name, loc, arg);
638 mutable AttributePool pool;
642 } // end namespace clang