IR
[repair.git] / Repair / RepairCompiler / MCC / IR / NaiveGenerator.java
1 package MCC.IR;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.State;
6
7 public class NaiveGenerator {
8
9     State state;
10     java.io.PrintWriter output = null;
11             
12     public NaiveGenerator(State state) {
13         this.state = state;
14     }
15
16     public void generate(java.io.OutputStream output) {
17         this.output = new java.io.PrintWriter(output, true); 
18
19         generate_hashtables();
20         generate_rules();
21         generate_implicit_checks();
22         generate_checks();
23
24     }
25
26     private void generate_hashtables() {
27
28         CodeWriter cr = new CodeWriter() {
29                 
30                 int indent = 0;
31                 public void indent() { indent++; }
32                 public void unindent() { indent--; assert indent >= 0; }
33                 private void doindent() {
34                     for (int i = 0; i < indent; i++) { 
35                         output.print("  ");
36                     }
37                 }
38                 public void outputline(String s) {
39                     doindent();
40                     output.println(s);
41                 }                                                             
42                 public void output(String s) { throw new IRException(); }
43                 public SymbolTable getSymbolTable() { throw new IRException(); }
44             };
45         
46         cr.outputline("// creating hashtables ");
47         
48         /* build all the hashtables */
49         Hashtable hashtables = new Hashtable();
50
51         /* build sets */
52         Iterator sets = state.stSets.descriptors();
53         
54         /* first pass create all the hash tables */
55         while (sets.hasNext()) {
56             SetDescriptor set = (SetDescriptor) sets.next();
57             cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();");
58         } 
59
60         /* second pass build relationships between hashtables */
61         sets = state.stSets.descriptors();
62         
63         while (sets.hasNext()) {
64             SetDescriptor set = (SetDescriptor) sets.next();
65             Iterator subsets = set.subsets();
66             
67             while (subsets.hasNext()) {
68                 SetDescriptor subset = (SetDescriptor) subsets.next();                
69                 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
70             }
71         } 
72
73         /* build relations */
74         Iterator relations = state.stRelations.descriptors();
75         
76         /* first pass create all the hash tables */
77         while (relations.hasNext()) {
78             RelationDescriptor relation = (RelationDescriptor) relations.next();
79             cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
80         } 
81
82         cr.outputline("");
83         cr.outputline("");
84
85     }
86
87     private void generate_rules() {
88
89         /* first we must sort the rules */
90         GraphNode.DFS.depthFirstSearch(state.rulenodes.values());
91
92         TreeSet topologicalsort = new TreeSet(new Comparator() {
93                 public boolean equals(Object obj) { return false; }
94                 public int compare(Object o1, Object o2) {
95                     GraphNode g1 = (GraphNode) o1;
96                     GraphNode g2 = (GraphNode) o2;
97                     return g2.getFinishingTime() - g1.getFinishingTime();
98                 }
99             });
100         
101         topologicalsort.addAll(state.rulenodes.values());        
102
103         /* build all the rules */
104         Iterator rules = topologicalsort.iterator();
105                 
106         while (rules.hasNext()) {
107
108             GraphNode rulenode = (GraphNode) rules.next();
109             Rule rule = (Rule) rulenode.getOwner();            
110
111             {
112
113                 final SymbolTable st = rule.getSymbolTable();
114                 
115                 CodeWriter cr = new CodeWriter() {
116                         boolean linestarted = false;
117                         int indent = 0;
118                         public void indent() { indent++; }
119                         public void unindent() { indent--; assert indent >= 0; }
120                         private void doindent() {
121                             for (int i = 0; i < indent; i++) { 
122                                 output.print("  ");
123                             }
124                             linestarted = true;
125                         }
126                         public void outputline(String s) {
127                             if (!linestarted) {
128                                 doindent();
129                             }
130                             output.println(s);
131                             linestarted = false;
132                         }                 
133                         public void output(String s) {
134                             if (!linestarted) {
135                                 doindent();
136                             }
137                             output.print(s);
138                             output.flush(); 
139                         }
140                         public SymbolTable getSymbolTable() { return st; }
141                     };
142                 
143                 cr.outputline("// build " + rule.getLabel());
144                 cr.outputline("{");
145                 cr.indent();
146
147                 ListIterator quantifiers = rule.quantifiers();
148
149                 while (quantifiers.hasNext()) {
150                     Quantifier quantifier = (Quantifier) quantifiers.next();                   
151                     quantifier.generate_open(cr);
152                 }            
153                         
154                 /* pretty print! */
155                 cr.output("//");
156                 rule.getGuardExpr().prettyPrint(cr);
157                 cr.outputline("");
158
159                 /* now we have to generate the guard test */
160         
161                 VarDescriptor guardval = VarDescriptor.makeNew();
162                 rule.getGuardExpr().generate(cr, guardval);
163                 
164                 cr.outputline("if (" + guardval.getSafeSymbol() + ") {");
165
166                 cr.indent();
167
168                 /* now we have to generate the inclusion code */
169                 rule.getInclusion().generate(cr);
170
171                 cr.unindent();
172
173                 cr.outputline("}");
174
175                 while (quantifiers.hasPrevious()) {
176                     Quantifier quantifier = (Quantifier) quantifiers.previous();
177                     cr.unindent();                    
178                     cr.outputline("}");
179                 }
180
181                 cr.unindent();
182                 cr.outputline("}");
183                 cr.outputline("");
184                 cr.outputline("");
185             }
186         }
187
188     }
189
190     private void generate_implicit_checks() {
191
192         /* do post checks */
193         //output.println("check to make sure all relations are well typed");
194         //output.println("check multiplicity");
195
196     }
197
198     private void generate_checks() {
199
200         /* do constraint checks */
201         Vector constraints = state.vConstraints;
202
203         for (int i = 0; i < constraints.size(); i++) {
204             //output.println("check constraint " + (i + 1));
205         }
206
207         //output.println("report problems");
208     }    
209
210 }