[ClavaAst] Adds ConstantArrayType.getArraySize() and .getArrayDims()
package pt.up.fe.specs.clava.ast.type;
import java.math.BigInteger;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import org.suikasoft.jOptions.Datakey.DataKey;
import org.suikasoft.jOptions.Datakey.KeyFactory;
import org.suikasoft.jOptions.Interfaces.DataStore;
import pt.up.fe.specs.clava.ClavaNode;
+import pt.up.fe.specs.util.SpecsMath;
/**
* Represents a C array with a specified constant size.
*
* <p>
- * For multiple dimensions there will be nested {@link ConstantArrayType} nodes. Top-most nodes represent outermost
- * dimensions of the array. E.g., int [3][4] will have dimension [4] as topmost node, them [3], then int.
+ * For multiple dimensions there will be nested {@link ConstantArrayType} nodes, accessible through getElementType().
+ * Top-most nodes represent outermost dimensions of the array. E.g., int [3][4] will have dimension [3] as topmost node,
+ * them [4], then int.
*
* <p>
* In this regard, the AST is opposite to what Clang does (topmost nodes represent innermost dimensions).
@@ -70,6 +74,29 @@ public class ConstantArrayType extends ArrayType {
// return constant;
}
+ public int getArraySize() {
+ return (int) SpecsMath.multiply(getArrayDims());
+ }
+
+ public List<Integer> getArrayDims() {
+
+ List<Integer> dims = new ArrayList<>();
+ getArrayDims(dims);
+ return dims;
+ }
+
+ private void getArrayDims(List<Integer> dims) {
+ dims.add(getConstant());
+
+ var elementType = getElementType();
+
+ if (!(elementType instanceof ConstantArrayType)) {
+ return;
+ }
+
+ ((ConstantArrayType) elementType).getArrayDims(dims);
+ }
+
// public BigInteger getConstant() {
// return get(ARRAY_SIZE);
// // return constant;
