/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */
package claw.tatsu.primitive;

import claw.tatsu.xcodeml.xnode.Xname;
import claw.tatsu.xcodeml.xnode.common.Xcode;
import claw.tatsu.xcodeml.xnode.common.Xnode;

import java.util.List;
import java.util.Set;

/**
 * Primitive analysis and transformation on condition node. This included:
 * - Check condition dependency on certain variables.
 *
 * @author clementval
 */
public final class Condition {

  // Avoid instantiation of this class
  private Condition() {
  }

  /**
   * Check whether the condition depends on some variables.
   *
   * @param condition Condition element to check.
   * @param variables List of variable name.
   * @return True if the condition depend on a variable in the list. False
   * otherwise.
   */
  public static boolean dependsOn(Xnode condition, Set<String> variables) {
    if(!Xnode.isOfCode(condition, Xcode.CONDITION) || variables.isEmpty()) {
      return false;
    }
    List<Xnode> vars = condition.matchAll(Xcode.VAR);
    for(Xnode var : vars) {
      if(var.isNotArrayIndex() && variables.contains(var.value())) {
        return true;
      }
    }
    return false;
  }

  /**
   * Check if the condition is checking if a variable is allocated.
   *
   * @param condition Condition node to check.
   * @return True if the condition is using the allocated intrinsic. False
   * otherwise.
   */
  public static boolean isAllocationRelated(Xnode condition) {
    if(!Xnode.isOfCode(condition, Xcode.CONDITION)) {
      return false;
    }

    List<Xnode> nodes = condition.matchAll(Xcode.FUNCTION_CALL);
    if(nodes.isEmpty()) {
      return false;
    }

    for(Xnode node : nodes) {
      Xnode name = node.matchSeq(Xcode.NAME);
      if(name.value().equals(Xname.F_INTR_ALLOCATED)) {
        return true;
      }
    }
    return false;
  }

}


@@ -1014,26 +1014,14 @@
     
-List%3CXnode%3E vars =
+return
  con
@@ -1050,50 +1050,42 @@
 VAR)
-;%0A    for(Xnode var : vars) %7B%0A      if(var
+.stream()%0A        .anyMatch(v -%3E v
 .isN
@@ -1122,18 +1122,16 @@
 ntains(v
-ar
 .value()
@@ -1136,62 +1136,8 @@
 ()))
- %7B%0A        return true;%0A      %7D%0A    %7D%0A    return false
 ;%0A  

Update dependsOn()