00001
00005 package edu.mit.csail.sdg.squander.spec;
00006
00007 import java.util.ArrayList;
00008 import java.util.HashMap;
00009 import java.util.List;
00010 import java.util.Map;
00011
00012 import edu.mit.csail.sdg.squander.spec.JType.Unary;
00013
00014 public class NameSpace {
00015
00016 private final Map<String, JType.Unary> locals;
00017 private final List<JType.Unary> scopes;
00018 private final JMethod method;
00019
00020
00021
00022
00023
00024 public static NameSpace forMethod(JMethod method) {
00025 List<JType.Unary> scopes = new ArrayList<JType.Unary>(1);
00026 scopes.add(method.declaringClass());
00027 return new NameSpace(method, method.params(), scopes);
00028 }
00029
00030 public static NameSpace forClass(Unary type) {
00031 List<Unary> scopes = new ArrayList<Unary>(1);
00032 scopes.add(type);
00033 Map<String, Unary> locals = new HashMap<String, Unary>(0);
00034 return new NameSpace(null, locals, scopes);
00035 }
00036
00037
00038
00039
00040
00041
00042 private NameSpace(JMethod method, Map<String, Unary> locals, List<Unary> scopes) {
00043 this.method = method;
00044 this.locals = locals;
00045 this.scopes = scopes;
00046 }
00047
00048 public NameSpace addLocal(String name, JType.Unary type) {
00049 HashMap<String, Unary> newLocals = new HashMap<String, Unary>(locals.size() + 1);
00050 newLocals.putAll(locals);
00051 newLocals.put(name, type);
00052 return new NameSpace(method, newLocals, scopes);
00053 }
00054
00055 public NameSpace addScope(JType.Unary clz) {
00056 List<JType.Unary> newScopes = new ArrayList<JType.Unary>(scopes.size() + 1);
00057 newScopes.addAll(scopes);
00058 newScopes.add(clz);
00059 return new NameSpace(method, locals, newScopes);
00060 }
00061
00062 public JField findField(String name, JavaScene scene) {
00063 JType.Unary declarer = scope();
00064 if (declarer == null)
00065 return null;
00066 ClassSpec clzSpec = scene.ensureClass(declarer);
00067 return clzSpec.ensureField(name);
00068 }
00069
00070 public JType.Unary findLocal(String name) {
00071 return locals.get(name);
00072 }
00073
00074 public boolean inArray() {
00075 JType.Unary clz = scope();
00076 if (clz == null)
00077 return false;
00078 return clz.clazz().isArray();
00079 }
00080
00081 public JMethod method() {
00082 return method;
00083 }
00084
00085 public JType.Unary scope() {
00086 if (scopes.isEmpty())
00087 return null;
00088 return scopes.get(scopes.size() - 1);
00089 }
00090
00091 public JType.Unary declarer() {
00092 if (scopes.isEmpty())
00093 return null;
00094 return scopes.get(0);
00095 }
00096
00097 }