/* Recursive helper */ static void gen_chararrayr(Dimset* dimset, int dimindex, int lastunlimited, Bytebuffer* databuf, Datalist* data, int fillchar, int unitsize, int expectedsize) { int i; size_t dimsize = dimset->dimsyms[dimindex]->dim.declsize; if(dimindex < lastunlimited) { /* keep recursing */ for(i=0;i<dimsize;i++) { NCConstant* c = datalistith(data,i); ASSERT(islistconst(c)); gen_chararrayr(dimset,dimindex+1,lastunlimited,databuf, c->value.compoundv,fillchar,unitsize,expectedsize); } } else {/* we should be at a list of simple constants */ 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,databuf,fillchar); if(constsize % unitsize > 0) { size_t padsize = unitsize - (constsize % unitsize); for(j=0;j<padsize;j++) bbAppend(databuf,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(databuf) == 0 && expectedsize == 1) { /* this is okay */ } else if(bbLength(databuf) > expectedsize) { semwarn(data->data[0].lineno,"character data list too long"); } else { size_t bufsize = bbLength(databuf); /* Pad to size dimproduct size */ if(bufsize % expectedsize > 0) { size_t padsize = expectedsize - (bufsize % expectedsize); for(i=0;i<padsize;i++) bbAppend(databuf,fillchar); } } }
void generate_vardata(Symbol* vsym, Generator* generator, Writer writer, Bytebuffer* code) { Dimset* dimset = &vsym->typ.dimset; int rank = dimset->ndims; Symbol* basetype = vsym->typ.basetype; Datalist* filler = getfiller(vsym); const size_t* start; const size_t* count; Odometer* odom; if(vsym->data == NULL) return; /* give the buffer a running start to be large enough*/ if(!bbSetalloc(code, nciterbuffersize)) return; if(rank == 0) {/*scalar case*/ NCConstant* c0 = datalistith(vsym->data,0); generate_basetype(basetype,c0,code,filler,generator); writer(generator,vsym,code,0,NULL,NULL); } else {/*rank > 0*/ /* First, create an odometer using all of the dimensions */ odom = newodometer(dimset,NULL,NULL); start = odometerstartvector(odom); count = odometercountvector(odom); generate_array(vsym,code,filler,generator,writer); } }
/* 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; ASSERT(dimset->ndims > 0); if(con != NULL && !isfillconst(con)) data = con->value.compoundv; else data = NULL; if(chartype) { /* Collect the char field in a separate buffer */ Bytebuffer* charbuf = bbNew(); gen_chararray(dimset,data,charbuf,filler); generator->charconstant(generator,codebuf,charbuf); bbFree(charbuf); } else { int uid; size_t xproduct = crossproduct(dimset,0,0); /* 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); } }
static void generate_primdata(Symbol* basetype, Constant* prim, Bytebuffer* codebuf, Datalist* filler, Generator* generator) { Constant target; if(prim == NULL || isfillconst(prim)) { Datalist* fill = (filler==NULL?getfiller(basetype):filler); ASSERT(fill->length == 1); prim = datalistith(fill,0); } ASSERT(prim->nctype != NC_COMPOUND); target.nctype = basetype->typ.typecode; if(target.nctype != NC_ECONST) { convert1(prim,&target); } switch (target.nctype) { case NC_ECONST: if(basetype->subclass != NC_ENUM) { semerror(constline(prim),"Conversion to enum not supported (yet)"); } break; case NC_OPAQUE: setprimlength(&target,basetype->typ.size*2); break; default: break; } generator->constant(generator,&target,codebuf); return; }
/* recursive helper for validataNIL */ static void validateNILr(Datalist* src) { int i; for(i=0;i<src->length;i++) { NCConstant* con = datalistith(src,i); if(isnilconst(con)) semerror(con->lineno,"NIL data can only be assigned to variables or attributes of type string"); else if(islistconst(con)) /* recurse */ validateNILr(con->value.compoundv); } }
/* Recursive helper */ static void gen_chararrayr(Dimset* dimset, int dimindex, Bytebuffer* databuf, Datalist* data, int fillchar, int unitsize, int expectedsize) { int i; size_t dimsize = declsizefor(dimset,dimindex); int rank = dimset->ndims; int firstunlim = findunlimited(dimset,0); int lastunlimited = findlastunlimited(dimset); int nextunlimited = findunlimited(dimset,dimindex+1); int islastgroup = (lastunlimited == rank || dimindex >= lastunlimited || dimindex == rank-1); Odometer* subodom = NULL; ASSERT(rank > 0); ASSERT((islastgroup)); /* we should be at a list of simple constants */ 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,databuf,fillchar); if(constsize % unitsize > 0) { size_t padsize = unitsize - (constsize % unitsize); for(j=0;j<padsize;j++) bbAppend(databuf,fillchar); } } else { semwarn(constline(c), "Encountered non-string and non-char constant in datalist; ignored"); } }/* for */ /* If |databuf| > expectedsize, complain: exception is zero length */ if(bbLength(databuf) == 0 && expectedsize == 1) { /* this is okay */ } else if(bbLength(databuf) > expectedsize) { semwarn(data->data[0].lineno,"character data list too long; expected %d character constant, found %d: ",expectedsize,bbLength(databuf)); } else { size_t bufsize = bbLength(databuf); /* Pad to size dimproduct size */ if(bufsize % expectedsize > 0) { size_t padsize = expectedsize - (bufsize % expectedsize); for(i=0;i<padsize;i++) bbAppend(databuf,fillchar); } } }
int binary_generate_data(Datalist* data, Symbol* tsym, Datalist* fillvalue, Bytebuffer* databuf) { int stat = NC_NOERR; size_t count = data->length; size_t i; bbClear(databuf); for(i=0;i<count;i++) { NCConstant* instance = datalistith(data,i); if((stat = bin_generate_data_r(instance, tsym, fillvalue, databuf))) goto done; } done: return stat; }
static void fixeconstref(Symbol* avsym, NCConstant* con) { Symbol* basetype = NULL; Symbol* refsym = con->value.enumv; Symbol* varsym = NULL; int i; /* Figure out the proper type associated with avsym */ ASSERT(avsym->objectclass == NC_VAR || avsym->objectclass == NC_ATT); if(avsym->objectclass == NC_VAR) { basetype = avsym->typ.basetype; varsym = avsym; } else { /*(avsym->objectclass == NC_ATT)*/ basetype = avsym->typ.basetype; varsym = avsym->container; if(varsym->objectclass == NC_GRP) varsym = NULL; } if(basetype->objectclass != NC_TYPE && basetype->subclass != NC_ENUM) semerror(con->lineno,"Enumconstant associated with a non-econst type"); if(con->nctype == NC_FILLVALUE) { Datalist* filllist = NULL; NCConstant* filler = NULL; filllist = getfiller(varsym == NULL?basetype:varsym); if(filllist == NULL) semerror(con->lineno, "Cannot determine enum constant fillvalue"); filler = datalistith(filllist,0); con->value.enumv = filler->value.enumv; return; } for(i=0;i<listlength(basetype->subnodes);i++) { Symbol* econst = listget(basetype->subnodes,i); ASSERT(econst->subclass == NC_ECONST); if(strcmp(econst->name,refsym->name)==0) { con->value.enumv = econst; return; } } semerror(con->lineno,"Undefined enum or enum constant reference: %s",refsym->name); }
void gen_charvlen(Datalist* data, Bytebuffer* databuf) { int i; NCConstant* c; ASSERT(bbLength(databuf) == 0); for(i=0;i<data->length;i++) { c = datalistith(data,i); if(isstringable(c->nctype)) { (void)gen_charconstant(c,databuf,NC_FILL_CHAR); } else { semerror(constline(c), "Encountered non-string and non-char constant in datalist"); return; } } }
void generate_attrdata(Symbol* asym, Generator* generator, Writer writer, Bytebuffer* codebuf) { Symbol* basetype = asym->typ.basetype; nc_type typecode = basetype->typ.typecode; if(typecode == NC_CHAR) { gen_charattr(asym->data,codebuf); } else { int uid; size_t count; generator->listbegin(generator,LISTATTR,asym->data->length,codebuf,&uid); for(count=0;count<asym->data->length;count++) { NCConstant* con = datalistith(asym->data,count); generator->list(generator,LISTATTR,uid,count,codebuf); generate_basetype(asym->typ.basetype,con,codebuf,NULL,generator); } generator->listend(generator,LISTATTR,uid,count,codebuf); } writer(generator,asym,codebuf,0,NULL,NULL); }
void generate_vardata(Symbol* vsym, Generator* generator, Writer writer, Bytebuffer* code) { Dimset* dimset = &vsym->typ.dimset; int rank = dimset->ndims; Symbol* basetype = vsym->typ.basetype; Datalist* filler = getfiller(vsym); if(vsym->data == NULL) return; /* give the buffer a running start to be large enough*/ bbSetalloc(code, nciterbuffersize); if(rank == 0) {/*scalar case*/ Constant* c0 = datalistith(vsym->data,0); generate_basetype(basetype,c0,code,filler,generator); writer(generator,vsym,code,0,NULL,NULL); } else {/*rank > 0*/ generate_array(vsym,code,filler,generator,writer); } }
void gen_leafchararray(Dimset* dimset, int lastunlim, Datalist* data, Bytebuffer* databuf, Datalist* fillsrc) { int i; size_t expectedsize,xproduct,unitsize; int ndims = dimset->ndims; int fillchar = getfillchar(fillsrc); ASSERT(bbLength(databuf) == 0); /* Assume dimindex is the last unlimited (or 0 if their are no unlimiteds => we should be at a list of simple constants */ /* Compute crossproduct upto the last dimension, starting at the last unlimited */ xproduct = crossproduct(dimset,lastunlim,ndims-1); /* Compute the required size (after padding) of each string constant */ /* expected size is the size of concat of the string constants after padding */ if(ndims == 0) { unitsize = 1; expectedsize = (xproduct * unitsize); } else if(lastunlim == ndims-1) {/* last dimension is unlimited */ unitsize = 1; expectedsize = (xproduct*dimset->dimsyms[lastunlim]->dim.declsize); } else { /* last dim is not unlimited */ unitsize = dimset->dimsyms[ndims-1]->dim.declsize; 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,databuf,fillchar); if(constsize == 0 || constsize % unitsize > 0) { size_t padsize = unitsize - (constsize % unitsize); for(j=0;j<padsize;j++) bbAppend(databuf,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(databuf) == 0 && expectedsize == 1) { /* this is okay */ } else if(bbLength(databuf) > expectedsize) { semwarn(data->data[0].lineno,"character data list too long"); } else { size_t bufsize = bbLength(databuf); /* Pad to size dimproduct size */ if(bufsize % expectedsize > 0) { size_t padsize = expectedsize - (bufsize % expectedsize); for(i=0;i<padsize;i++) bbAppend(databuf,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); } } }
static void generate_array(Symbol* vsym, Bytebuffer* code, Datalist* filler, Generator* generator, Writer writer ) { Dimset* dimset = &vsym->typ.dimset; int rank = dimset->ndims; Symbol* basetype = vsym->typ.basetype; nc_type typecode = basetype->typ.typecode; Odometer* odom; nciter_t iter; ASSERT(rank > 0); /* Start by doing easy cases */ #ifdef CHARBUG if(typecode == NC_CHAR) { /* case: character typed variable, rank > 0 */ Bytebuffer* charbuf = bbNew(); gen_chararray(dimset,vsym->data,charbuf,filler); generator->charconstant(generator,code,charbuf); bbFree(charbuf); odom = newodometer(dimset,NULL,NULL); writer(generator,vsym,code,odom->rank,odom->start,odom->count); } else #else /*!CHARBUG*/ /* Case: char var && dim 1..n are not unlimited */ if(findunlimited(dimset,1) == rank && typecode == NC_CHAR) { Bytebuffer* charbuf = bbNew(); gen_leafchararray(dimset,0,vsym->data,charbuf,filler); generator->charconstant(generator,code,charbuf); bbFree(charbuf); odom = newodometer(dimset,NULL,NULL); writer(generator,vsym,code,odom->rank,odom->start,odom->count); } else #endif /* Case 2: dim 1..n are not unlimited */ if(findunlimited(dimset,1) == rank) { size_t offset = 0; /* where are we in the data list */ size_t nelems = 0; /* # of data list items to read */ /* Create an iterator and odometer and just walk the datalist */ nc_get_iter(vsym,nciterbuffersize,&iter); odom = newodometer(dimset,NULL,NULL); for(;;offset+=nelems) { int i,uid; nelems=nc_next_iter(&iter,odom->start,odom->count); if(nelems == 0) break; bbClear(code); generator->listbegin(generator,LISTDATA,vsym->data->length,code,&uid); for(i=0;i<nelems;i++) { #ifdef ITERBUG Constant* con = datalistith(vsym->data,i); #else NCConstant* con = datalistith(vsym->data,i+offset); #endif generator->list(generator,LISTDATA,uid,i,code); #ifdef USE_NOFILL if(nofill_flag && con == NULL) break; else #endif generate_basetype(basetype,con,code,filler,generator); } generator->listend(generator,LISTDATA,uid,i,code); #ifdef USE_NOFILL writer(generator,vsym,code,rank,odom->start,odom->index); #else writer(generator,vsym,code,rank,odom->start,odom->count); #endif } } else { /* Hard case: multiple unlimited dimensions */ /* Setup iterator and odometer */ #ifdef CHARBUG nc_get_iter(vsym,nciterbuffersize,&iter); #else nc_get_iter(vsym,NC_MAX_UINT,&iter); /* effectively infinite */ #endif odom = newodometer(dimset,NULL,NULL); for(;;) {/* iterate in nelem chunks */ /* get nelems count and modify odometer */ size_t nelems=nc_next_iter(&iter,odom->start,odom->count); if(nelems == 0) break; generate_arrayr(vsym,code,vsym->data, odom, /*dim index=*/0, filler,generator ); #ifdef USE_NOFILL writer(generator,vsym,code,odom->rank,odom->start,odom->index); #else writer(generator,vsym,code,odom->rank,odom->start,odom->count); #endif } } odometerfree(odom); }
static void generate_primdata(Symbol* basetype, NCConstant* prim, Bytebuffer* codebuf, Datalist* filler, Generator* generator) { NCConstant target; int match; if(prim == NULL || isfillconst(prim)) { Datalist* fill = (filler==NULL?getfiller(basetype):filler); ASSERT(fill->length == 1); prim = datalistith(fill,0); } ASSERT(prim->nctype != NC_COMPOUND); /* Verify that the constant is consistent with the type */ match = 1; switch (prim->nctype) { case NC_CHAR: case NC_BYTE: case NC_SHORT: case NC_INT: case NC_FLOAT: case NC_DOUBLE: case NC_UBYTE: case NC_USHORT: case NC_UINT: case NC_INT64: case NC_UINT64: case NC_STRING: match = (basetype->subclass == NC_PRIM ? 1 : 0); break; #ifdef USE_NETCDF4 case NC_NIL: match = (basetype->subclass == NC_PRIM && basetype->typ.typecode == NC_STRING ? 1 : 0); break; case NC_OPAQUE: /* OPAQUE is also consistent with numbers */ match = (basetype->subclass == NC_OPAQUE || basetype->subclass == NC_PRIM ? 1 : 0); break; case NC_ECONST: match = (basetype->subclass == NC_ENUM ? 1 : 0); if(match) { /* Make sure this econst belongs to this enum */ Symbol* ec = prim->value.enumv; Symbol* en = ec->container; match = (en == basetype); } break; #endif default: match = 0; } if(!match) { semerror(constline(prim),"Data value is not consistent with the expected type: %s", basetype->name); } target.nctype = basetype->typ.typecode; if(target.nctype != NC_ECONST) { convert1(prim,&target); } switch (target.nctype) { case NC_ECONST: if(basetype->subclass != NC_ENUM) { semerror(constline(prim),"Conversion to enum not supported (yet)"); } break; case NC_OPAQUE: normalizeopaquelength(&target,basetype->typ.size); break; default: break; } generator->constant(generator,&target,codebuf); return; }
static void generate_array(Symbol* vsym, Bytebuffer* code, Datalist* filler, Generator* generator, Writer writer ) { Dimset* dimset = &vsym->typ.dimset; int rank = dimset->ndims; Symbol* basetype = vsym->typ.basetype; nc_type typecode = basetype->typ.typecode; Odometer* odom = NULL; nciter_t iter; int firstunlim = findunlimited(dimset,1); int nunlim = countunlimited(dimset); int isnc3unlim = (nunlim <= 1 && (firstunlim == 0 || firstunlim == rank)); /* netcdf-3 case of at most 1 unlim in 0th dimension */ ASSERT(rank > 0); if(isnc3unlim) { /* Handle NC_CHAR case separately */ if(typecode == NC_CHAR) { Bytebuffer* charbuf = bbNew(); gen_chararray(dimset,0,vsym->data,charbuf,filler); generator->charconstant(generator,code,charbuf); /* Create an odometer to get the dimension info */ odom = newodometer(dimset,NULL,NULL); writer(generator,vsym,code,odom->rank,odom->start,odom->count); // writer(generator,vsym,code,odom->rank,0,bbLength(charbuf)); bbFree(charbuf); } else { /* typecode != NC_CHAR */ /* Case: dim 1..rank-1 are not unlimited, dim 0 might be */ size_t offset = 0; /* where are we in the data list */ size_t nelems = 0; /* # of data list items to generate */ /* Create an iterator and odometer and just walk the datalist */ nc_get_iter(vsym,nciterbuffersize,&iter); odom = newodometer(dimset,NULL,NULL); for(;;offset+=nelems) { int i,uid; nelems=nc_next_iter(&iter,odometerstartvector(odom),odometercountvector(odom)); if(nelems == 0) break; bbClear(code); generator->listbegin(generator,LISTDATA,vsym->data->length,code,&uid); for(i=0;i<nelems;i++) { NCConstant* con = datalistith(vsym->data,i+offset); generator->list(generator,LISTDATA,uid,i,code); generate_basetype(basetype,con,code,filler,generator); } generator->listend(generator,LISTDATA,uid,i,code); writer(generator,vsym,code,rank,odom->start,odom->count); } } } else { /* Hard case: multiple unlimited dimensions or unlim in dim > 0*/ /* Setup iterator and odometer */ nc_get_iter(vsym,NC_MAX_UINT,&iter); /* effectively infinite */ odom = newodometer(dimset,NULL,NULL); for(;;) {/* iterate in nelem chunks */ /* get nelems count and modify odometer */ size_t nelems=nc_next_iter(&iter,odom->start,odom->count); if(nelems == 0) break; generate_arrayr(vsym,code,vsym->data, odom, /*dim index=*/0, filler,generator ); writer(generator,vsym,code,odom->rank,odom->start,odom->count); } } if(odom != NULL) odometerfree(odom); }
/* Generate an instance of the basetype */ void generate_basetype(Symbol* tsym, NCConstant* con, Bytebuffer* codebuf, Datalist* filler, Generator* generator) { Datalist* data; switch (tsym->subclass) { case NC_ENUM: case NC_OPAQUE: case NC_PRIM: if(islistconst(con)) { semerror(constline(con),"Expected primitive found {..}"); } generate_primdata(tsym,con,codebuf,filler,generator); break; case NC_COMPOUND: { int i,uid, nfields, dllen; if(con == NULL || isfillconst(con)) { Datalist* fill = (filler==NULL?getfiller(tsym):filler); ASSERT(fill->length == 1); con = &fill->data[0]; if(!islistconst(con)) semerror(con->lineno,"Compound data fill value is not enclosed in {..}"); } if(!islistconst(con)) {/* fail on no compound*/ semerror(constline(con),"Compound data must be enclosed in {..}"); } data = con->value.compoundv; nfields = listlength(tsym->subnodes); dllen = datalistlen(data); if(dllen > nfields) { semerror(con->lineno,"Datalist longer than the number of compound fields"); break; } generator->listbegin(generator,LISTCOMPOUND,listlength(tsym->subnodes),codebuf,&uid); for(i=0;i<nfields;i++) { Symbol* field = (Symbol*)listget(tsym->subnodes,i); con = datalistith(data,i); generator->list(generator,LISTCOMPOUND,uid,i,codebuf); generate_basetype(field,con,codebuf,NULL,generator); } generator->listend(generator,LISTCOMPOUND,uid,i,codebuf); } break; case NC_VLEN: { Bytebuffer* vlenbuf; int uid; size_t count; if(con == NULL || isfillconst(con)) { Datalist* fill = (filler==NULL?getfiller(tsym):filler); ASSERT(fill->length == 1); con = &fill->data[0]; if(con->nctype != NC_COMPOUND) { semerror(con->lineno,"Vlen data fill value is not enclosed in {..}"); } } if(!islistconst(con)) { semerror(constline(con),"Vlen data must be enclosed in {..}"); } data = con->value.compoundv; /* generate the nc_vlen_t instance*/ vlenbuf = bbNew(); if(tsym->typ.basetype->typ.typecode == NC_CHAR) { gen_charvlen(data,vlenbuf); generator->vlenstring(generator,vlenbuf,&uid,&count); } else { generator->listbegin(generator,LISTVLEN,data->length,codebuf,&uid); for(count=0;count<data->length;count++) { NCConstant* con; generator->list(generator,LISTVLEN,uid,count,vlenbuf); con = datalistith(data,count); generate_basetype(tsym->typ.basetype,con,vlenbuf,NULL,generator); } generator->listend(generator,LISTVLEN,uid,count,codebuf,(void*)vlenbuf); } generator->vlendecl(generator,codebuf,tsym,uid,count,vlenbuf); bbFree(vlenbuf); } break; case NC_FIELD: if(tsym->typ.dimset.ndims > 0) { /* Verify that we have a sublist (or fill situation) */ if(con != NULL && !isfillconst(con) && !islistconst(con)) semerror(constline(con),"Dimensioned fields must be enclose in {...}"); generate_fieldarray(tsym->typ.basetype,con,&tsym->typ.dimset,codebuf,filler,generator); } else { generate_basetype(tsym->typ.basetype,con,codebuf,NULL,generator); } break; default: PANIC1("generate_basetype: unexpected subclass %d",tsym->subclass); } }
static void generate_arrayr(Symbol* vsym, Bytebuffer* code, Datalist* list, Odometer* odom, int dimindex, Datalist* filler, Generator* generator ) { Symbol* basetype = vsym->typ.basetype; Dimset* dimset = &vsym->typ.dimset; int rank = dimset->ndims; int lastunlimited; int typecode = basetype->typ.typecode; lastunlimited = findlastunlimited(dimset); if(lastunlimited == rank) lastunlimited = 0; ASSERT(rank > 0); ASSERT(dimindex >= 0 && dimindex < rank); #ifdef CHARBUG ASSERT(typecode != NC_CHAR); #else /*!CHARBUG*/ if(dimindex == lastunlimited && typecode == NC_CHAR) { Bytebuffer* charbuf = bbNew(); gen_leafchararray(dimset,dimindex,list,charbuf,filler); generator->charconstant(generator,code,charbuf); bbFree(charbuf); } else #endif /*!CHARBUG*/ if(dimindex == lastunlimited) { int uid,i; Odometer* slabodom; /* build a special odometer to walk the last few dimensions (similar to case 2 above) */ slabodom = newsubodometer(odom,dimset,dimindex,rank); /* compute the starting offset in our datalist (Assumes that slabodom->index[i] == slabodom->start[i]) */ generator->listbegin(generator,LISTDATA,list->length,code,&uid); for(i=0;odometermore(slabodom);i++) { size_t offset = odometeroffset(slabodom); NCConstant* con = datalistith(list,offset); #ifdef USE_NOFILL if(nofill_flag && con == NULL) break; #endif generator->list(generator,LISTDATA,uid,i,code); generate_basetype(basetype,con,code,filler,generator); odometerincr(slabodom); } generator->listend(generator,LISTDATA,uid,i,code); odometerfree(slabodom); } else { /* If we are strictly to the left of the next unlimited then our datalist is a list of compounds representing the next unlimited; so walk the subarray from this index upto next unlimited. */ int i; Odometer* slabodom; int nextunlimited = findunlimited(dimset,dimindex+1); ASSERT((dimindex < nextunlimited && (dimset->dimsyms[nextunlimited]->dim.isunlimited))); /* build a sub odometer */ slabodom = newsubodometer(odom,dimset,dimindex,nextunlimited); /* compute the starting offset in our datalist (Assumes that slabodom->index[i] == slabodom->start[i]) */ for(i=0;odometermore(slabodom);i++) { size_t offset = odometeroffset(slabodom); NCConstant* con = datalistith(list,offset); #ifdef USE_NOFILL if(nofill_flag && con == NULL) break; #endif if(con == NULL || con->nctype == NC_FILL) { if(filler == NULL) filler = getfiller(vsym); generate_arrayr(vsym,code,filler,odom,nextunlimited,NULL,generator); } else if(!islistconst(con)) semwarn(constline(con),"Expected {...} representing unlimited list"); else { Datalist* sublist = con->value.compoundv; generate_arrayr(vsym,code,sublist,odom,nextunlimited,filler,generator); } odometerincr(slabodom); } odometerfree(slabodom); } return; }
/** The basic idea is to split the set of dimensions into groups and iterate over each group. A group is defined as the range of indices starting at an unlimited dimension upto (but not including) the next unlimited. The first group starts at index 0, even if dimension 0 is not unlimited. The last group is everything from the last unlimited dimension thru the last dimension (index rank-1). */ static void generate_arrayr(Symbol* vsym, Bytebuffer* code, Datalist* list, Odometer* odom, int dimindex, Datalist* filler, Generator* generator ) { int uid,i; Symbol* basetype = vsym->typ.basetype; Dimset* dimset = &vsym->typ.dimset; int rank = dimset->ndims; int lastunlimited = findlastunlimited(dimset); int nextunlimited = findunlimited(dimset,dimindex+1); int typecode = basetype->typ.typecode; int islastgroup = (lastunlimited == rank || dimindex >= lastunlimited || dimindex == rank-1); Odometer* subodom = NULL; ASSERT(rank > 0); ASSERT((dimindex >= 0 && dimindex < rank)); if(islastgroup) { /* Handle NC_CHAR case separately */ if(typecode == NC_CHAR) { Bytebuffer* charbuf = bbNew(); gen_chararray(dimset,dimindex,list,charbuf,filler); generator->charconstant(generator,code,charbuf); bbFree(charbuf); } else { /* build a special odometer to walk the last few dimensions */ subodom = newsubodometer(odom,dimset,dimindex,rank); generator->listbegin(generator,LISTDATA,list->length,code,&uid); for(i=0;odometermore(subodom);i++) { size_t offset = odometeroffset(subodom); NCConstant* con = datalistith(list,offset); generator->list(generator,LISTDATA,uid,i,code); generate_basetype(basetype,con,code,filler,generator); odometerincr(subodom); } generator->listend(generator,LISTDATA,uid,i,code); odometerfree(subodom); subodom = NULL; } } else {/* !islastgroup */ /* Our datalist must be a list of compounds representing the next unlimited; so walk the subarray from this index upto next unlimited. */ ASSERT((dimindex < nextunlimited)); ASSERT((isunlimited(dimset,nextunlimited))); /* build a sub odometer */ subodom = newsubodometer(odom,dimset,dimindex,nextunlimited); for(i=0;odometermore(subodom);i++) { size_t offset = odometeroffset(subodom); NCConstant* con = datalistith(list,offset); if(con == NULL || con->nctype == NC_FILL) { if(filler == NULL) filler = getfiller(vsym); generate_arrayr(vsym,code,filler,odom,nextunlimited,NULL,generator); } else if(islistconst(con)) { Datalist* sublist = compoundfor(con); generate_arrayr(vsym,code,sublist,odom,nextunlimited,filler,generator); } else { semerror(constline(con),"Expected {...} representing unlimited list"); return; } odometerincr(subodom); } odometerfree(subodom); subodom = NULL; } if(subodom != NULL) odometerfree(subodom); return; }
/* Recursive helper that does the bulk of the work */ static int bin_generate_data_r(NCConstant* instance, Symbol* tsym, Datalist* fillvalue, Bytebuffer* databuf) { int stat = NC_NOERR; if(instance->nctype == NC_FILLVALUE) { /* replace with fillvalue for the type */ Datalist* filllist = (fillvalue == NULL ? getfiller(tsym) : fillvalue); ASSERT(datalistlen(filllist)==1) instance = datalistith(filllist,0); } switch (tsym->subclass) { case NC_PRIM: { switch (tsym->nc_id) { case NC_CHAR: { char* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_CHAR; convert1(instance,tmp); p = &tmp->value.charv;; bbAppendn(databuf,p,sizeof(char)); reclaimconstant(tmp); } break; case NC_BYTE: { signed char* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_BYTE; convert1(instance,tmp); p = &tmp->value.int8v; bbAppendn(databuf,p,sizeof(signed char)); reclaimconstant(tmp); } break; case NC_UBYTE: { unsigned char* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_UBYTE; convert1(instance,tmp); p = &tmp->value.uint8v; bbAppendn(databuf,p,sizeof(unsigned char)); reclaimconstant(tmp); } break; case NC_SHORT: { short* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_SHORT; convert1(instance,tmp); p = &tmp->value.int16v; bbAppendn(databuf,p,sizeof(short)); reclaimconstant(tmp); } break; case NC_USHORT: { unsigned short* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_USHORT; convert1(instance,tmp); p = &tmp->value.uint16v; bbAppendn(databuf,p,sizeof(unsigned short)); reclaimconstant(tmp); } break; case NC_INT: { int* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_INT; convert1(instance,tmp); p = &tmp->value.int32v; bbAppendn(databuf,p,sizeof(int)); reclaimconstant(tmp); } break; case NC_UINT: { unsigned int* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_UINT; convert1(instance,tmp); p = &tmp->value.uint32v; bbAppendn(databuf,p,sizeof(unsigned int)); reclaimconstant(tmp); } break; case NC_INT64: { long long* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_INT64; convert1(instance,tmp); p = &tmp->value.int64v; bbAppendn(databuf,p,sizeof(long long)); reclaimconstant(tmp); } break; case NC_UINT64: { unsigned long long* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_UINT64; convert1(instance,tmp); p = &tmp->value.uint64v; bbAppendn(databuf,p,sizeof(unsigned long long)); reclaimconstant(tmp); } break; case NC_FLOAT: { float* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_FLOAT; convert1(instance,tmp); p = &tmp->value.floatv; bbAppendn(databuf,p,sizeof(float)); reclaimconstant(tmp); } break; case NC_DOUBLE: { double* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_DOUBLE; convert1(instance,tmp); p = &tmp->value.doublev; bbAppendn(databuf,p,sizeof(double)); reclaimconstant(tmp); } break; case NC_STRING: { char* p = NULL; NCConstant* tmp = nullconst(); tmp->nctype = NC_STRING; convert1(instance,tmp); p = emalloc(tmp->value.stringv.len+1); memcpy(p,tmp->value.stringv.stringv,tmp->value.stringv.len); p[tmp->value.stringv.len] = '\0'; bbAppendn(databuf,&p,sizeof(char*)); reclaimconstant(tmp); } break; default: stat = NC_EINTERNAL; goto done; /* Should never happen */ } break; /*switch*/ } break; /*NC_PRIM*/ case NC_ENUM: { Symbol* basetype = tsym->typ.basetype; /* Pretend */ stat = bin_generate_data_r(instance,basetype,fillvalue,databuf); } break; case NC_OPAQUE: { unsigned char* bytes = NULL; size_t len = 0; if(instance->nctype != NC_OPAQUE) {stat = NC_EBADTYPE; goto done;} /* Assume the opaque string has been normalized */ bytes=makebytestring(instance->value.opaquev.stringv,&len); if(bytes == NULL) {stat = NC_ENOMEM; goto done;} bbAppendn(databuf,(void*)bytes,len); free(bytes); } break; case NC_VLEN: { Datalist* sublist = NULL; Bytebuffer* vlendata = NULL; nc_vlen_t p; if(instance->nctype != NC_COMPOUND) { nclog(NCLOGERR,"Translating vlen: expected sublist"); stat = NC_EBADTYPE; goto done; } sublist = instance->value.compoundv; vlendata = bbNew(); if((stat = binary_generate_data(sublist,tsym->typ.basetype,NULL,vlendata))) goto done; p.len = datalistlen(sublist); p.p = bbContents(vlendata); bbAppendn(databuf,(char*)&p,sizeof(nc_vlen_t)); } break; case NC_COMPOUND: { /* The really hard one */ size_t nfields, fid, i; Datalist* cmpd = instance->value.compoundv; write_alignment(tsym->typ.cmpdalign,databuf); /* Get info about each field in turn and build it*/ nfields = listlength(tsym->subnodes); for(fid=0;fid<nfields;fid++) { Symbol* field = listget(tsym->subnodes,fid); NCConstant* fieldinstance = datalistith(cmpd,fid); int ndims = field->typ.dimset.ndims; size_t arraycount; if(ndims == 0) { ndims=1; /* fake the scalar case */ } /* compute the total number of elements in the field array */ arraycount = 1; for(i=0;i<ndims;i++) arraycount *= field->typ.dimset.dimsyms[i]->dim.declsize; write_alignment(field->typ.alignment,databuf); /* Write the instances */ for(i=0;i<arraycount;i++) { if((stat = bin_generate_data_r(fieldinstance, field->typ.basetype, NULL, databuf))) goto done; } } } break; default: stat = NC_EINTERNAL; goto done; /* Should never happen */ } done: return stat; }
/* Collect info by repeated walking of the attribute value list. */ static nc_type inferattributetype1(Datalist* adata) { nc_type result = NC_NAT; int hasneg = 0; int stringcount = 0; int charcount = 0; int forcefloat = 0; int forcedouble = 0; int forceuint64 = 0; int i; /* Walk the top level set of attribute values to ensure non-nesting */ for(i=0;i<datalistlen(adata);i++) { NCConstant* con = datalistith(adata,i); if(con == NULL) return NC_NAT; if(con->nctype > NC_MAX_ATOMIC_TYPE) { /* illegal */ return NC_NAT; } } /* Walk repeatedly to get info for inference (loops could be combined) */ /* Compute: all strings or chars? */ stringcount = 0; charcount = 0; for(i=0;i<datalistlen(adata);i++) { NCConstant* con = datalistith(adata,i); if(con->nctype == NC_STRING) stringcount++; else if(con->nctype == NC_CHAR) charcount++; } if((stringcount+charcount) > 0) { if((stringcount+charcount) < datalistlen(adata)) return NC_NAT; /* not all textual */ return NC_CHAR; } /* Compute: any floats/doubles? */ forcefloat = 0; forcedouble = 0; for(i=0;i<datalistlen(adata);i++) { NCConstant* con = datalistith(adata,i); if(con->nctype == NC_FLOAT) forcefloat = 1; else if(con->nctype == NC_DOUBLE) {forcedouble=1; break;} } if(forcedouble) return NC_DOUBLE; if(forcefloat) return NC_FLOAT; /* At this point all the constants should be integers */ /* Compute: are there any uint64 values > NC_MAX_INT64? */ forceuint64 = 0; for(i=0;i<datalistlen(adata);i++) { NCConstant* con = datalistith(adata,i); if(con->nctype != NC_UINT64) continue; if(con->value.uint64v > NC_MAX_INT64) {forceuint64=1; break;} } if(forceuint64) return NC_UINT64; /* Compute: are there any negative constants? */ hasneg = 0; for(i=0;i<datalistlen(adata);i++) { NCConstant* con = datalistith(adata,i); switch (con->nctype) { case NC_BYTE : if(con->value.int8v < 0) {hasneg = 1;} break; case NC_SHORT: if(con->value.int16v < 0) {hasneg = 1;} break; case NC_INT: if(con->value.int32v < 0) {hasneg = 1;} break; } } /* Compute: inferred integer type */ result = NC_NAT; for(i=0;i<datalistlen(adata);i++) { NCConstant* con = datalistith(adata,i); result = infertype(result,con->nctype,hasneg); if(result == NC_NAT) break; /* something wrong */ } return result; }