1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/*
FALCON - The Falcon Programming Language.
FILE: symtab.cpp
Symbol table definition
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: mar ago 3 2004
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include <falcon/symtab.h>
#include <falcon/symbol.h>
#include <falcon/stream.h>
#include <falcon/traits.h>
#include <falcon/module.h>
#include <falcon/traits.h>
namespace Falcon {
SymbolTable::SymbolTable():
m_map( &traits::t_stringptr(), &traits::t_voidp(), 19 )
{
}
void SymbolTable::exportUndefined()
{
MapIterator iter = m_map.begin();
while( iter.hasCurrent() )
{
Symbol *sym = *(Symbol **) iter.currentValue();
if( ! sym->isUndefined() )
sym->exported( true );
iter.next();
}
}
bool SymbolTable::add( Symbol *sym )
{
if( findByName( sym->name() ) != 0 )
return false;
m_map.insert( &sym->name(), sym );
return true;
}
bool SymbolTable::remove( const String &name )
{
return m_map.erase( &name );
}
bool SymbolTable::save( Stream *out ) const
{
uint32 value;
// save the symbol table size.
value = endianInt32( size() );
out->write( &value, sizeof(value) );
MapIterator iter = m_map.begin();
while( iter.hasCurrent() )
{
const Symbol *second = *(const Symbol **) iter.currentValue();
value = endianInt32( second->id() );
out->write( &value, sizeof(value) );
iter.next();
}
return true;
}
bool SymbolTable::load( const Module *mod, Stream *in )
{
// get the symtab type.
int32 value;
in->read( &value, sizeof(value) );
int32 final_size = endianInt32(value);
// preallocate all the symbols;
for ( int i = 0 ; i < final_size; i ++ )
{
Symbol *sym;
in->read( &value, sizeof(value) );
sym = mod->getSymbol( endianInt32(value) );
if ( sym == 0 ) {
return false;
}
if ( sym->name().getRawStorage() == 0 ) {
return false;
}
m_map.insert( &sym->name(), sym );
}
return true;
}
SymbolVector::SymbolVector():
GenericVector( &traits::t_voidp() )
{
}
SymbolVector::~SymbolVector()
{
// free the symbols.
for ( uint32 i = 0; i < size(); i ++ )
{
delete symbolAt( i );
}
}
bool SymbolVector::save( Stream *out ) const
{
uint32 value = endianInt32(size());
out->write( &value, sizeof(value) );
for( uint32 iter = 0; iter < size(); iter++ )
{
symbolAt( iter )->name().serialize( out );
}
for( uint32 iter = 0; iter < size(); iter++ )
{
if ( ! symbolAt( iter )->save( out ) )
return false;
}
return true;
}
bool SymbolVector::load( Module *owner, Stream *in )
{
uint32 value;
in->read( &value, sizeof(value) );
value = endianInt32( value );
resize( value );
for ( uint32 i = 0; i < value; i ++ )
{
Symbol *sym = new Symbol(owner);
sym->id( i );
set( sym, i );
if ( ! sym->name().deserialize( in ) )
return false;
}
for( uint32 iter = 0; iter < size(); iter++ )
{
if ( ! symbolAt( iter )->load( in ) )
return false;
}
return true;
}
}
/* end of symtab.cpp */
|