Search in sources :

Example 1 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class ABSUnitTestCaseBuilder method makeSetStatements.

void makeSetStatements(Map<String, String> typeHierarchy, Map<String, List<Stmt>> initialisations, List<String> initialisationsOrders, String testName, Set<String> heapNames, Map<ABSRef, ABSObject> initialHeap, Map<String, InterfaceTypeUse> objectsInHeap, ABSRef ref, ABSObject state, ClassDecl testClass) {
    String rn = heapRefBuilder.heapReferenceForTest(testName, getABSDataValue(ref));
    String concreteTypeName = getABSObjectType(state);
    ClassDecl concreteType = getDecl(model, ClassDecl.class, new DeclNamePredicate<ClassDecl>(concreteTypeName));
    if (concreteType == null) {
        throw new IllegalStateException("Cannot find class: " + concreteTypeName);
    }
    List<Stmt> statements = new ArrayList<Stmt>();
    initialisations.put(rn, statements);
    if (!initialisationsOrders.contains(rn)) {
        initialisationsOrders.add(rn);
    }
    Map<String, ABSData> fields = getABSObjectFields(state);
    abs.frontend.ast.List<ParamDecl> params = concreteType.getParamList();
    PureExp[] constructorArgs = new PureExp[params.getNumChild()];
    for (int i = 0; i < params.getNumChild(); i++) {
        ParamDecl param = params.getChild(i);
        String name = param.getName();
        assert fields.containsKey(name);
        ABSData d = fields.remove(name);
        objectsInHeap.putAll(getTypesFromABSData(testName, d));
        PureExp exp = pureExpBuilder.createPureExpression(rn, initialisationsOrders, testName, heapNames, d);
        constructorArgs[i] = exp;
    }
    statements.add(getVAssign(rn, newObj(concreteType, false, constructorArgs)));
    for (String fn : fields.keySet()) {
        ABSData d = fields.get(fn);
        objectsInHeap.putAll(getTypesFromABSData(testName, d));
        PureExp exp = pureExpBuilder.createPureExpression(rn, initialisationsOrders, testName, heapNames, d);
        Call call = getCall(new VarUse(rn), testCaseNameBuilder.setterMethodName(fn), true, exp);
        statements.add(getExpStmt(call));
    }
    // ADD getter and setter
    deltaBuilder.updateDelta(typeHierarchy, objectsInHeap.get(rn), getDecl(model, ClassDecl.class, new DeclNamePredicate<ClassDecl>(concreteTypeName)));
}
Also used : Call(abs.frontend.ast.Call) AbsASTBuilderUtil.getCall(abs.backend.tests.AbsASTBuilderUtil.getCall) PreviousCall(apet.testCases.PreviousCall) DeclNamePredicate(abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate) ArrayList(java.util.ArrayList) PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse) AbsASTBuilderUtil.getExpStmt(abs.backend.tests.AbsASTBuilderUtil.getExpStmt) Stmt(abs.frontend.ast.Stmt) ClassDecl(abs.frontend.ast.ClassDecl) ParamDecl(abs.frontend.ast.ParamDecl) ABSData(apet.testCases.ABSData)

Example 2 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class ABSUnitTestCaseTranslatorHelper method createTestClass.

ClassDecl createTestClass(InterfaceDecl inf) {
    ClassDecl ct = new ClassDecl(testCaseNameBuilder.className(inf.getName()), new abs.frontend.ast.List<Annotation>(), new abs.frontend.ast.List<ParamDecl>(), new abs.frontend.ast.List<InterfaceTypeUse>(), new Opt<InitBlock>(), new abs.frontend.ast.List<CaseBranchStmt>(), new abs.frontend.ast.List<FieldDecl>(), new abs.frontend.ast.List<MethodImpl>());
    ct.addAnnotation(getTestAnnotation(suiteType));
    ct.addImplementedInterfaceUse(new InterfaceTypeUse(inf.getName(), new abs.frontend.ast.List<abs.frontend.ast.Annotation>()));
    for (MethodSig m : inf.getAllMethodSigs()) {
        ct.addMethod(createMethodImpl(m));
    }
    FieldDecl assertImpl = new FieldDecl();
    assertImpl.setName(ASSERT_HELPER);
    assertImpl.setAccess(absAssertImpl.getImplementedInterfaceUse(0).treeCopyNoTransform());
    ct.addField(assertImpl);
    InitBlock block = new InitBlock();
    block.addStmtNoTransform(getVAssign(ASSERT_HELPER, newObj(absAssertImpl, false)));
    ct.setInitBlock(block);
    return ct;
}
Also used : InitBlock(abs.frontend.ast.InitBlock) CaseBranchStmt(abs.frontend.ast.CaseBranchStmt) Annotation(abs.frontend.ast.Annotation) FieldDecl(abs.frontend.ast.FieldDecl) AbsASTBuilderUtil.createMethodSig(abs.backend.tests.AbsASTBuilderUtil.createMethodSig) MethodSig(abs.frontend.ast.MethodSig) ClassDecl(abs.frontend.ast.ClassDecl) AbsASTBuilderUtil.createMethodImpl(abs.backend.tests.AbsASTBuilderUtil.createMethodImpl) MethodImpl(abs.frontend.ast.MethodImpl) ParamDecl(abs.frontend.ast.ParamDecl) InterfaceTypeUse(abs.frontend.ast.InterfaceTypeUse)

