Search in sources :

Example 6 with TempList

use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.

the class CompModule method resolveFuncDecls.

/**
 * Each FunAST will now point to a bodyless Func object.
 */
private JoinableList<Err> resolveFuncDecls(A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
    for (ArrayList<Func> list : funcs.values()) {
        for (int listi = 0; listi < list.size(); listi++) {
            Func f = list.get(listi);
            String fullname = (path.length() == 0 ? "this/" : (path + "/")) + f.label;
            // Each PARAMETER can refer to earlier parameter in the same
            // function, and any SIG or FIELD visible from here.
            // Each RETURNTYPE can refer to the parameters of the same
            // function, and any SIG or FIELD visible from here.
            Context cx = new Context(this, warns);
            cx.rootfunparam = true;
            TempList<Decl> tmpdecls = new TempList<Decl>();
            boolean err = false;
            for (Decl d : f.decls) {
                TempList<ExprVar> tmpvars = new TempList<ExprVar>();
                Expr val = cx.check(d.expr).resolve_as_set(warns);
                if (!val.errors.isEmpty()) {
                    err = true;
                    errors = errors.make(val.errors);
                }
                for (ExprHasName n : d.names) {
                    ExprVar v = ExprVar.make(n.span(), n.label, val.type());
                    cx.put(n.label, v);
                    tmpvars.add(v);
                    rep.typecheck((f.isPred ? "pred " : "fun ") + fullname + ", Param " + n.label + ": " + v.type() + "\n");
                }
                tmpdecls.add(new Decl(d.isPrivate, d.disjoint, d.disjoint2, tmpvars.makeConst(), val));
            }
            Expr ret = null;
            if (!f.isPred) {
                ret = cx.check(f.returnDecl).resolve_as_set(warns);
                if (!ret.errors.isEmpty()) {
                    err = true;
                    errors = errors.make(ret.errors);
                }
            }
            if (err)
                continue;
            try {
                f = new Func(f.pos, f.isPrivate, fullname, tmpdecls.makeConst(), ret, f.getBody());
                list.set(listi, f);
                rep.typecheck("" + f + ", RETURN: " + f.returnDecl.type() + "\n");
            } catch (Err ex) {
                errors = errors.make(ex);
            }
        }
    }
    return errors;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) Decl(edu.mit.csail.sdg.ast.Decl) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Expr(edu.mit.csail.sdg.ast.Expr) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName)

Example 7 with TempList

use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.

the class CompModule method populate.

/**
 * Resolve the name based on the current context and this module.
 */
