Ejemplo n.º 1
0
/*
This walk of the data lists collects
vlen sublists and constructs separate C constants
for each of them. The "id" of each list is then
recorded in the containing datalist.
*/
void
cdata_vlenconstants(List* vlenconstants, Bytebuffer* codebuf)
{
    int i,nvlen;
    Datasrc* vlensrc;
    Bytebuffer* tmp = bbNew();

    nvlen = listlength(vlenconstants);
    for(i=0;i<nvlen;i++) {
	Constant* cmpd = (Constant*)listget(vlenconstants,i);
	int chartype;
	Symbol* tsym = cmpd->value.compoundv->vlen.schema;
        ASSERT(tsym != NULL);
        chartype = (tsym->typ.basetype->typ.typecode == NC_CHAR);

        bbprintf0(tmp,"static const %s vlen_%u[] = ",
	        ctypename(tsym->typ.basetype),
                cmpd->value.compoundv->vlen.uid);
	bbCatbuf(codebuf,tmp);
	vlensrc = datalist2src(cmpd->value.compoundv);
	bbAppend(codebuf,'{');
	if(chartype) {
   	    /* Collect the char vlen in a separate buffer */
	    Bytebuffer* vlenbuf = bbNew();
            gen_charvlen(vlensrc,vlenbuf);
	    /* Add to the existing data buf as a single constant */
   	    cquotestring(vlenbuf);
            bbCatbuf(codebuf,vlenbuf);
   	    bbFree(vlenbuf);
	} else {
	    size_t count = 0;
  	    while(srcmore(vlensrc)) {
	        if(count > 0) bbCat(codebuf,", ");
                cdata_basetype(tsym->typ.basetype,vlensrc,codebuf,NULL);
		count++;
	    }
	    ASSERT(count == cmpd->value.compoundv->vlen.count);
        }
	bbCat(codebuf,"} ;\n");
    }
    bbFree(tmp);
}
Ejemplo n.º 2
0
static int
c_vlendecl(Generator* generator, Bytebuffer* codebuf, Symbol* tsym, int uid, size_t count, ...)
{
    /* Build a bytebuffer to capture the vlen decl */
    List* declstack = (List*)generator->state;
    Bytebuffer* decl = bbNew();
    Bytebuffer* vlenbuf;
    va_list ap;
    vastart(ap,count);
    vlenbuf = va_arg(ap, Bytebuffer*);
    va_end(ap);
    bbprintf0(decl,"static const %s vlen_%u[] = {",
	        ctypename(tsym->typ.basetype),
                uid);
    commify(vlenbuf);
    bbCatbuf(decl,vlenbuf);
    bbCat(decl,"} ;");
    listpush(declstack,(void*)decl);
    /* Now generate the reference to buffer */
    bbprintf(codebuf,"{%u,(void*)vlen_%u}",count,uid);
    return 1;
}