| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.compiler.index; |
|---|
| 19 | |
|---|
| 20 | import java.util.HashSet; |
|---|
| 21 | import java.util.Map; |
|---|
| 22 | import java.util.Set; |
|---|
| 23 | |
|---|
| 24 | import com.sun.fortress.nodes.APIName; |
|---|
| 25 | import com.sun.fortress.nodes.CompilationUnit; |
|---|
| 26 | import com.sun.fortress.nodes.Id; |
|---|
| 27 | import com.sun.fortress.nodes.IdOrOpOrAnonymousName; |
|---|
| 28 | import com.sun.fortress.nodes.Import; |
|---|
| 29 | import com.sun.fortress.nodes.ImportedNames; |
|---|
| 30 | import com.sun.fortress.nodes.NodeAbstractVisitor_void; |
|---|
| 31 | import com.sun.fortress.nodes.Op; |
|---|
| 32 | |
|---|
| 33 | import edu.rice.cs.plt.collect.CollectUtil; |
|---|
| 34 | import edu.rice.cs.plt.collect.Relation; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | public abstract class CompilationUnitIndex { |
|---|
| 38 | |
|---|
| 39 | private final CompilationUnit _ast; |
|---|
| 40 | private final Map<Id, Variable> _variables; |
|---|
| 41 | private final Relation<IdOrOpOrAnonymousName, Function> _functions; |
|---|
| 42 | private final Set<ParametricOperator> _parametricOperators; |
|---|
| 43 | private final Map<Id, TypeConsIndex> _typeConses; |
|---|
| 44 | private final Map<Id, Dimension> _dimensions; |
|---|
| 45 | private final Map<Id, Unit> _units; |
|---|
| 46 | private final long _modifiedDate; |
|---|
| 47 | |
|---|
| 48 | public CompilationUnitIndex(CompilationUnit ast, |
|---|
| 49 | Map<Id, Variable> variables, |
|---|
| 50 | Relation<IdOrOpOrAnonymousName, Function> functions, |
|---|
| 51 | Set<ParametricOperator> parametricOperators, |
|---|
| 52 | Map<Id, TypeConsIndex> typeConses, |
|---|
| 53 | Map<Id, Dimension> dimensions, |
|---|
| 54 | Map<Id, Unit> units, |
|---|
| 55 | long modifiedDate) { |
|---|
| 56 | _ast = ast; |
|---|
| 57 | _variables = CollectUtil.immutable(variables); |
|---|
| 58 | _functions = CollectUtil.immutable(functions); |
|---|
| 59 | _parametricOperators = CollectUtil.immutable(parametricOperators); |
|---|
| 60 | _typeConses = CollectUtil.immutable( |
|---|
| 61 | CollectUtil.union(typeConses, |
|---|
| 62 | CollectUtil.union(dimensions, units))); |
|---|
| 63 | _dimensions = CollectUtil.immutable(dimensions); |
|---|
| 64 | _units = CollectUtil.immutable(units); |
|---|
| 65 | _modifiedDate = modifiedDate; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public CompilationUnit ast() { return _ast; } |
|---|
| 69 | |
|---|
| 70 | public abstract Set<APIName> exports(); |
|---|
| 71 | |
|---|
| 72 | public Set<APIName> imports() { |
|---|
| 73 | final Set<APIName> result = new HashSet<APIName>(); |
|---|
| 74 | for (Import _import : ast().getImports()) { |
|---|
| 75 | _import.accept(new NodeAbstractVisitor_void() { |
|---|
| 76 | public void forImportedNames(ImportedNames that) { |
|---|
| 77 | result.add(that.getApiName()); |
|---|
| 78 | } |
|---|
| 79 | }); |
|---|
| 80 | } |
|---|
| 81 | return result; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public Map<Id, Variable> variables() { return _variables; } |
|---|
| 85 | |
|---|
| 86 | public Relation<IdOrOpOrAnonymousName, Function> functions() { return _functions; } |
|---|
| 87 | |
|---|
| 88 | public Set<ParametricOperator> parametricOperators() { return _parametricOperators; } |
|---|
| 89 | |
|---|
| 90 | public Map<Id, TypeConsIndex> typeConses() { return _typeConses; } |
|---|
| 91 | |
|---|
| 92 | public Map<Id, Dimension> dimensions() { return _dimensions; } |
|---|
| 93 | |
|---|
| 94 | public Map<Id, Unit> units() { return _units; } |
|---|
| 95 | |
|---|
| 96 | public long modifiedDate() { return _modifiedDate; } |
|---|
| 97 | |
|---|
| 98 | public boolean declared(IdOrOpOrAnonymousName name) { |
|---|
| 99 | if ( name instanceof Id ) { |
|---|
| 100 | Id id = (Id)name; |
|---|
| 101 | return ( _variables.keySet().contains(id) || |
|---|
| 102 | _functions.firstSet().contains(id) || |
|---|
| 103 | _typeConses.keySet().contains(id) || |
|---|
| 104 | _dimensions.keySet().contains(id) || |
|---|
| 105 | _units.keySet().contains(id) ); |
|---|
| 106 | } else { |
|---|
| 107 | if ( _functions.firstSet().contains(name) ) |
|---|
| 108 | return true; |
|---|
| 109 | else { |
|---|
| 110 | if ( name instanceof Op ) { |
|---|
| 111 | Op op = (Op)name; |
|---|
| 112 | for ( ParametricOperator opr : _parametricOperators ) { |
|---|
| 113 | if ( opr.name().getText().equals(op.getText()) ) |
|---|
| 114 | return true; |
|---|
| 115 | } |
|---|
| 116 | return false; |
|---|
| 117 | } else return false; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|