LLVM API Documentation

LLVMContext.h
Go to the documentation of this file.
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-c/Core.h"
00019 #include "llvm/Support/CBindingWrapping.h"
00020 #include "llvm/Support/Compiler.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 class DiagnosticInfo;
00031 template <typename T> class SmallVectorImpl;
00032 class Function;
00033 class DebugLoc;
00034 
00035 /// This is an important class for using LLVM in a threaded context.  It
00036 /// (opaquely) owns and manages the core "global" data of LLVM's core
00037 /// infrastructure, including the type and constant uniquing tables.
00038 /// LLVMContext itself provides no locking guarantees, so you should be careful
00039 /// to have one context per thread.
00040 class LLVMContext {
00041 public:
00042   LLVMContextImpl *const pImpl;
00043   LLVMContext();
00044   ~LLVMContext();
00045 
00046   // Pinned metadata names, which always have the same value.  This is a
00047   // compile-time performance optimization, not a correctness optimization.
00048   enum {
00049     MD_dbg = 0,  // "dbg"
00050     MD_tbaa = 1, // "tbaa"
00051     MD_prof = 2,  // "prof"
00052     MD_fpmath = 3,  // "fpmath"
00053     MD_range = 4, // "range"
00054     MD_tbaa_struct = 5, // "tbaa.struct"
00055     MD_invariant_load = 6 // "invariant.load"
00056   };
00057 
00058   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
00059   /// This ID is uniqued across modules in the current LLVMContext.
00060   unsigned getMDKindID(StringRef Name) const;
00061 
00062   /// getMDKindNames - Populate client supplied SmallVector with the name for
00063   /// custom metadata IDs registered in this LLVMContext.
00064   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
00065 
00066 
00067   typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
00068                                          unsigned LocCookie);
00069 
00070   /// Defines the type of a diagnostic handler.
00071   /// \see LLVMContext::setDiagnosticHandler.
00072   /// \see LLVMContext::diagnose.
00073   typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
00074 
00075   /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
00076   /// when problems with inline asm are detected by the backend.  The first
00077   /// argument is a function pointer and the second is a context pointer that
00078   /// gets passed into the DiagHandler.
00079   ///
00080   /// LLVMContext doesn't take ownership or interpret either of these
00081   /// pointers.
00082   void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
00083                                      void *DiagContext = nullptr);
00084 
00085   /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
00086   /// setInlineAsmDiagnosticHandler.
00087   InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
00088 
00089   /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
00090   /// setInlineAsmDiagnosticHandler.
00091   void *getInlineAsmDiagnosticContext() const;
00092 
00093   /// setDiagnosticHandler - This method sets a handler that is invoked
00094   /// when the backend needs to report anything to the user.  The first
00095   /// argument is a function pointer and the second is a context pointer that
00096   /// gets passed into the DiagHandler.
00097   ///
00098   /// LLVMContext doesn't take ownership or interpret either of these
00099   /// pointers.
00100   void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
00101                             void *DiagContext = nullptr);
00102 
00103   /// getDiagnosticHandler - Return the diagnostic handler set by
00104   /// setDiagnosticHandler.
00105   DiagnosticHandlerTy getDiagnosticHandler() const;
00106 
00107   /// getDiagnosticContext - Return the diagnostic context set by
00108   /// setDiagnosticContext.
00109   void *getDiagnosticContext() const;
00110 
00111   /// diagnose - Report a message to the currently installed diagnostic handler.
00112   /// This function returns, in particular in the case of error reporting
00113   /// (DI.Severity == RS_Error), so the caller should leave the compilation
00114   /// process in a self-consistent state, even though the generated code
00115   /// need not be correct.
00116   /// The diagnostic message will be implicitly prefixed with a severity
00117   /// keyword according to \p DI.getSeverity(), i.e., "error: "
00118   /// for RS_Error, "warning: " for RS_Warning, and "note: " for RS_Note.
00119   void diagnose(const DiagnosticInfo &DI);
00120 
00121   /// emitError - Emit an error message to the currently installed error handler
00122   /// with optional location information.  This function returns, so code should
00123   /// be prepared to drop the erroneous construct on the floor and "not crash".
00124   /// The generated code need not be correct.  The error message will be
00125   /// implicitly prefixed with "error: " and should not end with a ".".
00126   void emitError(unsigned LocCookie, const Twine &ErrorStr);
00127   void emitError(const Instruction *I, const Twine &ErrorStr);
00128   void emitError(const Twine &ErrorStr);
00129 
00130   /// emitOptimizationRemark - Emit an optimization remark message. \p PassName
00131   /// is the name of the pass emitting the message. If -Rpass= is given
00132   /// and \p PassName matches the regular expression in -Rpass, then the
00133   /// remark will be emitted. \p Fn is the function triggering the remark,
00134   /// \p DLoc is the debug location where the diagnostic is generated.
00135   /// \p Msg is the message string to use.
00136   void emitOptimizationRemark(const char *PassName, const Function &Fn,
00137                               const DebugLoc &DLoc, const Twine &Msg);
00138 
00139 private:
00140   LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
00141   void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
00142 
00143   /// addModule - Register a module as being instantiated in this context.  If
00144   /// the context is deleted, the module will be deleted as well.
00145   void addModule(Module*);
00146 
00147   /// removeModule - Unregister a module from this context.
00148   void removeModule(Module*);
00149 
00150   // Module needs access to the add/removeModule methods.
00151   friend class Module;
00152 };
00153 
00154 /// getGlobalContext - Returns a global context.  This is for LLVM clients that
00155 /// only care about operating on a single thread.
00156 extern LLVMContext &getGlobalContext();
00157 
00158 // Create wrappers for C Binding types (see CBindingWrapping.h).
00159 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
00160 
00161 /* Specialized opaque context conversions.
00162  */
00163 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
00164   return reinterpret_cast<LLVMContext**>(Tys);
00165 }
00166 
00167 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
00168   return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
00169 }
00170 
00171 }
00172 
00173 #endif