blob: 5903e2e55e1f0120e80c43301677e4a96764159e [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
15#ifndef LLVM_LLVMCONTEXT_H
16#define LLVM_LLVMCONTEXT_H
17
Craig Topper9f9ce612012-09-17 07:16:40 +000018#include "llvm/Support/Compiler.h"
19
Owen Anderson2bc29dc2009-06-30 00:48:55 +000020namespace llvm {
21
Benjamin Kramer12ddd402009-08-11 17:45:13 +000022class LLVMContextImpl;
Chris Lattner08113472009-12-29 09:01:33 +000023class StringRef;
Chris Lattner3a4c60c2012-01-03 23:47:05 +000024class Twine;
Chris Lattner38686bd2010-04-07 23:40:44 +000025class Instruction;
Owen Anderson30268be2010-09-08 18:03:32 +000026class Module;
Chris Lattner4afa1282010-11-17 08:13:01 +000027class SMDiagnostic;
Chris Lattner08113472009-12-29 09:01:33 +000028template <typename T> class SmallVectorImpl;
Dan Gohman82a47e92009-10-06 17:43:57 +000029
Owen Anderson52170072009-06-30 17:06:46 +000030/// This is an important class for using LLVM in a threaded context. It
31/// (opaquely) owns and manages the core "global" data of LLVM's core
32/// infrastructure, including the type and constant uniquing tables.
33/// LLVMContext itself provides no locking guarantees, so you should be careful
34/// to have one context per thread.
Benjamin Kramer12ddd402009-08-11 17:45:13 +000035class LLVMContext {
Owen Anderson1e8d5d22010-09-08 18:22:11 +000036public:
37 LLVMContextImpl *const pImpl;
38 LLVMContext();
39 ~LLVMContext();
40
Chris Lattnerec39f092010-03-30 23:03:27 +000041 // Pinned metadata names, which always have the same value. This is a
42 // compile-time performance optimization, not a correctness optimization.
43 enum {
Dan Gohmanb2143b62010-09-14 21:25:10 +000044 MD_dbg = 0, // "dbg"
Jakub Staszak9da99342011-07-06 18:22:43 +000045 MD_tbaa = 1, // "tbaa"
Peter Collingbourne999f90b2011-10-27 19:19:14 +000046 MD_prof = 2, // "prof"
Duncan Sands5e5c5f82012-04-14 12:36:06 +000047 MD_fpmath = 3, // "fpmath"
Dan Gohmanb54834b2012-09-13 17:56:17 +000048 MD_range = 4, // "range"
49 MD_tbaa_struct = 5 // "tbaa.struct"
Chris Lattnerec39f092010-03-30 23:03:27 +000050 };
51
Chris Lattner08113472009-12-29 09:01:33 +000052 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
53 /// This ID is uniqued across modules in the current LLVMContext.
54 unsigned getMDKindID(StringRef Name) const;
55
56 /// getMDKindNames - Populate client supplied SmallVector with the name for
Dan Gohman5d809112010-07-20 21:45:17 +000057 /// custom metadata IDs registered in this LLVMContext.
Chris Lattner08113472009-12-29 09:01:33 +000058 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
Chris Lattner42a4ee02010-04-06 00:44:45 +000059
Chris Lattner4afa1282010-11-17 08:13:01 +000060
61 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
62 unsigned LocCookie);
63
Chris Lattner42a4ee02010-04-06 00:44:45 +000064 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
65 /// when problems with inline asm are detected by the backend. The first
Chris Lattner4afa1282010-11-17 08:13:01 +000066 /// argument is a function pointer and the second is a context pointer that
67 /// gets passed into the DiagHandler.
Chris Lattner42a4ee02010-04-06 00:44:45 +000068 ///
Chris Lattner4afa1282010-11-17 08:13:01 +000069 /// LLVMContext doesn't take ownership or interpret either of these
Chris Lattner42a4ee02010-04-06 00:44:45 +000070 /// pointers.
Chris Lattner4afa1282010-11-17 08:13:01 +000071 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
72 void *DiagContext = 0);
Chris Lattner42a4ee02010-04-06 00:44:45 +000073
74 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
75 /// setInlineAsmDiagnosticHandler.
Chris Lattner4afa1282010-11-17 08:13:01 +000076 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
Chris Lattner42a4ee02010-04-06 00:44:45 +000077
78 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
79 /// setInlineAsmDiagnosticHandler.
80 void *getInlineAsmDiagnosticContext() const;
81
Chris Lattner38686bd2010-04-07 23:40:44 +000082
83 /// emitError - Emit an error message to the currently installed error handler
84 /// with optional location information. This function returns, so code should
85 /// be prepared to drop the erroneous construct on the floor and "not crash".
86 /// The generated code need not be correct. The error message will be
87 /// implicitly prefixed with "error: " and should not end with a ".".
Chris Lattner3a4c60c2012-01-03 23:47:05 +000088 void emitError(unsigned LocCookie, const Twine &ErrorStr);
89 void emitError(const Instruction *I, const Twine &ErrorStr);
90 void emitError(const Twine &ErrorStr);
Owen Andersoneb923302010-09-08 18:41:07 +000091
92private:
Craig Topper9f9ce612012-09-17 07:16:40 +000093 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
94 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
Owen Andersoneb923302010-09-08 18:41:07 +000095
96 /// addModule - Register a module as being instantiated in this context. If
97 /// the context is deleted, the module will be deleted as well.
98 void addModule(Module*);
99
100 /// removeModule - Unregister a module from this context.
101 void removeModule(Module*);
102
103 // Module needs access to the add/removeModule methods.
104 friend class Module;
Owen Anderson2bc29dc2009-06-30 00:48:55 +0000105};
106
Chris Lattner08113472009-12-29 09:01:33 +0000107/// getGlobalContext - Returns a global context. This is for LLVM clients that
108/// only care about operating on a single thread.
109extern LLVMContext &getGlobalContext();
Owen Andersondb47ed02009-06-30 23:39:59 +0000110
Owen Anderson2bc29dc2009-06-30 00:48:55 +0000111}
112
113#endif