Example 3 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class DeltaForGetSetFieldsBuilder method addSetter.

/**
 * Add an add method modifier
 * @param fieldName
 * @param exp
 * @param decl
 * @return
 */
AddMethodModifier addSetter(String fieldName, Access type) {
    MethodSig sig = new MethodSig(testCaseNameBuilder.setterMethodName(fieldName), new abs.frontend.ast.List<Annotation>(), getUnit(), new abs.frontend.ast.List<ParamDecl>());
    sig.addParam(new ParamDecl("v", type, new abs.frontend.ast.List<Annotation>()));
    Block block = new Block();
    block.addStmtNoTransform(getVAssign(new FieldUse(fieldName), new VarUse("v")));
    MethodImpl method = new MethodImpl(sig, block, false);
    AddMethodModifier modifier = new AddMethodModifier(method);
    return modifier;
}
Also used : AddMethodModifier(abs.frontend.ast.AddMethodModifier) VarUse(abs.frontend.ast.VarUse) Annotation(abs.frontend.ast.Annotation) AbsASTBuilderUtil.createMethodSig(abs.backend.tests.AbsASTBuilderUtil.createMethodSig) MethodSig(abs.frontend.ast.MethodSig) MethodImpl(abs.frontend.ast.MethodImpl) ParamDecl(abs.frontend.ast.ParamDecl) FieldUse(abs.frontend.ast.FieldUse) Block(abs.frontend.ast.Block)

Example 4 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class SchedulerChecker method checkScheduleExp.

private void checkScheduleExp(PureExp sched, ClassDecl class_decl, ASTNode<?> loc) {
    if (sched == null)
        return;
    if (!(sched instanceof FnApp)) {
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_ANNOTATION_TYPE, sched.getType()));
        return;
    }
    FnApp s = (FnApp) sched;
    Type scheduler_type = s.getType();
    if (s.getDecl().isUnknown()) {
        errors.add(new TypeError(loc, ErrorMessage.FUNCTION_NOT_RESOLVABLE, s.getName()));
        return;
    }
    FunctionDecl sd = (FunctionDecl) s.getDecl();
    // check scheduling function return type
    boolean schedulerTypeCorrect = scheduler_type.isDataType() && ((DataTypeType) scheduler_type).getQualifiedName().equals("ABS.Scheduler.Process");
    // check scheduling function first arg, pt.1: are we a list?
    boolean schedulerFunFirstArgCorrect = sd.getNumParam() > 0 && sd.getParam(0).getType().getQualifiedName().equals("ABS.StdLib.List");
    if (schedulerFunFirstArgCorrect) {
        // check scheduling function first arg, pt.2: are we a list of
        // processes?
        DataTypeType firstArgType = (DataTypeType) sd.getParam(0).getType();
        if (firstArgType.numTypeArgs() != 1) {
            // should not happen since ABS.StdLib.List takes 1 argument
            schedulerFunFirstArgCorrect = false;
        } else {
            schedulerFunFirstArgCorrect = firstArgType.getTypeArg(0).getQualifiedName().equals("ABS.Scheduler.Process");
        }
    }
    if (!schedulerTypeCorrect || !schedulerFunFirstArgCorrect) {
        // emit two messages: one at the annotation location, one for the
        // offending scheduler function
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_ANNOTATION_TYPE, "dummy"));
        errors.add(new TypeError(sd, ErrorMessage.WRONG_SCHEDULER_FUN_TYPE, s.getName()));
    }
    if (s.getNumParam() == 0 || !(s.getParam(0) instanceof VarUse) || !((VarUse) s.getParam(0)).getName().equals("queue")) {
        // first arg to the scheduler expression must be the magic `queue'
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_FIRST_ARGUMENT, "dummy"));
    }
    if (s.getNumParam() != sd.getNumParam()) {
        errors.add(new TypeError(loc, ErrorMessage.WRONG_NUMBER_OF_ARGS, s.getNumParam(), sd.getNumParam()));
    } else {
        // start from 1; magic first parameter `queue' already checked
        for (int i = 1; i < s.getNumParam(); i++) {
            PureExp arg = s.getParam(i);
            String argname = "";
            if (!(arg instanceof VarOrFieldUse)) {
                // argument was not a plain identifier
                errors.add(new TypeError(arg, ErrorMessage.WRONG_SCHEDULER_FIELD_ARGUMENT, Integer.toString(i + 1), class_decl.getName()));
            } else {
                // Check the rest of the parameters against class
                // field/param names and argument types of the scheduling
                // function.  Parts of this could be elided if we verify
                // that `VarUse's in scheduler annotation are rewritten to
                // `FieldUse's -- then we'd just have to check for the
                // presence of `VarUse' in the parameter list to detect
                // invalid args.  But can't hurt to open-code it (and we
                // still have to check the type of all arguments vs the
                // scheduling function parameters).
                VarOrFieldUse vararg = (VarOrFieldUse) arg;
                String name = vararg.getName();
                Type argtype = UnknownType.INSTANCE;
                for (ParamDecl p : class_decl.getParamList()) {
                    if (p.getName().equals(name))
                        argtype = p.getType();
                }
                for (FieldDecl f : class_decl.getFieldList()) {
                    if (f.getName().equals(name))
                        argtype = f.getType();
                }
                if (argtype.isUnknownType()) {
                    // identifier, but unknown in the class
                    errors.add(new TypeError(arg, ErrorMessage.WRONG_SCHEDULER_FIELD_ARGUMENT, "\"" + name + "\"", class_decl.getName()));
                } else {
                    // argtype: field; paramtype: function arg
                    Type paramtype = sd.getParam(i).getType();
                    if (!argtype.isAssignableTo(paramtype)) {
                        errors.add(new TypeError(arg, ErrorMessage.TYPE_MISMATCH, argtype, paramtype));
                    }
                }
            }
        }
    }
    if (class_decl.getType().isDeploymentComponentType()) {
        errors.add(new TypeError(loc, ErrorMessage.SCHEDULER_ON_DC, "dummy"));
    }
}
Also used : PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse) FunctionDecl(abs.frontend.ast.FunctionDecl) FieldDecl(abs.frontend.ast.FieldDecl) FnApp(abs.frontend.ast.FnApp) ParamDecl(abs.frontend.ast.ParamDecl) TypeError(abs.frontend.analyser.TypeError) VarOrFieldUse(abs.frontend.ast.VarOrFieldUse)

