5 public class SumExpr extends Expr {
11 public SumExpr(SetDescriptor sd, RelationDescriptor rd) {
12 if (sd == null||rd==null) {
13 throw new NullPointerException();
19 public String name() {
20 return "sum("+sd.toString()+"."+rd.toString()+")";
23 public boolean equals(Map remap, Expr e) {
24 if (e==null||!(e instanceof SumExpr))
26 SumExpr se=(SumExpr)e;
27 return (se.sd==sd)&&(se.rd==rd);
30 public boolean usesDescriptor(Descriptor d) {
31 return (sd==d)||(rd==d);
34 public Set useDescriptor(Descriptor d) {
35 HashSet newset=new HashSet();
41 public Descriptor getDescriptor() {
42 throw new Error("Sum shouldn't appear on left hand side!");
45 public boolean inverted() {
49 public Set getRequiredDescriptors() {
50 HashSet v=new HashSet();
56 public void generate(CodeWriter writer, VarDescriptor dest) {
57 writer.addDeclaration("int",dest.getSafeSymbol());
58 writer.outputline(dest.getSafeSymbol()+"=0;");
61 VarDescriptor itvd=VarDescriptor.makeNew("iterator");
62 writer.addDeclaration("struct SimpleIterator",itvd.getSafeSymbol());
63 writer.outputline("SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+itvd.getSafeSymbol()+");");
64 writer.outputline("while (hasNext(&"+itvd.getSafeSymbol()+"))");
67 VarDescriptor keyvd=VarDescriptor.makeNew("key");
68 writer.addDeclaration("int",keyvd.getSafeSymbol());
69 writer.outputline(keyvd.getSafeSymbol()+"=next(&"+itvd.getSafeSymbol()+");");
70 VarDescriptor tmpvar=VarDescriptor.makeNew("tmp");
71 writer.addDeclaration("int",tmpvar.getSafeSymbol());
73 VarDescriptor newset=VarDescriptor.makeNew("newset");
74 writer.addDeclaration("struct SimpleHash *",newset.getSafeSymbol());
75 writer.outputline(newset.getSafeSymbol()+"=SimpleHashimageSet("+rd.getSafeSymbol()+"_hash, "+keyvd.getSafeSymbol()+");");
77 VarDescriptor itvd2=VarDescriptor.makeNew("iterator");
78 writer.addDeclaration("struct SimpleIterator",itvd2.getSafeSymbol());
79 writer.outputline("SimpleHashiterator("+newset.getSafeSymbol()+", &"+itvd2.getSafeSymbol()+");");
81 writer.outputline("while (hasNext(&"+itvd2.getSafeSymbol()+"))");
84 VarDescriptor keyvd2=VarDescriptor.makeNew("keyinner");
85 writer.outputline(dest.getSafeSymbol()+"+=next(&"+itvd2.getSafeSymbol()+");");
88 writer.outputline("freeSimpleHash("+newset.getSafeSymbol()+");");
93 public void prettyPrint(PrettyPrinter pp) {
95 pp.output(sd.toString());
97 pp.output(rd.toString());
101 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
102 this.td = ReservedTypeDescriptor.INT;
106 public Set getInversedRelations() {
107 return new HashSet();