AttributeList.cpp revision 22f18fd72d1a2205afdfa6a975b75cd76ef40f3a
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"
18using namespace clang;
19
20size_t AttributeList::allocated_size() const {
21  if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
22  return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
23}
24
25AttributeFactory::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);
98}
99
100AttributeList::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 = AttrName.substr(2, AttrName.size() - 4);
106
107  return llvm::StringSwitch<AttributeList::Kind>(AttrName)
108    .Case("weak", AT_weak)
109    .Case("weakref", AT_weakref)
110    .Case("objc_arc_weak_reference_unavailable", AT_arc_weakref_unavailable)
111    .Case("objc_disable_automatic_synthesis", AT_objc_disable_automatic_synthesis)
112    .Case("pure", AT_pure)
113    .Case("mode", AT_mode)
114    .Case("used", AT_used)
115    .Case("alias", AT_alias)
116    .Case("align", AT_aligned)
117    .Case("cdecl", AT_cdecl)
118    .Case("const", AT_const)
119    .Case("__const", AT_const) // some GCC headers do contain this spelling
120    .Case("blocks", AT_blocks)
121    .Case("format", AT_format)
122    .Case("malloc", AT_malloc)
123    .Case("packed", AT_packed)
124    .Case("unused", AT_unused)
125    .Case("aligned", AT_aligned)
126    .Case("cleanup", AT_cleanup)
127    .Case("naked", AT_naked)
128    .Case("nodebug", AT_nodebug)
129    .Case("nonnull", AT_nonnull)
130    .Case("nothrow", AT_nothrow)
131    .Case("objc_gc", AT_objc_gc)
132    .Case("regparm", AT_regparm)
133    .Case("section", AT_section)
134    .Case("stdcall", AT_stdcall)
135    .Case("annotate", AT_annotate)
136    .Case("fastcall", AT_fastcall)
137    .Case("ibaction", AT_IBAction)
138    .Case("iboutlet", AT_IBOutlet)
139    .Case("iboutletcollection", AT_IBOutletCollection)
140    .Case("noreturn", AT_noreturn)
141    .Case("noinline", AT_noinline)
142    .Case("sentinel", AT_sentinel)
143    .Case("NSObject", AT_nsobject)
144    .Case("dllimport", AT_dllimport)
145    .Case("dllexport", AT_dllexport)
146    .Case("may_alias", AT_may_alias)
147    .Case("base_check", AT_base_check)
148    .Case("deprecated", AT_deprecated)
149    .Case("availability", AT_availability)
150    .Case("visibility", AT_visibility)
151    .Case("destructor", AT_destructor)
152    .Case("format_arg", AT_format_arg)
153    .Case("gnu_inline", AT_gnu_inline)
154    .Case("weak_import", AT_weak_import)
155    .Case("vecreturn", AT_vecreturn)
156    .Case("vector_size", AT_vector_size)
157    .Case("constructor", AT_constructor)
158    .Case("unavailable", AT_unavailable)
159    .Case("overloadable", AT_overloadable)
160    .Case("address_space", AT_address_space)
161    .Case("opencl_image_access", AT_opencl_image_access)
162    .Case("always_inline", AT_always_inline)
163    .Case("returns_twice", AT_returns_twice)
164    .Case("vec_type_hint", IgnoredAttribute)
165    .Case("objc_exception", AT_objc_exception)
166    .Case("objc_method_family", AT_objc_method_family)
167    .Case("objc_returns_inner_pointer", AT_objc_returns_inner_pointer)
168    .Case("ext_vector_type", AT_ext_vector_type)
169    .Case("neon_vector_type", AT_neon_vector_type)
170    .Case("neon_polyvector_type", AT_neon_polyvector_type)
171    .Case("transparent_union", AT_transparent_union)
172    .Case("analyzer_noreturn", AT_analyzer_noreturn)
173    .Case("warn_unused_result", AT_warn_unused_result)
174    .Case("carries_dependency", AT_carries_dependency)
175    .Case("ns_bridged", AT_ns_bridged)
176    .Case("ns_consumed", AT_ns_consumed)
177    .Case("ns_consumes_self", AT_ns_consumes_self)
178    .Case("ns_returns_autoreleased", AT_ns_returns_autoreleased)
179    .Case("ns_returns_not_retained", AT_ns_returns_not_retained)
180    .Case("ns_returns_retained", AT_ns_returns_retained)
181    .Case("cf_audited_transfer", AT_cf_audited_transfer)
182    .Case("cf_consumed", AT_cf_consumed)
183    .Case("cf_returns_not_retained", AT_cf_returns_not_retained)
184    .Case("cf_returns_retained", AT_cf_returns_retained)
185    .Case("cf_returns_autoreleased", AT_cf_returns_autoreleased)
186    .Case("cf_unknown_transfer", AT_cf_unknown_transfer)
187    .Case("ns_consumes_self", AT_ns_consumes_self)
188    .Case("ns_consumed", AT_ns_consumed)
189    .Case("objc_ownership", AT_objc_ownership)
190    .Case("objc_precise_lifetime", AT_objc_precise_lifetime)
191    .Case("ownership_returns", AT_ownership_returns)
192    .Case("ownership_holds", AT_ownership_holds)
193    .Case("ownership_takes", AT_ownership_takes)
194    .Case("reqd_work_group_size", AT_reqd_wg_size)
195    .Case("init_priority", AT_init_priority)
196    .Case("no_instrument_function", AT_no_instrument_function)
197    .Case("thiscall", AT_thiscall)
198    .Case("bounded", IgnoredAttribute)       // OpenBSD
199    .Case("pascal", AT_pascal)
200    .Case("__cdecl", AT_cdecl)
201    .Case("__stdcall", AT_stdcall)
202    .Case("__fastcall", AT_fastcall)
203    .Case("__thiscall", AT_thiscall)
204    .Case("__pascal", AT_pascal)
205    .Case("constant", AT_constant)
206    .Case("device", AT_device)
207    .Case("global", AT_global)
208    .Case("host", AT_host)
209    .Case("shared", AT_shared)
210    .Case("launch_bounds", AT_launch_bounds)
211    .Case("common", AT_common)
212    .Case("nocommon", AT_nocommon)
213    .Case("opencl_kernel_function", AT_opencl_kernel_function)
214    .Case("uuid", AT_uuid)
215    .Case("pcs", AT_pcs)
216    .Case("ms_struct", AT_MsStruct)
217    .Case("guarded_var", AT_guarded_var)
218    .Case("pt_guarded_var", AT_pt_guarded_var)
219    .Case("scoped_lockable", AT_scoped_lockable)
220    .Case("lockable", AT_lockable)
221    .Case("no_thread_safety_analysis", AT_no_thread_safety_analysis)
222    .Case("guarded_by", AT_guarded_by)
223    .Case("pt_guarded_by", AT_pt_guarded_by)
224    .Case("acquired_after", AT_acquired_after)
225    .Case("acquired_before", AT_acquired_before)
226    .Case("exclusive_lock_function", AT_exclusive_lock_function)
227    .Case("exclusive_locks_required", AT_exclusive_locks_required)
228    .Case("exclusive_trylock_function", AT_exclusive_trylock_function)
229    .Case("lock_returned", AT_lock_returned)
230    .Case("locks_excluded", AT_locks_excluded)
231    .Case("shared_lock_function", AT_shared_lock_function)
232    .Case("shared_locks_required", AT_shared_locks_required)
233    .Case("shared_trylock_function", AT_shared_trylock_function)
234    .Case("unlock_function", AT_unlock_function)
235    .Default(UnknownAttribute);
236}
237