Home | History | Annotate | Download | only in llvm
      1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
      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 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares LLVMContext, a container of "global" state in LLVM, such
     11 // as the global type and constant uniquing tables.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_LLVMCONTEXT_H
     16 #define LLVM_LLVMCONTEXT_H
     17 
     18 namespace llvm {
     19 
     20 class LLVMContextImpl;
     21 class StringRef;
     22 class Twine;
     23 class Instruction;
     24 class Module;
     25 class SMDiagnostic;
     26 template <typename T> class SmallVectorImpl;
     27 
     28 /// This is an important class for using LLVM in a threaded context.  It
     29 /// (opaquely) owns and manages the core "global" data of LLVM's core
     30 /// infrastructure, including the type and constant uniquing tables.
     31 /// LLVMContext itself provides no locking guarantees, so you should be careful
     32 /// to have one context per thread.
     33 class LLVMContext {
     34 public:
     35   LLVMContextImpl *const pImpl;
     36   LLVMContext();
     37   ~LLVMContext();
     38 
     39   // Pinned metadata names, which always have the same value.  This is a
     40   // compile-time performance optimization, not a correctness optimization.
     41   enum {
     42     MD_dbg = 0,  // "dbg"
     43     MD_tbaa = 1, // "tbaa"
     44     MD_prof = 2,  // "prof"
     45     MD_fpmath = 3,  // "fpmath"
     46     MD_range = 4 // "range"
     47   };
     48 
     49   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
     50   /// This ID is uniqued across modules in the current LLVMContext.
     51   unsigned getMDKindID(StringRef Name) const;
     52 
     53   /// getMDKindNames - Populate client supplied SmallVector with the name for
     54   /// custom metadata IDs registered in this LLVMContext.
     55   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
     56 
     57 
     58   typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
     59                                          unsigned LocCookie);
     60 
     61   /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
     62   /// when problems with inline asm are detected by the backend.  The first
     63   /// argument is a function pointer and the second is a context pointer that
     64   /// gets passed into the DiagHandler.
     65   ///
     66   /// LLVMContext doesn't take ownership or interpret either of these
     67   /// pointers.
     68   void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
     69                                      void *DiagContext = 0);
     70 
     71   /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
     72   /// setInlineAsmDiagnosticHandler.
     73   InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
     74 
     75   /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
     76   /// setInlineAsmDiagnosticHandler.
     77   void *getInlineAsmDiagnosticContext() const;
     78 
     79 
     80   /// emitError - Emit an error message to the currently installed error handler
     81   /// with optional location information.  This function returns, so code should
     82   /// be prepared to drop the erroneous construct on the floor and "not crash".
     83   /// The generated code need not be correct.  The error message will be
     84   /// implicitly prefixed with "error: " and should not end with a ".".
     85   void emitError(unsigned LocCookie, const Twine &ErrorStr);
     86   void emitError(const Instruction *I, const Twine &ErrorStr);
     87   void emitError(const Twine &ErrorStr);
     88 
     89 private:
     90   // DO NOT IMPLEMENT
     91   LLVMContext(LLVMContext&);
     92   void operator=(LLVMContext&);
     93 
     94   /// addModule - Register a module as being instantiated in this context.  If
     95   /// the context is deleted, the module will be deleted as well.
     96   void addModule(Module*);
     97 
     98   /// removeModule - Unregister a module from this context.
     99   void removeModule(Module*);
    100 
    101   // Module needs access to the add/removeModule methods.
    102   friend class Module;
    103 };
    104 
    105 /// getGlobalContext - Returns a global context.  This is for LLVM clients that
    106 /// only care about operating on a single thread.
    107 extern LLVMContext &getGlobalContext();
    108 
    109 }
    110 
    111 #endif
    112