[opaque pointer types] Push the passing of value types up from Function/GlobalVariabl...
[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 #include "llvm/Support/Options.h"
22
23 namespace llvm {
24
25 class LLVMContextImpl;
26 class StringRef;
27 class Twine;
28 class Instruction;
29 class Module;
30 class SMDiagnostic;
31 class DiagnosticInfo;
32 template <typename T> class SmallVectorImpl;
33 class Function;
34 class DebugLoc;
35
36 /// This is an important class for using LLVM in a threaded context.  It
37 /// (opaquely) owns and manages the core "global" data of LLVM's core
38 /// infrastructure, including the type and constant uniquing tables.
39 /// LLVMContext itself provides no locking guarantees, so you should be careful
40 /// to have one context per thread.
41 class LLVMContext {
42 public:
43   LLVMContextImpl *const pImpl;
44   LLVMContext();
45   ~LLVMContext();
46
47   // Pinned metadata names, which always have the same value.  This is a
48   // compile-time performance optimization, not a correctness optimization.
49   enum {
50     MD_dbg = 0,  // "dbg"
51     MD_tbaa = 1, // "tbaa"
52     MD_prof = 2,  // "prof"
53     MD_fpmath = 3,  // "fpmath"
54     MD_range = 4, // "range"
55     MD_tbaa_struct = 5, // "tbaa.struct"
56     MD_invariant_load = 6, // "invariant.load"
57     MD_alias_scope = 7, // "alias.scope"
58     MD_noalias = 8, // "noalias",
59     MD_nontemporal = 9, // "nontemporal"
60     MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
61     MD_nonnull = 11, // "nonnull"
62     MD_dereferenceable = 12, // "dereferenceable"
63     MD_dereferenceable_or_null = 13, // "dereferenceable_or_null"
64     MD_make_implicit = 14 // "make.implicit"
65   };
66
67   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
68   /// This ID is uniqued across modules in the current LLVMContext.
69   unsigned getMDKindID(StringRef Name) const;
70
71   /// getMDKindNames - Populate client supplied SmallVector with the name for
72   /// custom metadata IDs registered in this LLVMContext.
73   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
74
75
76   typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
77                                          unsigned LocCookie);
78
79   /// Defines the type of a diagnostic handler.
80   /// \see LLVMContext::setDiagnosticHandler.
81   /// \see LLVMContext::diagnose.
82   typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
83
84   /// Defines the type of a yield callback.
85   /// \see LLVMContext::setYieldCallback.
86   typedef void (*YieldCallbackTy)(LLVMContext *Context, void *OpaqueHandle);
87
88   /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
89   /// when problems with inline asm are detected by the backend.  The first
90   /// argument is a function pointer and the second is a context pointer that
91   /// gets passed into the DiagHandler.
92   ///
93   /// LLVMContext doesn't take ownership or interpret either of these
94   /// pointers.
95   void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
96                                      void *DiagContext = nullptr);
97
98   /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
99   /// setInlineAsmDiagnosticHandler.
100   InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
101
102   /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
103   /// setInlineAsmDiagnosticHandler.
104   void *getInlineAsmDiagnosticContext() const;
105
106   /// setDiagnosticHandler - This method sets a handler that is invoked
107   /// when the backend needs to report anything to the user.  The first
108   /// argument is a function pointer and the second is a context pointer that
109   /// gets passed into the DiagHandler.  The third argument should be set to
110   /// true if the handler only expects enabled diagnostics.
111   ///
112   /// LLVMContext doesn't take ownership or interpret either of these
113   /// pointers.
114   void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
115                             void *DiagContext = nullptr,
116                             bool RespectFilters = false);
117
118   /// getDiagnosticHandler - Return the diagnostic handler set by
119   /// setDiagnosticHandler.
120   DiagnosticHandlerTy getDiagnosticHandler() const;
121
122   /// getDiagnosticContext - Return the diagnostic context set by
123   /// setDiagnosticContext.
124   void *getDiagnosticContext() const;
125
126   /// \brief Report a message to the currently installed diagnostic handler.
127   ///
128   /// This function returns, in particular in the case of error reporting
129   /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
130   /// process in a self-consistent state, even though the generated code
131   /// need not be correct.
132   ///
133   /// The diagnostic message will be implicitly prefixed with a severity keyword
134   /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
135   /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
136   void diagnose(const DiagnosticInfo &DI);
137
138   /// \brief Registers a yield callback with the given context.
139   ///
140   /// The yield callback function may be called by LLVM to transfer control back
141   /// to the client that invoked the LLVM compilation. This can be used to yield
142   /// control of the thread, or perform periodic work needed by the client.
143   /// There is no guaranteed frequency at which callbacks must occur; in fact,
144   /// the client is not guaranteed to ever receive this callback. It is at the
145   /// sole discretion of LLVM to do so and only if it can guarantee that
146   /// suspending the thread won't block any forward progress in other LLVM
147   /// contexts in the same process.
148   ///
149   /// At a suspend point, the state of the current LLVM context is intentionally
150   /// undefined. No assumptions about it can or should be made. Only LLVM
151   /// context API calls that explicitly state that they can be used during a
152   /// yield callback are allowed to be used. Any other API calls into the
153   /// context are not supported until the yield callback function returns
154   /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
155   void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
156
157   /// \brief Calls the yield callback (if applicable).
158   ///
159   /// This transfers control of the current thread back to the client, which may
160   /// suspend the current thread. Only call this method when LLVM doesn't hold
161   /// any global mutex or cannot block the execution in another LLVM context.
162   void yield();
163
164   /// emitError - Emit an error message to the currently installed error handler
165   /// with optional location information.  This function returns, so code should
166   /// be prepared to drop the erroneous construct on the floor and "not crash".
167   /// The generated code need not be correct.  The error message will be
168   /// implicitly prefixed with "error: " and should not end with a ".".
169   void emitError(unsigned LocCookie, const Twine &ErrorStr);
170   void emitError(const Instruction *I, const Twine &ErrorStr);
171   void emitError(const Twine &ErrorStr);
172
173   /// \brief Query for a debug option's value.
174   ///
175   /// This function returns typed data populated from command line parsing.
176   template <typename ValT, typename Base, ValT(Base::*Mem)>
177   ValT getOption() const {
178     return OptionRegistry::instance().template get<ValT, Base, Mem>();
179   }
180
181 private:
182   LLVMContext(LLVMContext&) = delete;
183   void operator=(LLVMContext&) = delete;
184
185   /// addModule - Register a module as being instantiated in this context.  If
186   /// the context is deleted, the module will be deleted as well.
187   void addModule(Module*);
188
189   /// removeModule - Unregister a module from this context.
190   void removeModule(Module*);
191
192   // Module needs access to the add/removeModule methods.
193   friend class Module;
194 };
195
196 /// getGlobalContext - Returns a global context.  This is for LLVM clients that
197 /// only care about operating on a single thread.
198 extern LLVMContext &getGlobalContext();
199
200 // Create wrappers for C Binding types (see CBindingWrapping.h).
201 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
202
203 /* Specialized opaque context conversions.
204  */
205 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
206   return reinterpret_cast<LLVMContext**>(Tys);
207 }
208
209 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
210   return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
211 }
212
213 }
214
215 #endif