]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/kinds/Kind.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / kinds / Kind.java
1 package org.simantics.scl.compiler.types.kinds;
2
3 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
4
5 /**
6  * This is an abstract base class for type kinds. It has three child classes:
7  * KCon for constants, KArrow for kind arrows and KMetaVar for kind meta-variables.
8  * 
9  * Child classes need to implement the method {@link #contains(KMetaVar)}, which
10  * returns true, if a given kind meta-variable points to this instance.
11  * 
12  * The default implementation of {@link #toString()} is infinitely recursive, with
13  * {@link #toString(TypeUnparsingContext, StringBuilder)} and {@link #toStringPar(TypeUnparsingContext, StringBuilder)}
14  * both calling each other. 
15  */
16 public abstract class Kind {
17     public static final Kind[] EMPTY_ARRAY = new Kind[0];
18     
19     @Override
20     public String toString() {
21         return toString(new TypeUnparsingContext());
22     }
23
24     public String toString(TypeUnparsingContext tuc) {
25         StringBuilder b = new StringBuilder();
26         toString(tuc, b);
27         return b.toString();
28     }
29
30     /**
31      * Write this kind to a string builder as an unparenthesized expression.
32      * If a subclass does not need parenthesis, it can implement the
33      * {@link #toStringPar(TypeUnparsingContext, StringBuilder)} method instead.
34      */
35     protected void toString(TypeUnparsingContext tuc, StringBuilder b) {
36         toStringPar(tuc, b);
37     }
38     
39     /**
40      * Write this kind to a string builder as an atomic expression. The default implementation
41      * encloses the output of {@link #toString(TypeUnparsingContext, StringBuilder)} in parenthesis.
42      */
43     protected void toStringPar(TypeUnparsingContext tuc, StringBuilder b) {
44         b.append("(");
45         toString(tuc, b);
46         b.append(")");
47     }
48
49     /**
50      * Return true, if this kind has been unified with the given kind meta-variable.
51      */
52     public abstract boolean contains(KMetaVar var);
53 }