]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Sema/AttributeList.h
Upgrade our copy of llvm/clang to r130700, from upstream's trunk.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Sema / AttributeList.h
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.
43   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   /// \brief The location of the 'unavailable' keyword in an
81   /// availability attribute.
82   SourceLocation UnavailableLoc;
83
84   /// The next attribute in the current position.
85   AttributeList *NextInPosition;
86
87   /// The next attribute allocated in the current Pool.
88   AttributeList *NextInPool;
89
90   Expr **getArgsBuffer() {
91     return reinterpret_cast<Expr**>(this+1);
92   }
93   Expr * const *getArgsBuffer() const {
94     return reinterpret_cast<Expr* const *>(this+1);
95   }
96
97   enum AvailabilitySlot {
98     IntroducedSlot, DeprecatedSlot, ObsoletedSlot
99   };
100
101   AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) {
102     return reinterpret_cast<AvailabilityChange*>(this+1)[index];
103   }
104   const AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) const {
105     return reinterpret_cast<const AvailabilityChange*>(this+1)[index];
106   }
107
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
112
113   size_t allocated_size() const;
114
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),
122       NumArgs(numArgs),
123       DeclspecAttribute(declspec), CXX0XAttribute(cxx0x), Invalid(false),
124       IsAvailability(false), NextInPosition(0), NextInPool(0) {
125     if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(Expr*));
126   }
127
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);
144   }
145
146   friend class AttributePool;
147   friend class AttributeFactory;
148
149 public:
150   enum Kind {             // Please keep this list alphabetized.
151     AT_IBAction,          // Clang-specific.
152     AT_IBOutlet,          // Clang-specific.
153     AT_IBOutletCollection, // Clang-specific.
154     AT_address_space,
155     AT_alias,
156     AT_aligned,
157     AT_always_inline,
158     AT_analyzer_noreturn,
159     AT_annotate,
160     AT_availability,      // Clang-specific
161     AT_base_check,
162     AT_blocks,
163     AT_carries_dependency,
164     AT_cdecl,
165     AT_cleanup,
166     AT_common,
167     AT_const,
168     AT_constant,
169     AT_constructor,
170     AT_deprecated,
171     AT_destructor,
172     AT_device,
173     AT_dllexport,
174     AT_dllimport,
175     AT_ext_vector_type,
176     AT_fastcall,
177     AT_format,
178     AT_format_arg,
179     AT_global,
180     AT_gnu_inline,
181     AT_host,
182     AT_launch_bounds,
183     AT_malloc,
184     AT_may_alias,
185     AT_mode,
186     AT_neon_polyvector_type,    // Clang-specific.
187     AT_neon_vector_type,        // Clang-specific.
188     AT_naked,
189     AT_nodebug,
190     AT_noinline,
191     AT_no_instrument_function,
192     AT_nocommon,
193     AT_nonnull,
194     AT_noreturn,
195     AT_nothrow,
196     AT_nsobject,
197     AT_objc_exception,
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.
207     AT_objc_gc,
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.
214     AT_packed,
215     AT_pascal,
216     AT_pcs,  // ARM specific
217     AT_pure,
218     AT_regparm,
219     AT_section,
220     AT_sentinel,
221     AT_shared,
222     AT_stdcall,
223     AT_thiscall,
224     AT_transparent_union,
225     AT_unavailable,
226     AT_unused,
227     AT_used,
228     AT_uuid,
229     AT_vecreturn,     // PS3 PPU-specific.
230     AT_vector_size,
231     AT_visibility,
232     AT_warn_unused_result,
233     AT_weak,
234     AT_weakref,
235     AT_weak_import,
236     AT_reqd_wg_size,
237     AT_init_priority,
238     AT_MsStruct,
239     IgnoredAttribute,
240     UnknownAttribute
241   };
242
243   IdentifierInfo *getName() const { return AttrName; }
244   SourceLocation getLoc() const { return AttrLoc; }
245   
246   bool hasScope() const { return ScopeName; }
247   IdentifierInfo *getScopeName() const { return ScopeName; }
248   SourceLocation getScopeLoc() const { return ScopeLoc; }
249   
250   IdentifierInfo *getParameterName() const { return ParmName; }
251   SourceLocation getParameterLoc() const { return ParmLoc; }
252
253   bool isDeclspecAttribute() const { return DeclspecAttribute; }
254   bool isCXX0XAttribute() const { return CXX0XAttribute; }
255
256   bool isInvalid() const { return Invalid; }
257   void setInvalid(bool b = true) const { Invalid = b; }
258
259   Kind getKind() const { return getKind(getName()); }
260   static Kind getKind(const IdentifierInfo *Name);
261
262   AttributeList *getNext() const { return NextInPosition; }
263   void setNext(AttributeList *N) { NextInPosition = N; }
264
265   /// getNumArgs - Return the number of actual arguments to this attribute.
266   unsigned getNumArgs() const { return NumArgs; }
267
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; }
271
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];
276   }
277
278   class arg_iterator {
279     Expr * const *X;
280     unsigned Idx;
281   public:
282     arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {}
283
284     arg_iterator& operator++() {
285       ++Idx;
286       return *this;
287     }
288
289     bool operator==(const arg_iterator& I) const {
290       assert (X == I.X &&
291               "compared arg_iterators are for different argument lists");
292       return Idx == I.Idx;
293     }
294
295     bool operator!=(const arg_iterator& I) const {
296       return !operator==(I);
297     }
298
299     Expr* operator*() const {
300       return X[Idx];
301     }
302
303     unsigned getArgNum() const {
304       return Idx+1;
305     }
306   };
307
308   arg_iterator arg_begin() const {
309     return arg_iterator(getArgsBuffer(), 0);
310   }
311
312   arg_iterator arg_end() const {
313     return arg_iterator(getArgsBuffer(), NumArgs);
314   }
315
316   const AvailabilityChange &getAvailabilityIntroduced() const {
317     assert(getKind() == AT_availability && "Not an availability attribute");
318     return getAvailabilitySlot(IntroducedSlot);
319   }
320
321   const AvailabilityChange &getAvailabilityDeprecated() const {
322     assert(getKind() == AT_availability && "Not an availability attribute");
323     return getAvailabilitySlot(DeprecatedSlot);
324   }
325
326   const AvailabilityChange &getAvailabilityObsoleted() const {
327     assert(getKind() == AT_availability && "Not an availability attribute");
328     return getAvailabilitySlot(ObsoletedSlot);
329   }
330
331   SourceLocation getUnavailableLoc() const {
332     assert(getKind() == AT_availability && "Not an availability attribute");
333     return UnavailableLoc;
334   }
335 };
336
337 /// A factory, from which one makes pools, from which one creates
338 /// individual attributes which are deallocated with the pool.
339 ///
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 {
343 public:
344   enum {
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*))
351   };
352
353 private:
354   enum {
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*)
362   };
363
364   llvm::BumpPtrAllocator Alloc;
365
366   /// Free lists.  The index is determined by the following formula:
367   ///   (size - sizeof(AttributeList)) / sizeof(void*)
368   llvm::SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists;
369
370   // The following are the private interface used by AttributePool.
371   friend class AttributePool;
372
373   /// Allocate an attribute of the given size.
374   void *allocate(size_t size);
375
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);
382
383 public:
384   AttributeFactory();
385   ~AttributeFactory();
386 };
387
388 class AttributePool {
389   AttributeFactory &Factory;
390   AttributeList *Head;
391
392   void *allocate(size_t size) {
393     return Factory.allocate(size);
394   }
395
396   AttributeList *add(AttributeList *attr) {
397     // We don't care about the order of the pool.
398     attr->NextInPool = Head;
399     Head = attr;
400     return attr;
401   }
402
403   void takePool(AttributeList *pool);
404
405 public:
406   /// Create a new pool for a factory.
407   AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {}
408
409   /// Move the given pool's allocations to this pool.
410   AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) {
411     pool.Head = 0;
412   }
413
414   AttributeFactory &getFactory() const { return Factory; }
415
416   void clear() {
417     if (Head) {
418       Factory.reclaimPool(Head);
419       Head = 0;
420     }
421   }
422
423   /// Take the given pool's allocations and add them to this pool.
424   void takeAllFrom(AttributePool &pool) {
425     if (pool.Head) {
426       takePool(pool.Head);
427       pool.Head = 0;
428     }
429   }
430
431   ~AttributePool() {
432     if (Head) Factory.reclaimPool(Head);
433   }
434
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,
443                                           scopeName, scopeLoc,
444                                           parmName, parmLoc,
445                                           args, numArgs,
446                                           declspec, cxx0x));
447   }
448
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,
459                                           scopeName, scopeLoc,
460                                           parmName, parmLoc,
461                                           introduced, deprecated, obsoleted,
462                                           unavailable,
463                                           declspec, cxx0x));
464   }
465
466   AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
467                                         SourceLocation TokLoc, int Arg);
468 };
469
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) {
476   if (!Left)
477     return Right;
478
479   AttributeList *next = Left, *prev;
480   do {
481     prev = next;
482     next = next->getNext();
483   } while (next);
484   prev->setNext(Right);
485   return Left;
486 }
487
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;
494   SourceRange Range;
495   bool HasAttr;
496   CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
497     : AttrList(attrList), Range(range), HasAttr (hasAttr) {
498   }
499   CXX0XAttributeList ()
500     : AttrList(0), Range(), HasAttr(false) {
501   }
502 };
503
504 /// ParsedAttributes - A collection of parsed attributes.  Currently
505 /// we don't differentiate between the various attribute syntaxes,
506 /// which is basically silly.
507 ///
508 /// Right now this is a very lightweight container, but the expectation
509 /// is that this will become significantly more serious.
510 class ParsedAttributes {
511 public:
512   ParsedAttributes(AttributeFactory &factory)
513     : pool(factory), list(0) {
514   }
515
516   ParsedAttributes(ParsedAttributes &attrs)
517     : pool(attrs.pool), list(attrs.list) {
518     attrs.list = 0;
519   }
520
521   AttributePool &getPool() const { return pool; }
522
523   bool empty() const { return list == 0; }
524
525   void add(AttributeList *newAttr) {
526     assert(newAttr);
527     assert(newAttr->getNext() == 0);
528     newAttr->setNext(list);
529     list = newAttr;
530   }
531
532   void addAll(AttributeList *newList) {
533     if (!newList) return;
534
535     AttributeList *lastInNewList = newList;
536     while (AttributeList *next = lastInNewList->getNext())
537       lastInNewList = next;
538
539     lastInNewList->setNext(list);
540     list = newList;
541   }
542
543   void set(AttributeList *newList) {
544     list = newList;
545   }
546
547   void takeAllFrom(ParsedAttributes &attrs) {
548     addAll(attrs.list);
549     attrs.list = 0;
550     pool.takeAllFrom(attrs.pool);
551   }
552
553   void clear() { list = 0; pool.clear(); }
554   AttributeList *getList() const { return list; }
555
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; }
559
560
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);
569     add(attr);
570     return attr;
571   }
572
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,
584                   declspec, cxx0x);
585     add(attr);
586     return attr;
587   }
588
589   AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name,
590                                SourceLocation loc, int arg) {
591     AttributeList *attr =
592       pool.createIntegerAttribute(C, name, loc, arg);
593     add(attr);
594     return attr;
595   }
596
597
598 private:
599   mutable AttributePool pool;
600   AttributeList *list;
601 };
602
603 }  // end namespace clang
604
605 #endif