00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef FLC_SYMBOL_H
00017 #define FLC_SYMBOL_H
00018
00019 #include <falcon/types.h>
00020 #include <falcon/symtab.h>
00021 #include <falcon/symlist.h>
00022 #include <falcon/genericmap.h>
00023 #include <falcon/basealloc.h>
00024 #include <falcon/reflectfunc.h>
00025 #include <falcon/objectfactory.h>
00026 #include <falcon/fassert.h>
00027 #include <falcon/itemid.h>
00028 #include <falcon/vardef.h>
00029
00030 namespace Falcon {
00031
00032 class Symbol;
00033 class Stream;
00034 class AttribMap;
00035
00039 class FALCON_DYN_CLASS ExtFuncDef: public BaseAlloc
00040 {
00042 ext_func_t m_func;
00043
00047 void *m_extra;
00048
00049 SymbolTable *m_params;
00050
00051 public:
00052 ExtFuncDef( ext_func_t func ):
00053 m_func( func ),
00054 m_extra( 0 ),
00055 m_params( 0 )
00056 {}
00057
00061 ExtFuncDef( ext_func_t func, void *extra ):
00062 m_func( func ),
00063 m_extra( extra ),
00064 m_params(0)
00065 {}
00066
00067 ~ExtFuncDef();
00068
00072 void call( VMachine *vm ) const { m_func( vm ); }
00073
00076 void operator()( VMachine *vm ) const { call( vm ); }
00077
00081 void *extra() const { return m_extra; }
00082
00100 void extra( void *e ) { m_extra = e; }
00101
00103 int32 getParam( const String &name );
00104
00108 ExtFuncDef &addParam( Symbol *param, int32 id =-1 );
00109
00118 SymbolTable *parameters() const { return m_params; }
00119
00120 uint32 paramCount() const { return m_params == 0 ? 0 : m_params->size(); }
00121
00122 ext_func_t func() const { return m_func; }
00123 };
00124
00125
00129 class FALCON_DYN_CLASS ImportAlias: public BaseAlloc
00130 {
00131 const String *m_name;
00132 const String *m_origModule;
00133 bool m_isFileName;
00134
00135 public:
00136 ImportAlias( const String* name, const String* origModule, bool bIsFileName = false ):
00137 m_name( name ),
00138 m_origModule( origModule ),
00139 m_isFileName( bIsFileName )
00140 {}
00141
00142 const String *name() const { return m_name; }
00143 const String *origModule() const { return m_origModule; }
00144 bool isOrigFileName() const { return m_isFileName; }
00145 };
00146
00152 class FALCON_DYN_CLASS FuncDef: public BaseAlloc
00153 {
00155 SymbolTable m_symtab;
00156
00160 byte *m_code;
00161
00165 uint32 m_codeSize;
00166
00168 uint16 m_params;
00169
00171 uint16 m_locals;
00172
00174 uint16 m_undefined;
00175
00179 uint32 m_onceItemId;
00180
00181 uint32 m_basePC;
00182
00188 AttribMap* m_attributes;
00189
00190 public:
00191 enum {
00192 NO_STATE = 0xFFFFFFFF
00193 } enum_NO_STATE;
00194
00199 FuncDef( byte *code, uint32 codeSize );
00200 ~FuncDef();
00201
00202 const SymbolTable &symtab() const { return m_symtab; }
00203 SymbolTable &symtab() { return m_symtab; }
00204
00205 Symbol *addParameter( Symbol *sym );
00206 Symbol *addLocal( Symbol *sym );
00207 Symbol *addUndefined( Symbol *sym );
00208
00209 uint32 codeSize() const { return m_codeSize; }
00210 void codeSize( uint32 p ) { m_codeSize = p; }
00211 byte *code() const { return m_code; }
00212 void code( byte *b ) { m_code = b; }
00213 uint16 params() const { return m_params; }
00214 uint16 locals() const { return m_locals; }
00215 uint16 undefined() const { return m_undefined; }
00216 void params( uint16 p ) { m_params = p; }
00217 void locals( uint16 l ) { m_locals = l; }
00218 void undefined( uint16 u ) { m_undefined = u; }
00219
00220 void basePC( uint32 pc ) { m_basePC = pc; }
00221
00229 uint32 basePC() const { return m_basePC; }
00230
00238 void recount();
00239
00240 bool save( Stream *out ) const;
00241 bool load( Module *mod, Stream *in );
00242
00243 void onceItemId( uint32 id ) { m_onceItemId = id; }
00244 uint32 onceItemId() const { return m_onceItemId; }
00245
00249 void addAttrib( const String& name, VarDef* value );
00250
00254 AttribMap* attributes() const { return m_attributes; }
00255 };
00256
00257
00272 class FALCON_DYN_CLASS InheritDef: public BaseAlloc
00273 {
00274 private:
00276 Symbol *m_baseClass;
00277
00278 public:
00283 InheritDef( Symbol *bc ):
00284 m_baseClass( bc )
00285 {}
00286
00290 InheritDef():
00291 m_baseClass( 0 )
00292 {}
00293
00294 ~InheritDef();
00295
00296 bool save( Stream *out ) const;
00297 bool load( Module *mod, Stream *in );
00298
00299 Symbol *base() const { return m_baseClass; }
00300 };
00301
00317 class FALCON_DYN_CLASS ClassDef: public FuncDef
00318 {
00319 public:
00320 typedef List InheritList;
00321
00322 private:
00323
00330 Symbol *m_constructor;
00331
00349 Map m_properties;
00350
00353 InheritList m_inheritance;
00354
00358 ObjectFactory m_factory;
00359
00360 int m_metaclassFor;
00361
00362 bool m_bFinal;
00363
00364 public:
00373 ClassDef( ObjectFactory factory=0 );
00374
00384 ClassDef( Symbol *ext_ctor, ObjectFactory factory=0 );
00385 ~ClassDef();
00386
00392 void factory( ObjectFactory om ) { m_factory = om; }
00393 ObjectFactory factory() const { return m_factory; }
00394
00395 const Map &properties() const { return m_properties; }
00397 Map &properties() { return m_properties; }
00398
00399 const InheritList &inheritance() const { return m_inheritance; }
00401 InheritList &inheritance() { return m_inheritance; }
00402
00403 void constructor( Symbol *ctor ) { m_constructor = ctor; }
00404 Symbol *constructor() const { return m_constructor; }
00405
00406
00407 bool save( Stream *out ) const;
00408 bool load( Module *mod, Stream *in );
00409
00421 void addProperty( const String *name, VarDef *definition );
00422
00423 bool hasProperty( const String &name )
00424 {
00425 return m_properties.find( &name ) != 0;
00426 }
00427
00431 void addProperty( const String *name )
00432 {
00433 addProperty( name, new VarDef() );
00434 }
00435
00440 void addProperty( const String *name, Symbol *method )
00441 {
00442 addProperty( name, new VarDef( method ) );
00443 }
00444
00445 VarDef *getProperty( const String *name ) const;
00446
00447 VarDef *getProperty( const String &name ) const
00448 {
00449 return getProperty( &name );
00450 }
00451
00456 bool addInheritance( InheritDef *parent_class );
00457
00459 bool inheritsFrom( const String &find_name ) const;
00460
00461 int isMetaclassFor() const { return m_metaclassFor; }
00462 void setMetaclassFor( int ItemID ) { fassert( ItemID >= 0 && ItemID < FLC_ITEM_COUNT ); m_metaclassFor = ItemID; }
00463
00464 void setFinal( bool mod ) { m_bFinal = mod; }
00465 bool isFinal() const { return m_bFinal; }
00466 };
00467
00475 class FALCON_DYN_CLASS Symbol: public BaseAlloc
00476 {
00477 public:
00478 typedef enum {
00479 tundef,
00480 tglobal,
00481 tlocal,
00482 tparam,
00483 tlocalundef,
00484 tfunc,
00485 textfunc,
00486 tclass,
00487 tprop,
00488 tvar,
00489 tinst,
00490 tconst,
00491 timportalias
00492 } type_t;
00493
00494 private:
00495 typedef enum {
00496 FLAG_EXPORTED=0x1,
00497 FLAG_ETAFUNC=0x2,
00498 FLAG_WELLKNOWN=0x4,
00499 FLAG_IMPORTED=0x8,
00500 FLAG_ENUM=0x16
00501 }
00502 e_flags;
00503
00504 type_t m_type;
00505
00507 uint8 m_flags;
00508
00510 uint16 m_itemPos;
00511
00513 uint32 m_id;
00514
00516 int32 m_lineDecl;
00517
00519 const String *m_name;
00520
00522 Module *m_module;
00523
00524 union {
00525 FuncDef *v_func;
00526 ExtFuncDef *v_extfunc;
00527 ImportAlias *v_importalias;
00528 ClassDef *v_class;
00529 VarDef *v_prop;
00530 Symbol *v_symbol;
00531 } m_value;
00532
00533 void clear();
00534
00535 public:
00536
00544 Symbol( Module *mod, uint32 id, const String *name, bool exp ):
00545 m_type( tundef ),
00546 m_flags(exp ? 1: 0),
00547 m_id( id ),
00548 m_lineDecl(0),
00549 m_name( name ),
00550 m_module(mod)
00551 {}
00552
00563 Symbol( Module *mod, const String *name ):
00564 m_type( tundef ),
00565 m_flags( 0 ),
00566 m_id(0),
00567 m_lineDecl(0),
00568 m_name( name ),
00569 m_module( mod )
00570 {}
00571
00578 Symbol( Module *owner ):
00579 m_type( tundef ),
00580 m_flags( 0 ),
00581 m_id( 0 ),
00582 m_lineDecl(0),
00583 m_name( 0 ),
00584 m_module( owner )
00585 {}
00586
00587 ~Symbol() {
00588 clear();
00589 }
00590
00596 void name( const String *name ) { m_name = name; }
00597
00601 void id( uint32 i ) { m_id = i; }
00602
00607 Symbol* exported( bool exp ) {
00608 if ( exp )
00609 m_flags |= FLAG_EXPORTED;
00610 else
00611 m_flags &=~FLAG_EXPORTED;
00612 return this;
00613 }
00614
00622 Symbol* imported( bool exp ) {
00623 if ( exp )
00624 m_flags |= FLAG_IMPORTED;
00625 else
00626 m_flags &=~FLAG_IMPORTED;
00627 return this;
00628 }
00629
00636 Symbol* setEta( bool exp ) {
00637 if ( exp )
00638 m_flags |= FLAG_ETAFUNC;
00639 else
00640 m_flags &=~FLAG_ETAFUNC;
00641 return this;
00642 }
00643
00663 Symbol* setWKS( bool exp ) {
00664 if ( exp )
00665 m_flags |= FLAG_WELLKNOWN;
00666 else
00667 m_flags &=~FLAG_WELLKNOWN;
00668 return this;
00669 }
00670
00671
00679 Symbol* setEnum( bool exp ) {
00680 if ( exp )
00681 m_flags |= FLAG_ENUM;
00682 else
00683 m_flags &=~FLAG_ENUM;
00684 return this;
00685 }
00686
00687 void setUndefined() { clear(); m_type = tundef; }
00688 void setLocalUndef() { clear(); m_type = tlocalundef; }
00689 void setGlobal() { clear(); m_type = tglobal; }
00690 void setLocal() { clear(); m_type = tlocal; }
00691 void setParam() { clear(); m_type = tparam; }
00692 void setFunction( FuncDef *func ) { clear(); m_type = tfunc; m_value.v_func = func; }
00693 void setExtFunc( ExtFuncDef *f ) { clear(); m_type = textfunc; m_value.v_extfunc = f; }
00694 void setClass( ClassDef *f ) { clear(); m_type = tclass; m_value.v_class = f; }
00695 void setProp( VarDef *p ) { clear(); m_type = tprop; m_value.v_prop = p; }
00696 void setVar( VarDef *p ) { clear(); m_type = tvar; m_value.v_prop = p; }
00697 void setConst( VarDef *p ) { clear(); m_type = tconst; m_value.v_prop = p; }
00698 void setInstance( Symbol *base_class ) { clear(); m_type = tinst; m_value.v_symbol = base_class; }
00699 void setImportAlias( ImportAlias* alias )
00700 { clear(); m_type = timportalias; m_value.v_importalias = alias; imported(true); }
00701 void setImportAlias( const String *name, const String* origModule, bool bIsFileName = false ) {
00702 setImportAlias( new ImportAlias( name, origModule, bIsFileName ) ); }
00703
00704 const String &name() const { return *m_name; }
00705 uint32 id() const { return m_id; }
00706 type_t type() const { return m_type; }
00707 bool exported() const { return (! imported()) && ((m_flags & FLAG_EXPORTED) == FLAG_EXPORTED); }
00708 bool imported() const { return (m_flags & FLAG_IMPORTED) == FLAG_IMPORTED; }
00709 uint16 itemId() const { return m_itemPos; }
00710 void itemId( uint16 ip ) { m_itemPos = ip; }
00711 bool isEta() const { return (m_flags & FLAG_ETAFUNC) == FLAG_ETAFUNC; }
00712 bool isWKS() const { return (m_flags & FLAG_WELLKNOWN) == FLAG_WELLKNOWN; }
00713 bool isEnum() const { return (m_flags & FLAG_ENUM) == FLAG_ENUM; }
00714
00715 bool isUndefined() const { return imported() || m_type == tundef; }
00716 bool isLocalUndef() const { return m_type == tlocalundef; }
00717 bool isGlobal() const { return m_type == tglobal; }
00718 bool isLocal() const { return m_type == tlocal; }
00719 bool isParam() const { return m_type == tparam; }
00720 bool isFunction() const { return m_type == tfunc; }
00721 bool isExtFunc() const { return m_type == textfunc; }
00722 bool isClass() const { return m_type == tclass; }
00723 bool isProp() const { return m_type == tprop; }
00724 bool isVar() const { return m_type == tvar; }
00725 bool isCallable() const { return isFunction() || isExtFunc() || isClass(); }
00726 bool isInstance() const { return m_type == tinst; }
00727 bool isConst() const { return m_type == tconst; }
00728 bool isImportAlias() const { return m_type == timportalias; }
00729
00731 Symbol* addParam( const String ¶m );
00732
00733 FuncDef *getFuncDef() const { return m_value.v_func; }
00734 ExtFuncDef *getExtFuncDef() const { return m_value.v_extfunc; }
00735 ClassDef *getClassDef() const { return m_value.v_class; }
00736 VarDef *getVarDef() const { return m_value.v_prop; }
00737 Symbol *getInstance() const { return m_value.v_symbol; }
00738 ImportAlias* getImportAlias() const { return m_value.v_importalias; }
00739 uint16 getItemId() const { return m_itemPos; }
00740
00746 bool fromClass( const String &find_name ) const;
00747
00748 bool save( Stream *out ) const;
00749 bool load( Stream *in );
00750
00754 const Module *module() const { return m_module; }
00755 void module( Module *mod ) { m_module = mod; }
00756
00757 void declaredAt( int32 line ) { m_lineDecl = line; }
00758 int32 declaredAt() const { return m_lineDecl; }
00759 };
00760
00761
00763 #define FLC_CLSYM_VAR ((Falcon::Symbol *) 0)
00764 #define FLC_CLSYM_METHOD ((Falcon::Symbol *) 1)
00765 #define FLC_CLSYM_BASE ((Falcon::Symbol *) 2)
00766
00767 }
00768
00769 #endif
00770
00771