The 'target-independent' ISD::CALL isn't. Nuke it, making way for Nate's
[oota-llvm.git] / include / llvm / System / DynamicLibrary.h
1 //===-- llvm/System/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the sys::DynamicLibrary class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_DYNAMIC_LIBRARY_H
15 #define LLVM_SYSTEM_DYNAMIC_LIBRARY_H
16
17 #include "llvm/System/Path.h"
18 #include <string>
19
20 namespace llvm {
21 namespace sys {
22
23   /// This class provides a portable interface to dynamic libraries which also
24   /// might be known as shared libraries, shared objects, dynamic shared
25   /// objects, or dynamic link libraries. Regardless of the terminology or the
26   /// operating system interface, this class provides a portable interface that
27   /// allows dynamic libraries to be loaded and and searched for externally
28   /// defined symbols. This is typically used to provide "plug-in" support.
29   /// @since 1.4
30   /// @brief Portable dynamic library abstraction.
31   class DynamicLibrary {
32     /// @name Constructors
33     /// @{
34     public:
35       /// Construct a DynamicLibrary that represents the currently executing
36       /// program. The program must have been linked with -export-dynamic or
37       /// -dlopen self for this to work. Any symbols retrieved with the
38       /// GetAddressOfSymbol function will refer to the program not to any
39       /// library.
40       /// @throws std::string indicating why the program couldn't be opened.
41       /// @brief Open program as dynamic library.
42       DynamicLibrary();
43
44       /// This is the constructor for DynamicLibrary instances. It will open
45       /// the dynamic library specified by the \filename Path.
46       /// @throws std::string indicating why the library couldn't be opened.
47       /// @brief Open a dynamic library.
48       DynamicLibrary(const char* filename);
49
50       /// After destruction, the symbols of the library will no longer be
51       /// available to the program. It is important to make sure the lifespan
52       /// of a DynamicLibrary exceeds the lifetime of the pointers returned
53       /// by the GetAddressOfSymbol otherwise the program may walk off into
54       /// uncharted territory.
55       /// @see GetAddressOfSymbol.
56       /// @brief Closes the DynamicLibrary
57       ~DynamicLibrary();
58
59     /// @}
60     /// @name Functions
61     /// @{
62     public:
63       /// This function allows a library to be loaded without instantiating a
64       /// DynamicLibrary object. Consequently, it is marked as being permanent
65       /// and will only be unloaded when the program terminates.
66       /// @throws std::string on error.
67       /// @brief Open a dynamic library permanently.
68       static void LoadLibraryPermanently(const char* filename);
69
70       /// This function will search through all previously loaded dynamic
71       /// libraries for the symbol \p symbolName. If it is found, the addressof
72       /// that symbol is returned. If not, null is returned. Note that this will
73       /// search permanently loaded libraries (LoadLibraryPermanently) as well
74       /// as ephemerally loaded libraries (constructors).
75       /// @throws std::string on error.
76       /// @brief Search through libraries for address of a symbol
77       static void* SearchForAddressOfSymbol(const char* symbolName);
78
79       /// @brief Convenience function for C++ophiles.
80       static void* SearchForAddressOfSymbol(const std::string& symbolName) {
81         return SearchForAddressOfSymbol(symbolName.c_str());
82       }
83
84     /// @}
85     /// @name Accessors
86     /// @{
87     public:
88       /// Looks up a \p symbolName in the DynamicLibrary and returns its address
89       /// if it exists. If the symbol does not exist, returns (void*)0.
90       /// @returns the address of the symbol or 0.
91       /// @brief Get the address of a symbol in the DynamicLibrary.
92       void* GetAddressOfSymbol(const char* symbolName);
93
94       /// @brief Convenience function for C++ophiles.
95       void* GetAddressOfSymbol(const std::string& symbolName) {
96         return GetAddressOfSymbol(symbolName.c_str());
97       }
98
99     /// @}
100     /// @name Implementation
101     /// @{
102     protected:
103       void* handle;  // Opaque handle for information about the library
104
105       DynamicLibrary(const DynamicLibrary&); ///< Do not implement
106       DynamicLibrary& operator=(const DynamicLibrary&); ///< Do not implement
107     /// @}
108   };
109
110 } // End sys namespace
111 } // End llvm namespace
112
113 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H