PageRenderTime 39ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/package.d

https://github.com/ynd/clisp-branch--ynd-devel
D | 2890 lines | 1945 code | 111 blank | 834 comment | 299 complexity | 1ef9fed67b20bc31fb5be26f15f56aff MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, BSD-3-Clause
  1. /*
  2. * Package Management for CLISP
  3. * Bruno Haible 1990-2005
  4. * Sam Steingold 1999-2008
  5. * German comments translated into English: Stefan Kain 2002-02-20
  6. */
  7. #include "lispbibl.c"
  8. #include "arilev0.c" /* for hashcode calculation */
  9. /* data structure of the symbols: see LISPBIBL.D
  10. data structure of the symbol table:
  11. a vector with 3 Slots:
  12. size Fixnum >0, <2^24, = length of the table
  13. table vector of length size,
  14. contains single symbols (/= NIL) and symbol-lists
  15. count number of symbols in the table, Fixnum >=0 */
  16. #define Symtab_size(symtab) (TheSvector(symtab)->data[0])
  17. #define Symtab_table(symtab) (TheSvector(symtab)->data[1])
  18. #define Symtab_count(symtab) (TheSvector(symtab)->data[2])
  19. /* consistency rule: for each string there is in the table
  20. at most one symbol with this printname. */
  21. /* UP: Creates a new empty symbol-table.
  22. make_symtab(size)
  23. > size: the desired size of the table (odd number, >0, <2^24)
  24. < result: new symbol-table of this size
  25. can trigger GC */
  26. local maygc object make_symtab (uintL size) {
  27. var object table = allocate_vector(size); /* vector with size NIL-entries */
  28. pushSTACK(table);
  29. var object symtab = allocate_vector(3); /* vector of length 3 */
  30. Symtab_table(symtab) = popSTACK(); /* insert table */
  31. Symtab_size(symtab) = fixnum(size); /* insert size */
  32. Symtab_count(symtab) = Fixnum_0; /* insert count := 0 */
  33. return symtab;
  34. }
  35. /* UP: Calculates the hashcode of a string. This is a 24-bit-number.
  36. string_hashcode(string,invert)
  37. > string: a string
  38. > invert: whether to implicitly case-invert the string
  39. < result: the hashcode of the string */
  40. local uint32 string_hashcode (object string, bool invert) {
  41. var uintL len;
  42. var uintL offset;
  43. string = unpack_string_ro(string,&len,&offset);
  44. var uint32 hashcode = 0; /* hashcode, only the lower 24 Bit count */
  45. if (len > 0) {
  46. SstringDispatch(string,X, {
  47. var const cintX* charptr = &((SstringX)TheVarobject(string))->data[offset];
  48. /* there are len characters, starting at charptr */
  49. /* Look at all len characters, not just at the first min(len,16)
  50. characters, as we did earlier, because a bad hash function quasi
  51. turns the hash table into a few long linear lists. */
  52. var uintC count;
  53. dotimesC(count, len, {
  54. /* rotate hashcode by 5 bits to the left: */
  55. hashcode = hashcode << 5; hashcode = hashcode + (hashcode >> 24);
  56. /* 'add' next byte via XOR: */
  57. var cintX c = *charptr++;
  58. hashcode = hashcode ^ (uint32)(invert ? as_cint(invert_case(as_chart(c))) : c);
  59. });
  60. });
  61. }
  62. return hashcode & 0x00FFFFFF;
  63. }
  64. /* UP: Reorganizes a symbol-table, after it has grown, and
  65. tries to save Conses.
  66. rehash_symtab(symtab)
  67. > symtab: symbol-table
  68. < result: reorganized symbol-table (EQ to the first).
  69. call only, if BREAK_SEM_2 is set
  70. can trigger GC */
  71. local maygc object rehash_symtab (object symtab);
  72. /* auxiliary functions: */
  73. /* takes a Cons from free-conses or returns a fresh one.
  74. new_cons()
  75. < result: new Cons.
  76. stack layout: free-conses, newtable, listr, symbol, entry.
  77. can trigger GC */
  78. local maygc object new_cons (void) {
  79. var object free = STACK_4; /* free-conses */
  80. if (!nullp(free)) {
  81. STACK_4 = Cdr(free); /* shorten free-conses */
  82. return free;
  83. } else {
  84. return allocate_cons(); /* request new Cons from memory-management */
  85. }
  86. }
  87. /* inserts an additional symbol into the new table.
  88. newinsert(sym,size);
  89. > sym: symbol
  90. stack layout: tab, oldtable, free-conses, newtable, listr.
  91. can trigger GC */
  92. local maygc void newinsert (object sym, uintL size) {
  93. var uintL index = /* Index = Hashcode mod size */
  94. string_hashcode(Symbol_name(sym),false) % size;
  95. /* entry in the newtable */
  96. var object entry = TheSvector(STACK_1)->data[index];
  97. if ((!nullp(entry)) || nullp(sym)) {
  98. /* if entry=NIL and sym/=NIL, then simply enter sym.
  99. else, entry must be extended by cons-ing: */
  100. pushSTACK(sym); /* save symbol */
  101. pushSTACK(entry); /* save entry */
  102. if (!listp(entry)) {
  103. /* if entry is not a list, replace with (new-cons entry NIL): */
  104. var object new_entry = new_cons();
  105. Cdr(new_entry) = NIL; Car(new_entry) = STACK_0;
  106. STACK_0 = new_entry;
  107. }
  108. /* and cons symbol in front of it: */
  109. var object new_entry = new_cons();
  110. Cdr(new_entry) = popSTACK(); /* enter entry resp. list as CDR */
  111. Car(new_entry) = popSTACK(); /* enter symbol as CAR */
  112. sym = new_entry; /* and then enter new_entry */
  113. }
  114. TheSvector(STACK_1)->data[index] = sym; /* enter new entry in newtable */
  115. }
  116. local object rehash_symtab (object symtab) {
  117. pushSTACK(symtab); /* save symbol-table */
  118. var uintL oldsize = posfixnum_to_V(Symtab_size(symtab)); /* old size */
  119. var uintL newsize; /* new size */
  120. var object size; /* new size (as Fixnum) */
  121. pushSTACK(Symtab_table(symtab)); /* oldtable = old table-vector */
  122. pushSTACK(NIL); /* free-conses := NIL */
  123. #ifdef TYPECODES /* Svector_length is limited to max. 2^32-1 */
  124. /* new size = min(floor(oldsize*1.6),2^32-1) */
  125. { /* multiply oldsize (>0, <2^32) with 1.6*2^31, then divide by 2^31 : */
  126. var uint32 prod_hi;
  127. var uint32 prod_lo;
  128. mulu32(oldsize,3435973888UL, prod_hi=,prod_lo=);
  129. newsize =
  130. (prod_hi < (1UL<<31) ? (prod_hi << 1) | (prod_lo >> 31) : (1UL<<31)-1 );
  131. }
  132. #else /* Svector_length is limited to max. 2^24-1 */
  133. /* new size = min(floor(oldsize*1.6),2^24-1) */
  134. { /* multiply oldsize (>0, <2^24) with 1.6*2^7, then divide by 2^7 : */
  135. var uint32 prod = oldsize * 205UL;
  136. newsize = (prod < (1UL<<31) ? prod>>7 : (1UL<<24)-1 );
  137. } /* newsize is now >= oldsize > 0 and < 2^24 */
  138. #endif
  139. /* make newsize odd by rounding off: */
  140. newsize = (newsize - 1) | 1 ;
  141. /* calculate size: */
  142. size = fixnum(newsize);
  143. /* if newsize <= oldsize , the table does not need to be enlarged: */
  144. if (newsize <= oldsize) {
  145. skipSTACK(3);
  146. return symtab;
  147. }
  148. { /* new vector with size NILs */
  149. var object newtable = allocate_vector(newsize);
  150. pushSTACK(newtable); /* save */
  151. }
  152. /* here we could protect against breaks.
  153. stack layout: tab, oldtable, free-conses, newtable.
  154. transfer symbols from oldtable to newtable:
  155. first process the symbols, that sit in lists
  156. (maybe Conses become free): */
  157. {
  158. var gcv_object_t* offset = 0; /* offset = sizeof(gcv_object_t)*index */
  159. var uintC count;
  160. dotimespC(count,oldsize, {
  161. var object oldentry = /* entry with number index in oldtable */
  162. *(gcv_object_t*)(pointerplus(&TheSvector(STACK_2)->data[0],
  163. (aint)offset));
  164. if (consp(oldentry)) /* this time process only non-empty symbol-lists */
  165. do {
  166. pushSTACK(Cdr(oldentry)); /* save rest-list */
  167. /* cons oldentry in front of free-conses */
  168. Cdr(oldentry) = STACK_2; STACK_2 = oldentry;
  169. /* enter symbol in the new table */
  170. newinsert(Car(oldentry),newsize);
  171. oldentry = popSTACK(); /* rest-list */
  172. } while (consp(oldentry));
  173. offset++;
  174. });
  175. }
  176. { /* then process symbols, that sit there collision-free: */
  177. var gcv_object_t* offset = 0; /* offset = sizeof(gcv_object_t)*index */
  178. var uintC count;
  179. dotimespC(count,oldsize, {
  180. var object oldentry = /* entry with number index in oldtable */
  181. *(gcv_object_t*)(pointerplus(&TheSvector(STACK_2)->data[0],
  182. (aint)offset));
  183. if (!listp(oldentry)) { /* this time process only symbols /= NIL */
  184. pushSTACK(oldentry); /* dummy, so that the stack is fine */
  185. newinsert(oldentry,newsize); /* enter into the new table */
  186. skipSTACK(1);
  187. }
  188. offset++;
  189. });
  190. }
  191. /* stack layout: tab, oldtable, free-conses, newtable. */
  192. { /* update tab: */
  193. var object newtable = popSTACK(); /* newtable */
  194. skipSTACK(2);
  195. symtab = popSTACK(); /* tab */
  196. Symtab_size(symtab) = size;
  197. Symtab_table(symtab) = newtable;
  198. }
  199. /* here, breaks could be allowed again. */
  200. return symtab;
  201. }
  202. /* UP: Searches a symbol with given print-name in the symbol-table.
  203. symtab_lookup(string,invert,symtab,&sym)
  204. > string: string
  205. > invert: whether to implicitly case-invert the string
  206. > symtab: symbol-table
  207. < result: true if found, false if not found.
  208. if found:
  209. < sym: the symbol from the symbol-table, that has the given printname */
  210. local bool symtab_lookup (object string, bool invert, object symtab, object* sym_) {
  211. var uintL index = /* Index = Hashcode mod size */
  212. string_hashcode(string,invert) % (uintL)posfixnum_to_V(Symtab_size(symtab));
  213. /* entry in the table */
  214. var object entry = TheSvector(Symtab_table(symtab))->data[index];
  215. if (!listp(entry)) { /* entry is a single symbol */
  216. /* first string and printname of the found symbol are equal ? */
  217. if ((invert ? string_eq_inverted : string_eq)
  218. (string,Symbol_name(entry))) {
  219. if (sym_) { *sym_ = entry; }
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. } else { /* entry is a symbol-list */
  225. while (consp(entry)) {
  226. /* first string and printname of the symbol are equal ? */
  227. if ((invert ? string_eq_inverted : string_eq)
  228. (string,Symbol_name(Car(entry))))
  229. goto found;
  230. entry = Cdr(entry);
  231. }
  232. return false; /* not found */
  233. found: /* found as CAR of entry */
  234. if (sym_) { *sym_ = Car(entry); }
  235. return true;
  236. }
  237. }
  238. /* UP: Searches a given symbol in the symbol-table.
  239. symtab_find(sym,symtab)
  240. > sym: symbol
  241. > symtab: symbol-table
  242. < result: true, if found */
  243. local bool symtab_find (object sym, object symtab) {
  244. var uintL index = /* Index = Hashcode mod size */
  245. string_hashcode(Symbol_name(sym),false) % (uintL)posfixnum_to_V(Symtab_size(symtab));
  246. /* entry in the table */
  247. var object entry = TheSvector(Symtab_table(symtab))->data[index];
  248. if (!listp(entry)) { /* entry is a single symbol */
  249. /* sym and found symbol are equal ? */
  250. if (eq(sym,entry))
  251. return true;
  252. else
  253. return false;
  254. } else { /* entry is a symbol-list */
  255. if (nullp(memq(sym,entry))) return false; /* not found */
  256. else return true; /* found as CAR from entry */
  257. }
  258. }
  259. /* UP: Inserts a given symbol into the symbol-table (destructively).
  260. symtab_insert(sym,symtab)
  261. > sym: symbol
  262. > symtab: symbol-table
  263. < result: new symbol-table, EQ to the old one
  264. call only, if BREAK_SEM_2 is set
  265. can trigger GC */
  266. local maygc object symtab_insert (object sym, object symtab) {
  267. { /* first test if reorganization is necessary: */
  268. var uintL size = posfixnum_to_V(Symtab_size(symtab));
  269. var uintL count = posfixnum_to_V(Symtab_count(symtab));
  270. /* if count>=2*size , the table must be reorganized: */
  271. if (count >= 2*size) {
  272. pushSTACK(sym); /* save symbol */
  273. symtab = rehash_symtab(symtab);
  274. sym = popSTACK();
  275. }
  276. }
  277. /* then insert the symbol: */
  278. var uintL index = /* Index = Hashcode mod size */
  279. string_hashcode(Symbol_name(sym),false) % (uintL)posfixnum_to_V(Symtab_size(symtab));
  280. /* entry in the table */
  281. var object entry = TheSvector(Symtab_table(symtab))->data[index];
  282. if (!nullp(entry) || nullp(sym)) {
  283. /* if entry=NIL and sym/=NIL, then simply enter sym.
  284. else, entry must be extended by cons-ing: */
  285. pushSTACK(symtab); /* save symtab */
  286. pushSTACK(sym); /* save Symbol */
  287. pushSTACK(entry); /* save entry */
  288. if (!listp(entry)) {
  289. /* if entry is not a list, replace with (cons entry NIL): */
  290. var object new_entry = allocate_cons();
  291. Car(new_entry) = STACK_0;
  292. STACK_0 = new_entry;
  293. }
  294. { /* and cons symbol in front of it: */
  295. var object new_entry = allocate_cons();
  296. Cdr(new_entry) = popSTACK(); /* enter entry resp. list as CDR */
  297. Car(new_entry) = popSTACK(); /* enter symbol as CAR */
  298. sym = new_entry; /* and then enter new_entry */
  299. }
  300. symtab = popSTACK();
  301. }
  302. TheSvector(Symtab_table(symtab))->data[index] = sym; /* enter new entry */
  303. Symtab_count(symtab) = fixnum_inc(Symtab_count(symtab),1); /* (incf count) */
  304. return symtab;
  305. }
  306. /* UP: Removes a symbol from a symbol-table.
  307. symtab_delete(sym,symtab)
  308. > sym: symbol
  309. > symtab: symboltable */
  310. local void symtab_delete (object sym, object symtab) {
  311. var uintL index = /* Index = Hashcode mod size */
  312. string_hashcode(Symbol_name(sym),false) % (uintL)posfixnum_to_V(Symtab_size(symtab));
  313. var gcv_object_t* entryptr = &TheSvector(Symtab_table(symtab))->data[index];
  314. var object entry = *entryptr; /* entry in the table */
  315. if (!listp(entry)) { /* entry is a single symbol */
  316. /* sym and found symbol eq ? */
  317. if (!eq(sym,entry))
  318. goto notfound;
  319. /* replace entry with NIL: */
  320. *entryptr = NIL;
  321. } else { /* entry is a symbol-list */
  322. while (consp(entry)) {
  323. /* sym and symbol from entry eq ? */
  324. if (eq(sym,Car(entry)))
  325. goto found;
  326. entryptr = &Cdr(entry); entry = *entryptr;
  327. }
  328. goto notfound; /* not found */
  329. found: /* found as CAR of *entryptr = entry */
  330. /* -> discard a list-element: */
  331. *entryptr = Cdr(entry); /* replace entry with Cdr(entry) */
  332. }
  333. /* finally decrement the symbol-counter by 1: (decf count) */
  334. { Symtab_count(symtab) = fixnum_inc(Symtab_count(symtab),-1); }
  335. return;
  336. notfound:
  337. pushSTACK(unbound); /* PACKAGE-ERROR slot PACKAGE */
  338. pushSTACK(sym);
  339. error(package_error,
  340. GETTEXT("symbol ~S cannot be deleted from symbol table"));
  341. }
  342. /* lookup the STRING among the EXTernal (resp. INTernal) symbols of PACK */
  343. #define package_lookup_ext(string,invert,pack,res_) \
  344. symtab_lookup(string,invert,ThePackage(pack)->pack_external_symbols,res_)
  345. #define package_lookup_int(string,invert,pack,res_) \
  346. symtab_lookup(string,invert,ThePackage(pack)->pack_internal_symbols,res_)
  347. /* Test whether there is an inherited symbol with the given name.
  348. inherited_lookup(string,invert,pack,&sym)
  349. Return true if string is found in (package-use-list pack).
  350. > string: a Lisp string object
  351. > invert: whether to implicitly case-invert the string
  352. > pack: is a Lisp package object
  353. The symbol found is returned in *SYM_ (if SYM_ is not NULL). */
  354. local bool inherited_lookup (object string, bool invert, object pack, object* sym_) {
  355. var object packlistr = ThePackage(pack)->pack_use_list;
  356. while (consp(packlistr)) {
  357. var object usedpack = Car(packlistr);
  358. if (package_lookup_ext(string,invert,usedpack,sym_))
  359. return true;
  360. packlistr = Cdr(packlistr);
  361. }
  362. return false;
  363. }
  364. /* Check whether the symbol is inherited by the package.
  365. inherited_find(symbol,pack)
  366. SYMBOL is a Lisp symbol object
  367. PACK is a Lisp package object */
  368. local bool inherited_find (object symbol, object pack) {
  369. var object list = ThePackage(pack)->pack_use_list;
  370. while (consp(list)) {
  371. if (symtab_find(symbol,ThePackage(Car(list))->pack_external_symbols))
  372. return true;
  373. list = Cdr(list);
  374. }
  375. return false;
  376. }
  377. /* data structure of package, see LISPBIBL.D.
  378. Components:
  379. pack_external_symbols symbol-table of the externally present symbols
  380. pack_internal_symbols symbol-table of the internally present symbols
  381. pack_shadowing_symbols list of the shadowing-symbols
  382. pack_use_list use-list, a list of packages
  383. pack_used_by_list used-by-list, a list of packages
  384. pack_name the name, an immutable simple-string
  385. pack_nicknames the nicknames, a list of immutable simple-strings
  386. pack_docstring the documentation string or NIL
  387. consistency rules:
  388. 1. All packages are listed in ALL_PACKAGES exactly once.
  389. 2. The union over ALL_PACKAGES of { name } U nicknames is disjoint.
  390. 3. for any two packages p,q:
  391. p in use_list(q) <==> q in used_by_list(q)
  392. 4. p is a Package.
  393. accessible(p) = ISymbols(p) U ESymbols(p) U
  394. U { ESymbols(q) | q in use_list(p) }
  395. 5. For each Package p
  396. shadowing_symbols(p) is a subset of ISymbols(p) U ESymbols(p)
  397. and therefore also a subset of accessible(p).
  398. 6. s is a string, p is a package.
  399. If more than one element of accessible(p) has print name = s, then
  400. exactly one of these symbols is in shadowing_symbols(p).
  401. 7. s is a string, p is a package.
  402. At most one symbol with the print name = s
  403. is in ISymbols(p) U ESymbols(p).
  404. 8. If s is a symbol with the Home Package p /= NIL,
  405. then s is in ISymbols(p) U ESymbols(p). */
  406. /* UP: make sure pack_shortest_name is indeed the shortest */
  407. local void ensure_pack_shortest_name (object pack) {
  408. var object shortest_name = ThePackage(pack)->pack_name;
  409. var uintL shortest_len = Sstring_length(shortest_name);
  410. var object nick_list = ThePackage(pack)->pack_nicknames;
  411. while (consp(nick_list)) {
  412. var object nick = Car(nick_list); nick_list = Cdr(nick_list);
  413. var uintL nick_len = Sstring_length(nick);
  414. if (nick_len < shortest_len) {
  415. shortest_len = nick_len;
  416. shortest_name = nick;
  417. }
  418. }
  419. ThePackage(pack)->pack_shortest_name = shortest_name;
  420. }
  421. /* UP: Creates a new package, without testing for name-conflicts.
  422. make_package(name,nicknames,case_sensitive_p)
  423. > name: name (an immutable simple-string)
  424. > nicknames: nicknames (a list of immutable simple-strings)
  425. > case_sensitive_p: flag, if case-sensitive
  426. > case_inverted_p: flag, if case-inverted
  427. < result: new package
  428. can trigger GC */
  429. local maygc object make_package (object name, object nicknames,
  430. bool case_sensitive_p, bool case_inverted_p) {
  431. set_break_sem_2();
  432. pushSTACK(nicknames); pushSTACK(name); /* save nicknames and names */
  433. /* create table for external symbols: */
  434. { var object symtab = make_symtab(11); pushSTACK(symtab); }
  435. /* create table for internal symbols: */
  436. { var object symtab = make_symtab(63); pushSTACK(symtab); }
  437. /* create new package: */
  438. var object pack = allocate_package();
  439. /* and fill: */
  440. if (case_sensitive_p) { mark_pack_casesensitive(pack); }
  441. if (case_inverted_p) { mark_pack_caseinverted(pack); }
  442. ThePackage(pack)->pack_internal_symbols = popSTACK();
  443. ThePackage(pack)->pack_external_symbols = popSTACK();
  444. ThePackage(pack)->pack_shadowing_symbols = NIL;
  445. ThePackage(pack)->pack_use_list = NIL;
  446. ThePackage(pack)->pack_used_by_list = NIL;
  447. ThePackage(pack)->pack_name = popSTACK();
  448. ThePackage(pack)->pack_nicknames = popSTACK();
  449. ThePackage(pack)->pack_docstring = NIL;
  450. ensure_pack_shortest_name(pack);
  451. /* and insert in ALL_PACKAGES: */
  452. pushSTACK(pack);
  453. var object new_cons = allocate_cons();
  454. pack = popSTACK();
  455. Car(new_cons) = pack; Cdr(new_cons) = O(all_packages);
  456. O(all_packages) = new_cons;
  457. /* finished: */
  458. clr_break_sem_2();
  459. return pack;
  460. }
  461. /* UP: Searches a symbol of given printname in the shadowing-list
  462. of a package.
  463. shadowing_lookup(string,invert,pack,&sym)
  464. > string: string
  465. > invert: whether to implicitly case-invert the string
  466. > pack: package
  467. < result: true, if found.
  468. < sym: the symbol from the shadowing-list, that has the given printname
  469. (if found) */
  470. local bool shadowing_lookup (object string, bool invert, object pack, object* sym_) {
  471. var object list = ThePackage(pack)->pack_shadowing_symbols;
  472. /* traverse shadowing-list: */
  473. while (consp(list)) {
  474. if ((invert ? string_eq_inverted : string_eq)
  475. (string,Symbol_name(Car(list))))
  476. goto found;
  477. list = Cdr(list);
  478. }
  479. return false; /* not found */
  480. found: /* found */
  481. if (sym_) { *sym_ = Car(list); }
  482. return true;
  483. }
  484. /* UP: Searches a given symbol in the shadowing-list of a package.
  485. shadowing_find(sym,pack)
  486. > sym: symbol
  487. > pack: package
  488. < result: true if found. */
  489. #define shadowing_find(s,p) (!nullp(memq(s,ThePackage(p)->pack_shadowing_symbols)))
  490. /* UP: Adds a symbol to the shadowing-list of a package, that does not yet
  491. contain a symbol of the same name.
  492. shadowing_insert(&sym,&pack)
  493. > sym: symbol (in STACK)
  494. > pack: package (in STACK)
  495. < sym: symbol, EQ to the old one
  496. < pack: package, EQ to the old one
  497. can trigger GC */
  498. local maygc void shadowing_insert (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  499. /* insert a new cons with symbol as CAR in front of the shadowing-symbols: */
  500. var object new_cons = allocate_cons();
  501. var object pack = *pack_;
  502. Car(new_cons) = *sym_;
  503. Cdr(new_cons) = ThePackage(pack)->pack_shadowing_symbols;
  504. ThePackage(pack)->pack_shadowing_symbols = new_cons;
  505. }
  506. /* UP: Removes a symbol of given name from the shadowing-list
  507. of a package.
  508. shadowing_delete(string,invert,pack)
  509. > string: string
  510. > invert: whether to implicitly case-invert the string
  511. > pack: package */
  512. local void shadowing_delete (object string, bool invert, object pack) {
  513. var gcv_object_t* listptr = &ThePackage(pack)->pack_shadowing_symbols;
  514. var object list = *listptr;
  515. /* list = *listptr traverses the shadowing-list */
  516. while (consp(list)) {
  517. if ((invert ? string_eq_inverted : string_eq)
  518. (string,Symbol_name(Car(list))))
  519. goto found;
  520. listptr = &Cdr(list); list = *listptr;
  521. }
  522. /* no symbol with this name found, done. */
  523. return;
  524. found:
  525. /* equality: remove. After that we are done, because there can be only
  526. one symbol of the same printname in the shadowing-list. */
  527. *listptr = Cdr(list); /* replace list with Cdr(list) */
  528. return;
  529. }
  530. /* UP: Tests, if a symbol in a package is accessible and is not
  531. shadowed by another symbol of the same name.
  532. accessiblep(sym,pack)
  533. > sym: symbol
  534. > pack: package
  535. < result: true if sym is accessible in pack and is not shadowed,
  536. else false */
  537. global bool accessiblep (object sym, object pack) {
  538. /* method:
  539. First, search a symbol of equal name in the shadowing-list;
  540. if not found, search the symbol among the present ones and
  541. then among the inherited symbols.
  542. Other possible method (not realized here):
  543. If the home-package of sym is equal to pack, sym is present in pack,
  544. done. Else search a present symbol of equal name.
  545. sym found -> finished.
  546. Found another one -> sym is not in the shadowing-list and
  547. thus not visible.
  548. none found -> search sym among the inherited symbols. */
  549. var object shadowingsym;
  550. /* First, search in the shadowing-list of pack: */
  551. if (shadowing_lookup(Symbol_name(sym),false,pack,&shadowingsym)) {
  552. /* shadowingsym = symbol, found in the shadowing-list */
  553. return (eq(shadowingsym,sym)); /* compare with sym */
  554. } else { /* no symbol of equal name in the shadowing-list */
  555. /* Search among the internal symbols: */
  556. if (symtab_find(sym,ThePackage(pack)->pack_internal_symbols))
  557. return true;
  558. /* Search among the external symbols: */
  559. if (symtab_find(sym,ThePackage(pack)->pack_external_symbols))
  560. return true;
  561. /* Search among the external symbols of the packages from the use-list: */
  562. if (inherited_find(sym,pack))
  563. return true;
  564. return false;
  565. }
  566. }
  567. /* UP: tests, if a symbol is accessible in a package as
  568. external symbol.
  569. externalp(sym,pack)
  570. > sym: symbol
  571. > pack: package
  572. < result:
  573. true if sym is accessible in pack as external symbol,
  574. (in this case, sym is not shadowed, because a symbol,
  575. possibly shadowing sym, should be listed in shadowing-symbols(pack),
  576. according to the consistency-rules 5 and 7 identical with sym),
  577. else false */
  578. global bool externalp (object sym, object pack) {
  579. return symtab_find(sym,ThePackage(pack)->pack_external_symbols);
  580. }
  581. /* UP: locates an external symbol with a given printname in a package.
  582. find_external_symbol(string,invert,pack,&sym)
  583. > string: string
  584. > invert: whether to implicitly case-invert the string
  585. > pack: package
  586. < result: true, if an external symbol with that printname has been found in pack.
  587. < sym: this symbol, if found. */
  588. global bool find_external_symbol (object string, bool invert, object pack, object* sym_) {
  589. return package_lookup_ext(string,invert,pack,sym_);
  590. }
  591. /* UP: searches a package of given name or nickname
  592. find_package(string)
  593. > string: string
  594. < result: package of this name or NIL */
  595. global object find_package (object string) {
  596. var object packlistr = O(all_packages); /* traverse package-list */
  597. var object pack;
  598. while (consp(packlistr)) {
  599. pack = Car(packlistr); /* Package to be tested */
  600. /* test name: */
  601. if (string_eq(string,ThePackage(pack)->pack_name))
  602. return pack;
  603. { /* test nickname: */
  604. /* traverse nickname-list */
  605. var object nicknamelistr = ThePackage(pack)->pack_nicknames;
  606. while (consp(nicknamelistr)) {
  607. if (string_eq(string,Car(nicknamelistr)))
  608. return pack;
  609. nicknamelistr = Cdr(nicknamelistr);
  610. }
  611. }
  612. packlistr = Cdr(packlistr); /* next package */
  613. }
  614. /* not found */
  615. return NIL;
  616. }
  617. /* UP: Searches a symbol of given printname in a package.
  618. find_symbol(string,invert,pack,&sym)
  619. > string: string
  620. > invert: whether to implicitly case-invert the string
  621. > pack: package
  622. < sym: symbol, if found; else NIL
  623. < result: 0, if not found
  624. 1, if available as external symbol
  625. 2, if inherited via use-list
  626. 3, if available as internal symbol
  627. + (-4, if available in the shadowing-list) */
  628. local sintBWL find_symbol (object string, bool invert, object pack, object* sym_) {
  629. /* First search in the shadowing-list of pack: */
  630. if (shadowing_lookup(string,invert,pack,sym_)) {
  631. /* *sym_ = symbol, found in the shadowing-list */
  632. /* Search for it among the internal symbols: */
  633. if (symtab_find(*sym_,ThePackage(pack)->pack_internal_symbols))
  634. return 3-4; /* found among the internal symbols */
  635. /* Search it among the external symbols: */
  636. if (symtab_find(*sym_,ThePackage(pack)->pack_external_symbols))
  637. return 1-4; /* found among the external symbols */
  638. /* contradiction to consistency rule 5. */
  639. pushSTACK(*sym_); pushSTACK(pack);
  640. error(serious_condition,GETTEXT("~S inconsistent: symbol ~S is a shadowing symbol but not present"));
  641. } else { /* symbol not yet found */
  642. /* search among the internal symbols: */
  643. if (package_lookup_int(string,invert,pack,sym_))
  644. return 3; /* found among the internal symbols */
  645. /* search among the external symbols: */
  646. if (package_lookup_ext(string,invert,pack,sym_))
  647. return 1; /* found among the external symbols */
  648. /* search among the external packages from the use-list: */
  649. if (inherited_lookup(string,invert,pack,sym_))
  650. return 2; /* found among the inherited symbols */
  651. /* not found */
  652. *sym_ = NIL; return 0;
  653. }
  654. }
  655. /* Actually, one has to search in the shadowing-list only after
  656. one has searched among the present symbols, because the symbol in the
  657. shadowing-list is already present (consistency rule 5). */
  658. /* raise a continuable error when func(obj) was called on a locked package pack
  659. continue means "Ignore the lock and proceed"
  660. can trigger GC */
  661. local maygc void cerror_package_locked (object func, object pack, object obj) {
  662. pushSTACK(NIL); /* 7 continue-format-string */
  663. pushSTACK(S(package_error)); /* 6 error type */
  664. pushSTACK(S(Kpackage)); /* 5 :PACKAGE */
  665. if (consp(pack)) pushSTACK(Car(pack)); /* from check-redefinition */
  666. else pushSTACK(pack); /* 4 PACKAGE-ERROR slot PACKAGE */
  667. pushSTACK(NIL); /* 3 error-format-string */
  668. pushSTACK(func); /* 2 */
  669. pushSTACK(obj); /* 1 */
  670. pushSTACK(pack); /* 0 */
  671. /* CLSTEXT "can trigger GC", so it cannot be called until
  672. all the arguments have been already pushed on the STACK */
  673. STACK_7 = CLSTEXT("Ignore the lock and proceed"); /* continue-format-string */
  674. STACK_3 = CLSTEXT("~A(~S): ~S is locked"); /* error-format-string */
  675. funcall(L(cerror_of_type),8);
  676. }
  677. /* check the package lock */
  678. #define check_pack_lock(func,pack,obj) \
  679. if (pack_locked_p(pack)) cerror_package_locked(func,pack,obj)
  680. #define safe_check_pack_lock(func,pack,obj) \
  681. do { pushSTACK(pack); pushSTACK(obj); /* save */ \
  682. check_pack_lock(func, STACK_1 /*pack*/,STACK_0 /*obj*/); \
  683. obj = popSTACK(); pack = popSTACK(); /* restore */ \
  684. } while(0)
  685. /* UP: Inserts a symbol into a package, that has no symbol of the same name yet.
  686. Does not check for conflicts.
  687. make_present(sym,pack);
  688. > sym: symbol
  689. > pack: package
  690. only call, if BREAK_SEM_2 is set
  691. can trigger GC */
  692. local maygc void make_present (object sym, object pack) {
  693. if (!eq(pack,O(keyword_package))) {
  694. if (nullp(Symbol_package(sym)))
  695. Symbol_package(sym) = pack;
  696. /* Insert symbol into the internal symbols: */
  697. symtab_insert(sym,ThePackage(pack)->pack_internal_symbols);
  698. } else {
  699. if (nullp(Symbol_package(sym))) {
  700. pushSTACK(pack); /* save */
  701. sym = check_symbol_not_symbol_macro(sym);
  702. Symbol_package(sym) = pack = popSTACK();
  703. Symbol_value(sym) = sym; /* sym gets itself as value */
  704. set_const_flag(TheSymbol(sym)); /* mark as constant */
  705. }
  706. /* Insert symbol into the external symbols: */
  707. symtab_insert(sym,ThePackage(pack)->pack_external_symbols);
  708. }
  709. }
  710. /* UP: Interns a symbol with a given printname in a package.
  711. intern(string,invert,pack,&sym)
  712. > string: string
  713. > invert: whether to implicitly case-invert the string
  714. > pack: package
  715. < sym: symbol
  716. < result: 0, if not found, but newly created
  717. 1, if available as external symbol
  718. 2, if inherited via use-list
  719. 3, if available as internal symbol
  720. can trigger GC */
  721. global maygc uintBWL intern (object string, bool invert, object pack, object* sym_) {
  722. {
  723. var sintBWL result = find_symbol(string,invert,pack,sym_); /* search */
  724. if (!(result==0))
  725. return result & 3; /* found -> finished */
  726. }
  727. pushSTACK(pack); /* save package */
  728. if (pack_locked_p(pack)) {
  729. /* when STRING comes from READ, it points to a re-usable buffer
  730. that will be overwritten during the CERROR i/o
  731. therefore we must copy and save it */
  732. pushSTACK(coerce_ss(string));
  733. cerror_package_locked(S(intern),STACK_1/*pack*/,STACK_0/*string*/);
  734. string = popSTACK();
  735. }
  736. if (invert)
  737. string = string_invertcase(string);
  738. string = coerce_imm_ss(string); /* string --> immutable simple-string */
  739. var object sym = make_symbol(string); /* (make-symbol string) */
  740. pack = popSTACK();
  741. /* enter this new symbol into the package: */
  742. set_break_sem_2(); /* protect against breaks */
  743. pushSTACK(sym); /* save symbol */
  744. make_present(sym,pack); /* intern into this package */
  745. *sym_ = popSTACK();
  746. clr_break_sem_2(); /* allow breaks */
  747. return 0;
  748. }
  749. /* UP: Interns a symbol of given printname into the keyword-package.
  750. intern_keyword(string)
  751. > string: string
  752. < result: symbol, a keyword
  753. can trigger GC */
  754. global maygc object intern_keyword (object string) {
  755. var object sym;
  756. intern(string,false,O(keyword_package),&sym);
  757. return sym;
  758. }
  759. /* UP: lookup the string among the internal and, if not found,
  760. external symbols of the package PACK
  761. tab, if supplied, is the assignment that will set the table in which the
  762. STRINNG was found */
  763. #define package_lookup(string,invert,pack,res_,tab) \
  764. (symtab_lookup(string,invert,tab ThePackage(pack)->pack_internal_symbols,res_) || \
  765. symtab_lookup(string,invert,tab ThePackage(pack)->pack_external_symbols,res_))
  766. /* UP: Imports a symbol into a package and turns it into a shadowing-symbol.
  767. Possibly another present symbol in this package
  768. of the same name is uninterned.
  769. shadowing_import(&sym,&pack);
  770. > sym: symbol (in STACK)
  771. > pack: package (in STACK)
  772. < sym: symbol, EQ to the old one
  773. < pack: package, EQ to the old one
  774. can trigger GC */
  775. local maygc void shadowing_import (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  776. check_pack_lock(S(shadowing_import),*pack_,*sym_);
  777. set_break_sem_2(); /* protect against breaks */
  778. {
  779. var object sym = *sym_;
  780. var object pack = *pack_;
  781. /* Searches an internal or external symbol of the same name: */
  782. var object othersym;
  783. var object tab_found;
  784. var object string = Symbol_name(sym);
  785. pushSTACK(string); /* save string */
  786. if (package_lookup(string,false,pack,&othersym,tab_found=)) {
  787. /* a symbol othersym of the same name was
  788. already present in the package */
  789. if (!eq(othersym,sym)) { /* was it the to be imported symbol itself? */
  790. /* no -> have to take othersym away from the internal resp. */
  791. /* from the external symbols: */
  792. symtab_delete(othersym,tab_found);
  793. /* Was this symbol taken away from its home-package,
  794. its home-package must be set to NIL: */
  795. if (eq(Symbol_package(othersym),pack))
  796. Symbol_package(othersym) = NIL;
  797. /* symbol sym must be added to the package pack. */
  798. make_present(sym,pack);
  799. }
  800. } else { /* symbol sym must be added to the package pack. */
  801. make_present(sym,pack);
  802. }
  803. }
  804. /* symbol must be added to the shadowing-list of the package. */
  805. shadowing_delete(popSTACK(),false,*pack_); /* remove string from */
  806. /* the shadowing-list */
  807. shadowing_insert(sym_,pack_); /* add symbol to the shadowing-list */
  808. clr_break_sem_2(); /* allow breaks */
  809. }
  810. /* UP: Shadows in a package all symbols accessible from other packages
  811. of give name by one symbol present in this package
  812. of the same name.
  813. shadow(&sym,invert,&pack)
  814. > sym: symbol or string (in STACK)
  815. > invert: whether to implicitly case-invert the string
  816. > pack: package (in STACK)
  817. < pack: package, EQ to the old
  818. can trigger GC */
  819. local maygc void do_shadow (const gcv_object_t* sym_, bool invert, const gcv_object_t* pack_) {
  820. check_pack_lock(S(shadow),*pack_,*sym_);
  821. set_break_sem_2(); /* protect against breaks */
  822. /* Search an internal or external symbol of the same name: */
  823. var object string = /* only the name of the symbol counts. */
  824. test_stringsymchar_arg(*sym_,invert);
  825. var object pack = *pack_;
  826. pushSTACK(NIL); /* make room for othersym */
  827. pushSTACK(string); /* save string */
  828. var object othersym;
  829. if (package_lookup(string,invert,pack,&othersym,)) {
  830. STACK_1 = othersym;
  831. } else {
  832. /* not found -> create new symbol of the same name: */
  833. if (invert)
  834. string = string_invertcase(string);
  835. string = coerce_imm_ss(string); /* string --> immutable simple-string */
  836. var object othersym = make_symbol(string); /* new symbol */
  837. STACK_1 = othersym;
  838. make_present(othersym,*pack_); /* enter into the package */
  839. /* home-package of the new symbols is pack */
  840. Symbol_package(STACK_1) = *pack_;
  841. }
  842. /* stack-layout: othersym, string
  843. In the package, now symbol othersym of the same name is present.
  844. remove string from the shadowing-list */
  845. shadowing_delete(popSTACK(),invert,*pack_);
  846. /* therefore add othersym to the shadowing-list */
  847. shadowing_insert(&STACK_0,pack_);
  848. skipSTACK(1); /* forget othersym */
  849. clr_break_sem_2(); /* allow breaks */
  850. }
  851. local maygc void shadow (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  852. do_shadow(sym_,false,pack_);
  853. }
  854. local maygc void cs_shadow (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  855. do_shadow(sym_,true,pack_);
  856. }
  857. /* UP: Removes a symbol from the set of present symbols of a package
  858. and does conflict resolution if it was in the shadowing-list
  859. of this package and a name conflict arises.
  860. unintern(&sym,&pack)
  861. > sym: symbol (in STACK)
  862. > pack: package (in STACK)
  863. < sym: symbol, EQ to the old
  864. < pack: package, EQ to the old
  865. < result: T if found and deleted, NIL if nothing has been done.
  866. can trigger GC */
  867. local maygc object unintern (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  868. check_pack_lock(S(unintern),*pack_,*sym_);
  869. var object sym = *sym_;
  870. var object pack = *pack_;
  871. var object symtab;
  872. /* search sym among the internal and the external symbols: */
  873. if (symtab_find(sym,symtab=ThePackage(pack)->pack_internal_symbols)
  874. || symtab_find(sym,symtab=ThePackage(pack)->pack_external_symbols)) {
  875. /* found symbol sym in the table symtab */
  876. if (shadowing_find(sym,pack)) { /* search in the shadowing-list */
  877. /* possible conflict -> build up selection-list: */
  878. pushSTACK(symtab); /* save symboltable */
  879. pushSTACK(NIL); /* start option-list */
  880. pushSTACK(ThePackage(pack)->pack_use_list); /* traverse use-list */
  881. /* stack-layout: symboltable, OL, use-list-rest */
  882. while (mconsp(STACK_0)) {
  883. var object othersym;
  884. pack = Car(STACK_0); /* package from the use-list */
  885. STACK_0 = Cdr(STACK_0);
  886. /* search inherited symbol of the same name: */
  887. if (package_lookup_ext(Symbol_name(*sym_),false,pack,&othersym)) {
  888. /* check that othersym is not in the option-list yet */
  889. var object temp = STACK_1;
  890. while (mconsp(temp)) {
  891. if (eq(Cdr(Cdr(Car(temp))),othersym))
  892. goto next_package;
  893. temp = Cdr(temp);
  894. }
  895. /* othersym is a symbol of the same name, inherited from pack */
  896. pushSTACK(temp=ThePackage(pack)->pack_name); /* name of pack */
  897. pushSTACK(othersym); /* symbol */
  898. pushSTACK(NIL);
  899. pushSTACK(NIL); /* "symbol ~A from ~A will become a shadowing symbol" */
  900. pushSTACK(Symbol_name(othersym)); /* symbolname */
  901. pushSTACK(pack); /* package */
  902. STACK_2 = CLSTEXT("symbol ~A from ~A will become a shadowing symbol");
  903. /* (FORMAT NIL "..." symbolname packagename) */
  904. funcall(S(format),4);
  905. temp = value1;
  906. pushSTACK(temp); /* total-string */
  907. temp = allocate_cons();
  908. Car(temp) = popSTACK();
  909. Cdr(temp) = popSTACK();
  910. pushSTACK(temp); /* (cons total-string othersym) */
  911. temp = allocate_cons();
  912. Cdr(temp) = popSTACK();
  913. Car(temp) = popSTACK();
  914. /* temp = (list packagename total-string othersym) */
  915. /* STACK is correct, again */
  916. /* push to the option-list: */
  917. pushSTACK(temp);
  918. temp = allocate_cons();
  919. Car(temp) = popSTACK(); Cdr(temp) = STACK_1;
  920. STACK_1 = temp;
  921. }
  922. next_package:;
  923. }
  924. skipSTACK(1);
  925. /* option-list build-up finished.
  926. stack-layout: symboltable, OL
  927. if (length OL) >= 2, there's a conflict: */
  928. if (mconsp(STACK_0) && mconsp(Cdr(STACK_0))) {
  929. /* raise a correctable error, options is STACK_0 already */
  930. pushSTACK(*pack_); /* PACKAGE-ERROR slot PACKAGE */
  931. pushSTACK(*pack_); /* package */
  932. pushSTACK(*sym_); /* symbol */
  933. correctable_error(package_error,GETTEXT("Uninterning ~S from ~S uncovers a name conflict.\nYou may choose the symbol in favour of which to resolve the conflict."));
  934. pushSTACK(value1);
  935. } else
  936. STACK_0 = NIL;
  937. /* STACK_0 is the selection (NIL if no conflict arises) */
  938. /* stack-layout: symboltable, selection */
  939. set_break_sem_3();
  940. {
  941. var object sym = *sym_;
  942. var object pack = *pack_;
  943. /* remove symbol from symboltable: */
  944. symtab_delete(sym,STACK_1);
  945. /* if it was removed from its home-package,
  946. set the home-package to NIL: */
  947. if (eq(Symbol_package(sym),pack))
  948. Symbol_package(sym) = NIL;
  949. /* discard symbol from shadowing-list: */
  950. shadowing_delete(Symbol_name(sym),false,pack);
  951. }
  952. if (!nullp(STACK_0))
  953. /* in case of a conflict: import selected symbol: */
  954. shadowing_import(&STACK_0,pack_);
  955. skipSTACK(2); /* forget symboltable & selection */
  956. clr_break_sem_3();
  957. return T; /* that's it */
  958. } else { /* no conflict */
  959. set_break_sem_2();
  960. symtab_delete(sym,symtab); /* delete symbol */
  961. if (eq(Symbol_package(sym),pack))
  962. Symbol_package(sym) = NIL; /* maybe set home-package to NIL */
  963. clr_break_sem_2();
  964. return T;
  965. }
  966. } else /* not found */
  967. return NIL;
  968. }
  969. /* UP: raise a continuable error and query the user about how to proceed
  970. return true when an abort was requested
  971. dialog_type == 0 or 1 or 2
  972. can trigger GC */
  973. local maygc bool query_intern_conflict (object pack, object sym, object other,
  974. int dialog_type) {
  975. pushSTACK(NIL); /* place for OPTIONS */
  976. pushSTACK(pack); /* PACKAGE-ERROR slot PACKAGE */
  977. pushSTACK(other); pushSTACK(pack); pushSTACK(sym);
  978. switch (dialog_type) { /* fill OPTIONS */
  979. case 0: /* conflict */
  980. STACK_4=CLOTEXT("((IMPORT \"import it and unintern the other symbol\" . T)"
  981. " (IGNORE \"do not import it, leave undone\" . NIL))");
  982. break;
  983. case 1: /* conflict & shadowing */
  984. STACK_4=CLOTEXT("((IMPORT \"import it, unintern one other symbol and shadow the other symbols\" . T)"
  985. " (IGNORE \"do not import it, leave undone\" . NIL))");
  986. break;
  987. case 2: /* shadowing */
  988. STACK_4=CLOTEXT("((IMPORT \"import it and shadow the other symbol\" . T)"
  989. " (IGNORE \"do nothing\" . NIL))");
  990. break;
  991. default: NOTREACHED;
  992. }
  993. correctable_error(package_error,(dialog_type == 1
  994. ? GETTEXT("Importing ~S into ~S produces a name conflict with ~S and other symbols.")
  995. : GETTEXT("Importing ~S into ~S produces a name conflict with ~S.")));
  996. return nullp(value1);
  997. }
  998. /* UP: Imports a symbol into a package and does conflict resolution
  999. in case, that a name conflict arises either with a symbol
  1000. inherited from anotherpackage or with an already present symbol
  1001. in this package of the same name.
  1002. import(&sym,&pack);
  1003. > sym: symbol (in STACK)
  1004. > pack: package (in STACK)
  1005. < pack: package, EQ to the old
  1006. can trigger GC */
  1007. global maygc void import (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  1008. var object sym = *sym_;
  1009. var object pack = *pack_;
  1010. var object string = Symbol_name(sym);
  1011. var object othersym;
  1012. var object othersymtab;
  1013. /* search symbol of the same name among the internal
  1014. and the external symbols: */
  1015. if (package_lookup(string,false,pack,&othersym,othersymtab=)) {
  1016. /* othersym = symbol of the same name, found in othersymtab */
  1017. if (eq(othersym,sym)) /* the same symbol -> nothing to do */
  1018. return;
  1019. /* not the same symbol was present -> must throw out othersym and
  1020. insert the given symbol sym for it.
  1021. determine beforehand, if there are additional inherited
  1022. symbols there, and then raise Continuable Error. */
  1023. pushSTACK(string);
  1024. pushSTACK(othersym);
  1025. pushSTACK(othersymtab);
  1026. /* first calculate inherited-flag: */
  1027. var bool inheritedp = inherited_lookup(string,false,pack,NULL);
  1028. /* stack-layout: symbol-name, othersym, othersymtab. */
  1029. /* raise Continuable Error: */
  1030. if (query_intern_conflict(*pack_,*sym_,othersym,inheritedp ? 1 : 0)) {
  1031. skipSTACK(3); return; /* yes -> do not import, finished */
  1032. }
  1033. /* import: */
  1034. set_break_sem_2();
  1035. pack = *pack_;
  1036. { /* remove othersym from pack: */
  1037. var object othersym = STACK_1;
  1038. symtab_delete(othersym,STACK_0); /* remove othersym from othersymtab */
  1039. if (eq(Symbol_package(othersym),pack))
  1040. Symbol_package(othersym) = NIL; /* maybe home-package := NIL */
  1041. }
  1042. /* insert sym in pack: */
  1043. make_present(*sym_,pack);
  1044. /* remove symbols of the same name from the shadowing-list of pack: */
  1045. shadowing_delete(STACK_2,false,*pack_);
  1046. /* if inherited-flag, turn sym in pack into a shadowing-symbol: */
  1047. if (inheritedp)
  1048. shadowing_insert(sym_,pack_);
  1049. clr_break_sem_2();
  1050. skipSTACK(3); return;
  1051. } else {
  1052. /* no symbol of the same name was present.
  1053. Search a symbol of the same name, that is inherited (there is
  1054. at most one, according to the consistency rules 6 and 5): */
  1055. var object otherusedsym;
  1056. if (!inherited_lookup(string,false,pack,&otherusedsym)
  1057. || eq(otherusedsym,sym)) {
  1058. /* insert sym simply in pack: */
  1059. set_break_sem_2();
  1060. make_present(sym,pack);
  1061. clr_break_sem_2();
  1062. } else {
  1063. /* no -> raise Continuable Error and query user: */
  1064. if (query_intern_conflict(pack,sym,otherusedsym,2))
  1065. return; /* yes -> do not import, finished */
  1066. /* import: */
  1067. set_break_sem_2();
  1068. /* insert sym in pack: */
  1069. make_present(*sym_,*pack_);
  1070. /* turn sym in pack into a shadowing-symbol: */
  1071. shadowing_insert(sym_,pack_);
  1072. clr_break_sem_2();
  1073. }
  1074. }
  1075. }
  1076. /* UP: Sets a symbol back from external to internal status in
  1077. einer package.
  1078. unexport(&sym,&pack);
  1079. > sym: symbol (in STACK)
  1080. > pack: package (in STACK)
  1081. < pack: package, EQ to the old
  1082. can trigger GC */
  1083. local maygc void unexport (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  1084. check_pack_lock(S(unexport),*pack_,*sym_);
  1085. var object sym = *sym_;
  1086. var object pack = *pack_;
  1087. var object symtab;
  1088. if (symtab_find(sym,symtab=ThePackage(pack)->pack_external_symbols)) {
  1089. /* sym is external in pack */
  1090. if (eq(pack,O(keyword_package))) { /* test for keyword-package */
  1091. pushSTACK(pack); /* PACKAGE-ERROR slot PACKAGE */
  1092. pushSTACK(pack);
  1093. error(package_error,GETTEXT("UNEXPORT in ~S is illegal"));
  1094. }
  1095. set_break_sem_2();
  1096. symtab_delete(sym,symtab); /* remove sym from the external symbols */
  1097. /* therefor, insert it into the internal symbols */
  1098. symtab_insert(sym,ThePackage(pack)->pack_internal_symbols);
  1099. clr_break_sem_2();
  1100. } else {
  1101. /* Search, if the symbol is accessible at all. */
  1102. /* Search among the internal symbols: */
  1103. if (symtab_find(sym,ThePackage(pack)->pack_internal_symbols))
  1104. return;
  1105. /* Search among the external symbols of the packages from the use-list: */
  1106. if (inherited_find(sym,pack))
  1107. return;
  1108. /* not found among the accessible symbols */
  1109. pushSTACK(pack); /* PACKAGE-ERROR slot PACKAGE */
  1110. pushSTACK(pack); pushSTACK(sym);
  1111. error(package_error,
  1112. GETTEXT("UNEXPORT works only on accessible symbols, not on ~S in ~S"));
  1113. }
  1114. }
  1115. /* UP: Sets a present symbol into external status.
  1116. make_external(sym,pack);
  1117. > sym: symbol
  1118. > pack: package, in which the symbol is present
  1119. can trigger GC */
  1120. local maygc void make_external (object sym, object pack) {
  1121. if (symtab_find(sym,ThePackage(pack)->pack_external_symbols))
  1122. return; /* symbol already external -> nothing to do */
  1123. set_break_sem_2();
  1124. /* remove sym from the internal symbols */
  1125. symtab_delete(sym,ThePackage(pack)->pack_internal_symbols);
  1126. /* therefor, insert it into the external symbols */
  1127. symtab_insert(sym,ThePackage(pack)->pack_external_symbols);
  1128. clr_break_sem_2();
  1129. }
  1130. /* UP: Exports a symbol from a package
  1131. export(&sym,&pack);
  1132. > sym: symbol (in STACK)
  1133. > pack: package (in STACK)
  1134. < sym: symbol, EQ to the old
  1135. < pack: package, EQ to the old
  1136. can trigger GC */
  1137. global maygc void export (const gcv_object_t* sym_, const gcv_object_t* pack_) {
  1138. check_pack_lock(S(export),*pack_,*sym_);
  1139. var object sym = *sym_;
  1140. var object pack = *pack_;
  1141. /* search sym among the external symbols of pack: */
  1142. if (symtab_find(sym,ThePackage(pack)->pack_external_symbols))
  1143. return; /* found -> finished */
  1144. var bool import_it = false;
  1145. /* import_it = flag, if symbol has to be imported first. */
  1146. /* search sym among the internal symbols of pack: */
  1147. if (!(symtab_find(sym,ThePackage(pack)->pack_internal_symbols))) {
  1148. /* symbol sym is not present in package pack */
  1149. import_it = true;
  1150. /* Search, if it is at least accessible: */
  1151. if (!inherited_find(sym,pack)) {
  1152. /* symbol sym is not even accessible in the package pack ==>
  1153. raise correctable error: */
  1154. pushSTACK(NIL); /* place for OPTIONS */
  1155. pushSTACK(pack); /* PACKAGE-ERROR slot PACKAGE */
  1156. /* "symbol ~S has to be imported in ~S before being exported" */
  1157. pushSTACK(pack); pushSTACK(sym); pushSTACK(S(export));
  1158. STACK_4 = CLOTEXT("((IMPORT \"import the symbol first\" . T)"
  1159. " (IGNORE \"do nothing, do not export the symbol\" . NIL))");
  1160. correctable_error(package_error,GETTEXT("~S: Symbol ~S should be imported into ~S before being exported."));
  1161. if (nullp(value1)) /* NIL-option selected? */
  1162. return; /* yes -> do not export, finished */
  1163. }
  1164. }
  1165. /* Test for name-conflict: */
  1166. pushSTACK(NIL); /* conflict-resolver:=NIL */
  1167. /* stack-layout: conflict-resolver (a list of pairs (sym . pack),
  1168. for which shadowing_import has to be applied).
  1169. used-by-list is searched */
  1170. pushSTACK(ThePackage(*pack_)->pack_used_by_list);
  1171. while (mconsp(STACK_0)) {
  1172. var object usingpack = Car(STACK_0); /* USE-ing package */
  1173. STACK_0 = Cdr(STACK_0);
  1174. var object othersym;
  1175. if (find_symbol(Symbol_name(*sym_),false,usingpack,&othersym) > 0)
  1176. /* othersym is a symbol of the same name in usingpack */
  1177. if (!eq(othersym,*sym_)) {
  1178. var gcv_object_t *othersym_, *usingpack_;
  1179. /* it is not sym itself -> there is a conflict */
  1180. pushSTACK(othersym); othersym_ = &STACK_0;
  1181. pushSTACK(usingpack); usingpack_ = &STACK_0;
  1182. /* stack-layout: conflict-resolver, used-by-list-rest,
  1183. other symbol, USE-ing package. */
  1184. pushSTACK(NIL); /* space for OPTIONS */
  1185. pushSTACK(*pack_); /* PACKAGE-ERROR slot PACKAGE */
  1186. pushSTACK(usingpack); /* USE-ing package */
  1187. pushSTACK(usingpack); /* USE-ing package */
  1188. pushSTACK(othersym); /* other symbol */
  1189. pushSTACK(*pack_); /* package */
  1190. pushSTACK(*sym_); /* symbol */
  1191. { /* construct options-list: */
  1192. var object temp;
  1193. pushSTACK(ThePackage(*pack_)->pack_name); /* package name */
  1194. pushSTACK(CLSTEXT("the symbol to export, "));
  1195. pushSTACK(*sym_); /* symbol */
  1196. funcall(L(prin1_to_string),1); /* (prin1-to-string Symbol) */
  1197. pushSTACK(value1);
  1198. /* (string-concat "The new symbol " (prin1-to-string Symbol)) */
  1199. temp = string_concat(2);
  1200. pushSTACK(temp);
  1201. temp = listof(2); /* (list* symbol (string-concat ...) 'T) */
  1202. Cdr(Cdr(temp)) = T;
  1203. pushSTACK(temp);
  1204. pushSTACK(ThePackage(*usingpack_)->pack_name); /* USE-ing pack */
  1205. pushSTACK(CLSTEXT("the old symbol, "));
  1206. pushSTACK(*othersym_); /* other symbol */
  1207. /* (prin1-to-string anderesSymbol) */
  1208. funcall(L(prin1_to_string),1);
  1209. pushSTACK(value1);
  1210. /* (string-concat "The old symbol " (prin1-to-string old-symbol)) */
  1211. temp = string_concat(2);
  1212. pushSTACK(temp);
  1213. temp = listof(2); /* (list* other-symbol (string-concat ...) 'NIL) */
  1214. /* Cdr(Cdr(temp)) = NIL; not needed */
  1215. pushSTACK(temp);
  1216. temp = listof(2); /* (list (list s1 ... 'T) (list s2 ... 'NIL)) */
  1217. STACK_6 = temp; /* options */
  1218. }
  1219. correctable_error(package_error,GETTEXT("Exporting ~S from ~S produces a name conflict with ~S from ~S.\nYou may choose which symbol should be accessible in ~S."));
  1220. pushSTACK(nullp(value1)?STACK_1/*other symbol*/:*sym_);/*solvingsym*/
  1221. { /* extend conflict-resolver with (solvingsym . usingpack) : */
  1222. var object new_cons = allocate_cons();
  1223. Car(new_cons) = popSTACK(); /* solvingsym */
  1224. Cdr(new_cons) = popSTACK(); /* usingpack */
  1225. /* new_cons = (cons solvingsym usingpack) */
  1226. /* cons in front of conflict-resolver: */
  1227. STACK_0 = new_cons;
  1228. new_cons = allocate_cons();
  1229. Car(new_cons) = popSTACK(); /* (solvingsym . usingpack) */
  1230. Cdr(new_cons) = STACK_1;
  1231. STACK_1 = new_cons;
  1232. }
  1233. /* stack-layout: conflict-resolver, used-by-list-rest. */
  1234. }
  1235. }
  1236. skipSTACK(1);
  1237. /* stack-layout: conflict-resolver. */
  1238. /* Now maybe import symbol sym: */
  1239. if (import_it) {
  1240. /* import sym in pack: */
  1241. import(sym_,pack_);
  1242. /* This importing can be aborted by CERROR.
  1243. An abort is not dangerous at this point, because up to now
  1244. the symbol is only internal in the package (except if it is
  1245. the KEYWORD package, that can not be USE-ed). */
  1246. }
  1247. set_break_sem_3(); /* protect against breaks */
  1248. /* now resolve the conflicts: */
  1249. while (mconsp(STACK_0)) {
  1250. var object cons_sym_pack = Car(STACK_0);
  1251. STACK_0 = Cdr(STACK_0);
  1252. pushSTACK(Car(cons_sym_pack)); /* solvingsym */
  1253. pushSTACK(Cdr(cons_sym_pack)); /* usingpack */
  1254. shadowing_import(&STACK_1,&STACK_0); /* import and shadow */
  1255. skipSTACK(2);
  1256. }
  1257. skipSTACK(1);
  1258. make_external(*sym_,*pack_); /* make sym in pack external */
  1259. clr_break_sem_3(); /* allow breaks */
  1260. }
  1261. /* UP: Applies a function to all symbols in a symboltable.
  1262. (In the worst case, this function may delete the symbol via symtab_delete
  1263. from the table.)
  1264. map_symtab(fun,symtab);
  1265. > fun: function with one argument
  1266. > symtab: symboltable
  1267. can trigger GC */
  1268. local maygc void map_symtab (object fun, object symtab) {
  1269. pushSTACK(fun); /* function */
  1270. pushSTACK(Symtab_table(symtab)); /* table vector */
  1271. /* number of entries */
  1272. var uintL size = posfixnum_to_V(Symtab_size(symtab));
  1273. var gcv_object_t* offset = 0; /* offset = sizeof(gcv_object_t)*index */
  1274. var uintC count;
  1275. dotimespC(count,size, {
  1276. var object entry = /* entry with number index in table */
  1277. *(gcv_object_t*)(pointerplus(&TheSvector(STACK_0)->data[0],(aint)offset));
  1278. if (atomp(entry)) {
  1279. if (!nullp(entry)) {
  1280. /* entry is a symbol /= NIL */
  1281. pushSTACK(entry); funcall(STACK_2,1); /* apply function */
  1282. }
  1283. } else {
  1284. /* process non-empty symbol list */
  1285. pushSTACK(entry);
  1286. do {
  1287. var object listr = STACK_0;
  1288. STACK_0 = Cdr(listr);
  1289. /* apply function to symbol */
  1290. pushSTACK(Car(listr)); funcall(STACK_3,1);
  1291. } while (!matomp(STACK_0));
  1292. skipSTACK(1);
  1293. }
  1294. offset++;
  1295. });
  1296. skipSTACK(2);
  1297. }
  1298. /* UP: Applies a C-function to all symbols of a symbol table.
  1299. (In the worst case, this function may delete the symbol via symtab_delete
  1300. from the table.)
  1301. map_symtab_c(fun,data,symtab);
  1302. > fun: function with two arguments, may trigger GC
  1303. > data: first argument for the function
  1304. > symtab: symbol table
  1305. can trigger GC */
  1306. typedef maygc void one_sym_function_t (void* data, object sym);
  1307. local maygc void map_symtab_c (one_sym_function_t* fun, void* data, object symtab) {
  1308. pushSTACK(Symtab_table(symtab)); /* table vector */
  1309. /* number of entries */
  1310. var uintL size = posfixnum_to_V(Symtab_size(symtab));
  1311. var gcv_object_t* offset = 0; /* offset = sizeof(gcv_object_t)*index */
  1312. var uintC count;
  1313. dotimespC(count,size, {
  1314. var object entry = /* entry with number index in table */
  1315. *(gcv_object_t*)(pointerplus(&TheSvector(STACK_0)->data[0],(aint)offset));
  1316. if (atomp(entry)) {
  1317. if (!nullp(entry)) { /* entry is a symbol /= NIL */
  1318. (*fun)(data,entry); /* apply function */
  1319. }
  1320. } else { /* process non-empty symbol list */
  1321. pushSTACK(entry);
  1322. do {
  1323. var object listr = STACK_0;
  1324. STACK_0 = Cdr(listr);
  1325. (*fun)(data,Car(listr)); /* apply function to symbol */
  1326. } while (!matomp(STACK_0));
  1327. skipSTACK(1);
  1328. }
  1329. offset++;
  1330. });
  1331. skipSTACK(1);
  1332. }
  1333. /* UP: Effectuates, that all external symbols of a given list of packages
  1334. become implicitly accessible in a given package.
  1335. use_package(packlist,pack);
  1336. > packlist: list of packages, that are to be USE-ed
  1337. > pack: package
  1338. the list packlist is thereby destroyed!
  1339. can trigger GC */
  1340. local one_sym_function_t use_package_aux;
  1341. local maygc void use_package (object packlist, object pack) {
  1342. safe_check_pack_lock(S(use_package),pack,packlist);
  1343. { /* packlist := (delete-duplicates packlist :test #'eq) : */
  1344. var object packlist1 = packlist;
  1345. while (consp(packlist1)) {
  1346. var object to_delete = Car(packlist1);
  1347. /* remove to_delete destructively from (cdr packlist1) : */
  1348. var object packlist2 = packlist1; /* starts at packlist1 */
  1349. var object packlist3; /* always = (cdr packlist2) */
  1350. while (consp(packlist3=Cdr(packlist2))) {
  1351. if (eq(Car(packlist3),to_delete)) {
  1352. /* remove (car packlist3) destructively from the list: */
  1353. Cdr(packlist2) = Cdr(packlist3);
  1354. } else { /* advance: */
  1355. packlist2 = packlist3;
  1356. }
  1357. }
  1358. packlist1 = Cdr(packlist1);
  1359. }
  1360. }
  1361. /* Remove all the packages from packlist, that are equal to pack
  1362. or that already occur in the use-list of pack: */
  1363. pushSTACK(pack); /* save package pack */
  1364. pushSTACK(packlist); /* save list of packages to be USE-ed */
  1365. {
  1366. var gcv_object_t* packlistr_ = &STACK_0;
  1367. var object packlistr = *packlistr_;
  1368. /* packlistr loops over packlist, packlistr = *packlistr_ */
  1369. while (consp(packlistr)) {
  1370. /* test, if (car packlistr) must be discarded: */
  1371. var object pack_to_test = Car(packlistr);
  1372. if (eq(pack_to_test,pack))
  1373. goto delete_pack_to_test;
  1374. if (!nullp(memq(pack_to_test,ThePackage(pack)->pack_use_list)))
  1375. goto delete_pack_to_test;
  1376. if (true) { /* do not discard, advance: */
  1377. packlistr_ = &Cdr(packlistr); packlistr = *packlistr_;
  1378. } else { /* discard (car packlistr) : */
  1379. delete_pack_to_test:
  1380. packlistr = *packlistr_ = Cdr(packlistr);
  1381. }
  1382. }
  1383. }
  1384. /* build conflict list.
  1385. A conflict is an at least two-element list
  1386. of symbols of the same printname, together with the package,
  1387. from which this symbol is taken:
  1388. ((pack1 . sym1) ...) means, that on execution of the USE-PACKAGE
  1389. the symbole sym1,... (from pack1 etc.) would compete for
  1390. the visibility in package pack.
  1391. The conflict list is the list of all occurring conflicts. */
  1392. {
  1393. var gcv_object_t *pack_ = &STACK_1;
  1394. var gcv_object_t *packlist_ = &STACK_0;
  1395. var gcv_object_t *conflicts_, *conflict_resolver_;
  1396. pushSTACK(NIL); /* (so far empty) conflict list */
  1397. conflicts_ = &STACK_0;
  1398. /* stack-layout: pack, packlist, conflicts. */
  1399. { /* peruse package list: */
  1400. pushSTACK(*packlist_);
  1401. while (mconsp(STACK_0)) {
  1402. var object pack_to_use = Car(STACK_0);
  1403. STACK_0 = Cdr(STACK_0);
  1404. /* apply use_package_aux to all external symbols of pack_to_use: */
  1405. map_symtab_c(&use_package_aux,conflicts_,
  1406. ThePackage(pack_to_use)->pack_external_symbols);
  1407. }
  1408. skipSTACK(1);
  1409. }
  1410. { /* reconstruct conflict list: Each conflict ((pack1 . sym1) ...) is
  1411. transformed into ((packname1 pack1 . sym1) ...). */
  1412. pushSTACK(*conflicts_); /* traverse conflict list */
  1413. while (mconsp(STACK_0)) {
  1414. var object conflict = Car(STACK_0);
  1415. STACK_0 = Cdr(STACK_0);
  1416. pushSTACK(conflict); /* process conflict */
  1417. while (mconsp(STACK_0)) {
  1418. var object new_cons = allocate_cons(); /* new cons */
  1419. var object old_cons = Car(STACK_0); /* (pack . sym) */
  1420. /* replace pack by its name */
  1421. Car(new_cons) = ThePackage(Car(old_cons))->pack_name;
  1422. /* insert new-cons */
  1423. Cdr(new_cons) = old_cons; Car(STACK_0) = new_cons;
  1424. STACK_0 = Cdr(STACK_0);
  1425. }
  1426. skipSTACK(1);
  1427. }
  1428. skipSTACK(1);
  1429. }
  1430. /* conflict-list finished. */
  1431. pushSTACK(NIL); /* conflict-resolver := NIL */
  1432. conflict_resolver_ = &STACK_0;
  1433. /* stack-layout: pack, packlist, conflicts, conflict-resolver. */
  1434. /* treat conflicts with user-queries: */
  1435. while (!nullp(*conflicts_)) { /* only necessary for conflicts/=NIL */
  1436. /* raise correctable error: */
  1437. pushSTACK(Car(*conflicts_)); /* OPTIONS */
  1438. pushSTACK(*pack_); /* PACKAGE-ERROR slot PACKAGE */
  1439. pushSTACK(*pack_);
  1440. pushSTACK(Symbol_name(Cdr(Cdr(Car(Car(*conflicts_)))))); /* name */
  1441. pushSTACK(fixnum(llength(*conflicts_))); /* (length conflicts) */
  1442. pushSTACK(*pack_); pushSTACK(*packlist_); pushSTACK(S(use_package));
  1443. correctable_error(package_error,GETTEXT("(~S ~S ~S): ~S name conflicts remain\nWhich symbol with name ~S should be accessible in ~S?"));
  1444. pushSTACK(value1); /* sym */
  1445. {
  1446. var object new_cons = allocate_cons();
  1447. Car(new_cons) = popSTACK(); /* sym */
  1448. Cdr(new_cons) = *conflict_resolver_;
  1449. /* conflict-resolver := (cons sym conflict-resolver) */
  1450. *conflict_resolver_ = new_cons;
  1451. }
  1452. *conflicts_ = Cdr(*conflicts_);
  1453. }
  1454. /* stack-layout: pack, packlist, conflicts, conflict-resolver. */
  1455. { /* resolve conflicts: */
  1456. set_break_sem_3();
  1457. /* traverse conflict-resolver: */
  1458. while (mconsp(STACK_0)) {
  1459. pushSTACK(Car(STACK_0)); /* symbol from conflict-resolver */
  1460. /* make it into a shadowing-symbol in pack */
  1461. shadowing_import(&STACK_0,&STACK_4);
  1462. skipSTACK(1);
  1463. STACK_0 = Cdr(STACK_0);
  1464. }
  1465. skipSTACK(2); /* forget conflicts and conflict-resolver */
  1466. /* stack-layout: pack, packlist. */
  1467. /* traverse packlist: */
  1468. while (mconsp(STACK_0)) {
  1469. pushSTACK(Car(STACK_0)); /* pack_to_use */
  1470. { /* (push pack_to_use (package-use-list pack)) */
  1471. var object new_cons = allocate_cons();
  1472. var object pack = STACK_2;
  1473. Car(new_cons) = STACK_0; /* pack_to_use */
  1474. Cdr(new_cons) = ThePackage(pack)->pack_use_list;
  1475. ThePackage(pack)->pack_use_list = new_cons;
  1476. }
  1477. { /* (push pack (package-used-by-list pack_to_use)) */
  1478. var object new_cons = allocate_cons();
  1479. var object pack_to_use = popSTACK();
  1480. Car(new_cons) = STACK_1; /* pack */
  1481. Cdr(new_cons) = ThePackage(pack_to_use)->pack_used_by_list;
  1482. ThePackage(pack_to_use)->pack_used_by_list = new_cons;
  1483. }
  1484. STACK_0 = Cdr(STACK_0);
  1485. }
  1486. skipSTACK(2); /* forget pack and packlist */
  1487. clr_break_sem_3();
  1488. }
  1489. }
  1490. }
  1491. /* UP: Auxiliary function for use_package:
  1492. Test the argument (an external symbol from one of the packages of
  1493. packlist), if it creates a conflict. If yes, extend conflicts.
  1494. can trigger GC */
  1495. local maygc void use_package_aux (void* data, object sym) {
  1496. var gcv_object_t* localptr = (gcv_object_t*)data;
  1497. /* Pointer to local variables of use_package:
  1498. *(localptr STACKop 2) = pack,
  1499. *(localptr STACKop 1) = packlist,
  1500. *(localptr STACKop 0) = conflicts. */
  1501. var object string = Symbol_name(sym); /* printname of the passed symbol */
  1502. /* Is there a conflict between the symbols with printname = string ?
  1503. travares conflict list so far (((pack1 . sym1) ...) ...) : */
  1504. {
  1505. var object conflictsr = *(localptr STACKop 0);
  1506. while (consp(conflictsr)) {
  1507. /* conflict already treated?
  1508. (car conflictsr) = next conflict,
  1509. (car (car conflictsr)) = its first cons,
  1510. (cdr (car (car conflictsr))) = the symbol therein,
  1511. is its printname = string ? */
  1512. if (string_eq(Symbol_name(Cdr(Car(Car(conflictsr)))),string))
  1513. goto ok;
  1514. conflictsr = Cdr(conflictsr);
  1515. }
  1516. }
  1517. pushSTACK(string); /* save string */
  1518. /* build new conflict: */
  1519. { pushSTACK(NIL); } /* new conflict (still empty) */
  1520. { /* test, if a symbol of the same name is already accessible in pack: */
  1521. var object othersym;
  1522. var sintBWL code = find_symbol(string,false,*(localptr STACKop 2),&othersym);
  1523. if (code < 0) {
  1524. /* Eponymous symbol in the shadowing-list impedes conflict. */
  1525. skipSTACK(2); goto ok;
  1526. }
  1527. if (code > 0) {
  1528. /* accessible, but not shadowing ->
  1529. extend conflict by (pack . othersym) : */
  1530. pushSTACK(othersym);
  1531. {
  1532. var object temp = allocate_cons();
  1533. Cdr(temp) = popSTACK(); /* othersym */
  1534. Car(temp) = *(localptr STACKop 2); /* pack */
  1535. pushSTACK(temp); /* (pack . othersym) */
  1536. }
  1537. {
  1538. var object new_cons = allocate_cons();
  1539. Car(new_cons) = popSTACK(); Cdr(new_cons) = STACK_0;
  1540. STACK_0 = new_cons;
  1541. }
  1542. }
  1543. }
  1544. /* Test, in which packages from packlist a symbol of the same name
  1545. is external: */
  1546. {
  1547. var object packlistr = *(localptr STACKop 1); /* traverse packlist */
  1548. while (consp(packlistr)) {
  1549. var object pack_to_use = Car(packlistr);
  1550. packlistr = Cdr(packlistr);
  1551. var object othersym;
  1552. if (package_lookup_ext(STACK_1,false,pack_to_use,&othersym)) {
  1553. /* othersym has the printname = string and is
  1554. external in pack_to_use.
  1555. push (pack_to_use . othersym) on conflict: */
  1556. pushSTACK(packlistr); /* save packlistr */
  1557. pushSTACK(pack_to_use);
  1558. pushSTACK(othersym);
  1559. {
  1560. var object new_cons = allocate_cons();
  1561. Cdr(new_cons) = popSTACK(); Car(new_cons) = popSTACK();
  1562. pushSTACK(new_cons); /* (cons pack_to_use othersym) */
  1563. }
  1564. {
  1565. var object new_cons = allocate_cons();
  1566. Car(new_cons) = popSTACK();
  1567. packlistr = popSTACK();
  1568. Cdr(new_cons) = STACK_0;
  1569. /* conflict := (cons (cons pack_to_use othersym) conflict) */
  1570. STACK_0 = new_cons;
  1571. }
  1572. }
  1573. }
  1574. }
  1575. {
  1576. var object conflict = popSTACK(); /* the completed conflict */
  1577. /* conflict := (delete-duplicates conflict :key #'cdr :test #'eq): */
  1578. {
  1579. var object conflict1 = conflict;
  1580. while (consp(conflict1)) {
  1581. var object to_delete = Cdr(Car(conflict1));
  1582. /* Remove all elements with CDR=to_delete
  1583. destructively from (cdr conflict1) : */
  1584. var object conflict2 = conflict1; /* starts at conflict1 */
  1585. var object conflict3; /* always = (cdr conflict2) */
  1586. while (consp(conflict3=Cdr(conflict2))) {
  1587. if (eq(Cdr(Car(conflict3)),to_delete)) {
  1588. /* discard (car conflict3) destructively from the list: */
  1589. Cdr(conflict2) = Cdr(conflict3);
  1590. } else { /* advance: */
  1591. conflict2 = conflict3;
  1592. }
  1593. }
  1594. conflict1 = Cdr(conflict1);
  1595. }
  1596. }
  1597. /* if conflict has a length >=2 , it is consed to conflicts: */
  1598. if (consp(conflict) && mconsp(Cdr(conflict))) {
  1599. pushSTACK(conflict);
  1600. var object new_cons = allocate_cons();
  1601. Car(new_cons) = popSTACK(); /* conflict */
  1602. Cdr(new_cons) = *(localptr STACKop 0); /* conflicts */
  1603. /* conflicts := (cons conflict conflicts) */
  1604. *(localptr STACKop 0) = new_cons;
  1605. }
  1606. }
  1607. skipSTACK(1); /* forget string */
  1608. ok: ;
  1609. }
  1610. /* UP: Effectuates, that a given package is not USE-ed anymore
  1611. by (another) package.
  1612. unuse_1package(pack,qpack);
  1613. > pack: package
  1614. > qpack: package
  1615. Removes qpack from the use-list of pack
  1616. and pack from the used-by-list of qpack.
  1617. can trigger GC */
  1618. local maygc void unuse_1package (object pack, object qpack) {
  1619. safe_check_pack_lock(S(use_package),pack,qpack);
  1620. set_break_sem_2();
  1621. /* remove qpack from the use-list of pack: */
  1622. ThePackage(pack)->pack_use_list =
  1623. deleteq(ThePackage(pack)->pack_use_list,qpack);
  1624. /* remove pack from the used-by-list of qpack: */
  1625. ThePackage(qpack)->pack_used_by_list =
  1626. deleteq(ThePackage(qpack)->pack_used_by_list,pack);
  1627. clr_break_sem_2();
  1628. }
  1629. /* UP: Effectuates, that a list of given packages is not USE-ed anymore
  1630. by a given package.
  1631. unuse_package(packlist,pack);
  1632. > packlist: list of packages
  1633. > pack: package
  1634. Removes all packages from packlist from the use-list of pack
  1635. and pack from the used-by-lists of all packages from packlist.
  1636. can trigger GC */
  1637. local maygc void unuse_package (object packlist, object pack) {
  1638. pushSTACK(pack);
  1639. pushSTACK(packlist);
  1640. set_break_sem_3();
  1641. /* traverse packlist: */
  1642. while (mconsp(STACK_0)) {
  1643. unuse_1package(STACK_1,Car(STACK_0));
  1644. STACK_0 = Cdr(STACK_0);
  1645. }
  1646. clr_break_sem_3();
  1647. skipSTACK(2);
  1648. }
  1649. /* UP: returns the current package
  1650. get_current_package()
  1651. < result: current package
  1652. can trigger GC */
  1653. global maygc object get_current_package (void) {
  1654. var object pack = Symbol_value(S(packagestern)); /* value of *PACKAGE* */
  1655. if (packagep(pack) && !pack_deletedp(pack)) {
  1656. return pack;
  1657. } else {
  1658. var object newpack = /* reset *PACKAGE* */
  1659. Symbol_value(S(packagestern)) = O(default_package);
  1660. /* get_current_package() is often called by the reader,
  1661. so we need to save and restore the read buffers */
  1662. pushSTACK(O(token_buff_1)); O(token_buff_1) = NIL;
  1663. pushSTACK(O(token_buff_2)); O(token_buff_2) = NIL;
  1664. pushSTACK(NIL); /* 8: "Proceed with the new value." */
  1665. pushSTACK(S(type_error)); /* 7: error type */
  1666. pushSTACK(S(Kdatum)); /* 6: :DATUM */
  1667. pushSTACK(pack); /* 5: TYPE-ERROR slot DATUM */
  1668. pushSTACK(S(Kexpected_type)); /* 4: :EXPECTED-TYPE */
  1669. pushSTACK(S(package)); /* 3: TYPE-ERROR slot EXPECTED-TYPE */
  1670. pushSTACK(NIL); /* 2: "The value of ..." */
  1671. pushSTACK(pack); /* 1: old name */
  1672. pushSTACK(newpack); /* 0: new name */
  1673. STACK_2 = CLSTEXT("The value of *PACKAGE* was not a package and was reset. The old value was ~S. The new value is ~S.");
  1674. STACK_8 = CLSTEXT("Proceed with the new value.");
  1675. funcall(L(cerror_of_type),9);
  1676. O(token_buff_2) = popSTACK(); /* restore read buffers */
  1677. O(token_buff_1) = popSTACK();
  1678. return Symbol_value(S(packagestern));
  1679. }
  1680. }
  1681. /* UP: checks a package-argument.
  1682. Tests, if it is a package or a package name, and returns it as
  1683. a package. Else error message.
  1684. test_package_arg(obj)
  1685. > obj: argument
  1686. < result: argument turned into a package
  1687. can trigger GC */
  1688. local maygc object test_package_arg (object obj) {
  1689. restart_package_arg:
  1690. if (packagep(obj)) { /* package -> mostly OK */
  1691. if (!pack_deletedp(obj))
  1692. return obj;
  1693. pushSTACK(NIL); /* no PLACE */
  1694. pushSTACK(obj); /* PACKAGE-ERROR slot PACKAGE */
  1695. pushSTACK(obj); pushSTACK(TheSubr(subr_self)->name);
  1696. check_value(package_error,GETTEXT("~S: Package ~S has been deleted."));
  1697. obj = value1;
  1698. goto restart_package_arg;
  1699. }
  1700. if (stringp(obj))
  1701. string: { /* string -> search package with name obj: */
  1702. var object pack = find_package(obj);
  1703. if (!nullp(pack))
  1704. return pack;
  1705. pushSTACK(NIL); /* no PLACE */
  1706. pushSTACK(obj); /* PACKAGE-ERROR slot PACKAGE */
  1707. pushSTACK(obj); pushSTACK(TheSubr(subr_self)->name);
  1708. check_value(package_error,GETTEXT("~S: There is no package with name ~S"));
  1709. obj = value1;
  1710. goto restart_package_arg;
  1711. }
  1712. if (symbolp(obj)) { /* symbol -> string */
  1713. obj = Symbol_name(obj); goto string; /* use print name, no case-invert */
  1714. }
  1715. if (charp(obj)) { /* character -> string */
  1716. var object new_string = allocate_string(1);
  1717. TheSnstring(new_string)->data[0] = char_code(obj);
  1718. obj = new_string;
  1719. goto string;
  1720. }
  1721. pushSTACK(NIL); /* no PLACE */
  1722. pushSTACK(obj); /* TYPE-ERROR slot DATUM */
  1723. pushSTACK(O(type_packname)); /* TYPE-ERROR slot EXPECTED-TYPE */
  1724. pushSTACK(obj); pushSTACK(TheSubr(subr_self)->name);
  1725. check_value(type_error,GETTEXT("~S: argument should be a package or a package name, not ~S"));
  1726. obj = value1;
  1727. goto restart_package_arg;
  1728. }
  1729. LISPFUNNR(make_symbol,1) { /* (MAKE-SYMBOL printname), CLTL p. 168 */
  1730. var object arg = check_string(popSTACK());
  1731. VALUES1(make_symbol(coerce_imm_ss(arg)));
  1732. }
  1733. LISPFUNNR(find_package,1) { /* (FIND-PACKAGE name), CLTL p. 183 */
  1734. var object pack = popSTACK();
  1735. if (packagep(pack)) VALUES1(pack);
  1736. else {
  1737. var object name = test_stringsymchar_arg(pack,false);
  1738. VALUES1(find_package(name)); /* search package */
  1739. }
  1740. }
  1741. LISPFUNN(pfind_package,1) { /* (SYSTEM::%FIND-PACKAGE name) */
  1742. VALUES1(test_package_arg(popSTACK())); /* argument as package */
  1743. }
  1744. LISPFUNNR(package_name,1) { /* (PACKAGE-NAME package), CLTL p. 184 */
  1745. var object pack = popSTACK();
  1746. if (packagep(pack) && pack_deletedp(pack)) {
  1747. VALUES1(NIL);
  1748. } else {
  1749. pack = test_package_arg(pack); /* argument as package */
  1750. VALUES1(ThePackage(pack)->pack_name); /* the name */
  1751. }
  1752. }
  1753. LISPFUNNR(package_nicknames,1)
  1754. { /* (PACKAGE-NICKNAMES package), CLTL p. 184 */
  1755. var object pack = popSTACK();
  1756. if (packagep(pack) && pack_deletedp(pack)) {
  1757. VALUES1(NIL);
  1758. } else {
  1759. pack = test_package_arg(pack); /* argument as package */
  1760. /* copy nicknamelist for safety reasons */
  1761. VALUES1(copy_list(ThePackage(pack)->pack_nicknames));
  1762. }
  1763. }
  1764. /* UP: checks name and nicknames -
  1765. arguments of RENAME-PACKAGE and MAKE-PACKAGE.
  1766. Tests, if STACK_4 is a name, and turns it into a immutable simple-string.
  1767. Tests, if STACK_3 is a name or a list of names, and turns it
  1768. into a new list of immutable simple-strings.
  1769. > subr-self: caller (a SUBR)
  1770. can trigger GC */
  1771. local maygc void test_names_args (void) {
  1772. /* check name for string and turn it into a simple-string: */
  1773. STACK_4 = coerce_imm_ss(test_stringsymchar_arg(STACK_4,false));
  1774. { /* convert nickname-argument into a list: */
  1775. var object nicknames = STACK_3;
  1776. if (!boundp(nicknames)) {
  1777. STACK_3 = NIL; /* no nicknames specified -> default NIL */
  1778. } else {
  1779. if (!listp(nicknames)) {
  1780. /* nicknames not a list -> turn it into a one-element list: */
  1781. nicknames = allocate_cons();
  1782. Car(nicknames) = STACK_3;
  1783. STACK_3 = nicknames;
  1784. }
  1785. }
  1786. }
  1787. { /* check nickname(s) for string, turn into simple-strings
  1788. and build a new nicknamelist: */
  1789. pushSTACK(NIL); /* new nicknamelist := NIL */
  1790. while (mconsp(STACK_4)) {
  1791. {
  1792. var object nickname = Car(STACK_4); /* next nickname */
  1793. STACK_4 = Cdr(STACK_4);
  1794. /* as simple-string */
  1795. nickname = coerce_imm_ss(test_stringsymchar_arg(nickname,false));
  1796. /* cons in front of the new nicknamelist: */
  1797. pushSTACK(nickname);
  1798. }
  1799. var object new_cons = allocate_cons();
  1800. Car(new_cons) = popSTACK();
  1801. Cdr(new_cons) = STACK_0;
  1802. STACK_0 = new_cons;
  1803. }
  1804. var object nicknames = popSTACK();
  1805. STACK_3 = nicknames; /* new nicknamelist replaces the old */
  1806. }
  1807. }
  1808. /* (RENAME-PACKAGE pack name [nicknames]), CLTL p. 184 */
  1809. LISPFUN(rename_package,seclass_default,2,1,norest,nokey,0,NIL) {
  1810. /* Test, if pack is a package: */
  1811. STACK_2 = test_package_arg(STACK_2);
  1812. check_pack_lock(S(rename_package),STACK_2,STACK_1);
  1813. /* check name and nicknames:
  1814. name is a package designator here (but not in make-package!) */
  1815. if (packagep(STACK_1)) STACK_1 = ThePackage(STACK_1)->pack_name;
  1816. pushSTACK(NIL); pushSTACK(NIL); pushSTACK(NIL); /* dummies on the stack */
  1817. test_names_args();
  1818. skipSTACK(3);
  1819. var object pack = STACK_2;
  1820. { /* test, if a package-name-conflict arises: */
  1821. var object name = STACK_1;
  1822. var object nicknamelistr = STACK_0;
  1823. /* name loops over the names and all nicknames */
  1824. while (1) { /* find package with this name: */
  1825. var object found = find_package(name);
  1826. if (!(nullp(found) || eq(found,pack))) {
  1827. /* found, but another one than the given package: */
  1828. pushSTACK(pack); /* PACKAGE-ERROR slot PACKAGE */
  1829. pushSTACK(name); pushSTACK(TheSubr(subr_self)->name);
  1830. error(package_error,GETTEXT("~S: there is already a package named ~S"));
  1831. }
  1832. /* none or only the given package has the Name name ->
  1833. no conflict with this (nick)name, continue: */
  1834. if (atomp(nicknamelistr))
  1835. break;
  1836. name = Car(nicknamelistr); /* next nickname */
  1837. nicknamelistr = Cdr(nicknamelistr); /* shorten remaining nicknamelist */
  1838. }
  1839. }
  1840. /* There are no conflicts. */
  1841. set_break_sem_2();
  1842. ThePackage(pack)->pack_name = STACK_1;
  1843. ThePackage(pack)->pack_nicknames = STACK_0;
  1844. clr_break_sem_2();
  1845. ensure_pack_shortest_name(pack);
  1846. skipSTACK(3);
  1847. VALUES1(pack); /* pack as value */
  1848. }
  1849. LISPFUNNR(package_use_list,1) { /* (PACKAGE-USE-LIST package), CLTL p. 184 */
  1850. var object pack = test_package_arg(popSTACK()); /* argument as package */
  1851. /* copy use-list for safety reasons */
  1852. VALUES1(copy_list(ThePackage(pack)->pack_use_list));
  1853. }
  1854. LISPFUNNR(package_used_by_list,1)
  1855. { /* (PACKAGE-USED-BY-LIST package), CLTL p. 184 */
  1856. var object pack = test_package_arg(popSTACK()); /* argument as package */
  1857. /* copy used-by-list for safety reasons */
  1858. VALUES1(copy_list(ThePackage(pack)->pack_used_by_list));
  1859. }
  1860. LISPFUNNR(package_shadowing_symbols,1)
  1861. { /* (PACKAGE-SHADOWING-SYMBOLS package), CLTL p. 184 */
  1862. var object pack = test_package_arg(popSTACK()); /* argument as package */
  1863. /* copy shadowing-list for safety reasons */
  1864. VALUES1(copy_list(ThePackage(pack)->pack_shadowing_symbols));
  1865. }
  1866. /* (EXT:PACKAGE-CASE-SENSITIVE-P package) */
  1867. LISPFUNNR(package_case_sensitive_p,1) {
  1868. var object pack = test_package_arg(popSTACK());
  1869. VALUES_IF(pack_casesensitivep(pack));
  1870. }
  1871. /* ((SETF EXT:PACKAGE-CASE-SENSITIVE-P) value package) */
  1872. LISPFUNN(set_package_case_sensitive_p,2) {
  1873. var object pack = test_package_arg(popSTACK());
  1874. var bool value = !nullp(popSTACK());
  1875. if (value) mark_pack_casesensitive(pack);
  1876. else mark_pack_caseinsensitive(pack);
  1877. VALUES_IF(value);
  1878. }
  1879. /* (EXT:PACKAGE-CASE-INVERTED-P package) */
  1880. LISPFUNNR(package_case_inverted_p,1) {
  1881. var object pack = test_package_arg(popSTACK());
  1882. VALUES_IF(pack_caseinvertedp(pack));
  1883. }
  1884. /* ((SETF EXT:PACKAGE-CASE-INVERTED-P) value package) */
  1885. LISPFUNN(set_package_case_inverted_p,2) {
  1886. var object pack = test_package_arg(popSTACK());
  1887. var bool value = !nullp(popSTACK());
  1888. if (value) mark_pack_caseinverted(pack);
  1889. else mark_pack_casepreserved(pack);
  1890. VALUES_IF(value);
  1891. }
  1892. /* (SYS::PACKAGE-DOCUMENTATION package) */
  1893. LISPFUNNR(package_documentation,1) {
  1894. var object pack = test_package_arg(popSTACK());
  1895. VALUES1(ThePackage(pack)->pack_docstring);
  1896. }
  1897. /* ((SETF SYS::PACKAGE-DOCUMENTATION) new-value package)
  1898. documentation is either a doc-string or a list (doc-string impnotes-id) */
  1899. LISPFUNN(set_package_documentation,2) {
  1900. STACK_0 = test_package_arg(STACK_0);
  1901. if (!listp(STACK_1)) STACK_1 = check_string(STACK_1);
  1902. VALUES1(ThePackage(STACK_0)->pack_docstring = STACK_1);
  1903. skipSTACK(2);
  1904. }
  1905. LISPFUNNR(package_shortest_name,1)
  1906. { /* (EXT:PACKAGE-SHORTEST-NAME package) */
  1907. var object pack = test_package_arg(popSTACK());
  1908. VALUES1(ThePackage(pack)->pack_shortest_name);
  1909. }
  1910. LISPFUNNR(package_lock,1)
  1911. { /* (EXT:PACKAGE-LOCK package) */
  1912. var object pack = test_package_arg(popSTACK());
  1913. VALUES_IF(pack_locked_p(pack));
  1914. }
  1915. /* ((SETF EXT:PACKAGE-LOCK) lock package) */
  1916. LISPFUNN(set_package_lock,2) {
  1917. var bool unlock_p = nullp(STACK_1);
  1918. var object pack = STACK_0;
  1919. if (mconsp(pack)) {
  1920. while (mconsp(STACK_0)) {
  1921. var object pa = test_package_arg(Car(STACK_0)); STACK_0 = Cdr(STACK_0);
  1922. if (unlock_p) mark_pack_unlocked(pa);
  1923. else mark_pack_locked(pa);
  1924. }
  1925. } else if (nullp(pack)) { /* do nothing - package list was empty */
  1926. } else {
  1927. pack = test_package_arg(pack);
  1928. if (unlock_p) mark_pack_unlocked(pack);
  1929. else mark_pack_locked(pack);
  1930. }
  1931. skipSTACK(2);
  1932. VALUES_IF(!unlock_p);
  1933. }
  1934. /* barf when SYMBOL is an unaccessible special variable
  1935. being modified from a non-home package.
  1936. See compiler.lisp:set-check-lock.
  1937. can trigger GC */
  1938. #define SYM_VAL_LOCK(symbol,pack) \
  1939. (!nullp(pack) && !eq(pack,Symbol_value(S(packagestern))) /* non-home */ \
  1940. && special_var_p(TheSymbol(symbol)) /* special */ \
  1941. && !externalp(symbol,pack) /* for IN-PACKAGE forms */ \
  1942. && !accessiblep(symbol,Symbol_value(S(packagestern)))) /* accessible */
  1943. global maygc void symbol_value_check_lock (object caller, object symbol) {
  1944. var object pack = Symbol_package(symbol);
  1945. if (SYM_VAL_LOCK(symbol,pack))
  1946. check_pack_lock(caller,pack,symbol);
  1947. }
  1948. LISPFUNN(symbol_value_lock,1) { /* SYS::SYMBOL-VALUE-LOCK */
  1949. var object symb = check_symbol(popSTACK());
  1950. var object pack = Symbol_package(symb);
  1951. VALUES_IF(SYM_VAL_LOCK(symb,pack) && pack_locked_p(pack));
  1952. }
  1953. /* (SYSTEM::CHECK-PACKAGE-LOCK caller package symbol)
  1954. when FUNCTION is (P)SETQ, calls symbol_value_check_lock() */
  1955. LISPFUNN(check_package_lock,3) {
  1956. if (mconsp(STACK_1)) { /* package is actually a list of packages */
  1957. var bool locked = true;
  1958. var object list = STACK_1;
  1959. /* for the package list to be "locked", _all_ members must be locked
  1960. non-package members mean that the argument was a defmethod spec like
  1961. (eql 1), which means unlocked: you can always redefine such methods */
  1962. while (locked && mconsp(list)) {
  1963. locked = (packagep(Car(list)) ? pack_locked_p(Car(list)) : false);
  1964. list = Cdr(list);
  1965. }
  1966. if (locked) /* all packages are locked --> error */
  1967. cerror_package_locked(STACK_2,STACK_1,STACK_0);
  1968. } else if (packagep(STACK_1)) /* just one package - check it */
  1969. check_pack_lock(STACK_2,STACK_1,STACK_0);
  1970. skipSTACK(3);
  1971. mv_count = 0;
  1972. }
  1973. LISPFUNNR(list_all_packages,0)
  1974. { /* (LIST-ALL-PACKAGES) returns a list of all packages, CLTL p. 184 */
  1975. VALUES1(reverse(O(all_packages))); /* (copy of the list, as a precaution) */
  1976. }
  1977. /* UP: check the last argument &optional (pack *package*) of
  1978. a LISP-function.
  1979. test_optional_package_arg()
  1980. > STACK_0: last argument
  1981. < STACK_0: argument transformed into a package
  1982. can trigger GC */
  1983. local maygc void test_optional_package_arg (void) {
  1984. var object pack = STACK_0;
  1985. if (!boundp(pack)) {
  1986. STACK_0 = get_current_package(); /* default is the value of *PACKAGE* */
  1987. } else {
  1988. STACK_0 = test_package_arg(pack);
  1989. }
  1990. }
  1991. /* UP: Check of the arguments of INTERN and FIND-SYMBOL.
  1992. test_intern_args()
  1993. can trigger GC */
  1994. local maygc void test_intern_args (void) {
  1995. STACK_1 = check_string(STACK_1); /* test string */
  1996. test_optional_package_arg(); /* test package */
  1997. }
  1998. /* UP: Transforms a INTERN/FIND-SYMBOL - result into a keyword.
  1999. intern_result(code)
  2000. > code : flag as for intern and find_symbol
  2001. < result : corresponding keyword */
  2002. local object intern_result (uintBWL code) {
  2003. switch (code) {
  2004. case 0: { return NIL; } /* 0 -> NIL */
  2005. case 1: { return S(Kexternal); } /* 1 -> :EXTERNAL */
  2006. case 2: { return S(Kinherited); } /* 2 -> :INHERITED */
  2007. case 3: { return S(Kinternal); } /* 3 -> :INTERNAL */
  2008. default: NOTREACHED;
  2009. }
  2010. }
  2011. /* (INTERN string [package]) and its case-inverted variant */
  2012. local maygc Values do_intern (bool invert) {
  2013. test_intern_args(); /* test arguments */
  2014. var object pack = popSTACK();
  2015. var object string = popSTACK();
  2016. #if !defined(VALUE1_EXTRA)
  2017. var uintBWL code = intern(string,invert,pack,&value1); /* symbol to value1 */
  2018. #else
  2019. var object value;
  2020. var uintBWL code = intern(string,invert,pack,&value); /* Symbol to value */
  2021. value1 = value;
  2022. #endif
  2023. value2 = intern_result(code); mv_count=2; /* two values */
  2024. }
  2025. /* (INTERN string [package]), CLTL p. 184 */
  2026. LISPFUN(intern,seclass_default,1,1,norest,nokey,0,NIL) {
  2027. do_intern(false);
  2028. }
  2029. /* (CS-COMMON-LISP:INTERN string [package]) */
  2030. LISPFUN(cs_intern,seclass_default,1,1,norest,nokey,0,NIL) {
  2031. do_intern(true);
  2032. }
  2033. /* (FIND-SYMBOL string [package]) and its case-inverted variant */
  2034. local maygc Values do_find_symbol (bool invert) {
  2035. test_intern_args(); /* test arguments */
  2036. var object pack = popSTACK();
  2037. var object string = popSTACK();
  2038. #if !defined(VALUE1_EXTRA)
  2039. var uintBWL code = find_symbol(string,invert,pack,&value1) & 3; /* symbol to value1 */
  2040. #else
  2041. var object value;
  2042. var uintBWL code = find_symbol(string,invert,pack,&value) & 3; /* symbol to value */
  2043. value1 = value;
  2044. #endif
  2045. value2 = intern_result(code); mv_count=2; /* two values */
  2046. }
  2047. /* (FIND-SYMBOL string [package]), CLTL p. 185 */
  2048. LISPFUN(find_symbol,seclass_read,1,1,norest,nokey,0,NIL)
  2049. {
  2050. do_find_symbol(false);
  2051. }
  2052. /* (CS-COMMON-LISP:FIND-SYMBOL string [package]) */
  2053. LISPFUN(cs_find_symbol,seclass_read,1,1,norest,nokey,0,NIL)
  2054. {
  2055. do_find_symbol(true);
  2056. }
  2057. /* (UNINTERN symbol [package]), CLTL p. 185 */
  2058. LISPFUN(unintern,seclass_default,1,1,norest,nokey,0,NIL) {
  2059. /* test symbol: */
  2060. STACK_1 = check_symbol(STACK_1);
  2061. /* test package: */
  2062. test_optional_package_arg();
  2063. /* unintern: */
  2064. VALUES1(unintern(&STACK_1,&STACK_0));
  2065. skipSTACK(2);
  2066. }
  2067. /* UP: Dispatcher of a function like EXPORT, UNEXPORT, IMPORT, SHADOWING-IMPORT
  2068. or SHADOW. tests, if the first argument is a symbol-list, if
  2069. the second argument (default: *PACKAGE*) is a package, and applies the
  2070. subroutine to each of the symbols. Return 1 value T.
  2071. apply_symbols(&fun);
  2072. specification of the subroutine fun:
  2073. fun(&sym,&pack);
  2074. > sym: symbol (in STACK)
  2075. > pack: package (in STACK)
  2076. < pack: package, EQ to the old one
  2077. can trigger GC
  2078. < STACK: cleaned up
  2079. can trigger GC */
  2080. typedef maygc void sym_pack_function_t (const gcv_object_t* sym_, const gcv_object_t* pack_);
  2081. local maygc Values apply_symbols (sym_pack_function_t* fun) {
  2082. { /* test, if the first argument is a symbol-list or a symbol: */
  2083. var object symarg = STACK_1;
  2084. /* test for symbol: */
  2085. if (symbolp(symarg))
  2086. goto ok;
  2087. if ((fun == &shadow || fun == &cs_shadow)
  2088. && (stringp(symarg) || charp(symarg)))
  2089. goto ok;
  2090. /* test for symbol-list: */
  2091. while (consp(symarg)) { /* symarg loops over STACK_1 */
  2092. if (!(symbolp(Car(symarg))
  2093. || ((fun == &shadow || fun == &cs_shadow)
  2094. && (stringp(Car(symarg)) || charp(Car(symarg))))))
  2095. goto not_ok;
  2096. symarg = Cdr(symarg);
  2097. }
  2098. if (!nullp(symarg))
  2099. goto not_ok; /* list correctly finished? */
  2100. goto ok; /* correct symbol-list */
  2101. not_ok:
  2102. pushSTACK(STACK_1); pushSTACK(TheSubr(subr_self)->name);
  2103. error(error_condition,GETTEXT("~S: argument should be a symbol or a list of symbols, not ~S"));
  2104. ok: ;
  2105. }
  2106. /* test package: */
  2107. test_optional_package_arg();
  2108. /* stack-layout: symarg, pack. */
  2109. /* apply fun to all symbols: */
  2110. if (matomp(STACK_1)) {
  2111. if (nullp(STACK_1)) {
  2112. /* ANSI CL 11.1.1. says
  2113. "Where an operator takes an argument that is either a symbol or a list of
  2114. symbols, an argument of nil is treated as an empty list of symbols." */
  2115. } else {
  2116. /* single symbol */
  2117. /* stack-layout: sym, pack. */
  2118. (*fun)(&STACK_1,&STACK_0);
  2119. }
  2120. skipSTACK(2);
  2121. } else {
  2122. /* non-empty symbol-list */
  2123. pushSTACK(NIL);
  2124. do {
  2125. var object symlistr = STACK_2;
  2126. STACK_2 = Cdr(symlistr);
  2127. STACK_0 = Car(symlistr); /* symbol */
  2128. /* stack-layout: symlistr, pack, sym. */
  2129. (*fun)(&STACK_0,&STACK_1);
  2130. } while (!matomp(STACK_2));
  2131. skipSTACK(3);
  2132. }
  2133. /* finish: */
  2134. VALUES1(T);
  2135. }
  2136. /* (EXPORT symbols [package]), CLTL p. 186 */
  2137. LISPFUN(export,seclass_default,1,1,norest,nokey,0,NIL) {
  2138. return_Values apply_symbols(&export);
  2139. }
  2140. /* (UNEXPORT symbols [package]), CLTL p. 186 */
  2141. LISPFUN(unexport,seclass_default,1,1,norest,nokey,0,NIL) {
  2142. return_Values apply_symbols(&unexport);
  2143. }
  2144. /* (IMPORT symbols [package]), CLTL p. 186 */
  2145. LISPFUN(import,seclass_default,1,1,norest,nokey,0,NIL) {
  2146. return_Values apply_symbols(&import);
  2147. }
  2148. /* (SHADOWING-IMPORT symbols [package]), CLTL p. 186 */
  2149. LISPFUN(shadowing_import,seclass_default,1,1,norest,nokey,0,NIL) {
  2150. return_Values apply_symbols(&shadowing_import);
  2151. }
  2152. /* (SHADOW symbols [package]), CLTL p. 186 */
  2153. LISPFUN(shadow,seclass_default,1,1,norest,nokey,0,NIL) {
  2154. return_Values apply_symbols(&shadow);
  2155. }
  2156. /* (CS-COMMON-LISP:SHADOW symbols [package]) */
  2157. LISPFUN(cs_shadow,seclass_default,1,1,norest,nokey,0,NIL) {
  2158. return_Values apply_symbols(&cs_shadow);
  2159. }
  2160. /* UP: Preparation of the arguments of USE-PACKAGE and UNUSE-PACKAGE.
  2161. The first argument STACK_1 is turned into a (newly created)
  2162. list of packages, the second argument STACK_0 is checked.
  2163. can trigger GC */
  2164. local maygc void prepare_use_package (void) {
  2165. /* check second argument (package) : */
  2166. test_optional_package_arg();
  2167. { /* check first argument (package or package-list) : */
  2168. var object packs_to_use = STACK_1;
  2169. if (!listp(packs_to_use)) {
  2170. /* packs_to_use not a list -> turn it into a one-element list: */
  2171. pushSTACK(test_package_arg(packs_to_use)); /* single package */
  2172. var object new_cons = allocate_cons();
  2173. Car(new_cons) = popSTACK();
  2174. STACK_1 = new_cons;
  2175. } else { /* packs_to_use a list -> build up new package-list: */
  2176. pushSTACK(NIL); /* start with NIL */
  2177. while (mconsp(STACK_2)) {
  2178. var object packlistr = STACK_2;
  2179. STACK_2 = Cdr(packlistr);
  2180. pushSTACK(test_package_arg(Car(packlistr))); /* next package */
  2181. var object new_cons = allocate_cons();
  2182. Car(new_cons) = popSTACK();
  2183. Cdr(new_cons) = STACK_0;
  2184. STACK_0 = new_cons;
  2185. }
  2186. var object packlist = popSTACK(); /* new package-list */
  2187. STACK_1 = packlist;
  2188. }
  2189. }
  2190. }
  2191. /* (USE-PACKAGE packs-to-use [package]), CLTL p. 187 */
  2192. LISPFUN(use_package,seclass_default,1,1,norest,nokey,0,NIL) {
  2193. prepare_use_package();
  2194. var object pack = popSTACK();
  2195. var object packlist = popSTACK();
  2196. use_package(packlist,pack);
  2197. VALUES1(T);
  2198. }
  2199. /* (UNUSE-PACKAGE packs-to-use [package]), CLTL p. 187 */
  2200. LISPFUN(unuse_package,seclass_default,1,1,norest,nokey,0,NIL) {
  2201. prepare_use_package();
  2202. var object pack = popSTACK();
  2203. var object packlist = popSTACK();
  2204. unuse_package(packlist,pack);
  2205. VALUES1(T);
  2206. }
  2207. /* UP: Corrects a package(nick)name.
  2208. > name: Desired package-name (simple-string)
  2209. > nickname_p: is this a name or a nickname
  2210. < result: not yet existing package-name
  2211. or NIL if CONTINUE restart is selected
  2212. can trigger GC */
  2213. local maygc object correct_packname (object name, bool nickname_p) {
  2214. var object pack;
  2215. while (!nullp(pack=find_package(name))) {
  2216. /* package with this name already exists */
  2217. pushSTACK(NIL); /* OPTIONS */
  2218. pushSTACK(pack); /* PACKAGE-ERROR slot package */
  2219. pushSTACK(name); pushSTACK(TheSubr(subr_self)->name);
  2220. /* fill OPTIONS */
  2221. pushSTACK(S(continue)); /* restart name */
  2222. pushSTACK(nickname_p ? CLSTEXT("discard this nickname")
  2223. : CLSTEXT("return the existing package"));
  2224. var object tmp = listof(2);
  2225. pushSTACK(tmp);
  2226. pushSTACK(S(read)); /* restart name */
  2227. pushSTACK(nickname_p ? CLSTEXT("input another nickname")
  2228. : CLSTEXT("input another name"));
  2229. pushSTACK(S(prompt_for_new_value)); /* interactive function */
  2230. pushSTACK(NIL); /* place */
  2231. tmp = listof(4);
  2232. pushSTACK(tmp);
  2233. tmp = listof(2); STACK_3 = tmp; /* options list */
  2234. correctable_error(package_error,GETTEXT("~S: a package with name ~S already exists."));
  2235. if (nullp(value1)) return NIL; /* continue */
  2236. name = test_stringsymchar_arg(value1,false);
  2237. }
  2238. return coerce_imm_ss(name);
  2239. }
  2240. /* UP for MAKE-PACKAGE and %IN-PACKAGE:
  2241. Builds a new package and returns it as value.
  2242. > STACK_4: name-argument
  2243. > STACK_3: nicknames-argument
  2244. > STACK_2: uselist-argument
  2245. > STACK_1: case-sensitive-argument
  2246. > STACK_0: case-inverted-argument
  2247. removes the 5 STACK elements
  2248. can trigger GC */
  2249. local maygc void in_make_package (bool case_inverted) {
  2250. /* transform name into simple-string and
  2251. nicknames into a new simple-string-list: */
  2252. test_names_args();
  2253. var object new_name = correct_packname(STACK_4,false);
  2254. if (nullp(new_name)) { /* CONTINUE: re-use the existing package */
  2255. VALUES1(find_package(STACK_4));
  2256. skipSTACK(5);
  2257. return;
  2258. } else /* corrected: replace */
  2259. STACK_4 = new_name;
  2260. /* check nicknames and maybe adjust: */
  2261. pushSTACK(STACK_3);
  2262. while (mconsp(STACK_0)) {
  2263. var object correct_nick = correct_packname(Car(STACK_0),true);
  2264. Car(STACK_0) = correct_nick;
  2265. STACK_0 = Cdr(STACK_0);
  2266. }
  2267. skipSTACK(1);
  2268. STACK_3 = deleteq(STACK_3,NIL);
  2269. /* (DELETE-DUPLICATES NICKNAMES :TEST (FUNCTION STRING=)) */
  2270. pushSTACK(STACK_3); pushSTACK(S(Ktest)); pushSTACK(L(string_eq));
  2271. funcall(L(delete_duplicates),3);
  2272. STACK_3 = value1;
  2273. /* create package: */
  2274. STACK_4 = make_package(STACK_4,STACK_3,
  2275. boundp(STACK_1) ? !nullp(STACK_1) : case_inverted,
  2276. boundp(STACK_0) ? !nullp(STACK_0) : case_inverted);
  2277. /* stack-layout: pack, nicknames, uselist, case-sensitive, case-inverted. */
  2278. /* use default value for use-argument: */
  2279. if (!boundp(STACK_2))
  2280. STACK_2 = O(use_default);
  2281. /* execute (USE-PACKAGE uselist newpackage) : */
  2282. pushSTACK(STACK_2); /* uselist */
  2283. pushSTACK(STACK_(4+1)); /* package */
  2284. funcall(L(use_package),2);
  2285. skipSTACK(4);
  2286. VALUES1(popSTACK()); /* package as value */
  2287. }
  2288. /* (MAKE-PACKAGE name [:NICKNAMES nicknames] [:USE uselist]
  2289. [:CASE-SENSITIVE sensitivep] [:CASE-INVERTED invertedp]),
  2290. CLTL p. 183 */
  2291. LISPFUN(make_package,seclass_default,1,0,norest,key,4,
  2292. (kw(nicknames),kw(use),kw(case_sensitive),kw(case_inverted)) ) {
  2293. in_make_package(false);
  2294. }
  2295. /* (CS-COMMON-LISP:MAKE-PACKAGE name [:NICKNAMES nicknames] [:USE uselist]
  2296. [:CASE-SENSITIVE sensitivep] [:CASE-INVERTED invertedp]) */
  2297. LISPFUN(cs_make_package,seclass_default,1,0,norest,key,4,
  2298. (kw(nicknames),kw(use),kw(case_sensitive),kw(case_inverted)) ) {
  2299. in_make_package(true);
  2300. }
  2301. /* (SYSTEM::%IN-PACKAGE name [:NICKNAMES nicknames] [:USE uselist]
  2302. [:CASE-SENSITIVE sensitivep] [:CASE-INVERTED invertedp])
  2303. is like (IN-PACKAGE name [:NICKNAMES nicknames] [:USE uselist]), CLTL p. 183,
  2304. except that *PACKAGE* is not modified. */
  2305. LISPFUN(pin_package,seclass_default,1,0,norest,key,4,
  2306. (kw(nicknames),kw(use),kw(case_sensitive),kw(case_inverted)) ) {
  2307. /* check name and turn into string: */
  2308. var object name = test_stringsymchar_arg(STACK_4,false);
  2309. STACK_4 = name;
  2310. /* find package with this name: */
  2311. var object pack = find_package(name);
  2312. if (nullp(pack)) { /* package not found, must create a new one */
  2313. in_make_package(false);
  2314. } else { /* package found */
  2315. STACK_4 = pack; /* save pack */
  2316. /* stack-layout: pack, nicknames, uselist, case-sensitive, case-inverted. */
  2317. if (boundp(STACK_1)) { /* check the case-sensitivity: */
  2318. var bool value = !nullp(STACK_1);
  2319. if (!!pack_casesensitivep(pack) != value) {
  2320. pushSTACK(pack); pushSTACK(pack);
  2321. STACK_1 = CLSTEXT("One should not change the case sensitiveness of ~S.");
  2322. funcall(S(warn),2);
  2323. pack = STACK_4; /* restore for GC-safety */
  2324. }
  2325. if (value) mark_pack_casesensitive(pack);
  2326. else mark_pack_caseinsensitive(pack);
  2327. }
  2328. if (boundp(STACK_0)) { /* check the case-invertedness: */
  2329. var bool value = !nullp(STACK_0);
  2330. if (!!pack_caseinvertedp(pack) != value) {
  2331. pushSTACK(pack); pushSTACK(pack);
  2332. STACK_1 = CLSTEXT("One should not change the case inversion of ~S.");
  2333. funcall(S(warn),2);
  2334. pack = STACK_4; /* restore for GC-safety */
  2335. }
  2336. if (value) mark_pack_caseinverted(pack);
  2337. else mark_pack_casepreserved(pack);
  2338. }
  2339. /* adjust the nicknames: */
  2340. if (boundp(STACK_3)) {
  2341. /* install nicknames with RENAME-PACKAGE: */
  2342. pushSTACK(pack); /* pack */
  2343. pushSTACK(ThePackage(pack)->pack_name); /* (package-name pack) */
  2344. pushSTACK(STACK_(3+2)); /* nicknames */
  2345. /* (RENAME-PACKAGE pack (package-name pack) nicknames) */
  2346. funcall(L(rename_package),3);
  2347. }
  2348. /* adjust the use-list: */
  2349. if (boundp(STACK_2)) {
  2350. /* extend use-list with USE-PACKAGE
  2351. and shorten with UNUSE-PACKAGE: */
  2352. STACK_1 = STACK_2; /* use-list as 1. argument for USE-PACKAGE */
  2353. STACK_0 = STACK_4; /* pack as 2. argument for USE-PACKAGE */
  2354. prepare_use_package(); /* check arguments STACK_1, STACK_0 */
  2355. /* stack-layout: pack, nicknames, -, new use-list, pack. */
  2356. { /* execute USE-PACKAGE (with copied use-list): */
  2357. var object temp = reverse(STACK_1);
  2358. use_package(temp,STACK_4);
  2359. }
  2360. /* All packages, that are still listed in the use-list of pack,
  2361. but which do not occur in the uselist located in STACK_1,
  2362. are removed with unuse_1package: */
  2363. pack = STACK_4;
  2364. { /* traverse use-list of pack */
  2365. STACK_0 = ThePackage(pack)->pack_use_list;
  2366. while (mconsp(STACK_0)) {
  2367. var object qpack = Car(STACK_0);
  2368. /* search in uselist: */
  2369. if (nullp(memq(qpack,STACK_1)))
  2370. /* not found in uselist */
  2371. unuse_1package(STACK_4,qpack);
  2372. STACK_0 = Cdr(STACK_0);
  2373. }
  2374. }
  2375. }
  2376. /* the use-list is adjusted correctly. */
  2377. skipSTACK(4); /* forget uselist, nicknames etc. */
  2378. VALUES1(popSTACK());
  2379. }
  2380. }
  2381. local one_sym_function_t delete_package_aux;
  2382. /* (DELETE-PACKAGE package), CLTL2 p. 265-266 */
  2383. LISPFUNN(delete_package,1) {
  2384. var object pack = popSTACK();
  2385. if (packagep(pack)) {
  2386. if (pack_deletedp(pack)) {
  2387. VALUES1(NIL); return; /* already deleted -> 1 value NIL */
  2388. }
  2389. } else if (stringp(pack))
  2390. string: { /* string -> find package with this name: */
  2391. var object found = find_package(pack);
  2392. if (nullp(found)) {
  2393. /* raise Continuable Error: */
  2394. pushSTACK(NIL); /* "Ignore." */
  2395. pushSTACK(S(package_error)); /* PACKAGE-ERROR */
  2396. pushSTACK(S(Kpackage)); /* :PACKAGE */
  2397. pushSTACK(pack); /* package-name */
  2398. pushSTACK(NIL); /* "~S: A package with name ~S does not exist." */
  2399. pushSTACK(S(delete_package));
  2400. pushSTACK(pack);
  2401. STACK_6 = CLSTEXT("Ignore.");
  2402. STACK_2 = CLSTEXT("~S: There is no package with name ~S.");
  2403. /* (SYS::CERROR-OF-TYPE "..." 'PACKAGE-ERROR :PACKAGE pack "..."
  2404. 'DELETE-PACKAGE pack) */
  2405. funcall(L(cerror_of_type),7);
  2406. VALUES1(NIL);
  2407. return;
  2408. }
  2409. pack = found;
  2410. } else if (symbolp(pack)) { /* symbol -> string */
  2411. pack = Symbol_name(pack); goto string; /* use printname, no case-invert */
  2412. } else if (charp(pack)) { /* character -> string */
  2413. var object new_string = allocate_string(1);
  2414. TheSnstring(new_string)->data[0] = char_code(pack);
  2415. pack = new_string;
  2416. goto string;
  2417. } else
  2418. pack = test_package_arg(pack); /* report error */
  2419. pushSTACK(pack);
  2420. if (!nullp(ThePackage(pack)->pack_used_by_list)) {
  2421. /* raise Continuable Error: */
  2422. pushSTACK(NIL); /* "~*Delete ~S anyway." */
  2423. pushSTACK(S(package_error)); /* PACKAGE-ERROR */
  2424. pushSTACK(S(Kpackage)); /* :PACKAGE */
  2425. pushSTACK(pack); /* package */
  2426. pushSTACK(NIL); /* "~S: ~S is used by ~{~S~^, ~}." */
  2427. pushSTACK(S(delete_package));
  2428. pushSTACK(pack);
  2429. pushSTACK(ThePackage(pack)->pack_used_by_list);
  2430. STACK_7 = CLSTEXT("~*Delete ~S anyway.");
  2431. STACK_3 = CLSTEXT("~S: ~S is used by ~{~S~^, ~}.");
  2432. /* (SYS::CERROR-OF-TYPE "..." 'PACKAGE-ERROR :PACKAGE pack "..."
  2433. 'DELETE-PACKAGE pack used-by-list) */
  2434. funcall(L(cerror_of_type),8);
  2435. }
  2436. /* execute (DOLIST (p used-py-list) (UNUSE-PACKAGE pack p)) : */
  2437. set_break_sem_3();
  2438. while ((pack = STACK_0, mconsp(ThePackage(pack)->pack_used_by_list))) {
  2439. unuse_1package(Car(ThePackage(pack)->pack_used_by_list),pack);
  2440. }
  2441. clr_break_sem_3();
  2442. /* execute (UNUSE-PACKAGE (package-use-list pack) pack) : */
  2443. unuse_package(ThePackage(STACK_0)->pack_use_list,pack);
  2444. /* apply delete_package_aux to the symbols present in pack: */
  2445. map_symtab_c(&delete_package_aux,&STACK_0,
  2446. ThePackage(STACK_0)->pack_external_symbols);
  2447. map_symtab_c(&delete_package_aux,&STACK_0,
  2448. ThePackage(STACK_0)->pack_internal_symbols);
  2449. pack = popSTACK();
  2450. /* remove pack from the list of all packages and mark as deleted: */
  2451. set_break_sem_2();
  2452. O(all_packages) = deleteq(O(all_packages),pack);
  2453. mark_pack_deleted(pack);
  2454. clr_break_sem_2();
  2455. VALUES1(T);
  2456. }
  2457. /* UP: Auxiliary function for DELETE-PACKAGE:
  2458. Remove the argument (a present symbol) from pack.
  2459. can trigger GC */
  2460. local maygc void delete_package_aux (void* data, object sym) {
  2461. var gcv_object_t* localptr = (gcv_object_t*)data; /* pointer to pack */
  2462. pushSTACK(sym); unintern(&STACK_0,localptr); skipSTACK(1);
  2463. }
  2464. /* (FIND-ALL-SYMBOLS name) and its case-inverted variant */
  2465. local maygc Values do_find_all_symbols (bool invert) {
  2466. STACK_0 = test_stringsymchar_arg(STACK_0,invert); /* name as string */
  2467. pushSTACK(NIL); /* (so far empty) symbol-list */
  2468. pushSTACK(O(all_packages)); /* traverse list of all packages */
  2469. while (mconsp(STACK_0)) {
  2470. var object pack = Car(STACK_0); /* next package */
  2471. /* search in its internal and external symbols: */
  2472. var object sym;
  2473. if (package_lookup(STACK_2,invert,pack,&sym,)) {
  2474. /* found: symbol sym is present in package pack,
  2475. cons with (pushnew sym STACK_1 :test #'eq) on the symbol-list:
  2476. Search, if the found symbol sym occurs in STACK_1: */
  2477. if (nullp(memq(sym,STACK_1))) { /* not found, must cons: */
  2478. pushSTACK(sym);
  2479. {
  2480. var object new_cons = allocate_cons();
  2481. Car(new_cons) = popSTACK();
  2482. Cdr(new_cons) = STACK_1;
  2483. STACK_1 = new_cons;
  2484. }
  2485. }
  2486. }
  2487. STACK_0 = Cdr(STACK_0);
  2488. }
  2489. skipSTACK(1);
  2490. VALUES1(popSTACK()); /* symbol-list as value */
  2491. skipSTACK(1);
  2492. }
  2493. /* (FIND-ALL-SYMBOLS name), CLTL p. 187 */
  2494. LISPFUNNR(find_all_symbols,1)
  2495. {
  2496. do_find_all_symbols(false);
  2497. }
  2498. /* (CS-COMMON-LISP:FIND-ALL-SYMBOLS name) */
  2499. LISPFUNNR(cs_find_all_symbols,1)
  2500. {
  2501. do_find_all_symbols(true);
  2502. }
  2503. local one_sym_function_t map_symbols_aux;
  2504. /* (SYSTEM::MAP-SYMBOLS fun pack)
  2505. applies the function fun to all accessible symbols in pack. Value NIL. */
  2506. LISPFUNN(map_symbols,2) {
  2507. /* check second argument: */
  2508. STACK_0 = test_package_arg(STACK_0);
  2509. /* apply fun to all internal symbols: */
  2510. map_symtab(STACK_1,ThePackage(STACK_0)->pack_internal_symbols);
  2511. /* apply fun to all external symbols: */
  2512. map_symtab(STACK_1,ThePackage(STACK_0)->pack_external_symbols);
  2513. /* apply fun to all inherited symbols: */
  2514. pushSTACK(ThePackage(STACK_0)->pack_use_list); /* traverse use-list */
  2515. while (mconsp(STACK_0)) {
  2516. var object usedpack = Car(STACK_0); /* next package from the use-list */
  2517. STACK_0 = Cdr(STACK_0);
  2518. map_symtab_c(&map_symbols_aux,&STACK_1,
  2519. ThePackage(usedpack)->pack_external_symbols);
  2520. }
  2521. skipSTACK(3);
  2522. VALUES1(NIL);
  2523. }
  2524. /* UP: Auxiliary function for map_symbols:
  2525. Test, if the argument is not shadowed in the given package, and
  2526. then apply the given function.
  2527. can trigger GC */
  2528. local maygc void map_symbols_aux (void* data, object sym) {
  2529. var gcv_object_t* localptr = (gcv_object_t*)data;
  2530. /* Pointer to local variables of map_symbols:
  2531. *(localptr STACKop 1) = fun,
  2532. *(localptr STACKop 0) = pack.
  2533. The symbol STACK_0 is shadowed, if and only if a different
  2534. symbol of the same name is located in the
  2535. shadowing-list of pack. */
  2536. var object shadowingsym;
  2537. if (!(shadowing_lookup(Symbol_name(sym),false,*(localptr STACKop 0),&shadowingsym)
  2538. && !eq(shadowingsym,sym))) {
  2539. pushSTACK(sym); funcall(*(localptr STACKop 1),1);
  2540. } else {
  2541. /* symbol is shadowed in pack -> do not call function */
  2542. }
  2543. }
  2544. /* (SYSTEM::MAP-EXTERNAL-SYMBOLS fun pack)
  2545. applies the function fun to all external symbols in pack. Value NIL. */
  2546. LISPFUNN(map_external_symbols,2) {
  2547. /* check second argument: */
  2548. var object pack = test_package_arg(popSTACK());
  2549. /* apply fun to all external symbols: */
  2550. map_symtab(popSTACK(),ThePackage(pack)->pack_external_symbols);
  2551. VALUES1(NIL);
  2552. }
  2553. /* (SYSTEM::MAP-ALL-SYMBOLS fun)
  2554. applies the function fun to all symbols present in any package. */
  2555. LISPFUNN(map_all_symbols,1) {
  2556. pushSTACK(O(all_packages)); /* traverse package-list */
  2557. while (mconsp(STACK_0)) {
  2558. var object pack = Car(STACK_0); /* next package */
  2559. STACK_0 = Cdr(STACK_0);
  2560. pushSTACK(pack); /* save */
  2561. /* apply fun to all internal symbols: */
  2562. map_symtab(STACK_2,ThePackage(pack)->pack_internal_symbols);
  2563. pack = popSTACK();
  2564. /* apply fun to all external symbols: */
  2565. map_symtab(STACK_1,ThePackage(pack)->pack_external_symbols);
  2566. }
  2567. skipSTACK(2);
  2568. VALUES1(NIL);
  2569. }
  2570. /* UP: Subroutine for EXT:RE-EXPORT.
  2571. Exports a single symbol from TO-PACK. */
  2572. local void export_symbol_from (void *data, object sym) {
  2573. var gcv_object_t* pack_ = (gcv_object_t*)data; /* points into the STACK */
  2574. pushSTACK(sym);
  2575. export(&STACK_0,pack_);
  2576. skipSTACK(1);
  2577. }
  2578. /* (EXT:RE-EXPORT "FROM-PACK" "TO-PACK")
  2579. export all external symbols in FROM-PACK from TO-PACK */
  2580. LISPFUNN(re_export,2) {
  2581. STACK_1 = test_package_arg(STACK_1); /* FROM-PACK */
  2582. STACK_0 = test_package_arg(STACK_0); /* TO-PACK */
  2583. /* TO-PACK must be already using FROM-PACK */
  2584. var object pack_u_l = ThePackage(STACK_0)->pack_use_list;
  2585. if (nullp(memq(STACK_1,ThePackage(STACK_0)->pack_use_list))) {
  2586. pushSTACK(STACK_0); /* TO-PACK: PACKAGE slot of PACKAGE-ERROR */
  2587. pushSTACK(STACK_2); /* FROM-PACK */
  2588. pushSTACK(STACK_1); /* TO-PACK */
  2589. pushSTACK(S(re_export));
  2590. error(package_error,GETTEXT("~S: ~S is not using ~S"));
  2591. }
  2592. map_symtab_c(&export_symbol_from,&STACK_0,
  2593. ThePackage(STACK_1)->pack_external_symbols);
  2594. VALUES1(NIL);
  2595. skipSTACK(2);
  2596. }
  2597. /* Auxiliary functions for WITH-PACKAGE-ITERATOR, CLtL2 p. 275, and LOOP:
  2598. (SYSTEM::PACKAGE-ITERATOR package flags) returns an internal state
  2599. for iterating through the package.
  2600. (SYSTEM::PACKAGE-ITERATE internal-state) iterates through a package by
  2601. one, thereby changes the internal-state and returns: three values
  2602. T, symbol, accessibility of the next symbols resp. 1 value NIL at the end. */
  2603. LISPFUNN(package_iterator,2) {
  2604. STACK_1 = test_package_arg(STACK_1); /* check package-argument */
  2605. /* An internal state consists of a vector
  2606. #(entry index symtab inh-packages package flags)
  2607. whereby flags is a sub-list of (:INTERNAL :EXTERNAL :INHERITED) ,
  2608. package is the original package,
  2609. inh-packages is a sub-list of (package-use-list package) ,
  2610. symtab is a symbol-table or NIL,
  2611. index is an Index in symtab,
  2612. entry is the rest of an entry in symtab. */
  2613. var object state = allocate_vector(6);
  2614. /* TheSvector(state)->data[2] = NIL; */ /* invalid */
  2615. TheSvector(state)->data[3] = ThePackage(STACK_1)->pack_use_list;
  2616. TheSvector(state)->data[4] = STACK_1;
  2617. TheSvector(state)->data[5] = STACK_0;
  2618. VALUES1(state); skipSTACK(2); /* state as value */
  2619. }
  2620. LISPFUNN(package_iterate,1) {
  2621. var object state = popSTACK(); /* internal state */
  2622. /* hopefully a 6er-vector */
  2623. if (simple_vector_p(state) && (Svector_length(state) == 6)) {
  2624. /* state = #(entry index symtab inh-packages package flags) */
  2625. var object symtab = TheSvector(state)->data[2];
  2626. if (simple_vector_p(symtab)) {
  2627. if (false) {
  2628. search1:
  2629. TheSvector(state)->data[2] = symtab;
  2630. TheSvector(state)->data[1] = Symtab_size(symtab);
  2631. TheSvector(state)->data[0] = NIL;
  2632. }
  2633. search2:
  2634. {
  2635. var object entry = TheSvector(state)->data[0];
  2636. search3:
  2637. /* continue search within entry: */
  2638. if (consp(entry)) {
  2639. TheSvector(state)->data[0] = Cdr(entry);
  2640. value2 = Car(entry); goto found;
  2641. } else if (!nullp(entry)) {
  2642. TheSvector(state)->data[0] = NIL;
  2643. value2 = entry; goto found;
  2644. }
  2645. if (false) {
  2646. found:
  2647. /* Found a symbol value.
  2648. Verify that is it accessible in pack and, if :INHERITED
  2649. is requested,
  2650. 1. not hidden by a different symbol (which must be on the
  2651. shadowing-list of pack),
  2652. 2. itself not already present in pack (because in this case
  2653. the accessibility would be :INTERNAL or :EXTERNAL). */
  2654. {
  2655. var object shadowingsym;
  2656. if (!(eq(Car(TheSvector(state)->data[5]),S(Kinherited))
  2657. && (shadowing_lookup(Symbol_name(value2),false,
  2658. TheSvector(state)->data[4],
  2659. &shadowingsym)
  2660. || symtab_find(value2,
  2661. ThePackage(TheSvector(state)->data[4])->
  2662. pack_internal_symbols)
  2663. || symtab_find(value2,
  2664. ThePackage(TheSvector(state)->data[4])->
  2665. pack_external_symbols)))) {
  2666. /* Symbol value2 is really accessible. */
  2667. value1 = T; value3 = Car(TheSvector(state)->data[5]);
  2668. mv_count=3; return;
  2669. }
  2670. goto search2;
  2671. }
  2672. }
  2673. /* entry became =NIL -> go to next Index */
  2674. {
  2675. var uintL index = posfixnum_to_V(TheSvector(state)->data[1]);
  2676. if (index > 0) {
  2677. TheSvector(state)->data[1] = fixnum_inc(TheSvector(state)->
  2678. data[1],-1);
  2679. index--;
  2680. /* check index as a precaution */
  2681. entry = (index < (uintL)posfixnum_to_V(Symtab_size(symtab))
  2682. ? (object)TheSvector(Symtab_table(symtab))->data[index]
  2683. : NIL);
  2684. goto search3;
  2685. }
  2686. }
  2687. }
  2688. /* index became =0 -> go to next table */
  2689. if (eq(Car(TheSvector(state)->data[5]),S(Kinherited))) {
  2690. search4:
  2691. if (mconsp(TheSvector(state)->data[3])) {
  2692. /* go to next element of the list inh-packages */
  2693. symtab = ThePackage(Car(TheSvector(state)->data[3]))->
  2694. pack_external_symbols;
  2695. TheSvector(state)->data[3] = Cdr(TheSvector(state)->data[3]);
  2696. goto search1;
  2697. }
  2698. }
  2699. search5:
  2700. /* go to next element of flags */
  2701. TheSvector(state)->data[5] = Cdr(TheSvector(state)->data[5]);
  2702. }
  2703. var object flags = TheSvector(state)->data[5];
  2704. if (consp(flags)) {
  2705. var object flag = Car(flags);
  2706. if (eq(flag,S(Kinternal))) { /* :INTERNAL */
  2707. symtab = ThePackage(TheSvector(state)->data[4])->
  2708. pack_internal_symbols;
  2709. goto search1;
  2710. } else if (eq(flag,S(Kexternal))) { /* :EXTERNAL */
  2711. symtab = ThePackage(TheSvector(state)->data[4])->
  2712. pack_external_symbols;
  2713. goto search1;
  2714. }
  2715. else if (eq(flag,S(Kinherited))) { /* :INHERITED */
  2716. goto search4;
  2717. }
  2718. goto search5; /* skip invalid flag */
  2719. }
  2720. }
  2721. VALUES1(NIL); return;
  2722. }
  2723. /* UP: initialize the package list
  2724. init_packages();
  2725. can trigger GC */
  2726. global maygc void init_packages (void) {
  2727. O(all_packages) = NIL; /* ALL_PACKAGES := NIL */
  2728. { /* #<PACKAGE CS-COMMON-LISP-USER>: */
  2729. pushSTACK(coerce_imm_ss(ascii_to_string("CS-COMMON-LISP-USER")));
  2730. pushSTACK(coerce_imm_ss(ascii_to_string("CS-CL-USER")));
  2731. /* Provide nickname "CS-USER" for similarity with package "COMMON-LISP-USER". */
  2732. pushSTACK(coerce_imm_ss(ascii_to_string("CS-USER")));
  2733. var object nicks = listof(2); /* ("CS-CL-USER" "CS-USER") */
  2734. O(modern_user_package) = make_package(popSTACK(),nicks,true,true); /* "CS-COMMON-LISP-USER" */
  2735. }
  2736. { /* #<PACKAGE CS-COMMON-LISP>: */
  2737. pushSTACK(coerce_imm_ss(ascii_to_string("CS-COMMON-LISP")));
  2738. pushSTACK(coerce_imm_ss(ascii_to_string("CS-CL")));
  2739. /* Provide nickname "CS-LISP" for similarity with package "COMMON-LISP". */
  2740. pushSTACK(coerce_imm_ss(ascii_to_string("CS-LISP")));
  2741. var object nicks = listof(2); /* ("CS-CL" "CS-LISP") */
  2742. make_package(popSTACK(),nicks,true,true); /* "CS-COMMON-LISP" */
  2743. }
  2744. /* #<PACKAGE CHARSET>: */
  2745. pushSTACK(coerce_imm_ss(ascii_to_string("CHARSET")));
  2746. O(charset_package) = make_package(popSTACK(),NIL,false,false); /* "CHARSET",() */
  2747. /* #<PACKAGE KEYWORD>: */
  2748. pushSTACK(coerce_imm_ss(ascii_to_string("KEYWORD")));
  2749. O(keyword_package) = make_package(popSTACK(),NIL,false,false); /* "KEYWORD" */
  2750. { /* #<PACKAGE SYSTEM>: */
  2751. pushSTACK(coerce_imm_ss(ascii_to_string("SYSTEM")));
  2752. pushSTACK(coerce_imm_ss(ascii_to_string("COMPILER")));
  2753. pushSTACK(coerce_imm_ss(ascii_to_string("SYS")));
  2754. var object nicks = listof(2); /* ("COMPILER" "SYS") */
  2755. make_package(popSTACK(),nicks,false,false); /* "SYSTEM" */
  2756. }
  2757. { /* #<PACKAGE COMMON-LISP-USER>: */
  2758. pushSTACK(coerce_imm_ss(ascii_to_string("COMMON-LISP-USER")));
  2759. pushSTACK(coerce_imm_ss(ascii_to_string("CL-USER")));
  2760. pushSTACK(coerce_imm_ss(ascii_to_string("USER")));
  2761. var object nicks = listof(2); /* ("CL-USER" "USER") */
  2762. make_package(popSTACK(),nicks,false,false); /* "COMMON-LISP-USER" */
  2763. }
  2764. { /* #<PACKAGE COMMON-LISP>: */
  2765. pushSTACK(coerce_imm_ss(ascii_to_string("COMMON-LISP")));
  2766. pushSTACK(coerce_imm_ss(ascii_to_string("LISP")));
  2767. pushSTACK(coerce_imm_ss(ascii_to_string("CL")));
  2768. var object nicks = listof(2); /* ("LISP" "CL") */
  2769. O(default_package) = make_package(popSTACK(),nicks,false,false); /* "COMMON-LISP" */
  2770. }
  2771. /* Created all basic packages.
  2772. Now append all further packages to the end of O(all_packages). */
  2773. nreverse(O(all_packages));
  2774. #define LISPPACK LISPPACK_B
  2775. #include "constpack.c"
  2776. #undef LISPPACK
  2777. nreverse(O(all_packages));
  2778. }