001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.calls.inference;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
022 import org.jetbrains.jet.lang.types.JetType;
023 import org.jetbrains.jet.lang.types.TypeSubstitutor;
024 import org.jetbrains.jet.lang.types.Variance;
025
026 import java.util.Map;
027 import java.util.Set;
028
029 public interface ConstraintSystem {
030
031 /**
032 * Registers variables in a constraint system.
033 */
034 void registerTypeVariables(@NotNull Map<TypeParameterDescriptor, Variance> typeVariables);
035
036 /**
037 * Returns a set of all registered type variables.
038 */
039 @NotNull
040 Set<TypeParameterDescriptor> getTypeVariables();
041
042 /**
043 * Adds a constraint that the constraining type is a subtype of the subject type.<p/>
044 * Asserts that only subject type may contain registered type variables. <p/>
045 *
046 * For example, for {@code "fun <T> id(t: T) {}"} to infer <tt>T</tt> in invocation <tt>"id(1)"</tt>
047 * should be generated a constraint <tt>"Int is a subtype of T"</tt> where T is a subject type, and Int is a constraining type.
048 */
049 void addSubtypeConstraint(@Nullable JetType constrainingType, @NotNull JetType subjectType, @NotNull ConstraintPosition constraintPosition);
050
051 /**
052 * Adds a constraint that the constraining type is a supertype of the subject type. <p/>
053 * Asserts that only subject type may contain registered type variables. <p/>
054 *
055 * For example, for {@code "fun <T> create() : T"} to infer <tt>T</tt> in invocation <tt>"val i: Int = create()"</tt>
056 * should be generated a constraint <tt>"Int is a supertype of T"</tt> where T is a subject type, and Int is a constraining type.
057 */
058 void addSupertypeConstraint(@Nullable JetType constrainingType, @NotNull JetType subjectType, @NotNull ConstraintPosition constraintPosition);
059
060 @NotNull
061 ConstraintSystemStatus getStatus();
062
063 /**
064 * Returns the resulting type constraints of solving the constraint system for specific type variable. <p/>
065 * Returns null if the type variable was not registered.
066 */
067 @NotNull
068 TypeBounds getTypeBounds(@NotNull TypeParameterDescriptor typeVariable);
069
070 /**
071 * Returns a result of solving the constraint system (mapping from the type variable to the resulting type projection). <p/>
072 * In the resulting substitution should be concerned: <p/>
073 * - type constraints <p/>
074 * - variance of the type variable // not implemented yet <p/>
075 * - type parameter bounds (that can bind type variables with each other). // not implemented yet
076 * If the addition of the 'expected type' constraint made the system fail,
077 * this constraint is not included in the resulting substitution.
078 */
079 @NotNull
080 TypeSubstitutor getResultingSubstitutor();
081
082 /**
083 * Returns a current result of solving the constraint system (mapping from the type variable to the resulting type projection).
084 * If there is no information for type parameter, returns type projection for DONT_CARE type.
085 */
086 @NotNull
087 TypeSubstitutor getCurrentSubstitutor();
088
089 ConstraintSystem copy();
090 }