Add custom binding to SmallVector<Type*>

This commit is contained in:
Siu Kwan Lam 2013-01-22 21:23:20 -06:00
commit 6b88ffbaed
5 changed files with 51 additions and 8 deletions

View file

@ -1,5 +1,7 @@
PYTHON = python
PYMODS = raw_ostream Type DerivedTypes LLVMContext StringRef AssemblyAnnotationWriter Module
PYMODS = raw_ostream SmallVector Type DerivedTypes StringRef
PYMODS += LLVMContext AssemblyAnnotationWriter Module
all: _api.so _capsule.so

View file

@ -190,6 +190,16 @@ Bool = BuiltinType('bool')
Unsigned = BuiltinType('unsigned')
ConstStdString = BuiltinType('const std::string')
class PyObjectImpl(object):
name = 'PyObject*'
fullname = name
format = 'O'
def unwrap(self, println, var):
return var
PyObject = PyObjectImpl()
class Unwrapper(object):
def __init__(self, cls, pytype):
self.cls = cls

View file

@ -4,6 +4,8 @@
namespace llvm{
class Type; //forward declaration
class raw_svector_ostream_helper: public raw_svector_ostream {
SmallVectorImpl<char> *SV;
public:
@ -30,5 +32,25 @@ private:
void operator = (const raw_svector_ostream_helper&);
};
}
class SmallVector_Type : public SmallVector<Type*, 8> {
public:
static
SmallVector_Type* fromPySequence(PyObject* obj) {
SmallVector_Type* SV = new SmallVector_Type;
Py_ssize_t sz = PySequence_Size(obj);
for (Py_ssize_t i = 0; i < sz; ++i) {
PyObject* item = PySequence_GetItem(obj, i);
PyObject* cap = PyObject_GetAttrString(item, "_ptr");
Type* type = (Type*)PyCapsule_GetPointer(cap, "llvm::Type");
SV->push_back(type);
Py_XDECREF(cap);
Py_XDECREF(item);
}
return SV;
}
};
} // end namespace llvm

View file

@ -2,14 +2,17 @@ from binding import *
from namespace import llvm
from LLVMContext import LLVMContext
from Type import Type
from SmallVector import SmallVector_Type
FunctionType = Type.Subclass()
FunctionType.include.add('llvm/DerivedTypes.h')
get = FunctionType.staticmultimethod([FunctionType.Pointer,
Type.Pointer, Bool.From(bool)],)
# (FunctionType.Pointer,
# Type.Pointer, )])
_get_signatures = [(FunctionType.Pointer,
Type.Pointer, Bool.From(bool)),
(FunctionType.Pointer,
Type.Pointer, SmallVector_Type.Ref, Bool.From(bool))]
get = FunctionType.staticmultimethod(*_get_signatures)
isVarArg = FunctionType.method(Bool.To(bool))
getReturnType = FunctionType.method(Type.Pointer)

View file

@ -1,5 +1,5 @@
import api
#api.capsule.set_debug(True)
api.capsule.set_debug(True)
context = api.getGlobalContext()
@ -14,7 +14,6 @@ assert m.getPointerSize() == api.Module.PointerSize.AnyPointerSize
m.dump()
os = api.raw_svector_ostream_helper.create()
m.print_(os, None)
print os.str()
@ -26,6 +25,13 @@ int1ty.dump()
print int1ty.isIntegerTy(1)
fnty = api.FunctionType.get(int1ty, False)
fnty.dump()
print
types = [int1ty, api.Type.getIntNTy(context, 21)]
sv = api.SmallVector_Type.fromPySequence(types)
fnty = api.FunctionType.get(int1ty, sv, False)
os2 = api.raw_svector_ostream_helper.create()
fnty.print_(os2)