2009-06-30 00:48:55 +00:00
|
|
|
//===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-30 17:06:46 +00:00
|
|
|
//
|
|
|
|
// This file implements LLVMContext, as a wrapper around the opaque
|
2009-08-11 17:45:13 +00:00
|
|
|
// class LLVMContextImpl.
|
2009-06-30 17:06:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-30 00:48:55 +00:00
|
|
|
|
|
|
|
#include "llvm/LLVMContext.h"
|
2009-09-03 01:39:20 +00:00
|
|
|
#include "llvm/Metadata.h"
|
2009-06-30 00:48:55 +00:00
|
|
|
#include "llvm/Constants.h"
|
2009-07-13 04:09:18 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2009-06-30 23:39:59 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-06-30 00:48:55 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-06-30 23:39:59 +00:00
|
|
|
static ManagedStatic<LLVMContext> GlobalContext;
|
|
|
|
|
2009-07-01 23:13:44 +00:00
|
|
|
LLVMContext& llvm::getGlobalContext() {
|
2009-07-01 21:22:36 +00:00
|
|
|
return *GlobalContext;
|
2009-06-30 23:39:59 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 23:03:27 +00:00
|
|
|
LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
|
|
|
|
// Create the first metadata kind, which is always 'dbg'.
|
|
|
|
unsigned DbgID = getMDKindID("dbg");
|
|
|
|
assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
|
|
|
|
}
|
2009-06-30 00:48:55 +00:00
|
|
|
LLVMContext::~LLVMContext() { delete pImpl; }
|
2009-08-04 22:41:48 +00:00
|
|
|
|
2010-03-30 20:48:48 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/// isValidName - Return true if Name is a valid custom metadata handler name.
|
|
|
|
static bool isValidName(StringRef MDName) {
|
|
|
|
if (MDName.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!isalpha(MDName[0]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
|
|
|
|
++I) {
|
|
|
|
if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
|
|
|
|
unsigned LLVMContext::getMDKindID(StringRef Name) const {
|
|
|
|
assert(isValidName(Name) && "Invalid MDNode name");
|
|
|
|
|
|
|
|
unsigned &Entry = pImpl->CustomMDKindNames[Name];
|
|
|
|
|
|
|
|
// If this is new, assign it its ID.
|
|
|
|
if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
|
|
|
|
return Entry;
|
2009-08-04 22:41:48 +00:00
|
|
|
}
|
2009-08-11 06:31:57 +00:00
|
|
|
|
2010-03-30 20:48:48 +00:00
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custome
|
|
|
|
/// metadata name and ID.
|
|
|
|
void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
|
|
|
|
Names.resize(pImpl->CustomMDKindNames.size()+1);
|
|
|
|
Names[0] = "";
|
|
|
|
for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
|
|
|
|
E = pImpl->CustomMDKindNames.end(); I != E; ++I)
|
|
|
|
// MD Handlers are numbered from 1.
|
|
|
|
Names[I->second] = I->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
|