blob: a380bb88877442726fdebde8c22b91ae0ec099a5 [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 Gregorb6dd6052012-05-02 14:24:30 +000017#include "llvm/ADT/StringMap.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
Douglas Gregorb6dd6052012-05-02 14:24:30 +0000100
101typedef llvm::StringMap<AttributeList::Kind> AttributeNameKindMap;
102
103static AttributeNameKindMap createAttributeNameKindMap(){
104 AttributeNameKindMap Result;
105#include "clang/Sema/AttrParsedAttrKinds.inc"
106 Result["address_space"] = AttributeList::AT_address_space;
107 Result["align"] = AttributeList::AT_aligned; // FIXME: should it be "aligned"?
108 Result["base_check"] = AttributeList::AT_base_check;
109 Result["bounded"] = AttributeList::IgnoredAttribute; // OpenBSD
110 Result["__const"] = AttributeList::AT_const; // some GCC headers do contain this spelling
111 Result["cf_returns_autoreleased"] = AttributeList::AT_cf_returns_autoreleased;
112 Result["mode"] = AttributeList::AT_mode;
113 Result["vec_type_hint"] = AttributeList::IgnoredAttribute;
114 Result["ext_vector_type"] = AttributeList::AT_ext_vector_type;
115 Result["neon_vector_type"] = AttributeList::AT_neon_vector_type;
116 Result["neon_polyvector_type"] = AttributeList::AT_neon_polyvector_type;
117 Result["opencl_image_access"] = AttributeList::AT_opencl_image_access;
118 Result["objc_gc"] = AttributeList::AT_objc_gc;
119 Result["objc_ownership"] = AttributeList::AT_objc_ownership;
120 Result["vector_size"] = AttributeList::AT_vector_size;
121 return Result;
122}
123
Chris Lattner23351912008-02-20 23:14:47 +0000124AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 StringRef AttrName = Name->getName();
Chris Lattner23351912008-02-20 23:14:47 +0000126
127 // Normalize the attribute name, __foo__ becomes foo.
Richard Smith5297d712012-02-25 10:41:10 +0000128 if (AttrName.startswith("__") && AttrName.endswith("__") &&
129 AttrName.size() >= 4)
Daniel Dunbar4f90d8d2009-10-17 18:12:29 +0000130 AttrName = AttrName.substr(2, AttrName.size() - 4);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Douglas Gregorb6dd6052012-05-02 14:24:30 +0000132 static AttributeNameKindMap Map = createAttributeNameKindMap();
133 AttributeNameKindMap::iterator Pos = Map.find(AttrName);
134 if (Pos != Map.end())
135 return Pos->second;
136
137 return UnknownAttribute;
Chris Lattner23351912008-02-20 23:14:47 +0000138}