static void DMap_SortMethods( DMap *hash, DList *methods ) { DMap *map = DMap_New( DAO_DATA_STRING, 0 ); DString *name = DString_New(); DNode *it; daoint i, n; for(it=DMap_First(hash); it; it=DMap_Next(hash,it)){ if( it->value.pRoutine->overloads ){ DRoutines *one = it->value.pRoutine->overloads; for(i=0,n=one->routines->size; i<n; i++){ DaoRoutine *rout = one->routines->items.pRoutine[i]; DString_Assign( name, rout->routName ); DString_AppendChars( name, " " ); DString_Append( name, rout->routType->name ); DMap_Insert( map, name, (void*)rout ); } }else{ DaoRoutine *rout = it->value.pRoutine; DString_Assign( name, rout->routName ); DString_AppendChars( name, " " ); DString_Append( name, rout->routType->name ); DMap_Insert( map, name, (void*)rout ); } } DList_Clear( methods ); for(it=DMap_First(map); it; it=DMap_Next(map,it)) DList_Append( methods, it->value.pVoid ); DMap_Delete( map ); DString_Delete( name ); }
void DaoValue_MarkConst( DaoValue *self ) { DMap *map; DNode *it; daoint i, n; if( self == NULL || (self->xBase.trait & DAO_VALUE_CONST) ) return; self->xBase.trait |= DAO_VALUE_CONST; switch( self->type ){ case DAO_LIST : for(i=0,n=self->xList.items.size; i<n; i++) DaoValue_MarkConst( self->xList.items.items.pValue[i] ); break; case DAO_TUPLE : for(i=0,n=self->xTuple.size; i<n; i++) DaoValue_MarkConst( self->xTuple.items[i] ); break; case DAO_MAP : map = self->xMap.items; for(it=DMap_First( map ); it != NULL; it = DMap_Next(map, it) ){ DaoValue_MarkConst( it->key.pValue ); DaoValue_MarkConst( it->value.pValue ); } break; case DAO_OBJECT : n = self->xObject.defClass->instvars->size; for(i=1; i<n; i++) DaoValue_MarkConst( self->xObject.objValues[i] ); if( self->xObject.parent ) DaoValue_MarkConst( self->xObject.parent ); break; default : break; } }
void DaoCinType_DeriveMethods( DaoCinType *self ) { daoint i, k, m, N = self->supers->size; DaoCinType *super; DNode *it; for(i=0; i<N; i++){ super = (DaoCinType*) self->supers->items.pValue[i]; self->citype->bases = DList_New( DAO_DATA_VALUE ); self->vatype->bases = DList_New( DAO_DATA_VALUE ); DList_Append( self->citype->bases, super->citype ); DList_Append( self->vatype->bases, super->vatype ); for(it=DMap_First(super->methods); it; it=DMap_Next( super->methods, it )){ if( it->value.pRoutine->overloads ){ DRoutines *routs = it->value.pRoutine->overloads; for(k=0,m=routs->routines->size; k<m; k++){ DaoRoutine *rout = routs->routines->items.pRoutine[i]; DaoMethods_Insert( self->methods, rout, NULL, self->vatype ); } }else{ DaoMethods_Insert( self->methods, it->value.pRoutine, NULL, self->vatype ); } } } self->derived = 1; }
/* // Only activate one event per channel: */ void DaoChannel_ActivateEvent( DaoChannel *self, int type ) { DaoCallServer *server = daoCallServer; DNode *node; daoint i; for(i=0; i<server->events2->size; ++i){ DaoTaskEvent *event = (DaoTaskEvent*) server->events2->items.pVoid[i]; if( event->type != type ) continue; if( DaoCallServer_CheckEvent( event, NULL, self ) ){ DList_Append( server->events, event ); DList_Erase( server->events2, i, 1 ); return; } } for(node=DMap_First(server->waitings); node; node=DMap_Next(server->waitings,node)){ DaoTaskEvent *event = (DaoTaskEvent*) node->value.pValue; if( event->type != type ) continue; if( DaoCallServer_CheckEvent( event, NULL, self ) ){ DList_Append( server->events, event ); DMap_EraseNode( server->waitings, node ); return; } } }
void DaoxDataFrame_AddLabels( DaoxDataFrame *self, int dim, DMap *labels ) { DString *lab; DNode *it; if( labels->keytype != D_STRING && labels->keytype != D_VALUE ) return; if( labels->valtype != 0 && labels->valtype != D_VALUE ) return; lab = DString_New(1); DaoxDataFrame_AddLabelGroup( self, dim ); for(it=DMap_First(labels); it; it=DMap_Next(labels,it)){ DString *lab2 = it->key.pString; daoint idx = it->value.pInt; if( labels->keytype == D_VALUE ){ if( it->key.pValue->type != DAO_STRING ) continue; lab2 = it->key.pValue->xString.data; } if( labels->valtype == D_VALUE ){ if( it->value.pValue->type != DAO_INTEGER ) continue; idx = it->value.pValue->xInteger.value; } if( idx < 0 ) continue; DString_Reset( lab, 0 ); DString_Append( lab, lab2 ); DaoxDataFrame_AddLabel( self, dim, lab->mbs, idx ); } DString_Delete( lab ); }
/* // Activate all events waiting on a future value: */ void DaoFuture_ActivateEvent( DaoFuture *self ) { DaoCallServer *server = daoCallServer; DList *array = DList_New(0); DNode *node; daoint i; DMutex_Lock( & server->mutex ); for(i=0; i<server->events2->size; ++i){ DaoTaskEvent *event = (DaoTaskEvent*) server->events2->items.pVoid[i]; if( DaoCallServer_CheckEvent( event, self, NULL ) ){ event->state = DAO_EVENT_RESUME; DList_Append( server->events, event ); DList_Erase( server->events2, i, 1 ); i -= 1; } } for(node=DMap_First(server->waitings); node; node=DMap_Next(server->waitings,node)){ DaoTaskEvent *event = (DaoTaskEvent*) node->value.pValue; /* remove from timed waiting list: */ if( DaoCallServer_CheckEvent( event, self, NULL ) ){ event->state = DAO_EVENT_RESUME; DList_Append( server->events, event ); DList_Append( array, node->key.pVoid ); } } for(i=0; i<array->size; i++) DMap_Erase( server->waitings, array->items.pVoid[i] ); DCondVar_Signal( & server->condv ); DMutex_Unlock( & server->mutex ); DList_Delete( array ); }
static void DaoCallServer_Timer( void *p ) { DaoCallServer *server = daoCallServer; double time = 0.0; daoint i, timeout; server->timing = 1; while( server->finishing == 0 || server->stopped != server->total ){ DMutex_Lock( & server->mutex ); while( server->waitings->size == 0 ){ if( server->idle == server->total && server->events2->size ){ DaoCallServer_ActivateEvents(); } if( server->finishing && server->stopped == server->total ) break; DCondVar_TimedWait( & server->condv2, & server->mutex, 0.01 ); } if( server->waitings->size ){ DNode *node = DMap_First( server->waitings ); time = node->key.pValue->xComplex.value.real; time -= Dao_GetCurrentTime(); /* wait the right amount of time for the closest arriving timeout: */ if( time > 0 ) DCondVar_TimedWait( & server->condv2, & server->mutex, time ); } DMutex_Unlock( & server->mutex ); if( server->finishing && server->stopped == server->total ) break; DMutex_Lock( & server->mutex ); if( server->waitings->size ){ /* a new wait timed out: */ DNode *node = DMap_First( server->waitings ); time = Dao_GetCurrentTime(); if( node->key.pValue->xComplex.value.real < time ){ DaoTaskEvent *event = (DaoTaskEvent*) node->value.pVoid; event->state = DAO_EVENT_RESUME; event->timeout = 1; event->expiring = MIN_TIME; DArray_Append( server->events, node->value.pVoid ); DMap_EraseNode( server->waitings, node ); } } DCondVar_Signal( & server->condv ); DMutex_Unlock( & server->mutex ); } server->timing = 0; }
static DString* DaoClass_GetDataName( DaoClass *self, int st, int id ) { DNode *it; for(it=DMap_First(self->lookupTable); it; it=DMap_Next(self->lookupTable,it)){ if( st == LOOKUP_ST(it->value.pInt) && id == LOOKUP_ID(it->value.pInt) ){ return it->value.pString; } } return NULL; }
static void DaoMT_RoutMutexSet( DMap *mutexes ) { DNode *it; for(it=DMap_First(mutexes); it; it=DMap_Next(mutexes,it)){ DMutex *mutex = (DMutex*) it->value.pVoid; DMutex_Destroy( mutex ); dao_free( mutex ); } DMap_Delete( mutexes ); }
dao_complex DaoProfiler_Sum( DMap *profile ) { DNode *it2; dao_complex com = {0.0, 0.0}; for(it2=DMap_First(profile); it2; it2=DMap_Next(profile,it2)){ DaoRoutine *caller = (DaoRoutine*) it2->key.pValue; DaoComplex *data = (DaoComplex*) it2->value.pValue; com.real += data->value.real; com.imag += data->value.imag; } return com; }
void DaoxDataFrame_GetLabel( DaoxDataFrame *self, int dim, int group, daoint i, DString *lab ) { DMap *labels = self->labels[dim]->items.pMap[group]; DNode *it; DString_Reset( lab, 0 ); for(it=DMap_First(labels); it; it=DMap_Next(labels,it)){ if( it->value.pInt == i ){ DString_Append( lab, it->key.pString ); break; } } }
void DaoValue_SetType( DaoValue *to, DaoType *tp ) { DaoVmSpace *vms; DaoType *tp2; DNode *it; if( to->type != tp->tid && tp->tid != DAO_ANY ) return; if( tp->attrib & DAO_TYPE_SPEC ) return; switch( to->type ){ #ifdef DAO_WITH_NUMARRAY case DAO_ARRAY : if( to->xArray.size ) return; if( tp->tid != DAO_ARRAY || tp->args == NULL || tp->args->size == 0 ) break; tp = tp->args->items.pType[0]; if( tp->tid == DAO_NONE || tp->tid > DAO_COMPLEX ) break; DaoArray_SetNumType( (DaoArray*) to, tp->tid ); break; #endif case DAO_LIST : /* var x: any = {}, x->ctype should be list<any> */ if( tp->tid == DAO_ANY ){ vms = DaoType_GetVmSpace( tp ); tp = vms->typeListAny; } if( to->xList.ctype && !(to->xList.ctype->attrib & DAO_TYPE_UNDEF) ) break; GC_Assign( & to->xList.ctype, tp ); break; case DAO_MAP : if( tp->tid == DAO_ANY ){ vms = DaoType_GetVmSpace( tp ); tp = vms->typeMapAny; } if( to->xMap.ctype && !(to->xMap.ctype->attrib & DAO_TYPE_UNDEF) ) break; GC_Assign( & to->xMap.ctype, tp ); break; case DAO_TUPLE : tp2 = to->xTuple.ctype; if( tp->tid == DAO_ANY ) break; if( tp->args->size ==0 ) break; /* not to the generic tuple type */ if( tp2 == NULL || tp2->mapNames == NULL || tp2->mapNames->size ==0 ){ GC_Assign( & to->xTuple.ctype, tp ); break; } if( tp->mapNames == NULL || tp->mapNames->size ) break; for(it=DMap_First(tp2->mapNames); it!=NULL; it=DMap_Next(tp2->mapNames, it)){ if( DMap_Find( tp->mapNames, it->key.pVoid ) == NULL ) break; } if( it ) break; GC_Assign( & to->xTuple.ctype, tp ); break; default : break; } }
void DaoClass_PrintCode( DaoClass *self, DaoStream *stream ) { DNode *node = DMap_First( self->lookupTable ); DaoStream_WriteMBS( stream, "class " ); DaoStream_WriteString( stream, self->className ); DaoStream_WriteMBS( stream, ":\n" ); for( ; node != NULL; node = DMap_Next( self->lookupTable, node ) ){ DaoValue *val; if( LOOKUP_ST( node->value.pInt ) != DAO_CLASS_CONSTANT ) continue; val = self->constants->items.pConst[ LOOKUP_ID( node->value.pInt ) ]->value; if( val->type == DAO_ROUTINE && val->xRoutine.body ) DaoRoutine_PrintCode( & val->xRoutine, stream ); } }
void DaoValue_SetType( DaoValue *to, DaoType *tp ) { DaoType *tp2; DNode *it; if( to->type != tp->tid && tp->tid != DAO_ANY ) return; if( tp->attrib & DAO_TYPE_SPEC ) return; switch( to->type ){ #ifdef DAO_WITH_NUMARRAY case DAO_ARRAY : if( to->xArray.size ) return; if( tp->tid != DAO_ARRAY || tp->nested == NULL || tp->nested->size == 0 ) break; tp = tp->nested->items.pType[0]; if( tp->tid == DAO_NONE || tp->tid > DAO_COMPLEX ) break; DaoArray_SetNumType( (DaoArray*) to, tp->tid ); break; #endif case DAO_LIST : /* v : any = {}, v->unitype should be list<any> */ if( tp->tid == DAO_ANY ) tp = dao_list_any; if( to->xList.unitype && !(to->xList.unitype->attrib & DAO_TYPE_UNDEF) ) break; GC_ShiftRC( tp, to->xList.unitype ); to->xList.unitype = tp; break; case DAO_MAP : if( tp->tid == DAO_ANY ) tp = dao_map_any; if( to->xMap.unitype && !(to->xMap.unitype->attrib & DAO_TYPE_UNDEF) ) break; GC_ShiftRC( tp, to->xMap.unitype ); to->xMap.unitype = tp; break; case DAO_TUPLE : tp2 = to->xTuple.unitype; if( tp->tid == DAO_ANY ) break; if( tp->nested->size ==0 ) break; /* not to the generic tuple type */ if( tp2 == NULL || tp2->mapNames == NULL || tp2->mapNames->size ==0 ){ GC_ShiftRC( tp, to->xTuple.unitype ); to->xTuple.unitype = tp; break; } if( tp->mapNames == NULL || tp->mapNames->size ) break; for(it=DMap_First(tp2->mapNames); it!=NULL; it=DMap_Next(tp2->mapNames, it)){ if( DMap_Find( tp->mapNames, it->key.pVoid ) == NULL ) break; } if( it ) break; GC_ShiftRC( tp, to->xTuple.unitype ); to->xTuple.unitype = tp; break; default : break; } }
static int DaoValue_TryCastTuple( DaoValue *src, DaoValue **dest, DaoType *tp ) { DaoTuple *tuple; DaoType **item_types = tp->args->items.pType; DaoType *totype = src->xTuple.ctype; DaoValue **data = src->xTuple.values; DMap *names = totype ? totype->mapNames : NULL; DNode *node, *search; daoint i, T = tp->args->size; int tm, eqs = 0; /* // Auto-cast tuple type, on the following conditions: // (1) the item values of "dest" must match exactly to the item types of "tp"; // (2) "tp->mapNames" must contain "(*dest)->xTuple.ctype->mapNames"; */ if( src->xTuple.ctype == NULL ){ GC_IncRC( tp ); src->xTuple.ctype = tp; return 1; } if( DaoType_MatchValue( tp, src, NULL ) < DAO_MT_SIM ) return 1; /* Redundant? */ /* // Casting is not necessary if the tuple's field names are a superset of the // field names of the target type: */ if( tp->mapNames == NULL || tp->mapNames->size ==0 ) goto Finalize; if( names ){ daoint count = 0; for(node=DMap_First(names); node; node=DMap_Next(names,node)){ search = DMap_Find( tp->mapNames, node->key.pVoid ); if( search && node->value.pInt != search->value.pInt ) return 0; count += search != NULL; } /* be superset of the field names of the target type: */ if( count == tp->mapNames->size ) goto Finalize; } Finalize: tuple = DaoTuple_New( T ); for(i=0; i<T; i++){ DaoType *it = item_types[i]; if( it->tid == DAO_PAR_NAMED ) it = & it->aux->xType; DaoValue_Move( data[i], tuple->values+i, it ); } GC_IncRC( tp ); tuple->ctype = tp; GC_Assign( dest, tuple ); return 1; }
DaoCinType* DaoInterface_GetConcrete( DaoInterface *self, DaoType *type ) { DNode *it; if( self->concretes == NULL ) return NULL; it = DMap_Find( self->concretes, type ); if( it == NULL ){ for(it=DMap_First(self->concretes); it; it=DMap_Next(self->concretes,it)){ if( DaoType_MatchTo( it->key.pType, type, NULL ) >= DAO_MT_EQ ){ return (DaoCinType*) it->value.pVoid; } } return NULL; } return (DaoCinType*) it->value.pVoid; }
static void DaoMT_RunMapFunctional( void *p ) { DaoValue *res; DaoInteger tidint = {DAO_INTEGER,0,0,0,0,0}; DaoValue *threadid = (DaoValue*)(void*)&tidint; DaoTaskData *self = (DaoTaskData*)p; DaoMap *map = (DaoMap*) self->param; DaoList *list2 = (DaoList*) self->result; DaoProcess *clone = self->clone; DaoVmCode *sect = self->sect; DaoType *type = map->ctype; DNode *node = NULL; daoint i = 0; DaoMT_InitProcess( self->proto, clone, 3 ); tidint.value = self->first; type = type && type->nested->size > 1 ? type->nested->items.pType[1] : NULL; for(node=DMap_First( map->value ); node; node=DMap_Next(map->value, node) ){ if( (i++) % self->step != self->first ) continue; if( sect->b >0 ) DaoProcess_SetValue( clone, sect->a, node->key.pValue ); if( sect->b >1 ) DaoProcess_SetValue( clone, sect->a+1, node->value.pValue ); if( sect->b >2 ) DaoProcess_SetValue( clone, sect->a+2, threadid ); clone->topFrame->entry = self->entry; DaoProcess_Execute( clone ); if( clone->status != DAO_PROCESS_FINISHED ) break; res = clone->stackValues[0]; if( self->funct == DVM_FUNCT_MAP ){ self->status |= DaoList_SetItem( list2, res, i-1 ); }else if( self->funct == DVM_FUNCT_APPLY ){ self->status |= DaoValue_Move( res, & node->value.pValue, type ) == 0; }else if( self->funct == DVM_FUNCT_FIND ){ DNode **p = self->node; /* XXX: 2014-11-11 */ if( *p && DaoValue_Compare( (*p)->key.pValue, node->key.pValue ) < 0 ) break; if( res->xInteger.value ){ DMutex_Lock( self->mutex ); if( *p == NULL || DaoValue_Compare( (*p)->key.pValue, node->key.pValue ) >0 ) *p = node; DMutex_Unlock( self->mutex ); break; } } } }
static DaoValue* DaoValue_DeepCopy( DaoValue *self ) { DNode *it; daoint i; if( self == NULL ) return NULL; if( self->type <= DAO_ENUM ) return self; /* simple types will be copied at use; */ if( self->type == DAO_ARRAY ) return (DaoValue*) DaoArray_Copy( (DaoArray*) self ); if( self->type == DAO_LIST ){ DaoList *list = (DaoList*) self; DaoList *copy = DaoList_New(); GC_ShiftRC( list->unitype, copy->unitype ); copy->unitype = list->unitype; for(i=0; i<list->items.size; ++i){ DaoValue *value = DaoValue_DeepCopy( list->items.items.pValue[i] ); DaoList_Append( copy, value ); } return (DaoValue*) copy; }else if( self->type == DAO_MAP ){ DaoMap *map = (DaoMap*) self; DaoMap *copy = DaoMap_New( map->items->hashing ); GC_ShiftRC( map->unitype, copy->unitype ); copy->unitype = map->unitype; for(it=DMap_First(map->items); it; it=DMap_Next(map->items,it)){ DaoValue *key = DaoValue_DeepCopy( it->key.pValue ); DaoValue *value = DaoValue_DeepCopy( it->value.pValue ); DaoMap_Insert( copy, key, value ); } return (DaoValue*) copy; }else if( self->type == DAO_TUPLE ){ DaoTuple *tuple = (DaoTuple*) self; DaoTuple *copy = DaoTuple_New( tuple->size ); GC_ShiftRC( tuple->unitype, copy->unitype ); copy->unitype = tuple->unitype; for(i=0; i<tuple->size; ++i){ DaoValue *value = DaoValue_DeepCopy( tuple->items[i] ); DaoTuple_SetItem( copy, value, i ); } return (DaoValue*) copy; } return NULL; }
static int DaoMap_Serialize( DaoMap *self, DString *serial, DaoNamespace *ns, DaoProcess *proc, DString *buf, DMap *omap ) { DaoType *type = self->unitype; DaoType *keytype = NULL; DaoType *valtype = NULL; DNode *node; char *sep = self->items->hashing ? ":" : "=>"; int i = 0, rc = 1; if( type->nested && type->nested->size >0 ) keytype = type->nested->items.pType[0]; if( type->nested && type->nested->size >1 ) valtype = type->nested->items.pType[1]; if( keytype && (keytype->tid == 0 || keytype->tid >= DAO_ENUM)) keytype = NULL; if( valtype && (valtype->tid == 0 || valtype->tid >= DAO_ENUM)) valtype = NULL; for(node=DMap_First(self->items); node; node=DMap_Next(self->items,node)){ DaoType *kt = NULL, *vt = NULL; if( keytype == NULL ) kt = DaoNamespace_GetType( ns, node->key.pValue ); if( valtype == NULL ) vt = DaoNamespace_GetType( ns, node->value.pValue ); if( (i++) ) DString_AppendChar( serial, ',' ); rc &= DaoValue_Serialize2( node->key.pValue, serial, ns, proc, kt, buf, omap ); DString_AppendMBS( serial, sep ); rc &= DaoValue_Serialize2( node->value.pValue, serial, ns, proc, vt, buf, omap ); } return rc; }
static void NS_Backup( DaoNamespace *self, DaoProcess *proc, FILE *fout, int limit, int store ) { DNode *node = DMap_First( self->lookupTable ); DString *prefix = DString_New(1); DString *serial = DString_New(1); DaoValue *value = NULL; size_t max = limit * 1000; /* limit per object in KB */ int id, pm, up, st; for( ; node !=NULL; node = DMap_Next( self->lookupTable, node ) ){ DString *name = node->key.pString; id = node->value.pInt; up = LOOKUP_UP( id ); st = LOOKUP_ST( id ); pm = LOOKUP_PM( id ); if( up ) continue; if( st != store ) continue; if( st == DAO_GLOBAL_CONSTANT ) value = DaoNamespace_GetConst( self, id ); if( st == DAO_GLOBAL_VARIABLE ) value = DaoNamespace_GetVariable( self, id ); if( value == NULL ) continue; if( DaoValue_Serialize( value, serial, self, proc ) ==0 ) continue; prefix->size = 0; switch( pm ){ case DAO_DATA_PRIVATE : DString_AppendMBS( prefix, "private " ); break; case DAO_DATA_PROTECTED : DString_AppendMBS( prefix, "protected " ); break; case DAO_DATA_PUBLIC : DString_AppendMBS( prefix, "public " ); break; } switch( st ){ case DAO_GLOBAL_CONSTANT : DString_AppendMBS( prefix, "const " ); break; case DAO_GLOBAL_VARIABLE : DString_AppendMBS( prefix, "var " ); break; } if( max && prefix->size + name->size + serial->size + 4 > max ) continue; fprintf( fout, "%s%s = %s\n", prefix->mbs, name->mbs, serial->mbs ); } DString_Delete( prefix ); DString_Delete( serial ); }
int DaoEnum_Compare( DaoEnum *L, DaoEnum *R ) { DaoEnum E; DNode *N = NULL; DMap *ML = L->etype->mapNames; DMap *MR = R->etype->mapNames; uchar_t FL = L->etype->flagtype; uchar_t FR = R->etype->flagtype; char SL = L->etype->name->mbs[0]; char SR = R->etype->name->mbs[0]; if( L->etype == R->etype ){ return L->value == R->value ? 0 : ( L->value < R->value ? -1 : 1 ); }else if( SL == '$' && SR == '$' && FL == 0 && FR == 0 ){ return DString_Compare( L->etype->name, R->etype->name ); }else if( SL == '$' && SR == '$' ){ if( L->etype->mapNames->size != R->etype->mapNames->size ){ return number_compare( L->etype->mapNames->size, R->etype->mapNames->size ); }else{ for(N=DMap_First(ML);N;N=DMap_Next(ML,N)){ if( DMap_Find( MR, N->key.pVoid ) ==0 ) return -1; } return 0; } }else if( SL == '$' ){ E.etype = R->etype; E.value = R->value; DaoEnum_SetValue( & E, L, NULL ); return E.value == R->value ? 0 : ( E.value < R->value ? -1 : 1 ); }else if( SR == '$' ){ E.etype = L->etype; E.value = L->value; DaoEnum_SetValue( & E, R, NULL ); return L->value == E.value ? 0 : ( L->value < E.value ? -1 : 1 ); }else{ return L->value == R->value ? 0 : ( L->value < R->value ? -1 : 1 ); } }
void DaoInterface_DeriveMethods( DaoInterface *self ) { daoint i, k, m, N = self->bases->size; DaoNamespace *ns = self->nameSpace; DaoInterface *base; DNode *it; for(i=0; i<N; i++){ base = (DaoInterface*) self->bases->items.pValue[i]; if( self->abtype->bases == NULL ) self->abtype->bases = DList_New( DAO_DATA_VALUE ); DList_Append( self->abtype->bases, base->abtype ); for(it=DMap_First(base->methods); it; it=DMap_Next( base->methods, it )){ if( it->value.pRoutine->overloads ){ DRoutines *routs = it->value.pRoutine->overloads; for(k=0,m=routs->routines->size; k<m; k++){ DaoRoutine *rout = routs->routines->items.pRoutine[i]; DaoMethods_Insert( self->methods, rout, ns, self->abtype ); } }else{ DaoMethods_Insert( self->methods, it->value.pRoutine, ns, self->abtype ); } } } self->derived = 1; }
int DaoParser_ParseMacro( DaoParser *self, int start ) { int rb1, rb2, i = start, N = self->tokens->size; DaoToken **toks = self->tokens->items.pToken; DMacroUnit *first; DaoMacro *macro; DString *lang = NULL; DArray *stops; DMap *markers; DNode *it; if( start + 5 >= N ) return -1; if( toks[start+1]->type != DTOK_LCB ){ lang = & toks[start+1]->string; if( toks[start+1]->type != DTOK_IDENTIFIER ){ DaoParser_Error( self, DAO_TOKEN_NEED_NAME, lang ); return -1; } if( lang->size == 3 && strcmp( lang->mbs, "dao" ) == 0 ){ DaoParser_Error( self, DAO_TOKEN_NEED_NAME, lang ); return -1; } start += 1; } if( toks[start+1]->name != DTOK_LCB ) return -1; self->curLine = toks[start]->line; rb1 = DaoParser_FindPairToken( self, DTOK_LCB, DTOK_RCB, start, -1 ); if( rb1 <0 || rb1 +3 >= N ) return -1; if( toks[rb1+1]->name != DKEY_AS || toks[rb1+2]->name != DTOK_LCB ){ DaoParser_Error( self, DAO_CTW_INV_MAC_DEFINE, NULL ); return -1; } rb2 = DaoParser_FindPairToken( self, DTOK_LCB, DTOK_RCB, rb1 + 1, -1 ); if( rb2 <0 ) return -1; /* for( i=start; i<rb2; i++ ) printf( "%s ", toks[i]->string.mbs ); printf("\n"); */ macro = DaoMacro_New(); markers = DMap_New(D_STRING,0); if( DaoParser_MakeMacroGroup( self, macro->macroMatch, macro->macroMatch, start+2, rb1, markers, NULL ) ==0 ){ goto Error; } if( macro->macroMatch->units->size == 0 ) goto Error; first = (DMacroUnit*) macro->macroMatch->units->items.pVoid[0]; if( first->type != DMACRO_TOK ){ DaoParser_Error( self, DAO_CTW_INV_MAC_FIRSTOK, & toks[i]->string ); goto Error; } for(it=DMap_First(markers); it; it=DMap_Next(markers,it)){ if( it->value.pInt > 1 ){ DaoParser_Error( self, DAO_CTW_REDEF_MAC_MARKER, it->key.pString ); goto Error; } } DMap_Clear( markers ); if( toks[rb1+3]->line != toks[rb1+2]->line ) macro->macroApply->cpos = toks[rb1+3]->cpos; if( DaoParser_MakeMacroGroup( self, macro->macroApply, macro->macroApply, rb1+3, rb2, NULL, markers ) ==0 ){ goto Error; } for(it=DMap_First(markers); it; it=DMap_Next(markers,it)){ DArray_Append( macro->keyListApply, it->key.pString ); } stops = DArray_New(D_TOKEN); DMacroGroup_SetStop( macro->macroMatch, stops ); DMacroGroup_FindVariables( macro->macroMatch ); DArray_Clear( stops ); DMacroGroup_SetStop( macro->macroApply, stops ); DaoNamespace_AddMacro( self->nameSpace, lang, & first->marker->string, macro ); DArray_Delete( stops ); DMap_Delete( markers ); return rb2; Error: DaoMacro_Delete( macro ); DMap_Delete( markers ); return -1; }
int DaoClass_CopyField( DaoClass *self, DaoClass *other, DMap *deftypes ) { DaoNamespace *ns = other->classRoutine->nameSpace; DaoType *tp; DArray *offsets = DArray_New(0); DArray *routines = DArray_New(0); DNode *it; int i, k, st, up, id; for(i=0; i<other->superClass->size; i++){ DaoValue *sup = other->superClass->items.pValue[i]; if( sup->type == DAO_CLASS && sup->xClass.typeHolders ){ tp = DaoType_DefineTypes( sup->xClass.objType, ns, deftypes ); DArray_Append( self->superClass, tp->aux ); }else if( sup->type == DAO_CTYPE && sup->xCtype.ctype->typer->core->kernel->sptree ){ tp = DaoType_DefineTypes( sup->xCtype.ctype, ns, deftypes ); DArray_Append( self->superClass, tp->aux ); }else{ DArray_Append( self->superClass, sup ); } } DaoRoutine_CopyFields( self->classRoutine, other->classRoutine, 1, 1 ); for(it=DMap_First(other->lookupTable);it;it=DMap_Next(other->lookupTable,it)){ st = LOOKUP_ST( it->value.pInt ); up = LOOKUP_UP( it->value.pInt ); id = LOOKUP_ID( it->value.pInt ); if( up ==0 ){ if( st == DAO_CLASS_CONSTANT && id <self->constants->size ) continue; if( st == DAO_CLASS_VARIABLE && id <self->variables->size ) continue; if( st == DAO_OBJECT_VARIABLE && id <self->instvars->size ) continue; } DMap_Insert( self->lookupTable, it->key.pVoid, it->value.pVoid ); } for(i=self->objDataName->size; i<other->objDataName->size; i++) DArray_Append( self->objDataName, other->objDataName->items.pString[i] ); for(i=self->cstDataName->size; i<other->cstDataName->size; i++) DArray_Append( self->cstDataName, other->cstDataName->items.pString[i] ); for(i=self->glbDataName->size; i<other->glbDataName->size; i++) DArray_Append( self->glbDataName, other->glbDataName->items.pString[i] ); for(i=self->variables->size; i<other->variables->size; i++){ DaoVariable *var = other->variables->items.pVar[i]; var = DaoVariable_New( var->value, DaoType_DefineTypes( var->dtype, ns, deftypes ) ); DArray_Append( self->variables, var ); } for(i=self->instvars->size; i<other->instvars->size; i++){ DaoVariable *var = other->instvars->items.pVar[i]; var = DaoVariable_New( var->value, DaoType_DefineTypes( var->dtype, ns, deftypes ) ); DArray_Append( self->instvars, var ); /* TODO fail checking */ } for(i=self->constants->size; i<other->constants->size; i++){ DaoValue *value = other->constants->items.pConst[i]->value; DaoRoutine *rout = & value->xRoutine; if( value->type == DAO_ROUTINE && rout->overloads == NULL && rout->routHost == other->objType ){ DString *name = rout->routName; rout = DaoRoutine_Copy( rout, 1, 1 ); value = (DaoValue*) rout; k = DaoRoutine_Finalize( rout, self->objType, deftypes ); #if 0 printf( "%i %p: %s %s\n", i, rout, rout->routName->mbs, rout->routType->name->mbs ); #endif if( rout->attribs & DAO_ROUT_INITOR ){ DRoutines_Add( self->classRoutines->overloads, rout ); }else if( (it = DMap_Find( other->lookupTable, name )) ){ st = LOOKUP_ST( it->value.pInt ); up = LOOKUP_UP( it->value.pInt ); id = LOOKUP_ID( it->value.pInt ); if( st == DAO_CLASS_CONSTANT && up ==0 && id < i ){ DaoValue *v = self->constants->items.pConst[id]->value; if( v->type == DAO_ROUTINE && v->xRoutine.overloads ) DRoutines_Add( v->xRoutine.overloads, rout ); } } DArray_Append( self->constants, DaoConstant_New( value ) ); DArray_Append( routines, rout ); if( k == 0 ) goto Failed; continue; }else if( value->type == DAO_ROUTINE ){ /* No need to added the overloaded routines now; */ /* Each of them has an entry in constants, and will be handled later: */ DaoRoutine *routs = DaoRoutines_New( ns, self->objType, NULL ); DArray_Append( self->constants, DaoConstant_New( (DaoValue*) routs ) ); continue; } DArray_Append( self->constants, DaoConstant_New( value ) ); DaoValue_Update( & self->constants->items.pConst[i]->value, ns, deftypes ); } for(i=0; i<routines->size; i++){ if( DaoRoutine_DoTypeInference( routines->items.pRoutine[i], 0 ) == 0 ) goto Failed; } DArray_Delete( offsets ); DArray_Delete( routines ); DaoRoutine_Finalize( self->classRoutine, self->objType, deftypes ); return DaoRoutine_DoTypeInference( self->classRoutine, 0 ); Failed: DArray_Delete( offsets ); DArray_Delete( routines ); return 0; }
int DaoParser_MacroTransform( DaoParser *self, DaoMacro *macro, int start, int tag ) { DString *mbs = DString_New(1); DArray *toks = DArray_New(D_TOKEN); DArray *all = DArray_New(0); DMap *tokMap = DMap_New(D_STRING,0); DMap *used = DMap_New(0,D_MAP); DNode *it; daoint i; int j, p0, lev = 0, adjust=0; int indent[10]; char buf[20]; sprintf( buf, " %p %x", self->nameSpace, tag ); DString_SetMBS( mbs, buf ); for(i=0; i<10; i++) indent[i] = -1; j = DaoParser_MacroMatch( self, start, self->tokens->size, macro->macroMatch, tokMap, lev, all, indent ); /* printf( "MacroTransform %i\n", j ); */ if( j <0 ) goto Failed; for( it = DMap_First( tokMap ); it != NULL; it = DMap_Next( tokMap, it ) ){ DMacroNode *node = (DMacroNode*) it->value.pVoid; while( node->parent ) node = node->parent; it->value.pVoid = node; } lev = 0; p0 = self->tokens->items.pToken[start]->line; adjust = self->tokens->items.pToken[start]->cpos - macro->macroApply->cpos; if( DaoParser_MacroApply( self, toks, macro->macroApply, tokMap, used, lev, mbs, p0, adjust ) <0 ) goto Failed; /* for(i=0; i<toks->size; i++) printf( "%s ", toks->items.pToken[i]->string.mbs ); printf( "\n" ); */ DArray_Erase( self->tokens, start, j-start ); DArray_InsertArray( self->tokens, start, toks, 0, -1 ); /* for(i=0; i<toks->size; i++){ DArray_Insert( self->tokStr, (void*)toks->items.pString[i], start+i ); DArray_Insert( self->tokPos, (void*)poss->items.pInt[i], start+i ); } */ j = toks->size; DString_Delete( mbs ); for(i=0; i<all->size; i++) DMacroNode_Delete( (DMacroNode*) all->items.pVoid[i] ); DArray_Delete( all ); DArray_Delete( toks ); DMap_Delete( tokMap ); DMap_Delete( used ); return start + j; Failed : DString_Delete( mbs ); for(i=0; i<all->size; i++) DMacroNode_Delete( (DMacroNode*) all->items.pVoid[i] ); DArray_Delete( all ); DArray_Delete( toks ); DMap_Delete( tokMap ); DMap_Delete( used ); return -1; }
/* assumed to be called before parsing class body */ void DaoClass_DeriveClassData( DaoClass *self ) { DaoType *type; DaoValue *value; DArray *parents, *offsets; DString *mbs; DNode *it, *search; daoint i, k, id, perm, index; mbs = DString_New(1); if( self->clsType->bases == NULL ) self->clsType->bases = DArray_New(D_VALUE); if( self->objType->bases == NULL ) self->objType->bases = DArray_New(D_VALUE); DArray_Clear( self->clsType->bases ); DArray_Clear( self->objType->bases ); for(i=0; i<self->superClass->size; i++){ if( self->superClass->items.pValue[i]->type == DAO_CLASS ){ DaoValue *klass = self->superClass->items.pValue[i]; DArray_Append( self->clsType->bases, klass->xClass.clsType ); DArray_Append( self->objType->bases, klass->xClass.objType ); DString_Assign( mbs, klass->xClass.className ); DString_AppendChar( mbs, '#' ); DString_AppendInteger( mbs, i+1 ); DaoClass_AddConst( self, mbs, klass, DAO_DATA_PRIVATE ); }else if( self->superClass->items.pValue[i]->type == DAO_CTYPE ){ DaoCtype *cdata = (DaoCtype*) self->superClass->items.pValue[i]; DaoTypeKernel *kernel = cdata->ctype->kernel; DMap *values = kernel->values; DMap *methods = kernel->methods; DArray_Append( self->clsType->bases, cdata->ctype ); DArray_Append( self->objType->bases, cdata->cdtype ); if( values == NULL ){ DaoNamespace_SetupValues( kernel->nspace, kernel->typer ); values = kernel->values; } if( methods == NULL ){ DaoNamespace_SetupMethods( kernel->nspace, kernel->typer ); methods = kernel->methods; } DString_Assign( mbs, cdata->ctype->name ); // XXX DaoClass_AddConst( self, mbs, (DaoValue*)cdata, DAO_DATA_PRIVATE ); DString_AppendChar( mbs, '#' ); DString_AppendInteger( mbs, i+1 ); DaoClass_AddConst( self, mbs, (DaoValue*)cdata, DAO_DATA_PRIVATE ); } } parents = DArray_New(0); offsets = DArray_New(0); DaoClass_Parents( self, parents, offsets ); for(i=1; i<parents->size; i++){ DaoClass *klass = parents->items.pClass[i]; DaoCdata *cdata = parents->items.pCdata[i]; if( klass->type == DAO_CLASS ){ if( klass->vtable ){ if( self->vtable == NULL ) self->vtable = DHash_New(0,0); for(it=DMap_First(klass->vtable); it; it=DMap_Next(klass->vtable,it)){ DMap_Insert( self->vtable, it->key.pVoid, it->value.pVoid ); } } /* For class data: */ for( id=0; id<klass->cstDataName->size; id++ ){ DString *name = klass->cstDataName->items.pString[id]; value = klass->constants->items.pConst[ id ]->value; search = MAP_Find( klass->lookupTable, name ); if( search == NULL ) continue; perm = LOOKUP_PM( search->value.pInt ); /* NO deriving private member: */ if( perm <= DAO_DATA_PRIVATE ) continue; if( value->type == DAO_ROUTINE ){ if( DString_EQ( value->xRoutine.routName, klass->className ) ) continue; } search = MAP_Find( self->lookupTable, name ); if( search == NULL && value->type == DAO_ROUTINE && value->xRoutine.overloads ){ /* To skip the private methods: */ DaoClass_AddConst( self, name, (DaoValue*)value, perm ); }else if( search == NULL ){ index = LOOKUP_BIND( DAO_CLASS_CONSTANT, perm, i+1, self->constants->size ); MAP_Insert( self->lookupTable, name, index ); DArray_Append( self->constants, klass->constants->items.pConst[id] ); DArray_Append( self->cstDataName, (void*)name ); if( value->type == DAO_ROUTINE && (value->xRoutine.attribs & DAO_ROUT_VIRTUAL) ){ if( self->vtable == NULL ) self->vtable = DHash_New(0,0); MAP_Insert( self->vtable, value, value ); } }else if( value->type == DAO_ROUTINE && value->xRoutine.overloads ){ DRoutines *routs = value->xRoutine.overloads; for(k=0; k<routs->routines->size; k++){ DaoRoutine *rout = routs->routines->items.pRoutine[k]; /* skip methods not defined in this parent type */ if( rout->routHost != klass->objType ) continue; if( rout->attribs & DAO_ROUT_PRIVATE ) continue; DaoClass_AddConst( self, name, (DaoValue*)rout, perm ); } }else if( value->type == DAO_ROUTINE ){ /* skip methods not defined in this parent type */ if( value->xRoutine.routHost != klass->objType ) continue; if( value->xRoutine.attribs & DAO_ROUT_PRIVATE ) continue; DaoClass_AddConst( self, name, value, perm ); } } /* class global data */ for( id=0; id<klass->glbDataName->size; id ++ ){ DString *name = klass->glbDataName->items.pString[id]; DaoVariable *var = klass->variables->items.pVar[id]; search = MAP_Find( klass->lookupTable, name ); perm = LOOKUP_PM( search->value.pInt ); /* NO deriving private member: */ if( perm <= DAO_DATA_PRIVATE ) continue; search = MAP_Find( self->lookupTable, name ); /* To overide data: */ if( search == NULL ){ index = LOOKUP_BIND( DAO_CLASS_VARIABLE, perm, i+1, self->variables->size ); MAP_Insert( self->lookupTable, name, index ); DArray_Append( self->variables, var ); DArray_Append( self->glbDataName, (void*)name ); } } }else if( cdata->type == DAO_CTYPE ){ DaoCtype *ctypeobj = (DaoCtype*) cdata; DaoTypeKernel *kernel = cdata->ctype->kernel; DaoTypeBase *typer = kernel->typer; DMap *values = kernel->values; DMap *methods = kernel->methods; DNode *it; int j; DaoCdataType_SpecializeMethods( cdata->ctype ); kernel = cdata->ctype->kernel; methods = kernel->methods; if( typer->numItems ){ for(j=0; typer->numItems[j].name!=NULL; j++){ DString name = DString_WrapMBS( typer->numItems[j].name ); it = DMap_Find( values, & name ); if( it && DMap_Find( self->lookupTable, & name ) == NULL ) DaoClass_AddConst( self, it->key.pString, it->value.pValue, DAO_DATA_PUBLIC ); } } for(it=DMap_First( methods ); it; it=DMap_Next( methods, it )){ DaoRoutine *func = it->value.pRoutine; DaoRoutine **funcs = & func; int k, count = 1; if( it->value.pValue->type == DAO_ROUTINE && it->value.pRoutine->overloads ){ DRoutines *routs = it->value.pRoutine->overloads; funcs = routs->routines->items.pRoutine; count = routs->routines->size; } for(k=0; k<count; k++){ DaoRoutine *func = funcs[k]; if( func->routHost != ctypeobj->cdtype ) continue; if( func->attribs & DAO_ROUT_INITOR ) continue; DaoClass_AddConst( self, it->key.pString, (DaoValue*)func, DAO_DATA_PUBLIC ); } } } } DArray_Delete( parents ); DArray_Delete( offsets ); DString_Delete( mbs ); }
void DaoProfiler_Report( DaoProfiler *self0, DaoStream *stream ) { DaoComplex com = {DAO_COMPLEX,0,0,0,1,{0.0,0.0}}; DaoxProfiler *self = (DaoxProfiler*) self0; DMap *summary = DMap_New( DAO_DATA_VALUE, 0 ); DMap *summary2 = DMap_New( DAO_DATA_VALUE, 0 ); DString *name1 = DString_New(); DString *name2 = DString_New(); DNode *it, *it2; int count, max = 20; char buf1[32]; char buf2[24]; char buf[120]; for(it=DMap_First(self->profile); it; it=DMap_Next(self->profile,it)){ DaoRoutine *callee = (DaoRoutine*) it->key.pValue; com.value = DaoProfiler_Sum( it->value.pMap ); com.value.real = - com.value.real; com.value.imag = - com.value.imag; DMap_Insert( summary, & com, it ); } DaoStream_WriteChars( stream, "\n" ); DaoStream_WriteChars( stream, delimiter ); DaoStream_WriteChars( stream, delimiter2 ); snprintf( buf, sizeof(buf), header_format, "Routine", "#Calls", "CPU Time" ); DaoStream_WriteChars( stream, buf ); DaoStream_WriteChars( stream, delimiter2 ); for(count=max,it=DMap_First(summary); it; it=DMap_Next(summary,it),--count){ DNode *it2 = (DNode*) it->value.pVoid; dao_complex data = it->key.pValue->xComplex.value; DaoRoutine *callee = (DaoRoutine*) it2->key.pValue; DaoRoutine_MakeName( callee, name1, 28, 10, 2 ); snprintf( buf, sizeof(buf), row_format, name1->chars, (int) -data.imag, -data.real ); DaoStream_WriteChars( stream, buf ); if( count == 0 ) break; } DaoStream_WriteChars( stream, "\n" ); DaoStream_WriteChars( stream, delimiter3 ); snprintf( buf, sizeof(buf), header_format2, "Routine", "Caller", "#Calls", "CPU Time" ); DaoStream_WriteChars( stream, buf ); DaoStream_WriteChars( stream, delimiter3 ); for(count=max,it=DMap_First(summary); it; it=DMap_Next(summary,it),--count){ DNode *it2 = (DNode*) it->value.pVoid; DaoRoutine *callee = (DaoRoutine*) it2->key.pValue; DMap *profile = it2->value.pMap; DaoRoutine_MakeName( callee, name1, 30, 0, 0 ); DMap_Reset( summary2 ); for(it2=DMap_First(profile); it2; it2=DMap_Next(profile,it2)){ DaoRoutine *caller = (DaoRoutine*) it2->key.pValue; DaoComplex *data = (DaoComplex*) it2->value.pValue; com.value.real = - data->value.real; com.value.imag = - data->value.imag; DMap_Insert( summary2, & com, caller ); } for(it2=DMap_First(summary2); it2; it2=DMap_Next(summary2,it2)){ DaoRoutine *caller = (DaoRoutine*) it2->value.pValue; dao_complex data = it2->key.pValue->xComplex.value; DString_Reset( name2, 0 ); if( caller ) DaoRoutine_MakeName( caller, name2, 22, 0, 0 ); snprintf( buf, sizeof(buf), row_format2, name1->chars, name2->chars, (int) -data.imag, -data.real ); DaoStream_WriteChars( stream, buf ); DString_Reset( name1, 0 ); } if( count == 0 ) break; } DString_Delete( name1 ); DString_Delete( name2 ); }
DaoxScene* DaoxResource_GetScene( DaoxResource *self ) { if( self->scenes->size == 0 ) return NULL; return (DaoxScene*) DMap_First( self->scenes )->value.pVoid; }
/* // The layout of mixins in a host class: // 1. Each mixin occupies a continuous segment in the data arrays of the host. // The ranges of the segments are stored in DaoClass::ranges; // 2. If the mixin contains other mixins, those mixins occupy segments preceding // the segment for this mixin; // 3. The segments for the direct mixins of the host are arranged in the same // order as the mixins; // // For example, there are the following mixins: // class AA { var x = 1 } // class BB { var x = 1 } // class CC ( AA, BB ) { var x = 1 } // class DD ( CC, AA ) { var x = 1 } // The mixin layout for CC: // CC_Header, AA_Header, AA_Data, BB_Header, BB_Data, CC_Data // The mixin layout for DD: // DD_Header, AA_Header, AA_Data, BB_Header, BB_Data, CC_Header, CC_Data, DD_Data // // Where XX_Header are the data fields that are always placed at the header // of the data array. For example, XX_Header for class constants contains // two fields: one for the class itself, the other for the class constructor(s); // XX_Header for class static variables is empty; and XX_Header for class // instance variables contains only the field for the "self" variable. // And XX_Data constains the mixin's own data which are not from its // component mixins or from its paraent classes (actually only classes // without parent classes can be used as mixins). // // // To mix a mixin in the host class, the mixin (and its component mixins if any) // are properly arranged in the host class with layouts described above. // The non-trivial part is the update of variable types and the methods // that are added to the host class from the mixin. To update the types, // the type for the mixin are all replaced with the type for the host class. // // The update of methods involves three major steps: // 1. Update the routine signature type, local variable types and the static // variable types; // 2. Update the lookup table of the host class for the data from the mixins, // which is done by mapping the indices for the mixin data arrays to the // indices for the host data arrays; // 3. Update the method code (VM instructions) such that operands involving // class or class instance data are properly mapped from the indices for // the mixin data arrays to the indices for the host data arrays. */ static int DaoClass_MixIn( DaoClass *self, DaoClass *mixin, DMap *mixed, DaoMethodFields *mf ) { daoint i, j, k, id, bl = 1; DaoNamespace *ns = self->classRoutine->nameSpace; DArray *routines; DMap *deftypes; DMap *routmap; DMap *idmap; DNode *it; if( mixin->parent != NULL ) return 0; if( DMap_Find( mixed, mixin ) != NULL ) return 1; /* Mix the component mixins first: */ for(i=0; i<mixin->mixinBases->size; ++i){ DaoClass *mx = mixin->mixinBases->items.pClass[i]; bl = bl && DaoClass_MixIn( self, mx, mixed, mf ); } if( bl == 0 ) return 0; idmap = DMap_New(0,0); routmap = DMap_New(0,0); deftypes = DMap_New(0,0); routines = DArray_New(0); DMap_Insert( mixed, mixin, idmap ); DMap_Delete( idmap ); idmap = DMap_Find( mixed, mixin )->value.pMap; /* Add this mixin to the mixin list for both direct and indirect mixins: */ DArray_Append( self->mixins, mixin ); /* Save the starts of the ranges for this mixin in the host class: */ DVector_PushUshort( self->ranges, self->constants->size ); DVector_PushUshort( self->ranges, self->variables->size ); DVector_PushUshort( self->ranges, self->instvars->size ); /* For updating the types for the mixin to the types for the host class: */ DMap_Insert( deftypes, mixin->clsType, self->clsType ); DMap_Insert( deftypes, mixin->objType, self->objType ); #if 0 printf( "MixIn: %s %p %i\n", mixin->className->mbs, mixin, mixin->cstDataName->size ); #endif /* Add the own constants of the mixin to the host class: */ for(i=0; i<mixin->cstDataName->size; ++i){ daoint src = LOOKUP_BIND( DAO_CLASS_CONSTANT, 0, 0, i ); daoint des = LOOKUP_BIND( DAO_CLASS_CONSTANT, 0, 0, self->constants->size ); DString *name = mixin->cstDataName->items.pString[i]; DaoValue *value = mixin->constants->items.pConst[i]->value; DaoRoutine *rout = (DaoRoutine*) value; if( i >= mixin->cstMixinStart && i < mixin->cstMixinEnd2 ) continue; MAP_Insert( idmap, src, des ); /* Setup index mapping; */ DArray_Append( self->cstDataName, (void*) name ); if( value->type != DAO_ROUTINE || rout->routHost != mixin->objType ){ DaoConstant *cst = DaoConstant_New( value ); DArray_Append( self->constants, cst ); continue; } if( rout->overloads == NULL ){ DaoRoutine *old = rout; rout = DaoRoutine_Copy( rout, 1, 1, 1 ); bl = bl && DaoRoutine_Finalize( rout, self->objType, deftypes ); #if 0 printf( "%2i: %s %s\n", i, rout->routName->mbs, rout->routType->name->mbs ); #endif /* // Do not use DaoClass_AddConst() here, so that the original // method overloading structures will be mantained, without // interference from methods of other mixin component classes // or of the host class. */ it = DMap_Find( routmap, old ); if( it ) DRoutines_Add( it->value.pRoutine->overloads, rout ); DArray_Append( self->constants, DaoConstant_New( (DaoValue*) rout ) ); DArray_Append( routines, rout ); if( bl == 0 ) goto Finalize; }else{ /* No need to added the overloaded routines now; */ /* Each of them has an entry in constants, and will be handled later: */ DaoRoutine *routs = DaoRoutines_New( ns, self->objType, NULL ); routs->trait |= DAO_VALUE_CONST; DArray_Append( self->constants, DaoConstant_New( (DaoValue*) routs ) ); for(j=0; j<rout->overloads->routines->size; ++j){ DaoRoutine *R = rout->overloads->routines->items.pRoutine[j]; DMap_Insert( routmap, R, routs ); } } } for(i=mixin->glbMixinEnd2; i<mixin->glbDataName->size; ++i){ daoint src = LOOKUP_BIND( DAO_CLASS_VARIABLE, 0, 0, i ); daoint des = LOOKUP_BIND( DAO_CLASS_VARIABLE, 0, 0, self->variables->size ); DString *name = mixin->glbDataName->items.pString[i]; DaoValue *var = mixin->variables->items.pVar[i]->value; DaoType *type = mixin->variables->items.pVar[i]->dtype; type = DaoType_DefineTypes( type, ns, deftypes ); MAP_Insert( idmap, src, des ); DArray_Append( self->glbDataName, (void*) name ); DArray_Append( self->variables, DaoVariable_New( var, type ) ); } for(i=mixin->objMixinEnd2; i<mixin->objDataName->size; ++i){ daoint src = LOOKUP_BIND( DAO_OBJECT_VARIABLE, 0, 0, i ); daoint des = LOOKUP_BIND( DAO_OBJECT_VARIABLE, 0, 0, self->instvars->size ); DString *name = mixin->objDataName->items.pString[i]; DaoValue *var = mixin->instvars->items.pVar[i]->value; DaoType *type = mixin->instvars->items.pVar[i]->dtype; type = DaoType_DefineTypes( type, ns, deftypes ); MAP_Insert( idmap, src, des ); DArray_Append( self->objDataName, (void*) name ); DArray_Append( self->instvars, DaoVariable_New( var, type ) ); } /* Find the ends of own data of this mixin: */ DVector_PushUshort( self->ranges, self->constants->size ); DVector_PushUshort( self->ranges, self->variables->size ); DVector_PushUshort( self->ranges, self->instvars->size ); /* Update the lookup table: */ for(it=DMap_First(mixin->lookupTable); it; it=DMap_Next(mixin->lookupTable,it)){ int pm = LOOKUP_PM( it->value.pInt ); int st = LOOKUP_ST( it->value.pInt ); int up = LOOKUP_UP( it->value.pInt ); int id = LOOKUP_ID( it->value.pInt ); DaoValue *cst; /* Skip names from component mixins (because they have been handled): */ switch( st ){ case DAO_CLASS_CONSTANT : if( id >= mixin->cstMixinStart && id < mixin->cstMixinEnd2 ) continue; break; case DAO_CLASS_VARIABLE : if( id >= mixin->glbMixinStart && id < mixin->glbMixinEnd2 ) continue; break; case DAO_OBJECT_VARIABLE : if( id >= mixin->objMixinStart && id < mixin->objMixinEnd2 ) continue; break; } if( st != DAO_OBJECT_VARIABLE || id != 0 ){ /* not a "self": */ DNode *it2 = MAP_Find( idmap, LOOKUP_BIND( st, 0, 0, id ) ); if( it2 ) id = LOOKUP_ID( it2->value.pInt ); /* map index; */ } MAP_Insert( self->lookupTable, it->key.pString, LOOKUP_BIND( st, pm, up, id ) ); if( st != DAO_CLASS_CONSTANT ) continue; cst = self->constants->items.pConst[id]->value; if( cst->type != DAO_ROUTINE ) continue; DArray_Append( mf->names, it->key.pString ); DArray_Append( mf->perms, IntToPointer( pm ) ); DArray_Append( mf->routines, cst ); } for(i=0; i<routines->size; i++){ DaoRoutine *rout = routines->items.pRoutine[i]; DaoType **types; if( rout->body == NULL ) continue; //DaoRoutine_PrintCode( rout, rout->nameSpace->vmSpace->stdioStream ); types = rout->body->regType->items.pType; for(j=0; j<rout->body->annotCodes->size; ++j){ DaoVmCodeX *vmc = rout->body->annotCodes->items.pVmc[j]; DaoClass *klass; DString *name; switch( vmc->code ){ case DVM_GETCK: case DVM_GETCK_I: case DVM_GETCK_F: case DVM_GETCK_D: case DVM_GETCK_C: vmc->b = DaoClass_MapIndex( mixin, DAO_CLASS_CONSTANT, vmc->b, mixed ); break; case DVM_GETVK: case DVM_GETVK_I: case DVM_GETVK_F: case DVM_GETVK_D: case DVM_GETVK_C: case DVM_SETVK: case DVM_SETVK_II: case DVM_SETVK_FF: case DVM_SETVK_DD: case DVM_SETVK_CC: vmc->b = DaoClass_MapIndex( mixin, DAO_CLASS_VARIABLE, vmc->b, mixed ); break; case DVM_GETVO: case DVM_GETVO_I: case DVM_GETVO_F: case DVM_GETVO_D: case DVM_GETVO_C: case DVM_SETVO: case DVM_SETVO_II: case DVM_SETVO_FF: case DVM_SETVO_DD: case DVM_SETVO_CC: vmc->b = DaoClass_MapIndex( mixin, DAO_OBJECT_VARIABLE, vmc->b, mixed ); break; case DVM_GETF_KC: case DVM_GETF_KCI: case DVM_GETF_KCF: case DVM_GETF_KCD: case DVM_GETF_KCC: case DVM_GETF_OC: case DVM_GETF_OCI: case DVM_GETF_OCF: case DVM_GETF_OCD: case DVM_GETF_OCC: klass = (DaoClass*) types[ vmc->a ]->aux; name = DaoClass_GetDataName( klass, DAO_CLASS_CONSTANT, vmc->b ); vmc->b = DaoRoutine_GetFieldIndex( rout, name ); vmc->code = DVM_GETF; break; case DVM_GETF_KG: case DVM_GETF_KGI: case DVM_GETF_KGF: case DVM_GETF_KGD: case DVM_GETF_KGC: case DVM_GETF_OG: case DVM_GETF_OGI: case DVM_GETF_OGF: case DVM_GETF_OGD: case DVM_GETF_OGC: klass = (DaoClass*) types[ vmc->a ]->aux; name = DaoClass_GetDataName( klass, DAO_CLASS_VARIABLE, vmc->b ); vmc->b = DaoRoutine_GetFieldIndex( rout, name ); vmc->code = DVM_GETF; break; case DVM_GETF_OV: case DVM_GETF_OVI: case DVM_GETF_OVF: case DVM_GETF_OVD: case DVM_GETF_OVC: klass = (DaoClass*) types[ vmc->a ]->aux; name = DaoClass_GetDataName( klass, DAO_OBJECT_VARIABLE, vmc->b ); vmc->b = DaoRoutine_GetFieldIndex( rout, name ); vmc->code = DVM_GETF; break; case DVM_SETF_KG: case DVM_SETF_KGII: case DVM_SETF_KGFF: case DVM_SETF_KGDD: case DVM_SETF_KGCC: case DVM_SETF_OG: case DVM_SETF_OGII: case DVM_SETF_OGFF: case DVM_SETF_OGDD: case DVM_SETF_OGCC: klass = (DaoClass*) types[ vmc->c ]->aux; name = DaoClass_GetDataName( klass, DAO_CLASS_VARIABLE, vmc->b ); vmc->b = DaoRoutine_GetFieldIndex( rout, name ); vmc->code = DVM_SETF; break; case DVM_SETF_OV: case DVM_SETF_OVII: case DVM_SETF_OVFF: case DVM_SETF_OVDD: case DVM_SETF_OVCC: klass = (DaoClass*) types[ vmc->c ]->aux; name = DaoClass_GetDataName( klass, DAO_OBJECT_VARIABLE, vmc->b ); vmc->b = DaoRoutine_GetFieldIndex( rout, name ); vmc->code = DVM_SETF; break; } } //DaoRoutine_PrintCode( rout, rout->nameSpace->vmSpace->stdioStream ); bl = bl && DaoRoutine_DoTypeInference( rout, 0 ); if( bl == 0 ) goto Finalize; } Finalize: DArray_Delete( routines ); DMap_Delete( routmap ); DMap_Delete( deftypes ); return bl; }
/* assumed to be called before parsing class body */ int DaoClass_DeriveClassData( DaoClass *self ) { DaoType *type; DaoValue *value; DString *mbs; DNode *it, *search; DaoMethodFields *mf; daoint i, j, k, id; if( DaoCass_DeriveMixinData( self ) == 0 ) return 0; mbs = DString_New(1); mf = DaoMethodFields_New(); if( self->clsType->bases == NULL ) self->clsType->bases = DArray_New(D_VALUE); if( self->objType->bases == NULL ) self->objType->bases = DArray_New(D_VALUE); DArray_Clear( self->clsType->bases ); DArray_Clear( self->objType->bases ); self->cstParentStart = self->constants->size; self->glbParentStart = self->variables->size; DVector_PushUshort( self->offsets, self->constants->size ); DVector_PushUshort( self->offsets, self->variables->size ); if( self->parent && self->parent->type == DAO_CLASS ){ DaoClass *klass = (DaoClass*) self->parent; DArray_Append( self->clsType->bases, klass->clsType ); DArray_Append( self->objType->bases, klass->objType ); DArray_AppendArray( self->cstDataName, klass->cstDataName ); DArray_AppendArray( self->glbDataName, klass->glbDataName ); for(j=0; j<klass->constants->size; ++j){ DaoValue *cst = klass->constants->items.pConst[j]->value; DArray_Append( self->constants, klass->constants->items.pVoid[j] ); } for(j=0; j<klass->variables->size; ++j){ DArray_Append( self->variables, klass->variables->items.pVoid[j] ); } for(it=DMap_First(klass->lookupTable); it; it=DMap_Next(klass->lookupTable,it)){ daoint pm = LOOKUP_PM( it->value.pInt ); daoint st = LOOKUP_ST( it->value.pInt ); daoint up = LOOKUP_UP( it->value.pInt ); daoint id = LOOKUP_ID( it->value.pInt ); DaoValue *cst; if( st == DAO_CLASS_CONSTANT ){ id = LOOKUP_ID( it->value.pInt ); cst = klass->constants->items.pConst[id]->value; if( cst->type == DAO_ROUTINE ){ DArray_Append( mf->names, it->key.pString ); DArray_Append( mf->perms, IntToPointer( pm ) ); DArray_Append( mf->routines, cst ); } } if( st == DAO_OBJECT_VARIABLE ) continue; if( pm == DAO_DATA_PRIVATE ) continue; if( DMap_Find( self->lookupTable, it->key.pString ) ) continue; switch( st ){ case DAO_CLASS_CONSTANT : id += self->cstParentStart; break; case DAO_CLASS_VARIABLE : id += self->glbParentStart; break; case DAO_OBJECT_VARIABLE : continue; } id = LOOKUP_BIND( st, pm, up+1, id ); DMap_Insert( self->lookupTable, it->key.pString, (void*)id ); } }else if( self->parent && self->parent->type == DAO_CTYPE ){ DaoCtype *cdata = (DaoCtype*) self->parent; DaoTypeKernel *kernel = cdata->ctype->kernel; DaoTypeBase *typer = kernel->typer; DMap *methods = kernel->methods; DMap *values = kernel->values; DArray_Append( self->clsType->bases, cdata->ctype ); DArray_Append( self->objType->bases, cdata->cdtype ); DaoClass_AddConst( self, cdata->ctype->name, (DaoValue*)cdata, DAO_DATA_PUBLIC ); if( kernel->SetupValues ) kernel->SetupValues( kernel->nspace, kernel->typer ); if( kernel->SetupMethods ) kernel->SetupMethods( kernel->nspace, kernel->typer ); DaoCdataType_SpecializeMethods( cdata->ctype ); kernel = cdata->ctype->kernel; values = kernel->values; methods = kernel->methods; if( typer->numItems ){ for(j=0; typer->numItems[j].name!=NULL; j++){ DString name = DString_WrapMBS( typer->numItems[j].name ); it = DMap_Find( values, & name ); if( it == NULL ) continue; if( DMap_Find( self->lookupTable, it->key.pString ) ) continue; id = self->constants->size; id = LOOKUP_BIND( DAO_CLASS_CONSTANT, DAO_DATA_PUBLIC, 1, id ); DMap_Insert( self->lookupTable, it->key.pString, IntToPointer( id ) ); DArray_Append( self->cstDataName, it->key.pString ); DArray_Append( self->constants, DaoConstant_New( it->value.pValue ) ); } } for(it=DMap_First( methods ); it; it=DMap_Next( methods, it )){ if( DMap_Find( self->lookupTable, it->key.pString ) ) continue; id = self->constants->size; id = LOOKUP_BIND( DAO_CLASS_CONSTANT, DAO_DATA_PUBLIC, 1, id ); DMap_Insert( self->lookupTable, it->key.pString, IntToPointer( id ) ); DArray_Append( self->cstDataName, it->key.pString ); DArray_Append( self->constants, DaoConstant_New( it->value.pValue ) ); DArray_Append( mf->names, it->key.pString ); DArray_Append( mf->perms, IntToPointer( DAO_DATA_PUBLIC ) ); DArray_Append( mf->routines, it->value.pValue ); } } DaoClass_SetupMethodFields( self, mf ); DaoMethodFields_Delete( mf ); self->cstParentEnd = self->constants->size; self->glbParentEnd = self->variables->size; #if 0 for(j=0; j<self->constants->size; ++j){ DaoValue *value = self->constants->items.pConst[j]->value; DaoRoutine *routine = (DaoRoutine*) value; printf( "%3i: %3i %s\n", j, value->type, self->cstDataName->items.pString[j]->mbs ); if( value->type != DAO_ROUTINE ) continue; printf( "%3i: %3i %s\n", j, value->type, routine->routName->mbs ); if( routine->overloads ){ DArray *routs = routine->overloads->routines; for(k=0; k<routs->size; ++k){ DaoRoutine *rout = routs->items.pRoutine[k]; } }else{ if( routine->attribs & DAO_ROUT_PRIVATE ) continue; } } for(it=DMap_First(self->lookupTable); it; it=DMap_Next(self->lookupTable,it)){ printf( "%s %i\n", it->key.pString->mbs, it->value.pInt ); if( LOOKUP_ST( it->value.pInt ) != DAO_CLASS_CONSTANT ) continue; DaoValue *value = DaoClass_GetConst( self, it->value.pInt ); printf( "%i\n", value->type ); } #endif DString_Delete( mbs ); return 1; }