[Nice-commit] Nice/src/bossa/syntax Pattern.java,1.70,1.71 OverloadedSymbolExp.java,1.58,1.59 NiceMe
Brought to you by:
bonniot
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv27659/src/bossa/syntax Modified Files: Pattern.java OverloadedSymbolExp.java NiceMethod.java Module.java MethodBodyDefinition.java AbstractInterface.java AST.java Log Message: Replaced functions by methods with a default implementation. Index: Pattern.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Pattern.java,v retrieving revision 1.70 retrieving revision 1.71 diff -C2 -d -r1.70 -r1.71 *** Pattern.java 2 Sep 2003 19:57:52 -0000 1.70 --- Pattern.java 11 Sep 2003 20:50:27 -0000 1.71 *************** *** 104,107 **** --- 104,113 ---- } + static Pattern any(LocatedString name) + { + Pattern res = new Pattern(name); + return res; + } + final TypeConstructor getRuntimeTC() { Index: OverloadedSymbolExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/OverloadedSymbolExp.java,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** OverloadedSymbolExp.java 26 Jul 2003 22:29:47 -0000 1.58 --- OverloadedSymbolExp.java 11 Sep 2003 20:50:27 -0000 1.59 *************** *** 299,302 **** --- 299,357 ---- } + static void removeNonMinimal(List symbols) + { + // optimization + if(symbols.size()<2) + return; + + int len = symbols.size(); + VarSymbol[] syms = (VarSymbol[]) + symbols.toArray(new VarSymbol[len]); + boolean[] remove = new boolean[len]; + + for(int s1 = 0; s1<len; s1++) { + + Domain d1 = domain(syms[s1].getType()); + + for(int s2 = 0; s2<len; s2++) + /* + Look for symbols s1 and s2 such that + d2 <: d1 and not d1 <: d2 + In that case s1 can be removed, since it is less specific than s2. + + Optimizations: + Skip the diagonal. + If s2 was removed, then there is s3 below s2. + Therefore s1 will be removed anyway. + */ + if (s1 != s2 && !remove[s2]) { + + Domain d2 = domain(syms[s2].getType()); + + try{ + Typing.leq(d2, d1); + try { + Typing.leq(d1, d2); + } + catch (TypingEx e) { + remove[s1] = true; + break; + } + } + catch(TypingEx e){ + } + } + } + + for(int i = 0; i<len; i++) + if(remove[i]) + { + if (Debug.overloading) + Debug.println("Removing " + syms[i] + " since it is not minimal"); + + symbols.remove(syms[i]); + } + } + /** * Removes the symbols that do not have minimal domain types. *************** *** 370,374 **** { // remove nullness marker ! Monotype[] m = ((FunType) Types.rawType(t.getMonotype())).domain(); Monotype[] dom; --- 425,429 ---- { // remove nullness marker ! Monotype[] m = Types.domain(t.getMonotype()); Monotype[] dom; *************** *** 392,395 **** --- 447,458 ---- return new Domain(t.getConstraint(), new TupleType(dom)); + } + + private static Domain domain(Polytype t) + { + // remove nullness marker + Monotype[] m = Types.domain(t.getMonotype()); + + return new Domain(t.getConstraint(), new TupleType(m)); } Index: NiceMethod.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/NiceMethod.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** NiceMethod.java 15 Jul 2003 10:36:18 -0000 1.23 --- NiceMethod.java 11 Sep 2003 20:50:27 -0000 1.24 *************** *** 40,44 **** @param body the body of the function, or null if this is a real method */ ! public static MethodDeclaration create (MethodContainer c, LocatedString name, --- 40,44 ---- @param body the body of the function, or null if this is a real method */ ! public static Definition create (MethodContainer c, LocatedString name, *************** *** 126,131 **** return new NiceMethod(name, constraint, returnType, params, contract); else ! return new ToplevelFunction(name, constraint, returnType, params, ! body, contract); } --- 126,131 ---- return new NiceMethod(name, constraint, returnType, params, contract); else ! return NiceMethod.WithDefault.create ! (name, constraint, returnType, params, body, contract); } *************** *** 170,173 **** --- 170,235 ---- { s.print(super.toString() + ";\n"); + } + + /**************************************************************** + * Method with default implementation + ****************************************************************/ + + public static class WithDefault extends NiceMethod + { + /** + @param name the name of the method + @param constraint the constraint + @param returnType the return type + @param parameters the formal parameters + @param body the body of the function + */ + public static Definition create + (LocatedString name, + Constraint constraint, + Monotype returnType, + FormalParameters parameters, + Statement body, + Contract contract) + { + if (body == null) + return new NiceMethod + (name, constraint, returnType, parameters, contract); + + return new DefaultMethodImplementation + (name, constraint, returnType, parameters, contract, body); + } + + public WithDefault + (LocatedString name, + Constraint constraint, Monotype returnType, + FormalParameters parameters, + Contract contract, + DefaultMethodImplementation impl) + { + super(name, constraint, returnType, parameters, contract); + this.implementation = impl; + } + + DefaultMethodImplementation implementation; + + void resolve() + { + super.resolve(); + + mlsub.typing.Constraint cst = getType().getConstraint(); + if (mlsub.typing.Constraint.hasBinders(cst)) + try { + typeScope.addSymbols(cst.binders()); + } catch (TypeScope.DuplicateName ex) { + User.error(this, "Double declaration of the same type parameter"); + } + } + + void innerTypecheck() throws TypingEx + { + super.innerTypecheck(); + implementation.innerTypecheck(); + } } } Index: Module.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Module.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** Module.java 8 Aug 2003 19:16:33 -0000 1.15 --- Module.java 11 Sep 2003 20:50:27 -0000 1.16 *************** *** 32,36 **** gnu.expr.Declaration addGlobalVar(String name, gnu.bytecode.Type type, boolean constant); gnu.expr.Expression getDispatchMethod(NiceMethod def); - gnu.expr.Expression lookupPackageMethod(ToplevelFunction fun); gnu.expr.ReferenceExp addMethod(gnu.expr.LambdaExp method, boolean packageMethod); --- 32,35 ---- Index: MethodBodyDefinition.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MethodBodyDefinition.java,v retrieving revision 1.140 retrieving revision 1.141 diff -C2 -d -r1.140 -r1.141 *** MethodBodyDefinition.java 29 Aug 2003 16:08:11 -0000 1.140 --- MethodBodyDefinition.java 11 Sep 2003 20:50:27 -0000 1.141 *************** *** 34,39 **** @author Daniel Bonniot (bo...@us...) */ ! public class MethodBodyDefinition extends Definition ! implements Function { /** --- 34,38 ---- @author Daniel Bonniot (bo...@us...) */ ! public class MethodBodyDefinition extends MethodImplementation { /** *************** *** 54,66 **** Statement body) { ! super(name, Node.down); this.binders = binders; - this.formals = makeFormals(formals, container, name.location()); - this.body = body; this.declaration = null; ! this.insideClass = container != null || this.formals != null && this.formals.length >= 1 && --- 53,66 ---- Statement body) { ! super(name, body, makeFormals(formals, container, name.location())); this.binders = binders; this.declaration = null; + } ! boolean hasThis() ! { ! return this.formals != null && this.formals.length >= 1 && *************** *** 88,140 **** } ! private MonoSymbol[] buildSymbols(Pattern[] names, Monotype[] types) ! { ! if(names.length != types.length) ! switch(types.length){ ! case 0: User.error(this,"Method "+name+" has no parameters"); ! case 1: User.error(this,"Method "+name+" has 1 parameter"); ! default:User.error(this, ! "Method "+name+" has "+types.length+" parameters"); ! } ! ! MonoSymbol[] res = new MonoSymbol[names.length]; ! for(int tn = 0; tn < names.length; tn++) ! { ! Pattern p = names[tn]; ! ! Monotype type; ! ! if (p.atAny()) ! { ! // When a parameter is not dispatched on, it has the declared type ! // of that parameter in the method declaration. ! type = types[tn]; ! } ! else if (p.getRuntimeTC() != null) ! { ! AtomicKind v = p.tc.variance; ! p.getRuntimeTC().setVariance(v); ! type = new mlsub.typing.MonotypeConstructor(p.getRuntimeTC(), mlsub.typing.MonotypeVar.news(v.arity())); ! type.setKind(v); ! ! type = bossa.syntax.Monotype.sure(type); ! } ! else ! { ! if (p.name == null) ! // anonymous pattern ! type = new MonotypeVar(types[tn].toString()+ "(argument_" + tn+")"); ! else ! type = new MonotypeVar(types[tn].toString()+ "(" + p.name + ")"); ! } ! ! res[tn] = new MonoSymbol(p.name, type); ! } ! return res; ! } ! ! public MethodDeclaration getDeclaration() { ! return declaration; } --- 88,94 ---- } ! final TypeConstructor firstArgument() { ! return formals[0].tc; } *************** *** 159,164 **** alternative = new bossa.link.SourceAlternative(this); ! parameters = buildSymbols(this.formals, declaration.getArgTypes()); ! scope.addSymbols(parameters); } --- 113,117 ---- alternative = new bossa.link.SourceAlternative(this); ! buildSymbols(); } *************** *** 273,276 **** --- 226,230 ---- } + // Check that the non-dispatched parameter names match the declaration outer: for(Iterator it = symbols.iterator(); it.hasNext();) { MethodDeclaration m = ((MethodDeclaration.Symbol) it.next()).getMethodDeclaration(); *************** *** 286,289 **** --- 240,245 ---- } + OverloadedSymbolExp.removeNonMinimal(symbols); + if(symbols.size() == 1) return (VarSymbol) symbols.get(0); *************** *** 321,324 **** --- 277,281 ---- Typing.leq(Types.rawType(m1), Types.rawType(m2)); } + void doResolve() { *************** *** 372,409 **** } - void resolveBody() - { - if (insideClass) - Node.thisExp = new SymbolExp(parameters[0], location()); - - try { - body = bossa.syntax.dispatch.analyse - (body, scope, typeScope, !Types.isVoid(declaration.getReturnType())); - } - finally { - Node.thisExp = null; - } - } - /**************************************************************** * Type checking ****************************************************************/ - public mlsub.typing.Monotype getExpectedType() - { - return declaration.getReturnType(); - } - - public void checkReturnedType(mlsub.typing.Polytype returned) - throws Function.WrongReturnType - { - try { - Typing.leq(returned, declaration.getReturnType()); - } - catch (mlsub.typing.TypingEx e) { - throw new Function.WrongReturnType(e, declaration.getReturnType()); - } - } - void typecheck() { --- 329,336 ---- *************** *** 489,493 **** Node.currentFunction = this; ! if (insideClass) Node.thisExp = new SymbolExp(parameters[0], location()); --- 416,420 ---- Node.currentFunction = this; ! if (hasThis()) Node.thisExp = new SymbolExp(parameters[0], location()); *************** *** 525,529 **** ****************************************************************/ ! private Type[] javaArgTypes() { Type[] res = new Type[parameters.length]; --- 452,456 ---- ****************************************************************/ ! protected Type[] javaArgTypes() { Type[] res = new Type[parameters.length]; *************** *** 539,591 **** } - gnu.expr.ReferenceExp ref; - gnu.expr.LambdaExp compiledMethod; - - public gnu.expr.ReferenceExp getRefExp() - { - if (ref == null) - ref = createRef(); - - return ref; - } - - public void compile() - { - if (Debug.codeGeneration) - Debug.println("Compiling method body " + this); - - getRefExp(); - Gen.setMethodBody(compiledMethod, body.generateCode()); - } - - private gnu.expr.ReferenceExp createRef () - { - createMethod(name.toString(), false); - gnu.expr.ReferenceExp ref = module.addMethod(compiledMethod, true); - - compiledMethod.addBytecodeAttribute - (new MiscAttr("definition", declaration.getFullName().getBytes())); - compiledMethod.addBytecodeAttribute - (new MiscAttr("patterns", - Pattern.bytecodeRepresentation(formals).getBytes())); - - return ref; - } - - final TypeConstructor firstArgument() - { - return formals[0].tc; - } - - private void createMethod (String bytecodeName, boolean member) - { - compiledMethod = - Gen.createMethod(bytecodeName, - javaArgTypes(), - declaration.javaReturnType(), - parameters, - true, member); - } - gnu.expr.Expression[] compiledArguments() { --- 466,469 ---- *************** *** 597,600 **** --- 475,479 ---- bossa.link.Alternative getAlternative() { return alternative; } + /**************************************************************** * Printing *************** *** 606,617 **** } - public Pattern[] getPatterns() { return formals; } - - private MethodDeclaration declaration; - protected MonoSymbol[] parameters; - private Pattern[] formals; Collection /* of LocatedString */ binders; // Null if type parameters are not bound - private Statement body; - private boolean insideClass; } - --- 485,488 ---- Index: AbstractInterface.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/AbstractInterface.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** AbstractInterface.java 28 May 2003 12:57:25 -0000 1.15 --- AbstractInterface.java 11 Sep 2003 20:50:27 -0000 1.16 *************** *** 46,50 **** } ! public void addMethod(MethodDeclaration m) { addChild(m); --- 46,50 ---- } ! public void addMethod(Definition m) { addChild(m); *************** *** 76,81 **** { Object child = i.next(); ! if (child instanceof ToplevelFunction) ! ((ToplevelFunction) child).resolveBody(); } } --- 76,81 ---- { Object child = i.next(); ! if (child instanceof DefaultMethodImplementation) ! ((DefaultMethodImplementation) child).resolveBody(); } } Index: AST.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/AST.java,v retrieving revision 1.49 retrieving revision 1.50 diff -C2 -d -r1.49 -r1.50 *** AST.java 31 Jul 2003 21:59:39 -0000 1.49 --- AST.java 11 Sep 2003 20:50:27 -0000 1.50 *************** *** 55,58 **** --- 55,60 ---- else if (node instanceof GlobalVarDeclaration) globals.add(node); + else if (node instanceof DefaultMethodImplementation) + methods.add(((DefaultMethodImplementation) node).getDeclaration()); } |