private Expr populate(TempList<Expr> ch, TempList<String> re, Decl rootfield, Sig rootsig, boolean rootfunparam, Func rootfunbody, Pos pos, String fullname, Expr THIS) {
    // Return object can be Func(with > 0 arguments) or Expr
    final String name = (fullname.charAt(0) == '@') ? fullname.substring(1) : fullname;
    boolean fun = (rootsig != null && (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF)) || (rootsig == null && !rootfunparam);
    if (name.equals("univ"))
        return ExprUnary.Op.NOOP.make(pos, UNIV);
    if (name.equals("Int"))
        return ExprUnary.Op.NOOP.make(pos, SIGINT);
    if (name.equals("seq/Int"))
        return ExprUnary.Op.NOOP.make(pos, SEQIDX);
    if (name.equals("String"))
        return ExprUnary.Op.NOOP.make(pos, STRING);
    if (name.equals("none"))
        return ExprUnary.Op.NOOP.make(pos, NONE);
    if (name.equals("iden"))
        return ExprConstant.Op.IDEN.make(pos, 0);
    if (name.equals("sig$") || name.equals("field$"))
        if (world != null) {
            Sig s = world.sigs.get(name);
            if (s != null)
                return ExprUnary.Op.NOOP.make(pos, s);
        }
    final List<Object> ans = name.indexOf('/') >= 0 ? getRawQS(fun ? 5 : 1, name) : getRawNQS(this, fun ? 5 : 1, name);
    final Sig param = params.get(name);
    if (param != null && !ans.contains(param))
        ans.add(param);
    for (Object x : ans) {
        if (x instanceof Sig) {
            Sig y = (Sig) x;
            ch.add(ExprUnary.Op.NOOP.make(pos, y, null, 0));
            re.add("sig " + y.label);
        } else if (x instanceof Func) {
            Func f = (Func) x;
            int fn = f.count();
            int penalty = 0;
            if (resolution == 1 && fn > 0 && rootsig != null && THIS != null && THIS.type().hasArity(1) && f.get(0).type().intersects(THIS.type())) {
                // If we're inside a sig, and there is a unary variable
                // bound to "this",
                // we should consider it as a possible FIRST ARGUMENT of a
                // fun/pred call
                ConstList<Expr> t = Util.asList(THIS);
                // penalty
                ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 1 + penalty) : ExprBadCall.make(pos, null, f, t, 1 + penalty));
                // of
                // 1
                re.add((f.isPred ? "pred this." : "fun this.") + f.label);
            }
            if (resolution == 1) {
                ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, penalty) : ExprBadCall.make(pos, null, f, null, penalty));
                re.add((f.isPred ? "pred " : "fun ") + f.label);
            }
            if (resolution == 2 && f != rootfunbody && THIS != null && fullname.charAt(0) != '@' && fn > 0 && f.get(0).type().intersects(THIS.type())) {
                // If there is some value bound to "this", we should
                // consider it as a possible FIRST ARGUMENT of a fun/pred
                // call
                ConstList<Expr> t = Util.asList(THIS);
                ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 0) : ExprBadCall.make(pos, null, f, t, 0));
                re.add((f.isPred ? "pred this." : "fun this.") + f.label);
            }
            if (resolution != 1) {
                ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, 0) : ExprBadCall.make(pos, null, f, null, 0));
                re.add((f.isPred ? "pred " : "fun ") + f.label);
            }
        }
    }
    // All else: we can call, and can refer to anything visible.
    for (CompModule m : getAllNameableModules()) for (Sig s : m.sigs.values()) if (m == this || s.isPrivate == null)
        for (Field f : s.getFields()) if (f.isMeta == null && (m == this || f.isPrivate == null) && f.label.equals(name))
            if (resolution == 1) {
                Expr x = null;
                if (rootsig == null) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                } else if (rootsig.isSameOrDescendentOf(f.sig)) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                    if (fullname.charAt(0) != '@')
                        x = THIS.join(x);
                } else if (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 1);
                }
                // penalty of 1
                if (x != null) {
                    ch.add(x);
                    re.add("field " + f.sig.label + " <: " + f.label);
                }
            } else if (rootfield == null || rootsig.isSameOrDescendentOf(f.sig)) {
                Expr x0 = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                if (resolution == 2 && THIS != null && fullname.charAt(0) != '@' && f.type().firstColumnOverlaps(THIS.type())) {
                    ch.add(THIS.join(x0));
                    re.add("field " + f.sig.label + " <: this." + f.label);
                    if (rootsig != null)
                        continue;
                }
                ch.add(x0);
                re.add("field " + f.sig.label + " <: " + f.label);
            }
    if (metaSig() != null && (rootsig == null || rootfield == null)) {
        SafeList<PrimSig> children = null;
        try {
            children = metaSig().children();
        } catch (Err err) {
            return null;
        }
        // exception NOT possible
        for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
            Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
            ch.add(x);
            re.add("field " + f.sig.label + " <: " + f.label);
        }
    }
    if (metaField() != null && (rootsig == null || rootfield == null)) {
        SafeList<PrimSig> children = null;
        try {
            children = metaField().children();
        } catch (Err err) {
            return null;
        }
        // exception NOT possible
        for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
            Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
            ch.add(x);
            re.add("field " + f.sig.label + " <: " + f.label);
        }
    }
    return null;
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) Expr(edu.mit.csail.sdg.ast.Expr) ConstList(edu.mit.csail.sdg.alloy4.ConstList) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 8 with TempList

use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprQt x) throws Err {
    Expr xx = x.desugar();
    if (xx instanceof ExprQt)
        x = (ExprQt) xx;
    else
        return visitThis(xx);
    if (x.op == ExprQt.Op.COMPREHENSION) {
        TempList<SimTuple> ans = new TempList<SimTuple>();
        enumerate(ans, 0, x, x.sub, 0);
        return SimTupleset.make(ans.makeConst());
    }
    if (x.op == ExprQt.Op.ALL)
        return enumerate(null, 0, x, x.sub.not(), 0) == 0;
    if (x.op == ExprQt.Op.NO)
        return enumerate(null, 0, x, x.sub, 0) == 0;
    if (x.op == ExprQt.Op.SOME)
        return enumerate(null, 0, x, x.sub, 0) >= 1;
    if (x.op == ExprQt.Op.LONE)
        return enumerate(null, 0, x, x.sub, 0) <= 1;
    if (x.op == ExprQt.Op.ONE)
        return enumerate(null, 0, x, x.sub, 0) == 1;
    if (x.op == ExprQt.Op.SUM)
        return trunc(enumerate(null, 0, x, x.sub, 0));
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprQt.accept()");
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) ExprQt(edu.mit.csail.sdg.ast.ExprQt)

