blob: dd379ae5e72c9cb99b2a91e6bcb64da5b8b205c6 [file] [log] [blame]
Owen Anderson2bc29dc2009-06-30 00:48:55 +00001//===-- 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//===----------------------------------------------------------------------===//
Owen Anderson52170072009-06-30 17:06:46 +00009//
10// This file declares LLVMContext, a container of "global" state in LLVM, such
11// as the global type and constant uniquing tables.
12//
13//===----------------------------------------------------------------------===//
Owen Anderson2bc29dc2009-06-30 00:48:55 +000014
Jakub Staszak674be022013-01-10 00:45:19 +000015#ifndef LLVM_IR_LLVMCONTEXT_H
16#define LLVM_IR_LLVMCONTEXT_H
Owen Anderson2bc29dc2009-06-30 00:48:55 +000017
Filip Pizlo40be1e82013-05-01 20:59:00 +000018#include "llvm/Support/CBindingWrapping.h"
Craig Topper9f9ce612012-09-17 07:16:40 +000019#include "llvm/Support/Compiler.h"
Filip Pizlo40be1e82013-05-01 20:59:00 +000020#include "llvm-c/Core.h"
Craig Topper9f9ce612012-09-17 07:16:40 +000021
Owen Anderson2bc29dc2009-06-30 00:48:55 +000022namespace llvm {
23
Benjamin Kramer12ddd402009-08-11 17:45:13 +000024class LLVMContextImpl;
Chris Lattner08113472009-12-29 09:01:33 +000025class StringRef;
Chris Lattner3a4c60c2012-01-03 23:47:05 +000026class Twine;
Chris Lattner38686bd2010-04-07 23:40:44 +000027class Instruction;
Owen Anderson30268be2010-09-08 18:03:32 +000028class Module;
Chris Lattner4afa1282010-11-17 08:13:01 +000029class SMDiagnostic;
Chris Lattner08113472009-12-29 09:01:33 +000030template <typename T> class SmallVectorImpl;
Dan Gohman82a47e92009-10-06 17:43:57 +000031
Owen Anderson52170072009-06-30 17:06:46 +000032/// This is an important class for using LLVM in a threaded context. It
Jim Grosbach4e505a42013-05-24 22:53:06 +000033/// (opaquely) owns and manages the core "global" data of LLVM's core
Owen Anderson52170072009-06-30 17:06:46 +000034/// infrastructure, including the type and constant uniquing tables.
35/// LLVMContext itself provides no locking guarantees, so you should be careful
36/// to have one context per thread.
Benjamin Kramer12ddd402009-08-11 17:45:13 +000037class LLVMContext {
Owen Anderson1e8d5d22010-09-08 18:22:11 +000038public:
39 LLVMContextImpl *const pImpl;
40 LLVMContext();
41 ~LLVMContext();
Jim Grosbach4e505a42013-05-24 22:53:06 +000042
Chris Lattnerec39f092010-03-30 23:03:27 +000043 // Pinned metadata names, which always have the same value. This is a
44 // compile-time performance optimization, not a correctness optimization.
45 enum {
Dan Gohmanb2143b62010-09-14 21:25:10 +000046 MD_dbg = 0, // "dbg"
Jakub Staszak9da99342011-07-06 18:22:43 +000047 MD_tbaa = 1, // "tbaa"
Peter Collingbourne999f90b2011-10-27 19:19:14 +000048 MD_prof = 2, // "prof"
Duncan Sands5e5c5f82012-04-14 12:36:06 +000049 MD_fpmath = 3, // "fpmath"
Dan Gohmanb54834b2012-09-13 17:56:17 +000050 MD_range = 4, // "range"
Shuxin Yang985dac62013-03-06 17:48:48 +000051 MD_tbaa_struct = 5, // "tbaa.struct"
52 MD_invariant_load = 6 // "invariant.load"
Chris Lattnerec39f092010-03-30 23:03:27 +000053 };
Jim Grosbach4e505a42013-05-24 22:53:06 +000054
Chris Lattner08113472009-12-29 09:01:33 +000055 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
56 /// This ID is uniqued across modules in the current LLVMContext.
57 unsigned getMDKindID(StringRef Name) const;
Jim Grosbach4e505a42013-05-24 22:53:06 +000058
Chris Lattner08113472009-12-29 09:01:33 +000059 /// getMDKindNames - Populate client supplied SmallVector with the name for
Dan Gohman5d809112010-07-20 21:45:17 +000060 /// custom metadata IDs registered in this LLVMContext.
Chris Lattner08113472009-12-29 09:01:33 +000061 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
Jim Grosbach4e505a42013-05-24 22:53:06 +000062
63
Bob Wilsonf64c8892013-02-11 05:37:07 +000064 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
65 unsigned LocCookie);
Jim Grosbach4e505a42013-05-24 22:53:06 +000066
Bob Wilsonf64c8892013-02-11 05:37:07 +000067 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
68 /// when problems with inline asm are detected by the backend. The first
69 /// argument is a function pointer and the second is a context pointer that
70 /// gets passed into the DiagHandler.
Chris Lattner42a4ee02010-04-06 00:44:45 +000071 ///
Chris Lattner4afa1282010-11-17 08:13:01 +000072 /// LLVMContext doesn't take ownership or interpret either of these
Chris Lattner42a4ee02010-04-06 00:44:45 +000073 /// pointers.
Bob Wilson04de3152012-12-25 00:07:12 +000074 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
Bob Wilsonf64c8892013-02-11 05:37:07 +000075 void *DiagContext = 0);
76
77 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
78 /// setInlineAsmDiagnosticHandler.
79 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
80
81 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
82 /// setInlineAsmDiagnosticHandler.
83 void *getInlineAsmDiagnosticContext() const;
Jim Grosbach4e505a42013-05-24 22:53:06 +000084
85
Chris Lattner38686bd2010-04-07 23:40:44 +000086 /// emitError - Emit an error message to the currently installed error handler
87 /// with optional location information. This function returns, so code should
88 /// be prepared to drop the erroneous construct on the floor and "not crash".
89 /// The generated code need not be correct. The error message will be
90 /// implicitly prefixed with "error: " and should not end with a ".".
Chris Lattner3a4c60c2012-01-03 23:47:05 +000091 void emitError(unsigned LocCookie, const Twine &ErrorStr);
92 void emitError(const Instruction *I, const Twine &ErrorStr);
93 void emitError(const Twine &ErrorStr);
Owen Andersoneb923302010-09-08 18:41:07 +000094
95private:
Craig Topper9f9ce612012-09-17 07:16:40 +000096 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
97 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
Owen Andersoneb923302010-09-08 18:41:07 +000098
99 /// addModule - Register a module as being instantiated in this context. If
100 /// the context is deleted, the module will be deleted as well.
101 void addModule(Module*);
Jim Grosbach4e505a42013-05-24 22:53:06 +0000102
Owen Andersoneb923302010-09-08 18:41:07 +0000103 /// removeModule - Unregister a module from this context.
104 void removeModule(Module*);
Jim Grosbach4e505a42013-05-24 22:53:06 +0000105
Owen Andersoneb923302010-09-08 18:41:07 +0000106 // Module needs access to the add/removeModule methods.
107 friend class Module;
Owen Anderson2bc29dc2009-06-30 00:48:55 +0000108};
109
Chris Lattner08113472009-12-29 09:01:33 +0000110/// getGlobalContext - Returns a global context. This is for LLVM clients that
111/// only care about operating on a single thread.
112extern LLVMContext &getGlobalContext();
Owen Andersondb47ed02009-06-30 23:39:59 +0000113
Filip Pizlo40be1e82013-05-01 20:59:00 +0000114// Create wrappers for C Binding types (see CBindingWrapping.h).
115DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
116
117/* Specialized opaque context conversions.
118 */
119inline LLVMContext **unwrap(LLVMContextRef* Tys) {
120 return reinterpret_cast<LLVMContext**>(Tys);
121}
122
123inline LLVMContextRef *wrap(const LLVMContext **Tys) {
124 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
125}
126
Owen Anderson2bc29dc2009-06-30 00:48:55 +0000127}
128
129#endif