Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- AttributeList.cpp --------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the AttributeList class implementation |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/AttributeList.h" |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Chris Lattner | 8f823d2 | 2009-04-11 18:48:18 +0000 | [diff] [blame] | 16 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 20 | size_t AttributeList::allocated_size() const { |
| 21 | if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; |
| 22 | return (sizeof(AttributeList) + NumArgs * sizeof(Expr*)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | } |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 24 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 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); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 100 | AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) { |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 101 | llvm::StringRef AttrName = Name->getName(); |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 102 | |
| 103 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 4f90d8d | 2009-10-17 18:12:29 +0000 | [diff] [blame] | 104 | if (AttrName.startswith("__") && AttrName.endswith("__")) |
| 105 | AttrName = AttrName.substr(2, AttrName.size() - 4); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 107 | return llvm::StringSwitch<AttributeList::Kind>(AttrName) |
| 108 | .Case("weak", AT_weak) |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 109 | .Case("weakref", AT_weakref) |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 110 | .Case("objc_arc_weak_reference_unavailable", AT_arc_weakref_unavailable) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 111 | .Case("pure", AT_pure) |
| 112 | .Case("mode", AT_mode) |
| 113 | .Case("used", AT_used) |
| 114 | .Case("alias", AT_alias) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 115 | .Case("align", AT_aligned) |
Eli Friedman | 8f4c59e | 2009-11-09 18:38:53 +0000 | [diff] [blame] | 116 | .Case("cdecl", AT_cdecl) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 117 | .Case("const", AT_const) |
Gabor Greif | c5127ed | 2010-10-15 08:26:25 +0000 | [diff] [blame] | 118 | .Case("__const", AT_const) // some GCC headers do contain this spelling |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 119 | .Case("blocks", AT_blocks) |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 120 | .Case("format", AT_format) |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 121 | .Case("malloc", AT_malloc) |
| 122 | .Case("packed", AT_packed) |
| 123 | .Case("unused", AT_unused) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 124 | .Case("aligned", AT_aligned) |
| 125 | .Case("cleanup", AT_cleanup) |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 126 | .Case("naked", AT_naked) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 127 | .Case("nodebug", AT_nodebug) |
| 128 | .Case("nonnull", AT_nonnull) |
| 129 | .Case("nothrow", AT_nothrow) |
| 130 | .Case("objc_gc", AT_objc_gc) |
| 131 | .Case("regparm", AT_regparm) |
| 132 | .Case("section", AT_section) |
| 133 | .Case("stdcall", AT_stdcall) |
| 134 | .Case("annotate", AT_annotate) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 135 | .Case("fastcall", AT_fastcall) |
Ted Kremenek | efbddd2 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 136 | .Case("ibaction", AT_IBAction) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 137 | .Case("iboutlet", AT_IBOutlet) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 138 | .Case("iboutletcollection", AT_IBOutletCollection) |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 139 | .Case("noreturn", AT_noreturn) |
| 140 | .Case("noinline", AT_noinline) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 141 | .Case("sentinel", AT_sentinel) |
| 142 | .Case("NSObject", AT_nsobject) |
| 143 | .Case("dllimport", AT_dllimport) |
| 144 | .Case("dllexport", AT_dllexport) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 145 | .Case("may_alias", AT_may_alias) |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 146 | .Case("base_check", AT_base_check) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 147 | .Case("deprecated", AT_deprecated) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 148 | .Case("availability", AT_availability) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 149 | .Case("visibility", AT_visibility) |
| 150 | .Case("destructor", AT_destructor) |
| 151 | .Case("format_arg", AT_format_arg) |
| 152 | .Case("gnu_inline", AT_gnu_inline) |
| 153 | .Case("weak_import", AT_weak_import) |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 154 | .Case("vecreturn", AT_vecreturn) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 155 | .Case("vector_size", AT_vector_size) |
| 156 | .Case("constructor", AT_constructor) |
| 157 | .Case("unavailable", AT_unavailable) |
| 158 | .Case("overloadable", AT_overloadable) |
| 159 | .Case("address_space", AT_address_space) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 160 | .Case("opencl_image_access", AT_opencl_image_access) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 161 | .Case("always_inline", AT_always_inline) |
Chris Lattner | 9f6c772 | 2010-04-12 02:18:49 +0000 | [diff] [blame] | 162 | .Case("returns_twice", IgnoredAttribute) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 163 | .Case("vec_type_hint", IgnoredAttribute) |
| 164 | .Case("objc_exception", AT_objc_exception) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 165 | .Case("objc_method_family", AT_objc_method_family) |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 166 | .Case("objc_returns_inner_pointer", AT_objc_returns_inner_pointer) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 167 | .Case("ext_vector_type", AT_ext_vector_type) |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 168 | .Case("neon_vector_type", AT_neon_vector_type) |
| 169 | .Case("neon_polyvector_type", AT_neon_polyvector_type) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 170 | .Case("transparent_union", AT_transparent_union) |
| 171 | .Case("analyzer_noreturn", AT_analyzer_noreturn) |
| 172 | .Case("warn_unused_result", AT_warn_unused_result) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 173 | .Case("carries_dependency", AT_carries_dependency) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 174 | .Case("ns_consumed", AT_ns_consumed) |
| 175 | .Case("ns_consumes_self", AT_ns_consumes_self) |
| 176 | .Case("ns_returns_autoreleased", AT_ns_returns_autoreleased) |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 177 | .Case("ns_returns_not_retained", AT_ns_returns_not_retained) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 178 | .Case("ns_returns_retained", AT_ns_returns_retained) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 179 | .Case("cf_consumed", AT_cf_consumed) |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 180 | .Case("cf_returns_not_retained", AT_cf_returns_not_retained) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 181 | .Case("cf_returns_retained", AT_cf_returns_retained) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 182 | .Case("cf_returns_autoreleased", AT_cf_returns_autoreleased) |
| 183 | .Case("ns_consumes_self", AT_ns_consumes_self) |
| 184 | .Case("ns_consumed", AT_ns_consumed) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 185 | .Case("objc_ownership", AT_objc_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 186 | .Case("objc_precise_lifetime", AT_objc_precise_lifetime) |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 187 | .Case("ownership_returns", AT_ownership_returns) |
| 188 | .Case("ownership_holds", AT_ownership_holds) |
| 189 | .Case("ownership_takes", AT_ownership_takes) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 190 | .Case("reqd_work_group_size", AT_reqd_wg_size) |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 191 | .Case("init_priority", AT_init_priority) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 192 | .Case("no_instrument_function", AT_no_instrument_function) |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 193 | .Case("thiscall", AT_thiscall) |
Chris Lattner | cd5b306 | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 194 | .Case("bounded", IgnoredAttribute) // OpenBSD |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 195 | .Case("pascal", AT_pascal) |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 196 | .Case("__cdecl", AT_cdecl) |
| 197 | .Case("__stdcall", AT_stdcall) |
| 198 | .Case("__fastcall", AT_fastcall) |
| 199 | .Case("__thiscall", AT_thiscall) |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 200 | .Case("__pascal", AT_pascal) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 201 | .Case("constant", AT_constant) |
| 202 | .Case("device", AT_device) |
| 203 | .Case("global", AT_global) |
| 204 | .Case("host", AT_host) |
| 205 | .Case("shared", AT_shared) |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 206 | .Case("launch_bounds", AT_launch_bounds) |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 207 | .Case("common", AT_common) |
| 208 | .Case("nocommon", AT_nocommon) |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 209 | .Case("opencl_kernel_function", AT_opencl_kernel_function) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 210 | .Case("uuid", AT_uuid) |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 211 | .Case("pcs", AT_pcs) |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 212 | .Case("ms_struct", AT_MsStruct) |
Douglas Gregor | fc3bb49 | 2009-10-29 05:26:58 +0000 | [diff] [blame] | 213 | .Default(UnknownAttribute); |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 214 | } |