Search in sources :

Example 11 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class CompModule method resolveFieldDecl.

// ============================================================================================================================//
private static void resolveFieldDecl(CompModule res, final A4Reporter rep, final Sig s, final List<ErrorWarning> warns, boolean defined) throws Err {
    // When typechecking each field:
    // * it is allowed to refer to earlier fields in the same SIG or in any
    // visible ancestor sig
    // * it is allowed to refer to visible sigs
    // * it is NOT allowed to refer to any predicate or function
    // For example, if A.als opens B.als, and B/SIGX extends A/SIGY,
    // then B/SIGX's fields cannot refer to A/SIGY, nor any fields in
    // A/SIGY)
    final List<Decl> oldDecls = res.old2fields.get(res.new2old.get(s));
    if (oldDecls == null)
        return;
    final CompModule m = res.sig2module.get(s);
    final Context cx = new Context(m, warns);
    final ExprHasName dup = Decl.findDuplicateName(oldDecls);
    if (dup != null)
        throw new ErrorSyntax(dup.span(), "sig \"" + s + "\" cannot have 2 fields named \"" + dup.label + "\"");
    for (final Decl d : oldDecls) {
        if (d.expr.mult() != ExprUnary.Op.EXACTLYOF) {
            if (defined)
                continue;
        } else {
            if (!defined)
                continue;
        }
        // The name "this" does matter, since the parser and the typechecker
        // both refer to it as "this"
        cx.rootfield = d;
        cx.rootsig = s;
        cx.put("this", s.decl.get());
        Expr bound = cx.check(d.expr).resolve_as_set(warns);
        cx.remove("this");
        String[] names = new String[d.names.size()];
        for (int i = 0; i < names.length; i++) names[i] = d.names.get(i).label;
        Field[] fields = s.addTrickyField(d.span(), d.isPrivate, d.disjoint, d.disjoint2, null, names, bound);
        for (Field f : fields) {
            rep.typecheck("Sig " + s + ", Field " + f.label + ": " + f.type() + "\n");
        }
    }
}
Also used : Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) Decl(edu.mit.csail.sdg.ast.Decl)

Example 12 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class CompModule method resolveFuncBody.

/**
 * Each Func's body will now be typechecked Expr object.
 */
private JoinableList<Err> resolveFuncBody(A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
    for (ArrayList<Func> entry : funcs.values()) for (Func ff : entry) {
        Context cx = new Context(this, warns);
        cx.rootfunbody = ff;
        for (Decl d : ff.decls) for (ExprHasName n : d.names) cx.put(n.label, n);
        Expr newBody = cx.check(ff.getBody());
        if (ff.isPred)
            newBody = newBody.resolve_as_formula(warns);
        else
            newBody = newBody.resolve_as_set(warns);
        errors = errors.make(newBody.errors);
        if (!newBody.errors.isEmpty())
            continue;
        try {
            ff.setBody(newBody);
        } catch (Err er) {
            errors = errors.make(er);
            continue;
        }
        if (warns != null && ff.returnDecl.type().hasTuple() && newBody.type().hasTuple() && !newBody.type().intersects(ff.returnDecl.type()))
            warns.add(new ErrorWarning(newBody.span(), "Function return value is disjoint from its return type.\n" + "Function body has type " + newBody.type() + "\n" + "but the return type is " + ff.returnDecl.type()));
        // else if (warns!=null && Version.experimental &&
        // !newBody.type.isSubtypeOf(ff.returnDecl.type))
        // warns.add(new ErrorWarning(newBody.span(),
        // "Function may return a tuple not in its declared return
        // type.\n"
        // +"The Alloy Analyzer's analysis may be unsound\n"
        // +"if it returns a tuple outside its declared return type.\n"
        // +"Function body has type "+newBody.type+"\nbut the return
        // type is "+ff.returnDecl.type));
        rep.typecheck(ff.toString() + ", BODY:" + newBody.type() + "\n");
    }
    return errors;
}
Also used : Expr(edu.mit.csail.sdg.ast.Expr) Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) Decl(edu.mit.csail.sdg.ast.Decl)

Example 13 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class CompModule method addAssertion.

// ============================================================================================================================//
/**
 * Add an ASSERT declaration.
 */
String addAssertion(Pos pos, String name, Expr value) throws Err {
    status = 3;
    if (name == null || name.length() == 0)
        name = "assert$" + (1 + asserts.size());
    dup(pos, name, false);
    Expr old = asserts.put(name, ExprUnary.Op.NOOP.make(value.span().merge(pos), value));
    if (old != null) {
        asserts.put(name, old);
        throw new ErrorSyntax(pos, "\"" + name + "\" is already the name of an assertion in this module.");
    }
    return name;
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr)

Example 14 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class OurSyntaxWidget method doNav.

private void doNav() {
    try {
        String text = pane.getText();
        int[] sel = getCurrentWordSelection(text);
        Pos pos = Pos.toPos(text, sel[0], sel[1]);
        if (pos == null)
            return;
        String currentWord = getCurrentWord();
        if (currentWord == null)
            return;
        CompModule module = getModule();
        if (module == null)
            return;
        Expr expr = module.find(pos);
        if (expr != null) {
            Clause clause = expr.referenced();
            if (clause != null) {
                Pos where = clause.pos();
                if (where.sameFile(module.pos()))
                    select(where);
                else {
                    OurSyntaxWidget ow = parent.open(where);
                    if (ow != null) {
                        ow.select(where);
                    }
                }
            }
        }
    } catch (Exception e) {
    // Ignore, this is a best effort thingy
    }
}
Also used : CompModule(edu.mit.csail.sdg.parser.CompModule) Expr(edu.mit.csail.sdg.ast.Expr) Clause(edu.mit.csail.sdg.ast.Clause) BadLocationException(javax.swing.text.BadLocationException)

Example 15 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class OurSyntaxWidget method getTooltip.

public String getTooltip(MouseEvent event) {
    try {
        int offset = pane.viewToModel(event.getPoint());
        CompModule module = getModule();
        if (module == null)
            return null;
        String text = pane.getText();
        Pos pos = Pos.toPos(text, offset, offset + 1);
        Expr expr = module.find(pos);
        if (expr instanceof ExprBad) {
            return expr.toString();
        }
        if (expr != null) {
            Clause referenced = expr.referenced();
            if (referenced != null) {
                String s = referenced.explain();
                String table = "<html><pre>" + s + "</pre></html>";
                s = table.replaceAll("\n", "<br/>");
                return s;
            } else if (expr instanceof ExprConstant) {
                String token = pos.substring(text);
                if (token != null) {
                    String match = expr.toString();
                    if (!Objects.equals(token, match))
                        return match;
                }
            }
        }
    } catch (Exception e) {
    // e.printStackTrace();
    // ignore compile errors etc.
    }
    return null;
}
Also used : ExprBad(edu.mit.csail.sdg.ast.ExprBad) CompModule(edu.mit.csail.sdg.parser.CompModule) Expr(edu.mit.csail.sdg.ast.Expr) Clause(edu.mit.csail.sdg.ast.Clause) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Expr (edu.mit.csail.sdg.ast.Expr)46 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)17 Sig (edu.mit.csail.sdg.ast.Sig)15 Func (edu.mit.csail.sdg.ast.Func)12 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 Decl (edu.mit.csail.sdg.ast.Decl)8 Field (edu.mit.csail.sdg.ast.Sig.Field)8 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BinaryExpression (kodkod.ast.BinaryExpression)5