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