blob: f9d3fe0021dc03dce80091d962bcf71165ab2a6b [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
Owen Andersona42ac692009-08-13 23:27:32 +000029LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
Owen Anderson8e66e0b2009-06-30 00:48:55 +000030LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000031
Chris Lattnera3b94ba2010-03-30 20:48:48 +000032
33#ifndef NDEBUG
34/// isValidName - Return true if Name is a valid custom metadata handler name.
35static bool isValidName(StringRef MDName) {
36 if (MDName.empty())
37 return false;
38
39 if (!isalpha(MDName[0]))
40 return false;
41
42 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
43 ++I) {
44 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
45 return false;
46 }
47 return true;
Owen Andersonafd0c4c2009-08-04 22:41:48 +000048}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000049#endif
50
51/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
52unsigned LLVMContext::getMDKindID(StringRef Name) const {
53 assert(isValidName(Name) && "Invalid MDNode name");
54
55 unsigned &Entry = pImpl->CustomMDKindNames[Name];
56
57 // If this is new, assign it its ID.
58 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
59 return Entry;
60}
61
62/// getHandlerNames - Populate client supplied smallvector using custome
63/// metadata name and ID.
64void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
65 Names.resize(pImpl->CustomMDKindNames.size()+1);
66 Names[0] = "";
67 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
68 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
69 // MD Handlers are numbered from 1.
70 Names[I->second] = I->first();
71}
72
Devang Patelc5aa8c62009-08-11 06:31:57 +000073