[Nice-commit] Nice/src/bossa/syntax RetypedJavaMethod.java,1.12,1.13 NiceClass.java,1.76,1.77 Method
Brought to you by:
bonniot
From: <bo...@us...> - 2003-12-22 01:31:04
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv26684/src/bossa/syntax Modified Files: RetypedJavaMethod.java NiceClass.java MethodDeclaration.java JavaMethod.java JavaConstructor.java FormalParameters.java CustomConstructor.java Constructor.java AST.java Log Message: Classes now have default constructors build upon their parent's custom constructors. Index: RetypedJavaMethod.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/RetypedJavaMethod.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** RetypedJavaMethod.java 26 Nov 2003 08:50:23 -0000 1.12 --- RetypedJavaMethod.java 22 Dec 2003 01:30:59 -0000 1.13 *************** *** 19,22 **** --- 19,24 ---- import nice.tools.code.Types; import nice.tools.code.TypeImport; + import gnu.expr.QuoteExp; + import gnu.expr.InitializeProc; import java.util.*; *************** *** 238,241 **** --- 240,248 ---- } + gnu.expr.Expression getConstructorInvocation(boolean omitDefaults) + { + return new QuoteExp(new InitializeProc(reflectMethod)); + } + /**************************************************************** * Module interface Index: NiceClass.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/NiceClass.java,v retrieving revision 1.76 retrieving revision 1.77 diff -C2 -d -r1.76 -r1.77 *** NiceClass.java 14 Dec 2003 16:52:32 -0000 1.76 --- NiceClass.java 22 Dec 2003 01:30:59 -0000 1.77 *************** *** 165,171 **** } ! FormalParameters.Parameter asParameter(TypeMap map) { ! Monotype type = Monotype.create(sym.syntacticType.resolve(map)); if (value == null) return new FormalParameters.NamedParameter(type, sym.getName(), true); --- 165,171 ---- } ! FormalParameters.Parameter asParameter() { ! Monotype type = sym.syntacticType; if (value == null) return new FormalParameters.NamedParameter(type, sym.getName(), true); *************** *** 312,331 **** to this overriding. */ ! void updateConstructorParameter ! (FormalParameters.Parameter[] inherited, int nb, TypeScope scope) { String name = sym.getName().toString(); ! Monotype type = Monotype.create(sym.syntacticType.resolve(scope)); ! for (int i = 0; i < nb; i++) ! if (inherited[i].match(name)) { if (value != null) ! inherited[i] = new FormalParameters.OptionalParameter ! (type, sym.getName(), true, value, ! inherited[i].value() == null || inherited[i].isOverriden()); else ! inherited[i].resetType(type); } } --- 312,333 ---- to this overriding. */ ! void updateConstructorParameter(List inherited) { String name = sym.getName().toString(); ! Monotype type = sym.syntacticType; ! for (int i = 1; i < inherited.size(); i++) { ! FormalParameters.Parameter param = (FormalParameters.Parameter) ! inherited.get(i); ! if (param.match(name)) { if (value != null) ! inherited.set(i, new FormalParameters.OptionalParameter ! (type, sym.getName(), true, value, ! param.value() == null || param.isOverriden())); else ! param.resetType(type); } + } } *************** *** 402,406 **** resolveFields(); resolveIntitializers(); ! createConstructor(); addPublicCloneMethod(); } --- 404,408 ---- resolveFields(); resolveIntitializers(); ! createDefaultConstructors(); addPublicCloneMethod(); } *************** *** 634,669 **** /** - Collect in 'constraints' the constraints set by each class - on the type parameters. - */ - private static FormalParameters.Parameter[][] getFieldsAsParameters - (TypeConstructor tc, int nbFields, List constraints, - TypeSymbol[] binders) - { - ClassDefinition sup = ClassDefinition.get(tc); - if (sup != null && sup.implementation instanceof NiceClass) - return ((NiceClass) sup.implementation). - getFieldsAsParameters(nbFields, constraints, binders); - - List constructors = TypeConstructors.getConstructors(tc); - if (constructors == null) - return new FormalParameters.Parameter[][] - { new FormalParameters.Parameter[nbFields] }; - - FormalParameters.Parameter[][] res = - new FormalParameters.Parameter[constructors.size()][]; - int n = 0; - for (Iterator i = constructors.iterator(); i.hasNext(); n++) - { - MethodDeclaration.Symbol m = (MethodDeclaration.Symbol) i.next(); - res[n] = new FormalParameters.Parameter[nbFields + m.arity]; - mlsub.typing.Monotype[] args = m.getMethodDeclaration().getArgTypes(); - for (int j = 0; j < args.length; j++) - res[n][j] = new FormalParameters.Parameter(Monotype.create(args[j])); - } - return res; - } - - /** @return the scope that maps the type parameters of the other class to the corresponding symbol in the constructor of this class. --- 636,639 ---- *************** *** 686,708 **** } ! private FormalParameters.Parameter[][] getFieldsAsParameters ! (int nbFields, List constraints, TypeSymbol[] binders) { ! nbFields += this.fields.length; ! FormalParameters.Parameter[][] res = getFieldsAsParameters ! (definition.getSuperClass(), nbFields, constraints, binders); ! if (fields.length == 0 && overrides.length == 0 && ! definition.classConstraint == null) ! return res; TypeScope scope = Node.getGlobalTypeScope(); Map map = null; if (binders != null) { ! // Constructs a type scope that maps the type parameters of this ! // class to the corresponding symbol in the constructor. scope = new TypeScope(scope); ! map = new HashMap(); TypeSymbol[] ourBinders = definition.getBinders(); for (int i = 0; i < binders.length; i++) --- 656,703 ---- } ! /** ! Collect in 'constraints' the constraints set by each class ! on the type parameters. ! */ ! private static List getNativeConstructorParameters ! (TypeConstructor tc, List constraints, TypeSymbol[] binders) { ! List constructors = TypeConstructors.getConstructors(tc); ! if (constructors == null) ! { ! List res = new ArrayList(10); ! List params = new ArrayList(10); ! // null stands for the Object() constructor ! params.add(null); ! res.add(params); ! return res; ! } ! List res = new ArrayList(constructors.size()); ! int n = 0; ! for (Iterator i = constructors.iterator(); i.hasNext(); n++) ! { ! MethodDeclaration.Symbol m = (MethodDeclaration.Symbol) i.next(); ! List params = new ArrayList(10); ! params.add(m.getMethodDeclaration()); ! res.add(params); ! mlsub.typing.Monotype[] args = m.getMethodDeclaration().getArgTypes(); ! for (int j = 0; j < args.length; j++) ! params.add(new FormalParameters.Parameter(Monotype.create(args[j]))); ! } ! return res; ! } + private List getParentConstructorParameters + (List constraints, TypeSymbol[] binders) + { TypeScope scope = Node.getGlobalTypeScope(); Map map = null; if (binders != null) { ! // Constructs a type scope that maps the type parameters of this ! // class to the corresponding symbol in the constructor. scope = new TypeScope(scope); ! map = new HashMap(); TypeSymbol[] ourBinders = definition.getBinders(); for (int i = 0; i < binders.length; i++) *************** *** 713,721 **** } ! updateConstructorParameters(res[0], res[0].length - nbFields, scope); ! for (int j = 0; j < res.length; j++) ! for (int i = fields.length, n = res[j].length - nbFields + i; --i >= 0;) ! res[j][--n] = fields[i].asParameter(scope); if (definition.classConstraint != null) --- 708,723 ---- } ! ArrayList res = new ArrayList(constructors.size()); ! for (Iterator i = constructors.iterator(); i.hasNext();) ! { ! MethodDeclaration decl = ((MethodDeclaration.Symbol) i.next()). ! getMethodDeclaration(); ! List params = new ArrayList(1 + decl.arity); ! params.add(decl); ! if (decl.arity > 0) ! params.addAll(decl.formalParameters().getParameters(scope)); ! res.add(params); ! } if (definition.classConstraint != null) *************** *** 732,744 **** } /** This must be done in a given class for every subclass, since they have different type parameters. */ ! private void updateConstructorParameters ! (FormalParameters.Parameter[] inherited, int nb, TypeScope scope) { for (int f = 0; f < overrides.length; f++) ! overrides[f].updateConstructorParameter(inherited, nb, scope); } --- 734,774 ---- } + private List getConstructorParameters(List constraints, TypeSymbol[] binders) + { + TypeConstructor supTC = definition.getSuperClass(); + + NiceClass sup = get(supTC); + List res; + if (sup == null) + res = getNativeConstructorParameters(supTC, constraints, binders); + else + res = sup.getParentConstructorParameters(constraints, binders); + + if (overrides.length > 0) + for (Iterator i = res.iterator(); i.hasNext();) + updateConstructorParameters((List) i.next()); + + if (fields.length > 0) + for (int j = 0; j < res.size(); j++) + { + List params = (List) res.get(j); + for (int i = 0; i < fields.length; i++) + params.add(fields[i].asParameter()); + } + + if (definition.resolvedConstraints != null) + constraints.addAll(Arrays.asList(definition.resolvedConstraints)); + + return res; + } + /** This must be done in a given class for every subclass, since they have different type parameters. */ ! private void updateConstructorParameters(List inherited) { for (int f = 0; f < overrides.length; f++) ! overrides[f].updateConstructorParameter(inherited); } *************** *** 761,766 **** private Constructor[] constructorMethod; ! private void createConstructor() { if (definition instanceof ClassDefinition.Interface) return; --- 791,800 ---- private Constructor[] constructorMethod; ! private void createDefaultConstructors() { + if (definition.inInterfaceFile()) + // The constructors are loaded from the compiled package. + return; + if (definition instanceof ClassDefinition.Interface) return; *************** *** 773,780 **** constraints = new LinkedList(); ! FormalParameters.Parameter[][] params = ! getFieldsAsParameters(0, constraints, binders); ! ! checkFields(params[0]); Constraint cst; --- 807,811 ---- constraints = new LinkedList(); ! List allConstructorParams = getConstructorParameters(constraints, binders); Constraint cst; *************** *** 786,826 **** cst = Constraint.True; ! constructorMethod = new Constructor[params.length]; ! for (int i = 0; i < params.length; i++) { ! FormalParameters values = new FormalParameters(params[i]); constructorMethod[i] = new Constructor ! (this, fields, i, definition.location(), values, cst, ! Monotype.resolve(definition.typeScope, values.types()), Monotype.sure(new MonotypeConstructor(definition.tc, definition.getTypeParameters()))); TypeConstructors.addConstructor(definition.tc, constructorMethod[i]); } - } - - private static gnu.expr.Expression objectConstructor = - new gnu.expr.QuoteExp - (new gnu.expr.InitializeProc - (gnu.bytecode.Type.pointer_type.getDeclaredMethod("<init>", 0))); - - gnu.expr.Expression getSuper(int index, boolean omitDefaults) - { - TypeConstructor tc = definition.getSuperClass(); - - if (tc == null) - return objectConstructor; - - ClassDefinition sup = ClassDefinition.get(tc); - if (sup != null && sup.implementation instanceof NiceClass) - return ((NiceClass) sup.implementation). - constructorMethod[index].getConstructorInvocation(omitDefaults); - - List constructors = TypeConstructors.getConstructors(tc); - JavaMethod m = (JavaMethod) - ((MethodDeclaration.Symbol) constructors.get(index)).getDefinition(); - return m.getConstructorInvocation(); } --- 817,844 ---- cst = Constraint.True; ! constructorMethod = new Constructor[allConstructorParams.size()]; ! for (int i = 0; i < allConstructorParams.size(); i++) { ! List argList = (List) allConstructorParams.get(i); ! MethodDeclaration parent = (MethodDeclaration) argList.get(0); ! argList = argList.subList(1, argList.size()); ! FormalParameters.Parameter[] args = (FormalParameters.Parameter[]) ! argList.toArray(new FormalParameters.Parameter[argList.size()]); ! ! // Check only once. ! if (i == 0) ! checkFields(args); ! ! FormalParameters values = new FormalParameters(args); constructorMethod[i] = new Constructor ! (this, fields, parent, definition.location(), values, cst, ! Monotype.resolve(definition.getLocalScope(), values.types()), Monotype.sure(new MonotypeConstructor(definition.tc, definition.getTypeParameters()))); TypeConstructors.addConstructor(definition.tc, constructorMethod[i]); } } Index: MethodDeclaration.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/MethodDeclaration.java,v retrieving revision 1.52 retrieving revision 1.53 diff -C2 -d -r1.52 -r1.53 *** MethodDeclaration.java 8 Dec 2003 18:39:21 -0000 1.52 --- MethodDeclaration.java 22 Dec 2003 01:30:59 -0000 1.53 *************** *** 58,61 **** --- 58,64 ---- addChild(parameters); + if (constraint == null) + constraint = bossa.syntax.Constraint.True; + // remember it to print the interface syntacticConstraint = constraint.toString(); *************** *** 399,402 **** --- 402,413 ---- // Default implementation. return getCodeInCallPosition(); + } + + /** Call to the constructor. This should be overriden by methods + that really give access to a constructor. + */ + gnu.expr.Expression getConstructorInvocation(boolean omitDefaults) + { + throw new Error("Constructor for " + this + " (" + this.getClass() + ")"); } Index: JavaMethod.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/JavaMethod.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** JavaMethod.java 30 Aug 2003 10:37:13 -0000 1.28 --- JavaMethod.java 22 Dec 2003 01:30:59 -0000 1.29 *************** *** 102,110 **** } - gnu.expr.Expression getConstructorInvocation() - { - return new QuoteExp(new InitializeProc(reflectMethod)); - } - /**************************************************************** * Module interface --- 102,105 ---- Index: JavaConstructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/JavaConstructor.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** JavaConstructor.java 30 Aug 2003 10:37:13 -0000 1.3 --- JavaConstructor.java 22 Dec 2003 01:30:59 -0000 1.4 *************** *** 39,41 **** --- 39,46 ---- return new QuoteExp(new InstantiateProc(reflectMethod)); } + + gnu.expr.Expression getConstructorInvocation(boolean omitDefaults) + { + return new QuoteExp(new InitializeProc(reflectMethod)); + } } Index: FormalParameters.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/FormalParameters.java,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** FormalParameters.java 15 Dec 2003 02:40:17 -0000 1.34 --- FormalParameters.java 22 Dec 2003 01:30:59 -0000 1.35 *************** *** 38,41 **** --- 38,46 ---- Monotype type; + Parameter cloneParam() + { + return new Parameter(type); + } + boolean match(String id) { return false; } boolean requiresName() { return false; } *************** *** 159,162 **** --- 164,174 ---- boolean requiresName() { return nameRequired; } + Parameter cloneParam() + { + NamedParameter res = new NamedParameter(type, name); + res.nameRequired = this.nameRequired; + return res; + } + boolean match(String id) { return name.toString().equals(id); } *************** *** 209,212 **** --- 221,229 ---- boolean isOverriden() { return overriden; } + Parameter cloneParam() + { + return new OptionalParameter(type, name, nameRequired, defaultValue, overriden); + } + void resolve() { *************** *** 525,528 **** --- 542,557 ---- } + return res; + } + + List getParameters(TypeScope scope) + { + ArrayList res = new ArrayList(size); + for (int i = 0; i < size; i++) + { + Parameter p = parameters[i].cloneParam(); + p.type = Monotype.create(p.type.resolve(scope)); + res.add(p); + } return res; } Index: CustomConstructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CustomConstructor.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** CustomConstructor.java 16 Dec 2003 12:59:50 -0000 1.9 --- CustomConstructor.java 22 Dec 2003 01:30:59 -0000 1.10 *************** *** 41,44 **** --- 41,53 ---- } + CustomConstructor(NiceClass def, FormalParameters parameters) + { + super(new LocatedString("<init>", def.definition.location()), + def.definition.classConstraint, + returnType(def.definition), parameters, + Contract.noContract); + classe = def; + } + void addConstructorCallSymbol() { *************** *** 50,64 **** gnu.expr.Expression compileInCallPosition() { ! return getInitializationCode(); } }); } - CustomConstructor(NiceClass def, FormalParameters parameters) - { - this(def.definition.getName(), parameters); - classe = def; - } - private static Monotype returnType(LocatedString className) { --- 59,67 ---- gnu.expr.Expression compileInCallPosition() { ! return getInitializationCode(true); } }); } private static Monotype returnType(LocatedString className) { *************** *** 68,71 **** --- 71,81 ---- } + private static Monotype returnType(ClassDefinition def) + { + mlsub.typing.Monotype res = Monotype.sure + (new mlsub.typing.MonotypeConstructor(def.tc, def.getTypeParameters())); + return Monotype.create(res); + } + public void printInterface(java.io.PrintWriter s) { *************** *** 79,83 **** } ! abstract gnu.expr.Expression getInitializationCode(); NiceClass classe; --- 89,93 ---- } ! abstract gnu.expr.Expression getInitializationCode(boolean implicitThis); NiceClass classe; *************** *** 167,176 **** classe.getClassExp().addMethod(lambda); lambda.addBytecodeAttribute(parameters.asBytecodeAttribute()); ! initializationCode = new QuoteExp(new InitializeProc(lambda, true)); return new QuoteExp(new InstantiateProc(lambda)); } ! gnu.expr.Expression getInitializationCode() { getCode(); --- 177,189 ---- classe.getClassExp().addMethod(lambda); 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(); *************** *** 178,184 **** --- 191,207 ---- } + 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; } *************** *** 219,226 **** } ! gnu.expr.Expression getInitializationCode() { int dummyArgs = method.arg_types.length - arity; ! return new QuoteExp(new InitializeProc(method, true, dummyArgs)); } --- 242,255 ---- } ! gnu.expr.Expression getConstructorInvocation(boolean omitDefaults) { int dummyArgs = method.arg_types.length - arity; ! return new QuoteExp(new InitializeProc(method, false, dummyArgs)); ! } ! ! gnu.expr.Expression getInitializationCode(boolean implicitThis) ! { ! int dummyArgs = method.arg_types.length - arity; ! return new QuoteExp(new InitializeProc(method, implicitThis, dummyArgs)); } Index: Constructor.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/Constructor.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Constructor.java 14 Dec 2003 16:52:32 -0000 1.10 --- Constructor.java 22 Dec 2003 01:30:59 -0000 1.11 *************** *** 35,39 **** class Constructor extends MethodDeclaration { ! Constructor(NiceClass classe, NiceClass.Field[] fields, int index, Location location, FormalParameters formals, --- 35,40 ---- class Constructor extends MethodDeclaration { ! Constructor(NiceClass classe, NiceClass.Field[] fields, ! MethodDeclaration parent, Location location, FormalParameters formals, *************** *** 47,51 **** this.classe = classe; this.fields = fields; ! this.index = index; mlsub.typing.Polytype type = new mlsub.typing.Polytype --- 48,52 ---- this.classe = classe; this.fields = fields; ! this.parent = parent; mlsub.typing.Polytype type = new mlsub.typing.Polytype *************** *** 64,68 **** private NiceClass classe; private NiceClass.Field[] fields; ! private int index; Expression getConstructorInvocation(boolean omitDefaults) --- 65,69 ---- private NiceClass classe; private NiceClass.Field[] fields; ! private MethodDeclaration parent; Expression getConstructorInvocation(boolean omitDefaults) *************** *** 129,148 **** if (classe.definition.inInterfaceFile()) ! { ! Method m = classe.getClassExp().getClassType().getDeclaredMethod ! ("<init>", argTypesArray); ! ! if (omitDefaults) ! { ! initializeOmitDefaults = new QuoteExp(new InitializeProc(m)); ! } ! else ! { ! initialize = new QuoteExp(new InitializeProc(m)); ! initializeFromConstructor = new QuoteExp(new InitializeProc(m, true)); ! instantiate = new QuoteExp(new InstantiateProc(m)); ! } ! return; ! } ConstructorExp lambda = Gen.createConstructor --- 130,134 ---- if (classe.definition.inInterfaceFile()) ! throw new Error("Constructors are loaded from the compiled package"); ConstructorExp lambda = Gen.createConstructor *************** *** 152,155 **** --- 138,143 ---- Gen.setMethodBody(lambda, body(thisExp, fullArgs, omitDefaults)); classe.getClassExp().addMethod(lambda); + if (! omitDefaults) + lambda.addBytecodeAttribute(parameters.asBytecodeAttribute()); if (omitDefaults) *************** *** 165,168 **** --- 153,161 ---- } + private static gnu.expr.Expression objectConstructor = + new gnu.expr.QuoteExp + (new gnu.expr.InitializeProc + (gnu.bytecode.Type.pointer_type.getDeclaredMethod("<init>", 0))); + private Expression callSuper(Expression thisExp, MonoSymbol[] args, boolean omitDefaults) *************** *** 177,181 **** } ! Expression superExp = classe.getSuper(index, omitDefaults); return new ApplyExp(superExp, (Expression[]) superArgs.toArray(new Expression[superArgs.size()])); --- 170,176 ---- } ! // A null parent means no parent class: call the Object constructor. ! Expression superExp = parent == null ? ! objectConstructor : parent.getConstructorInvocation(omitDefaults); return new ApplyExp(superExp, (Expression[]) superArgs.toArray(new Expression[superArgs.size()])); Index: AST.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/AST.java,v retrieving revision 1.55 retrieving revision 1.56 diff -C2 -d -r1.55 -r1.56 *** AST.java 13 Dec 2003 16:53:04 -0000 1.55 --- AST.java 22 Dec 2003 01:30:59 -0000 1.56 *************** *** 108,119 **** Node.setModule(module); ! // Classes are resolved first, since code can depend on them ! for(int i = 0; i < classes.length; i++) ! resolve(classes[i]); ! ! // Custom constructors depend classes, and code can depend on them for(int i = 0; i < customConstructors.length; i++) resolve(customConstructors[i]); for(Iterator i = children.iterator();i.hasNext();) { --- 108,120 ---- Node.setModule(module); ! // Resolve custom constructors early, classes depend on them for(int i = 0; i < customConstructors.length; i++) resolve(customConstructors[i]); + // Classes are then resolved, since code can depend on them + for(int i = 0; i < classes.length; i++) + resolve(classes[i]); + + // Resolve all the rest for(Iterator i = children.iterator();i.hasNext();) { |