From: <vj...@us...> - 2009-10-25 02:40:46
|
Revision: 11721 http://x10.svn.sourceforge.net/x10/?rev=11721&view=rev Author: vj0 Date: 2009-10-25 02:40:37 +0000 (Sun, 25 Oct 2009) Log Message: ----------- Fixed type bounds for type parameters. Modified Paths: -------------- trunk/x10.compiler/.classpath trunk/x10.compiler/src/x10/ast/TypeParamNode.java trunk/x10.compiler/src/x10/ast/TypeParamNode_c.java trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java trunk/x10.compiler/src/x10/types/X10Context.java trunk/x10.compiler/src/x10/types/X10Context_c.java trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java trunk/x10.compiler/src/x10/types/X10TypeSystem.java trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java trunk/x10.compiler/src/x10/visit/X10PrettyPrinterVisitor.java Added Paths: ----------- trunk/x10.tests/examples/Constructs/Generics/Bounds7.x10 Modified: trunk/x10.compiler/.classpath =================================================================== --- trunk/x10.compiler/.classpath 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/.classpath 2009-10-25 02:40:37 UTC (rev 11721) @@ -1,8 +1,12 @@ -<?xml version="1.0" encoding="UTF-8"?> -<classpath> - <classpathentry kind="src" path="src"/> - <classpathentry kind="src" path="" including="data/**" excluding="src/"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> - <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> - <classpathentry kind="output" path="classes"/> -</classpath> +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry excluding="src/" including="data/**" kind="src" path=""/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry combineaccessrules="false" kind="src" path="/polyglot3"/> + <classpathentry combineaccessrules="false" kind="src" path="/x10.common"/> + <classpathentry combineaccessrules="false" kind="src" path="/x10.constraints"/> + <classpathentry combineaccessrules="false" kind="src" path="/x10.runtime"/> + <classpathentry kind="output" path="classes"/> +</classpath> Modified: trunk/x10.compiler/src/x10/ast/TypeParamNode.java =================================================================== --- trunk/x10.compiler/src/x10/ast/TypeParamNode.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/ast/TypeParamNode.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -8,6 +8,8 @@ package x10.ast; +import java.util.List; + import polyglot.ast.Id; import polyglot.ast.Term; import polyglot.types.Ref; @@ -22,4 +24,6 @@ TypeParamNode type(ParameterType type); public ParameterType.Variance variance(); public TypeParamNode variance(ParameterType.Variance variance); + + List<Type> upperBounds(); } Modified: trunk/x10.compiler/src/x10/ast/TypeParamNode_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/TypeParamNode_c.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/ast/TypeParamNode_c.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -19,6 +19,7 @@ import polyglot.types.Named; import polyglot.types.SemanticException; import polyglot.types.Types; +import polyglot.types.Type; import polyglot.util.CodeWriter; import polyglot.util.Position; import polyglot.visit.CFGBuilder; @@ -28,6 +29,8 @@ import x10.types.ParameterType; import x10.types.ParameterType_c; import x10.types.X10Context; +import x10.types.X10Context_c; +import x10.types.X10TypeEnv_c; import x10.types.X10TypeSystem; public class TypeParamNode_c extends Term_c implements TypeParamNode { @@ -112,4 +115,11 @@ public void prettyPrint(CodeWriter w, PrettyPrinter pp) { pp.print(this, name, w); } + public List<Type> upperBounds() { + Type type = type(); + X10TypeSystem ts = (X10TypeSystem) type.typeSystem(); + X10Context_c xc = (X10Context_c) ts.emptyContext(); + List<Type> results = new X10TypeEnv_c(xc).upperBounds(type, false); + return results; + } } Modified: trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -374,15 +374,14 @@ for (ParameterType t : type.typeParameters()) { xc.addNamed(t); } - /* vj TODO: This causes a loop. Need to investigate. - * DepParameterExpr v = classInvariant(); + DepParameterExpr v = classInvariant(); if (v != null) { Ref<TypeConstraint> tc = v.typeConstraint(); if (tc != null) { - xc.setCurrentTypeConstraint(tc.get()); + xc.setCurrentTypeConstraint(tc); } } - */ + // Now add this.location == currentLocation /* XConstrainedTerm placeTerm = xc.currentPlaceTerm().copy(); XRoot thisVar = type.thisVar(); @@ -477,41 +476,41 @@ // Add all the constraints on the supertypes into the invariant. c.setResolver(new Runnable() { - public void run() { - XConstraint x = new XConstraint_c(); - try { - if (ci != null) { - XConstraint xi = ci.valueConstraint().get(); - x.addIn(xi); - TypeConstraint ti = ci.typeConstraint().get(); - } - if (nn.superClass != null) { - Type t = nn.superClass.type(); - XConstraint tc = X10TypeMixin.xclause(t); - if (tc != null) - x.addIn(tc); - } - for (TypeNode tn : nn.interfaces) { - Type t = tn.type(); - XConstraint tc = X10TypeMixin.xclause(t); - if (tc != null) - x.addIn(tc); - } + public void run() { + XConstraint x = new XConstraint_c(); + try { + if (ci != null) { + XConstraint xi = ci.valueConstraint().get(); + x.addIn(xi); + TypeConstraint ti = ci.typeConstraint().get(); + } + if (nn.superClass != null) { + Type t = nn.superClass.type(); + XConstraint tc = X10TypeMixin.xclause(t); + if (tc != null) + x.addIn(tc); + } + for (TypeNode tn : nn.interfaces) { + Type t = tn.type(); + XConstraint tc = X10TypeMixin.xclause(t); + if (tc != null) + x.addIn(tc); + } + } + catch (XFailure e) { + x.setInconsistent(); + } + c.update(x); } - catch (XFailure e) { - x.setInconsistent(); - } - c.update(x); - } }); def.setClassInvariant(c); - final Ref<TypeConstraint> tc = new LazyRef_c<TypeConstraint>(new TypeConstraint_c()); + final LazyRef<TypeConstraint> tc = new LazyRef_c<TypeConstraint>(new TypeConstraint_c()); // Set the type bounds for the def. - c.setResolver(new Runnable() { + tc.setResolver(new Runnable() { public void run() { TypeConstraint x = new TypeConstraint_c(); Modified: trunk/x10.compiler/src/x10/types/X10Context.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10Context.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/types/X10Context.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -67,7 +67,7 @@ /** Current constraint on type variables in scope */ TypeConstraint currentTypeConstraint(); - void setCurrentTypeConstraint(TypeConstraint c); + void setCurrentTypeConstraint(Ref<TypeConstraint> c); /** * Looks up a property in the current scope. Modified: trunk/x10.compiler/src/x10/types/X10Context_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10Context_c.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/types/X10Context_c.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -296,12 +296,12 @@ return null; } - protected TypeConstraint currentTypeConstraint; + protected Ref<TypeConstraint> currentTypeConstraint; public TypeConstraint currentTypeConstraint() { if (currentTypeConstraint == null) return new TypeConstraint_c(); - return currentTypeConstraint; } - public void setCurrentTypeConstraint(TypeConstraint c) { + return currentTypeConstraint.get(); } + public void setCurrentTypeConstraint(Ref<TypeConstraint> c) { currentTypeConstraint = c; } Modified: trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -775,7 +775,7 @@ for (SubtypeConstraint tt : newEnv) { ec.addTerm(tt); } - xc2.setCurrentTypeConstraint(ec); + xc2.setCurrentTypeConstraint(Types.ref(ec)); X10TypeEnv_c tenv = copy(); tenv.context = xc2; @@ -1008,7 +1008,7 @@ for (SubtypeConstraint tt : newEnv) { ec.addTerm(tt); } - xc2.setCurrentTypeConstraint(ec); + xc2.setCurrentTypeConstraint(Types.ref(ec)); if (term.isEqualityConstraint()) { SubtypeConstraint eq = term; Modified: trunk/x10.compiler/src/x10/types/X10TypeSystem.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10TypeSystem.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/types/X10TypeSystem.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -427,4 +427,6 @@ XConstrainedTerm globalPlace(); boolean isX10Array(Type me); + + Context emptyContext(); } Modified: trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -287,7 +287,7 @@ } X10Context xc = (X10Context) context.pushBlock(); equals.addIn(xc.currentTypeConstraint()); - xc.setCurrentTypeConstraint(equals); + xc.setCurrentTypeConstraint(Types.ref(equals)); if (! consistent(c, xc)) return false; } @@ -2262,8 +2262,9 @@ X10MethodMatcher m = (X10MethodMatcher) matcher; List<MethodInstance> candidates = new ArrayList<MethodInstance>(); - - for (Type t : env(matcher.context()).upperBounds(container, true)) { + + List<Type> types = env(matcher.context()).upperBounds(container, true); + for (Type t : types) { List<MethodInstance> ms = super.findAcceptableMethods(t, matcher); candidates.addAll(ms); } Modified: trunk/x10.compiler/src/x10/visit/X10PrettyPrinterVisitor.java =================================================================== --- trunk/x10.compiler/src/x10/visit/X10PrettyPrinterVisitor.java 2009-10-25 02:30:33 UTC (rev 11720) +++ trunk/x10.compiler/src/x10/visit/X10PrettyPrinterVisitor.java 2009-10-25 02:40:37 UTC (rev 11721) @@ -527,6 +527,12 @@ for (TypeParamNode tp : n.typeParameters()) { w.write(sep); n.print(tp, w, tr); + List<Type> sups = tp.upperBounds(); + if (sups.size() > 0) { + //TODO: Decide what to do with multiple upper bounds. Not sure how Java will handle this. + w.write(" extends "); + er.printType(sups.get(0), PRINT_TYPE_PARAMS | NO_VARIANCE); + } sep = ", "; } w.end(); Added: trunk/x10.tests/examples/Constructs/Generics/Bounds7.x10 =================================================================== --- trunk/x10.tests/examples/Constructs/Generics/Bounds7.x10 (rev 0) +++ trunk/x10.tests/examples/Constructs/Generics/Bounds7.x10 2009-10-25 02:40:37 UTC (rev 11721) @@ -0,0 +1,32 @@ +/* + * + * (C) Copyright IBM Corporation 2008 + * + * This file is part of X10 Test. + * + */ +import harness.x10Test; + +/** + * Test type parameter bounds. + * + * @author nystrom 8/2008 + */ +public class Bounds7 extends x10Test { + + interface Sum { + def sum():int; + } + + public class Test[T]{T <: Sum} { + def sum(a:T) = a.sum(); + + } + + public def run() = true; + public static def main( Rail[String]) { + new Bounds7().execute(); + } +} + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |