1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
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 declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_LLVMCONTEXT_H
16 #define LLVM_IR_LLVMCONTEXT_H
18 #include "llvm/Support/CBindingWrapping.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm-c/Core.h"
24 class LLVMContextImpl;
30 template <typename T> class SmallVectorImpl;
32 /// This is an important class for using LLVM in a threaded context. It
33 /// (opaquely) owns and manages the core "global" data of LLVM's core
34 /// infrastructure, including the type and constant uniquing tables.
35 /// LLVMContext itself provides no locking guarantees, so you should be careful
36 /// to have one context per thread.
39 LLVMContextImpl *const pImpl;
43 // Pinned metadata names, which always have the same value. This is a
44 // compile-time performance optimization, not a correctness optimization.
47 MD_tbaa = 1, // "tbaa"
48 MD_prof = 2, // "prof"
49 MD_fpmath = 3, // "fpmath"
50 MD_range = 4, // "range"
51 MD_tbaa_struct = 5, // "tbaa.struct"
52 MD_invariant_load = 6 // "invariant.load"
55 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
56 /// This ID is uniqued across modules in the current LLVMContext.
57 unsigned getMDKindID(StringRef Name) const;
59 /// getMDKindNames - Populate client supplied SmallVector with the name for
60 /// custom metadata IDs registered in this LLVMContext.
61 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
64 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
67 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
68 /// when problems with inline asm are detected by the backend. The first
69 /// argument is a function pointer and the second is a context pointer that
70 /// gets passed into the DiagHandler.
72 /// LLVMContext doesn't take ownership or interpret either of these
74 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
75 void *DiagContext = 0);
77 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
78 /// setInlineAsmDiagnosticHandler.
79 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
81 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
82 /// setInlineAsmDiagnosticHandler.
83 void *getInlineAsmDiagnosticContext() const;
86 /// emitError - Emit an error message to the currently installed error handler
87 /// with optional location information. This function returns, so code should
88 /// be prepared to drop the erroneous construct on the floor and "not crash".
89 /// The generated code need not be correct. The error message will be
90 /// implicitly prefixed with "error: " and should not end with a ".".
91 void emitError(unsigned LocCookie, const Twine &ErrorStr);
92 void emitError(const Instruction *I, const Twine &ErrorStr);
93 void emitError(const Twine &ErrorStr);
96 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
97 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
99 /// addModule - Register a module as being instantiated in this context. If
100 /// the context is deleted, the module will be deleted as well.
101 void addModule(Module*);
103 /// removeModule - Unregister a module from this context.
104 void removeModule(Module*);
106 // Module needs access to the add/removeModule methods.
110 /// getGlobalContext - Returns a global context. This is for LLVM clients that
111 /// only care about operating on a single thread.
112 extern LLVMContext &getGlobalContext();
114 // Create wrappers for C Binding types (see CBindingWrapping.h).
115 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
117 /* Specialized opaque context conversions.
119 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
120 return reinterpret_cast<LLVMContext**>(Tys);
123 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
124 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));