//===-- Attribute.cpp - Implement AttributesList -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Attribute, AttributeImpl, AttrBuilder,
// AttributeSetImpl, and AttributeSet classes.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/Attributes.h"
#include "AttributeImpl.h"
#include "LLVMContextImpl.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Atomic.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
using namespace llvm;
//===----------------------------------------------------------------------===//
// Attribute Implementation
//===----------------------------------------------------------------------===//
Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Kinds) {
AttrBuilder B;
for (ArrayRef<AttrKind>::iterator I = Kinds.begin(), E = Kinds.end();
I != E; ++I)
B.addAttribute(*I);
return Attribute::get(Context, B);
}
Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
// If there are no attributes, return an empty Attribute class.
if (!B.hasAttributes())
return Attribute();
// Otherwise, build a key to look up the existing attributes.
LLVMContextImpl *pImpl = Context.pImpl;
FoldingSetNodeID ID;
ID.AddInteger(B.Raw());
void *InsertPoint;
AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
if (!PA) {
// If we didn't find any existing attributes of the same shape then create a
// new one and insert it.
PA = new AttributeImpl(Context, B.Raw());
pImpl->AttrsSet.InsertNode(PA, InsertPoint);
}
// Return the AttributesList that we found or created.
return Attribute(PA);
}
Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
AttrBuilder B;
return get(Context, B.addAlignmentAttr(Align));
}
Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
uint64_t Align) {
AttrBuilder B;
return get(Context, B.addStackAlignmentAttr(Align));
}
bool Attribute::hasAttribute(AttrKind Val) const {
return pImpl && pImpl->hasAttribute(Val);
}
bool Attribute::hasAttributes() const {
return pImpl && pImpl->hasAttributes();
}
/// This returns the alignment field of an attribute as a byte alignment value.
unsigned Attribute::getAlignment() const {
if (!hasAttribute(Attribute::Alignment))
return 0;
return pImpl->getAlignment();
}
/// This returns the stack alignment field of an attribute as a byte alignment
/// value.
unsigned Attribute::getStackAlignment() const {
if (!hasAttribute(Attribute::StackAlignment))
return 0;
return pImpl->getStackAlignment();
}
bool Attribute::operator==(AttrKind K) const {
return pImpl && *pImpl == K;
}
bool Attribute::operator!=(AttrKind K) const {
return !(*this == K);
}
bool Attribute::operator<(Attribute A) const {
if (!pImpl && !A.pImpl) return false;
if (!pImpl) return true;
if (!A.pImpl) return false;
return *pImpl < *A.pImpl;
}
uint64_t Attribute::Raw() const {
return pImpl ? pImpl->Raw() : 0;
}
std::string Attribute::getAsString() const {
std::string Result;
if (hasAttribute(Attribute::ZExt))
Result += "zeroext ";
if (hasAttribute(Attribute::SExt))
Result += "signext ";
if (hasAttribute(Attribute::NoReturn))
Result += "noreturn ";
if (hasAttribute(Attribute::NoUnwind))
Result += "nounwind ";
if (hasAttribute(Attribute::UWTable))
Result += "uwtable ";
if (hasAttribute(Attribute::ReturnsTwice))
Result += "returns_twice ";
if (hasAttribute(Attribute::InReg))
Result += "inreg ";
if (hasAttribute(Attribute::NoAlias))
Result += "noalias ";
if (hasAttribute(Attribute::NoCapture))
Result += "nocapture ";
if (hasAttribute(Attribute::StructRet))
Result += "sret ";
if (hasAttribute(Attribute::ByVal))
Result += "byval ";
if (hasAttribute(Attribute::Nest))
Result += "nest ";
if (hasAttribute(Attribute::ReadNone))
Result += "readnone ";
if (hasAttribute(Attribute::ReadOnly))
Result += "readonly ";
if (hasAttribute(Attribute::OptimizeForSize))