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 String name() {
18 String name=expr.name()+".";
21 name+=relation.toString();
25 public Expr getExpr() {
29 public boolean equals(Map remap, Expr e) {
30 if (e==null||!(e instanceof RelationExpr))
32 RelationExpr re=(RelationExpr)e;
33 if (re.relation!=relation)
35 if (!expr.equals(remap,re.expr))
37 if (inverse!=re.inverse)
42 public boolean usesDescriptor(Descriptor rd) {
46 return expr.usesDescriptor(rd);
49 public RelationDescriptor getRelation() {
53 public Descriptor getDescriptor() {
57 public boolean inverted() {
61 public Set getInversedRelations() {
62 Set set = expr.getInversedRelations();
69 public Set getRequiredDescriptors() {
70 Set v = expr.getRequiredDescriptors();
75 public void generate(CodeWriter writer, VarDescriptor dest) {
76 VarDescriptor domain = VarDescriptor.makeNew("domain");
77 String strinverse = inverse ? "inv" : "";
78 String found = (VarDescriptor.makeNew("found")).getSafeSymbol();
79 expr.generate(writer, domain);
80 writer.outputline(relation.getRange().getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + ";");
81 writer.outputline("int " + found + " = " + relation.getSafeSymbol() + "_hash" + strinverse + "->get(" + domain.getSafeSymbol() + ", " + dest.getSafeSymbol() + ");");
82 writer.outputline("if (!" + found + ") { maybe = 1; }");
85 // #TBD#: don't think this method is needed (or even called/referenced)
87 public void generate_set(CodeWriter writer, VarDescriptor dest) {
88 VarDescriptor domain = VarDescriptor.makeNew("domain");
89 expr.generate(writer, domain);
90 writer.outputline(relation.getRange().getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + " = " + relation.getSafeSymbol() + "_hash->get(" + domain.getSafeSymbol() + ");");
94 public void prettyPrint(PrettyPrinter pp) {
102 pp.output(relation.getSafeSymbol());
105 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
107 TypeDescriptor type = expr.typecheck(sa);
113 /* check to make sure that the types of the relation match up */
115 TypeDescriptor rangetype = relation.getRange().getType();
117 if (rangetype != type) {
118 sa.getErrorReporter().report(null, "Type of left side of relation operator '.' is '" + type.getSymbol() +
119 "' but must be '" + rangetype.getSymbol() +
120 "', the type of the range of the relation '" + relation.getSymbol() + "'");
124 this.td = relation.getDomain().getType();
126 } else { /* not inverse */
127 TypeDescriptor domaintype = relation.getDomain().getType();
129 if (domaintype != type) {
130 sa.getErrorReporter().report(null, "Type of left side of relation operator '.' is '" + type.getSymbol() +
131 "' but must be '" + domaintype.getSymbol() +
132 "', the type of the domain of the relation '" + relation.getSymbol() + "'");
136 this.td = relation.getRange().getType();