Thread: [Nice-commit] Nice/src/bossa/syntax FormalParameters.java,1.33,1.34 CustomConstructor.java,1.6,1.7
Brought to you by:
bonniot
From: <bo...@us...> - 2003-12-15 02:40:20
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv16555/src/bossa/syntax Modified Files: FormalParameters.java CustomConstructor.java Log Message: Load custom constructors form the bytecode of compiled packages. Index: FormalParameters.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/FormalParameters.java,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** FormalParameters.java 22 Nov 2003 16:37:39 -0000 1.33 --- FormalParameters.java 15 Dec 2003 02:40:17 -0000 1.34 *************** *** 528,531 **** --- 528,548 ---- } + gnu.bytecode.Attribute asBytecodeAttribute() + { + return new gnu.bytecode.MiscAttr("parameters", this.toString().getBytes()); + } + + static FormalParameters readBytecodeAttribute(gnu.bytecode.MiscAttr attr) + { + String value = new String(attr.data); + try { + return + bossa.parser.Loader.getParser(value).formalParameters(false, null); + } + catch (bossa.parser.ParseException ex) { + return null; + } + } + private Parameter[] parameters; int size; Index: CustomConstructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CustomConstructor.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** CustomConstructor.java 14 Dec 2003 16:52:32 -0000 1.6 --- CustomConstructor.java 15 Dec 2003 02:40:17 -0000 1.7 *************** *** 18,21 **** --- 18,24 ---- import gnu.expr.*; import gnu.bytecode.ClassType; + import gnu.bytecode.Method; + import gnu.bytecode.Attribute; + import gnu.bytecode.MiscAttr; import nice.tools.code.Gen; *************** *** 24,36 **** */ ! public class CustomConstructor extends UserOperator { ! public CustomConstructor(LocatedString className, FormalParameters params, Block body) { super(new LocatedString("<init>", className.location()), Constraint.True, returnType(className), params, Contract.noContract); ! this.className = className; ! this.body = body; } --- 27,52 ---- */ ! public abstract class CustomConstructor extends UserOperator { ! public static CustomConstructor make ! (LocatedString className, FormalParameters params, Block body) ! { ! return new SourceCustomConstructor(className, params, body); ! } ! ! CustomConstructor(LocatedString className, FormalParameters params) { super(new LocatedString("<init>", className.location()), Constraint.True, returnType(className), params, Contract.noContract); + } ! CustomConstructor(NiceClass def, FormalParameters parameters) ! { ! super(new LocatedString("<init>", def.definition.location()), ! Constraint.True, ! returnType(def.definition.getName()), ! parameters, ! Contract.noContract); ! classe = def; } *************** *** 42,130 **** } ! void resolve() { ! TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(className); ! TypeConstructors.addConstructor(tc, this); ! classe = NiceClass.get(tc); ! ! // 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() { ! 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); ! } ! public void printInterface(java.io.PrintWriter s) ! { ! s.print("new " + className + "(" + parameters + ");\n"); } ! public void compile() { ! // Make sure the constructor is generated. ! getCode(); } ! protected gnu.expr.Expression computeCode() { ! if (classe.definition.inInterfaceFile()) ! { ! return new QuoteExp(classe.getClassExp().getClassType().getDeclaredMethod ! ("<init>", javaArgTypes())); ! } ! ConstructorExp lambda = Gen.createCustomConstructor ! ((ClassType) javaReturnType(), javaArgTypes(), getSymbols()); ! Gen.setMethodBody(lambda, body.generateCode()); ! classe.getClassExp().addMethod(lambda); ! return new QuoteExp(new InstantiateProc(lambda)); } - - LocatedString className; - Statement body; - NiceClass classe; } --- 58,196 ---- } ! 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(); } ! NiceClass classe; ! ! /**************************************************************** ! * A custom constructor defined in a source program. ! ****************************************************************/ ! ! static class SourceCustomConstructor extends CustomConstructor { ! SourceCustomConstructor(LocatedString className, FormalParameters params, ! Block body) ! { ! super(className, params); ! this.className = className; ! this.body = body; ! } ! ! void resolve() ! { ! TypeConstructor tc = Node.getGlobalTypeScope().globalLookup(className); ! TypeConstructors.addConstructor(tc, this); ! classe = NiceClass.get(tc); ! ! // 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() ! { ! 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); } ! protected gnu.expr.Expression computeCode() ! { ! ConstructorExp lambda = Gen.createCustomConstructor ! ((ClassType) javaReturnType(), javaArgTypes(), getSymbols()); ! Gen.setMethodBody(lambda, body.generateCode()); ! classe.getClassExp().addMethod(lambda); ! lambda.addBytecodeAttribute(parameters.asBytecodeAttribute()); ! return new QuoteExp(new InstantiateProc(lambda)); ! } ! ! LocatedString className; ! Statement body; } ! /**************************************************************** ! * Loading compiled custom constructors. ! ****************************************************************/ ! ! public static CustomConstructor load(NiceClass def, Method method) { ! if (! method.isConstructor()) ! return null; ! ! MiscAttr attr = (MiscAttr) Attribute.get(method, "parameters"); ! if (attr == null) ! return null; ! ! return new ImportedCustomConstructor(def, method, attr); } ! static class ImportedCustomConstructor extends CustomConstructor { ! ImportedCustomConstructor(NiceClass def, Method method, MiscAttr attr) ! { ! super(def, FormalParameters.readBytecodeAttribute(attr)); ! this.method = method; ! } ! void resolve() ! { ! TypeConstructors.addConstructor(classe.definition.tc, this); ! } ! protected gnu.expr.Expression computeCode() ! { ! int dummyArgs = method.arg_types.length - arity; ! return new QuoteExp(new InstantiateProc(method, dummyArgs)); ! } ! private Method method; } } |