-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathattrib.c
1983 lines (1784 loc) · 57.4 KB
/
attrib.c
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
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1997--2023 The R Core Team
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <Defn.h>
#include <Internal.h>
#include <Rmath.h>
#ifdef Win32
#include <trioremap.h> /* for %lld */
#endif
static SEXP installAttrib(SEXP, SEXP, SEXP);
static SEXP removeAttrib(SEXP, SEXP);
SEXP comment(SEXP);
static SEXP commentgets(SEXP, SEXP);
static SEXP row_names_gets(SEXP vec, SEXP val)
{
SEXP ans;
if (vec == R_NilValue)
error(_("attempt to set an attribute on NULL"));
if(isReal(val) && LENGTH(val) == 2 && ISNAN(REAL(val)[0]) ) {
/* This should not happen, but if a careless user dput()s a
data frame and sources the result, it will */
PROTECT(vec);
PROTECT(val);
val = coerceVector(val, INTSXP);
UNPROTECT(1); /* val */
PROTECT(val);
ans = installAttrib(vec, R_RowNamesSymbol, val);
UNPROTECT(2); /* vec, val */
return ans;
}
if(isInteger(val)) {
Rboolean OK_compact = TRUE;
int i, n = LENGTH(val);
if(n == 2 && INTEGER(val)[0] == NA_INTEGER) {
n = INTEGER(val)[1];
} else if (n > 2) {
for(i = 0; i < n; i++)
if(INTEGER(val)[i] != i+1) {
OK_compact = FALSE;
break;
}
} else OK_compact = FALSE;
if(OK_compact) {
/* we hide the length in an impossible integer vector */
PROTECT(vec);
PROTECT(val = allocVector(INTSXP, 2));
INTEGER(val)[0] = NA_INTEGER;
INTEGER(val)[1] = n; // +n: compacted *and* automatic row names
ans = installAttrib(vec, R_RowNamesSymbol, val);
UNPROTECT(2); /* vec, val */
return ans;
}
} else if(!isString(val))
error(_("row names must be 'character' or 'integer', not '%s'"),
R_typeToChar(val));
PROTECT(vec);
PROTECT(val);
ans = installAttrib(vec, R_RowNamesSymbol, val);
UNPROTECT(2); /* vec, val */
return ans;
}
/* used in removeAttrib, commentgets and classgets */
static SEXP stripAttrib(SEXP tag, SEXP lst)
{
if(lst == R_NilValue) return lst;
if(tag == TAG(lst)) return stripAttrib(tag, CDR(lst));
SETCDR(lst, stripAttrib(tag, CDR(lst)));
return lst;
}
static Rboolean isOneDimensionalArray(SEXP vec)
{
if(isVector(vec) || isList(vec) || isLanguage(vec)) {
SEXP s = getAttrib(vec, R_DimSymbol);
if(TYPEOF(s) == INTSXP && LENGTH(s) == 1)
return TRUE;
}
return FALSE;
}
/* NOTE: For environments serialize.c calls this function to find if
there is a class attribute in order to reconstruct the object bit
if needed. This means the function cannot use OBJECT(vec) == 0 to
conclude that the class attribute is R_NilValue. If you want to
rewrite this function to use such a pre-test, be sure to adjust
serialize.c accordingly. LT */
attribute_hidden SEXP getAttrib0(SEXP vec, SEXP name)
{
SEXP s;
if (name == R_NamesSymbol) {
if(isOneDimensionalArray(vec)) {
s = getAttrib(vec, R_DimNamesSymbol);
if(!isNull(s)) {
MARK_NOT_MUTABLE(VECTOR_ELT(s, 0));
return VECTOR_ELT(s, 0);
}
}
if (isList(vec) || isLanguage(vec) || TYPEOF(vec) == DOTSXP) {
int len = length(vec);
PROTECT(s = allocVector(STRSXP, len));
int i = 0;
Rboolean any = FALSE;
for ( ; vec != R_NilValue; vec = CDR(vec), i++) {
if (TAG(vec) == R_NilValue)
{
SET_STRING_ELT(s, i, R_BlankString);
}
else if (isSymbol(TAG(vec))) {
any = TRUE;
SET_STRING_ELT(s, i, PRINTNAME(TAG(vec)));
}
else
error(_("getAttrib: invalid type (%s) for TAG"),
R_typeToChar(TAG(vec)));
}
UNPROTECT(1);
if (any) {
if (!isNull(s)) MARK_NOT_MUTABLE(s);
return (s);
} else
return R_NilValue;
}
}
for (s = ATTRIB(vec); s != R_NilValue; s = CDR(s))
if (TAG(s) == name) {
if (name == R_DimNamesSymbol && TYPEOF(CAR(s)) == LISTSXP)
error("old list is no longer allowed for dimnames attribute");
/**** this could be dropped for REFCNT or be less
stringent for NAMED for attributes where the setter
does not have a consistency check that could fail
after mutation in a complex assignment LT */
MARK_NOT_MUTABLE(CAR(s));
return CAR(s);
}
return R_NilValue;
}
SEXP getAttrib(SEXP vec, SEXP name)
{
if(TYPEOF(vec) == CHARSXP)
error("cannot have attributes on a CHARSXP");
/* pre-test to avoid expensive operations if clearly not needed -- LT */
if (ATTRIB(vec) == R_NilValue &&
! (TYPEOF(vec) == LISTSXP || TYPEOF(vec) == LANGSXP|| TYPEOF(vec) == DOTSXP))
return R_NilValue;
if (isString(name)) name = installTrChar(STRING_ELT(name, 0));
/* special test for c(NA, n) rownames of data frames: */
if (name == R_RowNamesSymbol) {
SEXP s = getAttrib0(vec, R_RowNamesSymbol);
if(isInteger(s) && LENGTH(s) == 2 && INTEGER(s)[0] == NA_INTEGER) {
int n = abs(INTEGER(s)[1]);
if (n > 0)
s = R_compact_intrange(1, n);
else
s = allocVector(INTSXP, 0);
}
return s;
} else
return getAttrib0(vec, name);
}
// R's .row_names_info(x, type = 1L) := .Internal(shortRowNames(x, type)) :
attribute_hidden
SEXP do_shortRowNames(SEXP call, SEXP op, SEXP args, SEXP env)
{
/* return n if the data frame 'vec' has c(NA, n) rownames;
* nrow(.) otherwise; note that data frames with nrow(.) == 0
* have no row.names.
==> is also used in dim.data.frame() */
checkArity(op, args);
SEXP s = getAttrib0(CAR(args), R_RowNamesSymbol), ans = s;
int type = asInteger(CADR(args));
if( type < 0 || type > 2)
error(_("invalid '%s' argument"), "type");
if(type >= 1) {
int n = (isInteger(s) && LENGTH(s) == 2 && INTEGER(s)[0] == NA_INTEGER)
? INTEGER(s)[1] : (isNull(s) ? 0 : LENGTH(s));
ans = ScalarInteger((type == 1) ? n : abs(n));
}
return ans;
}
// .Internal(copyDFattr(in, out)) -- is allowed to change 'out' (!!)
attribute_hidden
SEXP do_copyDFattr(SEXP call, SEXP op, SEXP args, SEXP env)
{
checkArity(op, args);
SEXP in = CAR(args), out = CADR(args);
SET_ATTRIB(out, shallow_duplicate(ATTRIB(in)));
IS_S4_OBJECT(in) ? SET_S4_OBJECT(out) : UNSET_S4_OBJECT(out);
SET_OBJECT(out, OBJECT(in));
return out;
}
/* 'name' should be 1-element STRSXP or SYMSXP */
SEXP setAttrib(SEXP vec, SEXP name, SEXP val)
{
PROTECT(vec);
PROTECT(name);
if (isString(name)) {
PROTECT(val);
name = installTrChar(STRING_ELT(name, 0));
UNPROTECT(1);
}
if (val == R_NilValue) {
/* FIXME: see do_namesgets().
if (name == R_NamesSymbol && isOneDimensionalArray(vec)) {
UNPROTECT(2);
return removeAttrib(vec, R_DimNamesSymbol);
}
*/
UNPROTECT(2);
return removeAttrib(vec, name);
}
/* We allow attempting to remove names from NULL */
if (vec == R_NilValue)
error(_("attempt to set an attribute on NULL"));
UNPROTECT(2);
if (name == R_NamesSymbol)
return namesgets(vec, val);
else if (name == R_DimSymbol)
return dimgets(vec, val);
else if (name == R_DimNamesSymbol)
return dimnamesgets(vec, val);
else if (name == R_ClassSymbol)
return classgets(vec, val);
else if (name == R_TspSymbol)
return tspgets(vec, val);
else if (name == R_CommentSymbol)
return commentgets(vec, val);
else if (name == R_RowNamesSymbol) // "row.names" -> care for data frames
return row_names_gets(vec, val);
else
return installAttrib(vec, name, val);
}
/* This is called in the case of binary operations to copy */
/* most attributes from (one of) the input arguments to */
/* the output. Note that the Dim and Names attributes */
/* should have been assigned elsewhere. */
void copyMostAttrib(SEXP inp, SEXP ans)
{
SEXP s;
if (ans == R_NilValue)
error(_("attempt to set an attribute on NULL"));
PROTECT(ans);
PROTECT(inp);
for (s = ATTRIB(inp); s != R_NilValue; s = CDR(s)) {
if ((TAG(s) != R_NamesSymbol) &&
(TAG(s) != R_DimSymbol) &&
(TAG(s) != R_DimNamesSymbol)) { // << for matrix, array ..
installAttrib(ans, TAG(s), CAR(s));
}
}
if (OBJECT(inp)) SET_OBJECT(ans, 1);
IS_S4_OBJECT(inp) ? SET_S4_OBJECT(ans) : UNSET_S4_OBJECT(ans);
UNPROTECT(2);
}
/* version that does not preserve ts information, for subsetting */
void copyMostAttribNoTs(SEXP inp, SEXP ans)
{
SEXP s;
int is_object = OBJECT(inp);
int is_s4_object = IS_S4_OBJECT(inp);
if (ans == R_NilValue)
error(_("attempt to set an attribute on NULL"));
PROTECT(ans);
PROTECT(inp);
for (s = ATTRIB(inp); s != R_NilValue; s = CDR(s)) {
if ((TAG(s) != R_NamesSymbol) &&
(TAG(s) != R_ClassSymbol) &&
(TAG(s) != R_TspSymbol) &&
(TAG(s) != R_DimSymbol) &&
(TAG(s) != R_DimNamesSymbol)) {
installAttrib(ans, TAG(s), CAR(s));
} else if (TAG(s) == R_ClassSymbol) {
SEXP cl = CAR(s);
int i;
Rboolean ists = FALSE;
for (i = 0; i < LENGTH(cl); i++)
if (strcmp(CHAR(STRING_ELT(cl, i)), "ts") == 0) { /* ASCII */
ists = TRUE;
break;
}
if (!ists) installAttrib(ans, TAG(s), cl);
else if(LENGTH(cl) <= 1) {
/* dropping class attribute */
is_object = 0;
is_s4_object = 0;
} else {
SEXP new_cl;
int i, j, l = LENGTH(cl);
PROTECT(new_cl = allocVector(STRSXP, l - 1));
for (i = 0, j = 0; i < l; i++)
if (strcmp(CHAR(STRING_ELT(cl, i)), "ts")) /* ASCII */
SET_STRING_ELT(new_cl, j++, STRING_ELT(cl, i));
installAttrib(ans, TAG(s), new_cl);
UNPROTECT(1);
}
}
}
SET_OBJECT(ans, is_object);
is_s4_object ? SET_S4_OBJECT(ans) : UNSET_S4_OBJECT(ans);
UNPROTECT(2);
}
/* Tweaks here based in part on PR#14934 */
static SEXP installAttrib(SEXP vec, SEXP name, SEXP val)
{
SEXP t = R_NilValue; /* -Wall */
if(TYPEOF(vec) == CHARSXP)
error("cannot set attribute on a CHARSXP");
if (TYPEOF(vec) == SYMSXP)
error(_("cannot set attribute on a symbol"));
/* this does no allocation */
for (SEXP s = ATTRIB(vec); s != R_NilValue; s = CDR(s)) {
if (TAG(s) == name) {
if (MAYBE_REFERENCED(val) && val != CAR(s))
val = R_FixupRHS(vec, val);
SETCAR(s, val);
return val;
}
t = s; // record last attribute, if any
}
/* The usual convention is that the caller protects,
but a lot of existing code depends assume that
setAttrib/installAttrib protects its arguments */
PROTECT(vec); PROTECT(name); PROTECT(val);
if (MAYBE_REFERENCED(val)) ENSURE_NAMEDMAX(val);
SEXP s = CONS(val, R_NilValue);
SET_TAG(s, name);
if (ATTRIB(vec) == R_NilValue) SET_ATTRIB(vec, s); else SETCDR(t, s);
UNPROTECT(3);
return val;
}
static SEXP removeAttrib(SEXP vec, SEXP name)
{
SEXP t;
if(TYPEOF(vec) == CHARSXP)
error("cannot set attribute on a CHARSXP");
if (name == R_NamesSymbol && isPairList(vec)) {
for (t = vec; t != R_NilValue; t = CDR(t))
SET_TAG(t, R_NilValue);
return R_NilValue;
}
else {
if (name == R_DimSymbol)
SET_ATTRIB(vec, stripAttrib(R_DimNamesSymbol, ATTRIB(vec)));
SET_ATTRIB(vec, stripAttrib(name, ATTRIB(vec)));
if (name == R_ClassSymbol)
SET_OBJECT(vec, 0);
}
return R_NilValue;
}
static void checkNames(SEXP x, SEXP s)
{
if (isVector(x) || isList(x) || isLanguage(x)) {
if (!isVector(s) && !isList(s))
error(_("invalid type (%s) for 'names': must be vector or NULL"),
R_typeToChar(s));
if (xlength(x) != xlength(s))
error(_("'names' attribute [%lld] must be the same length as the vector [%lld]"),
(long long)xlength(s), (long long)xlength(x));
}
else if(IS_S4_OBJECT(x)) {
/* leave validity checks to S4 code */
}
else error(_("names() applied to a non-vector"));
}
/* Time Series Parameters */
NORET static void badtsp(void)
{
error(_("invalid time series parameters specified"));
}
attribute_hidden
SEXP tspgets(SEXP vec, SEXP val)
{
double start, end, frequency;
int n;
if (vec == R_NilValue)
error(_("attempt to set an attribute on NULL"));
if(IS_S4_OBJECT(vec)) { /* leave validity checking to validObject */
if (!isNumeric(val)) /* but should have been checked */
error(_("'tsp' attribute must be numeric"));
installAttrib(vec, R_TspSymbol, val);
return vec;
}
if (!isNumeric(val) || LENGTH(val) != 3)
error(_("'tsp' attribute must be numeric of length three"));
if (isReal(val)) {
start = REAL(val)[0];
end = REAL(val)[1];
frequency = REAL(val)[2];
}
else {
start = (INTEGER(val)[0] == NA_INTEGER) ?
NA_REAL : INTEGER(val)[0];
end = (INTEGER(val)[1] == NA_INTEGER) ?
NA_REAL : INTEGER(val)[1];
frequency = (INTEGER(val)[2] == NA_INTEGER) ?
NA_REAL : INTEGER(val)[2];
}
if (frequency <= 0) badtsp();
n = nrows(vec);
if (n == 0) error(_("cannot assign 'tsp' to zero-length vector"));
/* FIXME: 1.e-5 should rather be == option('ts.eps') !! */
if (fabs(end - start - (n - 1)/frequency) > 1.e-5)
badtsp();
PROTECT(vec);
val = allocVector(REALSXP, 3);
PROTECT(val);
REAL(val)[0] = start;
REAL(val)[1] = end;
REAL(val)[2] = frequency;
installAttrib(vec, R_TspSymbol, val);
UNPROTECT(2);
return vec;
}
static SEXP commentgets(SEXP vec, SEXP comment)
{
if (vec == R_NilValue)
error(_("attempt to set an attribute on NULL"));
if (isNull(comment) || isString(comment)) {
if (length(comment) <= 0) {
SET_ATTRIB(vec, stripAttrib(R_CommentSymbol, ATTRIB(vec)));
}
else {
installAttrib(vec, R_CommentSymbol, comment);
}
return R_NilValue;
}
error(_("attempt to set invalid 'comment' attribute"));
return R_NilValue;/*- just for -Wall */
}
attribute_hidden SEXP do_commentgets(SEXP call, SEXP op, SEXP args, SEXP env)
{
checkArity(op, args);
if (MAYBE_SHARED(CAR(args))) SETCAR(args, duplicate(CAR(args)));
if (length(CADR(args)) == 0) SETCADR(args, R_NilValue);
setAttrib(CAR(args), R_CommentSymbol, CADR(args));
SETTER_CLEAR_NAMED(CAR(args));
return CAR(args);
}
attribute_hidden SEXP do_comment(SEXP call, SEXP op, SEXP args, SEXP env)
{
checkArity(op, args);
return getAttrib(CAR(args), R_CommentSymbol);
}
/* *Not* called from class(.) <- v, nor oldClass(.) <- v, but
* e.g. from attr(x, "class") <- value plus our own C, e.g. ./connections.c
*/
SEXP classgets(SEXP vec, SEXP klass)
{
if (isNull(klass) || isString(klass)) {
int ncl = length(klass);
if (ncl <= 0) {
SET_ATTRIB(vec, stripAttrib(R_ClassSymbol, ATTRIB(vec)));
SET_OBJECT(vec, 0);
// problems when package building: UNSET_S4_OBJECT(vec);
}
else {
/* When data frames were a special data type */
/* we had more exhaustive checks here. Now that */
/* use JMCs interpreted code, we don't need this */
/* FIXME : The whole "classgets" may as well die. */
/* HOWEVER, it is the way that the object bit gets set/unset */
Rboolean isfactor = FALSE;
if (vec == R_NilValue)
error(_("attempt to set an attribute on NULL"));
for(int i = 0; i < ncl; i++)
if(streql(CHAR(STRING_ELT(klass, i)), "factor")) { /* ASCII */
isfactor = TRUE;
break;
}
if(isfactor && TYPEOF(vec) != INTSXP) {
/* we cannot coerce vec here, so just fail */
error(_("adding class \"factor\" to an invalid object"));
}
installAttrib(vec, R_ClassSymbol, klass);
SET_OBJECT(vec, 1);
#ifdef R_classgets_copy_S4
// not ok -- fails at installation around byte-compiling methods
if(ncl == 1 && R_has_methods_attached()) { // methods: do not act too early
SEXP cld = R_getClassDef_R(klass);
if(!isNull(cld)) {
PROTECT(cld);
/* More efficient? can we protect? -- rather *assign* in method-ns?
static SEXP oldCl = NULL;
if(!oldCl) oldCl = R_getClassDef("oldClass");
if(!oldCl) oldCl = mkString("oldClass");
PROTECT(oldCl);
*/
if(!R_isVirtualClass(cld, R_MethodsNamespace) &&
!R_extends(cld, mkString("oldClass"), R_MethodsNamespace)) // set S4 bit :
// !R_extends(cld, oldCl, R_MethodsNamespace)) // set S4 bit :
SET_S4_OBJECT(vec);
UNPROTECT(1); // UNPROTECT(2);
}
}
#endif
}
}
else
error(_("attempt to set invalid 'class' attribute"));
return R_NilValue;
}
/* oldClass<-(), primitive */
attribute_hidden SEXP do_classgets(SEXP call, SEXP op, SEXP args, SEXP env)
{
checkArity(op, args);
// have 2 args: check1arg(args, call, "x");
if (MAYBE_SHARED(CAR(args)) ||
((! IS_ASSIGNMENT_CALL(call)) && MAYBE_REFERENCED(CAR(args))))
SETCAR(args, shallow_duplicate(CAR(args)));
if (length(CADR(args)) == 0) SETCADR(args, R_NilValue);
if(IS_S4_OBJECT(CAR(args)))
UNSET_S4_OBJECT(CAR(args));
setAttrib(CAR(args), R_ClassSymbol, CADR(args));
SETTER_CLEAR_NAMED(CAR(args));
return CAR(args);
}
// oldClass, primitive -- NB: class() |=> R_do_data_class() |=> R_data_class()
attribute_hidden SEXP do_class(SEXP call, SEXP op, SEXP args, SEXP env)
{
checkArity(op, args);
check1arg(args, call, "x");
SEXP x = CAR(args), s3class;
if(IS_S4_OBJECT(x)) {
if((s3class = S3Class(x)) != R_NilValue) {
return s3class;
}
} /* else */
return getAttrib(x, R_ClassSymbol);
}
/* character elements corresponding to the syntactic types in the
grammar */
static SEXP lang2str(SEXP obj)
{
SEXP symb = CAR(obj);
static SEXP if_sym = 0, while_sym, for_sym, eq_sym, gets_sym,
lpar_sym, lbrace_sym, call_sym;
if(!if_sym) {
/* initialize: another place for a hash table */
if_sym = install("if");
while_sym = install("while");
for_sym = install("for");
eq_sym = install("=");
gets_sym = install("<-");
lpar_sym = install("(");
lbrace_sym = install("{");
call_sym = install("call");
}
if(isSymbol(symb)) {
if(symb == if_sym || symb == for_sym || symb == while_sym ||
symb == lpar_sym || symb == lbrace_sym ||
symb == eq_sym || symb == gets_sym)
return PRINTNAME(symb);
}
return PRINTNAME(call_sym);
}
/* R's class(), for S4 dispatch required to be a single string;
if(!singleString) , keeps S3-style multiple classes.
Called from the methods package, so exposed.
*/
SEXP R_data_class(SEXP obj, Rboolean singleString)
{
SEXP value, klass = getAttrib(obj, R_ClassSymbol);
int n = length(klass);
if(n == 1 || (n > 0 && !singleString))
return(klass);
if(n == 0) {
SEXP dim = getAttrib(obj, R_DimSymbol);
int nd = length(dim);
if(nd > 0) {
if(nd == 2) {
if(singleString)
klass = mkChar("matrix");
else { // R >= 4.0.0 : class(<matrix>) |-> c("matrix", "array")
PROTECT(klass = allocVector(STRSXP, 2));
SET_STRING_ELT(klass, 0, mkChar("matrix"));
SET_STRING_ELT(klass, 1, mkChar("array"));
UNPROTECT(1);
return klass;
}
}
else
klass = mkChar("array");
}
else {
SEXPTYPE t = TYPEOF(obj);
switch(t) {
case CLOSXP: case SPECIALSXP: case BUILTINSXP:
klass = mkChar("function");
break;
case REALSXP:
klass = mkChar("numeric");
break;
case SYMSXP:
klass = mkChar("name");
break;
case LANGSXP:
klass = lang2str(obj);
break;
case OBJSXP:
klass = mkChar(IS_S4_OBJECT(obj) ? "S4" : "object");
break;
default:
klass = type2str(t);
}
}
}
else
klass = asChar(klass);
PROTECT(klass);
value = ScalarString(klass);
UNPROTECT(1);
return value;
}
static SEXP s_dot_S3Class = 0;
static SEXP R_S4_extends_table = 0;
static SEXP cache_class(const char *class, SEXP klass)
{
if(!R_S4_extends_table) {
R_S4_extends_table = R_NewHashedEnv(R_NilValue, 0);
R_PreserveObject(R_S4_extends_table);
}
if(isNull(klass)) {
R_removeVarFromFrame(install(class), R_S4_extends_table);
} else {
defineVar(install(class), klass, R_S4_extends_table);
}
return klass;
}
static SEXP S4_extends(SEXP klass, Rboolean use_tab) {
static SEXP s_extends = 0, s_extendsForS3;
SEXP e, val; const char *class;
const void *vmax;
if(use_tab) vmax = vmaxget();
if(!s_extends) {
s_extends = install("extends");
s_extendsForS3 = install(".extendsForS3");
R_S4_extends_table = R_NewHashedEnv(R_NilValue, 0);
R_PreserveObject(R_S4_extends_table);
}
if(!isMethodsDispatchOn()) {
return klass;
}
class = translateChar(STRING_ELT(klass, 0)); /* TODO: include package attr. */
if(use_tab) {
val = findVarInFrame(R_S4_extends_table, install(class));
vmaxset(vmax);
if(val != R_UnboundValue)
return val;
}
// else: val <- .extendsForS3(klass) -- and cache it
PROTECT(e = allocVector(LANGSXP, 2));
SETCAR(e, s_extendsForS3);
val = CDR(e);
SETCAR(val, klass);
PROTECT(val = eval(e, R_MethodsNamespace));
cache_class(class, val);
UNPROTECT(2); /* val, e */
return(val);
}
SEXP R_S4_extends(SEXP klass, SEXP useTable)
{
return S4_extends(klass, asLogical(useTable));
}
/* pre-allocated default class attributes */
static struct {
SEXP vector;
SEXP matrix;
SEXP array;
} Type2DefaultClass[MAX_NUM_SEXPTYPE];
static SEXP createDefaultClass(SEXP part1, SEXP part2, SEXP part3, SEXP part4)
{
int size = 0;
if (part1 != R_NilValue) size++;
if (part2 != R_NilValue) size++;
if (part3 != R_NilValue) size++;
if (part4 != R_NilValue) size++;
if (size == 0 || part3 == R_NilValue) // .. ?
return R_NilValue;
SEXP res = allocVector(STRSXP, size);
R_PreserveObject(res);
int i = 0;
if (part1 != R_NilValue) SET_STRING_ELT(res, i++, part1);
if (part2 != R_NilValue) SET_STRING_ELT(res, i++, part2);
if (part3 != R_NilValue) SET_STRING_ELT(res, i++, part3);
if (part4 != R_NilValue) SET_STRING_ELT(res, i, part4);
MARK_NOT_MUTABLE(res);
return res;
}
// called when R's main loop is setup :
attribute_hidden
void InitS3DefaultTypes(void)
{
for(int type = 0; type < MAX_NUM_SEXPTYPE; type++) {
SEXP part3 = R_NilValue;
SEXP part4 = R_NilValue;
int nprotected = 0;
switch(type) {
case CLOSXP:
case SPECIALSXP:
case BUILTINSXP:
part3 = PROTECT(mkChar("function"));
nprotected++;
break;
case INTSXP:
case REALSXP:
part3 = PROTECT(type2str_nowarn(type));
part4 = PROTECT(mkChar("numeric"));
nprotected += 2;
break;
case LANGSXP:
/* part3 remains R_NilValue: default type cannot be
pre-allocated, as it depends on the object value */
break;
case SYMSXP:
part3 = PROTECT(mkChar("name"));
nprotected++;
break;
default:
part3 = PROTECT(type2str_nowarn(type));
nprotected++;
}
Type2DefaultClass[type].vector =
createDefaultClass(R_NilValue, R_NilValue, part3, part4);
SEXP part2 = PROTECT(mkChar("array"));
SEXP part1 = PROTECT(mkChar("matrix"));
nprotected += 2;
Type2DefaultClass[type].matrix =
createDefaultClass(part1, part2, part3, part4);
Type2DefaultClass[type].array =
createDefaultClass(R_NilValue, part2, part3, part4);
UNPROTECT(nprotected);
}
}
/* Version for S3- and S4-dispatch -- workhorse for R's .class2() */
attribute_hidden SEXP R_data_class2 (SEXP obj)
{
SEXP klass = getAttrib(obj, R_ClassSymbol);
if(length(klass) > 0) {
if(IS_S4_OBJECT(obj))
return S4_extends(klass, TRUE);
else
return klass;
}
else { // length(klass) == 0 , i.e., no class *attribute*: attr(obj, "class") is NULL
SEXP dim = getAttrib(obj, R_DimSymbol);
int n = length(dim);
SEXPTYPE t = TYPEOF(obj);
SEXP defaultClass;
switch(n) {
case 0: defaultClass = Type2DefaultClass[t].vector; break;
case 2: defaultClass = Type2DefaultClass[t].matrix; break;
default: defaultClass = Type2DefaultClass[t].array; break;
}
if (defaultClass != R_NilValue) {
return defaultClass;
}
/* now t == LANGSXP, but check to make sure */
if (t != LANGSXP)
error("type must be LANGSXP at this point");
if (n == 0) {
return ScalarString(lang2str(obj));
}
/* Where on earth is this ever needed ??
* __FIXME / TODO__ ??
* warning("R_data_class2(<LANGSXP with \"dim\" attribute>) .. please report!");
*/
int I_mat = (n == 2) ? 1 : 0,
nprot = 2; /* part1, defaultClass */
defaultClass = PROTECT(allocVector(STRSXP, 2 + I_mat));
SEXP part1 = PROTECT(mkChar("array")), part2;
SET_STRING_ELT(defaultClass, 0, part1);
if (n == 2) {
part2 = PROTECT(mkChar("matrix")); nprot++;
SET_STRING_ELT(defaultClass, 1, part2);
}
SET_STRING_ELT(defaultClass, 1+I_mat, lang2str(obj));
UNPROTECT(nprot);
return defaultClass;
}
}
// class(x) & .cache_class(classname, extendsForS3(.)) {called from methods} & .class2() :
attribute_hidden SEXP R_do_data_class(SEXP call, SEXP op, SEXP args, SEXP env)
{
checkArity(op, args);
if(PRIMVAL(op) == 1) { // .cache_class() - typically re-defining existing cache
check1arg(args, call, "class");
SEXP klass = CAR(args);
if(TYPEOF(klass) != STRSXP || LENGTH(klass) < 1)
error("invalid class argument to internal .class_cache");
const char *class = translateChar(STRING_ELT(klass, 0));
return cache_class(class, CADR(args));
}
check1arg(args, call, "x");
if(PRIMVAL(op) == 2)
// .class2()
return R_data_class2(CAR(args));
// class():
return R_data_class(CAR(args), FALSE);
}
/* names(object) <- name */
attribute_hidden SEXP do_namesgets(SEXP call, SEXP op, SEXP args, SEXP env)
{
SEXP ans;
checkArity(op, args);
// 2 args ("x", "value")
/* DispatchOrEval internal generic: names<- */
if (DispatchOrEval(call, op, "names<-", args, env, &ans, 0, 1))
return(ans);
/* Special case: removing non-existent names, to avoid a copy */
if (CADR(args) == R_NilValue &&
getAttrib(CAR(args), R_NamesSymbol) == R_NilValue)
return CAR(args);
PROTECT(args = ans);
if (MAYBE_SHARED(CAR(args)) ||
((! IS_ASSIGNMENT_CALL(call)) && MAYBE_REFERENCED(CAR(args))))
SETCAR(args, R_shallow_duplicate_attr(CAR(args)));
if (TYPEOF(CAR(args)) == OBJSXP) {
const char *klass = CHAR(STRING_ELT(R_data_class(CAR(args), FALSE), 0));
error(_("invalid to use names()<- on an S4 object of class '%s'"),
klass);
}
SEXP names = CADR(args);
if (names != R_NilValue &&
! (TYPEOF(names) == STRSXP && ATTRIB(names) == R_NilValue)) {
PROTECT(call = allocList(2));
SET_TYPEOF(call, LANGSXP);
SETCAR(call, R_AsCharacterSymbol);
SETCADR(call, names);
names = eval(call, env);
SETCADR(call, R_NilValue); /* decrements REFCNT on names */
UNPROTECT(1);
}
/* FIXME:
Need to special-case names(x) <- NULL for 1-d arrays to perform
setAttrib(x, R_DimNamesSymbol, R_NilValue)
(and remove the dimnames) here if we want
setAttrib(x, R_NamesSymbol, R_NilValue)
to actually remove the names, as needed in subset.c.
*/
if(names == R_NilValue && isOneDimensionalArray(CAR(args)))
setAttrib(CAR(args), R_DimNamesSymbol, names);
else
setAttrib(CAR(args), R_NamesSymbol, names);
UNPROTECT(1);
SETTER_CLEAR_NAMED(CAR(args));
return CAR(args);
}
SEXP namesgets(SEXP vec, SEXP val)
{
int i;
SEXP s, rval, tval;
PROTECT(vec);
PROTECT(val);
/* Ensure that the labels are indeed */
/* a vector of character strings */
if (isList(val)) {
if (!isVectorizable(val))
error(_("incompatible 'names' argument"));
else {
rval = allocVector(STRSXP, length(vec));
PROTECT(rval);
/* See PR#10807 */
for (i = 0, tval = val;
i < length(vec) && tval != R_NilValue;
i++, tval = CDR(tval)) {
s = coerceVector(CAR(tval), STRSXP);
SET_STRING_ELT(rval, i, STRING_ELT(s, 0));
}
UNPROTECT(1);
val = rval;
}
} else val = coerceVector(val, STRSXP);
UNPROTECT(1);
PROTECT(val);
/* Check that the lengths and types are compatible */
if (xlength(val) < xlength(vec)) { // recycle
val = xlengthgets(val, xlength(vec));
UNPROTECT(1);
PROTECT(val);
}
checkNames(vec, val);
/* Special treatment for one dimensional arrays */
if(isOneDimensionalArray(vec)) {
PROTECT(val = CONS(val, R_NilValue));
setAttrib(vec, R_DimNamesSymbol, val);
UNPROTECT(3);