package jetbrains.mps.baseLanguage.typesystem;

/*Generated by MPS */

import jetbrains.mps.lang.typesystem.runtime.AbstractInferenceRule_Runtime;
import jetbrains.mps.lang.typesystem.runtime.InferenceRule_Runtime;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.typesystem.inference.TypeCheckingContext;
import jetbrains.mps.lang.typesystem.runtime.IsApplicableStatus;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.typesystem.inference.EquationInfo;
import jetbrains.mps.errors.BaseQuickFixProvider;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class typeof_VariableDeclaration_InferenceRule extends AbstractInferenceRule_Runtime implements InferenceRule_Runtime {
  public typeof_VariableDeclaration_InferenceRule() {
  }
  public void applyRule(final SNode variableDeclaration, final TypeCheckingContext typeCheckingContext, IsApplicableStatus status) {
    final SNode initializer = SLinkOperations.getTarget(variableDeclaration, LINKS.initializer$2twD);
    SConcept sconcept = SNodeOperations.getConcept(initializer);
    boolean abstractInitializer = sconcept != null && sconcept.isAbstract();
    if ((initializer != null)) {
      if (abstractInitializer && ((SLinkOperations.getTarget(variableDeclaration, LINKS.type$a1UY) == null) || SConceptOperations.isExactly(SNodeOperations.asSConcept(SNodeOperations.getConcept(SLinkOperations.getTarget(variableDeclaration, LINKS.type$a1UY))), CONCEPTS.Type$bu))) {
        // needed for MPS-20925
        return;
      } else {
        if (!(CheckingUtil.isValidByteOrShortExpression(SLinkOperations.getTarget(variableDeclaration, LINKS.type$a1UY), initializer))) {
          {
            final SNode initType = typeCheckingContext.typeOf(initializer, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "4371905702104105830", true);
            typeCheckingContext.whenConcrete(initType, () -> {
              if (InferenceHelper.isInferredFromContext(typeCheckingContext.getExpandedNode(initType))) {
                {
                  SNode _nodeToCheck_1029348928467 = variableDeclaration;
                  EquationInfo _info_12389875345 = new EquationInfo(_nodeToCheck_1029348928467, null, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "4371905702104109662", 0, null);
                  {
                    BaseQuickFixProvider intentionProvider = null;
                    intentionProvider = new BaseQuickFixProvider("jetbrains.mps.baseLanguage.typesystem.AddCast_QuickFix", "4371905702104109663", false);
                    intentionProvider.putArgument("desiredType", variableDeclaration);
                    intentionProvider.putArgument("expression", initializer);
                    _info_12389875345.addIntentionProvider(intentionProvider);
                  }
                  {
                    BaseQuickFixProvider intentionProvider = null;
                    intentionProvider = new BaseQuickFixProvider("jetbrains.mps.baseLanguage.typesystem.ChangeExpectedType_QuickFix", "4371905702104109668", false);
                    intentionProvider.putArgument("desiredType", initializer);
                    intentionProvider.putArgument("expression", variableDeclaration);
                    _info_12389875345.addIntentionProvider(intentionProvider);
                  }
                  typeCheckingContext.createLessThanInequality((SNode) typeCheckingContext.getExpandedNode(initType), (SNode) typeCheckingContext.typeOf(variableDeclaration, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "4371905702104109674", true), false, true, _info_12389875345);
                }
              } else {
                if (!(typeCheckingContext.isSingleTypeComputation())) {
                  {
                    SNode _nodeToCheck_1029348928467 = variableDeclaration;
                    EquationInfo _info_12389875345 = new EquationInfo(_nodeToCheck_1029348928467, null, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "554301099359820442", 0, null);
                    {
                      BaseQuickFixProvider intentionProvider = null;
                      intentionProvider = new BaseQuickFixProvider("jetbrains.mps.baseLanguage.typesystem.AddCast_QuickFix", "4017912752403843828", false);
                      intentionProvider.putArgument("desiredType", variableDeclaration);
                      intentionProvider.putArgument("expression", initializer);
                      _info_12389875345.addIntentionProvider(intentionProvider);
                    }
                    {
                      BaseQuickFixProvider intentionProvider = null;
                      intentionProvider = new BaseQuickFixProvider("jetbrains.mps.baseLanguage.typesystem.ChangeExpectedType_QuickFix", "4017912752406141711", false);
                      intentionProvider.putArgument("desiredType", initializer);
                      intentionProvider.putArgument("expression", variableDeclaration);
                      _info_12389875345.addIntentionProvider(intentionProvider);
                    }
                    typeCheckingContext.createLessThanInequality((SNode) typeCheckingContext.getExpandedNode(initType), (SNode) typeCheckingContext.typeOf(variableDeclaration, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "554301099359820444", true), true, true, _info_12389875345);
                  }
                }
              }
            }, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "4371905702104105744", true, true);
          }
        } else {
          // Ensure the initializer is an IntegerType
          if (!(typeCheckingContext.isSingleTypeComputation())) {
            {
              SNode _nodeToCheck_1029348928467 = initializer;
              EquationInfo _info_12389875345 = new EquationInfo(_nodeToCheck_1029348928467, null, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "2388917569121565670", 0, null);
              {
                BaseQuickFixProvider intentionProvider = null;
                intentionProvider = new BaseQuickFixProvider("jetbrains.mps.baseLanguage.typesystem.AddCast_QuickFix", "2388917569142431385", false);
                intentionProvider.putArgument("desiredType", variableDeclaration);
                intentionProvider.putArgument("expression", initializer);
                _info_12389875345.addIntentionProvider(intentionProvider);
              }
              {
                BaseQuickFixProvider intentionProvider = null;
                intentionProvider = new BaseQuickFixProvider("jetbrains.mps.baseLanguage.typesystem.ChangeExpectedType_QuickFix", "2388917569142431390", false);
                intentionProvider.putArgument("desiredType", initializer);
                intentionProvider.putArgument("expression", variableDeclaration);
                _info_12389875345.addIntentionProvider(intentionProvider);
              }
              typeCheckingContext.createLessThanInequality((SNode) typeCheckingContext.typeOf(_nodeToCheck_1029348928467, "r:00000000-0000-4000-0000-011c895902c5(jetbrains.mps.baseLanguage.typesystem)", "2388917569121560283", true), (SNode) _quotation_createNode_qyir2r_a1a0e0a0b0a0a0a0d0b(), true, true, _info_12389875345);
            }
          }
        }
      }
    }
  }
  public SAbstractConcept getApplicableConcept() {
    return CONCEPTS.VariableDeclaration$Y0;
  }
  public IsApplicableStatus isApplicableAndPattern(SNode argument) {
    return new IsApplicableStatus(argument.getConcept().isSubConceptOf(getApplicableConcept()), null);
  }
  public boolean overrides() {
    return false;
  }
  private static SNode _quotation_createNode_qyir2r_a1a0e0a0b0a0a0a0d0b() {
    SNode quotedNode_1 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf940d22479L, "IntegerType"));
    quotedNode_1 = nb.getResult();
    return quotedNode_1;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink initializer$2twD = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer");
    /*package*/ static final SContainmentLink type$a1UY = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept Type$bu = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506dL, "jetbrains.mps.baseLanguage.structure.Type");
    /*package*/ static final SConcept VariableDeclaration$Y0 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, "jetbrains.mps.baseLanguage.structure.VariableDeclaration");
  }
}
