[Nice-commit] Nice/src/bossa/syntax Pattern.java,1.81,1.82 NiceMethod.java,1.27,1.28 MethodImplement
Brought to you by:
bonniot
From: <bo...@us...> - 2004-02-28 14:33:15
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16026/src/bossa/syntax Modified Files: Pattern.java NiceMethod.java MethodImplementation.java MethodDeclaration.java MethodBodyDefinition.java DefaultMethodImplementation.java CustomConstructor.java AST.java Log Message: Methods now specialize other methods with a larger domain (ignoring subtyping on non-dispatchable types like primitive and tuple types). Covariance of the return type is not yet checked, and there is not yet a way to explicitely declare that a method is a specialization. Index: Pattern.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Pattern.java,v retrieving revision 1.81 retrieving revision 1.82 diff -C2 -d -r1.81 -r1.82 *** Pattern.java 28 Feb 2004 12:26:05 -0000 1.81 --- Pattern.java 28 Feb 2004 14:23:43 -0000 1.82 *************** *** 32,36 **** @version $Date$ ! @author Daniel Bonniot (d.b...@ma...) */ public class Pattern implements Located --- 32,36 ---- @version $Date$ ! @author Daniel Bonniot (bo...@us...) */ public class Pattern implements Located *************** *** 396,399 **** --- 396,408 ---- return false; + if (that.atNonNull()) + // The only reason it could be false is if we are atNull, but that's + // already handled. + return true; + + // that is not atNonNull nor atAny at this point + if (this.atNonNull()) + return false; + if (that.atBool()) return this.atBool() && (this.atTrue() == that.atTrue()); *************** *** 437,440 **** --- 446,452 ---- return tag == PrimitiveType.nullTC; + if (atNonNull()) + return tag != PrimitiveType.nullTC; + // a null tc is an unmatchable argument (e.g. function) if (tag == null) *************** *** 856,859 **** --- 868,877 ---- } public boolean atNull() { return atValue instanceof NullExp; } + /** This pattern only specifies that the vlaue is not null. + This cannot be explicitely used in source programs, but it is useful + when a method with a non-null parameter specializes one where that + parameter can be null. + */ + public boolean atNonNull() { return tc == PrimitiveType.sureTC; } public boolean atAny() { return atValue == null && tc == null; } public boolean atBool() { Index: NiceMethod.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/NiceMethod.java,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** NiceMethod.java 16 Feb 2004 21:47:11 -0000 1.27 --- NiceMethod.java 28 Feb 2004 14:23:43 -0000 1.28 *************** *** 15,18 **** --- 15,19 ---- import bossa.util.*; import mlsub.typing.*; + import nice.tools.typing.Types; import gnu.bytecode.*; *************** *** 148,151 **** --- 149,233 ---- } + void resolve() + { + homonyms = Node.getGlobalScope().lookup(getName()); + homonyms.remove(getSymbol()); + } + + void typedResolve() + { + findSpecializedMethods(); + + super.typedResolve(); + } + + private List homonyms; + + private List specializedMethods = null; + + public Iterator listSpecializedMethods() + { + return specializedMethods == null ? null : specializedMethods.iterator(); + } + + public boolean specializesMethods() + { + //if (homonyms != null) + //throw new Error(this.toString()); + + return specializedMethods != null; + } + + void findSpecializedMethods() + { + if (homonyms.isEmpty()) + { + homonyms = null; + return; + } + + Domain ourDomain = Types.domain(getType()); + + for (ListIterator i = homonyms.listIterator(); i.hasNext();) + { + VarSymbol s = (VarSymbol) i.next(); + MethodDeclaration d = s.getMethodDeclaration(); + + // Ignore non-methods. + if (d == null || d.isIgnored()) + continue; + + // Check that we have the same number of arguments + if (d.getArity() != this.getArity()) + continue; + + Domain itsDomain = Types.domain(s.getType()); + + // Do we have a smaller domain? + if (! (Typing.smaller(ourDomain, itsDomain, true))) + continue; + + if (Typing.smaller(itsDomain, ourDomain)) + { + if (module == d.module) + User.error(this, "This method has a domain identical to " + + d + ", which is defined at " + d.location()); + else + // Methods with identical domains in different packages are + // accepted, but they do not specialize each other. + // They can be refered to unambiguously by using their + // fully qualified name. + continue; + } + + if (specializedMethods == null) + specializedMethods = new ArrayList + (homonyms.size() - i.previousIndex()); + specializedMethods.add(d); + } + + homonyms = null; + } + /**************************************************************** * Code generation Index: MethodImplementation.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MethodImplementation.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** MethodImplementation.java 25 Feb 2004 11:23:29 -0000 1.4 --- MethodImplementation.java 28 Feb 2004 14:23:43 -0000 1.5 *************** *** 24,27 **** --- 24,28 ---- import mlsub.typing.Typing; import mlsub.typing.TypeConstructor; + import mlsub.typing.Monotype; import mlsub.typing.MonotypeVar; import gnu.bytecode.*; *************** *** 96,99 **** --- 97,119 ---- } + /** Where no patterns are present, add those corresponding to the method + declaration. + */ + void addPatterns() + { + Monotype[] parameters = Types.parameters(declaration.getType()); + for (int i = 0; i < formals.length; i++) + if (formals[i].tc == null) + { + formals[i].tc = Types.concreteConstructor(parameters[i]); + + if (formals[i].tc == null && Types.isSure(parameters[i])) + formals[i].tc = PrimitiveType.sureTC; + + if (Types.isPrimitive(formals[i].tc)) + formals[i].tc = null; + } + } + void resolveBody() { *************** *** 108,113 **** --- 128,140 ---- Node.thisExp = null; } + + // Register this alternative for the link test + alternative = new bossa.link.SourceAlternative(this); } + private bossa.link.Alternative alternative; + + bossa.link.Alternative getAlternative() { return alternative; } + /**************************************************************** * Type checking *************** *** 178,182 **** compiledMethod.addBytecodeAttribute ! (new MiscAttr("definition", declaration.getFullName().getBytes())); compiledMethod.addBytecodeAttribute (new MiscAttr("patterns", --- 205,209 ---- compiledMethod.addBytecodeAttribute ! (new MiscAttr("definition", declaration.getAllFullNames().getBytes())); compiledMethod.addBytecodeAttribute (new MiscAttr("patterns", Index: MethodDeclaration.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MethodDeclaration.java,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** MethodDeclaration.java 25 Feb 2004 11:23:29 -0000 1.54 --- MethodDeclaration.java 28 Feb 2004 14:23:43 -0000 1.55 *************** *** 394,397 **** --- 394,430 ---- } + public boolean specializesMethods() + { + return false; + } + + public Iterator listSpecializedMethods() + { + return null; + } + + /** @return a string that uniquely represents this method + and those that it specializes. + */ + String getAllFullNames() + { + Iterator i = listSpecializedMethods(); + + if (i == null) + return getFullName(); + + StringBuffer res = new StringBuffer(); + res.append(getFullName()); + while (i.hasNext()) + { + res.append(methodListSeparator); + res.append(((MethodDeclaration) i.next()).getFullName()); + } + + return res.toString(); + } + + public static final char methodListSeparator = ';'; + private gnu.expr.Expression code; Index: MethodBodyDefinition.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MethodBodyDefinition.java,v retrieving revision 1.148 retrieving revision 1.149 diff -C2 -d -r1.148 -r1.149 *** MethodBodyDefinition.java 25 Feb 2004 11:23:29 -0000 1.148 --- MethodBodyDefinition.java 28 Feb 2004 14:23:43 -0000 1.149 *************** *** 112,118 **** d.location()); - // Register this alternative for the link test - alternative = new bossa.link.SourceAlternative(this); - buildSymbols(); } --- 112,115 ---- *************** *** 343,347 **** } ! removeUnnecessaryDispatch(); } --- 340,345 ---- } ! if (! declaration.specializesMethods()) ! removeUnnecessaryDispatch(); } *************** *** 482,485 **** --- 480,489 ---- } } + + // If the method we implement specialize others, then we cannot + // omit the patterns, as we do handle only a special case of those + // more general methods. + if (declaration.specializesMethods()) + addPatterns(); } *************** *** 516,524 **** } - private bossa.link.Alternative alternative; - - bossa.link.Alternative getAlternative() { return alternative; } - - /**************************************************************** * Printing --- 520,523 ---- Index: DefaultMethodImplementation.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/DefaultMethodImplementation.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** DefaultMethodImplementation.java 25 Feb 2004 11:23:29 -0000 1.5 --- DefaultMethodImplementation.java 28 Feb 2004 14:23:43 -0000 1.6 *************** *** 78,86 **** { super.resolveBody(); - /*alternative =*/ new bossa.link.SourceAlternative(this); } void innerTypecheck() throws mlsub.typing.TypingEx { Node.currentFunction = this; if (hasThis()) --- 78,91 ---- { super.resolveBody(); } void innerTypecheck() throws mlsub.typing.TypingEx { + // If the method we implement specialize others, then we cannot + // omit the patterns, as we do handle only a special case of those + // more general methods. + if (declaration.specializesMethods()) + addPatterns(); + Node.currentFunction = this; if (hasThis()) Index: CustomConstructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CustomConstructor.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** CustomConstructor.java 20 Feb 2004 00:31:11 -0000 1.13 --- CustomConstructor.java 28 Feb 2004 14:23:43 -0000 1.14 *************** *** 131,134 **** --- 131,136 ---- void resolve() { + super.resolve(); + TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(className); TypeConstructors.addConstructor(tc, this); *************** *** 273,276 **** --- 275,279 ---- void resolve() { + super.resolve(); addConstructorCallSymbol(); } Index: AST.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/AST.java,v retrieving revision 1.56 retrieving revision 1.57 diff -C2 -d -r1.56 -r1.57 *** AST.java 22 Dec 2003 01:30:59 -0000 1.56 --- AST.java 28 Feb 2004 14:23:43 -0000 1.57 *************** *** 130,136 **** Node.setModule(module); ! for (int i = 0; i < methodImplementations.length; i++) try{ ! methodImplementations[i].lateBuildScope(); } catch(UserError ex){ --- 130,136 ---- Node.setModule(module); ! for (int i = 0; i < methods.length; i++) try{ ! methods[i].typedResolve(); } catch(UserError ex){ *************** *** 138,144 **** } ! for (int i = 0; i < methods.length; i++) try{ ! methods[i].typedResolve(); } catch(UserError ex){ --- 138,144 ---- } ! for (int i = 0; i < methodImplementations.length; i++) try{ ! methodImplementations[i].lateBuildScope(); } catch(UserError ex){ |