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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
|
/*
FALCON - The Falcon Programming Language.
FILE: symbol.cpp
Provide non-inlineizable symbol definitions
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: 2004-9-11
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include <falcon/setup.h>
#include <falcon/symbol.h>
#include <falcon/symtab.h>
#include <falcon/module.h>
#include <falcon/stream.h>
#include <falcon/attribmap.h>
#if FALCON_LITTLE_ENDIAN != 1
#include <falcon/pcode.h>
#include <string.h>
#endif
namespace Falcon
{
void Symbol::clear()
{
switch( m_type )
{
case tvar: delete m_value.v_prop; break;
case tfunc: delete m_value.v_func; break;
case textfunc: delete m_value.v_extfunc; break;
case tclass: delete m_value.v_class; break;
case tprop: delete m_value.v_prop; break;
case tconst: delete m_value.v_prop; break;
case timportalias: delete m_value.v_importalias; break;
default:
break;
}
m_type = tundef;
}
bool Symbol::fromClass( const String &find_name ) const
{
if( find_name == name() )
return true;
if( ! isClass() )
return false;
ClassDef *def = m_value.v_class;
return def->inheritsFrom( find_name );
}
bool Symbol::load( Stream *in )
{
uint32 strid;
setUndefined();
byte type;
in->read( &type, sizeof( type ) );
byte exp;
in->read( &exp, sizeof( exp ) );
m_flags = exp;
uint16 pos;
in->read( &pos, sizeof( pos ) );
m_itemPos = endianInt16( pos );
int32 line;
in->read( &line, sizeof( line ) );
m_lineDecl = (int32) endianInt32( line );
// the id is not restored, as it is assigned by load order sequence.
/*if ( ! m_name.deserialize( in, false ) )
{
return false;
}*/
switch( type_t( type ) ) {
case tfunc:
setFunction( new FuncDef( 0, 0 ) );
return getFuncDef()->load( m_module, in );
case tclass:
setClass( new ClassDef );
return getClassDef()->load( m_module, in );
case tvar:
setVar( new VarDef() );
return getVarDef()->load( m_module, in );
case tconst:
setConst( new VarDef() );
return getVarDef()->load( m_module, in );
case tprop:
setProp( new VarDef() );
return getVarDef()->load( m_module, in );
case tglobal:
setGlobal();
break;
case tinst:
{
in->read( &strid , sizeof( strid ) );
strid = endianInt32( strid );
Symbol *other = m_module->getSymbol( strid );
if ( other == 0 )
return false;
setInstance( other );
}
break;
case timportalias:
{
String name, origMod;
name.deserialize( in, false );
origMod.deserialize( in, false );
byte b;
in->read( &b, 1 );
setImportAlias( name, origMod, b == 1 );
}
break;
case tparam:
m_type = type_t( type );
break;
case tundef:
case tlocal:
case tlocalundef:
m_type = type_t( type );
break;
default:
// we don't expect anything else, included textfunc
return false;
}
return true;
}
bool Symbol::save( Stream *out ) const
{
uint32 strid;
byte type = (byte) m_type;
byte flags = m_flags;
int32 line = endianInt32( m_lineDecl );
uint16 pos = endianInt16( m_itemPos );
out->write( &type, sizeof( type ) );
out->write( &flags, sizeof( flags ) );
out->write( &pos, sizeof( pos ) );
out->write( &line, sizeof( line ) );
// the ID is not serialized, as it is determined by the module
//m_name.serialize( out );
switch( m_type ) {
case tfunc: getFuncDef()->save( m_module, out ); break;
case tclass: getClassDef()->save( m_module, out ); break;
case tvar:
case tconst:
case tprop: getVarDef()->save( m_module, out ); break;
case tinst:
strid = endianInt32( getInstance()->id() );
out->write( &strid, sizeof( strid ) );
break;
case timportalias:
getImportAlias()->name().serialize( out );
getImportAlias()->origModule().serialize( out );
{
byte b = getImportAlias()->isOrigFileName() ? 1 : 0;
out->write( &b, 1 );
}
break;
default:
break;
}
return true;
}
Symbol* Symbol::addParam( const String ¶m )
{
Symbol* tbc = this;
if ( isClass() )
{
tbc = getClassDef()->constructor();
if ( tbc == 0 )
return this;
}
switch( tbc->m_type ) {
case tfunc: tbc->getFuncDef()->addParameter(m_module->addSymbol( param )); break;
case textfunc: tbc->getExtFuncDef()->addParam(m_module->addSymbol( param )); break;
default:
return this;
}
return this;
}
//=================================================================
//
int32 ExtFuncDef::getParam( const String &name )
{
if ( m_params == 0 )
return -1;
Symbol *sym = m_params->findByName( name );
if ( sym == 0 )
return -1;
return sym->itemId();
}
/** Adds a function parameter with the specified ID.
Consider using Symbol::addParam() instead (candy grammar).
*/
ExtFuncDef &ExtFuncDef::addParam( Symbol *param, int32 id )
{
if ( m_params == 0 )
m_params = new SymbolTable;
param->setParam();
param->itemId( id == -1 ? m_params->size() : id );
m_params->add( param );
return *this;
}
ExtFuncDef::~ExtFuncDef()
{
delete m_params;
}
//=================================================================
//
FuncDef::FuncDef( byte *code, uint32 codeSize ):
m_code( code ),
m_codeSize( codeSize ),
m_params( 0 ),
m_locals( 0 ),
m_undefined( 0 ),
m_onceItemId( NO_STATE ),
m_basePC(0),
m_attributes(0)
{
}
FuncDef::~FuncDef()
{
memFree( m_code );
delete m_attributes;
}
Symbol *FuncDef::addParameter( Symbol *sym )
{
sym->itemId( m_params++ );
sym->setParam();
m_symtab.add( sym );
return sym;
}
Symbol *FuncDef::addLocal( Symbol *sym )
{
sym->itemId( m_locals++ );
sym->setLocal();
m_symtab.add( sym );
return sym;
}
Symbol *FuncDef::addUndefined( Symbol *sym )
{
sym->itemId( m_undefined++ );
sym->setLocalUndef();
m_symtab.add( sym );
return sym;
}
bool FuncDef::save( const Module* mod, Stream *out ) const
{
uint16 locs = endianInt16( m_locals );
uint16 params = endianInt16( m_params );
out->write( &locs, sizeof( locs ) );
out->write( ¶ms, sizeof( params ) );
uint32 onceId = endianInt32(m_onceItemId);
out->write( &onceId, sizeof( onceId ) );
uint32 basePC = endianInt32(m_basePC);
out->write( &basePC, sizeof( basePC ) );
uint32 codeSize = endianInt32(m_codeSize);
out->write( &codeSize, sizeof( codeSize ) );
if ( m_codeSize > 0 )
{
// On little endian platforms, save an endianized copy of the code.
#if FALCON_LITTLE_ENDIAN != 1
byte* ecode = (byte*) memAlloc( m_codeSize );
memcpy( ecode, m_code, m_codeSize );
PCODE::endianize( ecode, m_codeSize );
bool res = out->write( ecode, m_codeSize );
memFree( ecode );
#else
bool res = out->write( m_code, m_codeSize ) == (int) m_codeSize;
#endif
if ( ! res )
return false;
}
if ( m_attributes != 0)
{
basePC = endianInt32(1);
out->write( &basePC, sizeof( basePC ) );
m_attributes->save( mod, out );
}
else {
basePC = endianInt32(0);
out->write( &basePC, sizeof( basePC ) );
}
return m_symtab.save( out );
}
void FuncDef::addAttrib( const String& name, VarDef* vd )
{
if ( m_attributes == 0 )
m_attributes = new AttribMap;
m_attributes->insertAttrib( name, vd );
}
bool FuncDef::load( const Module *mod, Stream *in )
{
uint16 loc;
in->read( &loc, sizeof( loc ) );
m_locals = endianInt16( loc );
in->read( &loc, sizeof( loc ) );
m_params = endianInt16( loc );
uint32 onceItem = 0;
in->read( &onceItem, sizeof( onceItem ) );
m_onceItemId = endianInt32( onceItem );
uint32 basePC = 0;
in->read( &basePC, sizeof( basePC ) );
m_basePC = endianInt32( basePC );
int32 codeSize = 0;
in->read( &codeSize, sizeof( codeSize ) );
m_codeSize = endianInt32( codeSize );
m_code = 0;
// it's essential to check for errors now.
if ( ! in->good() )
return false;
if ( m_codeSize > 0 )
{
m_code = (byte *) memAlloc( m_codeSize );
in->read( m_code, m_codeSize );
// it's essential to check for errors now.
if ( ! in->good() )
return false;
// de-endianize the code on little endian platforms.
#if FALCON_LITTLE_ENDIAN != 1
PCODE::deendianize( m_code, m_codeSize );
#endif
}
in->read( &basePC, sizeof( basePC ) );
if( basePC != 0 )
{
m_attributes = new AttribMap;
if ( ! m_attributes->load( mod, in ) )
return false;
}
return m_symtab.load( mod, in );
}
InheritDef::~InheritDef()
{
}
bool InheritDef::save( Stream *out ) const
{
uint32 parentId = endianInt32( m_baseClass->id() );
out->write( &parentId, sizeof( parentId ) );
return true;
}
bool InheritDef::load( const Module *mod, Stream *in )
{
uint32 parentId;
in->read( &parentId , sizeof( parentId ) );
parentId = endianInt32( parentId );
m_baseClass = mod->getSymbol( parentId );
if ( m_baseClass == 0 )
return false;
return true;
}
//=================================================================
//
ClassDef::ClassDef( ObjectFactory fact ):
FuncDef( 0, 0 ),
m_constructor( 0 ),
m_properties( &traits::t_stringptr(), &traits::t_voidp() ),
m_factory( fact ),
m_metaclassFor( -1 ),
m_bFinal( false ),
m_states( &traits::t_stringptr(), &traits::t_voidp() )
{}
ClassDef::ClassDef( Symbol *ext_ctor, ObjectFactory fact ):
FuncDef( 0, 0 ),
m_constructor( ext_ctor ),
m_properties( &traits::t_stringptr(), &traits::t_voidp() ),
m_factory( fact ),
m_metaclassFor( -1 ),
m_bFinal( false ),
m_states( &traits::t_stringptr(), &traits::t_voidp() )
{}
ClassDef::~ClassDef()
{
MapIterator iterp = m_properties.begin();
while( iterp.hasCurrent() )
{
VarDef *vd = *(VarDef **) iterp.currentValue();
delete vd;
iterp.next();
}
ListElement *iteri = m_inheritance.begin();
while( iteri != 0 )
{
InheritDef *def = (InheritDef *) iteri->data();
delete def;
iteri = iteri->next();
}
MapIterator iters = m_states.begin();
while( iters.hasCurrent() )
{
StateDef *sd = *(StateDef **) iters.currentValue();
delete sd;
iters.next();
}
}
bool ClassDef::checkCircularInheritance( const Symbol *child ) const
{
if ( child->isClass() && child->getClassDef() == this )
return true;
ListElement *iteri = m_inheritance.begin();
while( iteri != 0 )
{
InheritDef *def = (InheritDef *) iteri->data();
if( def->base() == child )
return true;
if( def->base()->isClass() && def->base()->getClassDef()->checkCircularInheritance( child ) )
return true;
iteri = iteri->next();
}
return false;
}
void ClassDef::addProperty( const String *name, VarDef *definition )
{
MapIterator iter;
if ( m_properties.find( name, iter ) ) {
VarDef **vd =(VarDef **) iter.currentValue();
delete *vd;
*vd = definition;
}
else
m_properties.insert( name, definition );
}
VarDef *ClassDef::getProperty( const String *name ) const
{
VarDef **vd = (VarDef **) m_properties.find( name );
if ( vd != 0 )
return *vd;
return 0;
}
bool ClassDef::addInheritance( InheritDef *parent_class )
{
Symbol *parent = parent_class->base();
if ( getProperty( parent->name() ) != 0 )
return false;
m_inheritance.pushBack( parent_class );
addProperty( &parent->name(), new VarDef( VarDef::t_base, parent ) );
return true;
}
bool ClassDef::inheritsFrom( const String &find_name ) const
{
ListElement *iter = m_inheritance.begin();
// perform a flat-first search ? -- not for now
while( iter != 0 ) {
const InheritDef *def = (const InheritDef *) iter->data();
const Symbol *i = def->base();
if( i->fromClass( find_name ) )
return true;
iter = iter->next();
}
return false;
}
bool ClassDef::addState( const String *state_name, StateDef* state )
{
if( m_states.find( state_name ) != 0 )
return false;
m_states.insert( state_name, state );
return true;
}
StateDef* ClassDef::addState( const String *state_name )
{
if( m_states.find( state_name ) != 0 )
return 0;
StateDef* state = new StateDef( state_name );
m_states.insert( state_name, state );
return state;
}
bool ClassDef::save( const Module* mod, Stream *out ) const
{
if ( ! FuncDef::save( mod, out ) )
return false;
uint32 has;
// Have we got a constructor?
if( m_constructor == 0 ) {
has = 0xffFFffFF;
}
else {
has = endianInt32( (uint32)m_constructor->id() );
}
out->write( &has , sizeof( has ) );
// now save the property table
has = endianInt32(m_properties.size());
out->write( &has , sizeof( has ) );
MapIterator iter = m_properties.begin();
while( iter.hasCurrent() )
{
const String *key = *(const String **) iter.currentKey();
const VarDef *value = *(const VarDef **) iter.currentValue();
key->serialize( out );
value->save( mod, out );
iter.next();
}
// and finally the inheritance list
has = endianInt32(m_inheritance.size());
out->write( &has , sizeof( has ) );
ListElement *iiter = m_inheritance.begin();
while( iiter != 0 )
{
const InheritDef *def = (const InheritDef *) iiter->data();
if ( ! def->save( out ) )
return false;
iiter = iiter->next();
}
// and the state list
has = endianInt32(m_states.size());
out->write( &has , sizeof( has ) );
MapIterator siter = m_states.begin();
while( siter.hasCurrent() )
{
const String *key = *(const String **) siter.currentKey();
const StateDef *value = *(const StateDef **) siter.currentValue();
key->serialize( out );
value->save( out );
siter.next();
}
return true;
}
bool ClassDef::load( const Module *mod, Stream *in )
{
if ( ! FuncDef::load( mod, in ) )
return false;
uint32 value;
in->read( &value , sizeof( value ) );
value = endianInt32( value );
// Have we got a constructor?
if( value == 0xffFFffFF ) {
m_constructor = 0 ;
}
else {
m_constructor = mod->getSymbol( value );
if ( m_constructor == 0 )
return false;
}
// now load the property table
in->read( &value , sizeof( value ) );
value = endianInt32( value );
for( uint32 i = 0; i < value; i ++ )
{
String key;
if ( ! key.deserialize( in ) )
return false;
VarDef *def = new VarDef();
// avoid memleak by early insertion
m_properties.insert( new String( key ), def );
if ( ! def->load( mod, in ) )
return false;
}
// now load the inheritance table
in->read( &value , sizeof( value ) );
value = endianInt32( value );
for( uint32 j = 0; j < value; j ++ )
{
InheritDef *def = new InheritDef();
m_inheritance.pushBack( def );
if ( ! def->load( mod, in ) )
return false;
}
// and the state list
in->read( &value , sizeof( value ) );
value = endianInt32( value );
for( uint32 i = 0; i < value; i ++ )
{
uint32 id;
in->read( &id , sizeof( id ) );
const String *name = mod->getString( endianInt32( id ) );
if ( name == 0 )
return false;
StateDef *def = new StateDef( name );
// avoid memleak by early insertion
m_states.insert( name, def );
if ( ! def->load( mod, in ) )
return false;
}
return true;
}
//===================================================================
// statedef
StateDef::StateDef( const String* sname ):
m_name( sname ),
m_functions( &traits::t_stringptr_own(), &traits::t_voidp() )
{
}
bool StateDef::addFunction( const String& name, Symbol* func )
{
if ( m_functions.find( &name ) != 0 )
return false;
m_functions.insert( new String(name), func );
return true;
}
bool StateDef::save( Stream* out ) const
{
// List the functions
uint32 size = endianInt32(m_functions.size());
out->write( &size , sizeof( size ) );
MapIterator siter = m_functions.begin();
while( siter.hasCurrent() )
{
const String *key = *(const String **) siter.currentKey();
const Symbol *value = *(const Symbol **) siter.currentValue();
key->serialize( out );
size = endianInt32( value->id() );
out->write( &size, sizeof(size) );
siter.next();
}
return out->good();
}
bool StateDef::load( const Module *mod, Stream* in )
{
// List the functions
uint32 size;
in->read( &size , sizeof( size ) );
size = endianInt32( size );
for( uint32 i = 0; i < size; ++i )
{
int32 val;
String name;
if( ! name.deserialize( in, false ) )
return false;
if( in->read( &val , sizeof( val ) ) != sizeof(val) )
return false;
Symbol* func = mod->getSymbol(endianInt32( val ));
if ( func == 0 )
return false;
addFunction( name, func );
}
return true;
}
//===================================================================
// vardef
bool VarDef::save( const Module* mod, Stream *out ) const
{
int32 type = endianInt32((int32) m_val_type);
out->write( &type , sizeof( type ) );
switch( m_val_type )
{
case t_bool:
{
int32 val = m_value.val_bool ? 1: 0;
out->write( &val , sizeof( val ) );
}
break;
case t_int:
{
int64 val = endianInt64( m_value.val_int );
out->write( &val , sizeof( val ) );
}
break;
case t_num:
{
numeric num = endianNum( m_value.val_num );
out->write( &num , sizeof( num ) );
}
break;
case t_string:
{
uint32 val = mod->stringTable().findId( *asString() );
out->write( &val , sizeof( val ) );
}
break;
case t_symbol:
case t_reference:
case t_base:
{
uint32 val = endianInt32( asSymbol()->id() );
out->write( &val , sizeof( val ) );
}
default:
return true;
}
return out->good();
}
bool VarDef::load( const Module *mod, Stream *in )
{
setNil();
int32 type;
in->read( &type , sizeof( type ) );
m_val_type = (t_type) endianInt32(type);
switch( m_val_type )
{
case t_bool:
{
int32 val;
in->read( &val , sizeof( val ) );
m_value.val_bool = val != 0;
}
break;
case t_int:
{
int64 val;
in->read( &val , sizeof( val ) );
m_value.val_int = endianInt64( val );
}
break;
case t_num:
{
numeric val;
in->read( &val, sizeof( val ) );
m_value.val_num = endianNum( val );
}
break;
case t_string:
{
int32 val;
in->read( &val , sizeof( val ) );
m_value.val_str = mod->getString(endianInt32( val ));
if ( m_value.val_str == 0 )
return false;
}
break;
case t_symbol:
case t_reference:
case t_base:
{
int32 val;
in->read( &val , sizeof( val ) );
m_value.val_sym = mod->getSymbol(endianInt32( val ));
if ( m_value.val_sym == 0 )
return false;
}
break;
case t_reflective:
// we can save only set/get reflectors
setReflective( e_reflectSetGet, 0xFFFFFFFF );
break;
default:
break;
}
return true;
}
}
/* end of symbol.cpp */
|