AttrImpl.cpp revision b1c031be513705d924038f497279b9b599868ba1
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(VecReturn) 130DEF_SIMPLE_ATTR_CLONE(WarnUnusedResult) 131DEF_SIMPLE_ATTR_CLONE(Weak) 132DEF_SIMPLE_ATTR_CLONE(WeakImport) 133 134DEF_SIMPLE_ATTR_CLONE(WeakRef) 135DEF_SIMPLE_ATTR_CLONE(X86ForceAlignArgPointer) 136 137Attr* MaxFieldAlignmentAttr::clone(ASTContext &C) const { 138 return ::new (C) MaxFieldAlignmentAttr(Alignment); 139} 140 141Attr* AlignedAttr::clone(ASTContext &C) const { 142 return ::new (C) AlignedAttr(Alignment); 143} 144 145Attr* AnnotateAttr::clone(ASTContext &C) const { 146 return ::new (C) AnnotateAttr(C, getAnnotation()); 147} 148 149Attr *AsmLabelAttr::clone(ASTContext &C) const { 150 return ::new (C) AsmLabelAttr(C, getLabel()); 151} 152 153Attr *AliasAttr::clone(ASTContext &C) const { 154 return ::new (C) AliasAttr(C, getAliasee()); 155} 156 157Attr *ConstructorAttr::clone(ASTContext &C) const { 158 return ::new (C) ConstructorAttr(priority); 159} 160 161Attr *DestructorAttr::clone(ASTContext &C) const { 162 return ::new (C) DestructorAttr(priority); 163} 164 165Attr *IBOutletAttr::clone(ASTContext &C) const { 166 return ::new (C) IBOutletAttr; 167} 168 169Attr *IBOutletCollectionAttr::clone(ASTContext &C) const { 170 return ::new (C) IBOutletCollectionAttr(D); 171} 172 173Attr *IBActionAttr::clone(ASTContext &C) const { 174 return ::new (C) IBActionAttr; 175} 176 177Attr *GNUInlineAttr::clone(ASTContext &C) const { 178 return ::new (C) GNUInlineAttr; 179} 180 181Attr *SectionAttr::clone(ASTContext &C) const { 182 return ::new (C) SectionAttr(C, getName()); 183} 184 185Attr *NonNullAttr::clone(ASTContext &C) const { 186 return ::new (C) NonNullAttr(C, ArgNums, Size); 187} 188 189Attr *OwnershipAttr::clone(ASTContext &C) const { 190 return ::new (C) OwnershipAttr(AKind, C, ArgNums, Size, getModule(), OKind); 191} 192 193Attr *OwnershipReturnsAttr::clone(ASTContext &C) const { 194 return ::new (C) OwnershipReturnsAttr(C, ArgNums, Size, getModule()); 195} 196 197Attr *OwnershipTakesAttr::clone(ASTContext &C) const { 198 return ::new (C) OwnershipTakesAttr(C, ArgNums, Size, getModule()); 199} 200 201Attr *OwnershipHoldsAttr::clone(ASTContext &C) const { 202 return ::new (C) OwnershipHoldsAttr(C, ArgNums, Size, getModule()); 203} 204 205Attr *FormatAttr::clone(ASTContext &C) const { 206 return ::new (C) FormatAttr(C, getType(), formatIdx, firstArg); 207} 208 209Attr *FormatArgAttr::clone(ASTContext &C) const { 210 return ::new (C) FormatArgAttr(formatIdx); 211} 212 213Attr *SentinelAttr::clone(ASTContext &C) const { 214 return ::new (C) SentinelAttr(sentinel, NullPos); 215} 216 217Attr *VisibilityAttr::clone(ASTContext &C) const { 218 return ::new (C) VisibilityAttr(VisibilityType, FromPragma); 219} 220 221Attr *OverloadableAttr::clone(ASTContext &C) const { 222 return ::new (C) OverloadableAttr; 223} 224 225Attr *BlocksAttr::clone(ASTContext &C) const { 226 return ::new (C) BlocksAttr(BlocksAttrType); 227} 228 229Attr *CleanupAttr::clone(ASTContext &C) const { 230 return ::new (C) CleanupAttr(FD); 231} 232 233Attr *RegparmAttr::clone(ASTContext &C) const { 234 return ::new (C) RegparmAttr(NumParams); 235} 236 237Attr *ReqdWorkGroupSizeAttr::clone(ASTContext &C) const { 238 return ::new (C) ReqdWorkGroupSizeAttr(X, Y, Z); 239} 240 241Attr *InitPriorityAttr::clone(ASTContext &C) const { 242 return ::new (C) InitPriorityAttr(Priority); 243} 244 245Attr *MSP430InterruptAttr::clone(ASTContext &C) const { 246 return ::new (C) MSP430InterruptAttr(Number); 247} 248