/************************************** * Create a struct/union/class type. * Params: * name = name of struct (this function makes its own copy of the string) * Returns: * Tcount already incremented */ type *type_struct_class(const char *name, unsigned alignsize, unsigned structsize, type *arg1type, type *arg2type, bool isUnion, bool isClass, bool isPOD) { Symbol *s = symbol_calloc(name); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Salignsize = alignsize; s->Sstruct->Sstructalign = alignsize; s->Sstruct->Sstructsize = structsize; s->Sstruct->Sarg1type = arg1type; s->Sstruct->Sarg2type = arg2type; if (!isPOD) s->Sstruct->Sflags |= STRnotpod; if (isUnion) s->Sstruct->Sflags |= STRunion; if (isClass) { s->Sstruct->Sflags |= STRclass; assert(!isUnion && isPOD); } type *t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; t->Tcount++; return t; }
type *TypeAArray::toCtype() { type *t; if (ctype) return ctype; if (0 && global.params.symdebug) { /* An associative array is represented by: * struct AArray { size_t length; void* ptr; } */ static Symbol *s; if (!s) { s = symbol_calloc("_AArray"); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = alignsize(); s->Sstruct->Sstructalign = global.structalign; s->Sstruct->Sstructsize = size(0); slist_add(s); Symbol *s1 = symbol_name("length", SCmember, Type::tsize_t->toCtype()); list_append(&s->Sstruct->Sfldlst, s1); Symbol *s2 = symbol_name("data", SCmember, Type::tvoidptr->toCtype()); s2->Smemoff = Type::tsize_t->size(); list_append(&s->Sstruct->Sfldlst, s2); } t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; } else { if (global.params.symdebug == 1) { /* Generate D symbolic debug info, rather than C * Tnext: element type * Tkey: key type */ t = type_allocn(TYaarray, next->toCtype()); t->Tkey = index->toCtype(); t->Tkey->Tcount++; } else t = type_fake(TYaarray); } t->Tcount++; ctype = t; return t; }
type *TypeDelegate::toCtype() { type *t; if (ctype) return ctype; if (0 && global.params.symdebug) { /* A delegate consists of: * _Delegate { void* frameptr; Function *funcptr; } */ static Symbol *s; if (!s) { s = symbol_calloc("_Delegate"); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = alignsize(); s->Sstruct->Sstructalign = global.structalign; s->Sstruct->Sstructsize = size(0); slist_add(s); Symbol *s1 = symbol_name("frameptr", SCmember, Type::tvoidptr->toCtype()); list_append(&s->Sstruct->Sfldlst, s1); Symbol *s2 = symbol_name("funcptr", SCmember, Type::tvoidptr->toCtype()); s2->Smemoff = Type::tvoidptr->size(); list_append(&s->Sstruct->Sfldlst, s2); } t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; } else { if (global.params.symdebug == 1) { // Generate D symbolic debug info, rather than C t = type_allocn(TYdelegate, next->toCtype()); } else t = type_fake(TYdelegate); } t->Tcount++; ctype = t; return t; }
type *TypeDArray::toCtype() { type *t; if (ctype) return ctype; if (0 && global.params.symdebug) { /* Create a C type out of: * struct _Array_T { size_t length; T* data; } */ Symbol *s; char *id; assert(next->deco); id = (char *) alloca(7 + strlen(next->deco) + 1); sprintf(id, "_Array_%s", next->deco); s = symbol_calloc(id); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = alignsize(); s->Sstruct->Sstructalign = global.structalign; s->Sstruct->Sstructsize = size(0); slist_add(s); Symbol *s1 = symbol_name("length", SCmember, Type::tsize_t->toCtype()); list_append(&s->Sstruct->Sfldlst, s1); Symbol *s2 = symbol_name("data", SCmember, next->pointerTo()->toCtype()); s2->Smemoff = Type::tsize_t->size(); list_append(&s->Sstruct->Sfldlst, s2); t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; } else { if (global.params.symdebug == 1) { // Generate D symbolic debug info, rather than C t = type_allocn(TYdarray, next->toCtype()); } else t = type_fake(TYdarray); } t->Tcount++; ctype = t; return t; }
type *TypeClass::toCtype() { type *t; Symbol *s; //printf("TypeClass::toCtype() %s\n", toChars()); if (ctype) return ctype; /* Need this symbol to do C++ name mangling */ const char *name = sym->isCPPinterface() ? sym->ident->toChars() : sym->toPrettyChars(); s = symbol_calloc(name); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= STRclass; s->Sstruct->Salignsize = sym->alignsize; s->Sstruct->Sstructalign = sym->structalign; s->Sstruct->Sstructsize = sym->structsize; t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; slist_add(s); t = type_allocn(TYnptr, t); t->Tcount++; ctype = t; /* Add in fields of the class * (after setting ctype to avoid infinite recursion) */ if (global.params.symdebug) for (int i = 0; i < sym->fields.dim; i++) { VarDeclaration *v = sym->fields.tdata()[i]; Symbol *s2 = symbol_name(v->ident->toChars(), SCmember, v->type->toCtype()); s2->Smemoff = v->offset; list_append(&s->Sstruct->Sfldlst, s2); } return t; }
Classsym *fake_classsym(char *name) { TYPE *t; Classsym *scc; scc = (Classsym *)symbol_calloc("ClassInfo"); scc->Sclass = SCstruct; scc->Sstruct = struct_calloc(); scc->Sstruct->Sstructalign = 8; //scc->Sstruct->ptrtype = TYnptr; scc->Sstruct->Sflags = STRglobal; t = type_alloc(TYstruct); t->Tflags |= TFsizeunknown | TFforward; t->Ttag = scc; // structure tag name assert(t->Tmangle == 0); t->Tmangle = mTYman_c; t->Tcount++; scc->Stype = t; slist_add(scc); return scc; }
type *TypeStruct::toCtype() { if (ctype) return ctype; //printf("TypeStruct::toCtype() '%s'\n", sym->toChars()); type *t = type_alloc(TYstruct); Type *tm = mutableOf(); if (tm->ctype) { Symbol *s = tm->ctype->Ttag; t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; // Add modifiers switch (mod) { case 0: assert(0); break; case MODconst: case MODwild: t->Tty |= mTYconst; break; case MODimmutable: t->Tty |= mTYimmutable; break; case MODshared: t->Tty |= mTYshared; break; case MODshared | MODwild: case MODshared | MODconst: t->Tty |= mTYshared | mTYconst; break; default: assert(0); } ctype = t; } else { Symbol *s = symbol_calloc(sym->toPrettyChars()); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = sym->alignsize; s->Sstruct->Sstructalign = sym->alignsize; s->Sstruct->Sstructsize = sym->structsize; if (sym->isUnionDeclaration()) s->Sstruct->Sflags |= STRunion; t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; slist_add(s); tm->ctype = t; ctype = t; /* Add in fields of the struct * (after setting ctype to avoid infinite recursion) */ if (global.params.symdebug) for (int i = 0; i < sym->fields.dim; i++) { VarDeclaration *v = sym->fields.tdata()[i]; Symbol *s2 = symbol_name(v->ident->toChars(), SCmember, v->type->toCtype()); s2->Smemoff = v->offset; list_append(&s->Sstruct->Sfldlst, s2); } } //printf("t = %p, Tflags = x%x\n", t, t->Tflags); return t; }