5 public class RelationExpr extends Expr {
8 RelationDescriptor relation;
11 public RelationExpr(Expr expr, RelationDescriptor relation, boolean inverse) {
13 this.relation = relation;
14 this.inverse = inverse;
17 public Expr getExpr() {
21 public RelationDescriptor getRelation() {
25 public Set getInversedRelations() {
26 Set set = expr.getInversedRelations();
33 public Set getRequiredDescriptors() {
34 Set v = expr.getRequiredDescriptors();
39 public void generate(CodeWriter writer, VarDescriptor dest) {
40 VarDescriptor domain = VarDescriptor.makeNew("domain");
41 String strinverse = inverse ? "inv" : "";
42 String found = (VarDescriptor.makeNew("found")).getSafeSymbol();
43 expr.generate(writer, domain);
44 writer.outputline(relation.getRange().getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + ";");
45 writer.outputline("int " + found + " = " + relation.getSafeSymbol() + "_hash" + strinverse + "->get(" + domain.getSafeSymbol() + ", " + dest.getSafeSymbol() + ");");
46 writer.outputline("if (!" + found + ") { maybe = 1; }");
49 // #TBD#: don't think this method is needed (or even called/referenced)
51 public void generate_set(CodeWriter writer, VarDescriptor dest) {
52 VarDescriptor domain = VarDescriptor.makeNew("domain");
53 expr.generate(writer, domain);
54 writer.outputline(relation.getRange().getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + " = " + relation.getSafeSymbol() + "_hash->get(" + domain.getSafeSymbol() + ");");
58 public void prettyPrint(PrettyPrinter pp) {
66 pp.output(relation.getSafeSymbol());
69 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
71 TypeDescriptor type = expr.typecheck(sa);
77 /* check to make sure that the types of the relation match up */
79 TypeDescriptor rangetype = relation.getRange().getType();
81 if (rangetype != type) {
82 sa.getErrorReporter().report(null, "Type of left side of relation operator '.' is '" + type.getSymbol() +
83 "' but must be '" + rangetype.getSymbol() +
84 "', the type of the range of the relation '" + relation.getSymbol() + "'");
88 this.td = relation.getDomain().getType();
90 } else { /* not inverse */
91 TypeDescriptor domaintype = relation.getDomain().getType();
93 if (domaintype != type) {
94 sa.getErrorReporter().report(null, "Type of left side of relation operator '.' is '" + type.getSymbol() +
95 "' but must be '" + domaintype.getSymbol() +
96 "', the type of the domain of the relation '" + relation.getSymbol() + "'");
100 this.td = relation.getRange().getType();