Search in sources :

Example 1 with Sig

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

the class CompModule method addModelName.

/**
 * Add the "MODULE" declaration.
 */
void addModelName(Pos pos, String moduleName, List<ExprVar> list) throws Err {
    if (status > 0)
        throw new ErrorSyntax(pos, "The \"module\" declaration must occur at the top,\n" + "and can occur at most once.");
    this.moduleName = moduleName;
    this.modulePos = pos;
    boolean nextIsExact = false;
    if (list != null)
        for (ExprVar expr : list) {
            if (expr == null) {
                nextIsExact = true;
                continue;
            }
            String name = expr.label;
            dup(expr.span(), name, true);
            if (path.length() == 0) {
                Sig newSig = addSig(name, null, null, null, null, WHERE.make(expr.span()));
                if (nextIsExact)
                    exactSigs.add(newSig);
            } else {
                params.put(name, null);
                if (nextIsExact)
                    exactParams.add(name);
            }
            nextIsExact = false;
        }
    // This line must be at the end, since "addSig" will
    this.status = 1;
// otherwise bump the status value to 3
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 2 with Sig

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

the class CompModule method resolveAll.

// ============================================================================================================================//
/**
 * This method resolves the entire world; NOTE: if it throws an exception, it
 * may leave the world in an inconsistent state!
 */
static CompModule resolveAll(final A4Reporter rep, final CompModule root) throws Err {
    final List<ErrorWarning> warns = new ArrayList<ErrorWarning>();
    for (CompModule m : root.getAllReachableModules()) root.allModules.add(m);
    resolveParams(rep, root.allModules);
    resolveModules(rep, root.allModules);
    for (CompModule m : root.allModules) for (Sig s : m.sigs.values()) root.sig2module.put(s, m);
    // Resolves SigAST -> Sig, and topologically sort the sigs into the
    // "sorted" array
    root.new2old.put(UNIV, UNIV);
    root.new2old.put(SIGINT, SIGINT);
    root.new2old.put(SEQIDX, SEQIDX);
    root.new2old.put(STRING, STRING);
    root.new2old.put(NONE, NONE);
    HashSet<Object> topo = new HashSet<Object>();
    for (CompModule m : root.allModules) for (Sig s : m.sigs.values()) resolveSig(root, topo, s);
    // (since fields in subsigs are allowed to refer to parent's fields)
    for (Sig oldS : root.new2old.keySet()) resolveFieldDecl(root, rep, oldS, warns, false);
    // Typecheck the function declarations
    JoinableList<Err> errors = new JoinableList<Err>();
    for (CompModule x : root.allModules) errors = x.resolveFuncDecls(rep, errors, warns);
    if (!errors.isEmpty())
        throw errors.pick();
    // Typecheck the defined fields
    for (Sig oldS : root.new2old.keySet()) resolveFieldDecl(root, rep, oldS, warns, true);
    if (Version.experimental && root.seenDollar)
        resolveMeta(root);
    // Reject name clash
    rejectNameClash(root.allModules);
    // to function declarations)
    for (CompModule x : root.allModules) {
        errors = x.resolveFuncBody(rep, errors, warns);
        errors = x.resolveAssertions(rep, errors, warns);
        errors = x.resolveFacts(root, rep, errors, warns);
        // root module's list of exact sigs
        for (String n : x.exactParams) {
            Sig sig = x.params.get(n);
            if (sig != null)
                root.exactSigs.add(sig);
        }
    }
    if (!errors.isEmpty())
        throw errors.pick();
    // Typecheck the run/check commands (which can refer to function bodies
    // and assertions)
    root.resolveCommands(root.getAllReachableFacts());
    if (!errors.isEmpty())
        throw errors.pick();
    for (ErrorWarning w : warns) rep.warning(w);
    for (Sig s : root.exactSigs) rep.debug("Forced to be exact: " + s + "\n");
    return root;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Err(edu.mit.csail.sdg.alloy4.Err) ArrayList(java.util.ArrayList) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) JoinableList(edu.mit.csail.sdg.alloy4.JoinableList)

Example 3 with Sig

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

the class CompModule method resolveSig.

/**
 * The given Sig will now point to a nonnull Sig.
 */
private static Sig resolveSig(CompModule res, Set<Object> topo, Sig oldS) throws Err {
    if (res.new2old.containsKey(oldS))
        return oldS;
    Sig realSig;
    final Pos pos = oldS.pos;
    final CompModule u = res.sig2module.get(oldS);
    final String name = base(oldS);
    final String fullname = (u.path.length() == 0) ? ("this/" + name) : (u.path + "/" + name);
    if (!topo.add(oldS))
        throw new ErrorType(pos, "Sig " + oldS + " is involved in a cyclic inheritance.");
    if (oldS instanceof SubsetSig) {
        List<Sig> parents = new ArrayList<Sig>();
        for (Sig n : ((SubsetSig) oldS).parents) {
            Sig parentAST = u.getRawSIG(n.pos, n.label);
            if (parentAST == null)
                throw new ErrorSyntax(n.pos, "The sig \"" + n.label + "\" cannot be found.");
            parents.add(resolveSig(res, topo, parentAST));
        }
        realSig = new SubsetSig(fullname, parents, oldS.attributes.toArray(new Attr[0]));
    } else {
        Sig sup = ((PrimSig) oldS).parent;
        Sig parentAST = u.getRawSIG(sup.pos, sup.label);
        if (parentAST == null)
            throw new ErrorSyntax(sup.pos, "The sig \"" + sup.label + "\" cannot be found.");
        Sig parent = resolveSig(res, topo, parentAST);
        if (!(parent instanceof PrimSig))
            throw new ErrorSyntax(sup.pos, "Cannot extend the subset signature \"" + parent + "\".\n" + "A signature can only extend a toplevel signature or a subsignature.");
        PrimSig p = (PrimSig) parent;
        realSig = new PrimSig(fullname, p, oldS.attributes.toArray(new Attr[0]));
    }
    res.new2old.put(realSig, oldS);
    res.sig2module.put(realSig, u);
    for (CompModule m : res.allModules) {
        for (Map.Entry<String, Sig> e : m.sigs.entrySet()) if (e.getValue() == oldS)
            e.setValue(realSig);
        for (Map.Entry<String, Sig> e : m.params.entrySet()) if (e.getValue() == oldS)
            e.setValue(realSig);
    }
    if (res.exactSigs.remove(oldS))
        res.exactSigs.add(realSig);
    return realSig;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Pos(edu.mit.csail.sdg.alloy4.Pos) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 4 with Sig

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

the class CompModule method getRawSIG.

/**
 * Lookup a Sig from the current module (and it will also search this.params)
 */
private Sig getRawSIG(Pos pos, String name) throws Err {
    List<Object> s;
    Sig s2 = null;
    if (name.equals("sig$") || name.equals("field$"))
        if (world != null) {
            s2 = world.sigs.get(name);
            if (s2 != null)
                return s2;
        }
    if (name.equals("univ"))
        return UNIV;
    if (name.equals("Int"))
        return SIGINT;
    if (name.equals("seq/Int"))
        return SEQIDX;
    if (name.equals("String"))
        return STRING;
    if (name.equals("none"))
        return NONE;
    if (name.indexOf('/') < 0) {
        s = getRawNQS(this, 1, name);
        s2 = params.get(name);
    } else {
        if (name.startsWith("this/")) {
            name = name.substring(5);
            s2 = params.get(name);
        }
        s = getRawQS(1, name);
    }
    if (s2 != null && !s.contains(s2))
        s.add(s2);
    return (Sig) (unique(pos, name, s));
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig)

Example 5 with Sig

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

the class CompModule method resolveCommand.

/**
 * Resolve a particular command.
 */
private Command resolveCommand(Command cmd, ConstList<Sig> exactSigs, Expr globalFacts) throws Err {
    Command parent = cmd.parent == null ? null : resolveCommand(cmd.parent, exactSigs, globalFacts);
    String cname = ((ExprVar) (cmd.formula)).label;
    Expr e;
    Clause declaringClause = null;
    if (cmd.check) {
        // We prefer assertion in the
        List<Object> m = getRawQS(2, cname);
        // topmost module
        if (m.size() == 0 && cname.indexOf('/') < 0)
            m = getRawNQS(this, 2, cname);
        if (m.size() > 1)
            unique(cmd.pos, cname, m);
        if (m.size() < 1)
            throw new ErrorSyntax(cmd.pos, "The assertion \"" + cname + "\" cannot be found.");
        Expr expr = (Expr) (m.get(0));
        e = expr.not();
    } else {
        // We prefer fun/pred in the
        List<Object> m = getRawQS(4, cname);
        // topmost module
        if (m.size() == 0 && cname.indexOf('/') < 0)
            m = getRawNQS(this, 4, cname);
        if (m.size() > 1)
            unique(cmd.pos, cname, m);
        if (m.size() < 1)
            throw new ErrorSyntax(cmd.pos, "The predicate/function \"" + cname + "\" cannot be found.");
        Func f = (Func) (m.get(0));
        declaringClause = f;
        e = f.getBody();
        if (!f.isPred)
            e = e.in(f.returnDecl);
        if (f.decls.size() > 0)
            e = ExprQt.Op.SOME.make(null, null, f.decls, e);
    }
    if (e == null)
        e = ExprConstant.TRUE;
    TempList<CommandScope> sc = new TempList<CommandScope>(cmd.scope.size());
    for (CommandScope et : cmd.scope) {
        Sig s = getRawSIG(et.sig.pos, et.sig.label);
        if (s == null)
            throw new ErrorSyntax(et.sig.pos, "The sig \"" + et.sig.label + "\" cannot be found.");
        sc.add(new CommandScope(null, s, et.isExact, et.startingScope, et.endingScope, et.increment));
    }
    if (cmd.nameExpr != null) {
        cmd.nameExpr.setReferenced(declaringClause);
    }
    return new Command(cmd.pos, cmd.nameExpr, cmd.label, cmd.check, cmd.overall, cmd.bitwidth, cmd.maxseq, cmd.expects, sc.makeConst(), exactSigs, globalFacts.and(e), parent);
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) 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) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Command(edu.mit.csail.sdg.ast.Command) Clause(edu.mit.csail.sdg.ast.Clause) CommandScope(edu.mit.csail.sdg.ast.CommandScope)

Aggregations

Sig (edu.mit.csail.sdg.ast.Sig)45 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)33 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)25 Field (edu.mit.csail.sdg.ast.Sig.Field)17 Expr (edu.mit.csail.sdg.ast.Expr)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 ArrayList (java.util.ArrayList)8 Func (edu.mit.csail.sdg.ast.Func)7 HashMap (java.util.HashMap)7 TupleSet (kodkod.instance.TupleSet)7 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)6 A4Solution (edu.mit.csail.sdg.translator.A4Solution)6 LinkedHashMap (java.util.LinkedHashMap)6 Err (edu.mit.csail.sdg.alloy4.Err)5 SafeList (edu.mit.csail.sdg.alloy4.SafeList)4 Decl (edu.mit.csail.sdg.ast.Decl)4 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)4