1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Metadata.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "LLVMContextImpl.h"
23 static ManagedStatic<LLVMContext> GlobalContext;
25 LLVMContext& llvm::getGlobalContext() {
26 return *GlobalContext;
29 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
30 // Create the first metadata kind, which is always 'dbg'.
31 unsigned DbgID = getMDKindID("dbg");
32 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
34 LLVMContext::~LLVMContext() { delete pImpl; }
38 /// isValidName - Return true if Name is a valid custom metadata handler name.
39 static bool isValidName(StringRef MDName) {
43 if (!isalpha(MDName[0]))
46 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
48 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
55 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
56 unsigned LLVMContext::getMDKindID(StringRef Name) const {
57 assert(isValidName(Name) && "Invalid MDNode name");
59 unsigned &Entry = pImpl->CustomMDKindNames[Name];
61 // If this is new, assign it its ID.
62 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
66 /// getHandlerNames - Populate client supplied smallvector using custome
67 /// metadata name and ID.
68 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
69 Names.resize(pImpl->CustomMDKindNames.size()+1);
71 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
72 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
73 // MD Handlers are numbered from 1.
74 Names[I->second] = I->first();