1 package org.simantics.scl.compiler.internal.parsing.translation;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
7 import org.simantics.scl.compiler.elaboration.expressions.EVar;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
10 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
12 import gnu.trove.impl.Constants;
13 import gnu.trove.map.hash.THashMap;
14 import gnu.trove.map.hash.TObjectLongHashMap;
15 import gnu.trove.set.hash.THashSet;
17 public class ValueRepository {
18 THashMap<String, ArrayList<DValueAst>> values =
19 new THashMap<String, ArrayList<DValueAst>>();
20 THashMap<String, ArrayList<DAnnotationAst>> annotations =
21 new THashMap<String, ArrayList<DAnnotationAst>>();
22 THashSet<String> derived = new THashSet<String>();
23 TObjectLongHashMap<String> locations = new TObjectLongHashMap<String>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR,
24 Locations.NO_LOCATION);
26 public String add(DValueAst value) throws NotPatternException {
27 EVar name = value.lhs.getPatternHead();
28 ArrayList<DValueAst> vs = values.get(name.name);
30 vs = new ArrayList<DValueAst>(2);
31 values.put(name.name, vs);
33 long oldLocation = locations.get(name.name);
34 if(oldLocation == Locations.NO_LOCATION || name.location < oldLocation)
35 locations.put(name.name, name.location);
40 public void addDefinitions(String name, ArrayList<DValueAst> valueList) {
41 ArrayList<DValueAst> vs = values.get(name);
43 vs = new ArrayList<DValueAst>(valueList.size());
49 public void addAnnotation(String name, DAnnotationAst annotation) {
50 ArrayList<DAnnotationAst> as = annotations.get(name);
52 as = new ArrayList<DAnnotationAst>(2);
53 annotations.put(name, as);
58 public void addAnnotations(String name, ArrayList<DAnnotationAst> annotationList) {
59 ArrayList<DAnnotationAst> as = annotations.get(name);
61 as = new ArrayList<DAnnotationAst>(annotationList.size());
62 annotations.put(name, as);
64 as.addAll(annotationList);
67 public Collection<String> getValueNames() {
68 return values.keySet();
71 public ArrayList<DValueAst> getDefinition(String name) {
72 return values.get(name);
75 public ArrayList<DAnnotationAst> getAnnotations(String name) {
76 return annotations.get(name);
79 public void addFrom(ValueRepository repo, String oldName, String newName) {
80 addDefinitions(newName, repo.getDefinition(oldName));
82 ArrayList<DAnnotationAst> as = repo.getAnnotations(oldName);
84 addAnnotations(newName, as);
87 public long getLocation(String name) {
88 return locations.get(name);
91 public boolean isDerived(String name) {
92 return derived.contains(name);
95 public void setDerived(String name) {