blob: 3244f2842c4fdf0b2662b54185b7bdcfb61d72fd [file] [log] [blame]
Owen Anderson8e66e0b2009-06-30 00:48:55 +00001//===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Owen Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file implements LLVMContext, as a wrapper around the opaque
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000011// class LLVMContextImpl.
Owen Anderson36f62e52009-06-30 17:06:46 +000012//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
15#include "llvm/LLVMContext.h"
Devang Patelf7188322009-09-03 01:39:20 +000016#include "llvm/Metadata.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000017#include "llvm/Constants.h"
Owen Anderson53a52212009-07-13 04:09:18 +000018#include "llvm/Instruction.h"
Owen Anderson1938fb12009-06-30 23:39:59 +000019#include "llvm/Support/ManagedStatic.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000020#include "LLVMContextImpl.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000021using namespace llvm;
22
Owen Anderson1938fb12009-06-30 23:39:59 +000023static ManagedStatic<LLVMContext> GlobalContext;
24
Owen Anderson2a154432009-07-01 23:13:44 +000025LLVMContext& llvm::getGlobalContext() {
Owen Anderson1cf085d2009-07-01 21:22:36 +000026 return *GlobalContext;
Owen Anderson1938fb12009-06-30 23:39:59 +000027}
28
Chris Lattnerc263b422010-03-30 23:03:27 +000029LLVMContext::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;
33}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000034LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000035
Chris Lattner60955d42010-04-06 00:44:45 +000036void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler,
37 void *DiagContext) {
38 pImpl->InlineAsmDiagHandler = DiagHandler;
39 pImpl->InlineAsmDiagContext = DiagContext;
40}
41
42/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
43/// setInlineAsmDiagnosticHandler.
44void *LLVMContext::getInlineAsmDiagnosticHandler() const {
45 return pImpl->InlineAsmDiagHandler;
46}
47
48/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
49/// setInlineAsmDiagnosticHandler.
50void *LLVMContext::getInlineAsmDiagnosticContext() const {
51 return pImpl->InlineAsmDiagContext;
52}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000053
54#ifndef NDEBUG
55/// isValidName - Return true if Name is a valid custom metadata handler name.
56static bool isValidName(StringRef MDName) {
57 if (MDName.empty())
58 return false;
59
60 if (!isalpha(MDName[0]))
61 return false;
62
63 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
64 ++I) {
65 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
66 return false;
67 }
68 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +000069}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000070#endif
71
72/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
73unsigned LLVMContext::getMDKindID(StringRef Name) const {
74 assert(isValidName(Name) && "Invalid MDNode name");
75
76 unsigned &Entry = pImpl->CustomMDKindNames[Name];
77
78 // If this is new, assign it its ID.
79 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
80 return Entry;
81}
82
83/// getHandlerNames - Populate client supplied smallvector using custome
84/// metadata name and ID.
85void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
86 Names.resize(pImpl->CustomMDKindNames.size()+1);
87 Names[0] = "";
88 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
89 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
90 // MD Handlers are numbered from 1.
91 Names[I->second] = I->first();
92}