00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "setup.h"
00041
00042 #if OBJECT_SYSTEM
00043
00044 #include "argacces.h"
00045 #include "classcom.h"
00046 #include "classfun.h"
00047 #include "classinf.h"
00048 #include "envrnmnt.h"
00049 #include "exprnpsr.h"
00050 #include "evaluatn.h"
00051 #include "insfile.h"
00052 #include "insfun.h"
00053 #include "insmngr.h"
00054 #include "insmoddp.h"
00055 #include "insmult.h"
00056 #include "inspsr.h"
00057 #include "lgcldpnd.h"
00058 #include "memalloc.h"
00059 #include "msgcom.h"
00060 #include "msgfun.h"
00061 #include "router.h"
00062 #include "strngrtr.h"
00063 #include "sysdep.h"
00064 #include "utility.h"
00065 #include "commline.h"
00066
00067 #define _INSCOM_SOURCE_
00068 #include "inscom.h"
00069
00070
00071
00072
00073
00074
00075 #define ALL_QUALIFIER "inherit"
00076
00077
00078
00079
00080
00081
00082
00083 #if DEBUGGING_FUNCTIONS
00084 static long ListInstancesInModule(void *,int,char *,char *,intBool,intBool);
00085 static long TabulateInstances(void *,int,char *,DEFCLASS *,intBool,intBool);
00086 #endif
00087
00088 static void PrintInstance(void *,char *,INSTANCE_TYPE *,char *);
00089 static INSTANCE_SLOT *FindISlotByName(void *,INSTANCE_TYPE *,char *);
00090 static void DeallocateInstanceData(void *);
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 globle void SetupInstances(
00108 void *theEnv)
00109 {
00110 struct patternEntityRecord instanceInfo = { { "INSTANCE_ADDRESS",
00111 INSTANCE_ADDRESS,0,0,0,
00112 PrintInstanceName,
00113 PrintInstanceLongForm,
00114 EnvUnmakeInstance,
00115 NULL,
00116 EnvGetNextInstance,
00117 EnvDecrementInstanceCount,
00118 EnvIncrementInstanceCount,
00119 NULL,NULL,NULL,NULL,NULL
00120 },
00121 #if DEFRULE_CONSTRUCT && OBJECT_SYSTEM
00122 DecrementObjectBasisCount,
00123 IncrementObjectBasisCount,
00124 MatchObjectFunction,
00125 NetworkSynchronized
00126 #else
00127 NULL,NULL,NULL,NULL
00128 #endif
00129 };
00130
00131 INSTANCE_TYPE dummyInstance = { { NULL, NULL, 0, 0L },
00132 NULL, NULL, 0, 1, 0, 0, 0,
00133 NULL, 0, 0, 0, NULL, NULL, NULL, NULL,
00134 NULL, NULL, NULL, NULL, NULL };
00135
00136 AllocateEnvironmentData(theEnv,INSTANCE_DATA,sizeof(struct instanceData),DeallocateInstanceData);
00137
00138 InstanceData(theEnv)->MkInsMsgPass = TRUE;
00139 memcpy(&InstanceData(theEnv)->InstanceInfo,&instanceInfo,sizeof(struct patternEntityRecord));
00140 dummyInstance.header.theInfo = &InstanceData(theEnv)->InstanceInfo;
00141 memcpy(&InstanceData(theEnv)->DummyInstance,&dummyInstance,sizeof(INSTANCE_TYPE));
00142
00143 InitializeInstanceTable(theEnv);
00144 InstallPrimitive(theEnv,(struct entityRecord *) &InstanceData(theEnv)->InstanceInfo,INSTANCE_ADDRESS);
00145
00146 #if ! RUN_TIME
00147
00148 #if DEFRULE_CONSTRUCT && OBJECT_SYSTEM
00149 EnvDefineFunction2(theEnv,"initialize-instance",'u',
00150 PTIEF InactiveInitializeInstance,"InactiveInitializeInstance",NULL);
00151 EnvDefineFunction2(theEnv,"active-initialize-instance",'u',
00152 PTIEF InitializeInstanceCommand,"InitializeInstanceCommand",NULL);
00153 AddFunctionParser(theEnv,"active-initialize-instance",ParseInitializeInstance);
00154
00155 EnvDefineFunction2(theEnv,"make-instance",'u',PTIEF InactiveMakeInstance,"InactiveMakeInstance",NULL);
00156 EnvDefineFunction2(theEnv,"active-make-instance",'u',PTIEF MakeInstanceCommand,"MakeInstanceCommand",NULL);
00157 AddFunctionParser(theEnv,"active-make-instance",ParseInitializeInstance);
00158
00159 #else
00160 EnvDefineFunction2(theEnv,"initialize-instance",'u',
00161 PTIEF InitializeInstanceCommand,"InitializeInstanceCommand",NULL);
00162 EnvDefineFunction2(theEnv,"make-instance",'u',PTIEF MakeInstanceCommand,"MakeInstanceCommand",NULL);
00163 #endif
00164 AddFunctionParser(theEnv,"initialize-instance",ParseInitializeInstance);
00165 AddFunctionParser(theEnv,"make-instance",ParseInitializeInstance);
00166
00167 EnvDefineFunction2(theEnv,"init-slots",'u',PTIEF InitSlotsCommand,"InitSlotsCommand","00");
00168
00169 EnvDefineFunction2(theEnv,"delete-instance",'b',PTIEF DeleteInstanceCommand,
00170 "DeleteInstanceCommand","00");
00171 EnvDefineFunction2(theEnv,"(create-instance)",'b',PTIEF CreateInstanceHandler,
00172 "CreateInstanceHandler","00");
00173 EnvDefineFunction2(theEnv,"unmake-instance",'b',PTIEF UnmakeInstanceCommand,
00174 "UnmakeInstanceCommand","1*e");
00175
00176 #if DEBUGGING_FUNCTIONS
00177 EnvDefineFunction2(theEnv,"instances",'v',PTIEF InstancesCommand,"InstancesCommand","*3w");
00178 EnvDefineFunction2(theEnv,"ppinstance",'v',PTIEF PPInstanceCommand,"PPInstanceCommand","00");
00179 #endif
00180
00181 EnvDefineFunction2(theEnv,"symbol-to-instance-name",'u',
00182 PTIEF SymbolToInstanceName,"SymbolToInstanceName","11w");
00183 EnvDefineFunction2(theEnv,"instance-name-to-symbol",'w',
00184 PTIEF InstanceNameToSymbol,"InstanceNameToSymbol","11p");
00185 EnvDefineFunction2(theEnv,"instance-address",'u',PTIEF InstanceAddressCommand,
00186 "InstanceAddressCommand","12eep");
00187 EnvDefineFunction2(theEnv,"instance-addressp",'b',PTIEF InstanceAddressPCommand,
00188 "InstanceAddressPCommand","11");
00189 EnvDefineFunction2(theEnv,"instance-namep",'b',PTIEF InstanceNamePCommand,
00190 "InstanceNamePCommand","11");
00191 EnvDefineFunction2(theEnv,"instance-name",'u',PTIEF InstanceNameCommand,
00192 "InstanceNameCommand","11e");
00193 EnvDefineFunction2(theEnv,"instancep",'b',PTIEF InstancePCommand,"InstancePCommand","11");
00194 EnvDefineFunction2(theEnv,"instance-existp",'b',PTIEF InstanceExistPCommand,
00195 "InstanceExistPCommand","11e");
00196 EnvDefineFunction2(theEnv,"class",'u',PTIEF ClassCommand,"ClassCommand","11");
00197
00198 SetupInstanceModDupCommands(theEnv);
00199
00200 SetupInstanceMultifieldCommands(theEnv);
00201
00202 #endif
00203
00204 SetupInstanceFileCommands(theEnv);
00205
00206 AddCleanupFunction(theEnv,"instances",CleanupInstances,0);
00207 EnvAddResetFunction(theEnv,"instances",DestroyAllInstances,60);
00208 }
00209
00210
00211
00212
00213
00214 static void DeallocateInstanceData(
00215 void *theEnv)
00216 {
00217 INSTANCE_TYPE *tmpIPtr, *nextIPtr;
00218 long i;
00219 INSTANCE_SLOT *sp;
00220 IGARBAGE *tmpGPtr, *nextGPtr;
00221 struct patternMatch *theMatch, *tmpMatch;
00222
00223
00224
00225
00226
00227 rm(theEnv,InstanceData(theEnv)->InstanceTable,
00228 (int) (sizeof(INSTANCE_TYPE *) * INSTANCE_TABLE_HASH_SIZE));
00229
00230
00231
00232
00233
00234 tmpIPtr = InstanceData(theEnv)->InstanceList;
00235 while (tmpIPtr != NULL)
00236 {
00237 nextIPtr = tmpIPtr->nxtList;
00238
00239 theMatch = (struct patternMatch *) tmpIPtr->partialMatchList;
00240 while (theMatch != NULL)
00241 {
00242 tmpMatch = theMatch->next;
00243 rtn_struct(theEnv,patternMatch,theMatch);
00244 theMatch = tmpMatch;
00245 }
00246
00247 #if DEFRULE_CONSTRUCT
00248 ReturnEntityDependencies(theEnv,(struct patternEntity *) tmpIPtr);
00249 #endif
00250
00251 for (i = 0 ; i < tmpIPtr->cls->instanceSlotCount ; i++)
00252 {
00253 sp = tmpIPtr->slotAddresses[i];
00254 if ((sp == &sp->desc->sharedValue) ?
00255 (--sp->desc->sharedCount == 0) : TRUE)
00256 {
00257 if (sp->desc->multiple)
00258 { ReturnMultifield(theEnv,(MULTIFIELD_PTR) sp->value); }
00259 }
00260 }
00261
00262 if (tmpIPtr->cls->instanceSlotCount != 0)
00263 {
00264 rm(theEnv,(void *) tmpIPtr->slotAddresses,
00265 (tmpIPtr->cls->instanceSlotCount * sizeof(INSTANCE_SLOT *)));
00266 if (tmpIPtr->cls->localInstanceSlotCount != 0)
00267 {
00268 rm(theEnv,(void *) tmpIPtr->slots,
00269 (tmpIPtr->cls->localInstanceSlotCount * sizeof(INSTANCE_SLOT)));
00270 }
00271 }
00272
00273 rtn_struct(theEnv,instance,tmpIPtr);
00274
00275 tmpIPtr = nextIPtr;
00276 }
00277
00278
00279
00280
00281
00282 tmpGPtr = InstanceData(theEnv)->InstanceGarbageList;
00283 while (tmpGPtr != NULL)
00284 {
00285 nextGPtr = tmpGPtr->nxt;
00286 rtn_struct(theEnv,instance,tmpGPtr->ins);
00287 rtn_struct(theEnv,igarbage,tmpGPtr);
00288 tmpGPtr = nextGPtr;
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 globle intBool EnvDeleteInstance(
00303 void *theEnv,
00304 void *iptr)
00305 {
00306 INSTANCE_TYPE *ins,*itmp;
00307 int success = 1;
00308
00309 if (iptr != NULL)
00310 return(QuashInstance(theEnv,(INSTANCE_TYPE *) iptr));
00311 ins = InstanceData(theEnv)->InstanceList;
00312 while (ins != NULL)
00313 {
00314 itmp = ins;
00315 ins = ins->nxtList;
00316 if (QuashInstance(theEnv,(INSTANCE_TYPE *) itmp) == 0)
00317 success = 0;
00318 }
00319
00320 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00321 (EvaluationData(theEnv)->CurrentExpression == NULL))
00322 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00323
00324 return(success);
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 globle intBool EnvUnmakeInstance(
00336 void *theEnv,
00337 void *iptr)
00338 {
00339 INSTANCE_TYPE *ins;
00340 int success = 1,svmaintain;
00341
00342 svmaintain = InstanceData(theEnv)->MaintainGarbageInstances;
00343 InstanceData(theEnv)->MaintainGarbageInstances = TRUE;
00344 ins = (INSTANCE_TYPE *) iptr;
00345 if (ins != NULL)
00346 {
00347 if (ins->garbage)
00348 success = 0;
00349 else
00350 {
00351 DirectMessage(theEnv,MessageHandlerData(theEnv)->DELETE_SYMBOL,ins,NULL,NULL);
00352 if (ins->garbage == 0)
00353 success = 0;
00354 }
00355 }
00356 else
00357 {
00358 ins = InstanceData(theEnv)->InstanceList;
00359 while (ins != NULL)
00360 {
00361 DirectMessage(theEnv,MessageHandlerData(theEnv)->DELETE_SYMBOL,ins,NULL,NULL);
00362 if (ins->garbage == 0)
00363 success = 0;
00364 ins = ins->nxtList;
00365 while ((ins != NULL) ? ins->garbage : FALSE)
00366 ins = ins->nxtList;
00367 }
00368 }
00369 InstanceData(theEnv)->MaintainGarbageInstances = svmaintain;
00370 CleanupInstances(theEnv);
00371
00372 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00373 (EvaluationData(theEnv)->CurrentExpression == NULL))
00374 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00375
00376 return(success);
00377 }
00378
00379 #if DEBUGGING_FUNCTIONS
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 globle void InstancesCommand(
00391 void *theEnv)
00392 {
00393 int argno, inheritFlag = FALSE;
00394 void *theDefmodule;
00395 char *className = NULL;
00396 DATA_OBJECT temp;
00397
00398 theDefmodule = (void *) EnvGetCurrentModule(theEnv);
00399
00400 argno = EnvRtnArgCount(theEnv);
00401 if (argno > 0)
00402 {
00403 if (EnvArgTypeCheck(theEnv,"instances",1,SYMBOL,&temp) == FALSE)
00404 return;
00405 theDefmodule = EnvFindDefmodule(theEnv,DOToString(temp));
00406 if ((theDefmodule != NULL) ? FALSE :
00407 (strcmp(DOToString(temp),"*") != 0))
00408 {
00409 SetEvaluationError(theEnv,TRUE);
00410 ExpectedTypeError1(theEnv,"instances",1,"defmodule name");
00411 return;
00412 }
00413 if (argno > 1)
00414 {
00415 if (EnvArgTypeCheck(theEnv,"instances",2,SYMBOL,&temp) == FALSE)
00416 return;
00417 className = DOToString(temp);
00418 if (LookupDefclassAnywhere(theEnv,(struct defmodule *) theDefmodule,className) == NULL)
00419 {
00420 if (strcmp(className,"*") == 0)
00421 className = NULL;
00422 else
00423 {
00424 ClassExistError(theEnv,"instances",className);
00425 return;
00426 }
00427 }
00428 if (argno > 2)
00429 {
00430 if (EnvArgTypeCheck(theEnv,"instances",3,SYMBOL,&temp) == FALSE)
00431 return;
00432 if (strcmp(DOToString(temp),ALL_QUALIFIER) != 0)
00433 {
00434 SetEvaluationError(theEnv,TRUE);
00435 ExpectedTypeError1(theEnv,"instances",3,"keyword \"inherit\"");
00436 return;
00437 }
00438 inheritFlag = TRUE;
00439 }
00440 }
00441 }
00442 EnvInstances(theEnv,WDISPLAY,theDefmodule,className,inheritFlag);
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 globle void PPInstanceCommand(
00455 void *theEnv)
00456 {
00457 INSTANCE_TYPE *ins;
00458
00459 if (CheckCurrentMessage(theEnv,"ppinstance",TRUE) == FALSE)
00460 return;
00461 ins = GetActiveInstance(theEnv);
00462 if (ins->garbage == 1)
00463 return;
00464 PrintInstance(theEnv,WDISPLAY,ins,"\n");
00465 EnvPrintRouter(theEnv,WDISPLAY,"\n");
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 globle void EnvInstances(
00482 void *theEnv,
00483 char *logicalName,
00484 void *theVModule,
00485 char *className,
00486 int inheritFlag)
00487 {
00488 int id;
00489 struct defmodule *theModule;
00490 long count = 0L;
00491
00492
00493
00494
00495
00496 if ((id = GetTraversalID(theEnv)) == -1)
00497 return;
00498 SaveCurrentModule(theEnv);
00499
00500
00501
00502
00503
00504 if (theVModule == NULL)
00505 {
00506 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00507 while (theModule != NULL)
00508 {
00509 if (GetHaltExecution(theEnv) == TRUE)
00510 {
00511 RestoreCurrentModule(theEnv);
00512 ReleaseTraversalID(theEnv);
00513 return;
00514 }
00515
00516 EnvPrintRouter(theEnv,logicalName,EnvGetDefmoduleName(theEnv,(void *) theModule));
00517 EnvPrintRouter(theEnv,logicalName,":\n");
00518 EnvSetCurrentModule(theEnv,(void *) theModule);
00519 count += ListInstancesInModule(theEnv,id,logicalName,className,inheritFlag,TRUE);
00520 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,(void *) theModule);
00521 }
00522 }
00523
00524
00525
00526
00527
00528 else
00529 {
00530 EnvSetCurrentModule(theEnv,(void *) theVModule);
00531 count = ListInstancesInModule(theEnv,id,logicalName,className,inheritFlag,FALSE);
00532 }
00533
00534 RestoreCurrentModule(theEnv);
00535 ReleaseTraversalID(theEnv);
00536 if (EvaluationData(theEnv)->HaltExecution == FALSE)
00537 PrintTally(theEnv,logicalName,count,"instance","instances");
00538 }
00539
00540 #endif
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 globle void *EnvMakeInstance(
00555 void *theEnv,
00556 char *mkstr)
00557 {
00558 char *router = "***MKINS***";
00559 struct token tkn;
00560 EXPRESSION *top;
00561 DATA_OBJECT result;
00562
00563 result.type = SYMBOL;
00564 result.value = EnvFalseSymbol(theEnv);
00565 if (OpenStringSource(theEnv,router,mkstr,0) == 0)
00566 return(NULL);
00567 GetToken(theEnv,router,&tkn);
00568 if (tkn.type == LPAREN)
00569 {
00570 top = GenConstant(theEnv,FCALL,(void *) FindFunction(theEnv,"make-instance"));
00571 if (ParseSimpleInstance(theEnv,top,router) != NULL)
00572 {
00573 GetToken(theEnv,router,&tkn);
00574 if (tkn.type == STOP)
00575 {
00576 ExpressionInstall(theEnv,top);
00577 EvaluateExpression(theEnv,top,&result);
00578 ExpressionDeinstall(theEnv,top);
00579 }
00580 else
00581 SyntaxErrorMessage(theEnv,"instance definition");
00582 ReturnExpression(theEnv,top);
00583 }
00584 }
00585 else
00586 SyntaxErrorMessage(theEnv,"instance definition");
00587 CloseStringSource(theEnv,router);
00588
00589 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00590 (EvaluationData(theEnv)->CurrentExpression == NULL))
00591 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00592
00593 if ((result.type == SYMBOL) && (result.value == EnvFalseSymbol(theEnv)))
00594 return(NULL);
00595
00596 return((void *) FindInstanceBySymbol(theEnv,(SYMBOL_HN *) result.value));
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 globle void *EnvCreateRawInstance(
00612 void *theEnv,
00613 void *cptr,
00614 char *iname)
00615 {
00616 return((void *) BuildInstance(theEnv,(SYMBOL_HN *) EnvAddSymbol(theEnv,iname),(DEFCLASS *) cptr,FALSE));
00617 }
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627 globle void *EnvFindInstance(
00628 void *theEnv,
00629 void *theModule,
00630 char *iname,
00631 unsigned searchImports)
00632 {
00633 SYMBOL_HN *isym;
00634
00635 isym = FindSymbolHN(theEnv,iname);
00636 if (isym == NULL)
00637 return(NULL);
00638 if (theModule == NULL)
00639 theModule = (void *) EnvGetCurrentModule(theEnv);
00640 return((void *) FindInstanceInModule(theEnv,isym,(struct defmodule *) theModule,
00641 ((struct defmodule *) EnvGetCurrentModule(theEnv)),searchImports));
00642 }
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652 #if WIN_BTC
00653 #pragma argsused
00654 #endif
00655 globle int EnvValidInstanceAddress(
00656 void *theEnv,
00657 void *iptr)
00658 {
00659 #if MAC_MCW || WIN_MCW || MAC_XCD
00660 #pragma unused(theEnv)
00661 #endif
00662
00663 return((((INSTANCE_TYPE *) iptr)->garbage == 0) ? 1 : 0);
00664 }
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676 globle void EnvDirectGetSlot(
00677 void *theEnv,
00678 void *ins,
00679 char *sname,
00680 DATA_OBJECT *result)
00681 {
00682 INSTANCE_SLOT *sp;
00683
00684 if (((INSTANCE_TYPE *) ins)->garbage == 1)
00685 {
00686 SetEvaluationError(theEnv,TRUE);
00687 result->type = SYMBOL;
00688 result->value = EnvFalseSymbol(theEnv);
00689 return;
00690 }
00691 sp = FindISlotByName(theEnv,(INSTANCE_TYPE *) ins,sname);
00692 if (sp == NULL)
00693 {
00694 SetEvaluationError(theEnv,TRUE);
00695 result->type = SYMBOL;
00696 result->value = EnvFalseSymbol(theEnv);
00697 return;
00698 }
00699 result->type = (unsigned short) sp->type;
00700 result->value = sp->value;
00701 if (sp->type == MULTIFIELD)
00702 {
00703 result->begin = 0;
00704 SetpDOEnd(result,GetInstanceSlotLength(sp));
00705 }
00706 PropagateReturnValue(theEnv,result);
00707 }
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 globle int EnvDirectPutSlot(
00720 void *theEnv,
00721 void *ins,
00722 char *sname,
00723 DATA_OBJECT *val)
00724 {
00725 INSTANCE_SLOT *sp;
00726 DATA_OBJECT junk;
00727
00728 if ((((INSTANCE_TYPE *) ins)->garbage == 1) || (val == NULL))
00729 {
00730 SetEvaluationError(theEnv,TRUE);
00731 return(FALSE);
00732 }
00733 sp = FindISlotByName(theEnv,(INSTANCE_TYPE *) ins,sname);
00734 if (sp == NULL)
00735 {
00736 SetEvaluationError(theEnv,TRUE);
00737 return(FALSE);
00738 }
00739
00740 if (PutSlotValue(theEnv,(INSTANCE_TYPE *) ins,sp,val,&junk,"external put"))
00741 {
00742 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00743 (EvaluationData(theEnv)->CurrentExpression == NULL))
00744 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00745 return(TRUE);
00746 }
00747 return(FALSE);
00748 }
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758 #if WIN_BTC
00759 #pragma argsused
00760 #endif
00761 globle char *EnvGetInstanceName(
00762 void *theEnv,
00763 void *iptr)
00764 {
00765 #if MAC_MCW || WIN_MCW || MAC_XCD
00766 #pragma unused(theEnv)
00767 #endif
00768
00769 if (((INSTANCE_TYPE *) iptr)->garbage == 1)
00770 return(NULL);
00771 return(ValueToString(((INSTANCE_TYPE *) iptr)->name));
00772 }
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 #if ALLOW_ENVIRONMENT_GLOBALS
00783 globle char *GetInstanceName(
00784 void *iptr)
00785 {
00786 return EnvGetInstanceName(GetCurrentEnvironment(),iptr);
00787 }
00788 #endif
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798 #if WIN_BTC
00799 #pragma argsused
00800 #endif
00801 globle void *EnvGetInstanceClass(
00802 void *theEnv,
00803 void *iptr)
00804 {
00805 #if MAC_MCW || WIN_MCW || MAC_XCD
00806 #pragma unused(theEnv)
00807 #endif
00808
00809 if (((INSTANCE_TYPE *) iptr)->garbage == 1)
00810 return(NULL);
00811 return((void *) ((INSTANCE_TYPE *) iptr)->cls);
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823 globle unsigned long GetGlobalNumberOfInstances(
00824 void *theEnv)
00825 {
00826 return(InstanceData(theEnv)->GlobalNumberOfInstances);
00827 }
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839 globle void *EnvGetNextInstance(
00840 void *theEnv,
00841 void *iptr)
00842 {
00843 if (iptr == NULL)
00844 return((void *) InstanceData(theEnv)->InstanceList);
00845 if (((INSTANCE_TYPE *) iptr)->garbage == 1)
00846 return(NULL);
00847 return((void *) ((INSTANCE_TYPE *) iptr)->nxtList);
00848 }
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 globle void *GetNextInstanceInScope(
00864 void *theEnv,
00865 void *iptr)
00866 {
00867 INSTANCE_TYPE *ins = (INSTANCE_TYPE *) iptr;
00868
00869 if (ins == NULL)
00870 ins = InstanceData(theEnv)->InstanceList;
00871 else if (ins->garbage)
00872 return(NULL);
00873 else
00874 ins = ins->nxtList;
00875 while (ins != NULL)
00876 {
00877 if (DefclassInScope(theEnv,ins->cls,NULL))
00878 return((void *) ins);
00879 ins = ins->nxtList;
00880 }
00881 return(NULL);
00882 }
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895 #if WIN_BTC
00896 #pragma argsused
00897 #endif
00898 globle void *EnvGetNextInstanceInClass(
00899 void *theEnv,
00900 void *cptr,
00901 void *iptr)
00902 {
00903 #if MAC_MCW || WIN_MCW || MAC_XCD
00904 #pragma unused(theEnv)
00905 #endif
00906
00907 if (iptr == NULL)
00908 return((void *) ((DEFCLASS *) cptr)->instanceList);
00909 if (((INSTANCE_TYPE *) iptr)->garbage == 1)
00910 return(NULL);
00911 return((void *) ((INSTANCE_TYPE *) iptr)->nxtClass);
00912 }
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926 globle void *EnvGetNextInstanceInClassAndSubclasses(
00927 void *theEnv,
00928 void **cptr,
00929 void *iptr,
00930 DATA_OBJECT *iterationInfo)
00931 {
00932 INSTANCE_TYPE *nextInstance;
00933 DEFCLASS *theClass;
00934
00935 theClass = (DEFCLASS *) *cptr;
00936
00937 if (iptr == NULL)
00938 {
00939 ClassSubclassAddresses(theEnv,theClass,iterationInfo,TRUE);
00940 nextInstance = theClass->instanceList;
00941 }
00942 else if (((INSTANCE_TYPE *) iptr)->garbage == 1)
00943 { nextInstance = NULL; }
00944 else
00945 { nextInstance = ((INSTANCE_TYPE *) iptr)->nxtClass; }
00946
00947 while ((nextInstance == NULL) &&
00948 (GetpDOBegin(iterationInfo) <= GetpDOEnd(iterationInfo)))
00949 {
00950 theClass = (struct defclass *) GetMFValue(DOPToPointer(iterationInfo),
00951 GetpDOBegin(iterationInfo));
00952 *cptr = theClass;
00953 SetpDOBegin(iterationInfo,GetpDOBegin(iterationInfo) + 1);
00954 nextInstance = theClass->instanceList;
00955 }
00956
00957 return(nextInstance);
00958 }
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972 globle void EnvGetInstancePPForm(
00973 void *theEnv,
00974 char *buf,
00975 unsigned buflen,
00976 void *iptr)
00977 {
00978 char *pbuf = "***InstancePPForm***";
00979
00980 if (((INSTANCE_TYPE *) iptr)->garbage == 1)
00981 return;
00982 if (OpenStringDestination(theEnv,pbuf,buf,buflen+1) == 0)
00983 return;
00984 PrintInstance(theEnv,pbuf,(INSTANCE_TYPE *) iptr," ");
00985 CloseStringDestination(theEnv,pbuf);
00986 }
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 globle void ClassCommand(
00999 void *theEnv,
01000 DATA_OBJECT *result)
01001 {
01002 INSTANCE_TYPE *ins;
01003 char *func;
01004 DATA_OBJECT temp;
01005
01006 func = ValueToString(((struct FunctionDefinition *)
01007 EvaluationData(theEnv)->CurrentExpression->value)->callFunctionName);
01008 result->type = SYMBOL;
01009 result->value = EnvFalseSymbol(theEnv);
01010 EvaluateExpression(theEnv,GetFirstArgument(),&temp);
01011 if (temp.type == INSTANCE_ADDRESS)
01012 {
01013 ins = (INSTANCE_TYPE *) temp.value;
01014 if (ins->garbage == 1)
01015 {
01016 StaleInstanceAddress(theEnv,func,0);
01017 SetEvaluationError(theEnv,TRUE);
01018 return;
01019 }
01020 result->value = (void *) GetDefclassNamePointer((void *) ins->cls);
01021 }
01022 else if (temp.type == INSTANCE_NAME)
01023 {
01024 ins = FindInstanceBySymbol(theEnv,(SYMBOL_HN *) temp.value);
01025 if (ins == NULL)
01026 {
01027 NoInstanceError(theEnv,ValueToString(temp.value),func);
01028 return;
01029 }
01030 result->value = (void *) GetDefclassNamePointer((void *) ins->cls);
01031 }
01032 else
01033 {
01034 switch (temp.type)
01035 {
01036 case INTEGER :
01037 case FLOAT :
01038 case SYMBOL :
01039 case STRING :
01040 case MULTIFIELD :
01041 case EXTERNAL_ADDRESS :
01042 case FACT_ADDRESS :
01043 result->value = (void *)
01044 GetDefclassNamePointer((void *)
01045 DefclassData(theEnv)->PrimitiveClassMap[temp.type]);
01046 return;
01047
01048 default : PrintErrorID(theEnv,"INSCOM",1,FALSE);
01049 EnvPrintRouter(theEnv,WERROR,"Undefined type in function ");
01050 EnvPrintRouter(theEnv,WERROR,func);
01051 EnvPrintRouter(theEnv,WERROR,".\n");
01052 SetEvaluationError(theEnv,TRUE);
01053 }
01054 }
01055 }
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066 #if WIN_BTC
01067 #pragma argsused
01068 #endif
01069 globle intBool CreateInstanceHandler(
01070 void *theEnv)
01071 {
01072 #if MAC_MCW || WIN_MCW || MAC_XCD
01073 #pragma unused(theEnv)
01074 #endif
01075
01076 return(TRUE);
01077 }
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091 globle intBool DeleteInstanceCommand(
01092 void *theEnv)
01093 {
01094 if (CheckCurrentMessage(theEnv,"delete-instance",TRUE))
01095 return(QuashInstance(theEnv,GetActiveInstance(theEnv)));
01096 return(FALSE);
01097 }
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108 globle intBool UnmakeInstanceCommand(
01109 void *theEnv)
01110 {
01111 EXPRESSION *theArgument;
01112 DATA_OBJECT theResult;
01113 INSTANCE_TYPE *ins;
01114 int argNumber = 1,rtn = TRUE;
01115
01116 theArgument = GetFirstArgument();
01117 while (theArgument != NULL)
01118 {
01119 EvaluateExpression(theEnv,theArgument,&theResult);
01120 if ((theResult.type == INSTANCE_NAME) || (theResult.type == SYMBOL))
01121 {
01122 ins = FindInstanceBySymbol(theEnv,(SYMBOL_HN *) theResult.value);
01123 if ((ins == NULL) ? (strcmp(DOToString(theResult),"*") != 0) : FALSE)
01124 {
01125 NoInstanceError(theEnv,DOToString(theResult),"unmake-instance");
01126 return(FALSE);
01127 }
01128 }
01129 else if (theResult.type == INSTANCE_ADDRESS)
01130 {
01131 ins = (INSTANCE_TYPE *) theResult.value;
01132 if (ins->garbage)
01133 {
01134 StaleInstanceAddress(theEnv,"unmake-instance",0);
01135 SetEvaluationError(theEnv,TRUE);
01136 return(FALSE);
01137 }
01138 }
01139 else
01140 {
01141 ExpectedTypeError1(theEnv,"unmake-instance",argNumber,"instance-address, instance-name, or the symbol *");
01142 SetEvaluationError(theEnv,TRUE);
01143 return(FALSE);
01144 }
01145 if (EnvUnmakeInstance(theEnv,ins) == FALSE)
01146 rtn = FALSE;
01147 if (ins == NULL)
01148 return(rtn);
01149 argNumber++;
01150 theArgument = GetNextArgument(theArgument);
01151 }
01152 return(rtn);
01153 }
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164 globle void SymbolToInstanceName(
01165 void *theEnv,
01166 DATA_OBJECT *result)
01167 {
01168 if (EnvArgTypeCheck(theEnv,"symbol-to-instance-name",1,SYMBOL,result) == FALSE)
01169 {
01170 SetpType(result,SYMBOL);
01171 SetpValue(result,EnvFalseSymbol(theEnv));
01172 return;
01173 }
01174 SetpType(result,INSTANCE_NAME);
01175 }
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186 globle void *InstanceNameToSymbol(
01187 void *theEnv)
01188 {
01189 DATA_OBJECT result;
01190
01191 if (EnvArgTypeCheck(theEnv,"instance-name-to-symbol",1,INSTANCE_NAME,&result) == FALSE)
01192 return((SYMBOL_HN *) EnvFalseSymbol(theEnv));
01193 return((SYMBOL_HN *) result.value);
01194 }
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204 globle void InstanceAddressCommand(
01205 void *theEnv,
01206 DATA_OBJECT *result)
01207 {
01208 INSTANCE_TYPE *ins;
01209 DATA_OBJECT temp;
01210 struct defmodule *theModule;
01211 unsigned searchImports;
01212
01213 result->type = SYMBOL;
01214 result->value = EnvFalseSymbol(theEnv);
01215 if (EnvRtnArgCount(theEnv) > 1)
01216 {
01217 if (EnvArgTypeCheck(theEnv,"instance-address",1,SYMBOL,&temp) == FALSE)
01218 return;
01219 theModule = (struct defmodule *) EnvFindDefmodule(theEnv,DOToString(temp));
01220 if ((theModule == NULL) ? (strcmp(DOToString(temp),"*") != 0) : FALSE)
01221 {
01222 ExpectedTypeError1(theEnv,"instance-address",1,"module name");
01223 SetEvaluationError(theEnv,TRUE);
01224 return;
01225 }
01226 if (theModule == NULL)
01227 {
01228 searchImports = TRUE;
01229 theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
01230 }
01231 else
01232 searchImports = FALSE;
01233 if (EnvArgTypeCheck(theEnv,"instance-address",2,INSTANCE_NAME,&temp)
01234 == FALSE)
01235 return;
01236 ins = FindInstanceInModule(theEnv,(SYMBOL_HN *) temp.value,theModule,
01237 ((struct defmodule *) EnvGetCurrentModule(theEnv)),searchImports);
01238 if (ins != NULL)
01239 {
01240 result->type = INSTANCE_ADDRESS;
01241 result->value = (void *) ins;
01242 }
01243 else
01244 NoInstanceError(theEnv,ValueToString(temp.value),"instance-address");
01245 }
01246 else if (EnvArgTypeCheck(theEnv,"instance-address",1,INSTANCE_OR_INSTANCE_NAME,&temp))
01247 {
01248 if (temp.type == INSTANCE_ADDRESS)
01249 {
01250 ins = (INSTANCE_TYPE *) temp.value;
01251 if (ins->garbage == 0)
01252 {
01253 result->type = INSTANCE_ADDRESS;
01254 result->value = temp.value;
01255 }
01256 else
01257 {
01258 StaleInstanceAddress(theEnv,"instance-address",0);
01259 SetEvaluationError(theEnv,TRUE);
01260 }
01261 }
01262 else
01263 {
01264 ins = FindInstanceBySymbol(theEnv,(SYMBOL_HN *) temp.value);
01265 if (ins != NULL)
01266 {
01267 result->type = INSTANCE_ADDRESS;
01268 result->value = (void *) ins;
01269 }
01270 else
01271 NoInstanceError(theEnv,ValueToString(temp.value),"instance-address");
01272 }
01273 }
01274 }
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284 globle void InstanceNameCommand(
01285 void *theEnv,
01286 DATA_OBJECT *result)
01287 {
01288 INSTANCE_TYPE *ins;
01289 DATA_OBJECT temp;
01290
01291 result->type = SYMBOL;
01292 result->value = EnvFalseSymbol(theEnv);
01293 if (EnvArgTypeCheck(theEnv,"instance-name",1,INSTANCE_OR_INSTANCE_NAME,&temp) == FALSE)
01294 return;
01295 if (temp.type == INSTANCE_ADDRESS)
01296 {
01297 ins = (INSTANCE_TYPE *) temp.value;
01298 if (ins->garbage == 1)
01299 {
01300 StaleInstanceAddress(theEnv,"instance-name",0);
01301 SetEvaluationError(theEnv,TRUE);
01302 return;
01303 }
01304 }
01305 else
01306 {
01307 ins = FindInstanceBySymbol(theEnv,(SYMBOL_HN *) temp.value);
01308 if (ins == NULL)
01309 {
01310 NoInstanceError(theEnv,ValueToString(temp.value),"instance-name");
01311 return;
01312 }
01313 }
01314 result->type = INSTANCE_NAME;
01315 result->value = (void *) ins->name;
01316 }
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326 globle intBool InstanceAddressPCommand(
01327 void *theEnv)
01328 {
01329 DATA_OBJECT temp;
01330
01331 EvaluateExpression(theEnv,GetFirstArgument(),&temp);
01332 return((GetType(temp) == INSTANCE_ADDRESS) ? TRUE : FALSE);
01333 }
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343 globle intBool InstanceNamePCommand(
01344 void *theEnv)
01345 {
01346 DATA_OBJECT temp;
01347
01348 EvaluateExpression(theEnv,GetFirstArgument(),&temp);
01349 return((GetType(temp) == INSTANCE_NAME) ? TRUE : FALSE);
01350 }
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362 globle intBool InstancePCommand(
01363 void *theEnv)
01364 {
01365 DATA_OBJECT temp;
01366
01367 EvaluateExpression(theEnv,GetFirstArgument(),&temp);
01368 if ((GetType(temp) == INSTANCE_NAME) || (GetType(temp) == INSTANCE_ADDRESS))
01369 return(TRUE);
01370 return(FALSE);
01371 }
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381 globle intBool InstanceExistPCommand(
01382 void *theEnv)
01383 {
01384 DATA_OBJECT temp;
01385
01386 EvaluateExpression(theEnv,GetFirstArgument(),&temp);
01387 if (temp.type == INSTANCE_ADDRESS)
01388 return((((INSTANCE_TYPE *) temp.value)->garbage == 0) ? TRUE : FALSE);
01389 if ((temp.type == INSTANCE_NAME) || (temp.type == SYMBOL))
01390 return((FindInstanceBySymbol(theEnv,(SYMBOL_HN *) temp.value) != NULL) ?
01391 TRUE : FALSE);
01392 ExpectedTypeError1(theEnv,"instance-existp",1,"instance name, instance address or symbol");
01393 SetEvaluationError(theEnv,TRUE);
01394 return(FALSE);
01395 }
01396
01397
01398
01399
01400
01401
01402
01403 #if DEBUGGING_FUNCTIONS
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423 static long ListInstancesInModule(
01424 void *theEnv,
01425 int id,
01426 char *logicalName,
01427 char *className,
01428 intBool inheritFlag,
01429 intBool allModulesFlag)
01430 {
01431 void *theDefclass,*theInstance;
01432 long count = 0L;
01433
01434
01435
01436
01437
01438 if (className == NULL)
01439 {
01440
01441
01442
01443
01444
01445 if (allModulesFlag)
01446 {
01447 for (theDefclass = EnvGetNextDefclass(theEnv,NULL) ;
01448 theDefclass != NULL ;
01449 theDefclass = EnvGetNextDefclass(theEnv,theDefclass))
01450 count += TabulateInstances(theEnv,id,logicalName,
01451 (DEFCLASS *) theDefclass,FALSE,allModulesFlag);
01452 }
01453
01454
01455
01456
01457
01458
01459 else
01460 {
01461 theInstance = GetNextInstanceInScope(theEnv,NULL);
01462 while (theInstance != NULL)
01463 {
01464 if (GetHaltExecution(theEnv) == TRUE)
01465 { return(count); }
01466
01467 count++;
01468 PrintInstanceNameAndClass(theEnv,logicalName,(INSTANCE_TYPE *) theInstance,TRUE);
01469 theInstance = GetNextInstanceInScope(theEnv,theInstance);
01470 }
01471 }
01472 }
01473
01474
01475
01476
01477
01478 else
01479 {
01480 theDefclass = (void *) LookupDefclassAnywhere(theEnv,((struct defmodule *) EnvGetCurrentModule(theEnv)),className);
01481 if (theDefclass != NULL)
01482 {
01483 count += TabulateInstances(theEnv,id,logicalName,
01484 (DEFCLASS *) theDefclass,inheritFlag,allModulesFlag);
01485 }
01486 else if (! allModulesFlag)
01487 ClassExistError(theEnv,"instances",className);
01488 }
01489 return(count);
01490 }
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508 static long TabulateInstances(
01509 void *theEnv,
01510 int id,
01511 char *logicalName,
01512 DEFCLASS *cls,
01513 intBool inheritFlag,
01514 intBool allModulesFlag)
01515 {
01516 INSTANCE_TYPE *ins;
01517 long i;
01518 long count = 0;
01519
01520 if (TestTraversalID(cls->traversalRecord,id))
01521 return(0L);
01522 SetTraversalID(cls->traversalRecord,id);
01523 for (ins = cls->instanceList ; ins != NULL ; ins = ins->nxtClass)
01524 {
01525 if (EvaluationData(theEnv)->HaltExecution)
01526 return(count);
01527 if (allModulesFlag)
01528 EnvPrintRouter(theEnv,logicalName," ");
01529 PrintInstanceNameAndClass(theEnv,logicalName,ins,TRUE);
01530 count++;
01531 }
01532 if (inheritFlag)
01533 {
01534 for (i = 0 ; i < cls->directSubclasses.classCount ; i++)
01535 {
01536 if (EvaluationData(theEnv)->HaltExecution)
01537 return(count);
01538 count += TabulateInstances(theEnv,id,logicalName,
01539 cls->directSubclasses.classArray[i],inheritFlag,allModulesFlag);
01540 }
01541 }
01542 return(count);
01543 }
01544
01545 #endif
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558 static void PrintInstance(
01559 void *theEnv,
01560 char *logicalName,
01561 INSTANCE_TYPE *ins,
01562 char *separator)
01563 {
01564 long i;
01565 register INSTANCE_SLOT *sp;
01566
01567 PrintInstanceNameAndClass(theEnv,logicalName,ins,FALSE);
01568 for (i = 0 ; i < ins->cls->instanceSlotCount ; i++)
01569 {
01570 EnvPrintRouter(theEnv,logicalName,separator);
01571 sp = ins->slotAddresses[i];
01572 EnvPrintRouter(theEnv,logicalName,"(");
01573 EnvPrintRouter(theEnv,logicalName,ValueToString(sp->desc->slotName->name));
01574 if (sp->type != MULTIFIELD)
01575 {
01576 EnvPrintRouter(theEnv,logicalName," ");
01577 PrintAtom(theEnv,logicalName,(int) sp->type,sp->value);
01578 }
01579 else if (GetInstanceSlotLength(sp) != 0)
01580 {
01581 EnvPrintRouter(theEnv,logicalName," ");
01582 PrintMultifield(theEnv,logicalName,(MULTIFIELD_PTR) sp->value,0,
01583 (long) (GetInstanceSlotLength(sp) - 1),FALSE);
01584 }
01585 EnvPrintRouter(theEnv,logicalName,")");
01586 }
01587 }
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600 static INSTANCE_SLOT *FindISlotByName(
01601 void *theEnv,
01602 INSTANCE_TYPE *ins,
01603 char *sname)
01604 {
01605 SYMBOL_HN *ssym;
01606
01607 ssym = FindSymbolHN(theEnv,sname);
01608 if (ssym == NULL)
01609 return(NULL);
01610 return(FindInstanceSlot(theEnv,ins,ssym));
01611 }
01612
01613 #endif
01614