00001 #include "c.h"
00002 #include <float.h>
00003
00004
00005 static Field isfield(const char *, Field);
00006 static Type type(int, Type, int, int, void *);
00007
00008 static struct entry {
00009 struct type type;
00010 struct entry *link;
00011 } *typetable[128];
00012 static int maxlevel;
00013
00014 static Symbol pointersym;
00015
00016 Type chartype;
00017 Type doubletype;
00018 Type floattype;
00019 Type inttype;
00020 Type longdouble;
00021 Type longtype;
00022 Type longlong;
00023 Type shorttype;
00024 Type signedchar;
00025 Type unsignedchar;
00026 Type unsignedlong;
00027 Type unsignedlonglong;
00028 Type unsignedshort;
00029 Type unsignedtype;
00030 Type funcptype;
00031 Type charptype;
00032 Type voidptype;
00033 Type voidtype;
00034 Type unsignedptr;
00035 Type signedptr;
00036 Type widechar;
00037
00038 static Type xxinit(int op, char *name, Metrics m) {
00039 Symbol p = install(string(name), &types, GLOBAL, PERM);
00040 Type ty = type(op, 0, m.size, m.align, p);
00041
00042 assert(ty->align == 0 || ty->size%ty->align == 0);
00043 p->type = ty;
00044 p->addressed = m.outofline;
00045 switch (ty->op) {
00046 case INT:
00047 p->u.limits.max.i = ones(8*ty->size)>>1;
00048 p->u.limits.min.i = -p->u.limits.max.i - 1;
00049 break;
00050 case UNSIGNED:
00051 p->u.limits.max.u = ones(8*ty->size);
00052 p->u.limits.min.u = 0;
00053 break;
00054 case FLOAT:
00055 if (ty->size == sizeof (float))
00056 p->u.limits.max.d = FLT_MAX;
00057 else if (ty->size == sizeof (double))
00058 p->u.limits.max.d = DBL_MAX;
00059 else
00060 p->u.limits.max.d = LDBL_MAX;
00061 p->u.limits.min.d = -p->u.limits.max.d;
00062 break;
00063 default: assert(0);
00064 }
00065 return ty;
00066 }
00067 static Type type(int op, Type ty, int size, int align, void *sym) {
00068 unsigned h = (op^((unsigned long)ty>>3))
00069 &(NELEMS(typetable)-1);
00070 struct entry *tn;
00071
00072 if (op != FUNCTION && (op != ARRAY || size > 0))
00073 for (tn = typetable[h]; tn; tn = tn->link)
00074 if (tn->type.op == op && tn->type.type == ty
00075 && tn->type.size == size && tn->type.align == align
00076 && tn->type.u.sym == sym)
00077 return &tn->type;
00078 NEW0(tn, PERM);
00079 tn->type.op = op;
00080 tn->type.type = ty;
00081 tn->type.size = size;
00082 tn->type.align = align;
00083 tn->type.u.sym = sym;
00084 tn->link = typetable[h];
00085 typetable[h] = tn;
00086 return &tn->type;
00087 }
00088 void type_init(int argc, char *argv[]) {
00089 static int inited;
00090 int i;
00091
00092 if (inited)
00093 return;
00094 inited = 1;
00095 if (!IR)
00096 return;
00097 for (i = 1; i < argc; i++) {
00098 int size, align, outofline;
00099 if (strncmp(argv[i], "-unsigned_char=", 15) == 0)
00100 IR->unsigned_char = argv[i][15] - '0';
00101 #define xx(name) \
00102 else if (sscanf(argv[i], "-" #name "=%d,%d,%d", &size, &align, &outofline) == 3) { \
00103 IR->name.size = size; IR->name.align = align; \
00104 IR->name.outofline = outofline; }
00105 xx(charmetric)
00106 xx(shortmetric)
00107 xx(intmetric)
00108 xx(longmetric)
00109 xx(longlongmetric)
00110 xx(floatmetric)
00111 xx(doublemetric)
00112 xx(longdoublemetric)
00113 xx(ptrmetric)
00114 xx(structmetric)
00115 #undef xx
00116 }
00117 #define xx(v,name,op,metrics) v=xxinit(op,name,IR->metrics)
00118 xx(chartype, "char", IR->unsigned_char ? UNSIGNED : INT,charmetric);
00119 xx(doubletype, "double", FLOAT, doublemetric);
00120 xx(floattype, "float", FLOAT, floatmetric);
00121 xx(inttype, "int", INT, intmetric);
00122 xx(longdouble, "long double", FLOAT, longdoublemetric);
00123 xx(longtype, "long int", INT, longmetric);
00124 xx(longlong, "long long int", INT, longlongmetric);
00125 xx(shorttype, "short", INT, shortmetric);
00126 xx(signedchar, "signed char", INT, charmetric);
00127 xx(unsignedchar, "unsigned char", UNSIGNED,charmetric);
00128 xx(unsignedlong, "unsigned long", UNSIGNED,longmetric);
00129 xx(unsignedshort, "unsigned short", UNSIGNED,shortmetric);
00130 xx(unsignedtype, "unsigned int", UNSIGNED,intmetric);
00131 xx(unsignedlonglong,"unsigned long long",UNSIGNED,longlongmetric);
00132 #undef xx
00133 {
00134 Symbol p;
00135 p = install(string("void"), &types, GLOBAL, PERM);
00136 voidtype = type(VOID, NULL, 0, 0, p);
00137 p->type = voidtype;
00138 }
00139 pointersym = install(string("T*"), &types, GLOBAL, PERM);
00140 pointersym->addressed = IR->ptrmetric.outofline;
00141 pointersym->u.limits.max.p = (void*)ones(8*IR->ptrmetric.size);
00142 pointersym->u.limits.min.p = 0;
00143 voidptype = ptr(voidtype);
00144 funcptype = ptr(func(voidtype, NULL, 1));
00145 charptype = ptr(chartype);
00146 #define xx(v,t) if (v==NULL && t->size==voidptype->size && t->align==voidptype->align) v=t
00147 xx(unsignedptr,unsignedshort);
00148 xx(unsignedptr,unsignedtype);
00149 xx(unsignedptr,unsignedlong);
00150 xx(unsignedptr,unsignedlonglong);
00151 if (unsignedptr == NULL)
00152 unsignedptr = type(UNSIGNED, NULL, voidptype->size, voidptype->align, voidptype->u.sym);
00153 xx(signedptr,shorttype);
00154 xx(signedptr,inttype);
00155 xx(signedptr,longtype);
00156 xx(signedptr,longlong);
00157 if (signedptr == NULL)
00158 signedptr = type(INT, NULL, voidptype->size, voidptype->align, voidptype->u.sym);
00159 #undef xx
00160 widechar = unsignedshort;
00161 for (i = 0; i < argc; i++) {
00162 #define xx(name,type) \
00163 if (strcmp(argv[i], "-wchar_t=" #name) == 0) \
00164 widechar = type;
00165 xx(unsigned_char,unsignedchar)
00166 xx(unsigned_int,unsignedtype)
00167 xx(unsigned_short,unsignedshort)
00168 }
00169 #undef xx
00170 }
00171 void rmtypes(int lev) {
00172 if (maxlevel >= lev) {
00173 int i;
00174 maxlevel = 0;
00175 for (i = 0; i < NELEMS(typetable); i++) {
00176 struct entry *tn, **tq = &typetable[i];
00177 while ((tn = *tq) != NULL)
00178 if (tn->type.op == FUNCTION)
00179 tq = &tn->link;
00180 else if (tn->type.u.sym && tn->type.u.sym->scope >= lev)
00181 *tq = tn->link;
00182 else {
00183 if (tn->type.u.sym && tn->type.u.sym->scope > maxlevel)
00184 maxlevel = tn->type.u.sym->scope;
00185 tq = &tn->link;
00186 }
00187
00188 }
00189 }
00190 }
00191 Type ptr(Type ty) {
00192 return type(POINTER, ty, IR->ptrmetric.size,
00193 IR->ptrmetric.align, pointersym);
00194 }
00195 Type deref(Type ty) {
00196 if (isptr(ty))
00197 ty = ty->type;
00198 else
00199 error("type error: %s\n", "pointer expected");
00200 return isenum(ty) ? unqual(ty)->type : ty;
00201 }
00202 Type array(Type ty, int n, int a) {
00203 assert(ty);
00204 if (isfunc(ty)) {
00205 error("illegal type `array of %t'\n", ty);
00206 return array(inttype, n, 0);
00207 }
00208 if (isarray(ty) && ty->size == 0)
00209 error("missing array size\n");
00210 if (ty->size == 0) {
00211 if (unqual(ty) == voidtype)
00212 error("illegal type `array of %t'\n", ty);
00213 else if (Aflag >= 2)
00214 warning("declaring type array of %t' is undefined\n", ty);
00215
00216 } else if (n > INT_MAX/ty->size) {
00217 error("size of `array of %t' exceeds %d bytes\n",
00218 ty, INT_MAX);
00219 n = 1;
00220 }
00221 return type(ARRAY, ty, n*ty->size,
00222 a ? a : ty->align, NULL);
00223 }
00224 Type atop(Type ty) {
00225 if (isarray(ty))
00226 return ptr(ty->type);
00227 error("type error: %s\n", "array expected");
00228 return ptr(ty);
00229 }
00230 Type qual(int op, Type ty) {
00231 if (isarray(ty))
00232 ty = type(ARRAY, qual(op, ty->type), ty->size,
00233 ty->align, NULL);
00234 else if (isfunc(ty))
00235 warning("qualified function type ignored\n");
00236 else if (isconst(ty) && op == CONST
00237 || isvolatile(ty) && op == VOLATILE)
00238 error("illegal type `%k %t'\n", op, ty);
00239 else {
00240 if (isqual(ty)) {
00241 op += ty->op;
00242 ty = ty->type;
00243 }
00244 ty = type(op, ty, ty->size, ty->align, NULL);
00245 }
00246 return ty;
00247 }
00248 Type func(Type ty, Type *proto, int style) {
00249 if (ty && (isarray(ty) || isfunc(ty)))
00250 error("illegal return type `%t'\n", ty);
00251 ty = type(FUNCTION, ty, 0, 0, NULL);
00252 ty->u.f.proto = proto;
00253 ty->u.f.oldstyle = style;
00254 return ty;
00255 }
00256 Type freturn(Type ty) {
00257 if (isfunc(ty))
00258 return ty->type;
00259 error("type error: %s\n", "function expected");
00260 return inttype;
00261 }
00262 int variadic(Type ty) {
00263 if (isfunc(ty) && ty->u.f.proto) {
00264 int i;
00265 for (i = 0; ty->u.f.proto[i]; i++)
00266 ;
00267 return i > 1 && ty->u.f.proto[i-1] == voidtype;
00268 }
00269 return 0;
00270 }
00271 Type newstruct(int op, char *tag) {
00272 Symbol p;
00273
00274 assert(tag);
00275 if (*tag == 0)
00276 tag = stringd(genlabel(1));
00277 else
00278 if ((p = lookup(tag, types)) != NULL && (p->scope == level
00279 || p->scope == PARAM && level == PARAM+1)) {
00280 if (p->type->op == op && !p->defined)
00281 return p->type;
00282 error("redefinition of `%s' previously defined at %w\n",
00283 p->name, &p->src);
00284 }
00285 p = install(tag, &types, level, PERM);
00286 p->type = type(op, NULL, 0, 0, p);
00287 if (p->scope > maxlevel)
00288 maxlevel = p->scope;
00289 p->src = src;
00290 return p->type;
00291 }
00292 Field newfield(char *name, Type ty, Type fty) {
00293 Field p, *q = &ty->u.sym->u.s.flist;
00294
00295 if (name == NULL)
00296 name = stringd(genlabel(1));
00297 for (p = *q; p; q = &p->link, p = *q)
00298 if (p->name == name)
00299 error("duplicate field name `%s' in `%t'\n",
00300 name, ty);
00301 NEW0(p, PERM);
00302 *q = p;
00303 p->name = name;
00304 p->type = fty;
00305 if (xref) {
00306 if (ty->u.sym->u.s.ftab == NULL)
00307 ty->u.sym->u.s.ftab = table(NULL, level);
00308 install(name, &ty->u.sym->u.s.ftab, 0, PERM)->src = src;
00309 }
00310 return p;
00311 }
00312 int eqtype(Type ty1, Type ty2, int ret) {
00313 if (ty1 == ty2)
00314 return 1;
00315 if (ty1->op != ty2->op)
00316 return 0;
00317 switch (ty1->op) {
00318 case ENUM: case UNION: case STRUCT:
00319 case UNSIGNED: case INT: case FLOAT:
00320 return 0;
00321 case POINTER: return eqtype(ty1->type, ty2->type, 1);
00322 case VOLATILE: case CONST+VOLATILE:
00323 case CONST: return eqtype(ty1->type, ty2->type, 1);
00324 case ARRAY: if (eqtype(ty1->type, ty2->type, 1)) {
00325 if (ty1->size == ty2->size)
00326 return 1;
00327 if (ty1->size == 0 || ty2->size == 0)
00328 return ret;
00329 }
00330 return 0;
00331 case FUNCTION: if (eqtype(ty1->type, ty2->type, 1)) {
00332 Type *p1 = ty1->u.f.proto, *p2 = ty2->u.f.proto;
00333 if (p1 == p2)
00334 return 1;
00335 if (p1 && p2) {
00336 for ( ; *p1 && *p2; p1++, p2++)
00337 if (eqtype(unqual(*p1), unqual(*p2), 1) == 0)
00338 return 0;
00339 if (*p1 == NULL && *p2 == NULL)
00340 return 1;
00341 } else {
00342 if (variadic(p1 ? ty1 : ty2))
00343 return 0;
00344 if (p1 == NULL)
00345 p1 = p2;
00346 for ( ; *p1; p1++) {
00347 Type ty = unqual(*p1);
00348 if (promote(ty) != (isenum(ty) ? ty->type : ty))
00349 return 0;
00350 }
00351 return 1;
00352 }
00353 }
00354 return 0;
00355 }
00356 assert(0); return 0;
00357 }
00358 Type promote(Type ty) {
00359 ty = unqual(ty);
00360 switch (ty->op) {
00361 case ENUM:
00362 return inttype;
00363 case INT:
00364 if (ty->size < inttype->size)
00365 return inttype;
00366 break;
00367 case UNSIGNED:
00368 if (ty->size < inttype->size)
00369 return inttype;
00370 if (ty->size < unsignedtype->size)
00371 return unsignedtype;
00372 break;
00373 case FLOAT:
00374 if (ty->size < doubletype->size)
00375 return doubletype;
00376 }
00377 return ty;
00378 }
00379 Type signedint(Type ty) {
00380 if (ty->op == INT)
00381 return ty;
00382 assert(ty->op == UNSIGNED);
00383 #define xx(t) if (ty->size == t->size) return t
00384 xx(inttype);
00385 xx(longtype);
00386 xx(longlong);
00387 #undef xx
00388 assert(0); return NULL;
00389 }
00390 Type compose(Type ty1, Type ty2) {
00391 if (ty1 == ty2)
00392 return ty1;
00393 assert(ty1->op == ty2->op);
00394 switch (ty1->op) {
00395 case POINTER:
00396 return ptr(compose(ty1->type, ty2->type));
00397 case CONST+VOLATILE:
00398 return qual(CONST, qual(VOLATILE,
00399 compose(ty1->type, ty2->type)));
00400 case CONST: case VOLATILE:
00401 return qual(ty1->op, compose(ty1->type, ty2->type));
00402 case ARRAY: { Type ty = compose(ty1->type, ty2->type);
00403 if (ty1->size && (ty1->type->size && ty2->size == 0 || ty1->size == ty2->size))
00404 return array(ty, ty1->size/ty1->type->size, ty1->align);
00405 if (ty2->size && ty2->type->size && ty1->size == 0)
00406 return array(ty, ty2->size/ty2->type->size, ty2->align);
00407 return array(ty, 0, 0); }
00408 case FUNCTION: { Type *p1 = ty1->u.f.proto, *p2 = ty2->u.f.proto;
00409 Type ty = compose(ty1->type, ty2->type);
00410 List tlist = NULL;
00411 if (p1 == NULL && p2 == NULL)
00412 return func(ty, NULL, 1);
00413 if (p1 && p2 == NULL)
00414 return func(ty, p1, ty1->u.f.oldstyle);
00415 if (p2 && p1 == NULL)
00416 return func(ty, p2, ty2->u.f.oldstyle);
00417 for ( ; *p1 && *p2; p1++, p2++) {
00418 Type ty = compose(unqual(*p1), unqual(*p2));
00419 if (isconst(*p1) || isconst(*p2))
00420 ty = qual(CONST, ty);
00421 if (isvolatile(*p1) || isvolatile(*p2))
00422 ty = qual(VOLATILE, ty);
00423 tlist = append(ty, tlist);
00424 }
00425 assert(*p1 == NULL && *p2 == NULL);
00426 return func(ty, ltov(&tlist, PERM), 0); }
00427 }
00428 assert(0); return NULL;
00429 }
00430 int ttob(Type ty) {
00431 switch (ty->op) {
00432 case CONST: case VOLATILE: case CONST+VOLATILE:
00433 return ttob(ty->type);
00434 case VOID: case INT: case UNSIGNED: case FLOAT:
00435 return ty->op + sizeop(ty->size);
00436 case POINTER:
00437 return POINTER + sizeop(voidptype->size);
00438 case FUNCTION:
00439 return POINTER + sizeop(funcptype->size);
00440 case ARRAY: case STRUCT: case UNION:
00441 return STRUCT;
00442 case ENUM:
00443 return INT + sizeop(inttype->size);
00444 }
00445 assert(0); return INT;
00446 }
00447 Type btot(int op, int size) {
00448 #define xx(ty) if (size == (ty)->size) return ty;
00449 switch (optype(op)) {
00450 case F:
00451 xx(floattype);
00452 xx(doubletype);
00453 xx(longdouble);
00454 assert(0); return 0;
00455 case I:
00456 if (chartype->op == INT)
00457 xx(chartype);
00458 xx(signedchar);
00459 xx(shorttype);
00460 xx(inttype);
00461 xx(longtype);
00462 xx(longlong);
00463 assert(0); return 0;
00464 case U:
00465 if (chartype->op == UNSIGNED)
00466 xx(chartype);
00467 xx(unsignedchar);
00468 xx(unsignedshort);
00469 xx(unsignedtype);
00470 xx(unsignedlong);
00471 xx(unsignedlonglong);
00472 assert(0); return 0;
00473 case P:
00474 xx(voidptype);
00475 xx(funcptype);
00476 assert(0); return 0;
00477 }
00478 #undef xx
00479 assert(0); return 0;
00480 }
00481 int hasproto(Type ty) {
00482 if (ty == 0)
00483 return 1;
00484 switch (ty->op) {
00485 case CONST: case VOLATILE: case CONST+VOLATILE: case POINTER:
00486 case ARRAY:
00487 return hasproto(ty->type);
00488 case FUNCTION:
00489 return hasproto(ty->type) && ty->u.f.proto;
00490 case STRUCT: case UNION:
00491 case VOID: case FLOAT: case ENUM: case INT: case UNSIGNED:
00492 return 1;
00493 }
00494 assert(0); return 0;
00495 }
00496
00497 Field fieldlist(Type ty) {
00498 return ty->u.sym->u.s.flist;
00499 }
00500
00501
00502 Field fieldref(const char *name, Type ty) {
00503 Field p = isfield(name, unqual(ty)->u.sym->u.s.flist);
00504
00505 if (p && xref) {
00506 Symbol q;
00507 assert(unqual(ty)->u.sym->u.s.ftab);
00508 q = lookup(name, unqual(ty)->u.sym->u.s.ftab);
00509 assert(q);
00510 use(q, src);
00511 }
00512 return p;
00513 }
00514
00515
00516 Type ftype(Type rty, Type ty) {
00517 List list = append(ty, NULL);
00518
00519 list = append(voidtype, list);
00520 return func(rty, ltov(&list, PERM), 0);
00521 }
00522
00523
00524 static Field isfield(const char *name, Field flist) {
00525 for ( ; flist; flist = flist->link)
00526 if (flist->name == name)
00527 break;
00528 return flist;
00529 }
00530
00531
00532 void outtype(Type ty, FILE *f) {
00533 switch (ty->op) {
00534 case CONST+VOLATILE: case CONST: case VOLATILE:
00535 fprint(f, "%k %t", ty->op, ty->type);
00536 break;
00537 case STRUCT: case UNION: case ENUM:
00538 assert(ty->u.sym);
00539 if (ty->size == 0)
00540 fprint(f, "incomplete ");
00541 assert(ty->u.sym->name);
00542 if (*ty->u.sym->name >= '1' && *ty->u.sym->name <= '9') {
00543 Symbol p = findtype(ty);
00544 if (p == 0)
00545 fprint(f, "%k defined at %w", ty->op, &ty->u.sym->src);
00546 else
00547 fprint(f, p->name);
00548 } else {
00549 fprint(f, "%k %s", ty->op, ty->u.sym->name);
00550 if (ty->size == 0)
00551 fprint(f, " defined at %w", &ty->u.sym->src);
00552 }
00553 break;
00554 case VOID: case FLOAT: case INT: case UNSIGNED:
00555 fprint(f, ty->u.sym->name);
00556 break;
00557 case POINTER:
00558 fprint(f, "pointer to %t", ty->type);
00559 break;
00560 case FUNCTION:
00561 fprint(f, "%t function", ty->type);
00562 if (ty->u.f.proto && ty->u.f.proto[0]) {
00563 int i;
00564 fprint(f, "(%t", ty->u.f.proto[0]);
00565 for (i = 1; ty->u.f.proto[i]; i++)
00566 if (ty->u.f.proto[i] == voidtype)
00567 fprint(f, ",...");
00568 else
00569 fprint(f, ",%t", ty->u.f.proto[i]);
00570 fprint(f, ")");
00571 } else if (ty->u.f.proto && ty->u.f.proto[0] == 0)
00572 fprint(f, "(void)");
00573
00574 break;
00575 case ARRAY:
00576 if (ty->size > 0 && ty->type && ty->type->size > 0) {
00577 fprint(f, "array %d", ty->size/ty->type->size);
00578 while (ty->type && isarray(ty->type) && ty->type->type->size > 0) {
00579 ty = ty->type;
00580 fprint(f, ",%d", ty->size/ty->type->size);
00581 }
00582 } else
00583 fprint(f, "incomplete array");
00584 if (ty->type)
00585 fprint(f, " of %t", ty->type);
00586 break;
00587 default: assert(0);
00588 }
00589 }
00590
00591
00592 void printdecl(Symbol p, Type ty) {
00593 switch (p->sclass) {
00594 case AUTO:
00595 fprint(stderr, "%s;\n", typestring(ty, p->name));
00596 break;
00597 case STATIC: case EXTERN:
00598 fprint(stderr, "%k %s;\n", p->sclass, typestring(ty, p->name));
00599 break;
00600 case TYPEDEF: case ENUM:
00601 break;
00602 default: assert(0);
00603 }
00604 }
00605
00606
00607 void printproto(Symbol p, Symbol callee[]) {
00608 if (p->type->u.f.proto)
00609 printdecl(p, p->type);
00610 else {
00611 int i;
00612 List list = 0;
00613 if (callee[0] == 0)
00614 list = append(voidtype, list);
00615 else
00616 for (i = 0; callee[i]; i++)
00617 list = append(callee[i]->type, list);
00618 printdecl(p, func(freturn(p->type), ltov(&list, PERM), 0));
00619 }
00620 }
00621
00622
00623 static void prtype(Type ty, FILE *f, int indent, unsigned mark) {
00624 switch (ty->op) {
00625 default:
00626 fprint(f, "(%d %d %d [%p])", ty->op, ty->size, ty->align, ty->u.sym);
00627 break;
00628 case FLOAT: case INT: case UNSIGNED: case VOID:
00629 fprint(f, "(%k %d %d [\"%s\"])", ty->op, ty->size, ty->align, ty->u.sym->name);
00630 break;
00631 case CONST+VOLATILE: case CONST: case VOLATILE: case POINTER: case ARRAY:
00632 fprint(f, "(%k %d %d ", ty->op, ty->size, ty->align);
00633 prtype(ty->type, f, indent+1, mark);
00634 fprint(f, ")");
00635 break;
00636 case STRUCT: case UNION:
00637 fprint(f, "(%k %d %d [\"%s\"]", ty->op, ty->size, ty->align, ty->u.sym->name);
00638 if (ty->x.marked != mark) {
00639 Field p;
00640 ty->x.marked = mark;
00641 for (p = ty->u.sym->u.s.flist; p; p = p->link) {
00642 fprint(f, "\n%I", indent+1);
00643 prtype(p->type, f, indent+1, mark);
00644 fprint(f, " %s@%d", p->name, p->offset);
00645 if (p->lsb)
00646 fprint(f, ":%d..%d",
00647 fieldsize(p) + fieldright(p), fieldright(p));
00648 }
00649 fprint(f, "\n%I", indent);
00650 }
00651 fprint(f, ")");
00652 break;
00653 case ENUM:
00654 fprint(f, "(%k %d %d [\"%s\"]", ty->op, ty->size, ty->align, ty->u.sym->name);
00655 if (ty->x.marked != mark) {
00656 int i;
00657 Symbol *p = ty->u.sym->u.idlist;
00658 ty->x.marked = mark;
00659 for (i = 0; p[i] != NULL; i++)
00660 fprint(f, "%I%s=%d\n", indent+1, p[i]->name, p[i]->u.value);
00661 }
00662 fprint(f, ")");
00663 break;
00664 case FUNCTION:
00665 fprint(f, "(%k %d %d ", ty->op, ty->size, ty->align);
00666 prtype(ty->type, f, indent+1, mark);
00667 if (ty->u.f.proto) {
00668 int i;
00669 fprint(f, "\n%I{", indent+1);
00670 for (i = 0; ty->u.f.proto[i]; i++) {
00671 if (i > 0)
00672 fprint(f, "%I", indent+2);
00673 prtype(ty->u.f.proto[i], f, indent+2, mark);
00674 fprint(f, "\n");
00675 }
00676 fprint(f, "%I}", indent+1);
00677 }
00678 fprint(f, ")");
00679 break;
00680 }
00681 }
00682
00683
00684 void printtype(Type ty, int fd) {
00685 static unsigned mark;
00686 prtype(ty, fd == 1 ? stdout : stderr, 0, ++mark);
00687 fprint(fd == 1 ? stdout : stderr, "\n");
00688 }
00689
00690
00691 char *typestring(Type ty, char *str) {
00692 for ( ; ty; ty = ty->type) {
00693 Symbol p;
00694 switch (ty->op) {
00695 case CONST+VOLATILE: case CONST: case VOLATILE:
00696 if (isptr(ty->type))
00697 str = stringf("%k %s", ty->op, str);
00698 else
00699 return stringf("%k %s", ty->op, typestring(ty->type, str));
00700 break;
00701 case STRUCT: case UNION: case ENUM:
00702 assert(ty->u.sym);
00703 if ((p = findtype(ty)) != NULL)
00704 return *str ? stringf("%s %s", p->name, str) : p->name;
00705 if (*ty->u.sym->name >= '1' && *ty->u.sym->name <= '9')
00706 warning("unnamed %k in prototype\n", ty->op);
00707 if (*str)
00708 return stringf("%k %s %s", ty->op, ty->u.sym->name, str);
00709 else
00710 return stringf("%k %s", ty->op, ty->u.sym->name);
00711 case VOID: case FLOAT: case INT: case UNSIGNED:
00712 return *str ? stringf("%s %s", ty->u.sym->name, str) : ty->u.sym->name;
00713 case POINTER:
00714 if (!ischar(ty->type) && (p = findtype(ty)) != NULL)
00715 return *str ? stringf("%s %s", p->name, str) : p->name;
00716 str = stringf(isarray(ty->type) || isfunc(ty->type) ? "(*%s)" : "*%s", str);
00717 break;
00718 case FUNCTION:
00719 if ((p = findtype(ty)) != NULL)
00720 return *str ? stringf("%s %s", p->name, str) : p->name;
00721 if (ty->u.f.proto == 0)
00722 str = stringf("%s()", str);
00723 else if (ty->u.f.proto[0]) {
00724 int i;
00725 str = stringf("%s(%s", str, typestring(ty->u.f.proto[0], ""));
00726 for (i = 1; ty->u.f.proto[i]; i++)
00727 if (ty->u.f.proto[i] == voidtype)
00728 str = stringf("%s, ...", str);
00729 else
00730 str = stringf("%s, %s", str, typestring(ty->u.f.proto[i], ""));
00731 str = stringf("%s)", str);
00732 } else
00733 str = stringf("%s(void)", str);
00734 break;
00735 case ARRAY:
00736 if ((p = findtype(ty)) != NULL)
00737 return *str ? stringf("%s %s", p->name, str) : p->name;
00738 if (ty->type && ty->type->size > 0)
00739 str = stringf("%s[%d]", str, ty->size/ty->type->size);
00740 else
00741 str = stringf("%s[]", str);
00742 break;
00743 default: assert(0);
00744 }
00745 }
00746 assert(0); return 0;
00747 }
00748