AttributeList.h revision 414d8967e1d760ea1e19a4aca96b13777a8cf8c5
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
24namespace 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.
32struct 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///
54class AttributeList { // TODO: This should really be called ParsedAttribute
55private:
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
149public:
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    IgnoredAttribute,
239    UnknownAttribute
240  };
241
242  IdentifierInfo *getName() const { return AttrName; }
243  SourceLocation getLoc() const { return AttrLoc; }
244
245  bool hasScope() const { return ScopeName; }
246  IdentifierInfo *getScopeName() const { return ScopeName; }
247  SourceLocation getScopeLoc() const { return ScopeLoc; }
248
249  IdentifierInfo *getParameterName() const { return ParmName; }
250  SourceLocation getParameterLoc() const { return ParmLoc; }
251
252  bool isDeclspecAttribute() const { return DeclspecAttribute; }
253  bool isCXX0XAttribute() const { return CXX0XAttribute; }
254
255  bool isInvalid() const { return Invalid; }
256  void setInvalid(bool b = true) const { Invalid = b; }
257
258  Kind getKind() const { return getKind(getName()); }
259  static Kind getKind(const IdentifierInfo *Name);
260
261  AttributeList *getNext() const { return NextInPosition; }
262  void setNext(AttributeList *N) { NextInPosition = N; }
263
264  /// getNumArgs - Return the number of actual arguments to this attribute.
265  unsigned getNumArgs() const { return NumArgs; }
266
267  /// getArg - Return the specified argument.
268  Expr *getArg(unsigned Arg) const {
269    assert(Arg < NumArgs && "Arg access out of range!");
270    return getArgsBuffer()[Arg];
271  }
272
273  class arg_iterator {
274    Expr * const *X;
275    unsigned Idx;
276  public:
277    arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {}
278
279    arg_iterator& operator++() {
280      ++Idx;
281      return *this;
282    }
283
284    bool operator==(const arg_iterator& I) const {
285      assert (X == I.X &&
286              "compared arg_iterators are for different argument lists");
287      return Idx == I.Idx;
288    }
289
290    bool operator!=(const arg_iterator& I) const {
291      return !operator==(I);
292    }
293
294    Expr* operator*() const {
295      return X[Idx];
296    }
297
298    unsigned getArgNum() const {
299      return Idx+1;
300    }
301  };
302
303  arg_iterator arg_begin() const {
304    return arg_iterator(getArgsBuffer(), 0);
305  }
306
307  arg_iterator arg_end() const {
308    return arg_iterator(getArgsBuffer(), NumArgs);
309  }
310
311  const AvailabilityChange &getAvailabilityIntroduced() const {
312    assert(getKind() == AT_availability && "Not an availability attribute");
313    return getAvailabilitySlot(IntroducedSlot);
314  }
315
316  const AvailabilityChange &getAvailabilityDeprecated() const {
317    assert(getKind() == AT_availability && "Not an availability attribute");
318    return getAvailabilitySlot(DeprecatedSlot);
319  }
320
321  const AvailabilityChange &getAvailabilityObsoleted() const {
322    assert(getKind() == AT_availability && "Not an availability attribute");
323    return getAvailabilitySlot(ObsoletedSlot);
324  }
325
326  SourceLocation getUnavailableLoc() const {
327    assert(getKind() == AT_availability && "Not an availability attribute");
328    return UnavailableLoc;
329  }
330};
331
332/// A factory, from which one makes pools, from which one creates
333/// individual attributes which are deallocated with the pool.
334///
335/// Note that it's tolerably cheap to create and destroy one of
336/// these as long as you don't actually allocate anything in it.
337class AttributeFactory {
338public:
339  enum {
340    /// The required allocation size of an availability attribute,
341    /// which we want to ensure is a multiple of sizeof(void*).
342    AvailabilityAllocSize =
343      sizeof(AttributeList)
344      + ((3 * sizeof(AvailabilityChange) + sizeof(void*) - 1)
345         / sizeof(void*) * sizeof(void*))
346  };
347
348private:
349  enum {
350    /// The number of free lists we want to be sure to support
351    /// inline.  This is just enough that availability attributes
352    /// don't surpass it.  It's actually very unlikely we'll see an
353    /// attribute that needs more than that; on x86-64 you'd need 10
354    /// expression arguments, and on i386 you'd need 19.
355    InlineFreeListsCapacity =
356      1 + (AvailabilityAllocSize - sizeof(AttributeList)) / sizeof(void*)
357  };
358
359  llvm::BumpPtrAllocator Alloc;
360
361  /// Free lists.  The index is determined by the following formula:
362  ///   (size - sizeof(AttributeList)) / sizeof(void*)
363  llvm::SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists;
364
365  // The following are the private interface used by AttributePool.
366  friend class AttributePool;
367
368  /// Allocate an attribute of the given size.
369  void *allocate(size_t size);
370
371  /// Reclaim all the attributes in the given pool chain, which is
372  /// non-empty.  Note that the current implementation is safe
373  /// against reclaiming things which were not actually allocated
374  /// with the allocator, although of course it's important to make
375  /// sure that their allocator lives at least as long as this one.
376  void reclaimPool(AttributeList *head);
377
378public:
379  AttributeFactory();
380  ~AttributeFactory();
381};
382
383class AttributePool {
384  AttributeFactory &Factory;
385  AttributeList *Head;
386
387  void *allocate(size_t size) {
388    return Factory.allocate(size);
389  }
390
391  AttributeList *add(AttributeList *attr) {
392    // We don't care about the order of the pool.
393    attr->NextInPool = Head;
394    Head = attr;
395    return attr;
396  }
397
398  void takePool(AttributeList *pool);
399
400public:
401  /// Create a new pool for a factory.
402  AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {}
403
404  /// Move the given pool's allocations to this pool.
405  AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) {
406    pool.Head = 0;
407  }
408
409  AttributeFactory &getFactory() const { return Factory; }
410
411  void clear() {
412    if (Head) {
413      Factory.reclaimPool(Head);
414      Head = 0;
415    }
416  }
417
418  /// Take the given pool's allocations and add them to this pool.
419  void takeAllFrom(AttributePool &pool) {
420    if (pool.Head) {
421      takePool(pool.Head);
422      pool.Head = 0;
423    }
424  }
425
426  ~AttributePool() {
427    if (Head) Factory.reclaimPool(Head);
428  }
429
430  AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc,
431                        IdentifierInfo *scopeName, SourceLocation scopeLoc,
432                        IdentifierInfo *parmName, SourceLocation parmLoc,
433                        Expr **args, unsigned numArgs,
434                        bool declspec = false, bool cxx0x = false) {
435    void *memory = allocate(sizeof(AttributeList)
436                            + numArgs * sizeof(Expr*));
437    return add(new (memory) AttributeList(attrName, attrLoc,
438                                          scopeName, scopeLoc,
439                                          parmName, parmLoc,
440                                          args, numArgs,
441                                          declspec, cxx0x));
442  }
443
444  AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc,
445                        IdentifierInfo *scopeName, SourceLocation scopeLoc,
446                        IdentifierInfo *parmName, SourceLocation parmLoc,
447                        const AvailabilityChange &introduced,
448                        const AvailabilityChange &deprecated,
449                        const AvailabilityChange &obsoleted,
450                        SourceLocation unavailable,
451                        bool declspec = false, bool cxx0x = false) {
452    void *memory = allocate(AttributeFactory::AvailabilityAllocSize);
453    return add(new (memory) AttributeList(attrName, attrLoc,
454                                          scopeName, scopeLoc,
455                                          parmName, parmLoc,
456                                          introduced, deprecated, obsoleted,
457                                          unavailable,
458                                          declspec, cxx0x));
459  }
460
461  AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
462                                        SourceLocation TokLoc, int Arg);
463};
464
465/// addAttributeLists - Add two AttributeLists together
466/// The right-hand list is appended to the left-hand list, if any
467/// A pointer to the joined list is returned.
468/// Note: the lists are not left unmodified.
469inline AttributeList *addAttributeLists(AttributeList *Left,
470                                        AttributeList *Right) {
471  if (!Left)
472    return Right;
473
474  AttributeList *next = Left, *prev;
475  do {
476    prev = next;
477    next = next->getNext();
478  } while (next);
479  prev->setNext(Right);
480  return Left;
481}
482
483/// CXX0XAttributeList - A wrapper around a C++0x attribute list.
484/// Stores, in addition to the list proper, whether or not an actual list was
485/// (as opposed to an empty list, which may be ill-formed in some places) and
486/// the source range of the list.
487struct CXX0XAttributeList {
488  AttributeList *AttrList;
489  SourceRange Range;
490  bool HasAttr;
491  CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
492    : AttrList(attrList), Range(range), HasAttr (hasAttr) {
493  }
494  CXX0XAttributeList ()
495    : AttrList(0), Range(), HasAttr(false) {
496  }
497};
498
499/// ParsedAttributes - A collection of parsed attributes.  Currently
500/// we don't differentiate between the various attribute syntaxes,
501/// which is basically silly.
502///
503/// Right now this is a very lightweight container, but the expectation
504/// is that this will become significantly more serious.
505class ParsedAttributes {
506public:
507  ParsedAttributes(AttributeFactory &factory)
508    : pool(factory), list(0) {
509  }
510
511  ParsedAttributes(ParsedAttributes &attrs)
512    : pool(attrs.pool), list(attrs.list) {
513    attrs.list = 0;
514  }
515
516  AttributePool &getPool() const { return pool; }
517
518  bool empty() const { return list == 0; }
519
520  void add(AttributeList *newAttr) {
521    assert(newAttr);
522    assert(newAttr->getNext() == 0);
523    newAttr->setNext(list);
524    list = newAttr;
525  }
526
527  void addAll(AttributeList *newList) {
528    if (!newList) return;
529
530    AttributeList *lastInNewList = newList;
531    while (AttributeList *next = lastInNewList->getNext())
532      lastInNewList = next;
533
534    lastInNewList->setNext(list);
535    list = newList;
536  }
537
538  void set(AttributeList *newList) {
539    list = newList;
540  }
541
542  void takeAllFrom(ParsedAttributes &attrs) {
543    addAll(attrs.list);
544    attrs.list = 0;
545    pool.takeAllFrom(attrs.pool);
546  }
547
548  void clear() { list = 0; pool.clear(); }
549  AttributeList *getList() const { return list; }
550
551  /// Returns a reference to the attribute list.  Try not to introduce
552  /// dependencies on this method, it may not be long-lived.
553  AttributeList *&getListRef() { return list; }
554
555
556  AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc,
557                        IdentifierInfo *scopeName, SourceLocation scopeLoc,
558                        IdentifierInfo *parmName, SourceLocation parmLoc,
559                        Expr **args, unsigned numArgs,
560                        bool declspec = false, bool cxx0x = false) {
561    AttributeList *attr =
562      pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc,
563                  args, numArgs, declspec, cxx0x);
564    add(attr);
565    return attr;
566  }
567
568  AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc,
569                        IdentifierInfo *scopeName, SourceLocation scopeLoc,
570                        IdentifierInfo *parmName, SourceLocation parmLoc,
571                        const AvailabilityChange &introduced,
572                        const AvailabilityChange &deprecated,
573                        const AvailabilityChange &obsoleted,
574                        SourceLocation unavailable,
575                        bool declspec = false, bool cxx0x = false) {
576    AttributeList *attr =
577      pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc,
578                  introduced, deprecated, obsoleted, unavailable,
579                  declspec, cxx0x);
580    add(attr);
581    return attr;
582  }
583
584  AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name,
585                               SourceLocation loc, int arg) {
586    AttributeList *attr =
587      pool.createIntegerAttribute(C, name, loc, arg);
588    add(attr);
589    return attr;
590  }
591
592
593private:
594  mutable AttributePool pool;
595  AttributeList *list;
596};
597
598}  // end namespace clang
599
600#endif
601