Home | History | Annotate | Download | only in Sema
      1 //===--- AttributeList.cpp --------------------------------------*- 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 implementation
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Sema/AttributeList.h"
     15 #include "clang/AST/Expr.h"
     16 #include "clang/Basic/IdentifierTable.h"
     17 #include "llvm/ADT/StringSwitch.h"
     18 using namespace clang;
     19 
     20 size_t AttributeList::allocated_size() const {
     21   if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
     22   return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
     23 }
     24 
     25 AttributeFactory::AttributeFactory() {
     26   // Go ahead and configure all the inline capacity.  This is just a memset.
     27   FreeLists.resize(InlineFreeListsCapacity);
     28 }
     29 AttributeFactory::~AttributeFactory() {}
     30 
     31 static size_t getFreeListIndexForSize(size_t size) {
     32   assert(size >= sizeof(AttributeList));
     33   assert((size % sizeof(void*)) == 0);
     34   return ((size - sizeof(AttributeList)) / sizeof(void*));
     35 }
     36 
     37 void *AttributeFactory::allocate(size_t size) {
     38   // Check for a previously reclaimed attribute.
     39   size_t index = getFreeListIndexForSize(size);
     40   if (index < FreeLists.size()) {
     41     if (AttributeList *attr = FreeLists[index]) {
     42       FreeLists[index] = attr->NextInPool;
     43       return attr;
     44     }
     45   }
     46 
     47   // Otherwise, allocate something new.
     48   return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
     49 }
     50 
     51 void AttributeFactory::reclaimPool(AttributeList *cur) {
     52   assert(cur && "reclaiming empty pool!");
     53   do {
     54     // Read this here, because we're going to overwrite NextInPool
     55     // when we toss 'cur' into the appropriate queue.
     56     AttributeList *next = cur->NextInPool;
     57 
     58     size_t size = cur->allocated_size();
     59     size_t freeListIndex = getFreeListIndexForSize(size);
     60 
     61     // Expand FreeLists to the appropriate size, if required.
     62     if (freeListIndex >= FreeLists.size())
     63       FreeLists.resize(freeListIndex+1);
     64 
     65     // Add 'cur' to the appropriate free-list.
     66     cur->NextInPool = FreeLists[freeListIndex];
     67     FreeLists[freeListIndex] = cur;
     68 
     69     cur = next;
     70   } while (cur);
     71 }
     72 
     73 void AttributePool::takePool(AttributeList *pool) {
     74   assert(pool);
     75 
     76   // Fast path:  this pool is empty.
     77   if (!Head) {
     78     Head = pool;
     79     return;
     80   }
     81 
     82   // Reverse the pool onto the current head.  This optimizes for the
     83   // pattern of pulling a lot of pools into a single pool.
     84   do {
     85     AttributeList *next = pool->NextInPool;
     86     pool->NextInPool = Head;
     87     Head = pool;
     88     pool = next;
     89   } while (pool);
     90 }
     91 
     92 AttributeList *
     93 AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
     94                                       SourceLocation TokLoc, int Arg) {
     95   Expr *IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
     96                                       C.IntTy, TokLoc);
     97   return create(Name, TokLoc, 0, TokLoc, 0, TokLoc, &IArg, 1, 0);
     98 }
     99 
    100 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
    101   StringRef AttrName = Name->getName();
    102 
    103   // Normalize the attribute name, __foo__ becomes foo.
    104   if (AttrName.startswith("__") && AttrName.endswith("__") &&
    105       AttrName.size() >= 4)
    106     AttrName = AttrName.substr(2, AttrName.size() - 4);
    107 
    108   return llvm::StringSwitch<AttributeList::Kind>(AttrName)
    109     #include "clang/Sema/AttrParsedAttrKinds.inc"
    110     .Case("address_space", AT_address_space)
    111     .Case("align", AT_aligned) // FIXME - should it be "aligned"?
    112     .Case("base_check", AT_base_check)
    113     .Case("bounded", IgnoredAttribute)       // OpenBSD
    114     .Case("__const", AT_const) // some GCC headers do contain this spelling
    115     .Case("cf_returns_autoreleased", AT_cf_returns_autoreleased)
    116     .Case("mode", AT_mode)
    117     .Case("vec_type_hint", IgnoredAttribute)
    118     .Case("ext_vector_type", AT_ext_vector_type)
    119     .Case("neon_vector_type", AT_neon_vector_type)
    120     .Case("neon_polyvector_type", AT_neon_polyvector_type)
    121     .Case("opencl_image_access", AT_opencl_image_access)
    122     .Case("objc_gc", AT_objc_gc)
    123     .Case("objc_ownership", AT_objc_ownership)
    124     .Case("vector_size", AT_vector_size)
    125     .Default(UnknownAttribute);
    126 }
    127