AttrImpl.cpp revision 8a2c92cab213bd7e28ff669577e815cd70bafbe3
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
18void Attr::Destroy(ASTContext &C) {
19  if (Next) {
20    Next->Destroy(C);
21    Next = 0;
22  }
23  this->~Attr();
24  C.Deallocate((void*)this);
25}
26
27AttrWithString::AttrWithString(Attr::Kind AK, ASTContext &C, llvm::StringRef s)
28  : Attr(AK) {
29  assert(!s.empty());
30  StrLen = s.size();
31  Str = new (C) char[StrLen];
32  memcpy(const_cast<char*>(Str), s.data(), StrLen);
33}
34
35void AttrWithString::Destroy(ASTContext &C) {
36  C.Deallocate(const_cast<char*>(Str));
37  Attr::Destroy(C);
38}
39
40void AttrWithString::ReplaceString(ASTContext &C, llvm::StringRef newS) {
41  if (newS.size() > StrLen) {
42    C.Deallocate(const_cast<char*>(Str));
43    Str = new (C) char[newS.size()];
44  }
45  StrLen = newS.size();
46  memcpy(const_cast<char*>(Str), newS.data(), StrLen);
47}
48
49void FormatAttr::setType(ASTContext &C, llvm::StringRef type) {
50  ReplaceString(C, type);
51}
52
53NonNullAttr::NonNullAttr(ASTContext &C, unsigned* arg_nums, unsigned size)
54  : Attr(NonNull), ArgNums(0), Size(0) {
55  if (size == 0)
56    return;
57  assert(arg_nums);
58  ArgNums = new (C) unsigned[size];
59  Size = size;
60  memcpy(ArgNums, arg_nums, sizeof(*ArgNums)*size);
61}
62
63void NonNullAttr::Destroy(ASTContext &C) {
64  if (ArgNums)
65    C.Deallocate(ArgNums);
66  Attr::Destroy(C);
67}
68
69#define DEF_SIMPLE_ATTR_CLONE(ATTR)                                     \
70  Attr *ATTR##Attr::clone(ASTContext &C) const {                        \
71    return ::new (C) ATTR##Attr;                                        \
72  }
73
74// FIXME: Can we use variadic macro to define DEF_SIMPLE_ATTR_CLONE for
75// "non-simple" classes?
76
77DEF_SIMPLE_ATTR_CLONE(AlwaysInline)
78DEF_SIMPLE_ATTR_CLONE(AnalyzerNoReturn)
79DEF_SIMPLE_ATTR_CLONE(BaseCheck)
80DEF_SIMPLE_ATTR_CLONE(CDecl)
81DEF_SIMPLE_ATTR_CLONE(CFReturnsNotRetained)
82DEF_SIMPLE_ATTR_CLONE(CFReturnsRetained)
83DEF_SIMPLE_ATTR_CLONE(Const)
84DEF_SIMPLE_ATTR_CLONE(DLLExport)
85DEF_SIMPLE_ATTR_CLONE(DLLImport)
86DEF_SIMPLE_ATTR_CLONE(Deprecated)
87DEF_SIMPLE_ATTR_CLONE(FastCall)
88DEF_SIMPLE_ATTR_CLONE(Final)
89DEF_SIMPLE_ATTR_CLONE(Hiding)
90DEF_SIMPLE_ATTR_CLONE(Malloc)
91DEF_SIMPLE_ATTR_CLONE(NSReturnsNotRetained)
92DEF_SIMPLE_ATTR_CLONE(NSReturnsRetained)
93DEF_SIMPLE_ATTR_CLONE(NoDebug)
94DEF_SIMPLE_ATTR_CLONE(NoInline)
95DEF_SIMPLE_ATTR_CLONE(NoReturn)
96DEF_SIMPLE_ATTR_CLONE(NoThrow)
97DEF_SIMPLE_ATTR_CLONE(ObjCException)
98DEF_SIMPLE_ATTR_CLONE(ObjCNSObject)
99DEF_SIMPLE_ATTR_CLONE(Override)
100DEF_SIMPLE_ATTR_CLONE(Packed)
101DEF_SIMPLE_ATTR_CLONE(Pure)
102DEF_SIMPLE_ATTR_CLONE(StdCall)
103DEF_SIMPLE_ATTR_CLONE(ThisCall)
104DEF_SIMPLE_ATTR_CLONE(TransparentUnion)
105DEF_SIMPLE_ATTR_CLONE(Unavailable)
106DEF_SIMPLE_ATTR_CLONE(Unused)
107DEF_SIMPLE_ATTR_CLONE(Used)
108DEF_SIMPLE_ATTR_CLONE(WarnUnusedResult)
109DEF_SIMPLE_ATTR_CLONE(Weak)
110DEF_SIMPLE_ATTR_CLONE(WeakImport)
111DEF_SIMPLE_ATTR_CLONE(WeakRef)
112DEF_SIMPLE_ATTR_CLONE(X86ForceAlignArgPointer)
113
114Attr* MaxFieldAlignmentAttr::clone(ASTContext &C) const {
115  return ::new (C) MaxFieldAlignmentAttr(Alignment);
116}
117
118Attr* AlignedAttr::clone(ASTContext &C) const {
119  return ::new (C) AlignedAttr(Alignment);
120}
121
122Attr* AnnotateAttr::clone(ASTContext &C) const {
123  return ::new (C) AnnotateAttr(C, getAnnotation());
124}
125
126Attr *AsmLabelAttr::clone(ASTContext &C) const {
127  return ::new (C) AsmLabelAttr(C, getLabel());
128}
129
130Attr *AliasAttr::clone(ASTContext &C) const {
131  return ::new (C) AliasAttr(C, getAliasee());
132}
133
134Attr *ConstructorAttr::clone(ASTContext &C) const {
135  return ::new (C) ConstructorAttr(priority);
136}
137
138Attr *DestructorAttr::clone(ASTContext &C) const {
139  return ::new (C) DestructorAttr(priority);
140}
141
142Attr *IBOutletAttr::clone(ASTContext &C) const {
143  return ::new (C) IBOutletAttr;
144}
145
146Attr *IBOutletCollectionAttr::clone(ASTContext &C) const {
147  return ::new (C) IBOutletCollectionAttr(D);
148}
149
150Attr *IBActionAttr::clone(ASTContext &C) const {
151  return ::new (C) IBActionAttr;
152}
153
154Attr *GNUInlineAttr::clone(ASTContext &C) const {
155  return ::new (C) GNUInlineAttr;
156}
157
158Attr *SectionAttr::clone(ASTContext &C) const {
159  return ::new (C) SectionAttr(C, getName());
160}
161
162Attr *NonNullAttr::clone(ASTContext &C) const {
163  return ::new (C) NonNullAttr(C, ArgNums, Size);
164}
165
166Attr *FormatAttr::clone(ASTContext &C) const {
167  return ::new (C) FormatAttr(C, getType(), formatIdx, firstArg);
168}
169
170Attr *FormatArgAttr::clone(ASTContext &C) const {
171  return ::new (C) FormatArgAttr(formatIdx);
172}
173
174Attr *SentinelAttr::clone(ASTContext &C) const {
175  return ::new (C) SentinelAttr(sentinel, NullPos);
176}
177
178Attr *VisibilityAttr::clone(ASTContext &C) const {
179  return ::new (C) VisibilityAttr(VisibilityType);
180}
181
182Attr *OverloadableAttr::clone(ASTContext &C) const {
183  return ::new (C) OverloadableAttr;
184}
185
186Attr *BlocksAttr::clone(ASTContext &C) const {
187  return ::new (C) BlocksAttr(BlocksAttrType);
188}
189
190Attr *CleanupAttr::clone(ASTContext &C) const {
191  return ::new (C) CleanupAttr(FD);
192}
193
194Attr *RegparmAttr::clone(ASTContext &C) const {
195  return ::new (C) RegparmAttr(NumParams);
196}
197
198Attr *ReqdWorkGroupSizeAttr::clone(ASTContext &C) const {
199  return ::new (C) ReqdWorkGroupSizeAttr(X, Y, Z);
200}
201
202Attr *MSP430InterruptAttr::clone(ASTContext &C) const {
203  return ::new (C) MSP430InterruptAttr(Number);
204}
205