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;
}
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;
}
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()");
}
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;
}
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;
}
Aggregations