|
|
Sponsor |
Nice/src/bossa/syntax NiceFieldAccess.java,1.16,1.17 NiceClass.java,1.54,1.: msg#00132lang.nice.cvs
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv14144/src/bossa/syntax Modified Files: NiceFieldAccess.java NiceClass.java JavaFieldAccess.java IncrementExp.java FormalParameters.java FieldAccess.java EnumDefinition.java CallExp.java Log Message: Added field overriding. Index: NiceFieldAccess.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/NiceFieldAccess.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** NiceFieldAccess.java 28 Jul 2003 21:45:52 -0000 1.16 --- NiceFieldAccess.java 31 Jul 2003 18:45:35 -0000 1.17 *************** *** 50,53 **** --- 50,55 ---- } + public boolean isFinal() { return field.isFinal(); } + final NiceClass.Field field; Index: NiceClass.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/NiceClass.java,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** NiceClass.java 28 Jul 2003 21:45:52 -0000 1.54 --- NiceClass.java 31 Jul 2003 18:45:35 -0000 1.55 *************** *** 49,65 **** this.fields = noFields; else ! { ! this.fields = (Field[]) fields.toArray(new Field[fields.size()]); ! ! //do not enter fields into global scope ! for (int i = 0; i < this.fields.length; i++) ! this.fields[i].sym.propagate = Node.none; ! for (int i = 0; i < this.fields.length; i++) ! { ! NiceFieldAccess f = new NiceFieldAccess(this, this.fields[i]); ! this.fields[i].method = f; ! definition.addChild(f); ! } } } --- 49,63 ---- this.fields = noFields; else ! this.fields = (NewField[]) fields.toArray(new NewField[fields.size()]); ! } ! public void setOverrides(List overrides) ! { ! if (overrides == null || overrides.size() == 0) ! this.overrides = noOverrides; ! else ! { ! this.overrides = (OverridenField[]) ! overrides.toArray(new OverridenField[overrides.size()]); } } *************** *** 67,72 **** ClassDefinition definition; ! private static Field[] noFields = new Field[0]; /**************************************************************** * Fields --- 65,81 ---- ClassDefinition definition; ! private static NewField[] noFields = new NewField[0]; ! private static OverridenField[] noOverrides = new OverridenField[0]; + NiceClass getParent() + { + TypeConstructor tc = definition.getSuperClass(); + ClassDefinition sup = ClassDefinition.get(tc); + if (sup != null && sup.implementation instanceof NiceClass) + return ((NiceClass) sup.implementation); + else + return null; + } + /**************************************************************** * Fields *************** *** 79,100 **** if (definition instanceof ClassDefinition.Interface) User.error(sym, "An interface cannot have a field."); ! return new Field(sym, value, isFinal, isTransient, isVolatile); } ! public class Field { ! public Field(MonoSymbol sym, Expression value, ! boolean isFinal, boolean isTransient, boolean isVolatile) { this.sym = sym; this.value = value; - this.isFinal = isFinal; - this.isTransient = isTransient; - this.isVolatile = isVolatile; ! if (isFinal && isVolatile) ! throw User.error(sym, "A field cannot be final and volatile"); } void resolve(VarScope scope, TypeScope typeScope) { --- 88,124 ---- if (definition instanceof ClassDefinition.Interface) User.error(sym, "An interface cannot have a field."); ! ! return new NewField(sym, value, isFinal, isTransient, isVolatile); } ! public Field makeOverride (MonoSymbol sym, Expression value) { ! if (definition instanceof ClassDefinition.Interface) ! User.error(sym, "An interface cannot have a field."); ! ! return new OverridenField(sym, value); ! } ! ! abstract class Field ! { ! private Field(MonoSymbol sym, Expression value) { this.sym = sym; this.value = value; ! // Do not enter fields into global scope. ! sym.propagate = Node.none; ! ! method = new NiceFieldAccess(NiceClass.this, this); ! NiceClass.this.definition.addChild(method); } + NiceClass getDeclaringClass() + { + return NiceClass.this; + } + + abstract boolean isFinal(); + void resolve(VarScope scope, TypeScope typeScope) { *************** *** 107,123 **** } ! void createField() { ! method.fieldDecl = classe.addField ! (sym.name.toString(), Types.javaType(sym.type)); ! method.fieldDecl.setFlag(isTransient, gnu.expr.Declaration.TRANSIENT); ! method.fieldDecl.setFlag(isVolatile , gnu.expr.Declaration.VOLATILE); } ! void typecheck(NiceClass c) { if (value != null) { ! c.enterTypingContext(); mlsub.typing.Polytype declaredType = sym.getType(); --- 131,149 ---- } ! FormalParameters.Parameter asParameter(TypeMap map) { ! Monotype type = Monotype.create(sym.syntacticType.resolve(map)); ! if (value == null) ! return new FormalParameters.NamedParameter(type, sym.getName(), true); ! else ! return new FormalParameters.OptionalParameter ! (type, sym.getName(), true, value); } ! void typecheck() { if (value != null) { ! NiceClass.this.enterTypingContext(); mlsub.typing.Polytype declaredType = sym.getType(); *************** *** 139,171 **** { return - (isFinal ? "final " : "") + sym + (value == null ? "" : " = " + value); } ! FormalParameters.Parameter asParameter(TypeMap map) { ! Monotype type = Monotype.create(sym.syntacticType.resolve(map)); ! if (value == null) ! return new FormalParameters.NamedParameter(type, sym.getName(), true); ! else ! return new FormalParameters.OptionalParameter ! (type, sym.getName(), true, value); } ! NiceClass getDeclaringClass() { ! return NiceClass.this; } ! MonoSymbol sym; ! Expression value; boolean isFinal; boolean isTransient; boolean isVolatile; ! NiceFieldAccess method; } ! ! // Used to resolve fields, and constructor cosntraint. private TypeScope localScope; --- 165,324 ---- { return sym + (value == null ? "" : " = " + value); } ! MonoSymbol sym; ! Expression value; ! ! NiceFieldAccess method; ! } ! ! final class NewField extends Field ! { ! private NewField(MonoSymbol sym, Expression value, ! boolean isFinal, boolean isTransient, boolean isVolatile) { ! super(sym, value); ! this.isFinal = isFinal; ! this.isTransient = isTransient; ! this.isVolatile = isVolatile; ! ! if (isFinal && isVolatile) ! throw User.error(sym, "A field cannot be final and volatile"); } ! boolean isFinal() { return isFinal; } ! ! void createField() { ! method.fieldDecl = classe.addField ! (sym.name.toString(), Types.javaType(sym.type)); ! method.fieldDecl.setFlag(isTransient, gnu.expr.Declaration.TRANSIENT); ! method.fieldDecl.setFlag(isVolatile , gnu.expr.Declaration.VOLATILE); } ! void checkNoDuplicate(FormalParameters.Parameter[] fields, ! int rankInThisClass) ! { ! /* ! We check that there is no duplicate in all the inherited fields, ! but also in the fields of this class stricly before this one. ! */ ! int max = fields.length - NiceClass.this.fields.length + rankInThisClass; ! String name = sym.getName().toString(); ! for (int i = 0; i < max; i++) ! { ! if (fields[i].match(name)) ! User.error(sym, ! (max - i >= NiceClass.this.fields.length) ! ? "A field with the same name exists in a super-class" ! : "A field with the same name exists in this class"); ! } ! } ! ! public String toString() ! { ! return ! (isFinal ? "final " : "") + ! super.toString(); ! } ! boolean isFinal; boolean isTransient; boolean isVolatile; + } ! final class OverridenField extends Field ! { ! private OverridenField(MonoSymbol sym, Expression value) ! { ! super(sym, value); ! } ! ! boolean isFinal() { return true; } ! ! /** ! Update the type and default values for the constructor, according ! 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] = asParameter(scope); ! else ! inherited[i].resetType(type); ! } ! } ! ! void typecheck() ! { ! gnu.expr.Declaration decl = null; ! ! NiceClass parent = getParent(); ! if (parent != null) ! decl = parent.getOverridenField(this, value == null); ! ! if (decl == null) ! throw User.error(sym, ! "No field with this name exists in a super-class"); ! ! method.fieldDecl = decl; ! ! super.typecheck(); ! } ! ! /** ! @param checkValue ! Whether to check that the original field's value, if it exists, ! must be checked against the overriden type. ! @return the checkValue to be used for other versions of this field ! higher up in the hierarchy. ! */ ! boolean checkOverride(Field original, boolean checkValue) ! { ! NiceClass.this.enterTypingContext(); ! ! mlsub.typing.Monotype originalType = original.sym.syntacticType.resolve ! (original.getDeclaringClass().translationScope(NiceClass.this)); ! ! try { ! Typing.leq(this.sym.type, originalType); ! } ! catch (TypingEx ex) { ! User.error(this.sym, ! "The new type must be a subtype of the original type declared in " + original.getDeclaringClass() + ".\n" + ! "Original type: " + originalType); ! } ! ! if (checkValue && original.value != null) ! { ! try { ! Typing.leq(original.value.getType(), this.sym.getType()); ! } ! catch (mlsub.typing.TypingEx ex) { ! User.error(sym, "The default value declared in " + ! original.getDeclaringClass() + ! "\nis not compatible with the overriden type"); ! } ! return false; ! } ! ! return checkValue; ! } ! ! public String toString() ! { ! return "override " + super.toString(); ! } } ! ! // Used to resolve fields, and constructor constraint. private TypeScope localScope; *************** *** 182,190 **** private void resolveFields() { - if (fields.length == 0) - return; - for (int i = 0; i < fields.length; i++) fields[i].resolve(definition.scope, localScope); } --- 335,343 ---- private void resolveFields() { for (int i = 0; i < fields.length; i++) fields[i].resolve(definition.scope, localScope); + + for (int i = 0; i < overrides.length; i++) + overrides[i].resolve(definition.scope, localScope); } *************** *** 195,198 **** --- 348,379 ---- } + private gnu.expr.Declaration getOverridenField + (OverridenField field, boolean checkValue) + { + String name = field.sym.getName().toString(); + + for (int i = 0; i < fields.length; i++) + if (fields[i].sym.getName().toString().equals(name)) + { + if (! fields[i].isFinal) + User.error(field.sym, "The original field in class " + this + + " is not final, so its type cannot be overriden"); + + checkValue = field.checkOverride(fields[i], checkValue); + + return fields[i].method.fieldDecl; + } + + for (int i = 0; i < overrides.length; i++) + if (overrides[i].sym.getName().toString().equals(name)) + checkValue = field.checkOverride(overrides[i], checkValue); + + NiceClass parent = getParent(); + if (parent != null) + return parent.getOverridenField(field, checkValue); + else + return null; + } + /**************************************************************** * Initializers *************** *** 259,263 **** try { for (int i = 0; i < fields.length; i++) ! fields[i].typecheck(this); if (initializers.length != 0) --- 440,447 ---- try { for (int i = 0; i < fields.length; i++) ! fields[i].typecheck(); ! ! for (int i = 0; i < overrides.length; i++) ! overrides[i].typecheck(); if (initializers.length != 0) *************** *** 382,385 **** --- 566,590 ---- } + /** + @return the scope that maps the type parameters of the other class + to the corresponding symbol in the constructor of this class. + */ + private TypeScope translationScope(NiceClass other) + { + mlsub.typing.MonotypeVar[] typeParams = other.definition.getTypeParameters(); + TypeScope scope = Node.getGlobalTypeScope(); + Map map = null; + if (typeParams != null) + { + scope = new TypeScope(scope); + for (int i = 0; i < typeParams.length; i++) + try { + scope.addMapping(definition.classConstraint.typeParameters[i].getName(), typeParams[i]); + } catch(TypeScope.DuplicateName e) {} + } + + return scope; + } + private FormalParameters.Parameter[][] getFieldsAsParameters (int nbFields, List constraints, MonotypeVar[] typeParams) *************** *** 389,393 **** (definition.getSuperClass(), nbFields, constraints, typeParams); ! if (fields.length == 0 && definition.classConstraint == null) return res; --- 594,599 ---- (definition.getSuperClass(), nbFields, constraints, typeParams); ! if (fields.length == 0 && overrides.length == 0 && ! definition.classConstraint == null) return res; *************** *** 407,410 **** --- 613,618 ---- } + 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;) *************** *** 424,427 **** --- 632,656 ---- } + /** + 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); + } + + /** + This must be done only once per class. + */ + private void checkFields (FormalParameters.Parameter[] allFields) + { + int len = allFields.length; + for (int f = 0; f < fields.length; f++) + fields[f].checkNoDuplicate(allFields, f); + } + private Constructor[] constructorMethod; *************** *** 441,444 **** --- 670,675 ---- getFieldsAsParameters(0, constraints, typeParams); + checkFields(params[0]); + Constraint cst; if (typeParams != null) *************** *** 608,611 **** public String toString() { return definition.toString(); } ! private Field[] fields; } --- 839,843 ---- public String toString() { return definition.toString(); } ! private NewField[] fields; ! private OverridenField[] overrides; } Index: JavaFieldAccess.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/JavaFieldAccess.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** JavaFieldAccess.java 24 Jul 2003 21:26:20 -0000 1.14 --- JavaFieldAccess.java 31 Jul 2003 18:45:35 -0000 1.15 *************** *** 41,53 **** } ! private JavaFieldAccess(LocatedString className,String fieldName, ! LocatedString name, ! mlsub.typing.Constraint cst, ! mlsub.typing.Monotype returnType, mlsub.typing.Monotype[] parameters) { ! super(name, cst, parameters, returnType); ! this.className = className; ! this.fieldName = fieldName; } --- 41,56 ---- } ! private JavaFieldAccess(Field field, mlsub.typing.Monotype[] parameters) + throws Types.ParametricClassException, Types.NotIntroducedClassException { ! super(new LocatedString(field.getName(), Location.nowhere()), ! null, parameters, ! Types.monotype(field.getType(), /* sure: */ field.isFinal())); ! this.field = field; ! this.className = new LocatedString(field.getDeclaringClass().getName(), ! Location.nowhere()); ! this.fieldName = field.getName(); ! this.fieldDecl = new Declaration(fieldName, field); } *************** *** 64,78 **** params = null; ! JavaFieldAccess res = new JavaFieldAccess ! (new LocatedString(f.getDeclaringClass().getName(), ! Location.nowhere()), ! f.getName(), ! new LocatedString(f.getName(), Location.nowhere()), ! null, ! Types.monotype(f.getType(), /* sure: */ f.isFinal()), ! params); ! ! res.field = f; ! res.fieldDecl = new Declaration(f.getName(), f); if (Debug.javaTypes) --- 67,71 ---- params = null; ! JavaFieldAccess res = new JavaFieldAccess(f, params); if (Debug.javaTypes) *************** *** 89,92 **** --- 82,87 ---- } + public boolean isFinal() { return field.isFinal(); } + void buildScope(VarScope outer, TypeScope typeOuter) { Index: IncrementExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/IncrementExp.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** IncrementExp.java 2 Feb 2002 12:21:47 -0000 1.15 --- IncrementExp.java 31 Jul 2003 18:45:35 -0000 1.16 *************** *** 66,69 **** --- 66,72 ---- "so it should be a call to a FieldAccessMethod"); + if (access.isFinal()) + User.error(this, "Field " + access + " is final"); + return Inline.inline (new IncrementProc(access.fieldDecl, returnOld, increment), Index: FormalParameters.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/FormalParameters.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** FormalParameters.java 26 Jul 2003 22:02:01 -0000 1.28 --- FormalParameters.java 31 Jul 2003 18:45:35 -0000 1.29 *************** *** 65,68 **** --- 65,75 ---- LocatedString getName() { return null; } + void resetType(Monotype type) + { + this.type = type; + if (symbol != null) + symbol.syntacticType = type; + } + Symbol getSymbol() { Index: FieldAccess.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/FieldAccess.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** FieldAccess.java 2 Sep 2002 11:46:51 -0000 1.11 --- FieldAccess.java 31 Jul 2003 18:45:35 -0000 1.12 *************** *** 51,54 **** --- 51,56 ---- public boolean isFieldAccess() { return true; } + public abstract boolean isFinal(); + /**************************************************************** * Code generation Index: EnumDefinition.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/EnumDefinition.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** EnumDefinition.java 28 Jul 2003 17:50:50 -0000 1.4 --- EnumDefinition.java 31 Jul 2003 18:45:35 -0000 1.5 *************** *** 36,39 **** --- 36,40 ---- NiceClass impl = new NiceClass(classDef); impl.setFields(null); + impl.setOverrides(null); classDef.setImplementation(impl); Index: CallExp.java =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/CallExp.java,v retrieving revision 1.82 retrieving revision 1.83 diff -C2 -d -r1.82 -r1.83 *** CallExp.java 25 Jul 2003 16:13:10 -0000 1.82 --- CallExp.java 31 Jul 2003 18:45:35 -0000 1.83 *************** *** 301,305 **** resolveOverloading(); return function.getFieldAccessMethod(); ! } /**************************************************************** --- 301,305 ---- resolveOverloading(); return function.getFieldAccessMethod(); ! } /**************************************************************** *************** *** 343,346 **** --- 343,350 ---- FieldAccess access = function.getFieldAccessMethod(); + + if (access.isFinal()) + User.error(this, "Field " + access + " is final"); + switch (arguments.size()) { ------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Nice/testsuite/compiler/classes field-override.testsuite,NONE,1.1 fields.testsuite,1.10,1.11, bonniot |
|---|---|
| Next by Date: | Nice/src/gnu/bytecode Field.java,1.5,1.6, bonniot |
| Previous by Thread: | Nice/testsuite/compiler/classes field-override.testsuite,NONE,1.1 fields.testsuite,1.10,1.11, bonniot |
| Next by Thread: | Nice/src/gnu/bytecode Field.java,1.5,1.6, bonniot |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
Free MagazinesCisco NewsReceive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business. subscribe Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field. subscribe The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business. subscribe Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. subscribe Total Telecom Total Telecom is "The Economist of the communications industry". subscribe |
Home | sitemap
| advertise | OSDir is
an inevitable website.
|