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

1.1     ! root        1: /*ident        "@(#)ctrans:src/typ2.c  1.3" */
        !             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:        while (base == TYPE) this = Pbase(Pbase(this)->b_name->tp);
        !           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:                while (t->base == TYPE) t = Pbase(Pbase(t)->b_name->tp);
        !           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)->rdo;
        !           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)->rdo;
        !           210:                Pptr(t)->rdo = 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 = this;
        !           222: xxx:
        !           223:        switch (t->base) {
        !           224:        case TYPE:      t = Pbase(t)->b_name->tp; goto xxx;
        !           225:        case RPTR:      return Pptr(t);
        !           226:        default:        return 0;
        !           227:        }
        !           228: }
        !           229: 
        !           230: Pclass Mptr;
        !           231: 
        !           232: Pptr type::is_ptr()
        !           233: {
        !           234:        Ptype t = this;
        !           235: xxx:
        !           236:        switch (t->base) {
        !           237:        case TYPE:      t = Pbase(t)->b_name->tp; goto xxx;
        !           238:        case PTR:
        !           239:        case VEC:       Mptr = Pptr(t)->memof;
        !           240:                        return Pptr(t);
        !           241:        default:        return 0;
        !           242:        }
        !           243: }
        !           244: 
        !           245: Pptr type::is_ptr_or_ref()
        !           246: {
        !           247:        Ptype t = this;
        !           248: xxx:
        !           249:        switch (t->base) {
        !           250:        case TYPE:      t = Pbase(t)->b_name->tp; goto xxx;
        !           251:        case PTR:
        !           252:        case RPTR:
        !           253:        case VEC:       Mptr = Pptr(t)->memof;
        !           254:                        return Pptr(t);
        !           255:        default:        return 0;
        !           256:        }
        !           257: }
        !           258: 
        !           259: int type::align()
        !           260: {
        !           261:        Ptype t = this;
        !           262: xx:
        !           263: /*fprintf(stderr,"align %d %d\n",t,t->base);*/
        !           264:        switch (t->base) {
        !           265:        case TYPE:      t = Pbase(t)->b_name->tp; goto xx;
        !           266:        case COBJ:      t = Pbase(t)->b_name->tp; goto xx;
        !           267:        case VEC:       t = Pvec(t)->typ; goto xx;
        !           268:        case ANY:       return 1;
        !           269:        case CHAR:      return AL_CHAR;
        !           270:        case SHORT:     return AL_SHORT;
        !           271:        case INT:       return AL_INT;
        !           272:        case LONG:      return AL_LONG;
        !           273:        case FLOAT:     return AL_FLOAT;
        !           274:        case DOUBLE:    return AL_DOUBLE;
        !           275:        case LDOUBLE:   return AL_LDOUBLE;
        !           276:        case PTR:
        !           277:        case RPTR:      return AL_WPTR;
        !           278:        case CLASS:     return Pclass(t)->obj_align;
        !           279:        case ENUM:
        !           280:        case EOBJ:      return AL_INT;
        !           281:        case VOID:      error("illegal use of void"); return AL_INT;
        !           282:        default:        error('i',"(%d,%k)->type::align",t,t->base);
        !           283:        }
        !           284: }
        !           285: 
        !           286: bit fake_sizeof;
        !           287: 
        !           288: int type::tsizeof(int ptmc)
        !           289: /*
        !           290:        the sizeof type operator
        !           291:        return the size in bytes of the types representation
        !           292: */
        !           293: {
        !           294:        Ptype t = this;
        !           295: zx:
        !           296: //error('d',"zx %t %d",t,t->base);
        !           297:        if (t == 0) error('i',"typ.tsizeof(t==0)");
        !           298:        switch (t->base) {
        !           299:        case TYPE:
        !           300:                t = Pbase(t)->b_name->tp;
        !           301:                goto zx;
        !           302:        case COBJ:
        !           303:                t = Pbase(t)->b_name->tp;
        !           304:                if (t == 0) return 0;   // ``fake'' generated classes: _Sdd
        !           305:                goto zx;
        !           306: 
        !           307:        case ANY:       return 1;
        !           308:        case VOID:      return 0;
        !           309:        case ZTYPE:     return SZ_WPTR; /* assume pointer */
        !           310:        case CHAR:      return SZ_CHAR;
        !           311:        case SHORT:     return SZ_SHORT;
        !           312:        case INT:       return SZ_INT;
        !           313:        case LONG:      return SZ_LONG;
        !           314:        case FLOAT:     return SZ_FLOAT;
        !           315:        case DOUBLE:    return SZ_DOUBLE;
        !           316:        case LDOUBLE:   return SZ_LDOUBLE;
        !           317: 
        !           318:        case VEC:
        !           319:                {       Pvec v = Pvec(t);
        !           320:                        if (v->size == 0) {
        !           321:                                if (fake_sizeof == 0) error('w',"sizeof array with undeclared dimension");
        !           322:                                return SZ_WPTR; // vector argument has sizeof ptr
        !           323:                        }
        !           324:                        return v->size * v->typ->tsizeof();
        !           325:                }
        !           326:        case RPTR:      
        !           327:        case PTR:
        !           328:        {       
        !           329:                int k = (Pptr(t)->memof && !ptmc)?sizeof(short)+sizeof(short):0;
        !           330:                t = Pptr(t)->typ;
        !           331:        xxx:
        !           332:                switch (t->base) {
        !           333:                default:        return SZ_WPTR;
        !           334:                case CHAR:      return SZ_BPTR;
        !           335:                case FCT:       return SZ_WPTR+k;
        !           336:                case TYPE:      t = Pbase(t)->b_name->tp; goto xxx;
        !           337:                }
        !           338:        }
        !           339:        case FIELD:
        !           340:                error("sizeof(field)");
        !           341:                return Pbase(t)->b_bits/BI_IN_BYTE+1;
        !           342:        case FCT:
        !           343:                error("sizeof(function)");
        !           344:                return 0;
        !           345: 
        !           346:        case CLASS:     
        !           347:        {
        !           348:                 Pclass cl = Pclass(t);
        !           349:                if ((cl->defined&(DEFINED|SIMPLIFIED)) == 0) {
        !           350:                        error("%tU, size not known",cl);
        !           351:                        return SZ_INT;
        !           352:                }
        !           353:                if (cl->c_body == 1)    // detect first allocation or sizeof
        !           354:                        cl->dcl_print(0);
        !           355:                return cl->obj_size;
        !           356:        }
        !           357: 
        !           358:        case EOBJ:
        !           359:        case ENUM:      return SZ_INT;
        !           360: 
        !           361:        default:        return 0;       // deref can be called for any type
        !           362:                        //error('i',"sizeof(%d)",t->base);
        !           363:        }
        !           364: }
        !           365: 
        !           366: bit type::vec_type()
        !           367: {
        !           368:        Ptype t = this;
        !           369: xx:
        !           370:        switch (t->base) {
        !           371:        case ANY:
        !           372:        case VEC:
        !           373:        case PTR:
        !           374:        case RPTR:      return 1;
        !           375:        case TYPE:      t = Pbase(t)->b_name->tp; goto xx;
        !           376:        default:        return 0;
        !           377:        }
        !           378: }
        !           379: 
        !           380: int ref_initializer;
        !           381: 
        !           382: Ptype type::deref()
        !           383: /*     index==1:       *p
        !           384:        index==0:       p[expr]
        !           385: */
        !           386: {
        !           387: //error('d',"%t -> deref() refd %d",this,ref_initializer);
        !           388:        Ptype t = this;
        !           389: xx:
        !           390:        switch (t->base) {
        !           391:        case TYPE:
        !           392:                t = Pbase(t)->b_name->tp;
        !           393:                goto xx;
        !           394:        case PTR:
        !           395:        case RPTR:
        !           396:        case VEC:
        !           397:        {       if (t == Pvoid_type) error("void* dereferenced");
        !           398:                Ptype tt = t = Pvec(t)->typ;
        !           399:                if (ref_initializer == 0) {
        !           400:                        while (tt->base == TYPE) tt = Pbase(tt)->b_name->tp;
        !           401:                        if (tt->base == COBJ) { 
        !           402:                                tt = Pbase(tt)->b_name->tp;
        !           403:                                if (tt && Pclass(tt)->defined&(DEFINED|SIMPLIFIED))
        !           404:                                        (void) t->tsizeof();
        !           405:                         }
        !           406:                }
        !           407:                // no break
        !           408:        }
        !           409:        case ANY:
        !           410:                return t;
        !           411:        default:
        !           412:                error("nonP dereferenced");
        !           413:                return any_type;
        !           414:        }
        !           415: }
        !           416: 
        !           417: Pfct type::memptr()
        !           418: // is ``this'' a pointer to member function
        !           419: {
        !           420:        Ptype t = this;
        !           421:        while (t->base == TYPE) t = Pbase(t)->b_name->tp;
        !           422:        if (t->base != PTR || Pptr(t)->memof==0) return 0;
        !           423: 
        !           424:        t = Pptr(t)->typ;
        !           425:        while (t->base == TYPE) t = Pbase(t)->b_name->tp;
        !           426:        return (t->base == FCT) ? Pfct(t) : 0;
        !           427: }

unix.superglobalmegacorp.com

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