[Nice-commit] Nice/src/bossa/syntax NiceClass.java,1.75,1.76 CustomConstructor.java,1.5,1.6 Construc
Brought to you by:
bonniot
From: <bo...@us...> - 2003-12-14 16:52:36
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv3719/src/bossa/syntax Modified Files: NiceClass.java CustomConstructor.java Constructor.java Log Message: Resolve, typecheck and generate code for custom constructors. Index: NiceClass.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/NiceClass.java,v retrieving revision 1.75 retrieving revision 1.76 diff -C2 -d -r1.75 -r1.76 *** NiceClass.java 13 Dec 2003 19:09:40 -0000 1.75 --- NiceClass.java 14 Dec 2003 16:52:32 -0000 1.76 *************** *** 99,102 **** --- 99,115 ---- } + /** List of symbols for calling constructors of this class. */ + private ArrayList constructors = new ArrayList(10); + + void addConstructorCallSymbol(MethodDeclaration.Symbol sym) + { + constructors.add(sym); + } + + List getConstructorCallSymbols() + { + return (List) constructors.clone(); + } + /**************************************************************** * Fields Index: CustomConstructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CustomConstructor.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CustomConstructor.java 13 Dec 2003 18:59:00 -0000 1.5 --- CustomConstructor.java 14 Dec 2003 16:52:32 -0000 1.6 *************** *** 58,70 **** void resolveBody() { body = bossa.syntax.dispatch.analyse (body, thisScope, thisTypeScope, false); } void innerTypecheck() throws TypingEx { super.innerTypecheck(); ! bossa.syntax.dispatch.typecheck(body); } --- 58,98 ---- void resolveBody() { + resolveThis((Block) body); body = bossa.syntax.dispatch.analyse (body, thisScope, thisTypeScope, false); } + private void resolveThis(Block block) + { + Statement last = block.statements[block.statements.length - 1]; + if (last instanceof Block) + { + resolveThis((Block) last); + return; + } + + try { + CallExp call = (CallExp) ((ExpressionStmt) last).exp; + IdentExp ident = (IdentExp) call.function; + if (! call.function.toString().equals("this")) + User.error(this, + "The last statement must be a call to 'this' constructor"); + + Location loc = ident.location(); + call.function = new OverloadedSymbolExp + (classe.getConstructorCallSymbols(), FormalParameters.thisName); + call.function.setLocation(loc); + } + catch(ClassCastException ex) { + User.error(this, + "The last statement must be a call to 'this' constructor"); + } + } + void innerTypecheck() throws TypingEx { super.innerTypecheck(); ! bossa.syntax.dispatch.typecheck(body); } *************** *** 74,77 **** --- 102,111 ---- } + public void compile() + { + // Make sure the constructor is generated. + getCode(); + } + protected gnu.expr.Expression computeCode() { *************** *** 83,92 **** ConstructorExp lambda = Gen.createCustomConstructor ! ((ClassType) javaReturnType(), javaArgTypes(), parameters.getMonoSymbols()); Gen.setMethodBody(lambda, body.generateCode()); classe.getClassExp().addMethod(lambda); ! return lambda; } --- 117,126 ---- ConstructorExp lambda = Gen.createCustomConstructor ! ((ClassType) javaReturnType(), javaArgTypes(), getSymbols()); Gen.setMethodBody(lambda, body.generateCode()); classe.getClassExp().addMethod(lambda); ! return new QuoteExp(new InstantiateProc(lambda)); } Index: Constructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Constructor.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Constructor.java 27 Nov 2003 21:02:13 -0000 1.9 --- Constructor.java 14 Dec 2003 16:52:32 -0000 1.10 *************** *** 48,51 **** --- 48,63 ---- this.fields = fields; this.index = index; + + 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() + { + getCode(); + return initializeFromConstructor; + } + }); } *************** *** 66,69 **** --- 78,84 ---- private Expression initialize; + /** Call the constructor, with all the arguments, with an implicit this argument. */ + private Expression initializeFromConstructor; + /** Call the constructor, with only non-default arguments. */ private Expression initializeOmitDefaults; *************** *** 125,128 **** --- 140,144 ---- { initialize = new QuoteExp(new InitializeProc(m)); + initializeFromConstructor = new QuoteExp(new InitializeProc(m, true)); instantiate = new QuoteExp(new InstantiateProc(m)); } *************** *** 144,147 **** --- 160,164 ---- { initialize = new QuoteExp(new InitializeProc(lambda)); + initializeFromConstructor = new QuoteExp(new InitializeProc(lambda, true)); instantiate = new QuoteExp(new InstantiateProc(lambda)); } |