public void testSingleDimensionalArray() { // Set up array type TyClass objectClass = makeStubObjectClass(); TyJavaArray arrayType = new TyJavaArray("", objectClass); TyChar charType = TyChar.get(); arrayType.addToEnd(charType); // Set up NodeList ConstInt dim = new ConstInt(TyInt.get(), "3", 3); NodeList dimensions = new NodeList(); dimensions.addLastChild(dim); TyPointer tp = new TyPointer(); tp.addToEnd(arrayType); ExpNewArray exp = new ExpNewArray(tp, dimensions); PointerDatum thePointer = runTest(exp); assertTrue(thePointer.deref() != null); assertTrue(thePointer.deref() instanceof ArrayDatum); ArrayDatum theArray = (ArrayDatum) thePointer.deref(); assertTrue(theArray.getLength().getValue() == 3); for (int i = 0; i < 3; i++) { assertTrue(theArray.getElement(i) instanceof CharDatum); } }
/** * Contructor * * @param t The type of the resulting expression. * @param initializer_args The arguments to the constructor. * @param initialization The call to the constructor. */ public ExpNew(TypeNode t, NodeList initializer_args, ExpressionNode initialization) { super("ExpNew"); Assert.check(initialization != null); this.initialization = initialization; this.stateDummy = new NoExpNode(); set_type(t); set_selector(SelectorAlways.construct()); set_stepper(StepperNew.construct()); this.initializer_args = initializer_args; // Syntax // 0 compute a string for the type id. Assert.check(t instanceof TyAbstractPointer); TypeNode pointee = ((TyAbstractPointer) t).getPointeeType(); String preSyntax = "new " + pointee.getTypeString(); // 1 compute the syntax array String[] syntax = new String[initializer_args.length() + 1]; if (initializer_args.isEmpty()) { syntax[0] = preSyntax + "()"; } else { syntax[0] = preSyntax + "("; for (int i = 1; i < initializer_args.length(); ++i) { syntax[i] = ","; } syntax[initializer_args.length()] = ")"; } set_syntax(syntax); }
public void testMultiDimensionalArray() { // Set up array type TyClass objectClass = makeStubObjectClass(); TyJavaArray tax = new TyJavaArray("", objectClass); TyJavaArray tay = new TyJavaArray("", objectClass); TyJavaArray taz = new TyJavaArray("", objectClass); TyPointer tpx = new TyPointer(); TyPointer tpy = new TyPointer(); TyInt intType = TyInt.get(); taz.addToEnd(intType); tpy.addToEnd(taz); tay.addToEnd(tpy); tpx.addToEnd(tay); tax.addToEnd(tpx); // Set up NodeList NodeList dimensions = new NodeList(); ConstInt x = new ConstInt(TyInt.get(), "3", 3); ConstInt y = new ConstInt(TyInt.get(), "4", 4); ConstInt z = new ConstInt(TyInt.get(), "5", 5); dimensions.addLastChild(x); dimensions.addLastChild(y); dimensions.addLastChild(z); TyPointer tp = new TyPointer(); tp.addToEnd(tax); ExpNewArray exp = new ExpNewArray(tp, dimensions); PointerDatum thePointer = runTest(exp); assertTrue(thePointer.deref() != null); assertTrue(thePointer.deref() instanceof ArrayDatum); ArrayDatum ax = (ArrayDatum) thePointer.deref(); assertEquals(3, ((IntDatum) ax.getLength()).getValue()); for (int i = 0; i < 3; i++) { assertTrue(ax.getElement(i) instanceof PointerDatum); PointerDatum px = (PointerDatum) ax.getElement(i); assertTrue(px.deref() instanceof ArrayDatum); ArrayDatum ay = (ArrayDatum) px.deref(); assertEquals(4, ((IntDatum) ay.getLength()).getValue()); for (int j = 0; j < 4; j++) { assertTrue(ay.getElement(j) instanceof PointerDatum); PointerDatum py = (PointerDatum) ay.getElement(j); assertTrue(py.deref() instanceof ArrayDatum); ArrayDatum az = (ArrayDatum) py.deref(); assertEquals(5, ((IntDatum) az.getLength()).getValue()); for (int k = 0; k < 5; k++) { assertTrue(az.getElement(k) instanceof IntDatum); IntDatum id = (IntDatum) az.getElement(k); assertTrue(id.getValue() == 0); } } } }
public void addListAtEnd(NodeList xtl) { for (int i = 0, sz = xtl.length(); i < sz; ++i) { lll.addElement(xtl.get(i)); } }
protected NodeList getChildrenForDump() { NodeList nl = new NodeList(); for (int i = 0; i < initializer_args.length(); ++i) nl.addLastChild(initializer_args.get(i)); nl.addLastChild(initialization); return nl; }