package jetbrains.mps.kotlin.typesystem;

/*Generated by MPS */

import jetbrains.mps.lang.typesystem.runtime.AbstractNonTypesystemRule_Runtime;
import jetbrains.mps.lang.typesystem.runtime.NonTypesystemRule_Runtime;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.typesystem.inference.TypeCheckingContext;
import jetbrains.mps.lang.typesystem.runtime.IsApplicableStatus;
import jetbrains.mps.smodel.SModelStereotype;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.kotlin.behavior.NodeTypeVarSubs;
import jetbrains.mps.internal.collections.runtime.IMapping;
import jetbrains.mps.kotlin.api.declaration.TypeParameterDeclaration;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.typechecking.TypecheckingFacade;
import jetbrains.mps.errors.messageTargets.MessageTarget;
import jetbrains.mps.errors.messageTargets.NodeMessageTarget;
import jetbrains.mps.errors.IErrorReporter;
import jetbrains.mps.kotlin.behavior.IType__BehaviorDescriptor;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SInterfaceConcept;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class check_IType_bounds_NonTypesystemRule extends AbstractNonTypesystemRule_Runtime implements NonTypesystemRule_Runtime {
  public check_IType_bounds_NonTypesystemRule() {
  }
  public void applyRule(final SNode type, final TypeCheckingContext typeCheckingContext, IsApplicableStatus status) {
    if (SModelStereotype.isStubModel(SNodeOperations.getModel(type))) {
      return;
    }

    NodeTypeVarSubs subs = new NodeTypeVarSubs(type);

    for (IMapping<TypeParameterDeclaration, SNode> entry : MapSequence.fromMap(subs.getMap())) {
      SNode specified = SLinkOperations.getTarget(SNodeOperations.as(entry.value(), CONCEPTS.TypeProjection$5e), LINKS.type$x3no);
      if ((specified == null)) {
        continue;
      }

      for (SNode bound : ListSequence.fromList(entry.key().getUpperBounds())) {
        if (!(TypecheckingFacade.getFromContext().isSubtype(specified, bound))) {
          {
            final MessageTarget errorTarget = new NodeMessageTarget();
            IErrorReporter _reporter_2309309498 = typeCheckingContext.reportTypeError(specified, "type argument is not within its bounds\nExpected: " + IType__BehaviorDescriptor.toString_id4nn3FPlZH$r.invoke(bound, ((boolean) false)) + "\nFound: " + IType__BehaviorDescriptor.toString_id4nn3FPlZH$r.invoke(specified, ((boolean) false)), "r:aff09eac-afd3-4057-bdd8-e02a572d1436(jetbrains.mps.kotlin.typesystem)", "2852204284038482479", null, errorTarget);
          }

          // one error per entry
          break;
        }
      }
    }
  }
  public SAbstractConcept getApplicableConcept() {
    return CONCEPTS.IType$Ni;
  }
  public IsApplicableStatus isApplicableAndPattern(SNode argument) {
    return new IsApplicableStatus(argument.getConcept().isSubConceptOf(getApplicableConcept()), null);
  }
  public boolean overrides() {
    return false;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept TypeProjection$5e = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af3ccL, "jetbrains.mps.kotlin.structure.TypeProjection");
    /*package*/ static final SInterfaceConcept IType$Ni = MetaAdapterFactory.getInterfaceConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af441L, "jetbrains.mps.kotlin.structure.IType");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink type$x3no = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af3ccL, 0x28bef6d7551af67fL, "type");
  }
}
