Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[71bc8b]: src / code / target-package.lisp Maximize Restore History

Download this file

target-package.lisp    1430 lines (1321 with data), 63.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
;;;; PACKAGEs and stuff like that
;;;;
;;;; Note: The code in this file signals many correctable errors. This
;;;; is not just an arbitrary aesthetic decision on the part of the
;;;; implementor -- many of these are specified by ANSI 11.1.1.2.5,
;;;; "Prevention of Name Conflicts in Packages":
;;;; Within one package, any particular name can refer to at most one
;;;; symbol. A name conflict is said to occur when there would be more
;;;; than one candidate symbol. Any time a name conflict is about to
;;;; occur, a correctable error is signaled.
;;;;
;;;; FIXME: The code contains a lot of type declarations. Are they
;;;; all really necessary?
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB!IMPL")
(!begin-collecting-cold-init-forms)
(!cold-init-forms
(/show0 "entering !PACKAGE-COLD-INIT"))
;;;; Thread safety
;;;;
;;;; ...this could still use work, but the basic idea is:
;;;;
;;;; *PACKAGE-GRAPH-LOCK* is held via WITH-PACKAGE-GRAPH while working on
;;;; package graph, including package -> package links, and interning and
;;;; uninterning symbols.
;;;;
;;;; Hash-table lock on *PACKAGE-NAMES* is held via WITH-PACKAGE-NAMES while
;;;; frobbing name -> package associations.
;;;;
;;;; There should be no deadlocks due to ordering issues between these two, as
;;;; the latter is only held over operations guaranteed to terminate in finite
;;;; time.
;;;;
;;;; Errors may be signalled while holding on to the *PACKAGE-GRAPH-LOCK*,
;;;; which can still lead to pretty damned inconvenient situations -- but
;;;; since FIND-PACKAGE, FIND-SYMBOL from other threads isn't blocked by this,
;;;; the situation isn't *quite* hopeless.
;;;;
;;;; A better long-term solution seems to be in splitting the granularity of
;;;; the *PACKAGE-GRAPH-LOCK* down: for interning a per-package lock should be
;;;; sufficient, though interaction between parallel intern and use-package
;;;; needs to be considered with some care.
(defvar *package-graph-lock*)
(!cold-init-forms
(setf *package-graph-lock* (sb!thread:make-mutex :name "Package Graph Lock")))
(defun call-with-package-graph (function)
(declare (function function))
;; FIXME: Since name conflicts can be signalled while holding the
;; mutex, user code can be run leading to lock ordering problems.
;;
;; This used to be a spinlock, but there it can be held for a long
;; time while the debugger waits for user input.
(sb!thread:with-recursive-lock (*package-graph-lock*)
(funcall function)))
;;; a map from package names to packages
(defvar *package-names*)
(declaim (type hash-table *package-names*))
(!cold-init-forms
(setf *package-names* (make-hash-table :test 'equal :synchronized t)))
(defmacro with-package-names ((names &key) &body body)
`(let ((,names *package-names*))
(with-locked-system-table (,names)
,@body)))
;;;; PACKAGE-HASHTABLE stuff
(def!method print-object ((table package-hashtable) stream)
(declare (type stream stream))
(print-unreadable-object (table stream :type t)
(format stream
":SIZE ~S :FREE ~S :DELETED ~S"
(package-hashtable-size table)
(package-hashtable-free table)
(package-hashtable-deleted table))))
;;; the maximum load factor we allow in a package hashtable
(defconstant +package-rehash-threshold+ 0.75)
;;; the load factor desired for a package hashtable when writing a
;;; core image
(defconstant +package-hashtable-image-load-factor+ 0.5)
;;; Make a package hashtable having a prime number of entries at least
;;; as great as (/ SIZE +PACKAGE-REHASH-THRESHOLD+). If RES is supplied,
;;; then it is destructively modified to produce the result. This is
;;; useful when changing the size, since there are many pointers to
;;; the hashtable.
;;; Actually, the smallest table built here has three entries. This
;;; is necessary because the double hashing step size is calculated
;;; using a division by the table size minus two.
(defun make-or-remake-package-hashtable (size
&optional
res)
(flet ((actual-package-hashtable-size (size)
(loop for n of-type fixnum
from (logior (ceiling size +package-rehash-threshold+) 1)
by 2
when (positive-primep n) return n)))
(let* ((n (actual-package-hashtable-size size))
(size (truncate (* n +package-rehash-threshold+)))
(table (make-array n))
(hash (make-array n
:element-type '(unsigned-byte 8)
:initial-element 0)))
(if res
(setf (package-hashtable-table res) table
(package-hashtable-hash res) hash
(package-hashtable-size res) size
(package-hashtable-free res) size
(package-hashtable-deleted res) 0)
(setf res (%make-package-hashtable table hash size)))
res)))
;;; Destructively resize TABLE to have room for at least SIZE entries
;;; and rehash its existing entries.
(defun resize-package-hashtable (table size)
(let* ((vec (package-hashtable-table table))
(hash (package-hashtable-hash table))
(len (length vec)))
(make-or-remake-package-hashtable size table)
(dotimes (i len)
(when (> (aref hash i) 1)
(add-symbol table (svref vec i))))))
;;;; package locking operations, built conditionally on :sb-package-locks
#!+sb-package-locks
(progn
(defun package-locked-p (package)
#!+sb-doc
"Returns T when PACKAGE is locked, NIL otherwise. Signals an error
if PACKAGE doesn't designate a valid package."
(package-lock (find-undeleted-package-or-lose package)))
(defun lock-package (package)
#!+sb-doc
"Locks PACKAGE and returns T. Has no effect if PACKAGE was already
locked. Signals an error if PACKAGE is not a valid package designator"
(setf (package-lock (find-undeleted-package-or-lose package)) t))
(defun unlock-package (package)
#!+sb-doc
"Unlocks PACKAGE and returns T. Has no effect if PACKAGE was already
unlocked. Signals an error if PACKAGE is not a valid package designator."
(setf (package-lock (find-undeleted-package-or-lose package)) nil)
t)
(defun package-implemented-by-list (package)
#!+sb-doc
"Returns a list containing the implementation packages of
PACKAGE. Signals an error if PACKAGE is not a valid package designator."
(package-%implementation-packages (find-undeleted-package-or-lose package)))
(defun package-implements-list (package)
#!+sb-doc
"Returns the packages that PACKAGE is an implementation package
of. Signals an error if PACKAGE is not a valid package designator."
(let ((package (find-undeleted-package-or-lose package)))
(loop for x in (list-all-packages)
when (member package (package-%implementation-packages x))
collect x)))
(defun add-implementation-package (packages-to-add
&optional (package *package*))
#!+sb-doc
"Adds PACKAGES-TO-ADD as implementation packages of PACKAGE. Signals
an error if PACKAGE or any of the PACKAGES-TO-ADD is not a valid
package designator."
(let ((package (find-undeleted-package-or-lose package))
(packages-to-add (package-listify packages-to-add)))
(setf (package-%implementation-packages package)
(union (package-%implementation-packages package)
(mapcar #'find-undeleted-package-or-lose packages-to-add)))))
(defun remove-implementation-package (packages-to-remove
&optional (package *package*))
#!+sb-doc
"Removes PACKAGES-TO-REMOVE from the implementation packages of
PACKAGE. Signals an error if PACKAGE or any of the PACKAGES-TO-REMOVE
is not a valid package designator."
(let ((package (find-undeleted-package-or-lose package))
(packages-to-remove (package-listify packages-to-remove)))
(setf (package-%implementation-packages package)
(nset-difference
(package-%implementation-packages package)
(mapcar #'find-undeleted-package-or-lose packages-to-remove)))))
(defmacro with-unlocked-packages ((&rest packages) &body forms)
#!+sb-doc
"Unlocks PACKAGES for the dynamic scope of the body. Signals an
error if any of PACKAGES is not a valid package designator."
(with-unique-names (unlocked-packages)
`(let (,unlocked-packages)
(unwind-protect
(progn
(dolist (p ',packages)
(when (package-locked-p p)
(push p ,unlocked-packages)
(unlock-package p)))
,@forms)
(dolist (p ,unlocked-packages)
(when (find-package p)
(lock-package p)))))))
(defun package-lock-violation (package &key (symbol nil symbol-p)
format-control format-arguments)
(let* ((restart :continue)
(cl-violation-p (eq package *cl-package*))
(error-arguments
(append (list (if symbol-p
'symbol-package-locked-error
'package-locked-error)
:package package
:format-control format-control
:format-arguments format-arguments)
(when symbol-p (list :symbol symbol))
(list :references
(append '((:sbcl :node "Package Locks"))
(when cl-violation-p
'((:ansi-cl :section (11 1 2 1 2)))))))))
(restart-case
(apply #'cerror "Ignore the package lock." error-arguments)
(:ignore-all ()
:report "Ignore all package locks in the context of this operation."
(setf restart :ignore-all))
(:unlock-package ()
:report "Unlock the package."
(setf restart :unlock-package)))
(ecase restart
(:continue
(pushnew package *ignored-package-locks*))
(:ignore-all
(setf *ignored-package-locks* t))
(:unlock-package
(unlock-package package)))))
(defun package-lock-violation-p (package &optional (symbol nil symbolp))
;; KLUDGE: (package-lock package) needs to be before
;; comparison to *package*, since during cold init this gets
;; called before *package* is bound -- but no package should
;; be locked at that point.
(and package
(package-lock package)
;; In package or implementation package
(not (or (eq package *package*)
(member *package* (package-%implementation-packages package))))
;; Runtime disabling
(not (eq t *ignored-package-locks*))
(or (eq :invalid *ignored-package-locks*)
(not (member package *ignored-package-locks*)))
;; declarations for symbols
(not (and symbolp (member symbol (disabled-package-locks))))))
(defun disabled-package-locks ()
(if (boundp 'sb!c::*lexenv*)
(sb!c::lexenv-disabled-package-locks sb!c::*lexenv*)
sb!c::*disabled-package-locks*))
) ; progn
;;;; more package-locking these are NOPs unless :sb-package-locks is
;;;; in target features. Cross-compiler NOPs for these are in cross-misc.
;;; The right way to establish a package lock context is
;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR, defined in early-package.lisp
;;;
;;; Must be used inside the dynamic contour established by
;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR
(defun assert-package-unlocked (package &optional format-control
&rest format-arguments)
#!-sb-package-locks
(declare (ignore format-control format-arguments))
#!+sb-package-locks
(when (package-lock-violation-p package)
(package-lock-violation package
:format-control format-control
:format-arguments format-arguments))
package)
;;; Must be used inside the dynamic contour established by
;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR.
;;;
;;; FIXME: Maybe we should establish such contours for he toplevel
;;; and others, so that %set-fdefinition and others could just use
;;; this.
(defun assert-symbol-home-package-unlocked (name format)
#!-sb-package-locks
(declare (ignore format))
#!+sb-package-locks
(let* ((symbol (etypecase name
(symbol name)
(list (if (and (consp (cdr name))
(eq 'setf (first name)))
(second name)
;; Skip lists of length 1, single conses and
;; (class-predicate foo), etc.
;; FIXME: MOP and package-lock
;; interaction needs to be thought about.
(return-from
assert-symbol-home-package-unlocked
name)))))
(package (symbol-package symbol)))
(when (package-lock-violation-p package symbol)
(package-lock-violation package
:symbol symbol
:format-control format
:format-arguments (list name))))
name)
;;;; miscellaneous PACKAGE operations
(def!method print-object ((package package) stream)
(let ((name (package-%name package)))
(if name
(print-unreadable-object (package stream :type t)
(prin1 name stream))
(print-unreadable-object (package stream :type t :identity t)
(write-string "(deleted)" stream)))))
;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and
;;; most other operations, are unspecified for deleted packages. We
;;; just do the easy thing and signal errors in that case.
(macrolet ((def (ext real)
`(defun ,ext (x) (,real (find-undeleted-package-or-lose x)))))
(def package-nicknames package-%nicknames)
(def package-use-list package-%use-list)
(def package-used-by-list package-%used-by-list)
(def package-shadowing-symbols package-%shadowing-symbols))
(defun %package-hashtable-symbol-count (table)
(let ((size (the fixnum
(- (package-hashtable-size table)
(package-hashtable-deleted table)))))
(the fixnum
(- size (package-hashtable-free table)))))
(defun package-internal-symbol-count (package)
(%package-hashtable-symbol-count (package-internal-symbols package)))
(defun package-external-symbol-count (package)
(%package-hashtable-symbol-count (package-external-symbols package)))
(defvar *package* (error "*PACKAGE* should be initialized in cold load!")
#!+sb-doc "the current package")
;;; FIXME: should be declared of type PACKAGE, with no NIL init form,
;;; after I get around to cleaning up DOCUMENTATION
;;; This magical variable is T during initialization so that
;;; USE-PACKAGE's of packages that don't yet exist quietly win. Such
;;; packages are thrown onto the list *DEFERRED-USE-PACKAGES* so that
;;; this can be fixed up later.
;;;
;;; FIXME: This could be cleaned up the same way I do it in my package
;;; hacking when setting up the cross-compiler. Then we wouldn't have
;;; this extraneous global variable and annoying runtime tests on
;;; package operations. (*DEFERRED-USE-PACKAGES* would also go away.)
(defvar *in-package-init*)
;;; pending USE-PACKAGE arguments saved up while *IN-PACKAGE-INIT* is true
(defvar *!deferred-use-packages*)
(!cold-init-forms
(setf *!deferred-use-packages* nil))
(define-condition bootstrap-package-not-found (condition)
((name :initarg :name :reader bootstrap-package-name)))
(defun debootstrap-package (&optional condition)
(invoke-restart
(find-restart-or-control-error 'debootstrap-package condition)))
(defun find-package (package-designator)
(flet ((find-package-from-string (string)
(declare (type string string))
(let ((packageoid (gethash string *package-names*)))
(when (and (null packageoid)
(not *in-package-init*) ; KLUDGE
(let ((mismatch (mismatch "SB!" string)))
(and mismatch (= mismatch 3))))
(restart-case
(signal 'bootstrap-package-not-found :name string)
(debootstrap-package ()
(return-from find-package
(if (string= string "SB!XC")
(find-package "COMMON-LISP")
(find-package
(substitute #\- #\! string :count 1)))))))
packageoid)))
(typecase package-designator
(package package-designator)
(symbol (find-package-from-string (symbol-name package-designator)))
(string (find-package-from-string package-designator))
(character (find-package-from-string (string package-designator)))
(t (error 'type-error
:datum package-designator
:expected-type '(or character package string symbol))))))
;;; Return a list of packages given a package designator or list of
;;; package designators, or die trying.
(defun package-listify (thing)
(let ((res ()))
(dolist (thing (if (listp thing) thing (list thing)) res)
(push (find-undeleted-package-or-lose thing) res))))
;;; Make a package name into a simple-string.
(defun package-namify (n)
(stringify-package-designator n))
;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
;;; returns NIL (not an error) for a deleted package, so this is a special
;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
(defun package-name (package-designator)
(package-%name (%find-package-or-lose package-designator)))
;;;; operations on package hashtables
;;; Compute a number from the sxhash of the pname and the length which
;;; must be between 2 and 255.
(defmacro entry-hash (length sxhash)
`(the fixnum
(+ (the fixnum
(rem (the fixnum
(logxor ,length
,sxhash
(the fixnum (ash ,sxhash -8))
(the fixnum (ash ,sxhash -16))
(the fixnum (ash ,sxhash -19))))
254))
2)))
;;; FIXME: should be wrapped in EVAL-WHEN (COMPILE EXECUTE)
;;; Add a symbol to a package hashtable. The symbol is assumed
;;; not to be present.
(defun add-symbol (table symbol)
(when (zerop (package-hashtable-free table))
;; The hashtable is full. Resize it to be able to hold twice the
;; amount of symbols than it currently contains. The actual new size
;; can be smaller than twice the current size if the table contained
;; deleted entries.
(resize-package-hashtable table
(* (- (package-hashtable-size table)
(package-hashtable-deleted table))
2)))
(let* ((vec (package-hashtable-table table))
(hash (package-hashtable-hash table))
(len (length vec))
(sxhash (%sxhash-simple-string (symbol-name symbol)))
(h2 (1+ (rem sxhash (- len 2)))))
(declare (fixnum sxhash h2))
(do ((i (rem sxhash len) (rem (+ i h2) len)))
((< (the fixnum (aref hash i)) 2)
(if (zerop (the fixnum (aref hash i)))
(decf (package-hashtable-free table))
(decf (package-hashtable-deleted table)))
(setf (svref vec i) symbol)
(setf (aref hash i)
(entry-hash (length (symbol-name symbol))
sxhash)))
(declare (fixnum i)))))
;;; Resize the package hashtables of all packages so that their load
;;; factor is +PACKAGE-HASHTABLE-IMAGE-LOAD-FACTOR+. Called from
;;; SAVE-LISP-AND-DIE to optimize space usage in the image.
(defun tune-hashtable-sizes-of-all-packages ()
(flet ((tune-table-size (table)
(resize-package-hashtable
table
(round (* (/ +package-rehash-threshold+
+package-hashtable-image-load-factor+)
(- (package-hashtable-size table)
(package-hashtable-free table)
(package-hashtable-deleted table)))))))
(dolist (package (list-all-packages))
(tune-table-size (package-internal-symbols package))
(tune-table-size (package-external-symbols package)))))
;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
(defmacro with-symbol ((index-var symbol-var table string length sxhash
entry-hash)
&body forms)
(let ((vec (gensym)) (hash (gensym)) (len (gensym)) (h2 (gensym))
(name (gensym)) (name-len (gensym)) (ehash (gensym)))
`(let* ((,vec (package-hashtable-table ,table))
(,hash (package-hashtable-hash ,table))
(,len (length ,vec))
(,h2 (1+ (the index (rem (the hash ,sxhash)
(the index (- ,len 2)))))))
(declare (type index ,len ,h2))
(prog ((,index-var (rem (the hash ,sxhash) ,len))
,symbol-var ,ehash)
(declare (type (or index null) ,index-var))
LOOP
(setq ,ehash (aref ,hash ,index-var))
(cond ((eql ,ehash ,entry-hash)
(setq ,symbol-var (svref ,vec ,index-var))
(let* ((,name (symbol-name ,symbol-var))
(,name-len (length ,name)))
(declare (type index ,name-len))
(when (and (= ,name-len ,length)
(string= ,string ,name
:end1 ,length
:end2 ,name-len))
(go DOIT))))
((zerop ,ehash)
(setq ,index-var nil)
(go DOIT)))
(setq ,index-var (+ ,index-var ,h2))
(when (>= ,index-var ,len)
(setq ,index-var (- ,index-var ,len)))
(go LOOP)
DOIT
(return (progn ,@forms))))))
;;; Delete the entry for STRING in TABLE. The entry must exist.
(defun nuke-symbol (table string)
(declare (simple-string string))
(let* ((length (length string))
(hash (%sxhash-simple-string string))
(ehash (entry-hash length hash)))
(declare (type index length)
(type hash hash))
(with-symbol (index symbol table string length hash ehash)
(setf (aref (package-hashtable-hash table) index) 1)
(setf (aref (package-hashtable-table table) index) nil)
(incf (package-hashtable-deleted table))))
;; If the table is less than one quarter full, halve its size and
;; rehash the entries.
(let* ((size (package-hashtable-size table))
(deleted (package-hashtable-deleted table))
(used (- size
(package-hashtable-free table)
deleted)))
(declare (type fixnum size deleted used))
(when (< used (truncate size 4))
(resize-package-hashtable table (* used 2)))))
;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*. If there is a
;;; conflict then give the user a chance to do something about it. Caller is
;;; responsible for having acquired the mutex via WITH-PACKAGES.
(defun %enter-new-nicknames (package nicknames)
(declare (type list nicknames))
(dolist (n nicknames)
(let* ((n (package-namify n))
(found (with-package-names (names)
(or (gethash n names)
(progn
(setf (gethash n names) package)
(push n (package-%nicknames package))
package)))))
(cond ((eq found package))
((string= (the string (package-%name found)) n)
(cerror "Ignore this nickname."
'simple-package-error
:package package
:format-control "~S is a package name, so it cannot be a nickname for ~S."
:format-arguments (list n (package-%name package))))
(t
(cerror "Leave this nickname alone."
'simple-package-error
:package package
:format-control "~S is already a nickname for ~S."
:format-arguments (list n (package-%name found))))))))
(defun make-package (name &key
(use '#.*default-package-use-list*)
nicknames
(internal-symbols 10)
(external-symbols 10))
#!+sb-doc
#.(format nil
"Make a new package having the specified NAME, NICKNAMES, and USE
list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of
internal and external symbols which will ultimately be present in the package.
The default value of USE is implementation-dependent, and in this
implementation it is ~S." *default-package-use-list*)
(prog (clobber)
:restart
(when (find-package name)
;; ANSI specifies that this error is correctable.
(cerror "Clobber existing package."
"A package named ~S already exists" name)
(setf clobber t))
(with-package-graph ()
;; Check for race, signal the error outside the lock.
(when (and (not clobber) (find-package name))
(go :restart))
(let* ((name (package-namify name))
(package (internal-make-package
:%name name
:internal-symbols (make-or-remake-package-hashtable
internal-symbols)
:external-symbols (make-or-remake-package-hashtable
external-symbols))))
;; Do a USE-PACKAGE for each thing in the USE list so that checking for
;; conflicting exports among used packages is done.
(if *in-package-init*
(push (list use package) *!deferred-use-packages*)
(use-package use package))
;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
;; which would leave us with possibly-bad side effects from the earlier
;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
;; USE-PACKAGE, too.
(%enter-new-nicknames package nicknames)
(return (setf (gethash name *package-names*) package))))
(bug "never")))
;;; Change the name if we can, blast any old nicknames and then
;;; add in any new ones.
;;;
;;; FIXME: ANSI claims that NAME is a package designator (not just a
;;; string designator -- weird). Thus, NAME could
;;; be a package instead of a string. Presumably then we should not change
;;; the package name if NAME is the same package that's referred to by PACKAGE.
;;; If it's a *different* package, we should probably signal an error.
;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
(defun rename-package (package-designator name &optional (nicknames ()))
#!+sb-doc
"Changes the name and nicknames for a package."
(let ((package nil))
(tagbody :restart
(setq package (find-undeleted-package-or-lose package-designator))
(let* ((name (package-namify name))
(found (find-package name))
(nicks (mapcar #'string nicknames)))
(unless (or (not found) (eq found package))
(error 'simple-package-error
:package name
:format-control "A package named ~S already exists."
:format-arguments (list name)))
(with-single-package-locked-error ()
(unless (and (string= name (package-name package))
(null (set-difference nicks (package-nicknames package)
:test #'string=)))
(assert-package-unlocked package "rename as ~A~@[ with nickname~P ~
~{~A~^, ~}~]"
name (length nicks) nicks))
(with-package-names (names)
;; Check for race conditions now that we have the lock.
(unless (eq package (find-package package-designator))
(go :restart))
;; Do the renaming.
(remhash (package-%name package) names)
(dolist (n (package-%nicknames package))
(remhash n names))
(setf (package-%name package) name
(gethash name names) package
(package-%nicknames package) ()))
(%enter-new-nicknames package nicknames))))
package))
(defun delete-package (package-designator)
#!+sb-doc
"Delete the package designated by PACKAGE-DESIGNATOR from the package
system data structures."
(tagbody :restart
(let ((package (find-package package-designator)))
(cond ((not package)
;; This continuable error is required by ANSI.
(cerror
"Return ~S."
(make-condition
'simple-package-error
:package package-designator
:format-control "There is no package named ~S."
:format-arguments (list package-designator))
(return-from delete-package nil)))
((not (package-name package)) ; already deleted
(return-from delete-package nil))
(t
(with-single-package-locked-error
(:package package "deleting package ~A" package)
(let ((use-list (package-used-by-list package)))
(when use-list
;; This continuable error is specified by ANSI.
(cerror
"Remove dependency in other packages."
(make-condition
'simple-package-error
:package package
:format-control
"~@<Package ~S is used by package~P:~2I~_~S~@:>"
:format-arguments (list (package-name package)
(length use-list)
(mapcar #'package-name use-list))))
(dolist (p use-list)
(unuse-package package p))))
(with-package-graph ()
;; Check for races, restart if necessary.
(let ((package2 (find-package package-designator)))
(when (or (neq package package2) (package-used-by-list package2))
(go :restart)))
(dolist (used (package-use-list package))
(unuse-package used package))
(do-symbols (sym package)
(unintern sym package))
(with-package-names (names)
(remhash (package-name package) names)
(dolist (nick (package-nicknames package))
(remhash nick names))
(setf (package-%name package) nil
;; Setting PACKAGE-%NAME to NIL is required in order to
;; make PACKAGE-NAME return NIL for a deleted package as
;; ANSI requires. Setting the other slots to NIL
;; and blowing away the PACKAGE-HASHTABLES is just done
;; for tidiness and to help the GC.
(package-%nicknames package) nil))
(setf (package-%use-list package) nil
(package-tables package) nil
(package-%shadowing-symbols package) nil
(package-internal-symbols package)
(make-or-remake-package-hashtable 0)
(package-external-symbols package)
(make-or-remake-package-hashtable 0)))
(return-from delete-package t)))))))
(defun list-all-packages ()
#!+sb-doc
"Return a list of all existing packages."
(let ((res ()))
(with-package-names (names)
(maphash (lambda (k v)
(declare (ignore k))
(pushnew v res))
names))
res))
(defun intern (name &optional (package (sane-package)))
#!+sb-doc
"Return a symbol in PACKAGE having the specified NAME, creating it
if necessary."
;; We just simple-stringify the name and call INTERN*, where the real
;; logic is.
(let ((name (if (simple-string-p name)
name
(coerce name 'simple-string)))
(package (find-undeleted-package-or-lose package)))
(declare (simple-string name))
(intern* name
(length name)
package)))
(defun find-symbol (name &optional (package (sane-package)))
#!+sb-doc
"Return the symbol named STRING in PACKAGE. If such a symbol is found
then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
how the symbol is accessible. If no symbol is found then both values
are NIL."
;; We just simple-stringify the name and call FIND-SYMBOL*, where the
;; real logic is.
(let ((name (if (simple-string-p name) name (coerce name 'simple-string))))
(declare (simple-string name))
(find-symbol* name
(length name)
(find-undeleted-package-or-lose package))))
;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
;;; then create it, special-casing the keyword package.
(defun intern* (name length package)
(declare (simple-string name))
(multiple-value-bind (symbol where) (find-symbol* name length package)
(cond (where
(values symbol where))
(t
;; Let's try again with a lock: the common case has the
;; symbol already interned, handled by the first leg of the
;; COND, but in case another thread is interning in
;; parallel we need to check after grabbing the lock.
(with-package-graph ()
(setf (values symbol where) (find-symbol* name length package))
(if where
(values symbol where)
(let ((symbol-name (subseq name 0 length)))
(with-single-package-locked-error
(:package package "interning ~A" symbol-name)
(let ((symbol (make-symbol symbol-name)))
(%set-symbol-package symbol package)
(cond
((eq package *keyword-package*)
(%set-symbol-value symbol symbol)
(add-symbol (package-external-symbols package) symbol))
(t
(add-symbol (package-internal-symbols package) symbol)))
(values symbol nil))))))))))
;;; Check internal and external symbols, then scan down the list
;;; of hashtables for inherited symbols.
(defun find-symbol* (string length package)
(declare (simple-string string)
(type index length))
(let* ((hash (%sxhash-simple-substring string length))
(ehash (entry-hash length hash)))
(declare (type hash hash ehash))
(with-symbol (found symbol (package-internal-symbols package)
string length hash ehash)
(when found
(return-from find-symbol* (values symbol :internal))))
(with-symbol (found symbol (package-external-symbols package)
string length hash ehash)
(when found
(return-from find-symbol* (values symbol :external))))
(let ((head (package-tables package)))
(do ((prev head table)
(table (cdr head) (cdr table)))
((null table) (values nil nil))
(with-symbol (found symbol (car table) string length hash ehash)
(when found
;; At this point we used to move the table to the
;; beginning of the list, probably on the theory that we'd
;; soon be looking up further items there. Unfortunately
;; that was very much non-thread safe. Since the failure
;; mode was nasty (corruption of the package in a way
;; which would make symbol lookups loop infinitely) and it
;; would be triggered just by doing reads to a resource
;; that users can't do their own locking on, that code has
;; been removed. If we ever add locking to packages,
;; resurrecting that code might make sense, even though it
;; didn't seem to have much of an performance effect in
;; normal use.
;;
;; -- JES, 2006-09-13
(return-from find-symbol* (values symbol :inherited))))))))
;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
;;; This is used for fast name-conflict checking in this file and symbol
;;; printing in the printer.
(defun find-external-symbol (string package)
(declare (simple-string string))
(let* ((length (length string))
(hash (%sxhash-simple-string string))
(ehash (entry-hash length hash)))
(declare (type index length)
(type hash hash))
(with-symbol (found symbol (package-external-symbols package)
string length hash ehash)
(values symbol found))))
(defun print-symbol-with-prefix (stream symbol colon at)
#!+sb-doc
"For use with ~/: Write SYMBOL to STREAM as if it is not accessible from
the current package."
(declare (ignore colon at))
;; Only keywords should be accessible from the keyword package, and
;; keywords are always printed with colons, so this guarantees that the
;; symbol will not be printed without a prefix.
(let ((*package* *keyword-package*))
(write symbol :stream stream :escape t)))
(define-condition name-conflict (reference-condition package-error)
((function :initarg :function :reader name-conflict-function)
(datum :initarg :datum :reader name-conflict-datum)
(symbols :initarg :symbols :reader name-conflict-symbols))
(:default-initargs :references (list '(:ansi-cl :section (11 1 1 2 5))))
(:report
(lambda (c s)
(format s "~@<~S ~S causes name-conflicts in ~S between the ~
following symbols:~2I~@:_~
~{~/sb-impl::print-symbol-with-prefix/~^, ~}~:@>"
(name-conflict-function c)
(name-conflict-datum c)
(package-error-package c)
(name-conflict-symbols c)))))
(defun name-conflict (package function datum &rest symbols)
(restart-case
(error 'name-conflict :package package :symbols symbols
:function function :datum datum)
(resolve-conflict (chosen-symbol)
:report "Resolve conflict."
:interactive
(lambda ()
(let* ((len (length symbols))
(nlen (length (write-to-string len :base 10)))
(*print-pretty* t))
(format *query-io* "~&~@<Select a symbol to be made accessible in ~
package ~A:~2I~@:_~{~{~V,' D. ~
~/sb-impl::print-symbol-with-prefix/~}~@:_~}~
~@:>"
(package-name package)
(loop for s in symbols
for i upfrom 1
collect (list nlen i s)))
(loop
(format *query-io* "~&Enter an integer (between 1 and ~D): " len)
(finish-output *query-io*)
(let ((i (parse-integer (read-line *query-io*) :junk-allowed t)))
(when (and i (<= 1 i len))
(return (list (nth (1- i) symbols))))))))
(multiple-value-bind (package-symbol status)
(find-symbol (symbol-name chosen-symbol) package)
(let* ((accessiblep status) ; never NIL here
(presentp (and accessiblep
(not (eq :inherited status)))))
(ecase function
((unintern)
(if presentp
(if (eq package-symbol chosen-symbol)
(shadow (list package-symbol) package)
(shadowing-import (list chosen-symbol) package))
(shadowing-import (list chosen-symbol) package)))
((use-package export)
(if presentp
(if (eq package-symbol chosen-symbol)
(shadow (list package-symbol) package) ; CLHS 11.1.1.2.5
(if (eq (symbol-package package-symbol) package)
(unintern package-symbol package) ; CLHS 11.1.1.2.5
(shadowing-import (list chosen-symbol) package)))
(shadowing-import (list chosen-symbol) package)))
((import)
(if presentp
(if (eq package-symbol chosen-symbol)
nil ; re-importing the same symbol
(shadowing-import (list chosen-symbol) package))
(shadowing-import (list chosen-symbol) package)))))))))
;;; If we are uninterning a shadowing symbol, then a name conflict can
;;; result, otherwise just nuke the symbol.
(defun unintern (symbol &optional (package (sane-package)))
#!+sb-doc
"Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present then T is
returned, otherwise NIL. If PACKAGE is SYMBOL's home package, then it is made
uninterned."
(with-package-graph ()
(let* ((package (find-undeleted-package-or-lose package))
(name (symbol-name symbol))
(shadowing-symbols (package-%shadowing-symbols package)))
(declare (list shadowing-symbols))
(with-single-package-locked-error ()
(when (find-symbol name package)
(assert-package-unlocked package "uninterning ~A" name))
;; If a name conflict is revealed, give us a chance to
;; shadowing-import one of the accessible symbols.
(when (member symbol shadowing-symbols)
(let ((cset ()))
(dolist (p (package-%use-list package))
(multiple-value-bind (s w) (find-external-symbol name p)
(when w (pushnew s cset))))
(when (cdr cset)
(apply #'name-conflict package 'unintern symbol cset)
(return-from unintern t)))
(setf (package-%shadowing-symbols package)
(remove symbol shadowing-symbols)))
(multiple-value-bind (s w) (find-symbol name package)
(cond ((not (eq symbol s)) nil)
((or (eq w :internal) (eq w :external))
(nuke-symbol (if (eq w :internal)
(package-internal-symbols package)
(package-external-symbols package))
name)
(if (eq (symbol-package symbol) package)
(%set-symbol-package symbol nil))
t)
(t nil)))))))
;;; Take a symbol-or-list-of-symbols and return a list, checking types.
(defun symbol-listify (thing)
(cond ((listp thing)
(dolist (s thing)
(unless (symbolp s) (error "~S is not a symbol." s)))
thing)
((symbolp thing) (list thing))
(t
(error "~S is neither a symbol nor a list of symbols." thing))))
(defun string-listify (thing)
(mapcar #'string (if (listp thing)
thing
(list thing))))
;;; This is like UNINTERN, except if SYMBOL is inherited, it chases
;;; down the package it is inherited from and uninterns it there. Used
;;; for name-conflict resolution. Shadowing symbols are not uninterned
;;; since they do not cause conflicts.
(defun moby-unintern (symbol package)
(unless (member symbol (package-%shadowing-symbols package))
(or (unintern symbol package)
(let ((name (symbol-name symbol)))
(multiple-value-bind (s w) (find-symbol name package)
(declare (ignore s))
(when (eq w :inherited)
(dolist (q (package-%use-list package))
(multiple-value-bind (u x) (find-external-symbol name q)
(declare (ignore u))
(when x
(unintern symbol q)
(return t))))))))))
(defun export (symbols &optional (package (sane-package)))
#!+sb-doc
"Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
(with-package-graph ()
(let ((package (find-undeleted-package-or-lose package))
(symbols (symbol-listify symbols))
(syms ()))
;; Punt any symbols that are already external.
(dolist (sym symbols)
(multiple-value-bind (s w)
(find-external-symbol (symbol-name sym) package)
(declare (ignore s))
(unless (or w (member sym syms))
(push sym syms))))
(with-single-package-locked-error ()
(when syms
(assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
(length syms) syms))
;; Find symbols and packages with conflicts.
(let ((used-by (package-%used-by-list package)))
(dolist (sym syms)
(let ((name (symbol-name sym)))
(dolist (p used-by)
(multiple-value-bind (s w) (find-symbol name p)
(when (and w
(not (eq s sym))
(not (member s (package-%shadowing-symbols p))))
;; Beware: the name conflict is in package P, not in
;; PACKAGE.
(name-conflict p 'export sym sym s)))))))
;; Check that all symbols are accessible. If not, ask to import them.
(let ((missing ())
(imports ()))
(dolist (sym syms)
(multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
(cond ((not (and w (eq s sym)))
(push sym missing))
((eq w :inherited)
(push sym imports)))))
(when missing
(cerror
"~S these symbols into the ~A package."
(make-condition
'simple-package-error
:package package
:format-control
"~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
:format-arguments (list (package-%name package) missing))
'import (package-%name package))
(import missing package))
(import imports package))
;; And now, three pages later, we export the suckers.
(let ((internal (package-internal-symbols package))
(external (package-external-symbols package)))
(dolist (sym syms)
(nuke-symbol internal (symbol-name sym))
(add-symbol external sym))))
t)))
;;; Check that all symbols are accessible, then move from external to internal.
(defun unexport (symbols &optional (package (sane-package)))
#!+sb-doc
"Makes SYMBOLS no longer exported from PACKAGE."
(with-package-graph ()
(let ((package (find-undeleted-package-or-lose package))
(symbols (symbol-listify symbols))
(syms ()))
(dolist (sym symbols)
(multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
(cond ((or (not w) (not (eq s sym)))
(error 'simple-package-error
:package package
:format-control "~S is not accessible in the ~A package."
:format-arguments (list sym (package-%name package))))
((eq w :external) (pushnew sym syms)))))
(with-single-package-locked-error ()
(when syms
(assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
(length syms) syms))
(let ((internal (package-internal-symbols package))
(external (package-external-symbols package)))
(dolist (sym syms)
(add-symbol internal sym)
(nuke-symbol external (symbol-name sym)))))
t)))
;;; Check for name conflict caused by the import and let the user
;;; shadowing-import if there is.
(defun import (symbols &optional (package (sane-package)))
#!+sb-doc
"Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol is
already accessible then it has no effect. If a name conflict would result from
the importation, then a correctable error is signalled."
(with-package-graph ()
(let* ((package (find-undeleted-package-or-lose package))
(symbols (symbol-listify symbols))
(homeless (remove-if #'symbol-package symbols))
(syms ()))
(with-single-package-locked-error ()
(dolist (sym symbols)
(multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
(cond ((not w)
(let ((found (member sym syms :test #'string=)))
(if found
(when (not (eq (car found) sym))
(setf syms (remove (car found) syms))
(name-conflict package 'import sym sym (car found)))
(push sym syms))))
((not (eq s sym))
(name-conflict package 'import sym sym s))
((eq w :inherited) (push sym syms)))))
(when (or homeless syms)
(let ((union (delete-duplicates (append homeless syms))))
(assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
(length union) union)))
;; Add the new symbols to the internal hashtable.
(let ((internal (package-internal-symbols package)))
(dolist (sym syms)
(add-symbol internal sym)))
;; If any of the symbols are uninterned, make them be owned by PACKAGE.
(dolist (sym homeless)
(%set-symbol-package sym package))
t))))
;;; If a conflicting symbol is present, unintern it, otherwise just
;;; stick the symbol in.
(defun shadowing-import (symbols &optional (package (sane-package)))
#!+sb-doc
"Import SYMBOLS into package, disregarding any name conflict. If
a symbol of the same name is present, then it is uninterned."
(with-package-graph ()
(let* ((package (find-undeleted-package-or-lose package))
(internal (package-internal-symbols package))
(symbols (symbol-listify symbols))
(lock-asserted-p nil))
(with-single-package-locked-error ()
(dolist (sym symbols)
(multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
(unless (or lock-asserted-p
(and (eq s sym)
(member s (package-shadowing-symbols package))))
(assert-package-unlocked package "shadowing-importing symbol~P ~
~{~A~^, ~}" (length symbols) symbols)
(setf lock-asserted-p t))
(unless (and w (not (eq w :inherited)) (eq s sym))
(when (or (eq w :internal) (eq w :external))
;; If it was shadowed, we don't want UNINTERN to flame out...
(setf (package-%shadowing-symbols package)
(remove s (the list (package-%shadowing-symbols package))))
(unintern s package))
(add-symbol internal sym))
(pushnew sym (package-%shadowing-symbols package)))))))
t)
(defun shadow (symbols &optional (package (sane-package)))
#!+sb-doc
"Make an internal symbol in PACKAGE with the same name as each of the
specified SYMBOLS. If a symbol with the given name is already present in
PACKAGE, then the existing symbol is placed in the shadowing symbols list if
it is not already present."
(with-package-graph ()
(let* ((package (find-undeleted-package-or-lose package))
(internal (package-internal-symbols package))
(symbols (string-listify symbols))
(lock-asserted-p nil))
(flet ((present-p (w)
(and w (not (eq w :inherited)))))
(with-single-package-locked-error ()
(dolist (name symbols)
(multiple-value-bind (s w) (find-symbol name package)
(unless (or lock-asserted-p
(and (present-p w)
(member s (package-shadowing-symbols package))))
(assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
(length symbols) symbols)
(setf lock-asserted-p t))
(unless (present-p w)
(setq s (make-symbol name))
(%set-symbol-package s package)
(add-symbol internal s))
(pushnew s (package-%shadowing-symbols package))))))))
t)
;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
(defun use-package (packages-to-use &optional (package (sane-package)))
#!+sb-doc
"Add all the PACKAGES-TO-USE to the use list for PACKAGE so that the
external symbols of the used packages are accessible as internal symbols in
PACKAGE."
(with-package-graph ()
(let ((packages (package-listify packages-to-use))
(package (find-undeleted-package-or-lose package)))
;; Loop over each package, USE'ing one at a time...
(with-single-package-locked-error ()
(dolist (pkg packages)
(unless (member pkg (package-%use-list package))
(assert-package-unlocked package "using package~P ~{~A~^, ~}"
(length packages) packages)
(let ((shadowing-symbols (package-%shadowing-symbols package))
(use-list (package-%use-list package)))
;; If the number of symbols already accessible is less
;; than the number to be inherited then it is faster to
;; run the test the other way. This is particularly
;; valuable in the case of a new package USEing
;; COMMON-LISP.
(cond
((< (+ (package-internal-symbol-count package)
(package-external-symbol-count package)
(let ((res 0))
(dolist (p use-list res)
(incf res (package-external-symbol-count p)))))
(package-external-symbol-count pkg))
(do-symbols (sym package)
(multiple-value-bind (s w)
(find-external-symbol (symbol-name sym) pkg)
(when (and w
(not (eq s sym))
(not (member sym shadowing-symbols)))
(name-conflict package 'use-package pkg sym s))))
(dolist (p use-list)
(do-external-symbols (sym p)
(multiple-value-bind (s w)
(find-external-symbol (symbol-name sym) pkg)
(when (and w
(not (eq s sym))
(not (member
(find-symbol (symbol-name sym) package)
shadowing-symbols)))
(name-conflict package 'use-package pkg sym s))))))
(t
(do-external-symbols (sym pkg)
(multiple-value-bind (s w)
(find-symbol (symbol-name sym) package)
(when (and w
(not (eq s sym))
(not (member s shadowing-symbols)))
(name-conflict package 'use-package pkg sym s)))))))
(push pkg (package-%use-list package))
(push (package-external-symbols pkg) (cdr (package-tables package)))
(push package (package-%used-by-list pkg)))))))
t)
(defun unuse-package (packages-to-unuse &optional (package (sane-package)))
#!+sb-doc
"Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
(with-package-graph ()
(let ((package (find-undeleted-package-or-lose package))
(packages (package-listify packages-to-unuse)))
(with-single-package-locked-error ()
(dolist (p packages)
(when (member p (package-use-list package))
(assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
(length packages) packages))
(setf (package-%use-list package)
(remove p (the list (package-%use-list package))))
(setf (package-tables package)
(delete (package-external-symbols p)
(the list (package-tables package))))
(setf (package-%used-by-list p)
(remove package (the list (package-%used-by-list p))))))
t)))
(defun find-all-symbols (string-or-symbol)
#!+sb-doc
"Return a list of all symbols in the system having the specified name."
(let ((string (string string-or-symbol))
(res ()))
(with-package-names (names)
(maphash (lambda (k v)
(declare (ignore k))
(multiple-value-bind (s w) (find-symbol string v)
(when w (pushnew s res))))
names))
res))
;;;; APROPOS and APROPOS-LIST
(defun briefly-describe-symbol (symbol)
(fresh-line)
(prin1 symbol)
(when (boundp symbol)
(write-string " (bound)"))
(when (fboundp symbol)
(write-string " (fbound)")))
(defun apropos-list (string-designator
&optional
package-designator
external-only)
#!+sb-doc
"Like APROPOS, except that it returns a list of the symbols found instead
of describing them."
(if package-designator
(let ((package (find-undeleted-package-or-lose package-designator))
(string (stringify-string-designator string-designator))
(result nil))
(do-symbols (symbol package)
(when (and (eq (symbol-package symbol) package)
(or (not external-only)
(eq (nth-value 1 (find-symbol (symbol-name symbol)
package))
:external))
(search string (symbol-name symbol) :test #'char-equal))
(push symbol result)))
(sort result #'string-lessp))
(mapcan (lambda (package)
(apropos-list string-designator package external-only))
(sort (list-all-packages) #'string-lessp :key #'package-name))))
(defun apropos (string-designator &optional package external-only)
#!+sb-doc
"Briefly describe all symbols which contain the specified STRING.
If PACKAGE is supplied then only describe symbols present in
that package. If EXTERNAL-ONLY then only describe
external symbols in the specified package."
;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
;; of some unnecessary consing; and the unnecessary consing shouldn't be an
;; issue, since this function is is only useful interactively anyway, and
;; we can cons and GC a lot faster than the typical user can read..
(dolist (symbol (apropos-list string-designator package external-only))
(briefly-describe-symbol symbol))
(values))
;;;; final initialization
;;;; The cold loader (GENESIS) makes the data structure in
;;;; *!INITIAL-SYMBOLS*. We grovel over it, making the specified
;;;; packages and interning the symbols. For a description of the
;;;; format of *!INITIAL-SYMBOLS*, see the GENESIS source.
(defvar *!initial-symbols*)
(!cold-init-forms
(setq *in-package-init* t)
(/show0 "about to loop over *!INITIAL-SYMBOLS* to make packages")
(dolist (spec *!initial-symbols*)
(let* ((pkg (apply #'make-package (first spec)))
(internal (package-internal-symbols pkg))
(external (package-external-symbols pkg)))
(/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..")
(/primitive-print (package-name pkg))
;; Put internal symbols in the internal hashtable and set package.
(dolist (symbol (second spec))
(add-symbol internal symbol)
(%set-symbol-package symbol pkg))
;; External symbols same, only go in external table.
(dolist (symbol (third spec))
(add-symbol external symbol)
(%set-symbol-package symbol pkg))
;; Don't set package for imported symbols.
(dolist (symbol (fourth spec))
(add-symbol internal symbol))
(dolist (symbol (fifth spec))
(add-symbol external symbol))
;; Put shadowing symbols in the shadowing symbols list.
(setf (package-%shadowing-symbols pkg) (sixth spec))
;; Set the package documentation
(setf (package-doc-string pkg) (seventh spec))))
;; FIXME: These assignments are also done at toplevel in
;; boot-extensions.lisp. They should probably only be done once.
(/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*")
(setq *cl-package* (find-package "COMMON-LISP"))
(setq *keyword-package* (find-package "KEYWORD"))
(/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
(%makunbound '*!initial-symbols*) ; (so that it gets GCed)
;; Make some other packages that should be around in the cold load.
;; The COMMON-LISP-USER package is required by the ANSI standard,
;; but not completely specified by it, so in the cross-compilation
;; host Lisp it could contain various symbols, USE-PACKAGEs, or
;; nicknames that we don't want in our target SBCL. For that reason,
;; we handle it specially, not dumping the host Lisp version at
;; genesis time..
(aver (not (find-package "COMMON-LISP-USER")))
;; ..but instead making our own from scratch here.
(/show0 "about to MAKE-PACKAGE COMMON-LISP-USER")
(make-package "COMMON-LISP-USER"
:nicknames '("CL-USER")
:use '("COMMON-LISP"
;; ANSI encourages us to put extension packages
;; in the USE list of COMMON-LISP-USER.
"SB!ALIEN" "SB!ALIEN" "SB!DEBUG"
"SB!EXT" "SB!GRAY" "SB!PROFILE"))
;; Now do the *!DEFERRED-USE-PACKAGES*.
(/show0 "about to do *!DEFERRED-USE-PACKAGES*")
(dolist (args *!deferred-use-packages*)
(apply #'use-package args))
;; The Age Of Magic is over, we can behave ANSIly henceforth.
(/show0 "about to SETQ *IN-PACKAGE-INIT*")
(setq *in-package-init* nil)
;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
;;
;; FIXME: We should just set this to (FIND-PACKAGE
;; "COMMON-LISP-USER") once and for all here, instead of setting it
;; once here and resetting it later.
(setq *package* *cl-package*))
(!cold-init-forms
(/show0 "done with !PACKAGE-COLD-INIT"))
(!defun-from-collected-cold-init-forms !package-cold-init)