rtti.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 545 行 · 第 1/2 页

C
545
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include "plusplus.h"

#include <stdio.h>

#include "errdefns.h"
#include "carve.h"
#include "pcheader.h"
#include "initdefs.h"
#include "ring.h"
#include "cgfront.h"
#include "cgback.h"
#include "rtti.h"

#define BLOCK_RTTI_CLASS        16
#define BLOCK_RTTI_TYPEID       16
#define BLOCK_RTTI_VFPTR        16
static carve_t carveRTTI_CLASS;         // - allocations for RTTI_CLASSs
static carve_t carveRTTI_TYPEID;        // - allocations for RTTI_TYPEIDs
static carve_t carveRTTI_VFPTR;         // - allocations for RTTI_VFPTRs
static RTTI_CLASS *rttiClasses;         // - rtti classes so far
static RTTI_TYPEID *rttiTypeids;        // - rtti typeids so far


static RTTI_CLASS *newClass( TYPE class_type )
{
    RTTI_CLASS *new_class;

    new_class = CarveAlloc( carveRTTI_CLASS );
    RingPush( &rttiClasses, new_class );
    new_class->vfptrs = NULL;
    new_class->class_type = class_type;
    new_class->sym = NULL;
    new_class->offset = 0;
    new_class->done = FALSE;
    new_class->gen = FALSE;
    new_class->cg_gen = FALSE;
    new_class->free = FALSE;
    new_class->too_big = FALSE;
    return( new_class );
}

static RTTI_CLASS *findClass( TYPE class_type )
{
    RTTI_CLASS *curr;

    RingIterBeg( rttiClasses, curr ) {
        if( curr->class_type == class_type ) {
            return( curr );
        }
    } RingIterEnd( curr )
    return( NULL );
}

static RTTI_TYPEID *newTypeid( TYPE type )
{
    RTTI_TYPEID *new_typeid;

    new_typeid = CarveAlloc( carveRTTI_TYPEID );
    RingPush( &rttiTypeids, new_typeid );
    new_typeid->type = type;
    new_typeid->sym = MakeTypeidSym( type );
    new_typeid->free = FALSE;
    return( new_typeid );
}

static RTTI_TYPEID *findTypeid( TYPE type )
{
    RTTI_TYPEID *curr;

    RingIterBeg( rttiTypeids, curr ) {
        if( curr->type == type ) {
            return( curr );
        }
    } RingIterEnd( curr )
    return( NULL );
}

static RTTI_VFPTR *newVfptr( RTTI_CLASS *class_entry, CLASS_TABLE *location )
{
    RTTI_VFPTR *new_vfptr;

    new_vfptr = CarveAlloc( carveRTTI_VFPTR );
    RingAppend( &(class_entry->vfptrs), new_vfptr );
    new_vfptr->control = RA_NULL;
    new_vfptr->free = FALSE;
    if( location->delta != location->exact_delta ) {
        /* vfptr is in a virtual base */
        if( location->ctor_disp ) {
            new_vfptr->control |= RA_CDISP;
            new_vfptr->delta = location->delta;
        } else {
            new_vfptr->delta = location->exact_delta;
        }
    } else {
        new_vfptr->delta = location->delta;
    }
    new_vfptr->offset = class_entry->offset;
    class_entry->offset += RttiAdjustSize();
    return( new_vfptr );
}

SYMBOL RttiBuild( SCOPE host, CLASS_TABLE *location, target_offset_t *poffset )
/*****************************************************************************/
{
    SYMBOL sym;
    TYPE class_type;
    RTTI_CLASS *class_entry;
    RTTI_VFPTR *vfptr_entry;

    if( ! CompFlags.rtti_enabled ) {
        *poffset = 0;
        return( NULL );
    }
    class_type = ScopeClass( host );
    class_entry = findClass( class_type );
    if( class_entry == NULL ) {
        class_entry = newClass( class_type );
        sym = MakeVATableSym( host );
        class_entry->sym = sym;
    }
    vfptr_entry = newVfptr( class_entry, location );
    *poffset = vfptr_entry->offset;
    return( class_entry->sym );
}

void RttiDone( SCOPE host )
/*************************/
{
    TYPE class_type;
    RTTI_CLASS *class_entry;

    class_type = ScopeClass( host );
    if( ! CompFlags.rtti_enabled ) {
        return;
    }
    class_entry = findClass( class_type );
    DbgAssert( class_entry != NULL );
    // class_entry can be NULL if there is a bug in name mangling
    // not creating unique enough names (rtti08.c)
    if( class_entry != NULL ) {
        class_entry->done = TRUE;
    }
}

static target_size_t rttiInfoSize( RTTI_CLASS *class_entry )
{
    TYPE class_type;
    unsigned nbases;
    target_size_t size;

    DbgAssert( class_entry->done );
    class_type = class_entry->class_type;
    nbases = ScopeRttiClasses( class_type );
    DbgAssert( nbases >= 1 );
    size = class_entry->offset;
    size += RttiClassSize();
    size += ( nbases - 1 ) * RttiLeapSize();
#if _CPU == 8086
    if( size > 0x010000 ) {
        CErr( ERR_TOO_MUCH_FOR_RTTI, class_type, nbases );
        size = 1;
        class_entry->too_big = TRUE;
    }
#endif
    return( size );
}


void RttiRef( SYMBOL sym )
/************************/
{
    target_size_t size;
    TYPE table_type;
    RTTI_CLASS *curr;

    if( sym == NULL ) {
        return;
    }
    RingIterBeg( rttiClasses, curr ) {
        if( curr->sym == sym ) {
            if( ! curr->gen ) {
                curr->gen = TRUE;
                DbgAssert( sym->segid == SEG_NULL );
                table_type = TypedefModifierRemove( sym->sym_type );
                DbgAssert( table_type->id == TYP_ARRAY );
                size = rttiInfoSize( curr );
                table_type->u.a.array_size = size;
                CgSegId( sym );
                SegmentMarkUsed( sym->segid );
            }
            return;
        }
    } RingIterEnd( curr )
    DbgNever();
    return;
}


void RttiWalk( void (*walker)( RTTI_CLASS * ) )
/*********************************************/
{
    RTTI_CLASS *curr;

    RingIterBegSafe( rttiClasses, curr ) {
        (*walker)( curr );
    } RingIterEndSafe( curr )
}

SYMBOL TypeidAccess( TYPE type )
/******************************/
{
    TYPE ref_type;
    RTTI_TYPEID *typeid_entry;
    SYMBOL sym;

    // this needs to function even if RTTI is not enabled since type signatures
    DbgAssert( type != NULL );
    type = TypedefModifierRemoveOnly( type );
    ref_type = TypeReference( type );
    if( ref_type != NULL ) {
        type = TypedefModifierRemoveOnly( ref_type );
    }
    typeid_entry = findTypeid( type );
    if( typeid_entry == NULL ) {
        typeid_entry = newTypeid( type );
    }
    sym = typeid_entry->sym;
    DbgAssert( sym != NULL );
    return( sym );
}

SYMBOL TypeidICAccess( TYPE type )
/********************************/
{
    SYMBOL sym;

    sym = TypeidAccess( type );
    CgFrontCodePtr( IC_TYPEID_REF, sym );
    if( ModuleInitScope() == GetCurrScope() ) {
        // useless if module fn is generated (which it always will be if there
        // is any code to execute) but if there is no code, any static links

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?