heh
[repair.git] / Repair / RepairCompiler / MCC / IR / MetaInclusion.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class MetaInclusion extends Inclusion {
6     
7     Inclusion inclusion;
8     Vector rules;
9     Vector constraints;
10
11     public MetaInclusion() {
12         inclusion = null;
13         rules = new Vector();
14         constraints = new Vector();
15     }
16
17     public Set getTargetDescriptors() {
18         throw new IRException("unsupported");
19     }
20
21     public Set getRequiredDescriptors() {
22         return inclusion.getRequiredDescriptors();
23     }
24
25     public boolean typecheck(SemanticAnalyzer sa) {
26         throw new IRException("unsupported");
27     }
28
29     public void setInclusion(Inclusion in) {
30         assert (in instanceof SetInclusion); // we only support setinclusion meta inclusions
31         this.inclusion = in;
32     }
33
34     public Inclusion getInclusion() {
35         return inclusion;
36     }
37
38     public void addRules(Collection newrules) {
39         rules.addAll(newrules);
40     }
41
42     public void addConstraint(Constraint c) {
43         constraints.add(c);
44     }
45
46     public void generate(CodeWriter cr) {
47         
48         this.inclusion.generate(cr);
49
50         // for each of the rules, since they are guaranteed to be single quantifiers, and in fact,
51         // even stricter, there only quantifier is the same quantifier just generated by 
52         // the inclusion constraint, we only need to bind each of there vardescriptors in there 
53         // own blocks (to avoid name space clashes) to the newly quantified variable.
54
55         // we know that the inclusion is a  setinlusion
56         SetInclusion setinclusion = (SetInclusion) inclusion;
57
58         String addeditem = setinclusion.generatedaddeditem; // boolean : new item or not
59         String result = setinclusion.generatedresult; // item added to set... what we must bind
60
61         cr.outputline("if (" + addeditem + ")");
62         cr.startblock();
63         
64         ListIterator allrules = rules.listIterator();
65         while (allrules.hasNext()) {
66             Rule rule = (Rule) allrules.next();
67             
68             // we need to grab the vardescriptor of the first quantifeir (which is a setquantifier)
69             // and we need to instantiate it inside a new block scope and set it equal to the value
70             // in "result" .... we then need to generate the guard and inclusion inside of inner rule
71             
72             cr.startblock();{
73
74                 cr.outputline("// embedding " + rule.getLabel() );
75                 
76                 SetQuantifier sq = (SetQuantifier) rule.quantifiers().next(); // get first qunatifier
77                 VarDescriptor vd = sq.getVar();
78                 cr.outputline("int " + vd.getSafeSymbol() + " = " + result + ";");
79
80                 cr.pushSymbolTable(rule.getSymbolTable());
81                 
82                 VarDescriptor guardval = VarDescriptor.makeNew();
83                 rule.getGuardExpr().generate(cr, guardval);
84                 
85                 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
86                 cr.startblock(); {
87                     rule.getInclusion().generate(cr);
88                 } cr.endblock();
89
90                 cr.popSymbolTable();
91            
92             } cr.endblock();                
93         }
94
95         cr.endblock();
96
97         // constraints!!!!!!!!!!!!!!
98
99         ListIterator allconstraints = constraints.listIterator();
100         while (allconstraints.hasNext()) {
101             Constraint constraint = (Constraint) allconstraints.next();
102             
103             // ok... um... we need to grab teh vardescripntor of the first quantifier which is guaranteed
104             // to be a setquantifier... we then need to bind it to variable, generatedresult.
105             // once this is done we can generated the logicstatement and we can then test for pass/fail
106             // and emit an error
107
108             cr.startblock(); {
109
110                 cr.outputline("// checking embedded " + constraint.getLabel() );
111                 
112                 SetQuantifier sq = (SetQuantifier) constraint.quantifiers().next(); // get first qunatifier
113                 VarDescriptor vd = sq.getVar();
114                 cr.outputline("int " + vd.getSafeSymbol() + " = " + result + ";");
115
116                 cr.pushSymbolTable(constraint.getSymbolTable());
117                 
118                 cr.outputline("int maybe = 0;");
119                 
120                 /* now we have to generate the guard test */
121                 
122                 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
123                 constraint.getLogicStatement().generate(cr, constraintboolean);
124                 
125                 cr.outputline("if (maybe)");
126                 cr.startblock(); {
127
128                     cr.outputline("__Success = 0;");
129                     cr.outputline("printf(\"maybe fail " + constraint.getNum() + ". \");");
130                     cr.outputline("exit(1);");
131            
132                 } cr.endblock();
133                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");                
134                 cr.startblock(); {
135                 
136                     cr.outputline("__Success = 0;");
137                     cr.outputline("printf(\"fail " + constraint.getNum() + ". \");");
138                     cr.outputline("exit(1);");
139
140                 } cr.endblock();
141
142                 cr.popSymbolTable();
143            
144             } cr.endblock();                                      
145
146         }
147
148     }
149
150 }
151