[Nice-commit] Nice/src/bossa/syntax alternative.nice,1.4,1.5 compilation.nice,1.2,1.3 dispatch.java.
Brought to you by:
bonniot
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19437/F:/nice/src/bossa/syntax Modified Files: alternative.nice compilation.nice dispatch.java.bootstrap methodImplementation.nice methodbody.nice pattern.nice tools.nice Removed Files: Pattern.java Log Message: Converted Pattern. Index: methodbody.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/methodbody.nice,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** methodbody.nice 20 Dec 2004 16:05:18 -0000 1.6 --- methodbody.nice 30 Dec 2004 18:49:01 -0000 1.7 *************** *** 71,76 **** return null; ! ?mlsub.typing.TypeConstructor[] additionalTags = cast(Pattern.getAdditionalTC(formals)); ! boolean hasAdditionalTags = additionalTags.any( ? TypeConstructor tc => tc != null); // Try to remember what caused the error when no symbol could be found. --- 71,76 ---- return null; ! ?mlsub.typing.TypeConstructor[] additionalTags = formals.mapToArray(Pattern p => p.tc2); ! boolean hasAdditionalTags = additionalTags.any(?TypeConstructor tc => tc != null); // Try to remember what caused the error when no symbol could be found. *************** *** 251,255 **** //Resolution of the body is delayed to enable overloading ! Pattern.resolve(notNull(typeScope), Node.getGlobalScope(), formals); symbols = notNull(scope).lookup(name); --- 251,255 ---- //Resolution of the body is delayed to enable overloading ! resolvePatterns(notNull(typeScope), Node.getGlobalScope(), formals); symbols = notNull(scope).lookup(name); *************** *** 258,262 **** void lateBuildScope() { ! Pattern.resolveValues(formals); let s = this.findSymbol(notNull(symbols)); --- 258,262 ---- void lateBuildScope() { ! resolvePatternValues(formals); let s = this.findSymbol(notNull(symbols)); *************** *** 384,388 **** // The arguments are specialized by the patterns try { ! Pattern.in(monotypes, formals); } catch(TypingEx e){ throw User.error(name,"The patterns are not correct", e); --- 384,388 ---- // The arguments are specialized by the patterns try { ! monotypes.inPattern(formals); } catch(TypingEx e){ throw User.error(name,"The patterns are not correct", e); --- Pattern.java DELETED --- Index: methodImplementation.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/methodImplementation.nice,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** methodImplementation.nice 29 Dec 2004 23:10:48 -0000 1.3 --- methodImplementation.nice 30 Dec 2004 18:49:01 -0000 1.4 *************** *** 189,193 **** notNull(compiledMethod).addBytecodeAttribute (new gnu.bytecode.MiscAttr("patterns", ! Pattern.bytecodeRepresentation(formals).getBytes())); } --- 189,193 ---- notNull(compiledMethod).addBytecodeAttribute (new gnu.bytecode.MiscAttr("patterns", ! bytecodeRepresentation(formals).getBytes())); } Index: pattern.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/pattern.nice,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** pattern.nice 20 Dec 2004 16:05:18 -0000 1.13 --- pattern.nice 30 Dec 2004 18:49:01 -0000 1.14 *************** *** 15,21 **** import bossa.util.*; public Pattern createPattern(LocatedString name) { ! return new VariablePattern(name, name.location); } --- 15,249 ---- import bossa.util.*; + /** + Represents the information about one argument of a method body. + + @see MethodBodyDefinition + */ + public abstract class Pattern implements Located + { + ?LocatedString name = null; + ?TypeIdent typeConstructor= null; + private ?TypeIdent additional = null; + ?mlsub.typing.TypeConstructor tc = null; + ?mlsub.typing.TypeConstructor tc2 = null; + ?mlsub.typing.Interface itf2 = null; + + // The class constraint verified by this pattern. + private ?mlsub.typing.Constraint constraint = null; + // The type of the class matched, if it is constrained. + private ?mlsub.typing.Monotype patternType = null; + + private Location loc; + + public ?mlsub.typing.TypeConstructor getTC() = tc; + + ?mlsub.typing.TypeConstructor getRuntimeTC() = null; + + void resolveTC(TypeScope scope) + { + if (typeConstructor != null) + { + TypeSymbol sym = notNull(typeConstructor).resolveToTypeSymbol(scope); + + if (sym instanceof mlsub.typing.TypeConstructor) + tc = sym; + else + throw User.error(this, notNull(typeConstructor) + + " is not a declared class or interface"); + + if (this.exactlyAtType() && !instantiableTC(notNull(tc))) + User.error + (notNull(typeConstructor).location(), + "Pattern #" + notNull(typeConstructor) + + " cannot be matched because interfaces and abstract classes do not have direct instances."); + + typeConstructor = null; + } + if (additional != null) + { + TypeSymbol sym = notNull(additional).resolveToTypeSymbol(scope); + + if (sym instanceof mlsub.typing.TypeConstructor) + tc2 = sym; + else if (sym instanceof mlsub.typing.Interface) + itf2 = sym; + else + User.error(additional, + notNull(additional) + " should be a class or an interface"); + + additional = null; + } + + // Class constraints + if (tc == null) + return; + + let def = getTypeDefinition(notNull(tc)); + if (def == null) + return; + + let classType = def.getConstrainedType(); + if (classType != null) + { + constraint = classType.getConstraint(); + patternType = classType.getMonotype(); + } + } + + /** + Asserts that m is below this pattern. + */ + private void leq(mlsub.typing.Monotype m) + { + nice.tools.typing.Types.setMarkedKind(m); + m = m.equivalent(); + if (!(m instanceof mlsub.typing.MonotypeConstructor)) + throw Internal.error("Nullness check"); + + mlsub.typing.MonotypeConstructor mc = m; + + if (this.atNull()) + mlsub.typing.Typing.leq(nice.tools.typing.PrimitiveType.maybeTC, mc.getTC()); + + if (tc == null) + return; + + // the argument is not null + mlsub.typing.Typing.leq(mc.getTC(), nice.tools.typing.PrimitiveType.sureTC); + mlsub.typing.Monotype type = nice.tools.typing.Types.rawType(mc); + + if (constraint != null) + { + notNull(constraint).enter(); + mlsub.typing.Typing.leq(type, patternType); + if (this.exactlyAtType()) + { + mlsub.typing.Typing.leq(patternType, type); + mlsub.typing.MonotypeConstructor inner = cast(type.equivalent()); + inner.getTC().setMinimal(); + } + } + else + { + mlsub.typing.Typing.leq(type, tc); + if (this.exactlyAtType()) + { + mlsub.typing.Typing.leq(tc, type); + mlsub.typing.MonotypeConstructor inner = cast(type.equivalent()); + inner.getTC().setMinimal(); + } + } + } + + void inDomain(mlsub.typing.Monotype type) + { + nice.tools.typing.Types.setMarkedKind(type); + + if (this.atNull()) + mlsub.typing.Typing.leq(nice.tools.typing.PrimitiveType.maybeTC, type.head()); + + mlsub.typing.Monotype rawType = nice.tools.typing.Types.rawType(type); + + if (constraint != null) + { + notNull(constraint).enter(); + mlsub.typing.Typing.leq(patternType, rawType); + } + else + { + mlsub.typing.Typing.leq(tc, rawType); + } + + if (tc2 != null) + mlsub.typing.Typing.leq(tc2, rawType); + else if (itf2 != null) + mlsub.typing.Typing.assertImp(rawType.head(), itf2, false); + } + + public boolean matches(mlsub.typing.TypeConstructor tag); + + public boolean matchesValue(ConstantExp val); + + public Pattern setDomainEq(boolean equal) + { + return this; + } + + public void setDomainTC(?mlsub.typing.TypeConstructor domaintc) {} + + public void addValues(List<ConstantExp> values) {} + + public ?LocatedString getName() = name; + + location() = loc; + + /** + * Returns a string used to recognize this pattern in the bytecode. + * for the different patterns the following signatures are used: + * any : `@_` + * null : `@NULL` + * type : `@` + type where `.` in type are replaced by `$` + * exactly at type : `#` + type idem + * integer literal : `@-` / `@+` + int_literal + * char literal : `@'c'` where c is an unescaped char + * string literal : `@"string"` where string is an escaped string + * reference : `@` + globalconstantname + * int comparison : `@>` / `@>=` / `@<` / `@<=` + int_literal + * + * This is usefull to distinguish alternatives of a method. + */ + public String bytecodeRepresentation(); + + /** + Returns code that tests if the parameter is matched. + + @param dispatchMade indicates that dispatch has already occured. It is + still necessary to check for exact matching if applicable. + */ + public gnu.expr.Expression matchTest(gnu.expr.Expression parameter, boolean dispatchMade); + + public boolean atAny() = false; + public boolean atNull() = false; + boolean exactlyAtType() = false; + public boolean atValue() = false; + public boolean atTypeMatchingValue() = false; + } + + void resolvePatterns(TypeScope tscope, VarScope vscope, Pattern[] patterns) + { + for (int i = 0; i < patterns.length; i++) + patterns[i].resolveTC(tscope); + } + + void resolvePatternValues(Pattern[] patterns) + { + for (int i = 0; i < patterns.length; i++) + patterns[i] = resolveGlobalConstants(patterns[i]); + } + + /** + Assert that the monotypes belong the the patterns. + */ + void inPattern(mlsub.typing.Monotype[] monotypes, Pattern[] patterns) + { + for (int i = 0; i < monotypes.length; i++) + patterns[i].leq(monotypes[i]); + } + + + public String bytecodeRepresentation(Pattern[] patterns) + { + StringBuffer res = new StringBuffer(); + for (int i = 0; i < patterns.length; i++) + res.append(patterns[i].bytecodeRepresentation()); + + return res.toString(); + } + + public class UnknownPattern extends RuntimeException {} + public Pattern createPattern(LocatedString name) { ! return new VariablePattern(name: name, loc: notNull(name.location())); } *************** *** 23,29 **** { if (ti.toString().equals("Object")) ! return new NotNullPattern(name, nice.tools.typing.PrimitiveType.sureTC, name.location); ! return new TypePattern(name, ti, null, name.location(), exactlyAt: false); } --- 251,257 ---- { if (ti.toString().equals("Object")) ! return new NotNullPattern(name: name, tc: nice.tools.typing.PrimitiveType.sureTC, loc: notNull(name.location)); ! return new TypePattern(name: name, typeConstructor: ti, loc: notNull(name.location()), exactlyAt: false); } *************** *** 31,35 **** public Pattern createPattern(?LocatedString name, ?mlsub.typing.TypeConstructor tc, boolean sure) { // TODO: don't generate NotNullPatterns if the overriden domain is sure. ! let loc = (name != null) ? name.location() : Location.nowhere(); if (nice.tools.typing.Types.isPrimitive(tc)) --- 259,263 ---- public Pattern createPattern(?LocatedString name, ?mlsub.typing.TypeConstructor tc, boolean sure) { // TODO: don't generate NotNullPatterns if the overriden domain is sure. ! let loc = (name != null) ? notNull(name.location()) : Location.nowhere(); if (nice.tools.typing.Types.isPrimitive(tc)) *************** *** 37,46 **** if (sure && tc == null) ! return new NotNullPattern(name, nice.tools.typing.PrimitiveType.sureTC, loc); if (tc == null) ! return new VariablePattern(name, loc); ! return new TypePattern(name, tc, loc, exactlyAt: false); } --- 265,274 ---- if (sure && tc == null) ! return new NotNullPattern(name: name, tc: nice.tools.typing.PrimitiveType.sureTC, loc: loc); if (tc == null) ! return new VariablePattern(name: name, loc: loc); ! return new TypePattern(name: name, tc: tc, loc: loc, exactlyAt: false); } *************** *** 48,54 **** { if (ti.toString().equals("Object")) ! return new NotNullPattern(name, nice.tools.typing.PrimitiveType.sureTC, ti.location); ! return new TypePattern(name, ti, additional, ti.location(), exactlyAt: exactly, runtimeTC: runtimeTC); } --- 276,282 ---- { if (ti.toString().equals("Object")) ! return new NotNullPattern(name: name, tc: nice.tools.typing.PrimitiveType.sureTC, loc: ti.location); ! return new TypePattern(name: name, typeConstructor: ti, additional: additional, loc: ti.location(), exactlyAt: exactly, runtimeTC: runtimeTC); } *************** *** 61,77 **** { if (value instanceof NullExp) ! return new NullPattern(value.tc, additional, value.location()); if (value instanceof StringConstantExp) ! return new StringPattern(null, new TypeIdent(name: new LocatedString("java.lang.String", value.location())), ! additional, value.location(), atValue: value); if (value.tc == nice.tools.typing.PrimitiveType.boolTC) ! return new BoolPattern(value.tc, additional, value.location(), atValue: value); if (value.tc == nice.tools.typing.PrimitiveType.charTC) ! return new CharPattern(value.tc, additional, value.location(), atValue: value); ! return new IntPattern(value.tc, additional, value.location(), atValue: value); } --- 289,305 ---- { if (value instanceof NullExp) ! return new NullPattern(tc: value.tc, additional: additional, loc: value.location()); if (value instanceof StringConstantExp) ! return new StringPattern(typeConstructor: new TypeIdent(name: new LocatedString("java.lang.String", value.location())), ! additional: additional, loc: value.location(), atValue: value); if (value.tc == nice.tools.typing.PrimitiveType.boolTC) ! return new BoolPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); if (value.tc == nice.tools.typing.PrimitiveType.charTC) ! return new CharPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); ! return new IntPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); } *************** *** 89,98 **** let tc = (value != null) ? value.tc : null; ! return new IntComparePattern(name, tc, loc, atValue: cast(value), kind: k, refName: refName); } Pattern createPattern(mlsub.typing.TypeConstructor tc) { ! return new TypePattern(null, tc, null, exactlyAt: false); } --- 317,326 ---- let tc = (value != null) ? value.tc : null; ! return new IntComparePattern(name: name, tc: tc, loc: loc, atValue: cast(value), kind: k, refName: refName); } Pattern createPattern(mlsub.typing.TypeConstructor tc) { ! return new TypePattern(tc: tc, loc: Location.nowhere(), exactlyAt: false); } *************** *** 122,126 **** // only set it to atAny if it's a @type pattern if (equal && !exactlyAt) ! return new VariablePattern(name, this.location); // don't allow integer primitive types in @type and #type patterns --- 350,354 ---- // only set it to atAny if it's a @type pattern if (equal && !exactlyAt) ! return new VariablePattern(name: name, loc: this.location); // don't allow integer primitive types in @type and #type patterns *************** *** 509,515 **** { NewExp val = cast(symbol.getValue()); ! return new EnumPattern(pattern.name, val.tc, pattern.location, atValue: new ConstantExp(null, pattern.tc, symbol, ! notNull(pattern.name).toString(), pattern.location)); } --- 737,743 ---- { NewExp val = cast(symbol.getValue()); ! return new EnumPattern(name: pattern.name, tc: val.tc, loc: pattern.location(), atValue: new ConstantExp(null, pattern.tc, symbol, ! notNull(pattern.name).toString(), pattern.location())); } *************** *** 532,538 **** { NewExp val = cast(symbol.getValue()); ! return new ReferencePattern(pattern.name, val.tc, pattern.location, atValue: new ConstantExp(null, pattern.tc, symbol, ! notNull(pattern.name).toString(), pattern.location)); } else --- 760,766 ---- { NewExp val = cast(symbol.getValue()); ! return new ReferencePattern(name: pattern.name, tc: val.tc, loc: pattern.location(), atValue: new ConstantExp(null, pattern.tc, symbol, ! notNull(pattern.name).toString(), pattern.location())); } else *************** *** 619,623 **** if (name[0] == '=') ! return resolveGlobalConstants(new VariablePattern(new LocatedString(name.substring(1)), loc)); } --- 847,851 ---- if (name[0] == '=') ! return resolveGlobalConstants(new VariablePattern(name: new LocatedString(name.substring(1)), loc: loc)); } *************** *** 626,630 **** if (name.equals("NONNULL")) ! return new NotNullPattern(null, nice.tools.typing.PrimitiveType.sureTC, loc); if (name.equals("NULL")) --- 854,858 ---- if (name.equals("NONNULL")) ! return new NotNullPattern(tc: nice.tools.typing.PrimitiveType.sureTC, loc: loc); if (name.equals("NULL")) *************** *** 639,645 **** // This can happen if the class exists only in a later version // of the JDK. ! throw new Pattern.Unknown(); let mlsub.typing.TypeConstructor tc = cast(sym); ! return new TypePattern(null, tc, Location.nowhere(), exactlyAt:exact); } --- 867,873 ---- // This can happen if the class exists only in a later version // of the JDK. ! throw new UnknownPattern(); let mlsub.typing.TypeConstructor tc = cast(sym); ! return new TypePattern(tc: tc, loc: Location.nowhere(), exactlyAt: exact); } Index: tools.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v retrieving revision 1.85 retrieving revision 1.86 diff -C2 -d -r1.85 -r1.86 *** tools.nice 29 Dec 2004 23:10:48 -0000 1.85 --- tools.nice 30 Dec 2004 18:49:01 -0000 1.86 *************** *** 213,220 **** <T> String map(String, String, String, T[]) = native String bossa.util.Util.map(String, String, String, Object[]); <T> String map(String, String, String, ?Collection<T>) = native String bossa.util.Util.map(String, String, String, Collection); - void addValues(Pattern, List<ConstantExp>) = native void Pattern.addValues(List); - ?mlsub.typing.TypeConstructor getRuntimeTC(Pattern) = native mlsub.typing.TypeConstructor Pattern.getRuntimeTC(); - boolean matchesValue(Pattern, ConstantExp) = native boolean Pattern.matchesValue(ConstantExp); - ?LocatedString getName(Pattern) = native LocatedString Pattern.getName(); ?mlsub.typing.lowlevel.Kind getKind(mlsub.typing.lowlevel.Element) = native mlsub.typing.lowlevel.Kind mlsub.typing.lowlevel.Element.getKind(); ?gnu.expr.Declaration getDeclaration(Expression) = native gnu.expr.Declaration Expression.getDeclaration(); --- 213,216 ---- Index: compilation.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/compilation.nice,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** compilation.nice 29 Dec 2004 23:10:48 -0000 1.2 --- compilation.nice 30 Dec 2004 18:49:01 -0000 1.3 *************** *** 141,147 **** private NiceClass declaringClass(JavaMethod m, Alternative alt) { ! mlsub.typing.TypeConstructor firstArgument = alt.getPatterns()[0].getTC(); ! let def = getTypeDefinition(firstArgument); ! if (def != null && def.getImplementation() instanceof NiceClass) --- 141,146 ---- private NiceClass declaringClass(JavaMethod m, Alternative alt) { ! ?mlsub.typing.TypeConstructor firstArgument = alt.getPatterns()[0].getTC(); ! let def = firstArgument==null ? null : getTypeDefinition(firstArgument); if (def != null && def.getImplementation() instanceof NiceClass) Index: dispatch.java.bootstrap =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/dispatch.java.bootstrap,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** dispatch.java.bootstrap 29 Dec 2004 23:10:48 -0000 1.47 --- dispatch.java.bootstrap 30 Dec 2004 18:49:01 -0000 1.48 *************** *** 60,78 **** { return null; } - static Pattern resolveGlobalConstants(Pattern pattern) - { return null; } - - public static Pattern readPattern(String rep, int[] pos) - { return null; } - - public static Pattern createPattern(LocatedString name, mlsub.typing.TypeConstructor tc, boolean sure) - { return null; } - - public static boolean leq(Pattern a, Pattern b) - { return false; } - - public static boolean disjoint(Pattern a, Pattern b) - { return false; } - static Expression analyse(Expression e, VarScope v, TypeScope t) { return null; } --- 60,63 ---- Index: alternative.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/alternative.nice,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** alternative.nice 29 Dec 2004 23:10:48 -0000 1.4 --- alternative.nice 30 Dec 2004 18:49:01 -0000 1.5 *************** *** 421,425 **** (fullName, MethodDeclaration.methodListSeparator)); } ! catch(Pattern.Unknown ex) { // This can happen if the class exists only in a later version // of the JDK. We just ignore this alternative. --- 421,425 ---- (fullName, MethodDeclaration.methodListSeparator)); } ! catch(UnknownPattern ex) { // This can happen if the class exists only in a later version // of the JDK. We just ignore this alternative. |