Annotation of researchv10no/cmd/cfront/ptcfront/typ2.c, revision 1.1

1.1     ! root        1: /*ident        "@(#)ctrans:src/typ2.c  1.7" */
        !             2: /**************************************************************************
        !             3: 
        !             4:        C++ source for cfront, the C++ compiler front-end
        !             5:        written in the computer science research center of Bell Labs
        !             6: 
        !             7:        Copyright (c) 1984 AT&T, Inc. All Rights Reserved
        !             8:        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
        !             9: 
        !            10: typ2.c:
        !            11: 
        !            12: ***************************************************************************/
        !            13:  
        !            14: #include "cfront.h"
        !            15: #include "size.h"
        !            16: 
        !            17: extern int chars_in_largest;
        !            18: int largest_int;
        !            19: 
        !            20: void typ_init()
        !            21: {      
        !            22:        chars_in_largest = strlen(LARGEST_INT);
        !            23:        largest_int = int(str_to_long(LARGEST_INT));
        !            24: 
        !            25:        defa_type = new basetype(INT,0);        // note defa_type!=int_type
        !            26:        int_type = new basetype(INT,0);         // but they both represent `int'
        !            27: 
        !            28:        PERM(int_type); int_type->defined = DEFINED ;
        !            29:        PERM(defa_type); defa_type->defined = DEFINED ;
        !            30: 
        !            31:        moe_type = new basetype(INT,0);
        !            32:        PERM(moe_type); moe_type->defined = DEFINED ;
        !            33:        moe_type->b_const = 1;
        !            34:        moe_type->check(0);
        !            35: 
        !            36:        uint_type = new basetype(INT,0);
        !            37:        PERM(uint_type); uint_type->defined = DEFINED ;
        !            38:        uint_type->type_adj(UNSIGNED);
        !            39:        uint_type->check(0);
        !            40: 
        !            41:        long_type = new basetype(LONG,0);
        !            42:        PERM(long_type); long_type->defined = DEFINED ;
        !            43:        long_type->check(0);
        !            44: 
        !            45:        ulong_type = new basetype(LONG,0);
        !            46:        PERM(ulong_type); ulong_type->defined = DEFINED ;
        !            47:        ulong_type->type_adj(UNSIGNED);
        !            48:        ulong_type->check(0);
        !            49: 
        !            50:        short_type = new basetype(SHORT,0);
        !            51:        PERM(short_type); short_type->defined = DEFINED ;
        !            52:        short_type->check(0);
        !            53: 
        !            54:        ushort_type = new basetype(SHORT,0);
        !            55:        PERM(ushort_type); ushort_type->defined = DEFINED ;
        !            56:        ushort_type->type_adj(UNSIGNED);
        !            57:        ushort_type->check(0);
        !            58: 
        !            59:        float_type = new basetype(FLOAT,0);
        !            60:        PERM(float_type); float_type->defined = DEFINED ;
        !            61: 
        !            62:        double_type = new basetype(DOUBLE,0);
        !            63:        PERM(double_type); double_type->defined = DEFINED ;
        !            64: 
        !            65:        ldouble_type = new basetype(LDOUBLE,0);
        !            66:        PERM(ldouble_type); ldouble_type->defined = DEFINED ;
        !            67: 
        !            68:        zero_type = new basetype(ZTYPE,0);
        !            69:        PERM(zero_type); zero_type->defined = DEFINED ;
        !            70:        zero->tp = zero_type;
        !            71: 
        !            72:        void_type = new basetype(VOID,0);
        !            73:        PERM(void_type); void_type->defined = DEFINED ;
        !            74: 
        !            75:        char_type = new basetype(CHAR,0);
        !            76:        PERM(char_type); char_type->defined = DEFINED ;
        !            77: 
        !            78:        uchar_type = new basetype(CHAR,0);
        !            79:        PERM(uchar_type); uchar_type->defined = DEFINED ;
        !            80:        uchar_type->type_adj(UNSIGNED);
        !            81:        uchar_type->check(0);
        !            82: 
        !            83:        Pchar_type = char_type->addrof();
        !            84:        PERM(Pchar_type); Pchar_type->defined = DEFINED ;
        !            85: 
        !            86:        Pint_type = int_type->addrof();
        !            87:        PERM(Pint_type); Pint_type->defined = DEFINED ;
        !            88: 
        !            89:        Pvoid_type = void_type->addrof();
        !            90:        PERM(Pvoid_type); Pvoid_type->defined = DEFINED ;
        !            91: 
        !            92:        Pfctvec_type = new fct(int_type,0,0);   // must be last, see basetype::normalize()
        !            93:        Pfctvec_type = Pfctvec_type->addrof();
        !            94:        Pfctvec_type = Pfctvec_type->addrof();
        !            95:        PERM(Pfctvec_type); Pfctvec_type->defined = DEFINED ;
        !            96: 
        !            97:        gtbl = new table(GTBLSIZE,0,0);
        !            98:        gtbl->t_name = new name("global");
        !            99:        ptbl = new table(CTBLSIZE,0,0);
        !           100:        ptbl->t_name = new name("ptbl");
        !           101: }
        !           102: 
        !           103: bit enum_promote;
        !           104: 
        !           105: Pbase basetype::arit_conv(Pbase t)
        !           106: /*
        !           107:        perform the "usual arithmetic conversions" C ref Manual 6.6
        !           108:        on "this" op "t"
        !           109:        "this" and "t" are integral or floating
        !           110:        "t" may be 0
        !           111: */
        !           112: {
        !           113:        this = (Pbase)skiptypedefs();
        !           114: // error('d', "arit_conv: this: %k %d %t %d", base, base, this, this );
        !           115: 
        !           116:        bit l;
        !           117:        bit u;
        !           118:        bit f;
        !           119:        bit l1 = (base == LONG);
        !           120:        bit u1 = b_unsigned;
        !           121:        bit f1 = (base==FLOAT || base==DOUBLE || base==LDOUBLE);
        !           122:        if (t) {
        !           123:                t = (Pbase)t->skiptypedefs();
        !           124: // error('d', "arit_conv: t: %k %d %t %d", t->base, t->base, t, t );
        !           125: 
        !           126:                bit l2 = (t->base == LONG);
        !           127:                bit u2 = t->b_unsigned;
        !           128:                bit f2 = (t->base==FLOAT || t->base==DOUBLE || base==LDOUBLE);
        !           129:                l = l1 || l2;
        !           130:                u = u1 || u2;
        !           131:                f = f1 || f2;
        !           132:        }
        !           133:        else {
        !           134:                l = l1;
        !           135:                u = u1;
        !           136:                f = f1;
        !           137:        }
        !           138: 
        !           139:        if (f) {
        !           140:                if (base==LDOUBLE || (t && t->base==LDOUBLE)) return ldouble_type;
        !           141:                if (base==DOUBLE || (t && t->base==DOUBLE)) return double_type;
        !           142:                return float_type;
        !           143:        }
        !           144:        if (l & u) return ulong_type;
        !           145:        if (l & !u) return long_type;
        !           146:        if (u) {
        !           147:                if (base==INT || (t && t->base==INT)) return uint_type;
        !           148:                if (SZ_SHORT==SZ_INT) // ANSIism
        !           149:                        if (base==SHORT || (t && t->base==SHORT)) return uint_type;
        !           150:                return int_type;
        !           151:        }
        !           152: 
        !           153:        if (t && t->base == EOBJ && base == EOBJ) enum_promote = 1;
        !           154:        return int_type;
        !           155: }
        !           156: 
        !           157: bit vec_const = 0;
        !           158: bit fct_const = 0;
        !           159: 
        !           160: bit type::tconst()
        !           161: /*
        !           162:        is this type a constant
        !           163: */
        !           164: {
        !           165:        Ptype t = this;
        !           166:        vec_const = 0;
        !           167:        fct_const = 0;
        !           168: //error('d',"tconst %t",t);
        !           169: xxx:
        !           170:        switch (t->base) {
        !           171:        case TYPE:
        !           172:                if (Pbase(t)->b_const) return 1;
        !           173:                t = Pbase(t)->b_name->tp;
        !           174:                goto xxx;
        !           175:        case VEC:
        !           176:                vec_const = 1;
        !           177:                return 1;
        !           178:        case PTR:
        !           179:        case RPTR:
        !           180:                return Pptr(t)->b_const;
        !           181:        case FCT:
        !           182:        case OVERLOAD:
        !           183:                fct_const = 1;
        !           184:                return 1;
        !           185:        default:
        !           186:                return Pbase(t)->b_const;
        !           187:        }
        !           188: }
        !           189: 
        !           190: TOK type::set_const(bit mode)
        !           191: /*
        !           192:        make someting a constant or variable, return old status
        !           193: */
        !           194: {
        !           195:        Ptype t = this;
        !           196:        int m;
        !           197: xxx:
        !           198:        switch (t->base) {
        !           199:        case TYPE:
        !           200:                m = Pbase(t)->b_const;
        !           201:                Pbase(t)->b_const = mode;
        !           202:                t = Pbase(t)->b_name->tp;
        !           203:                goto xxx;
        !           204:        case ANY:
        !           205:        case RPTR:
        !           206:        case VEC:
        !           207:                return t->base;         // constant by definition
        !           208:        case PTR:
        !           209:                m = Pptr(t)->b_const;
        !           210:                Pptr(t)->b_const = mode;
        !           211:                return m;
        !           212:        default:
        !           213:                m = Pbase(t)->b_const;
        !           214:                Pbase(t)->b_const = mode;
        !           215:                return m;
        !           216:        }
        !           217: }
        !           218: 
        !           219: Pptr type::is_ref()
        !           220: {
        !           221:        Ptype t = skiptypedefs();
        !           222: 
        !           223:        switch (t->base) {
        !           224:        case RPTR:      return Pptr(t);
        !           225:        default:        return 0;
        !           226:        }
        !           227: }
        !           228: 
        !           229: Pclass Mptr;
        !           230: 
        !           231: Pptr type::is_ptr()
        !           232: {
        !           233:        Ptype t = skiptypedefs();
        !           234: 
        !           235:        switch (t->base) {
        !           236:        case PTR:
        !           237:        case VEC:       Mptr = Pptr(t)->memof;
        !           238:                        return Pptr(t);
        !           239:        default:        return 0;
        !           240:        }
        !           241: }
        !           242: 
        !           243: Pptr type::is_ptr_or_ref()
        !           244: {
        !           245:        Ptype t = skiptypedefs();
        !           246: 
        !           247:        switch (t->base) {
        !           248:        case PTR:
        !           249:        case RPTR:
        !           250:        case VEC:       Mptr = Pptr(t)->memof;
        !           251:                        return Pptr(t);
        !           252:        default:        return 0;
        !           253:        }
        !           254: }
        !           255: 
        !           256: int type::align()
        !           257: {
        !           258:        Ptype t = skiptypedefs();
        !           259: 
        !           260: /*fprintf(stderr,"align %d %d\n",t,t->base);*/
        !           261:        switch (t->base) {
        !           262:        case COBJ:      return t->bname_type()->align();
        !           263:        case VEC:       return Pvec(t)->typ->align();
        !           264:        case ANY:       return 1;
        !           265:        case CHAR:      return AL_CHAR;
        !           266:        case SHORT:     return AL_SHORT;
        !           267:        case INT:       return AL_INT;
        !           268:        case LONG:      return AL_LONG;
        !           269:        case FLOAT:     return AL_FLOAT;
        !           270:        case DOUBLE:    return AL_DOUBLE;
        !           271:        case LDOUBLE:   return AL_LDOUBLE;
        !           272:        case PTR:
        !           273:        case RPTR:      return AL_WPTR;
        !           274:        case CLASS:     return Pclass(t)->obj_align;
        !           275:        case ENUM:
        !           276:        case EOBJ:      return AL_INT;
        !           277:        case VOID:      error("illegal use of void"); return AL_INT;
        !           278:        default:        error('i',"(%d,%k)->type::align",t,t->base);
        !           279:        }
        !           280: }
        !           281: 
        !           282: bit fake_sizeof;
        !           283: 
        !           284: int type::tsizeof(int ptmc)
        !           285: /*
        !           286:        the sizeof type operator
        !           287:        return the size in bytes of the types representation
        !           288: */
        !           289: {
        !           290:        Ptype t = skiptypedefs();
        !           291: 
        !           292: //error('d',"zx %t %d",t,t->base);
        !           293:        if (t == 0) error('i',"typ.tsizeof(t==0)");
        !           294:        switch (t->base) {
        !           295:        case COBJ:
        !           296:                t = t->bname_type();
        !           297:                if (t == 0) return 0;   // ``fake'' generated classes: _Sdd
        !           298:                return t->tsizeof(ptmc);
        !           299: 
        !           300:        case ANY:       return 1;
        !           301:        case VOID:      return 0;
        !           302:        case ZTYPE:     return SZ_WPTR; /* assume pointer */
        !           303:        case CHAR:      return SZ_CHAR;
        !           304:        case SHORT:     return SZ_SHORT;
        !           305:        case INT:       return SZ_INT;
        !           306:        case LONG:      return SZ_LONG;
        !           307:        case FLOAT:     return SZ_FLOAT;
        !           308:        case DOUBLE:    return SZ_DOUBLE;
        !           309:        case LDOUBLE:   return SZ_LDOUBLE;
        !           310: 
        !           311:        case VEC:
        !           312:                {       Pvec v = Pvec(t);
        !           313:                        if (v->size == 0) {
        !           314:                                if (fake_sizeof == 0) error('w',"sizeof array with undeclared dimension");
        !           315:                                return SZ_WPTR; // vector argument has sizeof ptr
        !           316:                        }
        !           317:                        return v->size * v->typ->tsizeof();
        !           318:                }
        !           319:        case RPTR:      
        !           320:        case PTR:
        !           321:        {       
        !           322:                int k = (Pptr(t)->memof && !ptmc)?sizeof(short)+sizeof(short):0;
        !           323:                t = Pptr(t)->typ->skiptypedefs();
        !           324: 
        !           325:                switch (t->base) {
        !           326:                default:        return SZ_WPTR;
        !           327:                case CHAR:      return SZ_BPTR;
        !           328:                case FCT:       return SZ_WPTR+k;
        !           329:                }
        !           330:        }
        !           331:        case FIELD:
        !           332:                error("sizeof(field)");
        !           333:                return Pbase(t)->b_bits/BI_IN_BYTE+1;
        !           334:        case FCT:
        !           335:                error("sizeof(function)");
        !           336:                return 0;
        !           337: 
        !           338:        case CLASS:     
        !           339:        {
        !           340:                 Pclass cl = Pclass(t);
        !           341:                if ((cl->defined&(DEFINED|SIMPLIFIED)) == 0) {
        !           342:                        error("%tU, size not known",cl);
        !           343:                        return SZ_INT;
        !           344:                }
        !           345:                if (cl->c_body == 1)    // detect first allocation or sizeof
        !           346:                        cl->dcl_print(0);
        !           347:                return cl->obj_size;
        !           348:        }
        !           349: 
        !           350:        case EOBJ:
        !           351:        case ENUM:      return SZ_INT;
        !           352: 
        !           353:        default:        return 0;       // deref can be called for any type
        !           354:                        //error('i',"sizeof(%d)",t->base);
        !           355:        }
        !           356: }
        !           357: 
        !           358: bit type::vec_type()
        !           359: {
        !           360:        Ptype t = skiptypedefs();
        !           361: 
        !           362:        switch (t->base) {
        !           363:        case ANY:
        !           364:        case VEC:
        !           365:        case PTR:
        !           366:        case RPTR:      return 1;
        !           367:        default:        return 0;
        !           368:        }
        !           369: }
        !           370: 
        !           371: int ref_initializer;
        !           372: 
        !           373: Ptype type::deref()
        !           374: /*     index==1:       *p
        !           375:        index==0:       p[expr]
        !           376: */
        !           377: {
        !           378: //error('d',"%t -> deref() refd %d",this,ref_initializer);
        !           379:        Ptype t = skiptypedefs();
        !           380: 
        !           381:        switch (t->base) {
        !           382:        case PTR:
        !           383:        case RPTR:
        !           384:        case VEC:
        !           385:        {
        !           386:                t = Pvec(t)->typ;
        !           387:                Ptype tt = t->skiptypedefs();
        !           388:                if (tt == Pvoid_type) error("void* dereferenced");
        !           389:                if (ref_initializer == 0) {
        !           390:                        if (tt->base == COBJ) { 
        !           391:                                tt = tt->bname_type();
        !           392:                                if (tt && Pclass(tt)->defined&(DEFINED|SIMPLIFIED))
        !           393:                                        (void) t->tsizeof();
        !           394:                         }
        !           395:                }
        !           396:                // no break
        !           397:        }
        !           398:        case ANY:
        !           399:                return t;
        !           400:        default:
        !           401:                error("nonP dereferenced");
        !           402:                return any_type;
        !           403:        }
        !           404: }
        !           405: 
        !           406: Pfct type::memptr()
        !           407: // is ``this'' a pointer to member function
        !           408: {
        !           409:        Ptype t = skiptypedefs();
        !           410:        if (t->base != PTR || Pptr(t)->memof==0) return 0;
        !           411: 
        !           412:        t = Pptr(t)->typ->skiptypedefs();
        !           413:        return (t->base == FCT) ? Pfct(t) : 0;
        !           414: }
        !           415: 
        !           416: Ptype type::skiptypedefs()
        !           417: // starting from ``this'', return first non-typedef
        !           418: {
        !           419:        register Ptype t = this;
        !           420:        if(t==0) return t;
        !           421:        while (t->base == TYPE) {
        !           422:                t = t->bname_type();
        !           423:        }
        !           424:        return t;
        !           425: }
        !           426: 
        !           427: Ptype type::skiptypedefs(bit &isconst)
        !           428: // starting from ``this'', return first non-typedef
        !           429: {
        !           430:        register Ptype t = this;
        !           431:        if(t==0) return t;
        !           432:        while (t->base == TYPE) {
        !           433:                t = t->bname_type();
        !           434:                isconst |= Pbase(t)->b_const;
        !           435:        }
        !           436:        return t;
        !           437: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.