[Nice-commit] Nice/src/bossa/syntax Node.java,1.65,1.66 analyse.nice,1.122,1.123 call.nice,1.7,1.8 c
Brought to you by:
bonniot
From: Arjan B. <ar...@us...> - 2005-01-10 23:58:20
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30060/F:/nice/src/bossa/syntax Modified Files: Node.java analyse.nice call.nice constraint.nice customConstructor.nice importedconstructor.nice locals.nice methodContainer.nice niceMethod.nice nicefieldaccess.nice polytype.nice tools.nice typedef.nice Removed Files: AtomicConstraint.java Constraint.java Log Message: Converted AtomicConstraint and Constraint. Index: analyse.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/analyse.nice,v retrieving revision 1.122 retrieving revision 1.123 diff -C2 -d -r1.122 -r1.123 *** analyse.nice 3 Jan 2005 01:24:12 -0000 1.122 --- analyse.nice 10 Jan 2005 23:58:04 -0000 1.123 *************** *** 454,458 **** info.anonFunDepth = info.depth; ! if (e.constraint != Constraint.True && e.constraint != null) { e.cst = notNull(e.constraint).resolveToLowlevel(); --- 454,458 ---- info.anonFunDepth = info.depth; ! if (e.constraint != trueConstraint && e.constraint != null) { e.cst = notNull(e.constraint).resolveToLowlevel(); Index: customConstructor.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/customConstructor.nice,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** customConstructor.nice 4 Jan 2005 21:20:39 -0000 1.10 --- customConstructor.nice 10 Jan 2005 23:58:04 -0000 1.11 *************** *** 167,177 **** classe.nullness = Monotype.sure; ! if (cst == Constraint.True) return classe; ! TypeSymbol[] syms = cst.getBinderArray(); ! Monotype[] params = cast(new Monotype[syms.length]); ! for (int i = 0; i < syms.length; i++) { if (! (syms[i] instanceof mlsub.typing.MonotypeVar)) --- 167,177 ---- classe.nullness = Monotype.sure; ! if (cst == trueConstraint) return classe; ! List<TypeSymbol> syms = cst.getBinders(); ! Monotype[] params = cast(new Monotype[syms.size()]); ! for (int i = 0; i < syms.size(); i++) { if (! (syms[i] instanceof mlsub.typing.MonotypeVar)) Index: Node.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Node.java,v retrieving revision 1.65 retrieving revision 1.66 diff -C2 -d -r1.65 -r1.66 *** Node.java 30 Dec 2004 20:58:37 -0000 1.65 --- Node.java 10 Jan 2005 23:58:04 -0000 1.66 *************** *** 102,116 **** addTypeMap(s.toString(), s); } ! ! void addTypeSymbols(Collection c) ! { ! if(c!=null) ! for(Iterator i=c.iterator(); i.hasNext();) ! { ! TypeSymbol s = (TypeSymbol) i.next(); ! addTypeMap(s.toString(), s); ! } ! } ! void addTypeMap(String name, TypeSymbol symbol) { --- 102,106 ---- addTypeMap(s.toString(), s); } ! void addTypeMap(String name, TypeSymbol symbol) { Index: polytype.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/polytype.nice,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** polytype.nice 10 Jan 2005 20:04:43 -0000 1.1 --- polytype.nice 10 Jan 2005 23:58:04 -0000 1.2 *************** *** 21,25 **** public class Polytype extends Node { ! private Constraint constraint = Constraint.True; private Monotype monotype; --- 21,25 ---- public class Polytype extends Node { ! private Constraint constraint = trueConstraint; private Monotype monotype; Index: constraint.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/constraint.nice,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** constraint.nice 8 Dec 2004 23:03:37 -0000 1.1 --- constraint.nice 10 Jan 2005 23:58:04 -0000 1.2 *************** *** 16,19 **** --- 16,283 ---- /** + A list of binders + atomic constraints. + + @see AtomicConstraint + + */ + public class Constraint extends Node + { + private List<TypeSymbol> binders; + private List<AtomicConstraint> atomics; + + /** + * Returns a new constraint. + * The lists are new, but the list elements are the same. + */ + Constraint shallowClone() + { + return createConstraint(new ArrayList(binders), new ArrayList(atomics)); + } + + mlsub.typing.Constraint resolveToLowlevel() + { + return mlsub.typing.Constraint.create( + binders.isEmpty() ? null : binders.toArray(), + atomics.isEmpty() ? null : atomics.mapToArray( + AtomicConstraint ac => ac.resolve(notNull(typeScope)))); + } + + toString() + { + if (atomics.isEmpty()) + return Util.map("<",", ","> ",binders); + + if (binders.isEmpty()) + return Util.map("<",", ","> ",atomics); + + // Put in a parsable format. + StringBuffer res = new StringBuffer("<"); + boolean first = true; + + Constraint c = this.shallowClone(); + + let i = c.binders.iterator(); + while (i.hasNext()) + { + TypeSymbol s = i.next(); + if (!(s instanceof TypeConstructor)) + // Handle the ! constraint + { + MonotypeVar mv = cast(s); + let j = c.atomics.iterator(); + while (j.hasNext()) + { + AtomicConstraint atom = j.next(); + if (atom.isSureConstraintFor(mv)) + { + if (first) + first = false; + else + res.append(','); + res.append('!').append(s); + j.remove(); + i.remove(); + break; + } + } + + continue; + } + + TypeConstructor tc = s; + boolean ok = false; + let j = c.atomics.iterator(); + while (j.hasNext()) + { + AtomicConstraint atom = j.next(); + let parent = atom.getParentFor(tc); + if (parent!=null) + { + if (first) + first = false; + else + res.append(','); + res.append(parent).append(' ').append(tc); + j.remove(); + i.remove(); + ok = true; + break; + } + } + if (!ok) + Internal.error("Unable to print the constraint in a parsable form because of "+tc); + } + + res.append(Util.map((res.length()>1 ? ", " : ""),", ","", c.binders)); + res.append(Util.map(" | ",", ","",c.atomics)).append("> "); + return res.toString(); + } + + /** + Add the binder to the front of the list of binders. + */ + void addFirstBinder(TypeSymbol s) + { + binders.add(0, s); + this.addTypeSymbol(s); + } + + /** + Add the binder if it is not already there. + */ + void addBinder(TypeSymbol s) + { + if (!binders.contains(s)) + { + binders.add(s); + this.addTypeSymbol(s); + } + } + + /** + * Adds binders that are not already present + * + * @param b a collection of TypeSymbol + */ + void addBinders(TypeSymbol[?] bs) + { + if (bs==null) + return; + + for (int i = 0; i<bs.length; i++) + this.addBinder(bs[i]); + } + + void addAtom(AtomicConstraint atom) + { + atomics.add(atom); + } + + void addAtoms(List<AtomicConstraint> l) + { + atomics.addAll(l); + } + + public boolean isTrivial() = binders.isEmpty() && atomics.isEmpty(); + + List<TypeSymbol> getBinders() = binders; + List<AtomicConstraint> getAtoms() = atomics; + } + + /** + * Creates the constraint \forall binders . atomics + * + * @param binders a collection of TypeSymbols + * @param atomics a collection of AtomicConstraints + */ + public Constraint createConstraint(?List<TypeSymbol> binders, ?List<AtomicConstraint> atomics) + { + if (binders == null && atomics == null) + return trueConstraint; + + let res = new Constraint(Node.upper, binders: binders || new ArrayList(), + atomics: atomics || new ArrayList()); + if (binders != null) + for (b : binders) + res.addTypeSymbol(b); + + return res; + } + + /** + * The trivial constraint. + * + * This field is final, so pointer equality can be used + * to test whether a constraint is True. + * + * @return a constraint with no binders, always true + */ + let Constraint trueConstraint = new Constraint(Node.upper, binders: new ArrayList(0), atomics: new ArrayList(0)); + + /** + An abstract constraint atom. See children. + + @see Constraint + */ + public abstract class AtomicConstraint + { + + mlsub.typing.AtomicConstraint resolve(TypeScope scope); + + /** + * Returns a string that represents a constraint element + * another element was introduced in comparison of. + * + * For instance, in <Num N | ... > + * getParentFor(N) should be "Num". + * Used to reproduce a parsable form of the constraint. + * + * @param tc a constraint element + * @return the representation of its "parent", or <code>null</code> + */ + ?String getParentFor(mlsub.typing.TypeConstructor tc) + { + return null; + } + + boolean isSureConstraintFor(mlsub.typing.MonotypeVar mv) + { + return false; + } + } + + public AtomicConstraint createSureTypeVar(mlsub.typing.MonotypeVar tv) + { + return new AtomicConstraintWrapper + (atom: new mlsub.typing.MonotypeLeqTcCst(tv, nice.tools.typing.PrimitiveType.sureTC)); + } + + /** + Wrapper for lowlevel AtomicConstraint + */ + final class AtomicConstraintWrapper extends AtomicConstraint + { + private final mlsub.typing.AtomicConstraint atom; + + resolve(scope) = atom; + + getParentFor(tc) + { + if (atom instanceof mlsub.typing.TypeConstructorLeqCst) + { + mlsub.typing.TypeConstructorLeqCst leq = cast(atom); + if (leq.t1()==tc) + return leq.t2().toString(); + else + return null; + } + + if (atom instanceof mlsub.typing.ImplementsCst) + { + mlsub.typing.ImplementsCst leq = cast(atom); + if (leq.tc()==tc) + return leq.itf().toString(); + else + return null; + } + + return null; + } + + isSureConstraintFor(mv) + { + if (atom instanceof mlsub.typing.MonotypeLeqTcCst) + { + let mlsub.typing.MonotypeLeqTcCst a = cast(atom); + return a.m == mv; + } + + return false; + } + + toString() = atom.toString(); + } + + /** Syntactic inequality between monotypes. *************** *** 24,28 **** Monotype m2; ! resolve(ts) { return new mlsub.typing.MonotypeLeqCst --- 288,292 ---- Monotype m2; ! resolve(TypeScope ts) { return new mlsub.typing.MonotypeLeqCst *************** *** 37,46 **** */ ! public class TypeConstructorLeqCst extends AtomicConstraint { mlsub.typing.TypeConstructor t1; TypeIdent t2; ! resolve(ts) { // If t2 resolve to an interface definition, --- 301,310 ---- */ ! public class TypeConstructorLeqCst extends bossa.syntax.AtomicConstraint { mlsub.typing.TypeConstructor t1; TypeIdent t2; ! resolve(TypeScope ts) { // If t2 resolve to an interface definition, Index: locals.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/locals.nice,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** locals.nice 10 Jan 2005 20:04:43 -0000 1.4 --- locals.nice 10 Jan 2005 23:58:04 -0000 1.5 *************** *** 208,213 **** public Statement createLocalFunction(LocatedString name, Monotype returnType, FormalParameters parameters, Statement body) { ! let value = createFunExp(Constraint.True, parameters.getMonoSymbols() || new MonoSymbol[0], body); ! let symbol = new FunSymbol(name, Constraint.True, parameters, returnType); notNull(symbol.syntacticType).monotype.nullness = Monotype.sure; return new LocalFunction(left: symbol, value: value, parameters: parameters); --- 208,213 ---- public Statement createLocalFunction(LocatedString name, Monotype returnType, FormalParameters parameters, Statement body) { ! let value = createFunExp(trueConstraint, parameters.getMonoSymbols() || new MonoSymbol[0], body); ! let symbol = new FunSymbol(name, trueConstraint, parameters, returnType); notNull(symbol.syntacticType).monotype.nullness = Monotype.sure; return new LocalFunction(left: symbol, value: value, parameters: parameters); --- Constraint.java DELETED --- --- AtomicConstraint.java DELETED --- Index: niceMethod.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/niceMethod.nice,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** niceMethod.nice 4 Jan 2005 21:20:39 -0000 1.9 --- niceMethod.nice 10 Jan 2005 23:58:04 -0000 1.10 *************** *** 248,253 **** mlsub.typing.TypeSymbol[?] thisBinders = c.getBinders(); - int thisBindersLen = (thisBinders == null ? 0 : thisBinders.length); - mlsub.typing.TypeSymbol container = c.getTypeSymbol(); // contains the interface if container is one --- 248,251 ---- *************** *** 266,275 **** // we must create a new one, otherwise we would // modify other methods! ! if (constraint == Constraint.True) ! constraint = new Constraint ! (new ArrayList(thisBindersLen + (hasAlike ? 1 : 0)), ! new ArrayList((hasAlike ? 1 : 0) + ! (thisConstraint == null ? 0 : ! thisConstraint.getAtoms().size()))); constraint.addBinders(thisBinders); --- 264,269 ---- // we must create a new one, otherwise we would // modify other methods! ! if (constraint == trueConstraint) ! constraint = createConstraint(new ArrayList(), new ArrayList()); constraint.addBinders(thisBinders); *************** *** 296,300 **** else atom = new mlsub.typing.TypeConstructorLeqCst(alikeTC, tc); ! constraint.addAtom(AtomicConstraint.create(atom)); thisType = Monotype.create --- 290,294 ---- else atom = new mlsub.typing.TypeConstructorLeqCst(alikeTC, tc); ! constraint.addAtom(new AtomicConstraintWrapper(atom: atom)); thisType = Monotype.create Index: tools.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v retrieving revision 1.92 retrieving revision 1.93 diff -C2 -d -r1.92 -r1.93 *** tools.nice 5 Jan 2005 02:02:45 -0000 1.92 --- tools.nice 10 Jan 2005 23:58:04 -0000 1.93 *************** *** 230,234 **** ?gnu.expr.Declaration getDeclaration(VarSymbol) = native gnu.expr.Declaration VarSymbol.getDeclaration(); void printInterface(ClassImplementation, java.io.PrintWriter) = native void ClassImplementation.printInterface(java.io.PrintWriter); - List<AtomicConstraint> getAtoms(Constraint) = native List Constraint.getAtoms(); mlsub.typing.Monotype[?] resolve(TypeMap, Monotype[?]) = native mlsub.typing.Monotype[] Monotype.resolve(TypeMap, Monotype[]); ?gnu.bytecode.Type TypeImport_lookup(LocatedString) = native gnu.bytecode.Type nice.tools.code.TypeImport.lookup(LocatedString); --- 230,233 ---- *************** *** 238,243 **** gnu.expr.CopyArgument CopyArgument(Stack<gnu.bytecode.Variable>) = native new gnu.expr.CopyArgument(Stack); AST AST(List<Definition>, int) = native new AST(List, int); - ?String getParentFor(AtomicConstraint, mlsub.typing.TypeConstructor) = native String AtomicConstraint.getParentFor(mlsub.typing.TypeConstructor); - mlsub.typing.AtomicConstraint resolve(AtomicConstraint, TypeScope) = native mlsub.typing.AtomicConstraint AtomicConstraint.resolve(TypeScope); bossa.syntax.Monotype substitute(bossa.syntax.Monotype,Map<String,bossa.syntax.Monotype>) = native bossa.syntax.Monotype bossa.syntax.Monotype.substitute(Map); List<bossa.syntax.Monotype> substitute(Map<String,bossa.syntax.Monotype>,Collection<bossa.syntax.Monotype>) = native List bossa.syntax.Monotype.substitute(Map,Collection); --- 237,240 ---- *************** *** 251,259 **** List<VarSymbol> lookup(VarScope, LocatedString) = native List VarScope.lookup(LocatedString); Map<gnu.bytecode.Type,mlsub.typing.TypeConstructor> javaTypeConstructors(bossa.modules.Compilation ) = native bossa.modules.Compilation.javaTypeConstructors; - bossa.syntax.Constraint newConstraint(List<TypeSymbol>, List<AtomicConstraint>) = native new Constraint(List, List); - void addAtoms(Constraint, List<AtomicConstraint>) = native void Constraint.addAtoms(List); Map<VarSymbol, int[]> usedArguments(Arguments) = native Arguments.usedArguments; Map<VarSymbol, Expression[]> applicationExpressions(Arguments) = native Arguments.applicationExpressions; - Constraint _Constraint(mlsub.typing.TypeSymbol[], List<AtomicConstraint>) = native new Constraint(mlsub.typing.TypeSymbol[], List); // Retypings needed since java types are not strict. --- 248,253 ---- Index: typedef.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/typedef.nice,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** typedef.nice 5 Jan 2005 02:02:46 -0000 1.10 --- typedef.nice 10 Jan 2005 23:58:04 -0000 1.11 *************** *** 388,392 **** if (our != null) { ! binders.addAll(Arrays.asList(our.getBinderArray())); atoms.addAll(our.getAtoms()); } --- 388,392 ---- if (our != null) { ! binders.addAll(our.getBinders()); atoms.addAll(our.getAtoms()); } *************** *** 411,416 **** } ! return new ClassConstraint(binders, atoms, typeParameters: cast(typeParameters)); } --- 411,421 ---- } ! let res = new ClassConstraint(Node.upper, binders: binders, atomics: atoms, typeParameters: cast(typeParameters)); + + for (b : binders) + res.addTypeSymbol(b); + + return res; } Index: methodContainer.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/methodContainer.nice,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** methodContainer.nice 5 Jan 2005 02:02:43 -0000 1.1 --- methodContainer.nice 10 Jan 2005 23:58:04 -0000 1.2 *************** *** 50,55 **** } catch(TypeScope.DuplicateName ex) {} ! resolvedConstraints = ! AtomicConstraint.resolve(scope, notNull(classConstraint).getAtoms()); } } --- 50,55 ---- } catch(TypeScope.DuplicateName ex) {} ! resolvedConstraints = notNull(classConstraint).getAtoms().mapToArray( ! AtomicConstraint ac => ac.resolve(scope)); } } *************** *** 61,69 **** return null; else ! return notNull(classConstraint).getBinderArray(); } /** The type parameters of the class. */ ! public mlsub.typing.Monotype[?] getTypeParameters () { if (classConstraint == null) --- 61,69 ---- return null; else ! return notNull(classConstraint).getBinders().toArray(); } /** The type parameters of the class. */ ! public mlsub.typing.Monotype[?] getTypeParameters() { if (classConstraint == null) *************** *** 79,83 **** else return new mlsub.typing.Constraint ! (notNull(classConstraint).getBinderArray(), resolvedConstraints); } --- 79,83 ---- else return new mlsub.typing.Constraint ! (notNull(classConstraint).getBinders().toArray, resolvedConstraints); } *************** *** 150,154 **** boolean resolve; ! if (cst == bossa.syntax.Constraint.True || cst == null) { binders = cast(typeParameters); --- 150,154 ---- boolean resolve; ! if (cst == trueConstraint || cst == null) { binders = cast(typeParameters); *************** *** 159,163 **** { constraints = cst.getAtoms(); ! binders = cst.getBinderArray(); resolve = true; } --- 159,163 ---- { constraints = cst.getAtoms(); ! binders = cst.getBinders().toArray(); resolve = true; } *************** *** 198,201 **** nice.tools.typing.Types.makeMarkedType(tp); ! return new ClassConstraint(binders, atoms, typeParameters: tps); } \ No newline at end of file --- 198,207 ---- nice.tools.typing.Types.makeMarkedType(tp); ! let res = new ClassConstraint(Node.upper, binders: new ArrayList(binders), ! atomics: atoms, typeParameters: tps); ! ! for (b : binders) ! res.addTypeSymbol(b); ! ! return res; } \ No newline at end of file Index: nicefieldaccess.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/nicefieldaccess.nice,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** nicefieldaccess.nice 5 Jan 2005 02:02:44 -0000 1.6 --- nicefieldaccess.nice 10 Jan 2005 23:58:04 -0000 1.7 *************** *** 47,51 **** let params = createFormalParameters([new Parameter(type: Monotype.create(argType))]); ! let constr = bossa.syntax.Constraint.create(classDef.getBinders()); let res = new NiceFieldAccess(field.getName(), Node.down, parameters: params, --- 47,51 ---- let params = createFormalParameters([new Parameter(type: Monotype.create(argType))]); ! let constr = createConstraint(classDef.getBinders(), null); let res = new NiceFieldAccess(field.getName(), Node.down, parameters: params, Index: importedconstructor.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/importedconstructor.nice,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** importedconstructor.nice 4 Jan 2005 21:20:39 -0000 1.8 --- importedconstructor.nice 10 Jan 2005 23:58:04 -0000 1.9 *************** *** 100,104 **** let constraint = classDef.classConstraint == null ? ! bossa.syntax.Constraint.True : notNull(classDef.classConstraint).shallowClone(); let name = new LocatedString("<init>",def.getDefinition().location()); --- 100,104 ---- let constraint = classDef.classConstraint == null ? ! trueConstraint : notNull(classDef.classConstraint).shallowClone(); let name = new LocatedString("<init>",def.getDefinition().location()); Index: call.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/call.nice,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** call.nice 1 Jan 2005 16:36:31 -0000 1.7 --- call.nice 10 Jan 2005 23:58:04 -0000 1.8 *************** *** 276,280 **** { let CallExp exp = cast(callexp); ! exp.arguments.add(createFunExp(bossa.syntax.Constraint.True, [], block), name); } --- 276,280 ---- { let CallExp exp = cast(callexp); ! exp.arguments.add(createFunExp(trueConstraint, [], block), name); } |