Completed support for generating C code.
[repair.git] / Repair / RepairCompiler / MCC / IR / SizeofFunction.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class SizeofFunction extends Expr {
6
7     public VarDescriptor vd;
8     public RelationDescriptor rd;
9     public Rule rule;
10
11     public SizeofFunction(VarDescriptor vd, RelationDescriptor rd, Rule rule) {
12         this.vd = vd;
13         this.rd = rd;
14         this.rule = rule;
15     }
16
17     public Set getInversedRelations() {
18         return new HashSet();
19     }
20
21     public boolean equals(Map remap,Expr e) {
22         throw new Error("UNIMPLEMENTED");
23     }
24
25     public Set getRequiredDescriptors() {
26         // because we don't actually use rd for any generation, we return the empty set
27         return new HashSet();
28     }
29
30     public TypeDescriptor getType() {
31         throw new IRException("unsupported");
32     }
33
34     public void generate(CodeWriter cr, VarDescriptor dest) {
35
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
38
39         String destname = dest.getSafeSymbol();
40         cr.addDeclaration("int", destname);
41
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!
44
45         VarDescriptor domain = vd;
46
47         cr.pushSymbolTable(rule.getSymbolTable());
48         cr.startblock(); {
49
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();
54
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 + ";");
60
61             /* pretty print! */
62             cr.outputline("/* about to inbed relational function*/");
63             cr.output("/* ");
64             rule.getGuardExpr().prettyPrint(cr);
65             cr.outputline("*/");
66
67             /* now we have to generate the guard test */
68             VarDescriptor guardval = VarDescriptor.makeNew();
69             rule.getGuardExpr().generate(cr, guardval);
70
71             cr.outputline("if (" + guardval.getSafeSymbol() + ")");
72             cr.startblock(); {
73
74                 cr.outputline(destname + " = 1;");
75
76             } cr.endblock();
77             cr.outputline("else");
78             cr.startblock(); {
79
80                 cr.outputline(destname + " = 0;");
81
82             } cr.endblock();
83
84         } cr.endblock();
85
86
87
88     }
89
90     public void prettyPrint(PrettyPrinter pp) {
91         pp.output("sizeoffunction(");
92         pp.output(vd.toString() + "." + rd.toString());
93         pp.output(")");
94     }
95
96     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
97         throw new IRException();
98     }
99
100 }