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