blob: f142ab4c0a324407cc418f26e1de27e2d86dc5b7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the AttributeList class implementation
11//
12//===----------------------------------------------------------------------===//
13
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/AttributeList.h"
John McCall0b7e6782011-03-24 11:26:52 +000015#include "clang/AST/Expr.h"
Chris Lattner8f823d22009-04-11 18:48:18 +000016#include "clang/Basic/IdentifierTable.h"
Douglas Gregord6268ff2012-05-02 14:50:50 +000017#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
John McCall0b7e6782011-03-24 11:26:52 +000020size_t AttributeList::allocated_size() const {
21 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
22 return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
Reid Spencer5f016e22007-07-11 17:01:13 +000023}
Chris Lattner23351912008-02-20 23:14:47 +000024
John McCall0b7e6782011-03-24 11:26:52 +000025AttributeFactory::AttributeFactory() {
26 // Go ahead and configure all the inline capacity. This is just a memset.
27 FreeLists.resize(InlineFreeListsCapacity);
28}
29AttributeFactory::~AttributeFactory() {}
30
31static 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
37void *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
51void 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
73void 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
92AttributeList *
93AttributePool::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);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000098}
99
Chris Lattner23351912008-02-20 23:14:47 +0000100AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000101 StringRef AttrName = Name->getName();
Chris Lattner23351912008-02-20 23:14:47 +0000102
103 // Normalize the attribute name, __foo__ becomes foo.
Richard Smith5297d712012-02-25 10:41:10 +0000104 if (AttrName.startswith("__") && AttrName.endswith("__") &&
105 AttrName.size() >= 4)
Daniel Dunbar4f90d8d2009-10-17 18:12:29 +0000106 AttrName = AttrName.substr(2, AttrName.size() - 4);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Douglas Gregord6268ff2012-05-02 14:50:50 +0000108 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);
Chris Lattner23351912008-02-20 23:14:47 +0000126}