Add opaque StructType;

Remove test/typehandle.py;
Add test/opaque.py to demonstrate recursive type.
This commit is contained in:
Siu Kwan Lam 2012-07-22 15:14:52 -07:00
commit c9902ab8cf
9 changed files with 171 additions and 80 deletions

View file

@ -313,6 +313,8 @@ _wLLVMGetStructElementTypes(PyObject *self, PyObject *args)
_wrap_obj2obj(LLVMIsPackedStruct, LLVMTypeRef, int)
_wrap_obj2obj(LLVMIsOpaqueStruct, LLVMTypeRef, int)
_wrap_obj2obj(LLVMIsLiteralStruct, LLVMTypeRef, int)
/*===-- Array types ------------------------------------------------------===*/
@ -1379,6 +1381,8 @@ static PyMethodDef core_methods[] = {
_method( LLVMCountStructElementTypes )
_method( LLVMGetStructElementTypes )
_method( LLVMIsPackedStruct )
_method( LLVMIsOpaqueStruct )
_method( LLVMIsLiteralStruct )
_method( LLVMGetStructName )
_method( LLVMSetStructName )

View file

@ -507,13 +507,13 @@ class Module(llvm.Ownable, llvm.Cacheable):
def add_library(self, name):
return _core.LLVMModuleAddLibrary(self.ptr, name)
def _get_id(self):
return _core.LLVMGetModuleIdentifier(self.ptr)
def _set_id(self, string):
_core.LLVMSetModuleIdentifier(self.ptr, string)
id = property(_get_id, _set_id)
#===----------------------------------------------------------------------===
@ -584,6 +584,15 @@ class Type(object):
return _make_type(_core.LLVMFunctionType(return_ty.ptr, params,
var_arg), TYPE_FUNCTION)
@staticmethod
def opaque(name):
"""Create a opaque StructType"""
if not name:
raise llvm.LLVMException("Must specify a name")
objptr = _core.LLVMStructTypeIdentified(name)
return _make_type(objptr, TYPE_STRUCT)
@staticmethod
def struct(element_tys, name=''): # not packed
"""Create a (unpacked) structure type.
@ -591,17 +600,17 @@ class Type(object):
Creates a structure type with elements of types as given in the
iterable `element_tys'. This method creates a unpacked
structure. For a packed one, use the packed_struct() method.
If name is not '', creates a identified type;
If name is not '', creates a identified type;
otherwise, creates a literal type."""
elems = unpack_types(element_tys)
if name: # create Identified StructType
objptr = _core.LLVMStructTypeIdentified(name)
_core.LLVMSetStructBody(objptr, elems, 0)
else: # create Literal StructType
objptr = _core.LLVMStructType(elems, 0)
return _make_type(objptr, TYPE_STRUCT)
@staticmethod
@ -611,11 +620,11 @@ class Type(object):
Creates a structure type with elements of types as given in the
iterable `element_tys'. This method creates a packed
structure. For an unpacked one, use the struct() method.
If name is not '', creates a identified type;
If name is not '', creates a identified type;
otherwise, creates a literal type."""
elems = unpack_types(element_tys)
if name: # create Identified StructType
objptr = _core.LLVMStructTypeIdentified(name)
_core.LLVMSetStructBody(objptr, elems, 0)
@ -750,6 +759,23 @@ class StructType(Type):
pp = _core.LLVMGetStructElementTypes(self.ptr)
return [ _make_type(p, _core.LLVMGetTypeKind(p)) for p in pp ]
def set_body(self, elems, packed=False):
"""Filled the body of a opaque type.
"""
# check
if not self.is_opaque:
raise llvm.LLVMException("Body is already defined.")
# prepare arguments
elems = unpack_types(elems)
if packed:
packed = 1
else:
packed = 0
# call c API
_core.LLVMSetStructBody(self.ptr, elems, packed)
@property
def packed(self):
"""True if the structure is packed, False otherwise."""
@ -757,12 +783,24 @@ class StructType(Type):
def _set_name(self, name):
_core.LLVMSetStructName(self.ptr, name)
def _get_name(self):
return _core.LLVMGetStructName(self.ptr)
name = property(_get_name, _set_name)
@property
def is_literal(self):
return bool(_core.LLVMIsLiteralStruct(self.ptr))
@property
def is_identified(self):
return not _core.LLVMIsLiteralStruct(self.ptr)
@property
def is_opaque(self):
return bool(_core.LLVMIsOpaqueStruct(self.ptr))
class ArrayType(Type):
"""Represents an array type."""

View file

@ -90,6 +90,11 @@ char *do_print(W obj)
return strdup(buf.str().c_str());
}
int LLVMIsLiteralStruct(LLVMTypeRef type)
{
return llvm::unwrap<llvm::StructType>(type)->isLiteral();
}
LLVMTypeRef LLVMStructTypeIdentified(const char * name)
{
using namespace llvm;

View file

@ -41,6 +41,11 @@
extern "C" {
#endif
/*
* Wraps StructType::isLiteral()
*/
int LLVMIsLiteralStruct(LLVMTypeRef type);
/*
* Wraps StructType::create()
*/