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/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instruction.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/Support/DiagnosticInfo.h"
21 #include "llvm/Support/DiagnosticPrinter.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/SourceMgr.h"
27 static ManagedStatic<LLVMContext> GlobalContext;
29 LLVMContext& llvm::getGlobalContext() {
30 return *GlobalContext;
33 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
34 // Create the fixed metadata kinds. This is done in the same order as the
35 // MD_* enum values so that they correspond.
37 // Create the 'dbg' metadata kind.
38 unsigned DbgID = getMDKindID("dbg");
39 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
41 // Create the 'tbaa' metadata kind.
42 unsigned TBAAID = getMDKindID("tbaa");
43 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
45 // Create the 'prof' metadata kind.
46 unsigned ProfID = getMDKindID("prof");
47 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
49 // Create the 'fpmath' metadata kind.
50 unsigned FPAccuracyID = getMDKindID("fpmath");
51 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
54 // Create the 'range' metadata kind.
55 unsigned RangeID = getMDKindID("range");
56 assert(RangeID == MD_range && "range kind id drifted");
59 // Create the 'tbaa.struct' metadata kind.
60 unsigned TBAAStructID = getMDKindID("tbaa.struct");
61 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
64 // Create the 'invariant.load' metadata kind.
65 unsigned InvariantLdId = getMDKindID("invariant.load");
66 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
69 LLVMContext::~LLVMContext() { delete pImpl; }
71 void LLVMContext::addModule(Module *M) {
72 pImpl->OwnedModules.insert(M);
75 void LLVMContext::removeModule(Module *M) {
76 pImpl->OwnedModules.erase(M);
79 //===----------------------------------------------------------------------===//
80 // Recoverable Backend Errors
81 //===----------------------------------------------------------------------===//
84 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
86 pImpl->InlineAsmDiagHandler = DiagHandler;
87 pImpl->InlineAsmDiagContext = DiagContext;
90 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
91 /// setInlineAsmDiagnosticHandler.
92 LLVMContext::InlineAsmDiagHandlerTy
93 LLVMContext::getInlineAsmDiagnosticHandler() const {
94 return pImpl->InlineAsmDiagHandler;
97 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
98 /// setInlineAsmDiagnosticHandler.
99 void *LLVMContext::getInlineAsmDiagnosticContext() const {
100 return pImpl->InlineAsmDiagContext;
103 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
104 void *DiagnosticContext) {
105 pImpl->DiagnosticHandler = DiagnosticHandler;
106 pImpl->DiagnosticContext = DiagnosticContext;
109 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
110 return pImpl->DiagnosticHandler;
113 void *LLVMContext::getDiagnosticContext() const {
114 return pImpl->DiagnosticContext;
117 void LLVMContext::emitError(const Twine &ErrorStr) {
118 emitError(0U, ErrorStr);
121 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
122 unsigned LocCookie = 0;
123 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
124 if (SrcLoc->getNumOperands() != 0)
125 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
126 LocCookie = CI->getZExtValue();
128 return emitError(LocCookie, ErrorStr);
131 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
132 // If there is a report handler, use it.
133 if (pImpl->DiagnosticHandler != 0) {
134 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
137 // Otherwise, print the message with a prefix based on the severity.
138 std::string MsgStorage;
139 raw_string_ostream Stream(MsgStorage);
140 DiagnosticPrinterRawOStream DP(Stream);
143 switch (DI.getSeverity()) {
145 errs() << "error: " << MsgStorage << "\n";
148 errs() << "warning: " << MsgStorage << "\n";
151 errs() << "note: " << MsgStorage << "\n";
156 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
157 // If there is no error handler installed, just print the error and exit.
158 if (pImpl->InlineAsmDiagHandler == 0) {
159 errs() << "error: " << ErrorStr << "\n";
163 // If we do have an error handler, we can report the error and keep going.
164 SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
166 pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
169 //===----------------------------------------------------------------------===//
170 // Metadata Kind Uniquing
171 //===----------------------------------------------------------------------===//
174 /// isValidName - Return true if Name is a valid custom metadata handler name.
175 static bool isValidName(StringRef MDName) {
179 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
182 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
184 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
185 *I != '-' && *I != '.')
192 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
193 unsigned LLVMContext::getMDKindID(StringRef Name) const {
194 assert(isValidName(Name) && "Invalid MDNode name");
196 // If this is new, assign it its ID.
198 pImpl->CustomMDKindNames.GetOrCreateValue(
199 Name, pImpl->CustomMDKindNames.size()).second;
202 /// getHandlerNames - Populate client supplied smallvector using custome
203 /// metadata name and ID.
204 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
205 Names.resize(pImpl->CustomMDKindNames.size());
206 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
207 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
208 Names[I->second] = I->first();