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.addDeclaration("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.addDeclaration("int", tempvar);
57 cr.outputline(tempvar + " = " + domain.getSafeSymbol() + ";");
58 cr.addDeclaration("int", rulebinding.getSafeSymbol());
59 cr.outputline(rulebinding.getSafeSymbol() + " = " + tempvar + ";");
62 cr.outputline("/* about to inbed relational function*/");
64 rule.getGuardExpr().prettyPrint(cr);
67 /* now we have to generate the guard test */
68 VarDescriptor guardval = VarDescriptor.makeNew();
69 rule.getGuardExpr().generate(cr, guardval);
71 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
74 cr.outputline(destname + " = 1;");
77 cr.outputline("else");
80 cr.outputline(destname + " = 0;");
90 public void prettyPrint(PrettyPrinter pp) {
91 pp.output("sizeoffunction(");
92 pp.output(vd.toString() + "." + rd.toString());
96 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
97 throw new IRException();