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