Thread: [Nice-commit] Nice/src/bossa/syntax CustomConstructor.java,1.19,1.20
Brought to you by:
bonniot
From: Daniel B. <bo...@us...> - 2004-10-07 16:25:20
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23128/src/bossa/syntax Modified Files: CustomConstructor.java Log Message: Simplification of CustomConstructor, now that it only concerns source constructors. Index: CustomConstructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CustomConstructor.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** CustomConstructor.java 7 Oct 2004 15:29:29 -0000 1.19 --- CustomConstructor.java 7 Oct 2004 16:25:06 -0000 1.20 *************** *** 25,66 **** /** ! A user defined constructor. */ ! public abstract class CustomConstructor extends UserOperator { public static CustomConstructor make ! (LocatedString className, Constraint cst, FormalParameters params, Statement body) { ! return new SourceCustomConstructor(className, cst, params, body); } ! CustomConstructor(LocatedString className, Constraint cst, ! FormalParameters params) { ! super(new LocatedString("<init>", className.location()), cst, returnType(className, cst), params, Contract.noContract); - } ! void addConstructorCallSymbol() ! { ! mlsub.typing.Polytype type = new mlsub.typing.Polytype ! (getType().getConstraint(), ! new mlsub.typing.FunType(getArgTypes(), PrimitiveType.voidType)); ! classe.addConstructorCallSymbol ! (new MethodDeclaration.Symbol(name, type) { ! gnu.expr.Expression compileInCallPosition() ! { ! return getInitializationCode(true); ! } ! }); } private static Monotype returnType(LocatedString className, Constraint cst) { TypeIdent classe = new TypeIdent(className); classe.nullness = Monotype.sure; ! if (cst == Constraint.True) return classe; --- 25,57 ---- /** ! A user defined constructor, in a source program. */ ! public class CustomConstructor extends UserOperator { public static CustomConstructor make ! (LocatedString className, Constraint cst, FormalParameters params, Statement body) { ! return new CustomConstructor(className, cst, params, body); } ! CustomConstructor(LocatedString className, Constraint cst, ! FormalParameters params, Statement body) { ! super(new LocatedString("<init>", className.location()), cst, returnType(className, cst), params, Contract.noContract); ! this.className = className; ! this.body = body; } + NiceClass classe; + private static Monotype returnType(LocatedString className, Constraint cst) { TypeIdent classe = new TypeIdent(className); classe.nullness = Monotype.sure; ! if (cst == Constraint.True) return classe; *************** *** 82,205 **** } ! public void printInterface(java.io.PrintWriter s) { ! // Constructors are not printed, they are loaded from the bytecode. } ! public void compile() { ! // Make sure the constructor is generated. ! getCode(); ! } ! ! abstract gnu.expr.Expression getInitializationCode(boolean implicitThis); ! ! NiceClass classe; ! /**************************************************************** ! * A custom constructor defined in a source program. ! ****************************************************************/ ! static class SourceCustomConstructor extends CustomConstructor ! { ! SourceCustomConstructor(LocatedString className, Constraint cst, ! FormalParameters params, Statement body) ! { ! super(className, cst, params); ! this.className = className; ! this.body = body; ! } ! void resolve() ! { ! super.resolve(); ! TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(className); ! TypeConstructors.addConstructor(tc, this); ! classe = NiceClass.get(tc); ! if (classe == null) ! User.error(this, ! "It is impossible to add a constructor to class " + tc); ! addConstructorCallSymbol(); ! // Save the scopes, since we need them later, but they get null'ed. ! thisScope = scope; ! thisTypeScope = typeScope; ! } ! private VarScope thisScope; ! private TypeScope thisTypeScope; ! void resolveBody() ! { ! bossa.syntax.dispatch.resolveCCThis(body, this, classe); ! body = bossa.syntax.dispatch.analyse ! (body, thisScope, thisTypeScope, false); ! } ! void innerTypecheck() throws TypingEx ! { ! super.innerTypecheck(); ! bossa.syntax.dispatch.typecheck(body); ! } ! private Map map(TypeSymbol[] source, mlsub.typing.Monotype[] destination) ! { ! Map res = new HashMap(source.length); ! for (int i = 0; i < source.length; i++) ! res.put(source[i].toString(), Monotype.create(destination[i])); ! return res; ! } ! protected gnu.expr.Expression computeCode() ! { ! ConstructorExp lambda = Gen.createCustomConstructor ! ((ClassType) javaReturnType(), javaArgTypes(), getSymbols()); ! Gen.setMethodBody(lambda, body.generateCode()); ! classe.getClassExp().addMethod(lambda); ! ! // In the bytecode, we want to use the same type parameter names ! // as the class definition, even if the source of this custom constructor ! // renamed them locally. ! mlsub.typing.Constraint cst = getType().getConstraint(); ! if (mlsub.typing.Constraint.hasBinders(cst)) ! parameters.substitute ! (map(cst.binders(), classe.definition.getTypeParameters())); ! lambda.addBytecodeAttribute(parameters.asBytecodeAttribute()); ! initializationCode = ! new QuoteExp(new InitializeProc(lambda)); ! initializationCodeImplicitThis = ! new QuoteExp(new InitializeProc(lambda, true)); ! return new QuoteExp(new InstantiateProc(lambda)); ! } ! gnu.expr.Expression getConstructorInvocation(boolean omitDefaults) ! { ! getCode(); return initializationCode; - } - - gnu.expr.Expression getInitializationCode(boolean implicitThis) - { - getCode(); - if (implicitThis) - return initializationCodeImplicitThis; - else - return initializationCode; - } - - LocatedString className; - Statement body; - gnu.expr.Expression initializationCode; - gnu.expr.Expression initializationCodeImplicitThis; } } --- 73,190 ---- } ! void addConstructorCallSymbol() { ! mlsub.typing.Polytype type = new mlsub.typing.Polytype ! (getType().getConstraint(), ! new mlsub.typing.FunType(getArgTypes(), PrimitiveType.voidType)); ! classe.addConstructorCallSymbol ! (new MethodDeclaration.Symbol(name, type) { ! gnu.expr.Expression compileInCallPosition() ! { ! return getInitializationCode(true); ! } ! }); } ! void resolve() { ! super.resolve(); ! TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(className); ! TypeConstructors.addConstructor(tc, this); ! classe = NiceClass.get(tc); ! if (classe == null) ! User.error(this, ! "It is impossible to add a constructor to class " + tc); ! addConstructorCallSymbol(); ! // Save the scopes, since we need them later, but they get null'ed. ! thisScope = scope; ! thisTypeScope = typeScope; ! } ! private VarScope thisScope; ! private TypeScope thisTypeScope; ! void resolveBody() ! { ! bossa.syntax.dispatch.resolveCCThis(body, this, classe); ! body = bossa.syntax.dispatch.analyse ! (body, thisScope, thisTypeScope, false); ! } ! void innerTypecheck() throws TypingEx ! { ! super.innerTypecheck(); ! bossa.syntax.dispatch.typecheck(body); ! } ! public void printInterface(java.io.PrintWriter s) ! { ! // Constructors are not printed, they are loaded from the bytecode. ! } ! public void compile() ! { ! // Make sure the constructor is generated. ! getCode(); ! } ! private Map map(TypeSymbol[] source, mlsub.typing.Monotype[] destination) ! { ! Map res = new HashMap(source.length); ! for (int i = 0; i < source.length; i++) ! res.put(source[i].toString(), Monotype.create(destination[i])); ! return res; ! } ! protected gnu.expr.Expression computeCode() ! { ! ConstructorExp lambda = Gen.createCustomConstructor ! ((ClassType) javaReturnType(), javaArgTypes(), getSymbols()); ! Gen.setMethodBody(lambda, body.generateCode()); ! classe.getClassExp().addMethod(lambda); ! // In the bytecode, we want to use the same type parameter names ! // as the class definition, even if the source of this custom constructor ! // renamed them locally. ! mlsub.typing.Constraint cst = getType().getConstraint(); ! if (mlsub.typing.Constraint.hasBinders(cst)) ! parameters.substitute ! (map(cst.binders(), classe.definition.getTypeParameters())); ! lambda.addBytecodeAttribute(parameters.asBytecodeAttribute()); ! initializationCode = ! new QuoteExp(new InitializeProc(lambda)); ! initializationCodeImplicitThis = ! new QuoteExp(new InitializeProc(lambda, true)); ! return new QuoteExp(new InstantiateProc(lambda)); ! } ! gnu.expr.Expression getConstructorInvocation(boolean omitDefaults) ! { ! getCode(); ! return initializationCode; ! } ! gnu.expr.Expression getInitializationCode(boolean implicitThis) ! { ! getCode(); ! if (implicitThis) ! return initializationCodeImplicitThis; ! else return initializationCode; } + + LocatedString className; + Statement body; + gnu.expr.Expression initializationCode; + gnu.expr.Expression initializationCodeImplicitThis; } |