Example 9 with TempList

use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.

the class ExprList method resolve.

// ============================================================================================================//
/**
 * {@inheritDoc}
 */
@Override
public Expr resolve(Type p, Collection<ErrorWarning> warns) {
    TempList<Expr> newargs = new TempList<Expr>(args.size());
    boolean changed = false;
    if (errors.size() > 0)
        return this;
    if (op == Op.AND || op == Op.OR) {
        for (int i = 0; i < args.size(); i++) {
            Expr x = args.get(i);
            Expr y = x.resolve(Type.FORMULA, warns).typecheck_as_formula();
            if (x != y)
                changed = true;
            newargs.add(y);
        }
    }
    if (op == Op.DISJOINT) {
        for (int i = 0; i < args.size(); i++) {
            if (i == 0)
                p = Type.removesBoolAndInt(args.get(i).type);
            else
                p = p.unionWithCommonArity(args.get(i).type);
        }
        for (int i = 0; i < args.size(); i++) {
            Expr x = args.get(i);
            Expr y = x.resolve(p, warns).typecheck_as_set();
            if (x != y)
                changed = true;
            newargs.add(y);
        }
    }
    if (op == Op.TOTALORDER) {
        Type t = args.get(0).type.pickUnary();
        Expr a = args.get(0).resolve(t, warns).typecheck_as_set();
        Expr b = args.get(1).resolve(t, warns).typecheck_as_set();
        Expr c = args.get(2).resolve(t.product(t), warns).typecheck_as_set();
        changed = (a != args.get(0) || b != args.get(1) || c != args.get(2));
        newargs.add(a).add(b).add(c);
    }
    return changed ? make(pos, closingBracket, op, newargs.makeConst()) : this;
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList)

Example 10 with TempList

use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.

the class ExprCall method resolve.

// ============================================================================================================//
/**
 * {@inheritDoc}
 */
@Override
public Expr resolve(Type t, Collection<ErrorWarning> warns) {
    if (errors.size() > 0)
        return this;
    TempList<Expr> args = new TempList<Expr>(this.args.size());
    boolean changed = false;
    for (int i = 0; i < this.args.size(); i++) {
        Type p = fun.get(i).type;
        Expr x = this.args.get(i);
        // Use the
        Expr y = x.resolve(p, warns).typecheck_as_set();
        // the choices
        if (x != y)
            changed = true;
        args.add(y);
    // if (warns!=null && Version.experimental &&
    // !y.type.isSubtypeOf(p))
    // warns.add(new ErrorWarning(x.span(), "This argument may contain a
    // tuple not in the parameter's type.\n"
    // +"The Alloy Analyzer's analysis may be unsound\n"
    // +"if the argument has a tuple outside the parameter's type.\n"
    // +"The argument has type "+y.type+"\nbut the parameter has type
    // "+p));
    }
    return changed ? make(pos, closingBracket, fun, args.makeConst(), extraWeight) : this;
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList)

Aggregations

TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)9 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)5 Expr (edu.mit.csail.sdg.ast.Expr)5 Err (edu.mit.csail.sdg.alloy4.Err)4 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)4 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 Func (edu.mit.csail.sdg.ast.Func)3 Sig (edu.mit.csail.sdg.ast.Sig)2 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 ConstList (edu.mit.csail.sdg.alloy4.ConstList)1 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)1 Clause (edu.mit.csail.sdg.ast.Clause)1 Command (edu.mit.csail.sdg.ast.Command)1 CommandScope (edu.mit.csail.sdg.ast.CommandScope)1 Decl (edu.mit.csail.sdg.ast.Decl)1 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)1 ExprQt (edu.mit.csail.sdg.ast.ExprQt)1 Field (edu.mit.csail.sdg.ast.Sig.Field)1 ArrayList (java.util.ArrayList)1