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 /// \brief The location of the 'unavailable' keyword in an
81 /// availability attribute.
82 SourceLocation UnavailableLoc;
84 /// The next attribute in the current position.
85 AttributeList *NextInPosition;
87 /// The next attribute allocated in the current Pool.
88 AttributeList *NextInPool;
90 Expr **getArgsBuffer() {
91 return reinterpret_cast<Expr**>(this+1);
93 Expr * const *getArgsBuffer() const {
94 return reinterpret_cast<Expr* const *>(this+1);
97 enum AvailabilitySlot {
98 IntroducedSlot, DeprecatedSlot, ObsoletedSlot
101 AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) {
102 return reinterpret_cast<AvailabilityChange*>(this+1)[index];
104 const AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) const {
105 return reinterpret_cast<const AvailabilityChange*>(this+1)[index];
108 AttributeList(const AttributeList &); // DO NOT IMPLEMENT
109 void operator=(const AttributeList &); // DO NOT IMPLEMENT
110 void operator delete(void *); // DO NOT IMPLEMENT
111 ~AttributeList(); // DO NOT IMPLEMENT
113 size_t allocated_size() const;
115 AttributeList(IdentifierInfo *attrName, SourceLocation attrLoc,
116 IdentifierInfo *scopeName, SourceLocation scopeLoc,
117 IdentifierInfo *parmName, SourceLocation parmLoc,
118 Expr **args, unsigned numArgs,
119 bool declspec, bool cxx0x)
120 : AttrName(attrName), ScopeName(scopeName), ParmName(parmName),
121 AttrLoc(attrLoc), ScopeLoc(scopeLoc), ParmLoc(parmLoc),
123 DeclspecAttribute(declspec), CXX0XAttribute(cxx0x), Invalid(false),
124 IsAvailability(false), NextInPosition(0), NextInPool(0) {
125 if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(Expr*));
128 AttributeList(IdentifierInfo *attrName, SourceLocation attrLoc,
129 IdentifierInfo *scopeName, SourceLocation scopeLoc,
130 IdentifierInfo *parmName, SourceLocation parmLoc,
131 const AvailabilityChange &introduced,
132 const AvailabilityChange &deprecated,
133 const AvailabilityChange &obsoleted,
134 SourceLocation unavailable,
135 bool declspec, bool cxx0x)
136 : AttrName(attrName), ScopeName(scopeName), ParmName(parmName),
137 AttrLoc(attrLoc), ScopeLoc(scopeLoc), ParmLoc(parmLoc),
138 NumArgs(0), DeclspecAttribute(declspec), CXX0XAttribute(cxx0x),
139 Invalid(false), IsAvailability(true), UnavailableLoc(unavailable),
140 NextInPosition(0), NextInPool(0) {
141 new (&getAvailabilitySlot(IntroducedSlot)) AvailabilityChange(introduced);
142 new (&getAvailabilitySlot(DeprecatedSlot)) AvailabilityChange(deprecated);
143 new (&getAvailabilitySlot(ObsoletedSlot)) AvailabilityChange(obsoleted);
146 friend class AttributePool;
147 friend class AttributeFactory;
150 enum Kind { // Please keep this list alphabetized.
151 AT_IBAction, // Clang-specific.
152 AT_IBOutlet, // Clang-specific.
153 AT_IBOutletCollection, // Clang-specific.
158 AT_analyzer_noreturn,
160 AT_availability, // Clang-specific
163 AT_carries_dependency,
186 AT_neon_polyvector_type, // Clang-specific.
187 AT_neon_vector_type, // Clang-specific.
191 AT_no_instrument_function,
198 AT_objc_method_family,
199 AT_cf_returns_not_retained, // Clang-specific.
200 AT_cf_returns_retained, // Clang-specific.
201 AT_ns_returns_not_retained, // Clang-specific.
202 AT_ns_returns_retained, // Clang-specific.
203 AT_ns_returns_autoreleased, // Clang-specific.
204 AT_cf_consumed, // Clang-specific.
205 AT_ns_consumed, // Clang-specific.
206 AT_ns_consumes_self, // Clang-specific.
208 AT_opencl_image_access, // OpenCL-specific.
209 AT_opencl_kernel_function, // OpenCL-specific.
210 AT_overloadable, // Clang-specific.
211 AT_ownership_holds, // Clang-specific.
212 AT_ownership_returns, // Clang-specific.
213 AT_ownership_takes, // Clang-specific.
216 AT_pcs, // ARM specific
224 AT_transparent_union,
229 AT_vecreturn, // PS3 PPU-specific.
232 AT_warn_unused_result,
243 IdentifierInfo *getName() const { return AttrName; }
244 SourceLocation getLoc() const { return AttrLoc; }
246 bool hasScope() const { return ScopeName; }
247 IdentifierInfo *getScopeName() const { return ScopeName; }
248 SourceLocation getScopeLoc() const { return ScopeLoc; }
250 IdentifierInfo *getParameterName() const { return ParmName; }
251 SourceLocation getParameterLoc() const { return ParmLoc; }
253 bool isDeclspecAttribute() const { return DeclspecAttribute; }
254 bool isCXX0XAttribute() const { return CXX0XAttribute; }
256 bool isInvalid() const { return Invalid; }
257 void setInvalid(bool b = true) const { Invalid = b; }
259 Kind getKind() const { return getKind(getName()); }
260 static Kind getKind(const IdentifierInfo *Name);
262 AttributeList *getNext() const { return NextInPosition; }
263 void setNext(AttributeList *N) { NextInPosition = N; }
265 /// getNumArgs - Return the number of actual arguments to this attribute.
266 unsigned getNumArgs() const { return NumArgs; }
268 /// hasParameterOrArguments - Return true if this attribute has a parameter,
269 /// or has a non empty argument expression list.
270 bool hasParameterOrArguments() const { return ParmName || NumArgs; }
272 /// getArg - Return the specified argument.
273 Expr *getArg(unsigned Arg) const {
274 assert(Arg < NumArgs && "Arg access out of range!");
275 return getArgsBuffer()[Arg];
282 arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {}
284 arg_iterator& operator++() {
289 bool operator==(const arg_iterator& I) const {
291 "compared arg_iterators are for different argument lists");
295 bool operator!=(const arg_iterator& I) const {
296 return !operator==(I);
299 Expr* operator*() const {
303 unsigned getArgNum() const {
308 arg_iterator arg_begin() const {
309 return arg_iterator(getArgsBuffer(), 0);
312 arg_iterator arg_end() const {
313 return arg_iterator(getArgsBuffer(), NumArgs);
316 const AvailabilityChange &getAvailabilityIntroduced() const {
317 assert(getKind() == AT_availability && "Not an availability attribute");
318 return getAvailabilitySlot(IntroducedSlot);
321 const AvailabilityChange &getAvailabilityDeprecated() const {
322 assert(getKind() == AT_availability && "Not an availability attribute");
323 return getAvailabilitySlot(DeprecatedSlot);
326 const AvailabilityChange &getAvailabilityObsoleted() const {
327 assert(getKind() == AT_availability && "Not an availability attribute");
328 return getAvailabilitySlot(ObsoletedSlot);
331 SourceLocation getUnavailableLoc() const {
332 assert(getKind() == AT_availability && "Not an availability attribute");
333 return UnavailableLoc;
337 /// A factory, from which one makes pools, from which one creates
338 /// individual attributes which are deallocated with the pool.
340 /// Note that it's tolerably cheap to create and destroy one of
341 /// these as long as you don't actually allocate anything in it.
342 class AttributeFactory {
345 /// The required allocation size of an availability attribute,
346 /// which we want to ensure is a multiple of sizeof(void*).
347 AvailabilityAllocSize =
348 sizeof(AttributeList)
349 + ((3 * sizeof(AvailabilityChange) + sizeof(void*) - 1)
350 / sizeof(void*) * sizeof(void*))
355 /// The number of free lists we want to be sure to support
356 /// inline. This is just enough that availability attributes
357 /// don't surpass it. It's actually very unlikely we'll see an
358 /// attribute that needs more than that; on x86-64 you'd need 10
359 /// expression arguments, and on i386 you'd need 19.
360 InlineFreeListsCapacity =
361 1 + (AvailabilityAllocSize - sizeof(AttributeList)) / sizeof(void*)
364 llvm::BumpPtrAllocator Alloc;
366 /// Free lists. The index is determined by the following formula:
367 /// (size - sizeof(AttributeList)) / sizeof(void*)
368 llvm::SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists;
370 // The following are the private interface used by AttributePool.
371 friend class AttributePool;
373 /// Allocate an attribute of the given size.
374 void *allocate(size_t size);
376 /// Reclaim all the attributes in the given pool chain, which is
377 /// non-empty. Note that the current implementation is safe
378 /// against reclaiming things which were not actually allocated
379 /// with the allocator, although of course it's important to make
380 /// sure that their allocator lives at least as long as this one.
381 void reclaimPool(AttributeList *head);
388 class AttributePool {
389 AttributeFactory &Factory;
392 void *allocate(size_t size) {
393 return Factory.allocate(size);
396 AttributeList *add(AttributeList *attr) {
397 // We don't care about the order of the pool.
398 attr->NextInPool = Head;
403 void takePool(AttributeList *pool);
406 /// Create a new pool for a factory.
407 AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {}
409 /// Move the given pool's allocations to this pool.
410 AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) {
414 AttributeFactory &getFactory() const { return Factory; }
418 Factory.reclaimPool(Head);
423 /// Take the given pool's allocations and add them to this pool.
424 void takeAllFrom(AttributePool &pool) {
432 if (Head) Factory.reclaimPool(Head);
435 AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc,
436 IdentifierInfo *scopeName, SourceLocation scopeLoc,
437 IdentifierInfo *parmName, SourceLocation parmLoc,
438 Expr **args, unsigned numArgs,
439 bool declspec = false, bool cxx0x = false) {
440 void *memory = allocate(sizeof(AttributeList)
441 + numArgs * sizeof(Expr*));
442 return add(new (memory) AttributeList(attrName, attrLoc,
449 AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc,
450 IdentifierInfo *scopeName, SourceLocation scopeLoc,
451 IdentifierInfo *parmName, SourceLocation parmLoc,
452 const AvailabilityChange &introduced,
453 const AvailabilityChange &deprecated,
454 const AvailabilityChange &obsoleted,
455 SourceLocation unavailable,
456 bool declspec = false, bool cxx0x = false) {
457 void *memory = allocate(AttributeFactory::AvailabilityAllocSize);
458 return add(new (memory) AttributeList(attrName, attrLoc,
461 introduced, deprecated, obsoleted,
466 AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
467 SourceLocation TokLoc, int Arg);
470 /// addAttributeLists - Add two AttributeLists together
471 /// The right-hand list is appended to the left-hand list, if any
472 /// A pointer to the joined list is returned.
473 /// Note: the lists are not left unmodified.
474 inline AttributeList *addAttributeLists(AttributeList *Left,
475 AttributeList *Right) {
479 AttributeList *next = Left, *prev;
482 next = next->getNext();
484 prev->setNext(Right);
488 /// CXX0XAttributeList - A wrapper around a C++0x attribute list.
489 /// Stores, in addition to the list proper, whether or not an actual list was
490 /// (as opposed to an empty list, which may be ill-formed in some places) and
491 /// the source range of the list.
492 struct CXX0XAttributeList {
493 AttributeList *AttrList;
496 CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
497 : AttrList(attrList), Range(range), HasAttr (hasAttr) {
499 CXX0XAttributeList ()
500 : AttrList(0), Range(), HasAttr(false) {
504 /// ParsedAttributes - A collection of parsed attributes. Currently
505 /// we don't differentiate between the various attribute syntaxes,
506 /// which is basically silly.
508 /// Right now this is a very lightweight container, but the expectation
509 /// is that this will become significantly more serious.
510 class ParsedAttributes {
512 ParsedAttributes(AttributeFactory &factory)
513 : pool(factory), list(0) {
516 ParsedAttributes(ParsedAttributes &attrs)
517 : pool(attrs.pool), list(attrs.list) {
521 AttributePool &getPool() const { return pool; }
523 bool empty() const { return list == 0; }
525 void add(AttributeList *newAttr) {
527 assert(newAttr->getNext() == 0);
528 newAttr->setNext(list);
532 void addAll(AttributeList *newList) {
533 if (!newList) return;
535 AttributeList *lastInNewList = newList;
536 while (AttributeList *next = lastInNewList->getNext())
537 lastInNewList = next;
539 lastInNewList->setNext(list);
543 void set(AttributeList *newList) {
547 void takeAllFrom(ParsedAttributes &attrs) {
550 pool.takeAllFrom(attrs.pool);
553 void clear() { list = 0; pool.clear(); }
554 AttributeList *getList() const { return list; }
556 /// Returns a reference to the attribute list. Try not to introduce
557 /// dependencies on this method, it may not be long-lived.
558 AttributeList *&getListRef() { return list; }
561 AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc,
562 IdentifierInfo *scopeName, SourceLocation scopeLoc,
563 IdentifierInfo *parmName, SourceLocation parmLoc,
564 Expr **args, unsigned numArgs,
565 bool declspec = false, bool cxx0x = false) {
566 AttributeList *attr =
567 pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc,
568 args, numArgs, declspec, cxx0x);
573 AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc,
574 IdentifierInfo *scopeName, SourceLocation scopeLoc,
575 IdentifierInfo *parmName, SourceLocation parmLoc,
576 const AvailabilityChange &introduced,
577 const AvailabilityChange &deprecated,
578 const AvailabilityChange &obsoleted,
579 SourceLocation unavailable,
580 bool declspec = false, bool cxx0x = false) {
581 AttributeList *attr =
582 pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc,
583 introduced, deprecated, obsoleted, unavailable,
589 AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name,
590 SourceLocation loc, int arg) {
591 AttributeList *attr =
592 pool.createIntegerAttribute(C, name, loc, arg);
599 mutable AttributePool pool;
603 } // end namespace clang