[Python-checkins] python/dist/src/Python newcompile.c,1.1.2.45,1.1.2.46

nascheme@users.sourceforge.net nascheme@users.sourceforge.net
Sat, 29 Mar 2003 07:47:57 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv7212/Python

Modified Files:
      Tag: ast-branch
	newcompile.c 
Log Message:
Implement code generation for class definitions.  Set code flags from
compiler context when building code objects.


Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.45
retrieving revision 1.1.2.46
diff -C2 -d -r1.1.2.45 -r1.1.2.46
*** newcompile.c	28 Mar 2003 19:19:40 -0000	1.1.2.45
--- newcompile.c	29 Mar 2003 15:47:55 -0000	1.1.2.46
***************
*** 692,695 ****
--- 692,727 ----
  
  static int
+ compiler_class(struct compiler *c, stmt_ty s)
+ {
+ 	int n;
+ 	PyCodeObject *co;
+ 	/* push class name on stack, needed by BUILD_CLASS */
+ 	ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
+ 	/* push the tuple of base classes on the stack */
+ 	n = asdl_seq_LEN(s->v.ClassDef.bases);
+ 	if (n > 0)
+ 		VISIT_SEQ(c, expr, s->v.ClassDef.bases);
+ 	ADDOP_I(c, BUILD_TUPLE, n);
+ 	if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s))
+ 		return 0;
+ 	VISIT_SEQ(c, stmt, s->v.ClassDef.body);
+ 	ADDOP(c, LOAD_LOCALS);
+ 	ADDOP(c, RETURN_VALUE);
+ 	co = assemble(c);
+ 	if (co == NULL)
+ 		return 0;
+ 	compiler_exit_scope(c);
+ 
+ 	/* XXX closure */
+ 	ADDOP_O(c, LOAD_CONST, (PyObject *)co, consts);
+ 	ADDOP_I(c, MAKE_FUNCTION, 0);
+ 	ADDOP_I(c, CALL_FUNCTION, 0);
+ 	ADDOP(c, BUILD_CLASS);
+ 	if (!compiler_nameop(c, s->v.ClassDef.name, Store))
+ 		return 0;
+ 	return 1;
+ }
+ 
+ static int
  compiler_lambda(struct compiler *c, expr_ty e)
  {
***************
*** 1005,1011 ****
          case FunctionDef_kind:
  		return compiler_function(c, s);
- 		break;
          case ClassDef_kind:
! 		break;
          case Return_kind:
  		if (s->v.Return.value)
--- 1037,1042 ----
          case FunctionDef_kind:
  		return compiler_function(c, s);
          case ClassDef_kind:
! 		return compiler_class(c, s);
          case Return_kind:
  		if (s->v.Return.value)
***************
*** 1983,1986 ****
--- 2014,2035 ----
  }
  
+ static int
+ compute_code_flags(struct compiler *c)
+ {
+ 	PySTEntryObject *ste = c->u->u_ste;
+ 	int flags = 0;
+ 	if (ste->ste_type != ModuleBlock)
+ 		flags |= CO_NEWLOCALS;
+ 	if (ste->ste_type == FunctionBlock) {
+ 		if (ste->ste_optimized)
+ 			flags |= CO_OPTIMIZED;
+ 		if (ste->ste_nested)
+ 			flags |= CO_NESTED;
+ 		if (ste->ste_generator)
+ 			flags |= CO_GENERATOR;
+ 	}
+ 	return flags;
+ }
+ 
  static PyCodeObject *
  makecode(struct compiler *c, struct assembler *a)
***************
*** 2009,2013 ****
  
  	nlocals = PyList_GET_SIZE(c->u->u_varnames);
! 	co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), 0,
  			a->a_bytecode, consts, names, varnames,
  			nil, nil,
--- 2058,2063 ----
  
  	nlocals = PyList_GET_SIZE(c->u->u_varnames);
! 	co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c),
! 			compute_code_flags(c),
  			a->a_bytecode, consts, names, varnames,
  			nil, nil,