changelog shortlog tags browse all files raw

changeset: lists as literal structs

changeset 1213: 302abc2245aa
parent 1212:c086038b5c07
child 1214:fd38efcd7236
author: Robert Bradshaw <robertwb@math.washington.edu>
date: Tue Oct 07 15:02:54 2008 -0700 (3 months ago)
files: Cython/Compiler/ExprNodes.py Cython/Compiler/PyrexTypes.py
description: lists as literal structs
--- a/Cython/Compiler/ExprNodes.py	Tue Oct 07 14:20:09 2008 -0700
+++ b/Cython/Compiler/ExprNodes.py	Tue Oct 07 15:02:54 2008 -0700
@@ -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
--- a/Cython/Compiler/PyrexTypes.py	Tue Oct 07 14:20:09 2008 -0700
+++ b/Cython/Compiler/PyrexTypes.py	Tue Oct 07 15:02:54 2008 -0700
@@ -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 "<CStructOrUnionType %s %s%s>" % (self.name, self.cname,