AttributeList.h revision fe98da0fa352462c02db037360788748f95466f7
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 SourceRange AttrRange; 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 92 Expr **getArgsBuffer() { 93 return reinterpret_cast<Expr**>(this+1); 94 } 95 Expr * const *getArgsBuffer() const { 96 return reinterpret_cast<Expr* const *>(this+1); 97 } 98 99 enum AvailabilitySlot { 100 IntroducedSlot, DeprecatedSlot, ObsoletedSlot 101 }; 102 103 AvailabilityChange &getAvailabilitySlot(AvailabilitySlot index) { 104 return reinterpret_cast<AvailabilityChange*>(this+1)[index]; 105 } 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 117 AttributeList(IdentifierInfo *attrName, SourceRange attrRange, 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 AttrRange(attrRange), 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 131 AttributeList(IdentifierInfo *attrName, SourceRange attrRange, 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 AttrRange(attrRange), 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 153public: 154 enum Kind { // Please keep this list alphabetized. 155 AT_acquired_after, 156 AT_acquired_before, 157 AT_address_space, 158 AT_alias, 159 AT_aligned, 160 AT_always_inline, 161 AT_analyzer_noreturn, 162 AT_annotate, 163 AT_arc_weakref_unavailable, 164 AT_availability, // Clang-specific 165 AT_base_check, 166 AT_blocks, 167 AT_carries_dependency, 168 AT_cdecl, 169 AT_cf_consumed, // Clang-specific. 170 AT_cf_returns_autoreleased, // Clang-specific. 171 AT_cf_returns_not_retained, // Clang-specific. 172 AT_cf_returns_retained, // Clang-specific. 173 AT_cleanup, 174 AT_common, 175 AT_const, 176 AT_constant, 177 AT_constructor, 178 AT_deprecated, 179 AT_destructor, 180 AT_device, 181 AT_dllexport, 182 AT_dllimport, 183 AT_exclusive_lock_function, 184 AT_exclusive_locks_required, 185 AT_exclusive_trylock_function, 186 AT_ext_vector_type, 187 AT_fastcall, 188 AT_format, 189 AT_format_arg, 190 AT_global, 191 AT_gnu_inline, 192 AT_guarded_by, 193 AT_guarded_var, 194 AT_host, 195 AT_IBAction, // Clang-specific. 196 AT_IBOutlet, // Clang-specific. 197 AT_IBOutletCollection, // Clang-specific. 198 AT_init_priority, 199 AT_launch_bounds, 200 AT_lock_returned, 201 AT_lockable, 202 AT_locks_excluded, 203 AT_malloc, 204 AT_may_alias, 205 AT_mode, 206 AT_MsStruct, 207 AT_naked, 208 AT_neon_polyvector_type, // Clang-specific. 209 AT_neon_vector_type, // Clang-specific. 210 AT_no_instrument_function, 211 AT_no_thread_safety_analysis, 212 AT_nocommon, 213 AT_nodebug, 214 AT_noinline, 215 AT_nonnull, 216 AT_noreturn, 217 AT_nothrow, 218 AT_ns_bridged, // Clang-specific. 219 AT_ns_consumed, // Clang-specific. 220 AT_ns_consumes_self, // Clang-specific. 221 AT_ns_returns_autoreleased, // Clang-specific. 222 AT_ns_returns_not_retained, // Clang-specific. 223 AT_ns_returns_retained, // Clang-specific. 224 AT_nsobject, 225 AT_objc_exception, 226 AT_objc_gc, 227 AT_objc_method_family, 228 AT_objc_ownership, // Clang-specific. 229 AT_objc_precise_lifetime, // Clang-specific. 230 AT_objc_returns_inner_pointer, // Clang-specific. 231 AT_opencl_image_access, // OpenCL-specific. 232 AT_opencl_kernel_function, // OpenCL-specific. 233 AT_overloadable, // Clang-specific. 234 AT_ownership_holds, // Clang-specific. 235 AT_ownership_returns, // Clang-specific. 236 AT_ownership_takes, // Clang-specific. 237 AT_packed, 238 AT_pascal, 239 AT_pcs, // ARM specific 240 AT_pt_guarded_by, 241 AT_pt_guarded_var, 242 AT_pure, 243 AT_regparm, 244 AT_reqd_wg_size, 245 AT_scoped_lockable, 246 AT_section, 247 AT_sentinel, 248 AT_shared, 249 AT_shared_lock_function, 250 AT_shared_locks_required, 251 AT_shared_trylock_function, 252 AT_stdcall, 253 AT_thiscall, 254 AT_transparent_union, 255 AT_unavailable, 256 AT_unlock_function, 257 AT_unused, 258 AT_used, 259 AT_uuid, 260 AT_vecreturn, // PS3 PPU-specific. 261 AT_vector_size, 262 AT_visibility, 263 AT_warn_unused_result, 264 AT_weak, 265 AT_weak_import, 266 AT_weakref, 267 IgnoredAttribute, 268 UnknownAttribute 269 }; 270 271 IdentifierInfo *getName() const { return AttrName; } 272 SourceLocation getLoc() const { return AttrRange.getBegin(); } 273 SourceRange getRange() const { return AttrRange; } 274 275 bool hasScope() const { return ScopeName; } 276 IdentifierInfo *getScopeName() const { return ScopeName; } 277 SourceLocation getScopeLoc() const { return ScopeLoc; } 278 279 IdentifierInfo *getParameterName() const { return ParmName; } 280 SourceLocation getParameterLoc() const { return ParmLoc; } 281 282 bool isDeclspecAttribute() const { return DeclspecAttribute; } 283 bool isCXX0XAttribute() const { return CXX0XAttribute; } 284 285 bool isInvalid() const { return Invalid; } 286 void setInvalid(bool b = true) const { Invalid = b; } 287 288 Kind getKind() const { return Kind(AttrKind); } 289 static Kind getKind(const IdentifierInfo *Name); 290 291 AttributeList *getNext() const { return NextInPosition; } 292 void setNext(AttributeList *N) { NextInPosition = N; } 293 294 /// getNumArgs - Return the number of actual arguments to this attribute. 295 unsigned getNumArgs() const { return NumArgs; } 296 297 /// hasParameterOrArguments - Return true if this attribute has a parameter, 298 /// or has a non empty argument expression list. 299 bool hasParameterOrArguments() const { return ParmName || NumArgs; } 300 301 /// getArg - Return the specified argument. 302 Expr *getArg(unsigned Arg) const { 303 assert(Arg < NumArgs && "Arg access out of range!"); 304 return getArgsBuffer()[Arg]; 305 } 306 307 class arg_iterator { 308 Expr * const *X; 309 unsigned Idx; 310 public: 311 arg_iterator(Expr * const *x, unsigned idx) : X(x), Idx(idx) {} 312 313 arg_iterator& operator++() { 314 ++Idx; 315 return *this; 316 } 317 318 bool operator==(const arg_iterator& I) const { 319 assert (X == I.X && 320 "compared arg_iterators are for different argument lists"); 321 return Idx == I.Idx; 322 } 323 324 bool operator!=(const arg_iterator& I) const { 325 return !operator==(I); 326 } 327 328 Expr* operator*() const { 329 return X[Idx]; 330 } 331 332 unsigned getArgNum() const { 333 return Idx+1; 334 } 335 }; 336 337 arg_iterator arg_begin() const { 338 return arg_iterator(getArgsBuffer(), 0); 339 } 340 341 arg_iterator arg_end() const { 342 return arg_iterator(getArgsBuffer(), NumArgs); 343 } 344 345 const AvailabilityChange &getAvailabilityIntroduced() const { 346 assert(getKind() == AT_availability && "Not an availability attribute"); 347 return getAvailabilitySlot(IntroducedSlot); 348 } 349 350 const AvailabilityChange &getAvailabilityDeprecated() const { 351 assert(getKind() == AT_availability && "Not an availability attribute"); 352 return getAvailabilitySlot(DeprecatedSlot); 353 } 354 355 const AvailabilityChange &getAvailabilityObsoleted() const { 356 assert(getKind() == AT_availability && "Not an availability attribute"); 357 return getAvailabilitySlot(ObsoletedSlot); 358 } 359 360 SourceLocation getUnavailableLoc() const { 361 assert(getKind() == AT_availability && "Not an availability attribute"); 362 return UnavailableLoc; 363 } 364}; 365 366/// A factory, from which one makes pools, from which one creates 367/// individual attributes which are deallocated with the pool. 368/// 369/// Note that it's tolerably cheap to create and destroy one of 370/// these as long as you don't actually allocate anything in it. 371class AttributeFactory { 372public: 373 enum { 374 /// The required allocation size of an availability attribute, 375 /// which we want to ensure is a multiple of sizeof(void*). 376 AvailabilityAllocSize = 377 sizeof(AttributeList) 378 + ((3 * sizeof(AvailabilityChange) + sizeof(void*) - 1) 379 / sizeof(void*) * sizeof(void*)) 380 }; 381 382private: 383 enum { 384 /// The number of free lists we want to be sure to support 385 /// inline. This is just enough that availability attributes 386 /// don't surpass it. It's actually very unlikely we'll see an 387 /// attribute that needs more than that; on x86-64 you'd need 10 388 /// expression arguments, and on i386 you'd need 19. 389 InlineFreeListsCapacity = 390 1 + (AvailabilityAllocSize - sizeof(AttributeList)) / sizeof(void*) 391 }; 392 393 llvm::BumpPtrAllocator Alloc; 394 395 /// Free lists. The index is determined by the following formula: 396 /// (size - sizeof(AttributeList)) / sizeof(void*) 397 SmallVector<AttributeList*, InlineFreeListsCapacity> FreeLists; 398 399 // The following are the private interface used by AttributePool. 400 friend class AttributePool; 401 402 /// Allocate an attribute of the given size. 403 void *allocate(size_t size); 404 405 /// Reclaim all the attributes in the given pool chain, which is 406 /// non-empty. Note that the current implementation is safe 407 /// against reclaiming things which were not actually allocated 408 /// with the allocator, although of course it's important to make 409 /// sure that their allocator lives at least as long as this one. 410 void reclaimPool(AttributeList *head); 411 412public: 413 AttributeFactory(); 414 ~AttributeFactory(); 415}; 416 417class AttributePool { 418 AttributeFactory &Factory; 419 AttributeList *Head; 420 421 void *allocate(size_t size) { 422 return Factory.allocate(size); 423 } 424 425 AttributeList *add(AttributeList *attr) { 426 // We don't care about the order of the pool. 427 attr->NextInPool = Head; 428 Head = attr; 429 return attr; 430 } 431 432 void takePool(AttributeList *pool); 433 434public: 435 /// Create a new pool for a factory. 436 AttributePool(AttributeFactory &factory) : Factory(factory), Head(0) {} 437 438 /// Move the given pool's allocations to this pool. 439 AttributePool(AttributePool &pool) : Factory(pool.Factory), Head(pool.Head) { 440 pool.Head = 0; 441 } 442 443 AttributeFactory &getFactory() const { return Factory; } 444 445 void clear() { 446 if (Head) { 447 Factory.reclaimPool(Head); 448 Head = 0; 449 } 450 } 451 452 /// Take the given pool's allocations and add them to this pool. 453 void takeAllFrom(AttributePool &pool) { 454 if (pool.Head) { 455 takePool(pool.Head); 456 pool.Head = 0; 457 } 458 } 459 460 ~AttributePool() { 461 if (Head) Factory.reclaimPool(Head); 462 } 463 464 AttributeList *create(IdentifierInfo *attrName, SourceRange attrRange, 465 IdentifierInfo *scopeName, SourceLocation scopeLoc, 466 IdentifierInfo *parmName, SourceLocation parmLoc, 467 Expr **args, unsigned numArgs, 468 bool declspec = false, bool cxx0x = false) { 469 void *memory = allocate(sizeof(AttributeList) 470 + numArgs * sizeof(Expr*)); 471 return add(new (memory) AttributeList(attrName, attrRange, 472 scopeName, scopeLoc, 473 parmName, parmLoc, 474 args, numArgs, 475 declspec, cxx0x)); 476 } 477 478 AttributeList *create(IdentifierInfo *attrName, SourceRange attrRange, 479 IdentifierInfo *scopeName, SourceLocation scopeLoc, 480 IdentifierInfo *parmName, SourceLocation parmLoc, 481 const AvailabilityChange &introduced, 482 const AvailabilityChange &deprecated, 483 const AvailabilityChange &obsoleted, 484 SourceLocation unavailable, 485 bool declspec = false, bool cxx0x = false) { 486 void *memory = allocate(AttributeFactory::AvailabilityAllocSize); 487 return add(new (memory) AttributeList(attrName, attrRange, 488 scopeName, scopeLoc, 489 parmName, parmLoc, 490 introduced, deprecated, obsoleted, 491 unavailable, 492 declspec, cxx0x)); 493 } 494 495 AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name, 496 SourceLocation TokLoc, int Arg); 497}; 498 499/// addAttributeLists - Add two AttributeLists together 500/// The right-hand list is appended to the left-hand list, if any 501/// A pointer to the joined list is returned. 502/// Note: the lists are not left unmodified. 503inline AttributeList *addAttributeLists(AttributeList *Left, 504 AttributeList *Right) { 505 if (!Left) 506 return Right; 507 508 AttributeList *next = Left, *prev; 509 do { 510 prev = next; 511 next = next->getNext(); 512 } while (next); 513 prev->setNext(Right); 514 return Left; 515} 516 517/// CXX0XAttributeList - A wrapper around a C++0x attribute list. 518/// Stores, in addition to the list proper, whether or not an actual list was 519/// (as opposed to an empty list, which may be ill-formed in some places) and 520/// the source range of the list. 521struct CXX0XAttributeList { 522 AttributeList *AttrList; 523 SourceRange Range; 524 bool HasAttr; 525 CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr) 526 : AttrList(attrList), Range(range), HasAttr (hasAttr) { 527 } 528 CXX0XAttributeList () 529 : AttrList(0), Range(), HasAttr(false) { 530 } 531}; 532 533/// ParsedAttributes - A collection of parsed attributes. Currently 534/// we don't differentiate between the various attribute syntaxes, 535/// which is basically silly. 536/// 537/// Right now this is a very lightweight container, but the expectation 538/// is that this will become significantly more serious. 539class ParsedAttributes { 540public: 541 ParsedAttributes(AttributeFactory &factory) 542 : pool(factory), list(0) { 543 } 544 545 ParsedAttributes(ParsedAttributes &attrs) 546 : pool(attrs.pool), list(attrs.list) { 547 attrs.list = 0; 548 } 549 550 AttributePool &getPool() const { return pool; } 551 552 bool empty() const { return list == 0; } 553 554 void add(AttributeList *newAttr) { 555 assert(newAttr); 556 assert(newAttr->getNext() == 0); 557 newAttr->setNext(list); 558 list = newAttr; 559 } 560 561 void addAll(AttributeList *newList) { 562 if (!newList) return; 563 564 AttributeList *lastInNewList = newList; 565 while (AttributeList *next = lastInNewList->getNext()) 566 lastInNewList = next; 567 568 lastInNewList->setNext(list); 569 list = newList; 570 } 571 572 void set(AttributeList *newList) { 573 list = newList; 574 } 575 576 void takeAllFrom(ParsedAttributes &attrs) { 577 addAll(attrs.list); 578 attrs.list = 0; 579 pool.takeAllFrom(attrs.pool); 580 } 581 582 void clear() { list = 0; pool.clear(); } 583 AttributeList *getList() const { return list; } 584 585 /// Returns a reference to the attribute list. Try not to introduce 586 /// dependencies on this method, it may not be long-lived. 587 AttributeList *&getListRef() { return list; } 588 589 590 AttributeList *addNew(IdentifierInfo *attrName, SourceRange attrRange, 591 IdentifierInfo *scopeName, SourceLocation scopeLoc, 592 IdentifierInfo *parmName, SourceLocation parmLoc, 593 Expr **args, unsigned numArgs, 594 bool declspec = false, bool cxx0x = false) { 595 AttributeList *attr = 596 pool.create(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc, 597 args, numArgs, declspec, cxx0x); 598 add(attr); 599 return attr; 600 } 601 602 AttributeList *addNew(IdentifierInfo *attrName, SourceRange attrRange, 603 IdentifierInfo *scopeName, SourceLocation scopeLoc, 604 IdentifierInfo *parmName, SourceLocation parmLoc, 605 const AvailabilityChange &introduced, 606 const AvailabilityChange &deprecated, 607 const AvailabilityChange &obsoleted, 608 SourceLocation unavailable, 609 bool declspec = false, bool cxx0x = false) { 610 AttributeList *attr = 611 pool.create(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc, 612 introduced, deprecated, obsoleted, unavailable, 613 declspec, cxx0x); 614 add(attr); 615 return attr; 616 } 617 618 AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name, 619 SourceLocation loc, int arg) { 620 AttributeList *attr = 621 pool.createIntegerAttribute(C, name, loc, arg); 622 add(attr); 623 return attr; 624 } 625 626 627private: 628 mutable AttributePool pool; 629 AttributeList *list; 630}; 631 632} // end namespace clang 633 634#endif 635