/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */

package cx2x.translator.transformer;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cx2x.translator.common.GroupConfiguration;
import cx2x.translator.transformation.openacc.OpenAccContinuation;
import cx2x.xcodeml.transformation.*;
import cx2x.xcodeml.xnode.Xnode;
import org.w3c.dom.Element;

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

public class ClawTransformer implements Transformer {
  private int _transformationCounter = 0;

  // Hold all transformation groups
  private final Map<Class, TransformationGroup> _tGroups;

  // Hold cross-transformation elements
  private final Map<Element, Object> _crossTransformationTable;

  // Hold the module file cache
  private final ModuleCache _modCache;

  private int _maxColumns;


  /**
   * ClawTransformer ctor. Creates the transformation groups needed for the CLAW
   * transformation and order the accordingly to their interpretation order.
   * @param groups List of transformation groups that define the transformation
   *               order.
   * @param max    Maximum number of columns.
   */
  public ClawTransformer(List<GroupConfiguration> groups, int max){
    /*
     * Use LinkedHashMap to be able to iterate through the map
     * entries with the insertion order.
     */
    _tGroups = new LinkedHashMap<>();
    for(GroupConfiguration g : groups){
      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<>();

    _modCache = new ModuleCache();

    _maxColumns = max;
  }

  /**
   * @see Transformer#addTransformation(Transformation)
   */
  public void addTransformation(Transformation t){
    if(_tGroups.containsKey(t.getClass())){
      _tGroups.get(t.getClass()).add(t);
    }
  }

  /**
   * @see Transformer#getGroups()
   */
  public Map<Class, TransformationGroup> getGroups(){
    return _tGroups;
  }

  /**
   * Get the next extraction counter value.
   * @return Transformation counter value.
   */
  public int getNextTransformationCounter(){
    return _transformationCounter++;
  }

  /**
   * @see Transformer#getModCache()
   */
  @Override
  public ModuleCache getModCache() {
    return _modCache;
  }

  /**
   * @see Transformer#getMaxColumns()
   */
  @Override
  public int getMaxColumns() {
    return _maxColumns;
  }

  /**
   * @see Transformer#setMaxColumns(int)
   * @param max Max number of columns.
   */
  @Override
  public void setMaxColumns(int max) {
    _maxColumns = max;
  }

  /**
   * Get a stored element from a previous transformation.
   * @param key Key to use to retrieve the element.
   * @return The stored element if present. Null otherwise.
   */
  public Object hasElement(Xnode key){
    if(_crossTransformationTable.containsKey(key.getElement())){
      return _crossTransformationTable.get(key.getElement());
    }
    return null;
  }

  /**
   * Store a Xnode from a transformation for a possible usage in another
   * transformation. If a key is already present, the element is overwritten.
   * @param key   The element acting as a key.
   * @param value The element to be stored.
   */
  public void storeElement(Xnode key, Object value){
    if(_crossTransformationTable.containsKey(key.getElement())){
      _crossTransformationTable.remove(key.getElement());
    }
    _crossTransformationTable.put(key.getElement(), value);
  }
}

@@ -628,16 +628,17 @@
 ormer %7B%0A
+%0A
   privat
@@ -1129,32 +1129,37 @@
 retation order.%0A
+   *%0A
    * @param grou
@@ -1366,16 +1366,17 @@
 int max)
+ 
 %7B%0A    /*
@@ -1564,16 +1564,17 @@
  groups)
+ 
 %7B%0A      
@@ -1579,17 +1579,16 @@
   switch
- 
 (g.getTy
@@ -1592,16 +1592,17 @@
 tType())
+ 
 %7B%0A      
@@ -2356,16 +2356,17 @@
 ation t)
+ 
 %7B%0A    if
@@ -2397,24 +2397,25 @@
 getClass()))
+ 
 %7B%0A      _tGr
@@ -2554,16 +2554,17 @@
 Groups()
+ 
 %7B%0A    re
@@ -2629,24 +2629,29 @@
 nter value.%0A
+   *%0A
    * @return
@@ -2730,16 +2730,17 @@
 ounter()
+ 
 %7B%0A    re
@@ -3037,81 +3037,81 @@
  * @
-see Transformer#setMaxColumns(int)%0A   * @param max Max number of columns.
+param max Max number of columns.%0A   * @see Transformer#setMaxColumns(int)
 %0A   
@@ -3256,16 +3256,21 @@
 mation.%0A
+   *%0A
    * @pa
@@ -3416,16 +3416,17 @@
 ode key)
+ 
 %7B%0A    if
@@ -3474,32 +3474,33 @@
 y.getElement()))
+ 
 %7B%0A      return _
@@ -3732,16 +3732,21 @@
 ritten.%0A
+   *%0A
    * @pa
@@ -3885,16 +3885,17 @@
 t value)
+ 
 %7B%0A    if
@@ -3951,16 +3951,17 @@
 ment()))
+ 
 %7B%0A      

Format translator.transformer