+package MCC.IR;
+
+import java.util.*;
+
+public class MetaInclusion extends Inclusion {
+
+ Inclusion inclusion;
+ Vector rules;
+ Vector constraints;
+
+ public MetaInclusion() {
+ inclusion = null;
+ rules = new Vector();
+ constraints = new Vector();
+ }
+
+ public Set getTargetDescriptors() {
+ throw new IRException("unsupported");
+ }
+
+ public Set getRequiredDescriptors() {
+ return inclusion.getRequiredDescriptors();
+ }
+
+ public boolean typecheck(SemanticAnalyzer sa) {
+ throw new IRException("unsupported");
+ }
+
+ public void setInclusion(Inclusion in) {
+ assert (in instanceof SetInclusion); // we only support setinclusion meta inclusions
+ this.inclusion = in;
+ }
+
+ public Inclusion getInclusion() {
+ return inclusion;
+ }
+
+ public void addRules(Collection newrules) {
+ rules.addAll(newrules);
+ }
+
+ public void addConstraint(Constraint c) {
+ constraints.add(c);
+ }
+
+ public void generate(CodeWriter cr) {
+
+ this.inclusion.generate(cr);
+
+ // for each of the rules, since they are guaranteed to be single quantifiers, and in fact,
+ // even stricter, there only quantifier is the same quantifier just generated by
+ // the inclusion constraint, we only need to bind each of there vardescriptors in there
+ // own blocks (to avoid name space clashes) to the newly quantified variable.
+
+ // we know that the inclusion is a setinlusion
+ SetInclusion setinclusion = (SetInclusion) inclusion;
+
+ String addeditem = setinclusion.generatedaddeditem; // boolean : new item or not
+ String result = setinclusion.generatedresult; // item added to set... what we must bind
+
+ cr.outputline("if (" + addeditem + ")");
+ cr.startblock();
+
+ ListIterator allrules = rules.listIterator();
+ while (allrules.hasNext()) {
+ Rule rule = (Rule) allrules.next();
+
+ // we need to grab the vardescriptor of the first quantifeir (which is a setquantifier)
+ // and we need to instantiate it inside a new block scope and set it equal to the value
+ // in "result" .... we then need to generate the guard and inclusion inside of inner rule
+
+ cr.startblock();{
+
+ cr.outputline("// embedding " + rule.getLabel() );
+
+ SetQuantifier sq = (SetQuantifier) rule.quantifiers().next(); // get first qunatifier
+ VarDescriptor vd = sq.getVar();
+ cr.outputline("int " + vd.getSafeSymbol() + " = " + result + ";");
+
+ cr.pushSymbolTable(rule.getSymbolTable());
+
+ VarDescriptor guardval = VarDescriptor.makeNew();
+ rule.getGuardExpr().generate(cr, guardval);
+
+ cr.outputline("if (" + guardval.getSafeSymbol() + ")");
+ cr.startblock(); {
+ rule.getInclusion().generate(cr);
+ } cr.endblock();
+
+ cr.popSymbolTable();
+
+ } cr.endblock();
+ }
+
+ cr.endblock();
+
+ // constraints!!!!!!!!!!!!!!
+
+ ListIterator allconstraints = constraints.listIterator();
+ while (allconstraints.hasNext()) {
+ Constraint constraint = (Constraint) allconstraints.next();
+
+ // ok... um... we need to grab teh vardescripntor of the first quantifier which is guaranteed
+ // to be a setquantifier... we then need to bind it to variable, generatedresult.
+ // once this is done we can generated the logicstatement and we can then test for pass/fail
+ // and emit an error
+
+ cr.startblock(); {
+
+ cr.outputline("// checking embedded " + constraint.getLabel() );
+
+ SetQuantifier sq = (SetQuantifier) constraint.quantifiers().next(); // get first qunatifier
+ VarDescriptor vd = sq.getVar();
+ cr.outputline("int " + vd.getSafeSymbol() + " = " + result + ";");
+
+ cr.pushSymbolTable(constraint.getSymbolTable());
+
+ cr.outputline("int maybe = 0;");
+
+ /* now we have to generate the guard test */
+
+ VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
+ constraint.getLogicStatement().generate(cr, constraintboolean);
+
+ cr.outputline("if (maybe)");
+ cr.startblock(); {
+
+ cr.outputline("__Success = 0;");
+ cr.outputline("printf(\"maybe fail " + constraint.getNum() + ". \");");
+ cr.outputline("exit(1);");
+
+ } cr.endblock();
+ cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
+ cr.startblock(); {
+
+ cr.outputline("__Success = 0;");
+ cr.outputline("printf(\"fail " + constraint.getNum() + ". \");");
+ cr.outputline("exit(1);");
+
+ } cr.endblock();
+
+ cr.popSymbolTable();
+
+ } cr.endblock();
+
+ }
+
+ }
+
+}
+