AttrImpl.cpp revision dd0e490c24aeade2c59ca4cae171199f6af9f02e
1//===--- AttrImpl.cpp - Classes for representing attributes -----*- 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 contains out-of-line virtual methods for Attr classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Attr.h"
15#include "clang/AST/ASTContext.h"
16using namespace clang;
17
18Attr::~Attr() { }
19
20AttrWithString::AttrWithString(attr::Kind AK, ASTContext &C, llvm::StringRef s)
21  : Attr(AK) {
22  assert(!s.empty());
23  StrLen = s.size();
24  Str = new (C) char[StrLen];
25  memcpy(const_cast<char*>(Str), s.data(), StrLen);
26}
27
28void AttrWithString::ReplaceString(ASTContext &C, llvm::StringRef newS) {
29  if (newS.size() > StrLen) {
30    C.Deallocate(const_cast<char*>(Str));
31    Str = new (C) char[newS.size()];
32  }
33  StrLen = newS.size();
34  memcpy(const_cast<char*>(Str), newS.data(), StrLen);
35}
36
37void FormatAttr::setType(ASTContext &C, llvm::StringRef type) {
38  ReplaceString(C, type);
39}
40
41NonNullAttr::NonNullAttr(ASTContext &C, unsigned* arg_nums, unsigned size)
42  : Attr(attr::NonNull), ArgNums(0), Size(0) {
43  if (size == 0)
44    return;
45  assert(arg_nums);
46  ArgNums = new (C) unsigned[size];
47  Size = size;
48  memcpy(ArgNums, arg_nums, sizeof(*ArgNums)*size);
49}
50
51OwnershipAttr::OwnershipAttr(attr::Kind AK, ASTContext &C, unsigned* arg_nums,
52                                           unsigned size,
53                                           llvm::StringRef module,
54                                           OwnershipKind kind) :
55  AttrWithString(AK, C, module), ArgNums(0), Size(0), OKind(kind) {
56  if (size == 0)
57    return;
58  assert(arg_nums);
59  ArgNums = new (C) unsigned[size];
60  Size = size;
61  AKind = AK;
62  OKind = kind;
63  memcpy(ArgNums, arg_nums, sizeof(*ArgNums) * size);
64}
65
66
67void OwnershipAttr::Destroy(ASTContext &C) {
68  if (ArgNums)
69    C.Deallocate(ArgNums);
70}
71
72OwnershipTakesAttr::OwnershipTakesAttr(ASTContext &C, unsigned* arg_nums,
73                                       unsigned size, llvm::StringRef module) :
74  OwnershipAttr(attr::OwnershipTakes, C, arg_nums, size, module, Takes) {
75}
76
77OwnershipHoldsAttr::OwnershipHoldsAttr(ASTContext &C, unsigned* arg_nums,
78                                       unsigned size, llvm::StringRef module) :
79  OwnershipAttr(attr::OwnershipHolds, C, arg_nums, size, module, Holds) {
80}
81
82OwnershipReturnsAttr::OwnershipReturnsAttr(ASTContext &C, unsigned* arg_nums,
83                                           unsigned size,
84                                           llvm::StringRef module) :
85  OwnershipAttr(attr::OwnershipReturns, C, arg_nums, size, module, Returns) {
86}
87
88#define DEF_SIMPLE_ATTR_CLONE(ATTR)                                     \
89  Attr *ATTR##Attr::clone(ASTContext &C) const {                        \
90    return ::new (C) ATTR##Attr;                                        \
91  }
92
93// FIXME: Can we use variadic macro to define DEF_SIMPLE_ATTR_CLONE for
94// "non-simple" classes?
95
96DEF_SIMPLE_ATTR_CLONE(AlignMac68k)
97DEF_SIMPLE_ATTR_CLONE(AlwaysInline)
98DEF_SIMPLE_ATTR_CLONE(AnalyzerNoReturn)
99DEF_SIMPLE_ATTR_CLONE(BaseCheck)
100DEF_SIMPLE_ATTR_CLONE(CDecl)
101DEF_SIMPLE_ATTR_CLONE(CFReturnsNotRetained)
102DEF_SIMPLE_ATTR_CLONE(CFReturnsRetained)
103DEF_SIMPLE_ATTR_CLONE(Const)
104DEF_SIMPLE_ATTR_CLONE(DLLExport)
105DEF_SIMPLE_ATTR_CLONE(DLLImport)
106DEF_SIMPLE_ATTR_CLONE(Deprecated)
107DEF_SIMPLE_ATTR_CLONE(FastCall)
108DEF_SIMPLE_ATTR_CLONE(Final)
109DEF_SIMPLE_ATTR_CLONE(Hiding)
110DEF_SIMPLE_ATTR_CLONE(Malloc)
111DEF_SIMPLE_ATTR_CLONE(NSReturnsNotRetained)
112DEF_SIMPLE_ATTR_CLONE(NSReturnsRetained)
113DEF_SIMPLE_ATTR_CLONE(NoDebug)
114DEF_SIMPLE_ATTR_CLONE(NoInline)
115DEF_SIMPLE_ATTR_CLONE(NoInstrumentFunction)
116DEF_SIMPLE_ATTR_CLONE(NoReturn)
117DEF_SIMPLE_ATTR_CLONE(NoThrow)
118DEF_SIMPLE_ATTR_CLONE(ObjCException)
119DEF_SIMPLE_ATTR_CLONE(ObjCNSObject)
120DEF_SIMPLE_ATTR_CLONE(Override)
121DEF_SIMPLE_ATTR_CLONE(Packed)
122DEF_SIMPLE_ATTR_CLONE(Pure)
123DEF_SIMPLE_ATTR_CLONE(StdCall)
124DEF_SIMPLE_ATTR_CLONE(ThisCall)
125DEF_SIMPLE_ATTR_CLONE(TransparentUnion)
126DEF_SIMPLE_ATTR_CLONE(Unavailable)
127DEF_SIMPLE_ATTR_CLONE(Unused)
128DEF_SIMPLE_ATTR_CLONE(Used)
129DEF_SIMPLE_ATTR_CLONE(WarnUnusedResult)
130DEF_SIMPLE_ATTR_CLONE(Weak)
131DEF_SIMPLE_ATTR_CLONE(WeakImport)
132DEF_SIMPLE_ATTR_CLONE(WeakRef)
133DEF_SIMPLE_ATTR_CLONE(X86ForceAlignArgPointer)
134
135Attr* MaxFieldAlignmentAttr::clone(ASTContext &C) const {
136  return ::new (C) MaxFieldAlignmentAttr(Alignment);
137}
138
139Attr* AlignedAttr::clone(ASTContext &C) const {
140  return ::new (C) AlignedAttr(Alignment);
141}
142
143Attr* AnnotateAttr::clone(ASTContext &C) const {
144  return ::new (C) AnnotateAttr(C, getAnnotation());
145}
146
147Attr *AsmLabelAttr::clone(ASTContext &C) const {
148  return ::new (C) AsmLabelAttr(C, getLabel());
149}
150
151Attr *AliasAttr::clone(ASTContext &C) const {
152  return ::new (C) AliasAttr(C, getAliasee());
153}
154
155Attr *ConstructorAttr::clone(ASTContext &C) const {
156  return ::new (C) ConstructorAttr(priority);
157}
158
159Attr *DestructorAttr::clone(ASTContext &C) const {
160  return ::new (C) DestructorAttr(priority);
161}
162
163Attr *IBOutletAttr::clone(ASTContext &C) const {
164  return ::new (C) IBOutletAttr;
165}
166
167Attr *IBOutletCollectionAttr::clone(ASTContext &C) const {
168  return ::new (C) IBOutletCollectionAttr(D);
169}
170
171Attr *IBActionAttr::clone(ASTContext &C) const {
172  return ::new (C) IBActionAttr;
173}
174
175Attr *GNUInlineAttr::clone(ASTContext &C) const {
176  return ::new (C) GNUInlineAttr;
177}
178
179Attr *SectionAttr::clone(ASTContext &C) const {
180  return ::new (C) SectionAttr(C, getName());
181}
182
183Attr *NonNullAttr::clone(ASTContext &C) const {
184  return ::new (C) NonNullAttr(C, ArgNums, Size);
185}
186
187Attr *OwnershipAttr::clone(ASTContext &C) const {
188  return ::new (C) OwnershipAttr(AKind, C, ArgNums, Size, getModule(), OKind);
189}
190
191Attr *OwnershipReturnsAttr::clone(ASTContext &C) const {
192  return ::new (C) OwnershipReturnsAttr(C, ArgNums, Size, getModule());
193}
194
195Attr *OwnershipTakesAttr::clone(ASTContext &C) const {
196  return ::new (C) OwnershipTakesAttr(C, ArgNums, Size, getModule());
197}
198
199Attr *OwnershipHoldsAttr::clone(ASTContext &C) const {
200  return ::new (C) OwnershipHoldsAttr(C, ArgNums, Size, getModule());
201}
202
203Attr *FormatAttr::clone(ASTContext &C) const {
204  return ::new (C) FormatAttr(C, getType(), formatIdx, firstArg);
205}
206
207Attr *FormatArgAttr::clone(ASTContext &C) const {
208  return ::new (C) FormatArgAttr(formatIdx);
209}
210
211Attr *SentinelAttr::clone(ASTContext &C) const {
212  return ::new (C) SentinelAttr(sentinel, NullPos);
213}
214
215Attr *VisibilityAttr::clone(ASTContext &C) const {
216  return ::new (C) VisibilityAttr(VisibilityType);
217}
218
219Attr *OverloadableAttr::clone(ASTContext &C) const {
220  return ::new (C) OverloadableAttr;
221}
222
223Attr *BlocksAttr::clone(ASTContext &C) const {
224  return ::new (C) BlocksAttr(BlocksAttrType);
225}
226
227Attr *CleanupAttr::clone(ASTContext &C) const {
228  return ::new (C) CleanupAttr(FD);
229}
230
231Attr *RegparmAttr::clone(ASTContext &C) const {
232  return ::new (C) RegparmAttr(NumParams);
233}
234
235Attr *ReqdWorkGroupSizeAttr::clone(ASTContext &C) const {
236  return ::new (C) ReqdWorkGroupSizeAttr(X, Y, Z);
237}
238
239Attr *InitPriorityAttr::clone(ASTContext &C) const {
240  return ::new (C) InitPriorityAttr(Priority);
241}
242
243Attr *MSP430InterruptAttr::clone(ASTContext &C) const {
244  return ::new (C) MSP430InterruptAttr(Number);
245}
246