From 4540da0cd806e184e694fa315299cacc04628e3b Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Thu, 17 Oct 2013 17:07:46 +0200 Subject: Support partial unions in a way very similar to partial structs. Needed for a python-cffi mail. --- cffi/cparser.py | 2 +- cffi/model.py | 19 +++++++------------ cffi/vengine_cpy.py | 2 +- cffi/vengine_gen.py | 2 +- 4 files changed, 10 insertions(+), 15 deletions(-) (limited to 'cffi') diff --git a/cffi/cparser.py b/cffi/cparser.py index 6c248b3..1048934 100644 --- a/cffi/cparser.py +++ b/cffi/cparser.py @@ -488,7 +488,7 @@ class Parser(object): return tp def _make_partial(self, tp, nested): - if not isinstance(tp, model.StructType): + if not isinstance(tp, model.StructOrUnion): raise api.CDefError("%s cannot be partial" % (tp,)) if not tp.has_c_name() and not nested: raise NotImplementedError("%s is partial but has no C name" %(tp,)) diff --git a/cffi/model.py b/cffi/model.py index 2a9373f..2e989b3 100644 --- a/cffi/model.py +++ b/cffi/model.py @@ -248,6 +248,7 @@ class StructOrUnionOrEnum(BaseTypeByIdentity): class StructOrUnion(StructOrUnionOrEnum): fixedlayout = None completed = False + partial = False def __init__(self, name, fldnames, fldtypes, fldbitsize): self.name = name @@ -344,11 +345,6 @@ class StructOrUnion(StructOrUnionOrEnum): from .ffiplatform import VerificationError raise VerificationError(msg) - -class StructType(StructOrUnion): - kind = 'struct' - partial = False - def check_not_partial(self): if self.partial and self.fixedlayout is None: from . import ffiplatform @@ -357,19 +353,18 @@ class StructType(StructOrUnion): def build_backend_type(self, ffi, finishlist): self.check_not_partial() finishlist.append(self) - - return global_cache(self, ffi, 'new_struct_type', + # + return global_cache(self, ffi, 'new_%s_type' % self.kind, self.get_official_name(), key=self) +class StructType(StructOrUnion): + kind = 'struct' + + class UnionType(StructOrUnion): kind = 'union' - def build_backend_type(self, ffi, finishlist): - finishlist.append(self) - return global_cache(self, ffi, 'new_union_type', - self.get_official_name(), key=self) - class EnumType(StructOrUnionOrEnum): kind = 'enum' diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py index e8f3575..bcd0e06 100644 --- a/cffi/vengine_cpy.py +++ b/cffi/vengine_cpy.py @@ -491,7 +491,7 @@ class VCPythonEngine(object): # function = getattr(module, layoutfuncname) layout = function() - if isinstance(tp, model.StructType) and tp.partial: + if isinstance(tp, model.StructOrUnion) and tp.partial: # use the function()'s sizes and offsets to guide the # layout of the struct totalsize = layout[0] diff --git a/cffi/vengine_gen.py b/cffi/vengine_gen.py index dc38bb7..24cce71 100644 --- a/cffi/vengine_gen.py +++ b/cffi/vengine_gen.py @@ -282,7 +282,7 @@ class VGenericEngine(object): if x < 0: break layout.append(x) num += 1 - if isinstance(tp, model.StructType) and tp.partial: + if isinstance(tp, model.StructOrUnion) and tp.partial: # use the function()'s sizes and offsets to guide the # layout of the struct totalsize = layout[0] -- cgit v1.2.1