* net/loveruby/cflat/ast/Scope.java: use LinkedHashMap to unify entities and entitiesMap (entities should be ordered).

* net/loveruby/cflat/ast/Frame.java: ditto.
* net/loveruby/cflat/ast/ToplevelScope.java: ditto.
* net/loveruby/cflat/ast/Scope.java: remove unused method #entities.  Use #variables instead.
* net/loveruby/cflat/ast/Frame.java: ditto.
* net/loveruby/cflat/ast/ConstantTable.java: use 
* net/loveruby/cflat/compiler/TypeResolver.java: remove useless code.


git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@3887 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
Minero Aoki 2008-02-10 18:59:21 +00:00
parent 8c885a42bd
commit 209116f66a
6 changed files with 79 additions and 49 deletions

View File

@ -1,3 +1,22 @@
Mon Feb 11 03:59:19 2008 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/ast/Scope.java: use LinkedHashMap to unify
entities and entitiesMap (entities should be ordered).
* net/loveruby/cflat/ast/Frame.java: ditto.
* net/loveruby/cflat/ast/ToplevelScope.java: ditto.
* net/loveruby/cflat/ast/Scope.java: remove unused method
#entities. Use #variables instead.
* net/loveruby/cflat/ast/Frame.java: ditto.
* net/loveruby/cflat/ast/ConstantTable.java: use
* net/loveruby/cflat/compiler/TypeResolver.java: remove useless
code.
Mon Feb 11 03:08:16 2008 Minero Aoki <aamine@loveruby.net> Mon Feb 11 03:08:16 2008 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/ast/Scope.java: ban all allocate* methods, * net/loveruby/cflat/ast/Scope.java: ban all allocate* methods,

View File

