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 Set getRequiredDescriptors() {
22 // because we don't actually use rd for any generation, we return the empty set
26 public TypeDescriptor getType() {
27 throw new IRException("unsupported");
30 public void generate(CodeWriter cr, VarDescriptor dest) {
32 // basically a sizeoffunction can have two values ... zero or one... so what we need to do
33 // is expand the guard of the rule and if its true then its 1 otherwise 0
35 String destname = dest.getSafeSymbol();
36 cr.outputline("int " + destname + ";");
38 // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we
39 // set the destination in the guard ... otherwise maybe!
41 VarDescriptor domain = vd;
43 cr.pushSymbolTable(rule.getSymbolTable());
46 // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
47 SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
48 VarDescriptor rulebinding = sq.getVar();
49 String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
51 // this is to be safe about name overlap because int t = t; sets t to 0!
52 cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
53 cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
56 cr.outputline("// about to inbed relational function");
58 rule.getGuardExpr().prettyPrint(cr);
61 /* now we have to generate the guard test */
62 VarDescriptor guardval = VarDescriptor.makeNew();
63 rule.getGuardExpr().generate(cr, guardval);
65 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
68 cr.outputline(destname + " = 1;");
71 cr.outputline("else");
74 cr.outputline(destname + " = 0;");
84 public void prettyPrint(PrettyPrinter pp) {
85 pp.output("sizeoffunction(");
86 pp.output(vd.toString() + "." + rd.toString());
90 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
91 throw new IRException();