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