01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2002 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package mlsub.typing;
12:
13: /**
14: * A type constructor implements an interface.
15: *
16: * @author bonniot
17: */
18:
19: public final class ImplementsCst extends AtomicConstraint {
20: public ImplementsCst(TypeConstructor tc, Interface itf) {
21: this .tc = tc;
22: this .itf = itf;
23: identifyVariances();
24: }
25:
26: /**
27: Assert that the type constructor and the interface have the same variance.
28:
29: This is not necessary, as it would be discovered later in the constraint
30: solver. However it is good to discover it sooner, for efficiency reasons
31: (less inequalities get frozen) and for earlier error reporting.
32: */
33: private void identifyVariances() {
34: if (tc.variance == null && itf.variance != null)
35: tc.setVariance(itf.variance);
36: }
37:
38: /**
39: * Perform type symbol substitution inside the constraint.
40: *
41: * Does not need to create a new object, but must not
42: * imperatively modify the constraint.
43: *
44: * @param map a map from TypeSymbols to TypeSymbols
45: * @return an atomic constraint with substitution performed
46: */
47: AtomicConstraint substitute(java.util.Map map) {
48: Object ttc;
49: ttc = map.get(tc);
50:
51: if (ttc == null)
52: return this ;
53:
54: return new ImplementsCst((TypeConstructor) ttc, itf);
55: }
56:
57: /**
58: * Enter the constraint into the typing context.
59: */
60: void enter() throws TypingEx {
61: Typing.assertImp(tc, itf, false);
62: }
63:
64: /****************************************************************
65: * Printing
66: ****************************************************************/
67:
68: public String toString() {
69: return tc + ":" + itf;
70: }
71:
72: private TypeConstructor tc;
73: private Interface itf;
74:
75: public TypeConstructor tc() {
76: return tc;
77: }
78:
79: public Interface itf() {
80: return itf;
81: }
82: }
|