clang API Documentation

AttributeList.cpp
Go to the documentation of this file.
00001 //===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the AttributeList class implementation
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Sema/AttributeList.h"
00015 #include "clang/AST/Expr.h"
00016 #include "clang/Basic/IdentifierTable.h"
00017 #include "llvm/ADT/StringSwitch.h"
00018 #include "llvm/ADT/SmallString.h"
00019 using namespace clang;
00020 
00021 size_t AttributeList::allocated_size() const {
00022   if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
00023   return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
00024 }
00025 
00026 AttributeFactory::AttributeFactory() {
00027   // Go ahead and configure all the inline capacity.  This is just a memset.
00028   FreeLists.resize(InlineFreeListsCapacity);
00029 }
00030 AttributeFactory::~AttributeFactory() {}
00031 
00032 static size_t getFreeListIndexForSize(size_t size) {
00033   assert(size >= sizeof(AttributeList));
00034   assert((size % sizeof(void*)) == 0);
00035   return ((size - sizeof(AttributeList)) / sizeof(void*));
00036 }
00037 
00038 void *AttributeFactory::allocate(size_t size) {
00039   // Check for a previously reclaimed attribute.
00040   size_t index = getFreeListIndexForSize(size);
00041   if (index < FreeLists.size()) {
00042     if (AttributeList *attr = FreeLists[index]) {
00043       FreeLists[index] = attr->NextInPool;
00044       return attr;
00045     }
00046   }
00047 
00048   // Otherwise, allocate something new.
00049   return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
00050 }
00051 
00052 void AttributeFactory::reclaimPool(AttributeList *cur) {
00053   assert(cur && "reclaiming empty pool!");
00054   do {
00055     // Read this here, because we're going to overwrite NextInPool
00056     // when we toss 'cur' into the appropriate queue.
00057     AttributeList *next = cur->NextInPool;
00058 
00059     size_t size = cur->allocated_size();
00060     size_t freeListIndex = getFreeListIndexForSize(size);
00061 
00062     // Expand FreeLists to the appropriate size, if required.
00063     if (freeListIndex >= FreeLists.size())
00064       FreeLists.resize(freeListIndex+1);
00065 
00066     // Add 'cur' to the appropriate free-list.
00067     cur->NextInPool = FreeLists[freeListIndex];
00068     FreeLists[freeListIndex] = cur;
00069     
00070     cur = next;
00071   } while (cur);
00072 }
00073 
00074 void AttributePool::takePool(AttributeList *pool) {
00075   assert(pool);
00076 
00077   // Fast path:  this pool is empty.
00078   if (!Head) {
00079     Head = pool;
00080     return;
00081   }
00082 
00083   // Reverse the pool onto the current head.  This optimizes for the
00084   // pattern of pulling a lot of pools into a single pool.
00085   do {
00086     AttributeList *next = pool->NextInPool;
00087     pool->NextInPool = Head;
00088     Head = pool;
00089     pool = next;
00090   } while (pool);
00091 }
00092 
00093 AttributeList *
00094 AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
00095                                       SourceLocation TokLoc, int Arg) {
00096   Expr *IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
00097                                       C.IntTy, TokLoc);
00098   return create(Name, TokLoc, 0, TokLoc, 0, TokLoc, &IArg, 1, 0);
00099 }
00100 
00101 #include "clang/Sema/AttrParsedAttrKinds.inc"
00102 
00103 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
00104                                            const IdentifierInfo *ScopeName) {
00105   StringRef AttrName = Name->getName();
00106 
00107   // Normalize the attribute name, __foo__ becomes foo.
00108   if (AttrName.startswith("__") && AttrName.endswith("__") &&
00109       AttrName.size() >= 4)
00110     AttrName = AttrName.substr(2, AttrName.size() - 4);
00111 
00112   // FIXME: implement attribute namespacing correctly.
00113   SmallString<64> Buf;
00114   if (ScopeName)
00115     AttrName = ((Buf += ScopeName->getName()) += "___") += AttrName;
00116 
00117   return ::getAttrKind(AttrName);
00118 }