7 public class StructureGenerator {
11 TypeDescriptor[] tdarray;
13 StructureGenerator(State state, RepairGenerator rg) {
17 cr=new StandardCodeWriter(new java.io.PrintWriter(new FileOutputStream("size.c"),true));
18 crhead=new StandardCodeWriter(new java.io.PrintWriter(new FileOutputStream("size.h"),true));
19 } catch (Exception e) {
25 int max=TypeDescriptor.counter;
26 tdarray=new TypeDescriptor[max];
27 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
28 TypeDescriptor ttd=(TypeDescriptor)it.next();
29 tdarray[ttd.getId()]=ttd;
31 cr.outputline("#include \"size.h\"");
33 generategetnumfields();
38 generatecomputesize();
42 private void generatecalls() {
43 int max=TypeDescriptor.counter;
44 cr.outputline("int arsize["+max+"];");
45 cr.outputline("int arsizeBytes["+max+"];");
47 for(int i=0;i<max;i++) {
48 TypeDescriptor ttd=tdarray[i];
49 if (ttd instanceof StructureTypeDescriptor) {
50 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
51 String str="int arnumelement"+std.getId()+"["+std.fieldlist.size()+"]={";
52 for(int j=0;j<std.fieldlist.size();j++) {
54 if (((j+1)!=std.fieldlist.size()))
60 cr.outputline("int arnumelement"+ttd.getId()+"[1];"); // c doesn't like 0 length arrays
62 String str="int* arnumelements["+String.valueOf(max)+"]={";
63 for(int i=0;i<max;i++) {
64 str+="arnumelement"+i;
72 cr.outputline("int size(int type) {");
73 cr.outputline("return arsize[type];");
76 cr.outputline("int sizeBytes(int type) {");
77 cr.outputline("return arsizeBytes[type];");
80 cr.outputline("int numElements(int type, int fieldindex) {");
81 cr.outputline("return arnumelements[type][fieldindex];");
85 private void generatecomputesize() {
86 int max=TypeDescriptor.counter;
87 cr.outputline("void computesizes(struct "+rg.name+"_state * obj) {");
88 cr.outputline("int i;");
89 cr.outputline(rg.name+"_statecomputesizes(obj,arsize,arnumelements);");
90 cr.outputline("for(i=0;i<"+max+";i++) {");
91 cr.outputline("int bits=arsize[i];");
92 cr.outputline("int bytes=bits>>3;");
93 cr.outputline("if (bits%8) bytes++;");
94 cr.outputline("arsizeBytes[i]=bytes;");
99 private void generateheader() {
100 crhead.outputline("#include \""+rg.headername + "\"");
101 crhead.outputline("int getfield(int type, int fieldindex);");
102 crhead.outputline("int isArray(int type, int fieldindex);");
103 crhead.outputline("int isPtr(int type, int fieldindex);");
104 crhead.outputline("int numElements(int type, int fieldindex);");
105 crhead.outputline("int size(int type);");
106 crhead.outputline("int sizeBytes(int type);");
107 crhead.outputline("int getnumfields(int type);");
108 crhead.outputline("bool issubtype(int subtype, int type);");
109 crhead.outputline("void computesizes(struct "+rg.name+"_state *);");
113 private void generategetfield() {
114 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
115 TypeDescriptor ttd=(TypeDescriptor)it.next();
118 if (ttd instanceof StructureTypeDescriptor) {
119 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
120 str="int argetfield"+std.getId()+"["+std.fieldlist.size()+"]={";
121 for(int i=0;i<std.fieldlist.size();i++) {
122 FieldDescriptor fd = (FieldDescriptor)std.fieldlist.elementAt(i);
123 TypeDescriptor td = fd.getType();
124 str+=String.valueOf(td.getId());
125 if ((i+1)!=std.fieldlist.size())
131 cr.outputline("int argetfield"+ttd.getId()+"[1];"); //c doesn't like zero length arrays
133 int max=TypeDescriptor.counter;
134 String str="int* argetfield["+String.valueOf(max)+"]={";
135 for(int i=0;i<max;i++) {
143 cr.outputline("int getfield(int type, int fieldindex) {");
144 cr.outputline("return argetfield[type][fieldindex];");
148 private void generategetnumfields() {
149 int max=TypeDescriptor.counter;
150 String str="int argetnumfield["+String.valueOf(max)+"]={";
151 for(int i=0;i<max;i++) {
152 TypeDescriptor ttd=tdarray[i];
153 if (ttd instanceof StructureTypeDescriptor) {
154 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
155 str+=String.valueOf(std.fieldlist.size());
164 cr.outputline("int getnumfields(int type) {");
165 cr.outputline("return argetnumfield[type];");
168 private void generateisArray() {
169 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
170 TypeDescriptor ttd=(TypeDescriptor)it.next();
173 if (ttd instanceof StructureTypeDescriptor) {
174 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
175 str="int arisArray"+std.getId()+"["+std.fieldlist.size()+"]={";
176 for(int i=0;i<std.fieldlist.size();i++) {
177 FieldDescriptor fd = (FieldDescriptor)std.fieldlist.elementAt(i);
178 TypeDescriptor td = fd.getType();
179 if (fd instanceof ArrayDescriptor)
183 if ((i+1)!=std.fieldlist.size())
189 cr.outputline("int arisArray"+ttd.getId()+"[1];"); // c doesn't like 0 length arrays
191 int max=TypeDescriptor.counter;
192 String str="int* arisArray["+String.valueOf(max)+"]={";
193 for(int i=0;i<max;i++) {
201 cr.outputline("int isArray(int type, int fieldindex) {");
202 cr.outputline("return arisArray[type][fieldindex];");
205 private void generateisPtr() {
206 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
207 TypeDescriptor ttd=(TypeDescriptor)it.next();
210 if (ttd instanceof StructureTypeDescriptor) {
211 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
212 str="int arisPtr"+std.getId()+"["+std.fieldlist.size()+"]={";
213 for(int i=0;i<std.fieldlist.size();i++) {
214 FieldDescriptor fd = (FieldDescriptor)std.fieldlist.elementAt(i);
219 if ((i+1)!=std.fieldlist.size())
225 cr.outputline("int arisPtr"+ttd.getId()+"[1];"); // c doesn't like 0 length arrays
227 int max=TypeDescriptor.counter;
228 String str="int* arisPtr["+String.valueOf(max)+"]={";
229 for(int i=0;i<max;i++) {
237 cr.outputline("int isPtr(int type, int fieldindex) {");
238 cr.outputline("return arisPtr[type][fieldindex];");
242 void generateissubtype() {
243 int max=TypeDescriptor.counter;
244 String str="bool arissubtype["+max+"]["+max+"]={";
245 for(int i=0;i<max;i++) {
247 for(int j=0;j<max;j++) {
248 TypeDescriptor tdi=tdarray[i];
249 TypeDescriptor tdj=tdarray[j];
250 if (tdi.isSubtypeOf(tdj))
263 cr.outputline("bool issubtype(int subtype, int type) {");
264 cr.outputline("return arissubtype[subtype][type];");