package jetbrains.mps.baseLanguage.dataFlow;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.baseLanguage.behavior.Expression__BehaviorDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.typechecking.TypecheckingFacade;
import jetbrains.mps.lang.dataFlow.framework.Program;
import jetbrains.mps.lang.dataFlow.DataFlow;
import java.util.List;
import jetbrains.mps.lang.dataFlow.framework.instructions.Instruction;
import jetbrains.mps.lang.dataFlow.framework.instructions.WriteInstruction;
import java.util.Objects;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SProperty;

public class ConditionUtil {

  /**
   * Does not call isCompileTimeConstantValue()
   */
  public static Boolean getConditionConstant(SNode expr) {
    if (expr == null || SNodeOperations.getModel(expr) == null || !(SNodeOperations.isInstanceOf(expr, CONCEPTS.Expression$mB))) {
      return null;
    }
    Object compileTimeConstantValue = Expression__BehaviorDescriptor.getCompileTimeConstantValue_idi1LP2xI.invoke(expr, SNodeOperations.getModel(expr).getModule());
    if ((compileTimeConstantValue instanceof Boolean)) {
      return (Boolean) compileTimeConstantValue;
    } else {
      return null;
    }
  }

  /**
   * Does not call isCompileTimeConstantValue()
   */
  public static Boolean getConditionEffectivelyConstantValue(SNode expr) {
    if (expr == null || SNodeOperations.getModel(expr) == null || !(SNodeOperations.isInstanceOf(expr, CONCEPTS.Expression$mB))) {
      return null;
    }
    Object compileTimeConstantValue = Expression__BehaviorDescriptor.getCompileTimeConstantValue_idi1LP2xI.invoke(expr, SNodeOperations.getModel(expr).getModule());
    if ((boolean) Expression__BehaviorDescriptor.isCompileTimeConstant_idi1LOPRp.invoke(expr) && (compileTimeConstantValue instanceof Boolean)) {
      return (Boolean) compileTimeConstantValue;
    } else {
      // Handle effectively final boolean variables
      if (SNodeOperations.isInstanceOf(expr, CONCEPTS.VariableReference$TC)) {
        SNode variableDeclaration = SLinkOperations.getTarget(SNodeOperations.as(expr, CONCEPTS.VariableReference$TC), LINKS.variableDeclaration$N1XG);
        if (!(SPropertyOperations.getBoolean(variableDeclaration, PROPS.isFinal$gvTP)) && SConceptOperations.isExactly(SNodeOperations.asSConcept(SNodeOperations.getConcept(TypecheckingFacade.getFromContext().getTypeOf(variableDeclaration))), CONCEPTS.BooleanType$_u) && (SLinkOperations.getTarget(variableDeclaration, LINKS.initializer$2twD) != null) && (boolean) Expression__BehaviorDescriptor.isCompileTimeConstant_idi1LOPRp.invoke(SLinkOperations.getTarget(variableDeclaration, LINKS.initializer$2twD))) {
          SNode code = SNodeOperations.getParent(SNodeOperations.getNodeAncestor(variableDeclaration, CONCEPTS.StatementList$m_, false, false));
          Program program = DataFlow.buildProgram(code);
          List<Instruction> instructions = program.getInstructions();
          int count = 0;
          for (Instruction ins : instructions) {
            if (ins instanceof WriteInstruction && Objects.equals(variableDeclaration, ((WriteInstruction) ins).getVariable())) {
              count += 1;
            }
          }
          if (count == 1) {
            return (Boolean) Expression__BehaviorDescriptor.getCompileTimeConstantValue_idi1LP2xI.invoke(SLinkOperations.getTarget(variableDeclaration, LINKS.initializer$2twD), SNodeOperations.getModel(expr).getModule());
          }
        }
      }
      return null;
    }
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept Expression$mB = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506fL, "jetbrains.mps.baseLanguage.structure.Expression");
    /*package*/ static final SConcept VariableReference$TC = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c77f1e98L, "jetbrains.mps.baseLanguage.structure.VariableReference");
    /*package*/ static final SConcept StatementList$m_ = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList");
    /*package*/ static final SConcept BooleanType$_u = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf940d6513eL, "jetbrains.mps.baseLanguage.structure.BooleanType");
  }

  private static final class LINKS {
    /*package*/ static final SReferenceLink variableDeclaration$N1XG = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c77f1e98L, 0xf8cc6bf960L, "variableDeclaration");
    /*package*/ static final SContainmentLink initializer$2twD = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer");
  }

  private static final class PROPS {
    /*package*/ static final SProperty isFinal$gvTP = MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0x111f9e9f00cL, "isFinal");
  }
}
