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 "llvm/Support/SourceMgr.h"
21 #include "LLVMContextImpl.h"
24 static ManagedStatic<LLVMContext> GlobalContext;
26 LLVMContext& llvm::getGlobalContext() {
27 return *GlobalContext;
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
31 // Create the first metadata kind, which is always 'dbg'.
32 unsigned DbgID = getMDKindID("dbg");
33 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
35 LLVMContext::~LLVMContext() { delete pImpl; }
37 void LLVMContext::addModule(Module *M) {
38 pImpl->OwnedModules.insert(M);
41 void LLVMContext::removeModule(Module *M) {
42 pImpl->OwnedModules.erase(M);
45 //===----------------------------------------------------------------------===//
46 // Recoverable Backend Errors
47 //===----------------------------------------------------------------------===//
49 void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler,
51 pImpl->InlineAsmDiagHandler = DiagHandler;
52 pImpl->InlineAsmDiagContext = DiagContext;
55 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
56 /// setInlineAsmDiagnosticHandler.
57 void *LLVMContext::getInlineAsmDiagnosticHandler() const {
58 return pImpl->InlineAsmDiagHandler;
61 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
62 /// setInlineAsmDiagnosticHandler.
63 void *LLVMContext::getInlineAsmDiagnosticContext() const {
64 return pImpl->InlineAsmDiagContext;
67 void LLVMContext::emitError(StringRef ErrorStr) {
68 emitError(0U, ErrorStr);
71 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
72 unsigned LocCookie = 0;
73 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
74 if (SrcLoc->getNumOperands() != 0)
75 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
76 LocCookie = CI->getZExtValue();
78 return emitError(LocCookie, ErrorStr);
81 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
82 // If there is no error handler installed, just print the error and exit.
83 if (pImpl->InlineAsmDiagHandler == 0) {
84 errs() << "error: " << ErrorStr << "\n";
88 // If we do have an error handler, we can report the error and keep going.
89 SMDiagnostic Diag("", "error: " + ErrorStr.str());
91 ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
92 (Diag, pImpl->InlineAsmDiagContext, LocCookie);
96 //===----------------------------------------------------------------------===//
97 // Metadata Kind Uniquing
98 //===----------------------------------------------------------------------===//
101 /// isValidName - Return true if Name is a valid custom metadata handler name.
102 static bool isValidName(StringRef MDName) {
106 if (!isalpha(MDName[0]))
109 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
111 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
118 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
119 unsigned LLVMContext::getMDKindID(StringRef Name) const {
120 assert(isValidName(Name) && "Invalid MDNode name");
122 // If this is new, assign it its ID.
124 pImpl->CustomMDKindNames.GetOrCreateValue(
125 Name, pImpl->CustomMDKindNames.size()).second;
128 /// getHandlerNames - Populate client supplied smallvector using custome
129 /// metadata name and ID.
130 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
131 Names.resize(pImpl->CustomMDKindNames.size());
132 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
133 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
134 Names[I->second] = I->first();