package x2x.translator.xcodeml.xelement;

import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
 * The XarrayRef represents the FarrayRef (7.4.4) element in XcodeML
 * intermediate representation.
 *
 * Elements:
 * - Required:
 *   - varRef
 * - Optional:
 *   - arrayIndex TODO
 *   - indexRange TODO
 *   - FarrayConstructor TODO
 *   - FarrayRef TODO (XarrayRef)
 * Attributes:
 * - Optional: type (text)
 */

public class XarrayRef extends XbaseElement {

  private String _type = null;
  private Xvar _var = null;

  public XarrayRef(Element arrayRefElement){
    super(arrayRefElement);
    readElementInformation();
  }

  private void readElementInformation(){
    // TODO

    _type = XelementHelper.getAttributeValue(baseElement
      , XelementName.ATTR_TYPE);

    // Find Var element
    Element varElement = XelementHelper.findVar(baseElement);
    if(varElement != null){
      _var = new Xvar(varElement);
    }

  }

  public Xvar getVar(){
    return _var;
  }

  public void append(XbaseElement element){
    append(element, false);
  }

  public void append(XbaseElement element, boolean cloneElement){
    if(cloneElement){
      Node clone = element.clone();
      baseElement.appendChild(clone);
    } else {
      baseElement.appendChild(element.getBaseElement());
    }

    if(element instanceof Xvar){
      _var = (Xvar)element;
    }
  }

  /**
   * Create an empty arrayReg element in the given program
   * param type attribute of the element. If null, no attribute is set
   */
  public static XarrayRef createEmpty(XcodeProg xcodeml, String type){
    Element arrayRef = xcodeml.getDocument().
      createElement(XelementName.F_ARRAY_REF);
    if(type != null){
      arrayRef.setAttribute(XelementName.ATTR_TYPE, type);
    }
    return new XarrayRef(arrayRef);
  }



  /*public static Element create(XcodeProg xcodeml, Xvar var, String type, XarrayIndex index){
    // Make sure var is an array
    // TODO

    // Wrap the var with the array reference


    /*Element varRef = xcodeml.getDocument().createElement(XelementName.VAR_REF);
    varRef.setAttribute(XelementName.ATTR_TYPE, var.getType());
    varRef.appendChild(varClone);
    arrayRef.appendChild(varRef);
    arrayRef.appendChild(index.getBaseElement());*/


    /*<Var type="A7fa7b35045b0" scope="local">value1</Var>

    Create this element
    <FarrayRef type="Fint">
      <varRef type="A7fa7b35045b0">
        <Var type="A7fa7b35045b0" scope="local">value1</Var>
      </varRef>
      <arrayIndex>
        <Var type="Fint" scope="local">i</Var>
      </arrayIndex>
    </FarrayRef>




    return arrayRef;
  }*/
}

@@ -504,21 +504,27 @@
 ate Xvar
+Ref
  _var
+Ref
  = null;
@@ -849,16 +849,19 @@
 .findVar
+Ref
 (baseEle
@@ -905,16 +905,19 @@
     _var
+Ref
  = new X
@@ -919,16 +919,19 @@
 new Xvar
+Ref
 (varElem
@@ -965,15 +965,21 @@
 Xvar
+Ref
  getVar
+Ref
 ()%7B%0A
@@ -993,16 +993,19 @@
 urn _var
+Ref
 ;%0A  %7D%0A%0A 
@@ -1349,16 +1349,19 @@
 eof Xvar
+Ref
 )%7B%0A     
@@ -1369,16 +1369,19 @@
 _var
+Ref
  = (Xvar
 )ele
@@ -1376,16 +1376,19 @@
  = (Xvar
+Ref
 )element
@@ -1434,17 +1434,17 @@
  arrayRe
-g
+f
  element
@@ -1837,824 +1837,6 @@
   %7D%0A
-%0A%0A%0A  /*public static Element create(XcodeProg xcodeml, Xvar var, String type, XarrayIndex index)%7B%0A    // Make sure var is an array%0A    // TODO%0A%0A    // Wrap the var with the array reference%0A%0A%0A    /*Element varRef = xcodeml.getDocument().createElement(XelementName.VAR_REF);%0A    varRef.setAttribute(XelementName.ATTR_TYPE, var.getType());%0A    varRef.appendChild(varClone);%0A    arrayRef.appendChild(varRef);%0A    arrayRef.appendChild(index.getBaseElement());*/%0A%0A%0A    /*%3CVar type=%22A7fa7b35045b0%22 scope=%22local%22%3Evalue1%3C/Var%3E%0A%0A    Create this element%0A    %3CFarrayRef type=%22Fint%22%3E%0A      %3CvarRef type=%22A7fa7b35045b0%22%3E%0A        %3CVar type=%22A7fa7b35045b0%22 scope=%22local%22%3Evalue1%3C/Var%3E%0A      %3C/varRef%3E%0A      %3CarrayIndex%3E%0A        %3CVar type=%22Fint%22 scope=%22local%22%3Ei%3C/Var%3E%0A      %3C/arrayIndex%3E%0A    %3C/FarrayRef%3E%0A%0A%0A%0A%0A    return arrayRef;%0A  %7D*/%0A
 %7D%0A

Add access to required element