From: Robert Bradshaw Date: Tue, 7 Oct 2008 22:02:54 +0000 (-0700) Subject: lists as literal structs X-Git-Tag: 0.9.9.2.beta~65 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5361eb62fbfc6dc80f23bcd91cf629f8fd2cdb03;p=cython.git lists as literal structs --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 7585e1ef..9ea1325b 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -2680,6 +2680,15 @@ class ListNode(SequenceNode): for i in range(len(self.args)): arg = self.args[i] self.args[i] = arg.coerce_to(base_type, env) + elif dst_type.is_struct: + if len(self.args) > len(dst_type.scope.var_entries): + error(self.pos, "Too may members for '%s'" % dst_type) + else: + if len(self.args) < len(dst_type.scope.var_entries): + warning(self.pos, "Too few members for '%s'" % dst_type, 1) + for i, (arg, member) in enumerate(zip(self.args, dst_type.scope.var_entries)): + self.args[i] = arg.coerce_to(member.type, env) + self.type = dst_type else: self.type = error_type error(self.pos, "Cannot coerce list to type '%s'" % dst_type) @@ -2703,13 +2712,19 @@ class ListNode(SequenceNode): (self.result(), i, arg.py_result())) - else: + elif self.type.is_ptr: code.putln("%s = (%s[]) {" % (self.result(), self.type.base_type)) - for i, arg in enumerate(self.args): + for arg in self.args: code.put(arg.result()) code.put(", ") code.putln(); code.putln("};") + else: + for arg, member in zip(self.args, self.type.scope.var_entries): + code.putln("%s.%s = %s;" % ( + self.result(), + member.cname, + arg.result())) def generate_subexpr_disposal_code(self, code): # We call generate_post_assignment_code here instead diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 3caa102d..14333d2f 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -37,6 +37,7 @@ class PyrexType(BaseType): # is_null_ptr boolean Is the type of NULL # is_cfunction boolean Is a C function type # is_struct_or_union boolean Is a C struct or union type + # is_struct boolean Is a C struct type # is_enum boolean Is a C enum type # is_typedef boolean Is a typedef type # is_string boolean Is a C char * type @@ -88,6 +89,7 @@ class PyrexType(BaseType): is_null_ptr = 0 is_cfunction = 0 is_struct_or_union = 0 + is_struct = 0 is_enum = 0 is_typedef = 0 is_string = 0 @@ -929,6 +931,7 @@ class CStructOrUnionType(CType): self.kind = kind self.scope = scope self.typedef_flag = typedef_flag + self.is_struct = kind == 'struct' def __repr__(self): return "" % (self.name, self.cname,