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