5 public class SizeofFunction extends Expr {
7 public VarDescriptor vd;
8 public RelationDescriptor rd;
11 public SizeofFunction(VarDescriptor vd, RelationDescriptor rd, Rule rule) {
17 public Set getInversedRelations() {
21 public boolean equals(Map remap,Expr e) {
22 throw new Error("UNIMPLEMENTED");
25 public Set getRequiredDescriptors() {
26 // because we don't actually use rd for any generation, we return the empty set
30 public TypeDescriptor getType() {
31 throw new IRException("unsupported");
34 public void generate(CodeWriter cr, VarDescriptor dest) {
36 // basically a sizeoffunction can have two values ... zero or one... so what we need to do
37 // is expand the guard of the rule and if its true then its 1 otherwise 0
39 String destname = dest.getSafeSymbol();
40 cr.outputline("int " + destname + ";");
42 // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we
43 // set the destination in the guard ... otherwise maybe!
45 VarDescriptor domain = vd;
47 cr.pushSymbolTable(rule.getSymbolTable());
50 // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
51 SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
52 VarDescriptor rulebinding = sq.getVar();
53 String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
55 // this is to be safe about name overlap because int t = t; sets t to 0!
56 cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
57 cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
60 cr.outputline("// about to inbed relational function");
62 rule.getGuardExpr().prettyPrint(cr);
65 /* now we have to generate the guard test */
66 VarDescriptor guardval = VarDescriptor.makeNew();
67 rule.getGuardExpr().generate(cr, guardval);
69 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
72 cr.outputline(destname + " = 1;");
75 cr.outputline("else");
78 cr.outputline(destname + " = 0;");
88 public void prettyPrint(PrettyPrinter pp) {
89 pp.output("sizeoffunction(");
90 pp.output(vd.toString() + "." + rd.toString());
94 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
95 throw new IRException();