<commit_before>/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */
package claw.wani.x2t.translator;

import claw.shenron.transformation.DependentTransformationGroup;
import claw.shenron.transformation.IndependentTransformationGroup;
import claw.shenron.transformation.Transformation;
import claw.shenron.transformation.TransformationGroup;
import claw.shenron.translator.Translator;
import claw.tatsu.analysis.topology.DirectedGraph;
import claw.tatsu.analysis.topology.TopologicalSort;
import claw.tatsu.common.Message;
import claw.tatsu.xcodeml.exception.IllegalDirectiveException;
import claw.tatsu.xcodeml.exception.IllegalTransformationException;
import claw.tatsu.xcodeml.xnode.common.Xcode;
import claw.tatsu.xcodeml.xnode.common.XcodeProgram;
import claw.tatsu.xcodeml.xnode.common.Xnode;
import claw.wani.language.ClawPragma;
import claw.wani.transformation.internal.OpenAccContinuation;
import claw.wani.transformation.ll.caching.Kcaching;
import claw.wani.transformation.ll.directive.DirectivePrimitive;
import claw.wani.transformation.ll.loop.*;
import claw.wani.transformation.ll.utility.ArrayToFctCall;
import claw.wani.transformation.ll.utility.UtilityRemove;
import claw.wani.transformation.sca.ModelData;
import claw.wani.transformation.sca.Sca;
import claw.wani.transformation.sca.ScaForward;
import claw.wani.x2t.configuration.Configuration;
import claw.wani.x2t.configuration.GroupConfiguration;
import org.w3c.dom.Element;

import java.util.*;

/**
 * ClawTranslator stores all transformation groups applied during the
 * translation.
 *
 * @author clementval
 */

public class ClawTranslator implements Translator {

  // Hold all transformation groups
  private final Map<Class, TransformationGroup> _tGroups;
  // Hold cross-transformation elements
  private final Map<Element, Object> _crossTransformationTable;
  private final Map<ClawDirectiveKey, ClawPragma> _blockDirectives;
  private int _transformationCounter = 0;

  /**
   * ClawTranslator ctor. Creates the transformation groups needed for the CLAW
   * transformation and order the accordingly to their interpretation order.
   */
  public ClawTranslator() {
    /*
     * Use LinkedHashMap to be able to iterate through the map
     * entries with the insertion order.
     */
    _tGroups = new LinkedHashMap<>();
    for(GroupConfiguration g : Configuration.get().getGroups()) {
      switch(g.getType()) {
        case DEPENDENT:
          _tGroups.put(g.getTransformationClass(),
              new DependentTransformationGroup(g.getName()));
          break;
        case INDEPENDENT:
          _tGroups.put(g.getTransformationClass(),
              new IndependentTransformationGroup(g.getName()));
          break;
      }
    }

    // Internal transformations not specified by default configuration or user
    _tGroups.put(OpenAccContinuation.class,
        new IndependentTransformationGroup("internal-open-acc-continuation"));

    _crossTransformationTable = new HashMap<>();

    _blockDirectives = new Hashtable<>();
  }

  @Override
  public void generateTransformation(XcodeProgram xcodeml, Xnode pragma)
      throws IllegalTransformationException, IllegalDirectiveException
  {
    // Analyze the raw pragma with the CLAW language parser
    ClawPragma analyzedPragma = ClawPragma.analyze(pragma);

    // Create transformation object based on the directive
    switch(analyzedPragma.getDirective()) {
      case ARRAY_TO_CALL:
        addTransformation(xcodeml, new ArrayToFctCall(analyzedPragma));
        break;
      case KCACHE:
        addTransformation(xcodeml, new Kcaching(analyzedPragma));
        break;
      case LOOP_FUSION:
        addTransformation(xcodeml, new LoopFusion(analyzedPragma));
        break;
      case LOOP_INTERCHANGE:
        addTransformation(xcodeml, new LoopInterchange(analyzedPragma));
        break;
      case LOOP_EXTRACT:
        addTransformation(xcodeml, new LoopExtraction(analyzedPragma));
        break;
      case LOOP_HOIST:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case ARRAY_TRANSFORM:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case REMOVE:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case SCA:
        if(analyzedPragma.hasForwardClause()) {
          addTransformation(xcodeml, new ScaForward(analyzedPragma));
        } else {
          addTransformation(xcodeml, new Sca(analyzedPragma));
        }
        break;
      case PRIMITIVE:
        addTransformation(xcodeml, new DirectivePrimitive(analyzedPragma));
        break;
      case IF_EXTRACT:
        addTransformation(xcodeml, new IfExtract(analyzedPragma));
        break;
      // driver handled directives
      case IGNORE:
      case VERBATIM:
      case NO_DEP:
        break;
      default:
        throw new IllegalDirectiveException(null, "Unrecognized CLAW directive",
            pragma.lineNo());
    }
  }

