Re-sort all of the includes with ./utils/sort_includes.py so that
[oota-llvm.git] / include / llvm / IR / LLVMContext.h
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_IR_LLVMCONTEXT_H
16 #define LLVM_IR_LLVMCONTEXT_H
17
18 #include "llvm-c/Core.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Compiler.h"
21
22 namespace llvm {
23
24 class LLVMContextImpl;
25 class StringRef;
26 class Twine;
27 class Instruction;
28 class Module;
29 class SMDiagnostic;
30 class DiagnosticInfo;
31 template <typename T> class SmallVectorImpl;
32
33 /// This is an important class for using LLVM in a threaded context.  It
34 /// (opaquely) owns and manages the core "global" data of LLVM's core
35 /// infrastructure, including the type and constant uniquing tables.
36 /// LLVMContext itself provides no locking guarantees, so you should be careful
37 /// to have one context per thread.
38 class LLVMContext {
39 public:
40   LLVMContextImpl *const pImpl;
41   LLVMContext();
42   ~LLVMContext();
43
44   // Pinned metadata names, which always have the same value.  This is a
45   // compile-time performance optimization, not a correctness optimization.
46   enum {
47     MD_dbg = 0,  // "dbg"
48     MD_tbaa = 1, // "tbaa"
49     MD_prof = 2,  // "prof"
50     MD_fpmath = 3,  // "fpmath"
51     MD_range = 4, // "range"
52     MD_tbaa_struct = 5, // "tbaa.struct"
53     MD_invariant_load = 6 // "invariant.load"
54   };
55
56   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
57   /// This ID is uniqued across modules in the current LLVMContext.
58   unsigned getMDKindID(StringRef Name) const;
59
60   /// getMDKindNames - Populate client supplied SmallVector with the name for
61   /// custom metadata IDs registered in this LLVMContext.
62   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
63
64
65   typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
66                                          unsigned LocCookie);
67
68   /// Defines the type of a diagnostic handler.
69   /// \see LLVMContext::setDiagnosticHandler.
70   /// \see LLVMContext::diagnose.
71   typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
72
73   /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
74   /// when problems with inline asm are detected by the backend.  The first
75   /// argument is a function pointer and the second is a context pointer that
76   /// gets passed into the DiagHandler.
77   ///
78   /// LLVMContext doesn't take ownership or interpret either of these
79   /// pointers.
80   void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
81                                      void *DiagContext = 0);
82
83   /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
84   /// setInlineAsmDiagnosticHandler.
85   InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
86
87   /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
88   /// setInlineAsmDiagnosticHandler.
89   void *getInlineAsmDiagnosticContext() const;
90
91   /// setDiagnosticHandler - This method sets a handler that is invoked
92   /// when the backend needs to report anything to the user.  The first
93   /// argument is a function pointer and the second is a context pointer that
94   /// gets passed into the DiagHandler.
95   ///
96   /// LLVMContext doesn't take ownership or interpret either of these
97   /// pointers.
98   void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
99                             void *DiagContext = 0);
100
101   /// getDiagnosticHandler - Return the diagnostic handler set by
102   /// setDiagnosticHandler.
103   DiagnosticHandlerTy getDiagnosticHandler() const;
104
105   /// getDiagnosticContext - Return the diagnostic context set by
106   /// setDiagnosticContext.
107   void *getDiagnosticContext() const;
108
109   /// diagnose - Report a message to the currently installed diagnostic handler.
110   /// This function returns, in particular in the case of error reporting
111   /// (DI.Severity == RS_Error), so the caller should leave the compilation
112   /// process in a self-consistent state, even though the generated code
113   /// need not be correct.
114   /// The diagnostic message will be implicitly prefixed with a severity
115   /// keyword according to \p DI.getSeverity(), i.e., "error: "
116   /// for RS_Error, "warning: " for RS_Warning, and "note: " for RS_Note.
117   void diagnose(const DiagnosticInfo &DI);
118
119   /// emitError - Emit an error message to the currently installed error handler
120   /// with optional location information.  This function returns, so code should
121   /// be prepared to drop the erroneous construct on the floor and "not crash".
122   /// The generated code need not be correct.  The error message will be
123   /// implicitly prefixed with "error: " and should not end with a ".".
124   void emitError(unsigned LocCookie, const Twine &ErrorStr);
125   void emitError(const Instruction *I, const Twine &ErrorStr);
126   void emitError(const Twine &ErrorStr);
127
128 private:
129   LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
130   void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
131
132   /// addModule - Register a module as being instantiated in this context.  If
133   /// the context is deleted, the module will be deleted as well.
134   void addModule(Module*);
135
136   /// removeModule - Unregister a module from this context.
137   void removeModule(Module*);
138
139   // Module needs access to the add/removeModule methods.
140   friend class Module;
141 };
142
143 /// getGlobalContext - Returns a global context.  This is for LLVM clients that
144 /// only care about operating on a single thread.
145 extern LLVMContext &getGlobalContext();
146
147 // Create wrappers for C Binding types (see CBindingWrapping.h).
148 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
149
150 /* Specialized opaque context conversions.
151  */
152 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
153   return reinterpret_cast<LLVMContext**>(Tys);
154 }
155
156 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
157   return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
158 }
159
160 }
161
162 #endif