From 8462a3f0a32931347fa349b72756b5d30fcf871f Mon Sep 17 00:00:00 2001 From: Ge Wang Date: Wed, 27 Nov 2024 16:08:25 -0800 Subject: [PATCH] update compiler namespace commit() and rollback() --- src/core/chuck_compile.cpp | 6 +- src/core/chuck_scan.cpp | 19 ++++--- src/core/chuck_type.cpp | 111 +++++++++++++++++++++++++++++++++++-- src/core/chuck_type.h | 13 ++++- 4 files changed, 130 insertions(+), 19 deletions(-) diff --git a/src/core/chuck_compile.cpp b/src/core/chuck_compile.cpp index 56a9d4ce9..434406f15 100644 --- a/src/core/chuck_compile.cpp +++ b/src/core/chuck_compile.cpp @@ -1110,9 +1110,11 @@ t_CKBOOL Chuck_Compiler::compile_single( Chuck_CompileTarget * target ) EM_setCurrentTarget( NULL ); // commit - if( ret ) env()->global()->commit(); + if( ret ) // 1.5.4.3 (ge) update to call commit_namespaces | was: env()->global()->commit(); + { env()->commit_namespaces(); } // or rollback - else env()->global()->rollback(); + else // 1.5.4.3 (ge) update to call commit_namespaces | was: env()->global()->rollback(); + { env()->rollback_namespaces(); } // unload the context from the type-checker if( !type_engine_unload_context( env() ) ) diff --git a/src/core/chuck_scan.cpp b/src/core/chuck_scan.cpp index 158c80787..3e19b54a6 100644 --- a/src/core/chuck_scan.cpp +++ b/src/core/chuck_scan.cpp @@ -197,7 +197,8 @@ t_CKBOOL type_engine_scan0_prog( Chuck_Env * env, a_Program prog, // NOTE: for legacy reasons, fallback to [global] if no [user] namespace // this fallback mechanism was moved out of env->user() to here for clarity in 1.5.4.0 (ge) // needs further verification, as this fallback may not be necessary - if( !prog->section->class_def->home ) prog->section->class_def->home = env->global(); + if( !prog->section->class_def->home ) + { prog->section->class_def->home = env->global(); } // ----------------------------------------- } // scan the class definition @@ -307,7 +308,7 @@ t_CKBOOL type_engine_scan0_class_def( Chuck_Env * env, a_Class_Def class_def ) // name | 1.5.2.0 (ge) added the_class->nspc->pre_ctor->name = string("class ") + the_class->base_name; // add to env - env->curr->type.add( the_class->base_name, the_class ); // URGENT: make this global + env->curr->add_type( the_class->base_name, the_class ); // URGENT: make this global // incomplete the_class->is_complete = FALSE; @@ -370,7 +371,7 @@ t_CKBOOL type_engine_scan0_class_def( Chuck_Env * env, a_Class_Def class_def ) value->is_const = TRUE; value->is_member = FALSE; // add to env - env->curr->value.add( the_class->base_name, value ); + env->curr->add_value( the_class->base_name, value ); // remember class_def->type = the_class; @@ -2663,7 +2664,7 @@ t_CKBOOL type_engine_scan2_exp_decl_create( Chuck_Env * env, a_Exp_Decl decl ) } // enter into value binding - env->curr->value.add( var_decl->xid, + env->curr->add_value( S_name(var_decl->xid), value = env->context->new_Chuck_Value( type, S_name(var_decl->xid) ) ); // remember the owner @@ -3241,7 +3242,7 @@ t_CKBOOL type_engine_scan2_func_def( Chuck_Env * env, a_Func_Def f ) // later: add as value // symbols.push_back( arg_list ); // values.push_back( v ); - // later: env->curr->value.add( arg_list->var_decl->xid, v ); + // later: env->curr->add_value( arg_list->var_decl->xid, v ); // stack v->offset = f->stack_depth; @@ -3394,15 +3395,15 @@ t_CKBOOL type_engine_scan2_func_def( Chuck_Env * env, a_Func_Def f ) } // add as value - env->curr->value.add( value->name, value ); + env->curr->add_value( value->name, value ); // enter the name into the function table - env->curr->func.add( func->name, func ); + env->curr->add_func( func->name, func ); // if not overload, add entries for orig name if( !overload ) { - env->curr->value.add( orig_name, value ); - env->curr->func.add( orig_name, func ); + env->curr->add_value( orig_name, value ); + env->curr->add_func( orig_name, func ); } // set the current function to this diff --git a/src/core/chuck_type.cpp b/src/core/chuck_type.cpp index 39c7b735f..06921b29e 100644 --- a/src/core/chuck_type.cpp +++ b/src/core/chuck_type.cpp @@ -903,9 +903,11 @@ t_CKBOOL type_engine_check_prog( Chuck_Env * env, a_Program prog, cleanup: // commit - if( ret ) env->global()->commit(); + if( ret ) // 1.5.4.3 (ge) update to env->commit_namespaces() | was: env->global()->commit(); + { env->commit_namespaces(); } // or rollback - else env->global()->rollback(); + else // 1.5.4.3 (ge) update to env->commit_namespaces() | was: env->global()->rollback(); + { env->rollback_namespaces(); } // unload the context from the type-checker if( !type_engine_unload_context( env ) ) @@ -4406,7 +4408,7 @@ t_CKTYPE type_engine_check_exp_decl_part2( Chuck_Env * env, a_Exp_Decl decl ) if( !env->class_def || env->class_scope > 0 ) { // add as value - env->curr->value.add( var_decl->xid, value ); + env->curr->add_value( S_name(var_decl->xid), value ); } // the next var decl @@ -5568,7 +5570,7 @@ t_CKBOOL type_engine_check_func_def( Chuck_Env * env, a_Func_Def f ) } // add as value - env->curr->value.add( arg_list->var_decl->xid, v ); + env->curr->add_value( S_name(arg_list->var_decl->xid), v ); // increment count count++; @@ -5655,6 +5657,51 @@ Chuck_Namespace::~Chuck_Namespace() +//----------------------------------------------------------------------------- +// name: add_type() +// desc: add type to name space +//----------------------------------------------------------------------------- +void Chuck_Namespace::add_type( const std::string & xid, Chuck_Type * type ) +{ + // log it + EM_log( CK_LOG_DEBUG, "namespace '%s' adding type '%s'->'%s'", this->name.c_str(), xid.c_str(), type->name().c_str() ); + // add it + this->type.add( xid, type ); +} + + + + +//----------------------------------------------------------------------------- +// name: add_value() +// desc: add value to name space +//----------------------------------------------------------------------------- +void Chuck_Namespace::add_value( const std::string & xid, Chuck_Value * value ) +{ + // log it + EM_log( CK_LOG_DEBUG, "namespace '%s' adding value '%s'->'%s'", this->name.c_str(), xid.c_str(), value->name.c_str() ); + // add it + this->value.add( xid, value ); +} + + + + +//----------------------------------------------------------------------------- +// name: add_func() +// desc: add type to name space +//----------------------------------------------------------------------------- +void Chuck_Namespace::add_func( const std::string & xid, Chuck_Func * func ) +{ + // log it + EM_log( CK_LOG_DEBUG, "namespace '%s' adding func '%s'->'%s'", this->name.c_str(), xid.c_str(), func->base_name.c_str() ); + // add it + this->func.add( xid, func ); +} + + + + //----------------------------------------------------------------------------- // name: lookup_value() // desc: lookup value in the env; climb means to climb the scope and... @@ -6859,7 +6906,7 @@ Chuck_Type * type_engine_import_class_begin( Chuck_Env * env, Chuck_Type * type, value->is_member = FALSE; // add to env - where->value.add( value->name, value ); + where->add_value( value->name, value ); // make the type current env->nspc_stack.push_back( env->curr ); @@ -6912,7 +6959,7 @@ Chuck_Type * type_engine_import_class_begin( Chuck_Env * env, const char * name, type = new Chuck_Type( env, te_user, name, parent, sizeof(void *) ); // add to namespace - TODO: handle failure, remove from where - where->type.add( name, type ); + where->add_type( name, type ); // do the rest if( !type_engine_import_class_begin( env, type, where, pre_ctor, dtor, doc ) ) @@ -8082,6 +8129,58 @@ Chuck_Type * Chuck_Env::get_array_type( Chuck_Type * array_parent, +//----------------------------------------------------------------------------- +// name: commit_namespaces() +// desc: commit namespace | 1.5.4.3 (ge) added +//----------------------------------------------------------------------------- +void Chuck_Env::commit_namespaces() +{ + // get current + Chuck_Namespace * nspc = this->curr; + // while not null and above user() + while( nspc != NULL && nspc != this->user() && nspc != this->global() ) + { + // commit + nspc->commit(); + // move up to parent + nspc = nspc->parent; + } + + // if user, explicitly commit user + if( this->user() ) this->user()->commit(); + // explicitly commit global + this->global()->commit(); +} + + + + +//----------------------------------------------------------------------------- +// name: rollback_namespaces() +// desc: commit namespace | 1.5.4.3 (ge) added +//----------------------------------------------------------------------------- +void Chuck_Env::rollback_namespaces() +{ + // get current + Chuck_Namespace * nspc = this->curr; + // while not null and above user() + while( nspc != NULL && nspc != this->user() && nspc != this->global() ) + { + // rollback + nspc->rollback(); + // move up to parent + nspc = nspc->parent; + } + + // if user, explicitly rollback user + if( this->user() ) this->user()->rollback(); + // explicitly rollback global + this->global()->rollback(); +} + + + + //----------------------------------------------------------------------------- // operator overload (for map) //----------------------------------------------------------------------------- diff --git a/src/core/chuck_type.h b/src/core/chuck_type.h index 54213cf94..41550435e 100644 --- a/src/core/chuck_type.h +++ b/src/core/chuck_type.h @@ -334,6 +334,11 @@ struct Chuck_Namespace : public Chuck_VM_Object // destructor virtual ~Chuck_Namespace(); + // add type to name space + void add_type( const std::string & xid, Chuck_Type * target ); + void add_value( const std::string & xid, Chuck_Value * target ); + void add_func( const std::string & xid, Chuck_Func * target ); + // look up value Chuck_Value * lookup_value( const std::string & name, t_CKINT climb = 1, t_CKBOOL stayWithinClassDef = FALSE ); Chuck_Value * lookup_value( S_Symbol name, t_CKINT climb = 1, t_CKBOOL stayWithinClassDef = FALSE ); @@ -346,13 +351,13 @@ struct Chuck_Namespace : public Chuck_VM_Object // commit the maps void commit() { - EM_log( CK_LOG_FINER, "committing namespace: '%s'...", name.c_str() ); + EM_log( CK_LOG_DEBUG, "namespace: '%s' committing...", name.c_str() ); type.commit(); value.commit(); func.commit(); } // rollback the maps void rollback() { - EM_log( CK_LOG_FINER, "rolling back namespace: '%s'...", name.c_str() ); + EM_log( CK_LOG_DEBUG, "namespace: '%s' rolling back...", name.c_str() ); type.rollback(); value.rollback(); func.rollback(); } @@ -752,6 +757,10 @@ struct Chuck_Env : public Chuck_VM_Object Chuck_Namespace * nspc_top(); // get type at top of type stack Chuck_Type * class_top(); + // commit namespaces + void commit_namespaces(); + // rollback namespace + void rollback_namespaces(); public: // REFACTOR-2017: carrier and accessors