e0f35203bac5b4eceed5dbd14ebc9f15959dd753
[oota-llvm.git] / bindings / python / llvm / core.py
1 #===- core.py - Python LLVM Bindings -------------------------*- python -*--===#
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 from .common import LLVMObject
11 from .common import c_object_p
12 from .common import get_library
13
14 from ctypes import POINTER
15 from ctypes import byref
16 from ctypes import c_char_p
17
18 __all__ = [
19     "lib",
20     "MemoryBuffer",
21 ]
22
23 lib = get_library()
24
25 class MemoryBuffer(LLVMObject):
26     """Represents an opaque memory buffer."""
27
28     def __init__(self, filename=None):
29         """Create a new memory buffer.
30
31         Currently, we support creating from the contents of a file at the
32         specified filename.
33         """
34         if filename is None:
35             raise Exception("filename argument must be defined")
36
37         memory = c_object_p()
38         out = c_char_p(None)
39
40         result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
41                 byref(memory), byref(out))
42
43         if result:
44             raise Exception("Could not create memory buffer: %s" % out.value)
45
46         LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
47
48 def register_library(library):
49     library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
50             POINTER(c_object_p), POINTER(c_char_p)]
51     library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
52
53     library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
54
55 register_library(lib)