[Nice-commit] Nice/src/bossa/syntax TupleExp.java,1.21,1.22 PrimitiveType.java,1.9,1.10 LiteralArray
Brought to you by:
bonniot
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5377/src/bossa/syntax Modified Files: TupleExp.java PrimitiveType.java LiteralArrayExp.java Expression.java ClassDefinition.java CallExp.java Log Message: Use more typing information from the context when deciding what bytecode types to use for literal arrays and tuples, especially when they are nested inside other literal arrays or tuples. This allows to generate more efficient code, and fixes several runtime errors (fixes #761629 and #892625). Index: TupleExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/TupleExp.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** TupleExp.java 24 Oct 2003 10:35:50 -0000 1.21 --- TupleExp.java 11 Feb 2004 12:46:39 -0000 1.22 *************** *** 70,82 **** expectedType.simplify(); ! Monotype m = expectedType.getMonotype(); ! // get rid of the nullness part ! m = Types.rawType((MonotypeConstructor) m); // Get the expected component types if (m instanceof TupleType) ! expectedComponents = ((TupleType) m).getComponents(); ! return this; } --- 70,92 ---- expectedType.simplify(); ! adjustToExpectedType(expectedType.getMonotype()); ! ! return this; ! } ! ! void adjustToExpectedType(Monotype expectedType) ! { ! Monotype m = Types.equivalent(expectedType); // Get the expected component types if (m instanceof TupleType) ! { ! expectedComponents = ((TupleType) m).getComponents(); ! // Do the same for the elements of the tuple, since they might be ! // tuples themselves, literal arrays, ... ! bossa.syntax.Expression.adjustToExpectedType ! (expressions, expectedComponents); ! } } *************** *** 122,126 **** return nice.tools.code.TupleType.createExp (Types.lowestCommonSupertype(expectedComponents), ! Types.javaType(components), bossa.syntax.Expression.compile(expressions)); } --- 132,136 ---- return nice.tools.code.TupleType.createExp (Types.lowestCommonSupertype(expectedComponents), ! Types.javaType(expectedComponents), bossa.syntax.Expression.compile(expressions)); } Index: PrimitiveType.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/PrimitiveType.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** PrimitiveType.java 8 Dec 2003 19:42:37 -0000 1.9 --- PrimitiveType.java 11 Feb 2004 12:46:39 -0000 1.10 *************** *** 188,191 **** --- 188,192 ---- static TypeConstructor typeTC; + static TypeConstructor collectionTC; static TypeConstructor throwableTC; static TypeConstructor throwableTC() *************** *** 193,197 **** return throwableTC; } ! private static Polytype throwableType; static Polytype throwableType() --- 194,198 ---- return throwableTC; } ! private static Polytype throwableType; static Polytype throwableType() Index: LiteralArrayExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/LiteralArrayExp.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** LiteralArrayExp.java 8 Dec 2003 19:42:37 -0000 1.16 --- LiteralArrayExp.java 11 Feb 2004 12:46:39 -0000 1.17 *************** *** 21,24 **** --- 21,26 ---- import mlsub.typing.MonotypeConstructor; import mlsub.typing.Polytype; + import mlsub.typing.TypeConstructor; + import nice.tools.code.Types; /** *************** *** 49,54 **** if (! type.trySimplify()) type = array(PrimitiveType.objectPolytype()); - - nice.tools.code.Types.setBytecodeType(type); } --- 51,54 ---- *************** *** 63,66 **** --- 63,111 ---- } + /** + Adjust the array type according to the context. + + This is usefull because arrays are non-variant. + For instance, different code must be generated + for [ 1, 2 ] in the contexts: + List<int[]> i = [[ 1, 2 ]] + and + List<List<byte[]>> b = [[ 1, 2 ]] + */ + bossa.syntax.Expression resolveOverloading(Polytype expectedType) + { + Monotype elementType = Types.getTypeParameter(expectedType, 0); + + if (elementType != null) + for (int i = 0; i < elements.length; i++) + elements[i].adjustToExpectedType(elementType); + + return this; + } + + void adjustToExpectedType(Monotype expectedType) + { + TypeConstructor tc = Types.equivalent(expectedType).head(); + + // Remember that we will need to wrap the array to make it a collection. + // This cannot be found easily during code generation for nested arrays + // since the bytecode type of both List<List<T>> and List<T[]> is + // simply List. + if (tc != PrimitiveType.arrayTC && + tc != null && tc.isRigid() && + mlsub.typing.Typing.testRigidLeq(tc, PrimitiveType.collectionTC)) + { + wrapAsCollection = true; + } + + // Adjust nested elements. + Monotype elementType = Types.getTypeParameter(expectedType, 0); + if (elementType != null) + for (int i = 0; i < elements.length; i++) + elements[i].adjustToExpectedType(elementType); + } + + private boolean wrapAsCollection; + /**************************************************************** * Code generation *************** *** 69,78 **** public gnu.expr.Expression compile() { ! Type t = nice.tools.code.Types.javaType(getType()); ! return new gnu.expr.ApplyExp ! (new nice.tools.code.LiteralArrayProc((ArrayType) t, ! elements.length), ! Expression.compile(elements)); } --- 114,124 ---- public gnu.expr.Expression compile() { ! gnu.expr.Expression[] args = Expression.compile(elements); ! ArrayType t = (ArrayType) nice.tools.code.SpecialTypes.array(Types.lowestUpperBound(args)); ! return new gnu.expr.ApplyExp ! (new nice.tools.code.LiteralArrayProc ! (t, elements.length, wrapAsCollection), ! args); } Index: Expression.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Expression.java,v retrieving revision 1.53 retrieving revision 1.54 diff -C2 -d -r1.53 -r1.54 *** Expression.java 9 Dec 2003 15:21:05 -0000 1.53 --- Expression.java 11 Feb 2004 12:46:39 -0000 1.54 *************** *** 17,20 **** --- 17,21 ---- import mlsub.typing.Polytype; + import mlsub.typing.Monotype; /** *************** *** 131,135 **** return this; } ! /** computes the static type of the expression */ abstract void computeType(); --- 132,156 ---- return this; } ! ! /** ! Called with the type that this expression is expected to have. ! This information can be used to generate better code. ! */ ! void adjustToExpectedType(Monotype expectedType) ! { ! // Default: do nothing. ! } ! ! static void adjustToExpectedType (Expression[] expressions, Monotype[] types) ! { ! // The domain of a function can be null in rare circumstances, like ! // for a function of type <T> T ! if (types == null) ! return; ! ! for (int i = 0; i < types.length; i++) ! expressions[i].adjustToExpectedType(types[i]); ! } ! /** computes the static type of the expression */ abstract void computeType(); Index: ClassDefinition.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/ClassDefinition.java,v retrieving revision 1.102 retrieving revision 1.103 diff -C2 -d -r1.102 -r1.103 *** ClassDefinition.java 19 Dec 2003 18:08:33 -0000 1.102 --- ClassDefinition.java 11 Feb 2004 12:46:39 -0000 1.103 *************** *** 427,430 **** --- 427,432 ---- if (name.equals("nice.lang.Throwable")) PrimitiveType.throwableTC = tc; + else if (name.equals("nice.lang.Collection")) + PrimitiveType.collectionTC = tc; } Index: CallExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CallExp.java,v retrieving revision 1.89 retrieving revision 1.90 diff -C2 -d -r1.89 -r1.90 *** CallExp.java 3 Feb 2004 01:28:10 -0000 1.89 --- CallExp.java 11 Feb 2004 12:46:39 -0000 1.90 *************** *** 249,252 **** --- 249,259 ---- function.checkSpecialRequirements(arguments.computedExpressions); + + // make sure computedExpressions is valid. + getType(); + // Allow expressions to know in what context they are used. + // Important for litteral arrays and tuples. + Expression.adjustToExpectedType(arguments.computedExpressions, + Types.domain(function.getType())); } |