/* Used only for structure field arrays*/ static void generate_fieldarray(Symbol* basetype, NCConstant* con, Dimset* dimset, Bytebuffer* codebuf, Datalist* filler, Generator* generator) { int i; int chartype = (basetype->typ.typecode == NC_CHAR); Datalist* data; int rank = rankfor(dimset); ASSERT(dimset->ndims > 0); if(con != NULL && !isfillconst(con)) data = con->value.compoundv; else data = NULL; if(chartype) { Bytebuffer* charbuf = bbNew(); gen_chararray(dimset,0,data,charbuf,filler); generator->charconstant(generator,codebuf,charbuf); bbFree(charbuf); } else { int uid; size_t xproduct = crossproduct(dimset,0,rank); /* compute total number of elements */ generator->listbegin(generator,LISTFIELDARRAY,xproduct,codebuf,&uid); for(i=0;i<xproduct;i++) { con = (data == NULL ? NULL : datalistith(data,i)); generator->list(generator,LISTFIELDARRAY,uid,i,codebuf); generate_basetype(basetype,con,codebuf,NULL,generator); } generator->listend(generator,LISTFIELDARRAY,uid,i,codebuf); } }
void gen_chararray(Dimset* dimset, int dimindex, Datalist* data, Bytebuffer* charbuf, Datalist* fillsrc) { int fillchar = getfillchar(fillsrc); int rank = rankfor(dimset); int firstunlim = findunlimited(dimset,0); int nunlim = countunlimited(dimset); int nc3unlim = (nunlim <= 1 && (firstunlim == 0 || firstunlim == rank)); /* netcdf-3 case of at most 1 unlim in 0th dimension */ /* Case: netcdf3 case */ if(nc3unlim) { gen_leafchararray(dimset,0,data,charbuf,fillchar); return; } /* else generate should have done all the hard work */ gen_leafchararray(dimset,dimindex,data,charbuf,fillchar); }
static void gen_leafchararray(Dimset* dimset, int dimindex, Datalist* data, Bytebuffer* charbuf, int fillchar) { int i; size_t expectedsize,xproduct,unitsize; int rank = rankfor(dimset); ASSERT(bbLength(charbuf) == 0); ASSERT((findlastunlimited(dimset) == rank || findlastunlimited(dimset) == dimindex)); /* There are a number of special cases that must be considered, mostly driven by the need to keep consistent with ncgen3. These cases are driven by the number of dimensions, which dimensions are unlimited (if any), etc. The general rule is based on the size of the last dimension, we compute the required size (after padding) of each string constant. Expected size is then the size of concat of the string constants after padding. There is another special case used for back compatability with ncgen3. In the datalist, all sequences of character constants (i.e. 'X') are concatenated into a single string; the result, however is not concatenated with any trailing or leading string (with double quotes). */ /* Rebuild the datalist to merge 'x' constants */ { int i,cccount = 0; /* Do initial walk */ for(i=0;i<datalistlen(data);i++) { NCConstant* con = datalistith(data,i); if(consttype(con) == NC_CHAR || consttype(con) == NC_BYTE) { cccount++; } } if(cccount > 1) { char* accum = (char*)malloc(cccount+1); int len = 0; Datalist* newlist = builddatalist(datalistlen(data)); int lineno = 0; NCConstant* con; for(i=0;i<datalistlen(data);i++) { con = datalistith(data,i); if(consttype(con) == NC_CHAR || consttype(con) == NC_BYTE) { if(len == 0) lineno = constline(con); accum[len] = con->value.charv; len++; } else { if(len > 0) { con = makeconst(lineno,len,accum); len = 0; lineno = 0; } dlappend(newlist,con); } } /* deal with any unclosed strings */ if(len > 0) { con = makeconst(lineno,len,accum); len = 0; lineno = 0; dlappend(newlist,con); } free(accum); data = newlist; } } /* Compute crossproduct upto (but not includng) the last dimension */ xproduct = crossproduct(dimset,dimindex,rank-1); /* Start casing it out */ if(rank == 0) { unitsize = 1; expectedsize = (xproduct * unitsize); } else if(rank == 1) { unitsize = 1; expectedsize = (xproduct * declsizefor(dimset,rank-1)); } else if(isunlimited(dimset,rank-1)) {/* last dimension is unlimited */ unitsize = 1; expectedsize = (xproduct*declsizefor(dimset,rank-1)); } else { /* rank > 0 && last dim is not unlimited */ unitsize = declsizefor(dimset,rank-1); expectedsize = (xproduct * unitsize); } for(i=0;i<data->length;i++) { NCConstant* c = datalistith(data,i); ASSERT(!islistconst(c)); if(isstringable(c->nctype)) { int j; size_t constsize; constsize = gen_charconstant(c,charbuf,fillchar); if(constsize == 0 || constsize % unitsize > 0) { size_t padsize = unitsize - (constsize % unitsize); for(j=0;j<padsize;j++) bbAppend(charbuf,fillchar); } } else { semwarn(constline(c),"Encountered non-string and non-char constant in datalist; ignored"); } } /* If |databuf| > expectedsize, complain: exception is zero length */ if(bbLength(charbuf) == 0 && expectedsize == 1) { /* this is okay */ } else if(bbLength(charbuf) > expectedsize) { semwarn(data->data[0].lineno,"character data list too long; expected %d character constant, found %d: ",expectedsize,bbLength(charbuf)); } else { size_t bufsize = bbLength(charbuf); /* Pad to size dimproduct size */ if(bufsize % expectedsize > 0) { size_t padsize = expectedsize - (bufsize % expectedsize); for(i=0;i<padsize;i++) bbAppend(charbuf,fillchar); } } }
/* Compute type sizes and compound offsets*/ void computesize(Symbol* tsym) { int i; int offset = 0; int largealign; unsigned long totaldimsize; if(tsym->touched) return; tsym->touched=1; switch (tsym->subclass) { case NC_VLEN: /* actually two sizes for vlen*/ computesize(tsym->typ.basetype); /* first size*/ tsym->typ.size = ncsize(tsym->typ.typecode); tsym->typ.alignment = ncaux_class_alignment(tsym->typ.typecode); tsym->typ.nelems = 1; /* always a single compound datalist */ break; case NC_PRIM: tsym->typ.size = ncsize(tsym->typ.typecode); tsym->typ.alignment = ncaux_class_alignment(tsym->typ.typecode); tsym->typ.nelems = 1; break; case NC_OPAQUE: /* size and alignment already assigned*/ tsym->typ.nelems = 1; break; case NC_ENUM: computesize(tsym->typ.basetype); /* first size*/ tsym->typ.size = tsym->typ.basetype->typ.size; tsym->typ.alignment = tsym->typ.basetype->typ.alignment; tsym->typ.nelems = 1; break; case NC_COMPOUND: /* keep if all fields are primitive*/ /* First, compute recursively, the size and alignment of fields*/ for(i=0;i<listlength(tsym->subnodes);i++) { Symbol* field = (Symbol*)listget(tsym->subnodes,i); ASSERT(field->subclass == NC_FIELD); computesize(field); if(i==0) tsym->typ.alignment = field->typ.alignment; } /* now compute the size of the compound based on what user specified*/ offset = 0; largealign = 1; for(i=0;i<listlength(tsym->subnodes);i++) { Symbol* field = (Symbol*)listget(tsym->subnodes,i); /* only support 'c' alignment for now*/ int alignment = field->typ.alignment; int padding = getpadding(offset,alignment); offset += padding; field->typ.offset = offset; offset += field->typ.size; if (alignment > largealign) { largealign = alignment; } } tsym->typ.cmpdalign = largealign; /* total structure size alignment */ offset += (offset % largealign); tsym->typ.size = offset; break; case NC_FIELD: /* Compute size assume no unlimited dimensions*/ if(tsym->typ.dimset.ndims > 0) { computesize(tsym->typ.basetype); totaldimsize = crossproduct(&tsym->typ.dimset,0,rankfor(&tsym->typ.dimset)); tsym->typ.size = tsym->typ.basetype->typ.size * totaldimsize; tsym->typ.alignment = tsym->typ.basetype->typ.alignment; tsym->typ.nelems = 1; } else { tsym->typ.size = tsym->typ.basetype->typ.size; tsym->typ.alignment = tsym->typ.basetype->typ.alignment; tsym->typ.nelems = tsym->typ.basetype->typ.nelems; } break; default: PANIC1("computesize: unexpected type class: %d",tsym->subclass); break; } }