LLVM API Documentation
00001 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares LLVMContext, a container of "global" state in LLVM, such 00011 // as the global type and constant uniquing tables. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_IR_LLVMCONTEXT_H 00016 #define LLVM_IR_LLVMCONTEXT_H 00017 00018 #include "llvm/Support/CBindingWrapping.h" 00019 #include "llvm/Support/Compiler.h" 00020 #include "llvm-c/Core.h" 00021 00022 namespace llvm { 00023 00024 class LLVMContextImpl; 00025 class StringRef; 00026 class Twine; 00027 class Instruction; 00028 class Module; 00029 class SMDiagnostic; 00030 template <typename T> class SmallVectorImpl; 00031 00032 /// This is an important class for using LLVM in a threaded context. It 00033 /// (opaquely) owns and manages the core "global" data of LLVM's core 00034 /// infrastructure, including the type and constant uniquing tables. 00035 /// LLVMContext itself provides no locking guarantees, so you should be careful 00036 /// to have one context per thread. 00037 class LLVMContext { 00038 public: 00039 LLVMContextImpl *const pImpl; 00040 LLVMContext(); 00041 ~LLVMContext(); 00042 00043 // Pinned metadata names, which always have the same value. This is a 00044 // compile-time performance optimization, not a correctness optimization. 00045 enum { 00046 MD_dbg = 0, // "dbg" 00047 MD_tbaa = 1, // "tbaa" 00048 MD_prof = 2, // "prof" 00049 MD_fpmath = 3, // "fpmath" 00050 MD_range = 4, // "range" 00051 MD_tbaa_struct = 5, // "tbaa.struct" 00052 MD_invariant_load = 6 // "invariant.load" 00053 }; 00054 00055 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. 00056 /// This ID is uniqued across modules in the current LLVMContext. 00057 unsigned getMDKindID(StringRef Name) const; 00058 00059 /// getMDKindNames - Populate client supplied SmallVector with the name for 00060 /// custom metadata IDs registered in this LLVMContext. 00061 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const; 00062 00063 00064 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context, 00065 unsigned LocCookie); 00066 00067 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked 00068 /// when problems with inline asm are detected by the backend. The first 00069 /// argument is a function pointer and the second is a context pointer that 00070 /// gets passed into the DiagHandler. 00071 /// 00072 /// LLVMContext doesn't take ownership or interpret either of these 00073 /// pointers. 00074 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 00075 void *DiagContext = 0); 00076 00077 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 00078 /// setInlineAsmDiagnosticHandler. 00079 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const; 00080 00081 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 00082 /// setInlineAsmDiagnosticHandler. 00083 void *getInlineAsmDiagnosticContext() const; 00084 00085 00086 /// emitError - Emit an error message to the currently installed error handler 00087 /// with optional location information. This function returns, so code should 00088 /// be prepared to drop the erroneous construct on the floor and "not crash". 00089 /// The generated code need not be correct. The error message will be 00090 /// implicitly prefixed with "error: " and should not end with a ".". 00091 void emitError(unsigned LocCookie, const Twine &ErrorStr); 00092 void emitError(const Instruction *I, const Twine &ErrorStr); 00093 void emitError(const Twine &ErrorStr); 00094 00095 private: 00096 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION; 00097 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION; 00098 00099 /// addModule - Register a module as being instantiated in this context. If 00100 /// the context is deleted, the module will be deleted as well. 00101 void addModule(Module*); 00102 00103 /// removeModule - Unregister a module from this context. 00104 void removeModule(Module*); 00105 00106 // Module needs access to the add/removeModule methods. 00107 friend class Module; 00108 }; 00109 00110 /// getGlobalContext - Returns a global context. This is for LLVM clients that 00111 /// only care about operating on a single thread. 00112 extern LLVMContext &getGlobalContext(); 00113 00114 // Create wrappers for C Binding types (see CBindingWrapping.h). 00115 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef) 00116 00117 /* Specialized opaque context conversions. 00118 */ 00119 inline LLVMContext **unwrap(LLVMContextRef* Tys) { 00120 return reinterpret_cast<LLVMContext**>(Tys); 00121 } 00122 00123 inline LLVMContextRef *wrap(const LLVMContext **Tys) { 00124 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys)); 00125 } 00126 00127 } 00128 00129 #endif