Example 5 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class JavaGeneratorHelper method isLocalVarUse.

/**
 * checks if astNode is a use of a local variable or parameter
 */
private static boolean isLocalVarUse(ASTNode<?> astNode) {
    if (astNode instanceof VarUse) {
        VarUse v = (VarUse) astNode;
        VarOrFieldDecl decl = v.getDecl();
        if (decl instanceof VarDecl || decl instanceof ParamDecl) {
            return !(decl.getParent() instanceof LetExp);
        }
    }
    return false;
}
Also used : TypedVarOrFieldDecl(abs.frontend.ast.TypedVarOrFieldDecl) VarOrFieldDecl(abs.frontend.ast.VarOrFieldDecl) VarDecl(abs.frontend.ast.VarDecl) ParamDecl(abs.frontend.ast.ParamDecl) LetExp(abs.frontend.ast.LetExp) VarUse(abs.frontend.ast.VarUse)

Aggregations

ParamDecl (abs.frontend.ast.ParamDecl)16 FieldDecl (abs.frontend.ast.FieldDecl)6 MethodImpl (abs.frontend.ast.MethodImpl)4 MethodSig (abs.frontend.ast.MethodSig)4 VarUse (abs.frontend.ast.VarUse)4 AbsASTBuilderUtil.createMethodSig (abs.backend.tests.AbsASTBuilderUtil.createMethodSig)3 Annotation (abs.frontend.ast.Annotation)3 AddMethodModifier (abs.frontend.ast.AddMethodModifier)2 Block (abs.frontend.ast.Block)2 ClassDecl (abs.frontend.ast.ClassDecl)2 FieldUse (abs.frontend.ast.FieldUse)2 PureExp (abs.frontend.ast.PureExp)2 ReturnStmt (abs.frontend.ast.ReturnStmt)2 ArrayList (java.util.ArrayList)2 ABSDynamicClass (abs.backend.java.lib.runtime.ABSDynamicClass)1 ABSField (abs.backend.java.lib.runtime.ABSField)1 ABSValue (abs.backend.java.lib.types.ABSValue)1 DeclNamePredicate (abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate)1 AbsASTBuilderUtil.createMethodImpl (abs.backend.tests.AbsASTBuilderUtil.createMethodImpl)1 AbsASTBuilderUtil.getCall (abs.backend.tests.AbsASTBuilderUtil.getCall)1