From: <vj...@us...> - 2010-01-16 16:28:44
|
Revision: 12513 http://x10.svn.sourceforge.net/x10/?rev=12513&view=rev Author: vj0 Date: 2010-01-12 12:14:36 +0000 (Tue, 12 Jan 2010) Log Message: ----------- Fixes XTENLANG-756. -- A one-line mistake in place-checking code that meant that place checks were not performed on receivers of interface types. This caused a number of place-type bugs to surface in XRX. Fixed them per my understanding -- needs to be reviewed by Olivier. -- Fixed X10TypeEnv_c.typeBounds so that it is aligned with the new type system. The includeObject flag should be ignored. Every type parameter now takes Any as an upper bound. Fixes a traceback that arose because of a ClassCastException when processing an ambiguous proto types. Proto is handled as a flag. This flag needs to be recorded on the ambiguous type, and then communicated to the resolveed top. Fixed a bug in proto type checking so that proto return types for constructors are now permitted. Restructured closure code -- moved various AST generators and type generators into x10.util.ClosureSynthesizer. Started to document the surprisingly brittle code for processing closures. Needs to be cleaned up and redesigned. Fixed home processing in Desugarer to align with recent changes to XRX which broke this piece of code. This now breaks the C++ backend -- apparently home() is not defined yet on structs? (Should be easy to fix.) Fixed inscrutable error message that was triggered when processing a non-static typedef. Fixed closure types toString so that the annoying "<anonymous subtype of ...>" is replaced by "...". Modified Paths: -------------- trunk/x10.compiler/src/x10/ast/AmbMacroTypeNode_c.java trunk/x10.compiler/src/x10/ast/Closure.java trunk/x10.compiler/src/x10/ast/Closure_c.java trunk/x10.compiler/src/x10/ast/ForLoop_c.java trunk/x10.compiler/src/x10/ast/PlacedClosure_c.java trunk/x10.compiler/src/x10/ast/X10AmbTypeNode_c.java trunk/x10.compiler/src/x10/ast/X10Call_c.java trunk/x10.compiler/src/x10/ast/X10CanonicalTypeNode.java trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java trunk/x10.compiler/src/x10/ast/X10FieldAssign_c.java trunk/x10.compiler/src/x10/ast/X10New_c.java trunk/x10.compiler/src/x10/types/ClosureDef.java trunk/x10.compiler/src/x10/types/ClosureType_c.java trunk/x10.compiler/src/x10/types/TypeDefAsMacroTypeTransform.java trunk/x10.compiler/src/x10/types/X10ClassDef_c.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/util/Synthesizer.java trunk/x10.compiler/src/x10/visit/Desugarer.java trunk/x10.compiler/src/x10cpp/visit/MessagePassingCodeGenerator.java trunk/x10.runtime/src-x10/x10/io/ByteWriter.x10 trunk/x10.runtime/src-x10/x10/lang/Equals.x10 trunk/x10.runtime/src-x10/x10/util/AbstractCollection.x10 trunk/x10.runtime/src-x10/x10/util/Collection.x10 trunk/x10.runtime/src-x10/x10/util/DistributedRail.x10 trunk/x10.runtime/src-x10/x10/util/MapIterator.x10 trunk/x10.runtime/src-x10/x10/util/MapSet.x10 Added Paths: ----------- trunk/x10.compiler/src/x10/util/ClosureSynthesizer.java Property Changed: ---------------- trunk/x10.compiler/src/x10/ast/ Property changes on: trunk/x10.compiler/src/x10/ast ___________________________________________________________________ Modified: svn:ignore - *.local + *.local X10Loop_cprs.xml X10Loop_csym.xml X10Loop_c.l Modified: trunk/x10.compiler/src/x10/ast/AmbMacroTypeNode_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/AmbMacroTypeNode_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/AmbMacroTypeNode_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -23,6 +23,7 @@ import polyglot.ast.TypeNode_c; import polyglot.frontend.Globals; import polyglot.frontend.Goal; +import polyglot.types.Flags; import polyglot.types.LazyRef; import polyglot.types.LocalDef; import polyglot.types.Name; @@ -30,6 +31,7 @@ import polyglot.types.Ref; import polyglot.types.SemanticException; import polyglot.types.Type; +import polyglot.types.Types; import polyglot.types.UnknownType; import polyglot.util.CodeWriter; import polyglot.util.CollectionUtil; @@ -52,11 +54,12 @@ import x10.types.X10ClassType; import x10.types.X10Context; import x10.types.X10ParsedClassType; +import x10.types.X10Type; import x10.types.X10TypeMixin; import x10.types.X10TypeSystem; -public class AmbMacroTypeNode_c extends TypeNode_c implements AmbMacroTypeNode { +public class AmbMacroTypeNode_c extends TypeNode_c implements AmbMacroTypeNode, AddFlags { protected Prefix prefix; protected Id name; @@ -104,6 +107,10 @@ public List<Expr> args() { return this.args; } + Flags flags; + public void addFlags(Flags f) { + this.flags = f; + } public AmbMacroTypeNode args(List<Expr> args) { AmbMacroTypeNode_c n = (AmbMacroTypeNode_c) copy(); n.args = TypedList.copyAndCheck(args, Expr.class, true); @@ -350,7 +357,11 @@ } } } - + if (n.flags != null) { + t = ((X10Type) t).setFlags(flags); + n.flags = null; + } + // Update the symbol with the base type so that if we try to get the type while checking the constraint, we don't get a cyclic // dependency error, but instead get a less precise type. sym.update(t); @@ -404,12 +415,14 @@ } Node postprocess(CanonicalTypeNode result, AmbMacroTypeNode_c n, ContextVisitor childtc) throws SemanticException { - n = (AmbMacroTypeNode_c) X10Del_c.visitAnnotations(n, childtc); - - result = (CanonicalTypeNode) ((X10Del) result.del()).annotations(((X10Del) n.del()).annotations()); - result = (CanonicalTypeNode) ((X10Del) result.del()).setComment(((X10Del) n.del()).comment()); + n = (AmbMacroTypeNode_c) X10Del_c.visitAnnotations(n, childtc); - return result.del().typeCheck(childtc); + result = (CanonicalTypeNode) ((X10Del) result.del()).annotations(((X10Del) n.del()).annotations()); + result = (CanonicalTypeNode) ((X10Del) result.del()).setComment(((X10Del) n.del()).comment()); + result = (CanonicalTypeNode) result.del().typeCheck(childtc); + + + return result; } public Node exceptionCheck(ExceptionChecker ec) throws SemanticException { Modified: trunk/x10.compiler/src/x10/ast/Closure.java =================================================================== --- trunk/x10.compiler/src/x10/ast/Closure.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/Closure.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -20,6 +20,12 @@ import polyglot.util.Position; import x10.types.ClosureDef; +/** + * A Closure AST node represents a closure literal in the source text. Its type is a Closuredef. + * + * @author vj + * + */ public interface Closure extends Expr, CodeBlock { // List<TypeParamNode> typeParameters(); Modified: trunk/x10.compiler/src/x10/ast/Closure_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/Closure_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/Closure_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -63,7 +63,18 @@ import x10.types.X10MemberDef; import x10.types.X10TypeMixin; import x10.types.X10TypeSystem; +import x10.types.X10TypeSystem_c; +import x10.util.ClosureSynthesizer; +/** + * An implementation of a closure literal in the source text. + * + * It has associated with it a ClosurDef. + * + * Its type is an anonymous class that implements the Fun_m_n synthesized interface associated with ClosureDef. + * @author vj + * + */ public class Closure_c extends Expr_c implements Closure { // List<TypeParamNode> typeParameters; List<Formal> formals; @@ -364,7 +375,7 @@ @Override public Node typeCheck(ContextVisitor tc) throws SemanticException { - X10TypeSystem x10ts = (X10TypeSystem) tc.typeSystem(); + X10TypeSystem_c xts = (X10TypeSystem_c) tc.typeSystem(); Context c = tc.context(); Closure_c n = this; @@ -374,7 +385,7 @@ Type t = tn.type(); if (! t.isThrowable()) { throw new SemanticException("Type \"" + t + - "\" is not a subclass of \"" + x10ts.Throwable() + "\".", + "\" is not a subclass of \"" + xts.Throwable() + "\".", tn.position()); } } @@ -383,20 +394,18 @@ NodeFactory nf = tc.nodeFactory(); TypeSystem ts = tc.typeSystem(); // Body had no return statement. Set to void. - Type t; - if (! (((Ref<Type>) n.returnType().typeRef()).getCached() instanceof UnknownType)) { - t = ((Ref<Type>) n.returnType().typeRef()).getCached(); - } - else { + Ref<Type> tr=((Ref<Type>) n.returnType().typeRef()); + Type t = tr.getCached(); + if (t instanceof UnknownType) { t = ts.Void(); } - ((Ref<Type>) n.returnType().typeRef()).update(t); + tr.update(t); n = (Closure_c) n.returnType(nf.CanonicalTypeNode(n.returnType().position(), t)); } // Create an anonymous subclass of the closure type. ClosureDef def = n.closureDef; - ClassDef cd = x10ts.closureAnonymousClassDef(def); + ClassDef cd = ClosureSynthesizer.closureAnonymousClassDef(xts, def); return n.type(cd.asType()); } @@ -493,7 +502,7 @@ if (iter.hasNext()) sb.append(", "); } sb.append(")"); - sb.append(guard); + sb.append(guard==null?"{}":guard); sb.append(": "); sb.append(returnType.toString()); if (throwTypes.size() > 0) { Modified: trunk/x10.compiler/src/x10/ast/ForLoop_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/ForLoop_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/ForLoop_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -69,6 +69,9 @@ return succs; } + public Node typeCheck(TypeChecker tc) throws SemanticException { + return super.typeCheck(tc); + } /** Type check the statement. */ // public Node typeCheck(TypeChecker tc) throws SemanticException { // ForLoop_c n = (ForLoop_c) super.typeCheck(tc); Modified: trunk/x10.compiler/src/x10/ast/PlacedClosure_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/PlacedClosure_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/PlacedClosure_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -99,8 +99,7 @@ public static XConstrainedTerm computePlaceTerm( Expr place, X10Context xc, X10TypeSystem ts ) throws SemanticException { - //System.err.println("PlacedClosure_c: Golden! evaluating placeterm for " + this); - Type placeType = place.type(); + Type placeType = place.type(); XConstraint d = X10TypeMixin.xclause(placeType); d = (d==null) ? new XConstraint_c() : d.copy(); XConstraint pc = null; @@ -120,7 +119,7 @@ term + " and constraint " + d + "."); } } else { - boolean placeIsRef = ts.isImplicitCastValid(placeType, ts.Object(), xc); + boolean placeIsRef = true; // ts.isImplicitCastValid(placeType, ts.Object(), xc); if (placeIsRef) { XTerm src = ts.xtypeTranslator().trans(pc, place, xc); if (src == null) { Modified: trunk/x10.compiler/src/x10/ast/X10AmbTypeNode_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10AmbTypeNode_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/X10AmbTypeNode_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -139,7 +139,7 @@ } } else if (prefix instanceof Expr) { - throw new InternalCompilerError("non-static type members not implemented", pos); + throw new SemanticException("Non-static type members not implemented: " + prefix + " cannot be understood.", pos); } if (typeDefContainer != null) { Modified: trunk/x10.compiler/src/x10/ast/X10Call_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10Call_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/X10Call_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -587,8 +587,9 @@ Receiver target = target(); - // Method invocations on structs and values are always permitted - if (! ts.isSubtype(target.type(), ts.Object(), xc)) + // Method invocations on structs are always permitted + if (ts.isStructType(target.type())) + //if (! (ts.isSubtype(target.type(), ts.Object(), xc) || ts.isInterfaceType(target.type()))) return; Modified: trunk/x10.compiler/src/x10/ast/X10CanonicalTypeNode.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10CanonicalTypeNode.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/X10CanonicalTypeNode.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -10,7 +10,7 @@ import polyglot.ast.CanonicalTypeNode; -public interface X10CanonicalTypeNode extends CanonicalTypeNode { +public interface X10CanonicalTypeNode extends CanonicalTypeNode, AddFlags { public DepParameterExpr constraintExpr(); public X10CanonicalTypeNode constraintExpr(DepParameterExpr e); } Modified: trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/X10ClassDecl_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -93,6 +93,7 @@ import x10.types.ParameterType; import x10.types.TypeConstraint; import x10.types.TypeConstraint_c; +import x10.types.TypeDef; import x10.types.X10ClassDef; import x10.types.X10ClassDef_c; import x10.types.X10ClassType; @@ -677,6 +678,15 @@ } } + // Check for instance type definitions -- these are not supported. + for (TypeDef def : ((X10ClassDef) type).memberTypes()) { + MacroType mt = def.asType(); + if (mt.container() != null && !mt.flags().isStatic()) { + throw new SemanticException("Illegal type def " + mt + ": type-defs must be static.", def.position()); + } + } + + Map<X10ClassDef,X10ClassType> map = new HashMap<X10ClassDef, X10ClassType>(); for (X10ClassType ct : supers) { X10ClassType t = map.get(ct.x10Def()); Modified: trunk/x10.compiler/src/x10/ast/X10FieldAssign_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10FieldAssign_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/X10FieldAssign_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -80,6 +80,7 @@ n.checkFieldPlaceType(tc); n= (X10FieldAssign_c) n.type(t); + return n; } return X10LocalAssign_c.typeCheckAssign(n, tc); Modified: trunk/x10.compiler/src/x10/ast/X10New_c.java =================================================================== --- trunk/x10.compiler/src/x10/ast/X10New_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/ast/X10New_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -501,9 +501,13 @@ } X10TypeSystem ts = (X10TypeSystem) tc.typeSystem(); - Type tp = ci.returnType(); - - if (!ts.isSubtype(tp, t, tc.context())) { + X10Type tp = (X10Type) ci.returnType(); + X10Type tp1 = (X10Type) tp.copy(); + if (tp1.isProto()) { + tp1 = tp1.clearFlags(X10Flags.PROTO); + } + + if (!ts.isSubtype(tp1, t, tc.context())) { throw new SemanticException("Constructor return type " + tp + " is not a subtype of " + t + ".", position()); } Modified: trunk/x10.compiler/src/x10/types/ClosureDef.java =================================================================== --- trunk/x10.compiler/src/x10/types/ClosureDef.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/ClosureDef.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -21,6 +21,15 @@ import x10.constraint.XConstraint; import x10.constraint.XTerm; +/** + * A ClosureDef represents the type information for a closure: + * its formal parameters, + * its return types, + * throw types + * a placeterm representing the place at which the body of this function is intended to execute. + * @author vj + * + */ public interface ClosureDef extends FunctionDef, X10Def, X10ProcedureDef { ClosureInstance asInstance(); Modified: trunk/x10.compiler/src/x10/types/ClosureType_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/ClosureType_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/ClosureType_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -93,7 +93,8 @@ sb.append(", "); sb.append(t); } - return "(" + sb.toString() + ")" + guard() + "=> " + mi.returnType(); + XConstraint guard = guard(); + return "(" + sb.toString() + ")" + (guard==null? "{}" : guard) + "=> " + mi.returnType(); } @Override Modified: trunk/x10.compiler/src/x10/types/TypeDefAsMacroTypeTransform.java =================================================================== --- trunk/x10.compiler/src/x10/types/TypeDefAsMacroTypeTransform.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/TypeDefAsMacroTypeTransform.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -13,12 +13,15 @@ import polyglot.util.Transformation; public class TypeDefAsMacroTypeTransform implements Transformation<TypeDef, Type> { + public TypeDefAsMacroTypeTransform() { + } + public Type transform(TypeDef def) { X10TypeSystem xts = (X10TypeSystem) def.typeSystem(); MacroType mt = def.asType(); - if (mt.container() != null && !mt.flags().isStatic()) { + /*if (mt.container() != null && !mt.flags().isStatic()) { throw new InternalCompilerError("non-static member typedefs are unimplemented"); - } + }*/ return mt; } } Modified: trunk/x10.compiler/src/x10/types/X10ClassDef_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10ClassDef_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/X10ClassDef_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -14,9 +14,12 @@ import java.util.List; import polyglot.frontend.Source; +import polyglot.types.ClassDef; import polyglot.types.ClassDef_c; import polyglot.types.FieldDef; import polyglot.types.LazyRef_c; +import polyglot.types.Name; +import polyglot.types.Package; import polyglot.types.QName; import polyglot.types.Ref; import polyglot.types.Ref_c; @@ -333,4 +336,32 @@ public boolean isFunction() { return false; } + public String toString() { + Name name = name(); + + if (kind() == null) { + return "<unknown class " + name + ">"; + } + + if (kind() == ANONYMOUS) { + if (interfaces != null && ! interfaces.isEmpty()) { + return isFunction() ? "" + interfaces.get(0) : "<anonymous subtype of " + interfaces.get(0) + ">"; + } + if (superType != null) { + return "<anonymous subclass of " + superType + ">"; + } + } + + if (kind() == TOP_LEVEL) { + Package p = Types.get(package_()); + return (p != null ? p.toString() + "." : "") + name; + } + else if (kind() == MEMBER) { + ClassDef outer = Types.get(outer()); + return (outer != null ? outer.toString() + "." : "") + name; + } + else { + return name.toString(); + } + } } Modified: trunk/x10.compiler/src/x10/types/X10Context_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10Context_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/X10Context_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -59,6 +59,7 @@ import polyglot.types.ImportTable; import polyglot.types.LocalDef; import polyglot.types.LocalInstance; +import polyglot.types.Matcher; import polyglot.types.MethodInstance; import polyglot.types.Name; import polyglot.types.Named; @@ -895,4 +896,6 @@ public Name getNewVarName() { return Name.make(MAGIC_VAR_PREFIX + (varCount++)); } + + } Modified: trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/X10TypeEnv_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -449,11 +449,12 @@ } if (kind == Bound.UPPER && result.isEmpty()) - if (includeObject) + return Collections.<Type>singletonList(ts.Any()); + /* if (includeObject) return Collections.<Type>singletonList(ts.Object()); else return Collections.<Type>emptyList(); - + */ return new ArrayList<Type>(result); } @@ -1982,7 +1983,7 @@ if (acceptable.size() == 0) { if (error == null) { - error = new NoMemberException(NoMemberException.CONSTRUCTOR, "No valid constructor found for " + container + matcher.signature() + "."); + error = new NoMemberException(NoMemberException.CONSTRUCTOR, "No valid constructor found for " + matcher.signature() + "."); } throw error; Modified: trunk/x10.compiler/src/x10/types/X10TypeSystem.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10TypeSystem.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/X10TypeSystem.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -332,7 +332,7 @@ boolean isFunctionType(Type type); - X10ClassDef closureAnonymousClassDef(ClosureDef def); + // List<ClosureType> getFunctionSupertypes(Type type, X10Context context); Modified: trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java =================================================================== --- trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/types/X10TypeSystem_c.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -92,6 +92,7 @@ import x10.constraint.XTerms; import x10.constraint.XVar; import x10.parser.X10ParsedName; +import x10.util.ClosureSynthesizer; /** * A TypeSystem implementation for X10. @@ -340,175 +341,8 @@ throw new NoClassException(name.toString(), container); } - public X10ClassDef closureAnonymousClassDef(final ClosureDef def) { - final X10TypeSystem xts = this; - final Position pos = def.position(); - - X10ClassDef cd = new X10ClassDef_c(this, null); - - cd.position(pos); - cd.name(null); - cd.setPackage(null); - cd.kind(ClassDef.ANONYMOUS); - cd.flags(Flags.NONE); - - int numTypeParams = def.typeParameters().size(); - int numValueParams = def.formalTypes().size(); - - // Add type parameters. - List<Ref<? extends Type>> typeParams = new ArrayList<Ref<? extends Type>>(); - List<Type> typeArgs = new ArrayList<Type>(); - - ClosureInstance ci = (ClosureInstance) def.asInstance(); - typeArgs.addAll(ci.formalTypes()); - - if (!ci.returnType().isVoid()) { - typeArgs.add(ci.returnType()); - } - - // Instantiate the super type on the new parameters. - X10ClassType sup = (X10ClassType) closureBaseInterfaceDef(numTypeParams, - numValueParams, - ci.returnType().isVoid(), - def.formalNames(), - def.guard()) - .asType(); - - assert sup.x10Def().typeParameters().size() == typeArgs.size() : def + ", " + sup + ", " + typeArgs; - sup = sup.typeArguments(typeArgs); - - // cd.superType(Types.ref(Value())); // Closures are values. - cd.addInterface(Types.ref(sup)); - - return cd; - } - - public X10ClassDef closureBaseInterfaceDef(final int numTypeParams, final int numValueParams, - final boolean isVoid) { - return closureBaseInterfaceDef(numTypeParams, numValueParams, isVoid, null, null); - } - - /** - * Synthetically generated interface for the function types. - * @param numTypeParams - * @param numValueParams - * @param isVoid - * @param formalNames - * @param guard - * @return - */ - public X10ClassDef closureBaseInterfaceDef(final int numTypeParams, - final int numValueParams, - final boolean isVoid, - List<LocalDef> formalNames, - final Ref<XConstraint> guard) { - final X10TypeSystem xts = this; - final Position pos = Position.COMPILER_GENERATED; - - String name = "Fun_" + numTypeParams + "_" + numValueParams; - - if (isVoid) { - name = "Void" + name; - } - - // Check if the class has already been defined. - QName fullName = QName.make("x10.lang", name); - Named n = xts.systemResolver().check(fullName); - - if (n instanceof X10ClassType) { - X10ClassType ct = (X10ClassType) n; - return ct.x10Def(); - } - - X10ClassDef cd = (X10ClassDef) new X10ClassDef_c(this, null) { - @Override - public boolean isFunction() { - return true; - } - @Override - public ClassType asType() { - if (asType == null) { - X10ClassDef cd = this; - asType = new ClosureType_c(xts, pos, this); - } - return asType; - } - }; - - cd.position(pos); - cd.name(Name.make(name)); - try { - cd.setPackage(Types.ref(xts.packageForName(fullName.qualifier()))); - } - catch (SemanticException e) { - assert false; - } - - cd.kind(ClassDef.TOP_LEVEL); - cd.superType(null); // interfaces have no superclass - // Functions implement the Any interface. - cd.setInterfaces(Collections.<Ref<? extends Type>> singletonList(Types.ref(Any()))); - cd.flags(X10Flags.toX10Flags(Flags.PUBLIC.Abstract().Interface())); - - final List<Ref<? extends Type>> typeParams = new ArrayList<Ref<? extends Type>>(); - final List<Ref<? extends Type>> argTypes = new ArrayList<Ref<? extends Type>>(); - - for (int i = 0; i < numTypeParams; i++) { - Type t = new ParameterType_c(xts, pos, Name.make("X" + i), Types.ref(cd)); - typeParams.add(Types.ref(t)); - } - - for (int i = 0; i < numValueParams; i++) { - ParameterType t = new ParameterType_c(xts, pos, Name.make("Z" + (i + 1)), Types.ref(cd)); - argTypes.add(Types.ref(t)); - cd.addTypeParameter(t, ParameterType.Variance.CONTRAVARIANT); - } - - Type rt = null; - - if (!isVoid) { - ParameterType returnType = new ParameterType_c(xts, pos, Name.make("U"), Types.ref(cd)); - cd.addTypeParameter(returnType, ParameterType.Variance.COVARIANT); - rt = returnType; - } - else { - rt = Void(); - } - - // NOTE: don't call cd.asType() until after the type parameters are - // added. - FunctionType ct = (FunctionType) cd.asType(); - xts.systemResolver().install(fullName, ct); - - String fullNameWithThis = fullName + "#this"; - //String fullNameWithThis = "this"; - XName thisName = new XNameWrapper<Object>(new Object(), fullNameWithThis); - XRoot thisVar = XTerms.makeLocal(thisName); - - if (formalNames == null) { - formalNames = dummyLocalDefs(argTypes); - } - X10MethodDef mi = methodDef(pos, Types.ref(ct), - Flags.PUBLIC.Abstract(), Types.ref(rt), - Name.make("apply"), - typeParams, - argTypes, - thisVar, - formalNames, - guard, - null, - Collections.EMPTY_LIST, - null); - cd.addMethod(mi); - - return cd; - } - - - - - - + + public List<LocalDef> dummyLocalDefs(List<Ref<? extends Type>> types) { List<LocalDef> list = new ArrayList<LocalDef>(); for (int i = 0; i < types.size(); i++) { @@ -723,6 +557,15 @@ return new X10MostSpecificComparator<S, T>(matcher, context); } + private boolean contains(Collection<X10Type> c, X10Type x) { + Context cxt = emptyContext(); + for (X10Type t : c) { + if (typeEquals(t, x, cxt)) { + return true; + } + } + return false; + } public MacroType findTypeDef(Type container, TypeDefMatcher matcher, Context context) throws SemanticException { List<MacroType> acceptable = findAcceptableTypeDefs(container, matcher, context); @@ -732,7 +575,22 @@ Collection<MacroType> maximal = findMostSpecificProcedures(acceptable, (Matcher) matcher, context); + if (maximal.size() > 1) { // remove references that resolve to the same type. + Collection<Type> reduced = Collections.EMPTY_LIST; + Collection<MacroType> max2 = Collections.EMPTY_LIST; + for (MacroType mt : maximal) { + Type expanded = X10TypeMixin.baseType(mt); + if (! reduced.contains(expanded)) { + reduced.add(expanded); + max2.add(mt); + } + } + maximal = max2; + } + if (maximal.size() > 1) { + + StringBuffer sb = new StringBuffer(); for (Iterator<MacroType> i = maximal.iterator(); i.hasNext();) { MacroType ma = (MacroType) i.next(); @@ -1027,7 +885,7 @@ // return isX10BaseSubtype(t, Ref()); } - + public boolean isStructType(Type t) { return kind(t, null) == Kind.STRUCT; } @@ -1076,7 +934,7 @@ public List<X10ClassType> allImplementedInterfaces(X10ClassType c) { return allImplementedInterfaces(c, true); } - + public List<X10ClassType> allImplementedInterfaces(X10ClassType c, boolean checkSuperClasses) { List<X10ClassType> ans = new ArrayList<X10ClassType>(); allImplementedInterfaces(c, checkSuperClasses, ans); @@ -1100,12 +958,12 @@ } if (checkSuperClasses && c.superClass() != null) { - allImplementedInterfaces((X10ClassType)X10TypeMixin.baseType(c.superClass()), + allImplementedInterfaces((X10ClassType)X10TypeMixin.baseType(c.superClass()), checkSuperClasses, l); } for (Type parent : c.interfaces()) { - allImplementedInterfaces((X10ClassType)X10TypeMixin.baseType(parent), + allImplementedInterfaces((X10ClassType)X10TypeMixin.baseType(parent), checkSuperClasses, l); } } @@ -1181,7 +1039,7 @@ protected final X10Flags X10_FIELD_VARIABLE_FLAGS = (X10Flags) legalFieldFlags(); @Override - public MethodDef methodDef(Position pos, Ref<? extends StructType> container, Flags flags, + public MethodDef methodDef(Position pos, Ref<? extends StructType> container, Flags flags, Ref<? extends Type> returnType, Name name, List<Ref<? extends Type>> argTypes, List<Ref<? extends Type>> excTypes) { @@ -1246,18 +1104,18 @@ Ref<? extends Type> returnType, List<Ref<? extends Type>> argTypes, XRoot thisVar, List<LocalDef> formalNames, Ref<XConstraint> guard, List<Ref<? extends Type>> throwTypes) { - return new ClosureDef_c(this, p, typeContainer, methodContainer, returnType, + return new ClosureDef_c(this, p, typeContainer, methodContainer, returnType, argTypes, thisVar, formalNames, guard, throwTypes); } - public FunctionType closureType(Position p, Ref<? extends Type> returnType, - // List<Ref<? extends Type>> typeParams, + public FunctionType closureType(Position p, Ref<? extends Type> returnType, + // List<Ref<? extends Type>> typeParams, List<Ref<? extends Type>> argTypes, - List<LocalDef> formalNames, Ref<XConstraint> guard, - // Ref<TypeConstraint> typeGuard, + List<LocalDef> formalNames, Ref<XConstraint> guard, + // Ref<TypeConstraint> typeGuard, List<Ref<? extends Type>> throwTypes) { Type rt = Types.get(returnType); - X10ClassDef def = closureBaseInterfaceDef(0 /*typeParams.size()*/, argTypes.size(), rt.isVoid(), formalNames, guard); + X10ClassDef def = ClosureSynthesizer.closureBaseInterfaceDef(this, 0 /*typeParams.size()*/, argTypes.size(), rt.isVoid(), formalNames, guard); FunctionType ct = (FunctionType) def.asType(); List<Type> typeArgs = new ArrayList<Type>(); for (Ref<? extends Type> ref : argTypes) { @@ -1438,7 +1296,7 @@ // return XOBJECT_; // } - + public Type Object() { if (OBJECT_ == null) OBJECT_ = load("x10.lang.Object"); @@ -1450,7 +1308,7 @@ return CLASS_; return CLASS_ = load("x10.lang.Class"); } - + Type ANY_ = null; public Type Any() { if (ANY_ != null) @@ -1469,7 +1327,7 @@ } Type STRUCT_ = null; public Type Struct() { - if (STRUCT_ != null) + if (STRUCT_ != null) return STRUCT_; return STRUCT_ = x10.util.Struct.makeDef(this).asType(); } @@ -1796,7 +1654,7 @@ public boolean isValRail(Type t) { return hasSameClassDef(t, ValRail()); } - + public boolean isValRailOf(Type t, Type p) { if (!isValRail(t)) return false; List<Type> ta = ((X10ClassType)X10TypeMixin.baseType(t)).typeArguments(); @@ -1842,11 +1700,11 @@ public boolean isAny(Type me) { return typeEquals(me, Any(), emptyContext()); } - + public boolean isStruct(Type me) { return typeEquals(me, Struct(), emptyContext()); } - + public boolean isClock(Type me) { return isSubtype(me, Clock(), emptyContext()); } @@ -1896,7 +1754,7 @@ return v; } - + protected XTypeTranslator xtt = new XTypeTranslator(this); public XTypeTranslator xtypeTranslator() { @@ -2221,7 +2079,7 @@ Ref<? extends ClassType> container) { assert_(container); - // access for the default constructor is determined by the + // access for the default constructor is determined by the // access of the containing class. See the JLS, 2nd Ed., 8.8.7. Flags access = Flags.NONE; Flags flags = container.get().flags(); @@ -2229,10 +2087,10 @@ access = access.Private(); } if (flags.isProtected()) { - access = access.Protected(); + access = access.Protected(); } if (flags.isPublic()) { - access = access.Public(); + access = access.Public(); } return constructorDef(pos, container, access, Collections.<Ref<? extends Type>>emptyList(), @@ -2249,7 +2107,7 @@ String fullNameWithThis = "this#this"; XName thisName = new XNameWrapper<Object>(new Object(), fullNameWithThis); XRoot thisVar = XTerms.makeLocal(thisName); - + return constructorDef(pos, container, flags, Types.ref(Types.get(container)), Collections.EMPTY_LIST, argTypes, thisVar, dummyLocalDefs(argTypes), null, null, throwTypes); } @@ -2259,7 +2117,7 @@ assert_(container); assert_(argTypes); assert_(excTypes); - + X10ClassType t = (X10ClassType) Types.get(returnType); assert t != null : "Cannot set return type of constructor to " + t; if (t==null) @@ -2288,8 +2146,8 @@ o.setDefAnnotations(newATs); } + - public boolean clausesConsistent(x10.constraint.XConstraint c1, x10.constraint.XConstraint c2, Context context) { X10TypeEnv env = env(context); return env.clausesConsistent(c1, c2); @@ -2346,7 +2204,7 @@ X10MethodMatcher m = (X10MethodMatcher) matcher; List<MethodInstance> candidates = new ArrayList<MethodInstance>(); - + List<Type> types = env(matcher.context()).upperBounds(container, true); for (Type t : types) { List<MethodInstance> ms = super.findAcceptableMethods(t, matcher); @@ -2398,24 +2256,24 @@ if (fi == null) return null; - Type c = container != null - ? container + Type c = container != null + ? container : fi.container(); XVar v = X10TypeMixin.selfVarBinding(c); // ensureBound should have been called on container. - + X10TypeSystem ts = (X10TypeSystem) fi.typeSystem(); XRoot oldThis = fi.x10Def().thisVar(); if (oldThis != null && v == null) assert false; - // TODO: vj: 08/11/09 + // TODO: vj: 08/11/09 // Shouldnt we be setting thisVar on the type? Type t = fi.type(); Type rt = fi.rightType(); if (v != null && oldThis != null) { - t = Subst.subst(t, (new XVar[] { v }), + t = Subst.subst(t, (new XVar[] { v }), (new XRoot[] { oldThis }), new Type[] {}, new ParameterType[] {}); - rt = Subst.subst(rt, (new XVar[] { v }), (new XRoot[] { oldThis }), + rt = Subst.subst(rt, (new XVar[] { v }), (new XRoot[] { oldThis }), new Type[] {}, new ParameterType[] {}); rt = X10TypeMixin.setThisVar(rt, v); } @@ -2493,7 +2351,7 @@ protected List<Type> typeArgs; protected List<Expr> args; - + protected X10ConstructorMatcher(Type container, List<Type> argTypes, Context context) { this(container, Collections.EMPTY_LIST, argTypes, context); } @@ -2501,15 +2359,15 @@ protected X10ConstructorMatcher(Type container, List<Type> typeArgs, List<Type> argTypes, Context context) { this(container, typeArgs, null, argTypes, context); } - - protected X10ConstructorMatcher(Type container, List<Type> typeArgs, List<Expr> args, + + protected X10ConstructorMatcher(Type container, List<Type> typeArgs, List<Expr> args, List<Type> argTypes, Context context) { super(container, argTypes, context); this.typeArgs = typeArgs; this.args = args; } + - public List<Type> arguments() { return argTypes; } @@ -2527,7 +2385,7 @@ X10ConstructorInstance xmi = (X10ConstructorInstance) ci; Type c = container != null ? container : xmi.container(); if (typeArgs.isEmpty() || typeArgs.size() == xmi.typeParameters().size()) - return X10MethodInstance_c.inferAndCheckAndInstantiate((X10Context) c.typeSystem().emptyContext(), + return X10MethodInstance_c.inferAndCheckAndInstantiate((X10Context) c.typeSystem().emptyContext(), xmi, c, typeArgs, argTypes); } return null; @@ -2634,7 +2492,7 @@ X10Context xc = (X10Context) context; return env(context).isSubtypeWithValueInterfaces(t1, t2); } - + // Returns the number of bytes required to represent the type, or null if unknown (e.g. involves an address somehow) // Note for rails and valrails this returns the size of 1 element, this will have to be scaled // by the number of elements to get the true figure. @@ -2653,32 +2511,32 @@ } return null; } - + public boolean isAtPlace(Receiver r, Expr place, X10Context xc) { XConstraint_c c = new XConstraint_c(); XTerm placeTerm = xtypeTranslator().trans(c, place, xc); - if (placeTerm == null) + if (placeTerm == null) return false; return isAtPlace(r, placeTerm, xc); } - + public boolean isAtPlace(Receiver r, XTerm placeTerm, X10Context xc) { // If the code is executing in a global context then // no receiver can be local. if (placeTerm.equals(globalPlace())) return false; - + try { XConstraint pc = xc.currentPlaceTerm().xconstraint(); Type rType = r.type(); - XTerm target = X10TypeMixin.selfVarBinding(rType); // + XTerm target = X10TypeMixin.selfVarBinding(rType); // if (target == null) { target = xtypeTranslator().trans(pc, r, xc); if (target == null) // The receiver is not named. So make up a new name. // The only thing we know about the name is that it is of rType, target = XConstraint_c.genEQV(); - } + } // rType = X10TypeMixin.setSelfVar(rType, (XVar) target); rType = Subst.subst(rType, target, (XRoot) X10TypeMixin.selfVar(rType)); assert xc.currentPlaceTerm() != null; @@ -2709,7 +2567,7 @@ return false; } - + public XConstraint isHereConstraint(Receiver r, X10Context xc) { XConstraint pc = new XConstraint_c(); @@ -2724,24 +2582,24 @@ } protected XConstraint isHereConstraint(XConstraint pc, XTerm target, X10Context xc) { try { - XTerm eloc = xtypeTranslator().trans(pc, target, + XTerm eloc = xtypeTranslator().trans(pc, target, ((StructType) Object()).fieldNamed(homeName())); pc.addBinding(eloc, xc.currentPlaceTerm()); } catch (XFailure z) { pc.setInconsistent(); - } + } return pc; } public XTerm homeVar(XTerm target, X10Context xc) { XConstraint pc = new XConstraint_c(); - return xtypeTranslator().trans(pc, target, + return xtypeTranslator().trans(pc, target, ((StructType) Object()).fieldNamed(homeName())); } public XTerm locVar(Receiver r, X10Context xc) { XConstraint pc = new XConstraint_c(); XTerm target = xtypeTranslator().trans(pc, r, xc); if (target == null) return null; - return xtypeTranslator().trans(pc, target, + return xtypeTranslator().trans(pc, target, ((StructType) Object()).fieldNamed(homeName())); } public XConstrainedTerm globalPlace() { @@ -2750,8 +2608,8 @@ public boolean isHere(Receiver r, X10Context xc) { return isAtPlace(r, xc.currentPlaceTerm().term(), xc); } - - + + public FieldInstance findField(Type container, TypeSystem_c.FieldMatcher matcher) throws SemanticException { container = X10TypeMixin.ensureSelfBound(container); @@ -2765,12 +2623,12 @@ // First, try to determine if there in fact a struct in scope with the given name. TypeNode otn = new X10ParsedName(nf, ts, Position.COMPILER_GENERATED, name).toType();// // nf.AmbDepTypeNode(position(), null, name(), typeArguments, Collections.EMPTY_LIST, null); - + TypeNode tn = (TypeNode) otn.visit(tb); - + // First ensure that there is a type associated with tn. tn = (TypeNode) tn.disambiguate(tc); - + // ok, if we made it this far, then there is a type. Check that it is a struct. Type t = tn.type(); t = ts.expandMacros(t); @@ -2785,13 +2643,13 @@ public List<Type> abstractSuperInterfaces(Type t) { List<Type> result = super.abstractSuperInterfaces(t); // A work-around for the current transient state of the system in which - // Object is an interface. + // Object is an interface. if (isStructType(t)) { result.remove(Object()); } return result; } - + Name homeName = Name.make("home"); public Name homeName() { return homeName;} } Added: trunk/x10.compiler/src/x10/util/ClosureSynthesizer.java =================================================================== --- trunk/x10.compiler/src/x10/util/ClosureSynthesizer.java (rev 0) +++ trunk/x10.compiler/src/x10/util/ClosureSynthesizer.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -0,0 +1,271 @@ +package x10.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import polyglot.ast.Block; +import polyglot.ast.Formal; +import polyglot.types.ClassDef; +import polyglot.types.ClassType; +import polyglot.types.Flags; +import polyglot.types.LocalDef; +import polyglot.types.Name; +import polyglot.types.Named; +import polyglot.types.QName; +import polyglot.types.Ref; +import polyglot.types.SemanticException; +import polyglot.types.Type; +import polyglot.types.Types; +import polyglot.util.Position; +import x10.ast.Closure; +import x10.ast.X10NodeFactory; +import x10.constraint.XConstraint; +import x10.constraint.XName; +import x10.constraint.XNameWrapper; +import x10.constraint.XRoot; +import x10.constraint.XTerms; +import x10.types.ClosureDef; +import x10.types.ClosureInstance; +import x10.types.ClosureType_c; +import x10.types.FunctionType; +import x10.types.ParameterType; +import x10.types.ParameterType_c; +import x10.types.X10ClassDef; +import x10.types.X10ClassDef_c; +import x10.types.X10ClassType; +import x10.types.X10Context; +import x10.types.X10Flags; +import x10.types.X10MethodDef; +import x10.types.X10TypeSystem; +import x10.types.X10TypeSystem_c; + +public class ClosureSynthesizer { + + /** Return an instance of the AST node, Closure, representing a closure (parms):retType => body. + * This is treated as if it were defined by + * new Fun_n_m + * The type of this node is an anonymous class implementing the interface + * + * @param xts + * @param xnf + * @param pos + * @param retType + * @param parms + * @param body + * @param context + * @return + */ + public static Closure makeClosure(X10TypeSystem_c xts, X10NodeFactory xnf, Position pos, Type retType, + List<Formal> parms, Block body, + X10Context context) { + List<Ref<? extends Type>> fTypes = new ArrayList<Ref<? extends Type>>(); + List<LocalDef> fNames = new ArrayList<LocalDef>(); + for (Formal f : parms) { + fTypes.add(Types.ref(f.type().type())); + fNames.add(f.localDef()); + } + ClosureDef cDef = xts.closureDef(pos, Types.ref(context.currentClass()), + Types.ref(context.currentCode().asInstance()), + Types.ref(retType), + // Collections.EMPTY_LIST, + fTypes, + (XRoot) null, + fNames, + null, + // null, + Collections.EMPTY_LIST); + Closure closure = (Closure) xnf.Closure(pos, //Collections.EMPTY_LIST, + parms, + null, + xnf.CanonicalTypeNode(pos, retType), + Collections.EMPTY_LIST, body) + .closureDef(cDef) + .type(closureAnonymousClassDef((X10TypeSystem_c) xts, cDef).asType()); + return closure; + } + /** + * Return the type of an anonymous class implementing a given closure + * type, def. + * @param xts + * @param def + * @return + */ + public static X10ClassDef closureAnonymousClassDef(final X10TypeSystem_c xts, final ClosureDef def) { + + final Position pos = def.position(); + + X10ClassDef cd = new X10ClassDef_c(xts, null) { + @Override + public boolean isFunction() { + return true; + } + }; + + cd.position(pos); + cd.name(null); + cd.setPackage(null); + cd.kind(ClassDef.ANONYMOUS); + cd.flags(Flags.NONE); + + int numTypeParams = def.typeParameters().size(); + int numValueParams = def.formalTypes().size(); + + // Add type parameters. + List<Ref<? extends Type>> typeParams = new ArrayList<Ref<? extends Type>>(); + List<Type> typeArgs = new ArrayList<Type>(); + + ClosureInstance ci = (ClosureInstance) def.asInstance(); + typeArgs.addAll(ci.formalTypes()); + + if (!ci.returnType().isVoid()) { + typeArgs.add(ci.returnType()); + } + + // Instantiate the super type on the new parameters. + X10ClassType sup = (X10ClassType) closureBaseInterfaceDef(xts, numTypeParams, + numValueParams, + ci.returnType().isVoid(), + def.formalNames(), + def.guard()) + .asType(); + + assert sup.x10Def().typeParameters().size() == typeArgs.size() : def + ", " + sup + ", " + typeArgs; + sup = sup.typeArguments(typeArgs); + cd.addInterface(Types.ref(sup)); + + return cd; + } + public static X10ClassDef closureBaseInterfaceDef(final X10TypeSystem_c xts, final int numTypeParams, final int numValueParams, + final boolean isVoid) { + return ClosureSynthesizer.closureBaseInterfaceDef(xts, numTypeParams, numValueParams, isVoid, null, null); + } + + /** + * Synthetic generated interface for the function types. Mimics an X10 source level definition of + * the following interface, where numTypeParams=m, and numValueParams=n. + * + * + * package x10.lang; + * public interface Fun_m_n extends Any ( + * + * public abstract def apply[X1,..,Xm,-Z1,..,-Zn,+U](formalNames){guard}:U; + * or: + * public abstract def apply[X1,..,Xm,-Z1,..,-Zn](formalNames){guard}:Void; + * } + * + * + * @param numTypeParams + * @param numValueParams + * @param isVoid + * @param formalNames + * @param guard + * @return + */ + public static X10ClassDef closureBaseInterfaceDef(final X10TypeSystem_c xts, final int numTypeParams, + final int numValueParams, + final boolean isVoid, + List<LocalDef> formalNames, + final Ref<XConstraint> guard) { + final Position pos = Position.COMPILER_GENERATED; + + String name = "Fun_" + numTypeParams + "_" + numValueParams; + + if (isVoid) { + name = "Void" + name; + } + + // Check if the class has already been defined. + QName fullName = QName.make("x10.lang", name); + Named n = xts.systemResolver().check(fullName); + + if (n instanceof X10ClassType) { + X10ClassType ct = (X10ClassType) n; + return ct.x10Def(); + } + + X10ClassDef cd = (X10ClassDef) new X10ClassDef_c(xts, null) { + @Override + public boolean isFunction() { + return true; + } + @Override + public ClassType asType() { + if (asType == null) { + X10ClassDef cd = this; + asType = new ClosureType_c(xts, pos, this); + } + return asType; + } + }; + + cd.position(pos); + cd.name(Name.make(name)); + try { + cd.setPackage(Types.ref(xts.packageForName(fullName.qualifier()))); + } + catch (SemanticException e) { + assert false; + } + + cd.kind(ClassDef.TOP_LEVEL); + cd.superType(null); // interfaces have no superclass + // Functions implement the Any interface. + cd.setInterfaces(Collections.<Ref<? extends Type>> singletonList(Types.ref(xts.Any()))); + cd.flags(X10Flags.toX10Flags(Flags.PUBLIC.Abstract().Interface())); + + final List<Ref<? extends Type>> typeParams = new ArrayList<Ref<? extends Type>>(); + final List<Ref<? extends Type>> argTypes = new ArrayList<Ref<? extends Type>>(); + + for (int i = 0; i < numTypeParams; i++) { + Type t = new ParameterType_c(xts, pos, Name.make("X" + i), Types.ref(cd)); + typeParams.add(Types.ref(t)); + } + + for (int i = 0; i < numValueParams; i++) { + ParameterType t = new ParameterType_c(xts, pos, Name.make("Z" + (i + 1)), Types.ref(cd)); + argTypes.add(Types.ref(t)); + cd.addTypeParameter(t, ParameterType.Variance.CONTRAVARIANT); + } + + Type rt = null; + + if (!isVoid) { + ParameterType returnType = new ParameterType_c(xts, pos, Name.make("U"), Types.ref(cd)); + cd.addTypeParameter(returnType, ParameterType.Variance.COVARIANT); + rt = returnType; + } + else { + rt = xts.Void(); + } + + // NOTE: don't call cd.asType() until after the type parameters are + // added. + FunctionType ct = (FunctionType) cd.asType(); + xts.systemResolver().install(fullName, ct); + + String fullNameWithThis = fullName + "#this"; + //String fullNameWithThis = "this"; + XName thisName = new XNameWrapper<Object>(new Object(), fullNameWithThis); + XRoot thisVar = XTerms.makeLocal(thisName); + + if (formalNames == null) { + formalNames = xts.dummyLocalDefs(argTypes); + } + X10MethodDef mi = xts.methodDef(pos, Types.ref(ct), + Flags.PUBLIC.Abstract(), Types.ref(rt), + Name.make("apply"), + typeParams, + argTypes, + thisVar, + formalNames, + guard, + null, + Collections.EMPTY_LIST, + null); + cd.addMethod(mi); + + return cd; + } + +} Modified: trunk/x10.compiler/src/x10/util/Synthesizer.java =================================================================== --- trunk/x10.compiler/src/x10/util/Synthesizer.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/util/Synthesizer.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -375,38 +375,57 @@ } - public Closure makeClosure(Position pos, Type retType, Block body, - X10Context context) { + public Call makeInstanceCall(Position pos, + Receiver receiver, + Name name, + List<TypeNode> typeArgsN, + List<Expr> args, + Type returnType, + List<Type> argTypes, + X10Context xc) throws SemanticException { + + List<Type> typeArgs = new ArrayList<Type>(); + for (TypeNode t : typeArgsN) typeArgs.add(t.type()); + MethodInstance mi = xts.findMethod(receiver.type(), + xts.MethodMatcher(receiver.type(), name, typeArgs, argTypes, xc)); + Call result= (Call) xnf.X10Call(pos, + receiver, + xnf.Id(pos, name), + typeArgsN, + args) + .methodInstance(mi) + .type(returnType); + return result; + + } + /** + * Return a synthesized AST node for (parms):retType => body, at the given position and context. + * @param pos + * @param retType + * @param parms + * @param body + * @param context + * @return + */ + public Closure makeClosure(Position pos, Type retType, List<Formal> parms, Block body, X10Context context) { + return ClosureSynthesizer.makeClosure((X10TypeSystem_c) xts, xnf, pos, retType, parms, body, context); + } + + /** + * Return a synthesized AST node for ():retType => body, at the given position and context. + * @param pos + * @param retType + * @param parms + * @param body + * @param context + * @return + */ + + public Closure makeClosure(Position pos, Type retType, Block body, X10Context context) { return makeClosure(pos, retType, Collections.EMPTY_LIST, body, context); } - public Closure makeClosure(Position pos, Type retType, List<Formal> parms, Block body, - X10Context context) { - List<Ref<? extends Type>> fTypes = new ArrayList<Ref<? extends Type>>(); - List<LocalDef> fNames = new ArrayList<LocalDef>(); - for (Formal f : parms) { - fTypes.add(Types.ref(f.type().type())); - fNames.add(f.localDef()); - } - ClosureDef cDef = xts.closureDef(pos, Types.ref(context.currentClass()), - Types.ref(context.currentCode().asInstance()), - Types.ref(retType), - // Collections.EMPTY_LIST, - fTypes, - (XRoot) null, - fNames, - null, - // null, - Collections.EMPTY_LIST); - Closure closure = (Closure) xnf.Closure(pos, //Collections.EMPTY_LIST, - parms, - null, - xnf.CanonicalTypeNode(pos, retType), - Collections.EMPTY_LIST, body) - .closureDef(cDef) - .type(xts.closureAnonymousClassDef(cDef).asType()); - return closure; - } + public Block toBlock(Stmt body) { return body instanceof Block ? (Block) body : xnf.Block(body.position(), body); } Modified: trunk/x10.compiler/src/x10/visit/Desugarer.java =================================================================== --- trunk/x10.compiler/src/x10/visit/Desugarer.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10/visit/Desugarer.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -94,6 +94,7 @@ import x10.types.X10TypeMixin; import x10.types.X10TypeSystem; import x10.types.X10TypeSystem_c; +import x10.util.ClosureSynthesizer; import x10.util.Synthesizer; /** @@ -200,19 +201,26 @@ return visitRemoteClosure(e, EVAL_AT, e.place()); } - + Expr getPlace(Position pos, Expr place) throws SemanticException{ + if (! xts.isImplicitCastValid(place.type(), xts.Place(), context)) { + place = synth.makeInstanceCall(pos, place, xts.homeName(), + Collections.EMPTY_LIST, + Collections.EMPTY_LIST, + xts.Place(), + Collections.EMPTY_LIST, + xContext()); + } + return place; + } private Expr visitRemoteClosure(Closure c, Name implName, Expr place) throws SemanticException { Position pos = c.position(); - if (xts.isImplicitCastValid(place.type(), xts.Object(), context)) { - place = synth.makeFieldAccess(pos,place, xts.homeName(), xContext()); - } - + place = getPlace(pos, place); List<TypeNode> typeArgs = Arrays.asList(new TypeNode[] { c.returnType() }); Position bPos = c.body().position(); ClosureDef cDef = c.closureDef().position(bPos); Expr closure = xnf.Closure(c, bPos) .closureDef(cDef) - .type(xts.closureAnonymousClassDef(cDef).asType()); + .type(ClosureSynthesizer.closureAnonymousClassDef((X10TypeSystem_c) xts, cDef).asType()); List<Expr> args = new ArrayList<Expr>(Arrays.asList(new Expr[] { place, closure })); List<Type> mArgs = new ArrayList<Type>(Arrays.asList(new Type[] { xts.Place(), cDef.asType() @@ -225,14 +233,13 @@ } private Stmt atStmt(Position pos, Stmt body, Expr place) throws SemanticException { - if (xts.isImplicitCastValid(place.type(), xts.Object(), context)) { - place = synth.makeFieldAccess(pos,place, xts.homeName(), xContext()); - } + place = getPlace(pos, place); + Expr placeId = synth.makeFieldAccess(pos, place, Name.make("id"), xContext()); Closure closure = synth.makeClosure(body.position(), xts.Void(), synth.toBlock(body), xContext()); Stmt result = xnf.Eval(pos, synth.makeStaticCall(pos, xts.Runtime(), RUN_AT, - Arrays.asList(new Expr[] { place, closure }), xts.Void(), + Arrays.asList(new Expr[] { placeId, closure }), xts.Void(), xContext())); return result; } Modified: trunk/x10.compiler/src/x10cpp/visit/MessagePassingCodeGenerator.java =================================================================== --- trunk/x10.compiler/src/x10cpp/visit/MessagePassingCodeGenerator.java 2010-01-12 05:46:20 UTC (rev 12512) +++ trunk/x10.compiler/src/x10cpp/visit/MessagePassingCodeGenerator.java 2010-01-12 12:14:36 UTC (rev 12513) @@ -215,6 +215,7 @@ import x10.visit.Stati... [truncated message content] |