]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/types/TTupleAst.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / types / TTupleAst.java
1 package org.simantics.scl.compiler.internal.parsing.types;
2
3 import java.util.List;
4
5 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
6 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
7 import org.simantics.scl.compiler.types.Type;
8 import org.simantics.scl.compiler.types.Types;
9 import org.simantics.scl.compiler.types.kinds.Kind;
10 import org.simantics.scl.compiler.types.kinds.Kinds;
11
12 import gnu.trove.map.hash.TObjectIntHashMap;
13 import gnu.trove.set.hash.TIntHashSet;
14
15
16
17 public class TTupleAst extends TypeAst {
18     public final TypeAst[] components;
19     
20     public TTupleAst(TypeAst ... components) {
21         this.components = components;
22     }
23     
24     public TTupleAst(List<TypeAst> components) {
25         this(components.toArray(new TypeAst[components.size()]));
26     }
27
28     @Override
29     public void toString(StringBuilder b) {
30         b.append('(');
31         for(int i=0;i<components.length;++i) {
32             if(i > 0)
33                 b.append(", ");
34             components[i].toString(b);
35         }
36         b.append(')');
37     }
38
39     @Override
40     public Type toType(TypeTranslationContext context, Kind expectedKind) {
41         if(components.length == 1) {
42             return components[0].toType(context, expectedKind);
43         }
44         else {
45             context.unify(location, Kinds.STAR, expectedKind);
46             return Types.tuple(toTypes(context, components));
47         }
48     }
49     
50     @Override
51     public Type toType(TypeElaborationContext context) {
52         if(components.length == 1) {
53             return components[0].toType(context);
54         }
55         else {
56             return Types.tuple(toTypes(context, components));
57         }
58     }
59
60     @Override
61     public int getPrecedence() {
62         return 0;
63     }
64
65     @Override
66     public void collectReferences(TObjectIntHashMap<String> typeNameMap,
67             TIntHashSet set) {
68         for(TypeAst component : components)
69             component.collectReferences(typeNameMap, set);
70     }
71 }