[Nice-commit] Nice/src/bossa/syntax tools.nice,1.22,1.23 analyse.nice,1.79,1.80 TypeConstantExp.java
Brought to you by:
bonniot
From: <bo...@us...> - 2003-08-27 09:33:49
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv4636/src/bossa/syntax Modified Files: tools.nice analyse.nice TypeConstantExp.java MonoSymbol.java Log Message: Dynamic type inference for instanceof. Index: tools.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** tools.nice 27 May 2003 21:07:53 -0000 1.22 --- tools.nice 27 Aug 2003 09:33:45 -0000 1.23 *************** *** 93,96 **** --- 93,126 ---- } + ?(MonoSymbol, TypeConstructor) getInstanceof(IfExp ifExp) + { + if (! (ifExp.condition instanceof CallExp)) + return null; + + CallExp condition = cast(ifExp.condition); + if (! condition.isCallTo("instanceof") || condition.arguments.size() != 2) + return null; + + Expression arg1 = condition.arguments.getExp(0); + if (! (arg1 instanceof SymbolExp)) + return null; + + SymbolExp symExp = cast(arg1); + if (! (symExp.getSymbol() instanceof MonoSymbol)) + return null; + + MonoSymbol sym = cast(symExp.getSymbol()); + + Expression arg2 = condition.arguments.getExp(1); + if (! (arg2 instanceof TypeConstantExp)) + return null; + + TypeConstantExp type = cast(arg2); + if (type.representedType == null) + return null; + + return (sym, notNull(type.representedType)); + } + /**************************************************************** * Temporary: this should be solved when AST classes are written Index: analyse.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/analyse.nice,v retrieving revision 1.79 retrieving revision 1.80 diff -C2 -d -r1.79 -r1.80 *** analyse.nice 24 Aug 2003 18:09:53 -0000 1.79 --- analyse.nice 27 Aug 2003 09:33:45 -0000 1.80 *************** *** 174,178 **** } ! void addVar(MonoSymbol symbol) { ?Monotype type = symbol.syntacticType; --- 174,178 ---- } ! void addVar(MonoSymbol symbol, boolean redefining = false) { ?Monotype type = symbol.syntacticType; *************** *** 183,187 **** throw error(symbol, "A variable cannot have a void type"); } ! this.checkNotDefined(symbol); this.vars[symbol.name.toString()] = symbol; } --- 183,188 ---- throw error(symbol, "A variable cannot have a void type"); } ! if (! redefining) ! this.checkNotDefined(symbol); this.vars[symbol.name.toString()] = symbol; } *************** *** 478,484 **** --- 479,513 ---- info.beginCases(); + + ?(MonoSymbol, TypeConstructor) test = getInstanceof(e); + + if (test != null) + { + (MonoSymbol variable, TypeConstructor tc) = test; + mlsub.typing.Monotype[?] parameters = null; + if (tc.arity() == 0) + parameters = null; + else if (variable.getMonotype() instanceof mlsub.typing.MonotypeConstructor) + { + mlsub.typing.MonotypeConstructor mc = cast(variable.getMonotype()); + parameters = mc.getTP(); + } + else + test = null; + + if (test != null) + { + mlsub.typing.Monotype type = Monotype.sure(new mlsub.typing.MonotypeConstructor(tc, parameters)); + info.begin(); + info.addVar(MonoSymbol.mock(variable, type), redefining: true); + } + } + e.thenExp = analyse(e.thenExp, info); e.thenUnreachable = info.getUnreachable(); + if (test != null) + info.end(); + info.otherCase(); e.elseExp = analyse(e.elseExp, info); *************** *** 555,558 **** --- 584,589 ---- e.value = object(type); + e.representedType = bossa.syntax.Node.getGlobalTypeScope(). + globalLookup(name.toString(), name.location()); return e; Index: TypeConstantExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/TypeConstantExp.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TypeConstantExp.java 21 Aug 2002 18:20:31 -0000 1.2 --- TypeConstantExp.java 27 Aug 2003 09:33:45 -0000 1.3 *************** *** 26,28 **** --- 26,30 ---- super(PrimitiveType.typeTC, name, name.toString(), name.location()); } + + mlsub.typing.TypeConstructor representedType; } Index: MonoSymbol.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MonoSymbol.java,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** MonoSymbol.java 26 Jul 2003 22:29:47 -0000 1.22 --- MonoSymbol.java 27 Aug 2003 09:33:45 -0000 1.23 *************** *** 161,163 **** --- 161,178 ---- Monotype syntacticType; mlsub.typing.Monotype type; + + /**************************************************************** + * Using a symbol instead of another when a more precise type is known. + ****************************************************************/ + + static MonoSymbol mock(final MonoSymbol origin, mlsub.typing.Monotype type) + { + return new MonoSymbol(origin.getName(), type) + { + gnu.expr.Declaration getDeclaration() + { + return origin.getDeclaration(); + } + }; + } } |