@ -1,13 +1,12 @@
package net.loveruby.cflat.ast; package net.loveruby.cflat.ast;
import java.util.*; import java.util.*;
public class ConstantTable public class ConstantTable {
{ protected Map table;
protected HashMap table;
protected long id; protected long id;
public ConstantTable() { public ConstantTable() {
table = new HashMap(); table = new LinkedHashMap();
id = 0; id = 0;
} }

View File

@ -11,7 +11,7 @@ public class Frame extends Scope {
Iterator scopes = allChildren(); Iterator scopes = allChildren();
while (scopes.hasNext()) { while (scopes.hasNext()) {
Scope s = (Scope)scopes.next(); Scope s = (Scope)scopes.next();
Iterator vars = s.variables(); Iterator vars = s.entities.values().iterator();
while (vars.hasNext()) { while (vars.hasNext()) {
DefinedVariable var = (DefinedVariable)vars.next(); DefinedVariable var = (DefinedVariable)vars.next();
if (var.isPrivate()) { if (var.isPrivate()) {
@ -27,7 +27,7 @@ public class Frame extends Scope {
} }
public Iterator localVariables() { public Iterator localVariables() {
return bodyScope().entities(); return bodyScope().variables();
} }
private Scope bodyScope() { private Scope bodyScope() {

View File

@ -6,8 +6,7 @@ import java.util.*;
public class Scope { public class Scope {
protected Scope parent; protected Scope parent;
protected List children; protected List children;
protected List entities; protected Map entities;
protected Map entitiesMap;
protected long numAllEntities; protected long numAllEntities;
public Scope(Scope up) { public Scope(Scope up) {
@ -15,8 +14,7 @@ public class Scope {
if (up != null) up.addChild(this); if (up != null) up.addChild(this);
children = new ArrayList(); children = new ArrayList();
numAllEntities = -1; numAllEntities = -1;
entities = new ArrayList(); entities = new LinkedHashMap();
entitiesMap = new HashMap();
} }
public boolean isToplevel() { public boolean isToplevel() {
@ -61,21 +59,20 @@ public class Scope {
/** Declare variable or function in this scope. */ /** Declare variable or function in this scope. */
// #@@range/declareEntity{ // #@@range/declareEntity{
public void declareEntity(Entity ent) { public void declareEntity(Entity ent) {
if (entitiesMap.containsKey(ent.name())) { if (entities.containsKey(ent.name())) {
throw new Error("duplicated entity: " + ent.name()); throw new Error("duplicated entity: " + ent.name());
} }
entities.add(ent); entities.put(ent.name(), ent);
entitiesMap.put(ent.name(), ent);
} }
// #@@} // #@@}
public boolean isDefinedLocally(String name) { public boolean isDefinedLocally(String name) {
return entitiesMap.containsKey(name); return entities.containsKey(name);
} }
// #@@range/get{ // #@@range/get{
public Entity get(String name) throws SemanticException { public Entity get(String name) throws SemanticException {
Entity ent = (Entity)entitiesMap.get(name); Entity ent = (Entity)entities.get(name);
if (ent != null) { if (ent != null) {
return ent; return ent;
} }
@ -85,26 +82,30 @@ public class Scope {
} }
// #@@} // #@@}
// Returns all function local variables defined in this scope.
// Does includes all nested local variables.
// Does NOT include static local variables.
public Iterator allVariables() {
return allEntities().iterator();
}
public Iterator variables() {
return entities();
}
public long numEntities() { public long numEntities() {
return entities.size(); return entities.size();
} }
// Returns local variables defined in this scope itself. // Returns local variables defined in this scope.
// Does NOT include nested local variables. // Does includes all nested local variables.
// Does NOT include static local variables. // Does NOT include static local variables.
public Iterator entities() { public Iterator variables() {
return entities.iterator(); return variablesList().iterator();
}
protected List variablesList() {
List result = new ArrayList();
Iterator ents = entities.values().iterator();
while (ents.hasNext()) {
Entity ent = (Entity)ents.next();
if (ent instanceof DefinedVariable) {
DefinedVariable var = (DefinedVariable)ent;
if (!var.isPrivate()) {
result.add(var);
}
}
}
return result;
} }
public long numAllEntities() { public long numAllEntities() {
@ -120,27 +121,34 @@ public class Scope {
return numAllEntities; return numAllEntities;
} }
// Returns all function local variables defined in this scope.
// Does includes all nested local variables.
// Does NOT include static local variables.
public Iterator allVariables() {
return allEntities().iterator();
}
protected List allEntities() { protected List allEntities() {
List result = new ArrayList(); List result = new ArrayList();
Iterator cs = allChildren(); Iterator scopes = allChildren();
while (cs.hasNext()) { while (scopes.hasNext()) {
Scope c = (Scope)cs.next(); Scope s = (Scope)scopes.next();
result.addAll(c.entities); result.addAll(s.variablesList());
} }
return result; return result;
} }
public void checkReferences(ErrorHandler h) { public void checkReferences(ErrorHandler h) {
Iterator ents = entities.iterator(); Iterator ents = entities.values().iterator();
while (ents.hasNext()) { while (ents.hasNext()) {
Entity ent = (Entity)ents.next(); Entity ent = (Entity)ents.next();
if (ent.isDefined() && ent.isPrivate() && !ent.isRefered()) { if (ent.isDefined() && ent.isPrivate() && !ent.isRefered()) {
h.warn(ent.location(), "unused variable: " + ent.name()); h.warn(ent.location(), "unused variable: " + ent.name());
} }
} }
Iterator cs = children.iterator(); Iterator scopes = children.iterator();
while (cs.hasNext()) { while (scopes.hasNext()) {
Scope s = (Scope)cs.next(); Scope s = (Scope)scopes.next();
s.checkReferences(h); s.checkReferences(h);
} }
} }

View File

@ -17,7 +17,7 @@ public class ToplevelScope extends Scope {
/** Searches and gets entity searching scopes upto ToplevelScope. */ /** Searches and gets entity searching scopes upto ToplevelScope. */
// #@@range/get{ // #@@range/get{
public Entity get(String name) throws SemanticException { public Entity get(String name) throws SemanticException {
Entity ent = (Entity)entitiesMap.get(name); Entity ent = (Entity)entities.get(name);
if (ent == null) { if (ent == null) {
throw new SemanticException("unresolved reference: " + name); throw new SemanticException("unresolved reference: " + name);
} }
@ -25,10 +25,22 @@ public class ToplevelScope extends Scope {
} }
// #@@} // #@@}
public Iterator variables() {
throw new Error("TopScope#variables called");
}
public Iterator allVariables() { public Iterator allVariables() {
throw new Error("TopScope#allVariables called"); throw new Error("TopScope#allVariables called");
} }
public long numAllEntities() {
throw new Error("TopScope#numAllEntities called");
}
protected List allEntities() {
throw new Error("TopScope#allEntities called");
}
protected List staticLocalVariables() { protected List staticLocalVariables() {
if (staticLocalVariables == null) { if (staticLocalVariables == null) {
staticLocalVariables = new ArrayList(); staticLocalVariables = new ArrayList();
@ -61,7 +73,7 @@ public class ToplevelScope extends Scope {
public List globalVariables() { public List globalVariables() {
List result = new ArrayList(); List result = new ArrayList();
List src = new ArrayList(); List src = new ArrayList();
src.addAll(entities); src.addAll(entities.values());
src.addAll(staticLocalVariables()); src.addAll(staticLocalVariables());
Iterator ents = src.iterator(); Iterator ents = src.iterator();
while (ents.hasNext()) { while (ents.hasNext()) {
@ -82,7 +94,7 @@ public class ToplevelScope extends Scope {
public List commonSymbols() { public List commonSymbols() {
List result = new ArrayList(); List result = new ArrayList();
List src = new ArrayList(); List src = new ArrayList();
src.addAll(entities); src.addAll(entities.values());
src.addAll(staticLocalVariables()); src.addAll(staticLocalVariables());
Iterator ents = src.iterator(); Iterator ents = src.iterator();
while (ents.hasNext()) { while (ents.hasNext()) {

View File

@ -76,7 +76,6 @@ public class TypeResolver extends Visitor {
public void visit(DefinedFunction func) { public void visit(DefinedFunction func) {
resolveFunctionHeader(func); resolveFunctionHeader(func);
//resolveLocalVariables(func);
visitNode(func.body()); visitNode(func.body());
} }
@ -93,13 +92,6 @@ public class TypeResolver extends Visitor {
} }
} }
protected void resolveLocalVariables(DefinedFunction func) {
Iterator vars = func.localVariables();
while (vars.hasNext()) {
visit((DefinedVariable)vars.next());
}
}
public void visit(AddressNode node) { public void visit(AddressNode node) {
super.visit(node); super.visit(node);
// to avoid SemanticError which occurs when getting type of // to avoid SemanticError which occurs when getting type of