TYPEPTR EnumDecl( int flags ) { TYPEPTR typ; TAGPTR tag; flags = flags; NextToken(); if( CurToken == T_ID ) { /* could be: (1) "enum" <id> ";" (2) "enum" <id> <variable_name> ";" (3) "enum" <id> "{" <enum_const_decl> ... "}" */ tag = TagLookup(); NextToken(); if( CurToken != T_LEFT_BRACE ) { typ = tag->sym_type; if( typ == NULL ) { CErr1( ERR_INCOMPLETE_ENUM_DECL ); typ = TypeDefault(); } else { if( typ->decl_type != TYPE_ENUM ) { /* 18-jan-89 */ CErr2p( ERR_DUPLICATE_TAG, tag->name ); } typ->u.tag = tag; } return( typ ); } tag = VfyNewTag( tag, TYPE_ENUM ); } else { tag = NullTag(); } typ = TypeNode( TYPE_ENUM, GetType( TYPE_INT ) ); typ->u.tag = tag; tag->sym_type = typ; tag->size = TARGET_INT; tag->u.enum_list = NULL; if( CurToken == T_LEFT_BRACE ) { const_val val; enum enum_rng index; enum enum_rng const_index; enum enum_rng start_index; enum enum_rng step; enum enum_rng error; uint64 n; uint64 Inc; bool minus; bool has_sign; ENUMPTR *prev_lnk; ENUMPTR esym; source_loc error_loc; char buff[50]; if( CompFlags.make_enums_an_int ) { start_index = ENUM_INT; } else { start_index = ENUM_S8; } const_index = ENUM_UNDEF; NextToken(); if( CurToken == T_RIGHT_BRACE ) { CErr1( ERR_EMPTY_ENUM_LIST ); } U32ToU64( 1, &Inc ); U64Clear( n ); minus = FALSE; has_sign = FALSE; step = 1; prev_lnk = &esym; esym = NULL; while( CurToken == T_ID ) { esym = EnumLkAdd( tag ); *prev_lnk = esym; prev_lnk = &esym->thread; error_loc = TokenLoc; NextToken(); if( CurToken == T_EQUAL ) { NextToken(); error_loc = TokenLoc; ConstExprAndType( &val ); switch( val.type ){ case TYPE_ULONG: case TYPE_UINT: case TYPE_ULONG64: minus = FALSE; break; default: if( val.value.u.sign.v ) { minus = TRUE; step = 2; } else { minus = FALSE; } break; } n = val.value; } else if( has_sign ) { if( n.u.sign.v ) { minus = TRUE; } else { minus = FALSE; } } for( index = start_index; index < ENUM_SIZE; index += step ) { if( minus ) { if( I64Cmp( &n, &( RangeTable[ index ][LOW] ) ) >= 0 ) break; } else { if( U64Cmp( &n, &( RangeTable[ index ][HIGH]) ) <= 0 ) break; } } error = ENUM_UNDEF; if( !CompFlags.extensions_enabled && ( index > ENUM_INT )) { error = ENUM_INT; } if( index >= ENUM_SIZE ) { // overflow signed maximum range if( error == ENUM_UNDEF ) { error = const_index; } } else if(( const_index == ENUM_SIZE - 1 ) && minus ) { // overflow unsigned maximum range by any negative signed value if( error == ENUM_UNDEF ) error = const_index; step = 1; } else { if( !has_sign && minus) { has_sign = TRUE; if( index < const_index ) { // round up to signed index = ( const_index + 1 ) & ~1; } } if( index > const_index ) { const_index = index; typ->object = GetType( ItypeTable[const_index].decl_type ); tag->size = ItypeTable[const_index].size; } } if( error != ENUM_UNDEF ) { SetErrLoc( &error_loc ); get_msg_range( buff, error ); CErr2p( ERR_ENUM_CONSTANT_OUT_OF_RANGE, buff ); InitErrLoc(); } esym->value = n; EnumTable[ esym->hash ] = esym; /* 08-nov-94 */ if( CurToken == T_RIGHT_BRACE ) break; U64Add( &n, &Inc, &n ); MustRecog( T_COMMA ); if( !CompFlags.extensions_enabled && !CompFlags.c99_extensions && ( CurToken == T_RIGHT_BRACE )) { ExpectIdentifier(); /* 13-may-91 */ } } MustRecog( T_RIGHT_BRACE ); } return( typ ); }
static PTREE foldInt64( CGOP op, PTREE left, signed_64 v2 ) { signed_64 test; signed_64 v1; float_handle t0, t1, t2; v1 = left->u.int64_constant; switch( op ) { case CO_PLUS: U64Add( &v1, &v2, &left->u.int64_constant ); break; case CO_MINUS: U64Sub( &v1, &v2, &left->u.int64_constant ); break; case CO_TIMES: t0 = BFCnvI64F( v1 ); t1 = BFCnvI64F( v2 ); t2 = BFMul( t0, t1 ); test = BFCnvF64( t2 ); BFFree( t0 ); BFFree( t1 ); BFFree( t2 ); U64Mul( &v1, &v2, &left->u.int64_constant ); if( 0 != I64Cmp( &test, &left->u.int64_constant ) ) { CErr1( ANSI_ARITHMETIC_OVERFLOW ); } break; case CO_DIVIDE: { signed_64 rem; idiv64( &v1, &v2, &left->u.int64_constant, &rem ); } break; case CO_PERCENT: { signed_64 div; idiv64( &v1, &v2, &div, &left->u.int64_constant ); } break; case CO_AND: left->u.int64_constant.u._32[0] = v1.u._32[0] & v2.u._32[0]; left->u.int64_constant.u._32[1] = v1.u._32[1] & v2.u._32[1]; break; case CO_OR: left->u.int64_constant.u._32[0] = v1.u._32[0] | v2.u._32[0]; left->u.int64_constant.u._32[1] = v1.u._32[1] | v2.u._32[1]; break; case CO_XOR: left->u.int64_constant.u._32[0] = v1.u._32[0] ^ v2.u._32[0]; left->u.int64_constant.u._32[1] = v1.u._32[1] ^ v2.u._32[1]; break; case CO_RSHIFT: I64ShiftR( &v1, v2.u._32[ I64LO32 ], &left->u.int64_constant ); break; case CO_LSHIFT: U64ShiftL( &v1, v2.u._32[ I64LO32 ], &left->u.int64_constant ); break; case CO_EQ: left = makeBooleanConst( left, 0 == I64Cmp( &v1, &v2 ) ); return( left ); case CO_NE: left = makeBooleanConst( left, 0 != I64Cmp( &v1, &v2 ) ); return( left ); case CO_GT: left = makeBooleanConst( left, 0 < I64Cmp( &v1, &v2 )) ; return( left ); case CO_LE: left = makeBooleanConst( left, 0 >= I64Cmp( &v1, &v2 ) ); return( left ); case CO_LT: left = makeBooleanConst( left, 0 > I64Cmp( &v1, &v2 )) ; return( left ); case CO_GE: left = makeBooleanConst( left, 0 <= I64Cmp( &v1, &v2 ) ); return( left ); case CO_AND_AND: left = makeBooleanConst( left, !Zero64( &v1 ) && !Zero64( &v2 ) ); return( left ); case CO_OR_OR: left = makeBooleanConst( left, !Zero64( &v1) || !Zero64( &v2 ) ); return( left ); case CO_COMMA: left->u.int64_constant = v2; break; default: return( NULL ); } left->op = PT_INT_CONSTANT; return( left ); }