* 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>
* net/loveruby/cflat/ast/Scope.java: ban all allocate* methods,

View File

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

View File

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

View File

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

View File

@ -17,7 +17,7 @@ public class ToplevelScope extends Scope {
/** Searches and gets entity searching scopes upto ToplevelScope. */
// #@@range/get{
public Entity get(String name) throws SemanticException {
Entity ent = (Entity)entitiesMap.get(name);
Entity ent = (Entity)entities.get(name);
if (ent == null) {
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() {
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() {
if (staticLocalVariables == null) {
staticLocalVariables = new ArrayList();
@ -61,7 +73,7 @@ public class ToplevelScope extends Scope {
public List globalVariables() {
List result = new ArrayList();
List src = new ArrayList();
src.addAll(entities);
src.addAll(entities.values());
src.addAll(staticLocalVariables());
Iterator ents = src.iterator();
while (ents.hasNext()) {
@ -82,7 +94,7 @@ public class ToplevelScope extends Scope {
public List commonSymbols() {
List result = new ArrayList();
List src = new ArrayList();
src.addAll(entities);
src.addAll(entities.values());
src.addAll(staticLocalVariables());
Iterator ents = src.iterator();
while (ents.hasNext()) {

View File

@ -76,7 +76,6 @@ public class TypeResolver extends Visitor {
public void visit(DefinedFunction func) {
resolveFunctionHeader(func);
//resolveLocalVariables(func);
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) {
super.visit(node);
// to avoid SemanticError which occurs when getting type of