[Nice-commit] Nice/src/bossa/syntax typecheck.nice,1.104,1.105 tools.nice,1.38,1.39 analyse.nice,1.1
Brought to you by:
bonniot
From: Daniel B. <bo...@us...> - 2004-07-30 19:08:56
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6432/src/bossa/syntax Modified Files: typecheck.nice tools.nice analyse.nice TypeIdent.java TypeConstantExp.java PrimitiveType.java Monotype.java ConstantExp.java ClassDefinition.java Log Message: java.lang.Class is now parameterized by the type it represents. Index: analyse.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/analyse.nice,v retrieving revision 1.106 retrieving revision 1.107 diff -C2 -d -r1.106 -r1.107 *** analyse.nice 29 Jul 2004 12:11:40 -0000 1.106 --- analyse.nice 30 Jul 2004 19:08:45 -0000 1.107 *************** *** 613,633 **** } ! analyse(TypeConstantExp e, info) { ! LocatedString name = cast(e.value); ! ! ?gnu.bytecode.Type type = nice.tools.code.Types.typeRepresentationToBytecode ! (name.toString(), name.location()); ! ! if (type == null) { ! if (info.lookupType(name) instanceof MonotypeVar) ! throw new bossa.util.UserError(name, "A type variable cannot be used here"); else ! throw new bossa.util.UserError(name, "Class " + name + " is not declared"); } ! e.value = type; ! e.representedType = bossa.syntax.Node.getGlobalTypeScope(). ! globalLookup(name.toString(), name.location()); return e; --- 613,661 ---- } ! mlsub.typing.Polytype typeRepresentationToPolytype(String name, ! ?bossa.util.Location loc, ! Info info, ! boolean sure = true) { ! if (name.charAt(0) == '[') { ! mlsub.typing.Polytype res = ! typeRepresentationToPolytype(name.substring(1), loc, info, sure: false); ! ! let type = new mlsub.typing.MonotypeConstructor ! (PrimitiveType.arrayTC, [res.getMonotype()]); ! ! return new mlsub.typing.Polytype ! (res.getConstraint(), ! sure ? Monotype.sure(type) : Monotype.maybe(type)); ! } ! ! ?TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(name, loc); ! ! if (tc == null) ! { ! if (name.equals("Object") || name.equals("java.lang.Object")) ! return PrimitiveType.objectPolytype(); ! ! if (info.typeVars[name] instanceof MonotypeVar) ! throw new bossa.util.UserError(loc, "A type variable cannot be used here"); else ! throw new bossa.util.UserError(loc, "Class "name" is not declared"); } ! ! return TypeConstantExp.universalPolytype(tc, sure); ! } ! ! analyse(TypeConstantExp e, info) ! { ! LocatedString name = cast(e.value); ! ! mlsub.typing.Polytype type = typeRepresentationToPolytype(name.toString(), name.location(), info); ! ! ?gnu.bytecode.Type bytecodeType = ! nice.tools.code.Types.typeRepresentationToBytecode ! (name.toString(), name.location()); ! ! e.setRepresentedType(type, bytecodeType); return e; *************** *** 892,895 **** // Local Variables: ! // nice-xprogram: "nicec -d \"$HOME/Nice/classes\" --sourcepath=\"$HOME/Nice/src\" --classpath=\"$HOME/Nice/classes\"" // End: --- 920,923 ---- // Local Variables: ! // nice-xprogram: "../bin/nicec -d ../classes --sourcepath=../src" // End: Index: TypeConstantExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/TypeConstantExp.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** TypeConstantExp.java 9 Dec 2003 17:20:15 -0000 1.4 --- TypeConstantExp.java 30 Jul 2004 19:08:45 -0000 1.5 *************** *** 13,16 **** --- 13,21 ---- package bossa.syntax; + import mlsub.typing.Polytype; + import mlsub.typing.MonotypeConstructor; + import mlsub.typing.TypeConstructor; + import mlsub.typing.MonotypeVar; + /** A type used as an expression. *************** *** 20,36 **** */ ! class TypeConstantExp extends ConstantExp { ! TypeConstantExp(LocatedString name) { ! super(PrimitiveType.typeTC, name, name.toString(), name.location()); } ! private TypeConstantExp(LocatedString name, gnu.bytecode.Type type) { ! this(name); ! this.value = type; } /** @return an Expression representing ident as a type or package literal. --- 25,101 ---- */ ! public class TypeConstantExp extends ConstantExp { ! public TypeConstantExp(LocatedString name) { ! super(null, null, name, name.toString(), name.location()); } ! gnu.bytecode.ClassType staticClass() { ! // If this is a '<name>.class' expression, do not consider it as a ! // qualified prefix. ! if (isExpression) ! return null; ! ! return (gnu.bytecode.ClassType) value; ! } ! ! void setRepresentedType(Polytype type, gnu.bytecode.Type bytecodeType) ! { ! this.value = bytecodeType; ! ! this.representedType = type.getMonotype(); ! ! this.type = new Polytype ! (type.getConstraint(), ! Monotype.sure ! (new MonotypeConstructor ! (PrimitiveType.classTC, ! new mlsub.typing.Monotype[]{ type.getMonotype() }))); ! } ! ! protected gnu.expr.Expression compile() ! { ! if (isLiteral) ! return super.compile(); ! ! gnu.bytecode.Type type = (gnu.bytecode.Type) value; ! String representation = type instanceof gnu.bytecode.ArrayType ? ! type.getSignature().replace('/', '.') : ! type.getName(); ! ! return new gnu.expr.ApplyExp ! (forName, ! new gnu.expr.Expression[]{ ! new gnu.expr.QuoteExp(representation)}); ! } ! ! static final gnu.bytecode.Method forName = ! gnu.bytecode.ClassType.make("java.lang.Class").getDeclaredMethod("forName", 1); ! ! TypeConstructor getTC() ! { ! return nice.tools.typing.Types.rawType(representedType).head(); } + mlsub.typing.Monotype representedType; + + public boolean isExpression = false; + public boolean isLiteral = false; + + static Polytype universalPolytype(TypeConstructor tc, boolean sure) + { + MonotypeVar[] vars = MonotypeVar.news(tc.arity()); + mlsub.typing.Monotype type = new MonotypeConstructor(tc, vars); + return new Polytype + (vars == null ? null : new mlsub.typing.Constraint(vars, null), + sure ? Monotype.sure(type) : Monotype.maybe(type)); + } + + /**************************************************************** + * Type literals + ****************************************************************/ + /** @return an Expression representing ident as a type or package literal. *************** *** 40,44 **** return create(null, ident); } ! /** @return an Expression representing [root].[name] --- 105,109 ---- return create(null, ident); } ! /** @return an Expression representing [root].[name] *************** *** 49,54 **** if (root != null) fullName = root.name.append(".").append(fullName).toString(); ! ! mlsub.typing.TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(fullName, name.location()); --- 114,119 ---- if (root != null) fullName = root.name.append(".").append(fullName).toString(); ! ! TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(fullName, name.location()); *************** *** 56,64 **** { gnu.bytecode.Type type = nice.tools.code.Types.javaType(tc); // type might not be a class // for instance if the ident was "int" if (type instanceof gnu.bytecode.ClassType) { ! Expression res = new TypeConstantExp(name, type); res.setLocation(root == null ? name.location() : root.location()); return res; --- 121,131 ---- { gnu.bytecode.Type type = nice.tools.code.Types.javaType(tc); + // type might not be a class // for instance if the ident was "int" if (type instanceof gnu.bytecode.ClassType) { ! TypeConstantExp res = new TypeConstantExp(name); ! res.setRepresentedType(universalPolytype(tc, true), type); res.setLocation(root == null ? name.location() : root.location()); return res; *************** *** 74,83 **** return root; } - - gnu.bytecode.ClassType staticClass() - { - return (gnu.bytecode.ClassType) value; - } - - mlsub.typing.TypeConstructor representedType; } --- 141,143 ---- Index: TypeIdent.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/TypeIdent.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** TypeIdent.java 23 Dec 2003 10:20:40 -0000 1.28 --- TypeIdent.java 30 Jul 2004 19:08:45 -0000 1.29 *************** *** 16,19 **** --- 16,20 ---- import java.util.*; import mlsub.typing.TypeConstructor; + import mlsub.typing.MonotypeConstructor; import mlsub.typing.Interface; import mlsub.typing.MonotypeVar; *************** *** 78,82 **** { TypeSymbol res = resolveToTypeSymbol(scope); ! if (res instanceof mlsub.typing.Monotype) return (mlsub.typing.Monotype) res; --- 79,83 ---- { TypeSymbol res = resolveToTypeSymbol(scope); ! if (res instanceof mlsub.typing.Monotype) return (mlsub.typing.Monotype) res; *************** *** 87,94 **** try{ ! return new mlsub.typing.MonotypeConstructor(tc, null); } catch(mlsub.typing.BadSizeEx e){ ! throw User.error(this, name + Util.has(e.expected, "type parameter", e.actual)); } --- 88,95 ---- try{ ! return nice.tools.typing.Types.zeroArgMonotype(tc); } catch(mlsub.typing.BadSizeEx e){ ! throw User.error(this, name + Util.has(e.expected, "type parameter", e.actual)); } Index: ClassDefinition.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/ClassDefinition.java,v retrieving revision 1.106 retrieving revision 1.107 diff -C2 -d -r1.106 -r1.107 *** ClassDefinition.java 16 Jun 2004 10:00:38 -0000 1.106 --- ClassDefinition.java 30 Jul 2004 19:08:45 -0000 1.107 *************** *** 395,398 **** --- 395,400 ---- else if (name.equals("nice.lang.Collection")) PrimitiveType.collectionTC = tc; + else if (name.equals("nice.lang.Class")) + PrimitiveType.classTC = tc; } Index: Monotype.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Monotype.java,v retrieving revision 1.35 retrieving revision 1.36 diff -C2 -d -r1.35 -r1.36 *** Monotype.java 16 Jun 2004 10:00:37 -0000 1.35 --- Monotype.java 30 Jul 2004 19:08:45 -0000 1.36 *************** *** 218,222 **** ****************************************************************/ ! static Monotype create(mlsub.typing.Monotype m) { return new Wrapper(m); --- 218,222 ---- ****************************************************************/ ! public static Monotype create(mlsub.typing.Monotype m) { return new Wrapper(m); Index: PrimitiveType.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/PrimitiveType.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** PrimitiveType.java 22 Jul 2004 15:19:19 -0000 1.11 --- PrimitiveType.java 30 Jul 2004 19:08:45 -0000 1.12 *************** *** 154,161 **** return gnu.bytecode.Type.pointer_type; } ! if (name.equals("nice.lang.Type")) { - typeTC = tc; // to differ with the null result, which signals error return gnu.bytecode.Type.pointer_type; --- 154,160 ---- return gnu.bytecode.Type.pointer_type; } ! if (name.equals("nice.lang.Type")) { // to differ with the null result, which signals error return gnu.bytecode.Type.pointer_type; *************** *** 182,185 **** --- 181,186 ---- } + public static void reset() { objectPolytype = null; } + public static TypeConstructor maybeTC, sureTC, nullTC; *************** *** 187,191 **** public static Monotype synVoidType; ! static TypeConstructor typeTC; static TypeConstructor collectionTC; static TypeConstructor throwableTC; --- 188,192 ---- public static Monotype synVoidType; ! public static TypeConstructor classTC; static TypeConstructor collectionTC; static TypeConstructor throwableTC; Index: tools.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** tools.nice 30 Jul 2004 13:06:21 -0000 1.38 --- tools.nice 30 Jul 2004 19:08:45 -0000 1.39 *************** *** 114,126 **** TypeConstantExp type = cast(arg2); ! if (type.representedType == null) return null; ! TypeConstructor tc = notNull(type.representedType); mlsub.typing.Monotype[?] parameters; if (tc.arity() == 0) parameters = null; ! else { let originalType = nice.tools.typing.Types.rawType(sym.getMonotype()); --- 114,130 ---- TypeConstantExp type = cast(arg2); ! if (type.getTC() == null) return null; ! TypeConstructor tc = notNull(type.getTC()); ! ! if (tc == PrimitiveType.arrayTC) ! return (sym, notNull(type.representedType)); ! mlsub.typing.Monotype[?] parameters; if (tc.arity() == 0) parameters = null; ! else { let originalType = nice.tools.typing.Types.rawType(sym.getMonotype()); *************** *** 173,176 **** // Local Variables: ! // nice-xprogram: "nicec -d \"$HOME/Nice/classes\" --sourcepath=\"$HOME/Nice/src\" --classpath=\"$HOME/Nice/classes\"" // End: --- 177,180 ---- // Local Variables: ! // nice-xprogram: "../bin/nicec -d ../classes --sourcepath=../src" // End: Index: ConstantExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/ConstantExp.java,v retrieving revision 1.49 retrieving revision 1.50 diff -C2 -d -r1.49 -r1.50 *** ConstantExp.java 24 Nov 2003 17:27:22 -0000 1.49 --- ConstantExp.java 30 Jul 2004 19:08:45 -0000 1.50 *************** *** 280,288 **** return res; } - - public static ConstantExp makeType(LocatedString representation) - { - return new TypeConstantExp(representation); - } /**************************************************************** --- 280,283 ---- Index: typecheck.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/typecheck.nice,v retrieving revision 1.104 retrieving revision 1.105 diff -C2 -d -r1.104 -r1.105 *** typecheck.nice 29 Jul 2004 12:11:40 -0000 1.104 --- typecheck.nice 30 Jul 2004 19:08:45 -0000 1.105 *************** *** 114,118 **** } ! enterIf(); try { typecheck(args.getExp(0)); --- 114,118 ---- } ! enterBlock(); try { typecheck(args.getExp(0)); *************** *** 156,160 **** } ! enterIf(); try { typecheck(args.getExp(0)); --- 156,160 ---- } ! enterBlock(); try { typecheck(args.getExp(0)); *************** *** 397,401 **** var Stack<(MonoSymbol, mlsub.typing.Monotype)> conditionalTypes = new Stack(); ! void enterIf() { ifLevel++; } void enterElse() --- 397,403 ---- var Stack<(MonoSymbol, mlsub.typing.Monotype)> conditionalTypes = new Stack(); ! void enterBlock() { ifLevel++; } ! ! void exitBlock () { enterElse(); exitIf(); } void enterElse() *************** *** 506,510 **** e.condition = condition; ! enterIf(); try{ --- 508,512 ---- e.condition = condition; ! enterBlock(); try{ *************** *** 717,729 **** typecheck(Block b) { ! b.statements = rewrite(b.statements); ! b.locals.foreach(?Block.LocalDeclaration d => typecheck(d)); ! b.statements.foreach ! (?Statement s => { ! try { typecheck(s); } ! catch (bossa.util.UserError ex) ! { throw ensureLocated(ex, notNull(s)); } ! }); } --- 719,738 ---- typecheck(Block b) { ! enterBlock(); ! try { ! b.statements = rewrite(b.statements); ! ! b.locals.foreach(?Block.LocalDeclaration d => typecheck(d)); ! b.statements.foreach ! (?Statement s => { ! try { typecheck(s); } ! catch (bossa.util.UserError ex) ! { throw ensureLocated(ex, notNull(s)); } ! }); ! } ! finally { ! exitBlock(); ! } } *************** *** 816,820 **** } ! enterIf(); try { --- 825,829 ---- } ! enterBlock(); try { |