1 //===--- AttributeList.h - Parsed attribute sets ----------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the AttributeList class, which is used to collect
11 // parsed attributes.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_SEMA_ATTRLIST_H
16 #define LLVM_CLANG_SEMA_ATTRLIST_H
17
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/VersionTuple.h"
22 #include <cassert>
23
24 namespace clang {
25 class ASTContext;
26 class IdentifierInfo;
27 class Expr;
28
29 /// \brief Represents information about a change in availability for
30 /// an entity, which is part of the encoding of the 'availability'
31 /// attribute.
32 struct AvailabilityChange {
33 /// \brief The location of the keyword indicating the kind of change.
34 SourceLocation KeywordLoc;
35
36 /// \brief The version number at which the change occurred.
37 VersionTuple Version;
38
39 /// \brief The source range covering the version number.
40 SourceRange VersionRange;
41
42 /// \brief Determine whether this availability change is valid.
isValidAvailabilityChange43 bool isValid() const { return !Version.empty(); }
44 };
45
46 /// AttributeList - Represents GCC's __attribute__ declaration. There are
47 /// 4 forms of this construct...they are:
48 ///
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.
53 ///
54 class AttributeList { // TODO: This should really be called ParsedAttribute
55 private:
56 IdentifierInfo *AttrName;
57 IdentifierInfo *ScopeName;
58 IdentifierInfo *ParmName;
59 SourceLocation AttrLoc;
60 SourceLocation ScopeLoc;
61 SourceLocation ParmLoc;
62
63 /// The number of expression arguments this attribute has.
64 /// The expressions themselves are stored after the object.
65 unsigned NumArgs : 16;
66
67 /// True if Microsoft style: declspec(foo).
68 unsigned DeclspecAttribute : 1;
69
70 /// True if C++0x-style: [[foo]].
71 unsigned CXX0XAttribute : 1;
72
73 /// True if already diagnosed as invalid.
74 mutable unsigned Invalid : 1;
75
76 /// True if this has the extra information associated with an
77 /// availability attribute.
78 unsigned IsAvailability : 1;
79
80 unsigned AttrKind : 8;
81
82 /// \brief The location of the 'unavailable' keyword in an
83 /// availability attribute.
84 SourceLocation UnavailableLoc;
85
86 /// The next attribute in the current position.
87 AttributeList *NextInPosition;
88
89 /// The next attribute allocated in the current Pool.
90 AttributeList *NextInPool;
91
getArgsBuffer()92 Expr **getArgsBuffer() {
93 return reinterpret_cast<Expr**>(this+1);
94 }
getArgsBuffer()95 Expr * const *getArgsBuffer() const {
96 return reinterpret_cast<Expr* const *>(this+1);
97 }
98
99 enum AvailabilitySlot {
100 IntroducedSlot, DeprecatedSlot, ObsoletedSlot
101 };
102
getAvailabilitySlot(AvailabilitySlot index)103 AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) {
104 return reinterpret_cast<AvailabilityChange*>(this+1)[index];
105 }
getAvailabilitySlot(AvailabilitySlot index)106 const AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) const {
107 return reinterpret_cast<const AvailabilityChange*>(this+1)[index];
108 }
109
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
114
115 size_t allocated_size() const;
116
AttributeList(IdentifierInfo * attrName,SourceLocation attrLoc,IdentifierInfo * scopeName,SourceLocation scopeLoc,IdentifierInfo * parmName,SourceLocation parmLoc,Expr ** args,unsigned numArgs,bool declspec,bool cxx0x)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),
124 NumArgs(numArgs),
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());
129 }
130
AttributeList(IdentifierInfo * attrName,SourceLocation attrLoc,IdentifierInfo * scopeName,SourceLocation scopeLoc,IdentifierInfo * parmName,SourceLocation parmLoc,const AvailabilityChange & introduced,const AvailabilityChange & deprecated,const AvailabilityChange & obsoleted,SourceLocation unavailable,bool declspec,bool cxx0x)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());
148 }
149
150 friend class AttributePool;
151 friend class AttributeFactory;
152
153 public:
154 enum Kind { // Please keep this list alphabetized.
155 AT_address_space,
156 AT_alias,
157 AT_aligned,
158 AT_always_inline,
159 AT_analyzer_noreturn,
160 AT_annotate,
161 AT_arc_weakref_unavailable,
162 AT_availability, // Clang-specific
163 AT_base_check,
164 AT_blocks,
165 AT_carries_dependency,
166 AT_cdecl,
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.
171 AT_cleanup,
172 AT_common,
173 AT_const,
174 AT_constant,
175 AT_constructor,
176 AT_deprecated,
177 AT_destructor,
178 AT_device,
179 AT_dllexport,
180 AT_dllimport,
181 AT_ext_vector_type,
182 AT_fastcall,
183 AT_format,
184 AT_format_arg,
185 AT_global,
186 AT_gnu_inline,
187 AT_host,
188 AT_IBAction, // Clang-specific.
189 AT_IBOutlet, // Clang-specific.
190 AT_IBOutletCollection, // Clang-specific.
191 AT_init_priority,
192 AT_launch_bounds,
193 AT_malloc,
194 AT_may_alias,
195 AT_mode,
196 AT_MsStruct,
197 AT_naked,
198 AT_neon_polyvector_type, // Clang-specific.
199 AT_neon_vector_type, // Clang-specific.
200 AT_no_instrument_function,
201 AT_nocommon,
202 AT_nodebug,
203 AT_noinline,
204 AT_nonnull,
205 AT_noreturn,
206 AT_nothrow,
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.
212 AT_nsobject,
213 AT_objc_exception,
214 AT_objc_gc,
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.
224 AT_packed,
225 AT_pascal,
226 AT_pcs, // ARM specific
227 AT_pure,
228 AT_regparm,
229 AT_reqd_wg_size,
230 AT_section,
231 AT_sentinel,
232 AT_shared,
233 AT_stdcall,
234 AT_thiscall,
235 AT_transparent_union,
236 AT_unavailable,
237 AT_unused,
238 AT_used,
239 AT_uuid,
240 AT_vecreturn, // PS3 PPU-specific.
241 AT_vector_size,
242 AT_visibility,
243 AT_warn_unused_result,
244 AT_weak,
245 AT_weak_import,
246 AT_weakref,
247 IgnoredAttribute,
248 UnknownAttribute
249 };
250
getName()251 IdentifierInfo *getName() const { return AttrName; }
getLoc()252 SourceLocation getLoc() const { return AttrLoc; }
253
hasScope()254 bool hasScope() const { return ScopeName; }
getScopeName()255 IdentifierInfo *getScopeName() const { return ScopeName; }
getScopeLoc()256 SourceLocation getScopeLoc() const { return ScopeLoc; }
257
getParameterName()258 IdentifierInfo *getParameterName() const { return ParmName; }
getParameterLoc()259 SourceLocation getParameterLoc() const { return ParmLoc; }
260
isDeclspecAttribute()261 bool isDeclspecAttribute() const { return DeclspecAttribute; }
isCXX0XAttribute()262 bool isCXX0XAttribute() const { return CXX0XAttribute; }
263
isInvalid()264 bool isInvalid() const { return Invalid; }
265 void setInvalid(bool b = true) const { Invalid = b; }
266
getKind()267 Kind getKind() const { return Kind(AttrKind); }
268 static Kind getKind(const IdentifierInfo *Name);
269
getNext()270 AttributeList *getNext() const { return NextInPosition; }
setNext(AttributeList * N)271 void setNext(AttributeList *N) { NextInPosition = N; }
272
273 /// getNumArgs - Return the number of actual arguments to this attribute.
getNumArgs()274 unsigned getNumArgs() const { return NumArgs; }
275
276 /// hasParameterOrArguments - Return true if this attribute has a parameter,
277 /// or has a non empty argument expression list.
hasParameterOrArguments()278 bool hasParameterOrArguments() const { return ParmName || NumArgs; }
279
280 /// getArg - Return the specified argument.
getArg(unsigned Arg)281 Expr *getArg(unsigned Arg) const {
282 assert(Arg < NumArgs && "Arg access out of range!");
283 return getArgsBuffer()[Arg];
284 }
285
286 class arg_iterator {
287 Expr * const *X;
288 unsigned Idx;
289 public:
arg_iterator(Expr * const * x,unsigned idx)290 arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {}
291
292 arg_iterator& operator++() {
293 ++Idx;
294 return *this;
295 }
296
297 bool operator==(const arg_iterator& I) const {
298 assert (X == I.X &&
299 "compared arg_iterators are for different argument lists");
300 return Idx == I.Idx;
301 }
302
303 bool operator!=(const arg_iterator& I) const {
304 return !operator==(I);
305 }
306
307 Expr* operator*() const {
308 return X[Idx];
309 }
310
getArgNum()311 unsigned getArgNum() const {
312 return Idx+1;
313 }
314 };
315
arg_begin()316 arg_iterator arg_begin() const {
317 return arg_iterator(getArgsBuffer(), 0);
318 }
319
arg_end()320 arg_iterator arg_end() const {
321 return arg_iterator(getArgsBuffer(), NumArgs);
322 }
323
getAvailabilityIntroduced()324 const AvailabilityChange &getAvailabilityIntroduced() const {
325 assert(getKind() == AT_availability && "Not an availability attribute");
326 return getAvailabilitySlot(IntroducedSlot);
327 }
328
getAvailabilityDeprecated()329 const AvailabilityChange &getAvailabilityDeprecated() const {
330 assert(getKind() == AT_availability && "Not an availability attribute");
331 return getAvailabilitySlot(DeprecatedSlot);
332 }
333
getAvailabilityObsoleted()334 const AvailabilityChange &getAvailabilityObsoleted() const {
335 assert(getKind() == AT_availability && "Not an availability attribute");
336 return getAvailabilitySlot(ObsoletedSlot);
337 }
338
getUnavailableLoc()339 SourceLocation getUnavailableLoc() const {
340 assert(getKind() == AT_availability && "Not an availability attribute");
341 return UnavailableLoc;
342 }
343 };
344
345 /// A factory, from which one makes pools, from which one creates
346 /// individual attributes which are deallocated with the pool.
347 ///
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 {
351 public:
352 enum {
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*))
359 };
360
361 private:
362 enum {
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*)
370 };
371
372 llvm::BumpPtrAllocator Alloc;
373
374 /// Free lists. The index is determined by the following formula:
375 /// (size - sizeof(AttributeList)) / sizeof(void*)
376 llvm::SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists;
377
378 // The following are the private interface used by AttributePool.
379 friend class AttributePool;
380
381 /// Allocate an attribute of the given size.
382 void *allocate(size_t size);
383
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);
390
391 public:
392 AttributeFactory();
393 ~AttributeFactory();
394 };
395
396 class AttributePool {
397 AttributeFactory &Factory;
398 AttributeList *Head;
399
allocate(size_t size)400 void *allocate(size_t size) {
401 return Factory.allocate(size);
402 }
403
add(AttributeList * attr)404 AttributeList *add(AttributeList *attr) {
405 // We don't care about the order of the pool.
406 attr->NextInPool = Head;
407 Head = attr;
408 return attr;
409 }
410
411 void takePool(AttributeList *pool);
412
413 public:
414 /// Create a new pool for a factory.
AttributePool(AttributeFactory & factory)415 AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {}
416
417 /// Move the given pool's allocations to this pool.
AttributePool(AttributePool & pool)418 AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) {
419 pool.Head = 0;
420 }
421
getFactory()422 AttributeFactory &getFactory() const { return Factory; }
423
clear()424 void clear() {
425 if (Head) {
426 Factory.reclaimPool(Head);
427 Head = 0;
428 }
429 }
430
431 /// Take the given pool's allocations and add them to this pool.
takeAllFrom(AttributePool & pool)432 void takeAllFrom(AttributePool &pool) {
433 if (pool.Head) {
434 takePool(pool.Head);
435 pool.Head = 0;
436 }
437 }
438
~AttributePool()439 ~AttributePool() {
440 if (Head) Factory.reclaimPool(Head);
441 }
442
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,
451 scopeName, scopeLoc,
452 parmName, parmLoc,
453 args, numArgs,
454 declspec, cxx0x));
455 }
456
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,
467 scopeName, scopeLoc,
468 parmName, parmLoc,
469 introduced, deprecated, obsoleted,
470 unavailable,
471 declspec, cxx0x));
472 }
473
474 AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
475 SourceLocation TokLoc, int Arg);
476 };
477
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.
addAttributeLists(AttributeList * Left,AttributeList * Right)482 inline AttributeList *addAttributeLists(AttributeList *Left,
483 AttributeList *Right) {
484 if (!Left)
485 return Right;
486
487 AttributeList *next = Left, *prev;
488 do {
489 prev = next;
490 next = next->getNext();
491 } while (next);
492 prev->setNext(Right);
493 return Left;
494 }
495
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;
502 SourceRange Range;
503 bool HasAttr;
CXX0XAttributeListCXX0XAttributeList504 CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
505 : AttrList(attrList), Range(range), HasAttr (hasAttr) {
506 }
CXX0XAttributeListCXX0XAttributeList507 CXX0XAttributeList ()
508 : AttrList(0), Range(), HasAttr(false) {
509 }
510 };
511
512 /// ParsedAttributes - A collection of parsed attributes. Currently
513 /// we don't differentiate between the various attribute syntaxes,
514 /// which is basically silly.
515 ///
516 /// Right now this is a very lightweight container, but the expectation
517 /// is that this will become significantly more serious.
518 class ParsedAttributes {
519 public:
ParsedAttributes(AttributeFactory & factory)520 ParsedAttributes(AttributeFactory &factory)
521 : pool(factory), list(0) {
522 }
523
ParsedAttributes(ParsedAttributes & attrs)524 ParsedAttributes(ParsedAttributes &attrs)
525 : pool(attrs.pool), list(attrs.list) {
526 attrs.list = 0;
527 }
528
getPool()529 AttributePool &getPool() const { return pool; }
530
empty()531 bool empty() const { return list == 0; }
532
add(AttributeList * newAttr)533 void add(AttributeList *newAttr) {
534 assert(newAttr);
535 assert(newAttr->getNext() == 0);
536 newAttr->setNext(list);
537 list = newAttr;
538 }
539
addAll(AttributeList * newList)540 void addAll(AttributeList *newList) {
541 if (!newList) return;
542
543 AttributeList *lastInNewList = newList;
544 while (AttributeList *next = lastInNewList->getNext())
545 lastInNewList = next;
546
547 lastInNewList->setNext(list);
548 list = newList;
549 }
550
set(AttributeList * newList)551 void set(AttributeList *newList) {
552 list = newList;
553 }
554
takeAllFrom(ParsedAttributes & attrs)555 void takeAllFrom(ParsedAttributes &attrs) {
556 addAll(attrs.list);
557 attrs.list = 0;
558 pool.takeAllFrom(attrs.pool);
559 }
560
clear()561 void clear() { list = 0; pool.clear(); }
getList()562 AttributeList *getList() const { return list; }
563
564 /// Returns a reference to the attribute list. Try not to introduce
565 /// dependencies on this method, it may not be long-lived.
getListRef()566 AttributeList *&getListRef() { return list; }
567
568
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);
577 add(attr);
578 return attr;
579 }
580
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,
592 declspec, cxx0x);
593 add(attr);
594 return attr;
595 }
596
addNewInteger(ASTContext & C,IdentifierInfo * name,SourceLocation loc,int arg)597 AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name,
598 SourceLocation loc, int arg) {
599 AttributeList *attr =
600 pool.createIntegerAttribute(C, name, loc, arg);
601 add(attr);
602 return attr;
603 }
604
605
606 private:
607 mutable AttributePool pool;
608 AttributeList *list;
609 };
610
611 } // end namespace clang
612
613 #endif
614