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;
16 public class ValueRepository {
17 THashMap<String, ArrayList<DValueAst>> values =
18 new THashMap<String, ArrayList<DValueAst>>();
19 THashMap<String, ArrayList<DAnnotationAst>> annotations =
20 new THashMap<String, ArrayList<DAnnotationAst>>();
21 TObjectLongHashMap<String> locations = new TObjectLongHashMap<String>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR,
22 Locations.NO_LOCATION);
24 public String add(DValueAst value) throws NotPatternException {
25 EVar name = value.lhs.getPatternHead();
26 ArrayList<DValueAst> vs = values.get(name.name);
28 vs = new ArrayList<DValueAst>(2);
29 values.put(name.name, vs);
31 long oldLocation = locations.get(name.name);
32 if(oldLocation == Locations.NO_LOCATION || name.location < oldLocation)
33 locations.put(name.name, name.location);
38 public void addDefinitions(String name, ArrayList<DValueAst> valueList) {
39 ArrayList<DValueAst> vs = values.get(name);
41 vs = new ArrayList<DValueAst>(valueList.size());
47 public void addAnnotation(String name, DAnnotationAst annotation) {
48 ArrayList<DAnnotationAst> as = annotations.get(name);
50 as = new ArrayList<DAnnotationAst>(2);
51 annotations.put(name, as);
56 public void addAnnotations(String name, ArrayList<DAnnotationAst> annotationList) {
57 ArrayList<DAnnotationAst> as = annotations.get(name);
59 as = new ArrayList<DAnnotationAst>(annotationList.size());
60 annotations.put(name, as);
62 as.addAll(annotationList);
65 public Collection<String> getValueNames() {
66 return values.keySet();
69 public ArrayList<DValueAst> getDefinition(String name) {
70 return values.get(name);
73 public ArrayList<DAnnotationAst> getAnnotations(String name) {
74 return annotations.get(name);
77 public void addFrom(ValueRepository repo, String oldName, String newName) {
78 addDefinitions(newName, repo.getDefinition(oldName));
80 ArrayList<DAnnotationAst> as = repo.getAnnotations(oldName);
82 addAnnotations(newName, as);
85 public long getLocation(String name) {
86 return locations.get(name);