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