  @Override
  public void finalize(XcodeProgram xcodeml)<commit_msg>Handle new block directive model-data<commit_after>/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */
package claw.wani.x2t.translator;

import claw.shenron.transformation.DependentTransformationGroup;
import claw.shenron.transformation.IndependentTransformationGroup;
import claw.shenron.transformation.Transformation;
import claw.shenron.transformation.TransformationGroup;
import claw.shenron.translator.Translator;
import claw.tatsu.analysis.topology.DirectedGraph;
import claw.tatsu.analysis.topology.TopologicalSort;
import claw.tatsu.common.Message;
import claw.tatsu.xcodeml.exception.IllegalDirectiveException;
import claw.tatsu.xcodeml.exception.IllegalTransformationException;
import claw.tatsu.xcodeml.xnode.common.Xcode;
import claw.tatsu.xcodeml.xnode.common.XcodeProgram;
import claw.tatsu.xcodeml.xnode.common.Xnode;
import claw.wani.language.ClawPragma;
import claw.wani.transformation.internal.OpenAccContinuation;
import claw.wani.transformation.ll.caching.Kcaching;
import claw.wani.transformation.ll.directive.DirectivePrimitive;
import claw.wani.transformation.ll.loop.*;
import claw.wani.transformation.ll.utility.ArrayToFctCall;
import claw.wani.transformation.ll.utility.UtilityRemove;
import claw.wani.transformation.sca.ModelData;
import claw.wani.transformation.sca.Sca;
import claw.wani.transformation.sca.ScaForward;
import claw.wani.x2t.configuration.Configuration;
import claw.wani.x2t.configuration.GroupConfiguration;
import org.w3c.dom.Element;

import java.util.*;

/**
 * ClawTranslator stores all transformation groups applied during the
 * translation.
 *
 * @author clementval
 */

public class ClawTranslator implements Translator {

  // Hold all transformation groups
  private final Map<Class, TransformationGroup> _tGroups;
  // Hold cross-transformation elements
  private final Map<Element, Object> _crossTransformationTable;
  private final Map<ClawDirectiveKey, ClawPragma> _blockDirectives;
  private int _transformationCounter = 0;

  /**
   * ClawTranslator ctor. Creates the transformation groups needed for the CLAW
   * transformation and order the accordingly to their interpretation order.
   */
  public ClawTranslator() {
    /*
     * Use LinkedHashMap to be able to iterate through the map
     * entries with the insertion order.
     */
    _tGroups = new LinkedHashMap<>();
    for(GroupConfiguration g : Configuration.get().getGroups()) {
      switch(g.getType()) {
        case DEPENDENT:
          _tGroups.put(g.getTransformationClass(),
              new DependentTransformationGroup(g.getName()));
          break;
        case INDEPENDENT:
          _tGroups.put(g.getTransformationClass(),
              new IndependentTransformationGroup(g.getName()));
          break;
      }
    }

    // Internal transformations not specified by default configuration or user
    _tGroups.put(OpenAccContinuation.class,
        new IndependentTransformationGroup("internal-open-acc-continuation"));

    _crossTransformationTable = new HashMap<>();

    _blockDirectives = new Hashtable<>();
  }

  @Override
  public void generateTransformation(XcodeProgram xcodeml, Xnode pragma)
      throws IllegalTransformationException, IllegalDirectiveException
  {
    // Analyze the raw pragma with the CLAW language parser
    ClawPragma analyzedPragma = ClawPragma.analyze(pragma);

    // Create transformation object based on the directive
    switch(analyzedPragma.getDirective()) {
      case ARRAY_TO_CALL:
        addTransformation(xcodeml, new ArrayToFctCall(analyzedPragma));
        break;
      case KCACHE:
        addTransformation(xcodeml, new Kcaching(analyzedPragma));
        break;
      case LOOP_FUSION:
        addTransformation(xcodeml, new LoopFusion(analyzedPragma));
        break;
      case LOOP_INTERCHANGE:
        addTransformation(xcodeml, new LoopInterchange(analyzedPragma));
        break;
      case LOOP_EXTRACT:
        addTransformation(xcodeml, new LoopExtraction(analyzedPragma));
        break;
      case LOOP_HOIST:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case ARRAY_TRANSFORM:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case REMOVE:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case SCA:
        if(analyzedPragma.hasForwardClause()) {
          addTransformation(xcodeml, new ScaForward(analyzedPragma));
        } else {
          addTransformation(xcodeml, new Sca(analyzedPragma));
        }
        break;
      case MODEL_DATA:
        HandleBlockDirective(xcodeml, analyzedPragma);
        break;
      case PRIMITIVE:
        addTransformation(xcodeml, new DirectivePrimitive(analyzedPragma));
        break;
      case IF_EXTRACT:
        addTransformation(xcodeml, new IfExtract(analyzedPragma));
        break;
      // driver handled directives
      case IGNORE:
      case VERBATIM:
      case NO_DEP:
        break;
      default:
        throw new IllegalDirectiveException(null, "Unrecognized CLAW directive",
            pragma.lineNo());
    }
  }

  @Override
  public void finalize(XcodeProgram xcodeml)
{'fraction_non_alphanumeric': 0.08280379313715906, 'fraction_numerical': 0.0009776126698602015, 'mean_word_length': 3.686211635364178, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7386932', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2942, 'n_words': 624}