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 {

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

  private void readElementInformation(){

  }

  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
    Node varClone = var.clone();
    Element arrayRef = xcodeml.getDocument().createElement(XelementName.F_ARRAY_REF);
    if(type != null){ // Set ref type of the Xvar
      arrayRef.setAttribute(XelementName.ATTR_TYPE, type);
    }
    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;
  }
}

@@ -459,16 +459,48 @@
 ment %7B%0A%0A
+  private String _type = null;%0A%0A
   public
@@ -644,230 +644,648 @@
 ()%7B%0A
-%0A  %7D%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    Node varClone = var.clone();
+    // TODO%0A%0A    _type = XelementHelper.getAttributeValue(baseElement%0A      , XelementName.ATTR_TYPE);%0A  %7D%0A%0A  public void append(XbaseElement element)%7B%0A    append(element, false);%0A  %7D%0A%0A  public void append(XbaseElement element, boolean cloneElement)%7B%0A    if(cloneElement)%7B%0A      Node clone = element.clone();%0A      baseElement.appendChild(clone);%0A    %7D else %7B%0A      baseElement.appendChild(element.getBaseElement());%0A    %7D%0A  %7D%0A%0A  /**%0A   * Create an empty arrayReg element in the given program%0A   * param type attribute of the element. If null, no attribute is set%0A   */%0A  public static XarrayRef createEmpty(XcodeProg xcodeml, String type)%7B
 %0A   
@@ -1318,32 +1318,39 @@
 l.getDocument().
+%0A      
 createElement(Xe
@@ -1399,36 +1399,8 @@
 ll)%7B
- // Set ref type of the Xvar
 %0A   
@@ -1461,24 +1461,257 @@
 ;%0A    %7D%0A    
+return new XarrayRef(arrayRef);%0A  %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 varR
@@ -1955,16 +1955,18 @@
 ment());
+*/
 %0A%0A%0A    /
@@ -2292,14 +2292,8 @@
 ef%3E%0A
-    */
 %0A%0A%0A%0A
@@ -2316,11 +2316,13 @@
 Ref;%0A  %7D
+*/
 %0A%7D%0A

Add createEmpty method