/
QoreClass.cpp
4220 lines (3560 loc) · 151 KB
/
QoreClass.cpp
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
/*
QoreClass.cpp
Qore Programming Language
Copyright (C) 2003 - 2015 David Nichols
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#include <qore/Qore.h>
#include <qore/intern/Sequence.h>
#include <qore/intern/QoreClassIntern.h>
#include <qore/intern/ConstantList.h>
#include <qore/intern/qore_program_private.h>
#include <qore/intern/ql_crypto.h>
#include <qore/intern/QoreObjectIntern.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
// global class ID sequence
DLLLOCAL Sequence classIDSeq(1);
DLLLOCAL QoreValue qore_method_private::evalNormalVariant(QoreObject* self, const QoreExternalMethodVariant* ev, const QoreListNode* args, ExceptionSink* xsink) const {
const AbstractQoreFunctionVariant* variant = reinterpret_cast<const AbstractQoreFunctionVariant*>(ev);
CodeEvaluationHelper ceh(xsink, getFunction(), variant, getName(), args, variant->className());
if (*xsink) return QoreValue();
return METHV_const(variant)->evalMethod(self, ceh, xsink);
}
void qore_method_private::parseInit() {
assert(!static_flag);
//printd(5, "qore_method_private::parseInit() this: %p %s::%s() func: %p\n", this, parent_class->getName(), func->getName(), func);
func->parseInit();
const char* name = func->getName();
if (strcmp(name, "constructor")
&& strcmp(name, "destructor")
&& strcmp(name, "copy")) {
if ((!strcmp(name, "methodGate")
|| !strcmp(name, "memberGate")
|| !strcmp(name, "memberNotification"))) {
if (!func->pendingEmpty()) {
// ensure that there is no more than one parameter declared, and if it
// has a type, it must be a string
UserSignature* sig = UMV(func->pending_first())->getUserSignature();
const QoreTypeInfo* t = sig->getParamTypeInfo(0);
if (!stringTypeInfo->parseAccepts(t)) {
QoreStringNode* desc = new QoreStringNode;
desc->sprintf("%s::%s(%s) has an invalid signature; the first argument declared as ", parent_class->getName(), func->getName(), sig->getSignatureText());
t->getThisType(*desc);
desc->concat(" is not compatible with 'string'");
qore_program_private::makeParseException(getProgram(), "PARSE-TYPE-ERROR", desc);
}
}
}
else {
// make sure the method doesn't override a "final" method in a base class
func->checkFinal();
}
}
}
void SignatureHash::set(const QoreString& str) {
DigestHelper dh(str.getBuffer(), str.size());
dh.doDigest(0, EVP_sha1());
assert(dh.size() == SH_SIZE);
memcpy(buf, dh.getBuffer(), dh.size());
}
void SignatureHash::update(const QoreString& str) {
if (!is_set) {
set(str);
is_set = true;
}
else {
// make copy of old buffer
unsigned char cbuf[SH_SIZE];
memcpy(cbuf, buf, SH_SIZE);
// set hash for new addition
set(str);
// xor old hash with new hash
for (unsigned i = 0; i < SH_SIZE; ++i)
buf[i] ^= cbuf[i];
}
#ifdef DEBUG
//QoreString dbg;
//toString(dbg);
//printd(5, "class hash %p set to: %s\n", this, dbg.getBuffer());
#endif
}
AbstractMethod::AbstractMethod(const AbstractMethod& old) {
assert(!old.vlist.empty());
for (vmap_t::const_iterator i = old.vlist.begin(), e = old.vlist.end(); i != e; ++i) {
assert(vlist.find(i->first) == vlist.end());
i->second->ref();
vlist.insert(vmap_t::value_type(i->first, i->second));
}
}
AbstractMethod::~AbstractMethod() {
for (vmap_t::iterator i = vlist.begin(), e = vlist.end(); i != e; ++i)
i->second->deref();
for (vmap_t::iterator i = pending_vlist.begin(), e = pending_vlist.end(); i != e; ++i)
i->second->deref();
for (vmap_t::iterator i = pending_save.begin(), e = pending_save.end(); i != e; ++i)
i->second->deref();
}
int AbstractMethod::parseCommit() {
for (vmap_t::iterator i = pending_save.begin(), e = pending_save.end(); i != e; ++i)
i->second->deref();
pending_save.clear();
for (vmap_t::iterator i = pending_vlist.begin(), e = pending_vlist.end(); i != e; ++i) {
assert(vlist.find(i->first) == vlist.end());
vlist.insert(vmap_t::value_type(i->first, i->second));
}
pending_vlist.clear();
return vlist.empty() ? -1 : 0;
}
// merge changes from parent class method of the same name during parse initialization
void AbstractMethod::parseMergeBase(AbstractMethod& m, bool committed) {
//printd(5, "AbstractMethod::parseMergeBase(m: %p) this: %p m.pending_save: %d m.pending_vlist: %d\n", &m, this, !m.pending_save.empty(), !m.pending_vlist.empty());
// move pending committed variants from our vlist that are in parent's pending_save list to our pending_save
for (vmap_t::iterator i = m.pending_save.begin(), e = m.pending_save.end(); i != e; ++i) {
const char* sig = i->second->getAbstractSignature();
vmap_t::iterator vi = vlist.find(sig);
if (vi != vlist.end()) {
pending_save.insert(vmap_t::value_type(sig, i->second));
vlist.erase(vi);
}
}
// add new pending abstract methods from parent to our list - if they are not already in our pending_vlist or in our pending_save list
for (vmap_t::iterator i = m.pending_vlist.begin(), e = m.pending_vlist.end(); i != e; ++i) {
const char* sig = i->second->getAbstractSignature();
//printd(5, "AbstractMethod::parseMergeBase(m: %p) this: %p checking parent: '%s'\n", &m, this, sig);
if (pending_save.find(sig) != pending_save.end()) {
continue;
}
if (pending_vlist.find(sig) != pending_vlist.end()) {
continue;
}
//printd(5, "AbstractMethod::parseMergeBase(m: %p) this: %p adding to pending_vlist from parent: '%s'\n", &m, this, sig);
i->second->ref();
pending_vlist.insert(vmap_t::value_type(sig, i->second));
}
if (!committed)
return;
// add committed variants to our committed list
for (vmap_t::iterator i = m.vlist.begin(), e = m.vlist.end(); i != e; ++i) {
const char* sig = i->second->getAbstractSignature();
// see if this method already exists in this class
if (vlist.find(sig) != vlist.end())
return;
// add to vlist
i->second->ref();
vlist.insert(vmap_t::value_type(sig, i->second));
// remove from pending_vlist if present because we've already added it to the committed list
vmap_t::iterator vi = pending_vlist.find(sig);
if (vi != pending_vlist.end()) {
vi->second->deref();
pending_vlist.erase(vi);
}
}
}
// merge changes from parent class method of the same name during parse initialization
void AbstractMethod::parseMergeBase(AbstractMethod& m, MethodFunctionBase* f, bool committed) {
//printd(5, "AbstractMethod::parseMergeBase(m: %p, f: %p %s::%s) this: %p m.pending_save: %d m.pending_vlist: %d\n", &m, f, f ? f->getClassName() : "n/a", f ? f->getName() : "n/a", this, !m.pending_save.empty(), !m.pending_vlist.empty());
// move pending committed variants from our vlist that are in parent's pending_save list to our pending_save
for (vmap_t::iterator i = m.pending_save.begin(), e = m.pending_save.end(); i != e; ++i) {
const char* sig = i->second->getAbstractSignature();
vmap_t::iterator vi = vlist.find(sig);
if (vi != vlist.end()) {
pending_save.insert(vmap_t::value_type(sig, i->second));
vlist.erase(vi);
}
}
// add new pending abstract methods from parent to our list - if they are not already in our pending_vlist or in our pending_save list
for (vmap_t::iterator i = m.pending_vlist.begin(), e = m.pending_vlist.end(); i != e; ++i) {
const char* sig = i->second->getAbstractSignature();
//printd(5, "AbstractMethod::parseMergeBase(m: %p, f: %p %s::%s) this: %p checking parent: '%s' (f: %p: %d) '%s'\n", &m, f, f ? f->getClassName() : "n/a", f ? f->getName() : "n/a", this, sig, f, f && f->parseHasVariantWithSignature(i->second), sig);
if (f && f->parseHasVariantWithSignature(i->second)) {
// add to our pending_save
i->second->ref();
pending_save.insert(vmap_t::value_type(sig, i->second));
continue;
}
if (pending_save.find(sig) != pending_save.end()) {
continue;
}
if (pending_vlist.find(sig) != pending_vlist.end()) {
continue;
}
//printd(5, "AbstractMethod::parseMergeBase(m: %p, f: %p %s::%s) this: %p adding to pending_vlist from parent: '%s'\n", &m, f, f ? f->getClassName() : "n/a", f ? f->getName() : "n/a", this, sig);
i->second->ref();
pending_vlist.insert(vmap_t::value_type(sig, i->second));
}
if (!committed)
return;
// add committed variants to our committed list
for (vmap_t::iterator i = m.vlist.begin(), e = m.vlist.end(); i != e; ++i) {
const char* sig = i->second->getAbstractSignature();
if (f && f->parseHasVariantWithSignature(i->second)) {
// we already have a pending variant with this signature, so we can ignore the parent's abstract variant
// if there is a parse commit - the pending variant is committed and we don't need the parent's abstract record
// if there is a parse rollback - the current class is rolled back entirely (this function is only executed
// in one time class initialization)
continue;
}
else {
//printd(5, "AbstractMethod::parseMergeCommitted() inheriting abstract method variant %s::%s asig: %s\n", f ? f->getClassName() : "xxx", f ? f->getName() : "xxx", sig);
// insert in the committed list for this class
assert(vlist.find(sig) == vlist.end());
i->second->ref();
vlist.insert(vmap_t::value_type(sig, i->second));
// cannot be in pending_vlist
assert(pending_vlist.find(sig) == pending_vlist.end());
}
}
}
void AbstractMethod::parseAdd(MethodVariantBase* v) {
// see if there is already an committed variant matching this signature
// in this case it must be inherited
const char* sig = v->getAbstractSignature();
if (vlist.find(sig) != vlist.end())
return;
//printd(5, "AbstractMethod::parseAdd(v: %p) this: %p (%s) new\n", v, this, sig);
// already referenced for "normal" insertion, ref again for abstract method insertion
v->ref();
pending_vlist.insert(vmap_t::value_type(sig, v));
}
void AbstractMethod::parseOverride(MethodVariantBase* v) {
// see if there is already an committed variant matching this signature
// in this case it must be inherited
const char* sig = v->getAbstractSignature();
vmap_t::iterator vi = vlist.find(sig);
if (vi != vlist.end()) {
pending_save.insert(vmap_t::value_type(sig, vi->second));
// move from vlist to pending_save
vlist.erase(vi);
// if override is true, then we know we have a variant in a base class, so we can do nothing here
return;
}
}
void AbstractMethod::add(MethodVariantBase* v) {
// see if there is already an committed variant matching this signature
// in this case it must be inherited
const char* sig = v->getAbstractSignature();
if (vlist.find(sig) != vlist.end())
return;
// already referenced for "normal" insertion, ref again for abstract method insertion
v->ref();
vlist.insert(vmap_t::value_type(sig, v));
//printd(5, "AbstractMethod::add() adding xxx::xxx(%s)\n", sig);
}
void AbstractMethod::override(MethodVariantBase* v) {
// see if there is already an committed variant matching this signature
// in this case it must be inherited
const char* sig = v->getAbstractSignature();
vmap_t::iterator vi = vlist.find(sig);
if (vi != vlist.end()) {
vi->second->deref();
vlist.erase(vi);
}
}
void AbstractMethod::checkAbstract(const char* cname, const char* mname, vmap_t& vlist, QoreStringNode*& desc) {
//printd(5, "AbstractMethod::checkAbstract() checking %s::%s() vlist: %d\n", cname, mname, !vlist.empty());
if (!vlist.empty()) {
if (!desc)
desc = new QoreStringNodeMaker("class '%s' cannot be instantiated because it has the following unimplemented abstract variants:", cname);
for (vmap_t::const_iterator vi = vlist.begin(), ve = vlist.end(); vi != ve; ++vi) {
MethodVariantBase* v = vi->second;
desc->sprintf("\n * abstract %s %s::%s(%s);", v->getReturnTypeInfo()->getName(), cname, mname, v->getSignature()->getSignatureText());
}
}
}
// try to find match non-abstract variants in base classes (allows concrete variants to be inherited from another parent class)
void AbstractMethodMap::parseInit(qore_class_private& qc, BCList* scl) {
if (!scl)
return;
//printd(5, "AbstractMethodMap::parseInit() this: %p cname: %s scl: %p ae: %d\n", this, qc.name.c_str(), scl, empty());
for (amap_t::iterator i = begin(), e = end(); i != e; ++i) {
for (vmap_t::iterator vi = i->second->vlist.begin(), ve = i->second->vlist.end(); vi != ve;) {
// if there is a matching non-abstract variant in any parent class, then move the variant from vlist to pending_save
MethodVariantBase* v = scl->matchNonAbstractVariant(i->first, vi->second);
if (v) {
const char* sig = vi->second->getAbstractSignature();
i->second->pending_save.insert(vmap_t::value_type(sig, vi->second));
vmap_t::iterator ti = vi++;
i->second->vlist.erase(ti);
// replace abstract variant
QoreMethod* m = qc.parseFindLocalMethod(i->first);
if (!m) {
m = new QoreMethod(qc.cls, new NormalUserMethod(qc.cls, i->first.c_str()), false);
qc.hm[m->getName()] = m;
}
m->getFunction()->replaceAbstractVariant(v);
continue;
}
++vi;
}
//printd(5, "AbstractMethodMap::parseInit() this: %p %s::%s() vle: %d\n", this, qc.name.c_str(), i->first.c_str(), i->second->vlist.empty());
for (vmap_t::iterator vi = i->second->pending_vlist.begin(), ve = i->second->pending_vlist.end(); vi != ve;) {
// if there is a matching non-abstract variant in any parent class, then remove the variant from pending_vlist
//printd(5, "AbstractMethodMap::parseInit() this: %p checking abstract %s::%s(%s): %p\n", this, qc.name.c_str(), i->first.c_str(), vi->second->getAbstractSignature(), vi->second);
MethodVariantBase* v = scl->matchNonAbstractVariant(i->first, vi->second);
if (v) {
//printd(5, "AbstractMethodMap::parseInit() this: %p %s::%s() FOUND v: %p (%s)\n", this, qc.name.c_str(), i->first.c_str(), v, v->getAbstractSignature());
vmap_t::iterator ti = vi++;
ti->second->deref();
i->second->pending_vlist.erase(ti);
// replace abstract variant
QoreMethod* m = qc.parseFindLocalMethod(i->first);
//printd(5, "AbstractMethodMap::parseInit() this: %p %s::%s() FOUND v: %p m: %p am: %p\n", this, qc.name.c_str(), i->first.c_str(), v, m, i->second);
if (!m) {
m = new QoreMethod(qc.cls, new NormalUserMethod(qc.cls, i->first.c_str()), false);
qc.hm[m->getName()] = m;
}
m->getFunction()->replaceAbstractVariant(v);
continue;
}
++vi;
}
}
}
void AbstractMethodMap::parseAddAbstractVariant(const char* name, MethodVariantBase* f) {
amap_t::iterator i = amap_t::find(name);
if (i == end()) {
AbstractMethod* m = new AbstractMethod;
// already referenced for "normal" insertion, ref again for abstract method insertion
f->ref();
const char* sig = f->getAbstractSignature();
m->pending_vlist.insert(vmap_t::value_type(sig, f));
//printd(5, "AbstractMethodMap::parseAddAbstractVariant(name: '%s', v: %p) this: %p first (%s)\n", name, f, this, sig);
insert(amap_t::value_type(name, m));
return;
}
//printd(5, "AbstractMethodMap::parseAddAbstractVariant(name: '%s', v: %p) this: %p additional\n", name, f, this);
i->second->parseAdd(f);
}
void AbstractMethodMap::parseOverrideAbstractVariant(const char* name, MethodVariantBase* f) {
amap_t::iterator i = amap_t::find(name);
if (i == end())
return;
i->second->parseOverride(f);
}
void AbstractMethodMap::addAbstractVariant(const char* name, MethodVariantBase* f) {
amap_t::iterator i = amap_t::find(name);
if (i == end()) {
AbstractMethod* m = new AbstractMethod;
// already referenced for "normal" insertion, ref again for abstract method insertion
f->ref();
m->vlist.insert(vmap_t::value_type(f->getAbstractSignature(), f));
//printd(5, "AbstractMethodMap::addAbstractVariant(name: xxx::%s asig: %s, v: %p) this: %p (new)\n", name, f->getAbstractSignature(), f, this);
insert(amap_t::value_type(name, m));
return;
}
//printd(5, "AbstractMethodMap::addAbstractVariant(name: xxx::%s asig: %s, v: %p) this: %p\n", name, f->getAbstractSignature(), f, this);
i->second->add(f);
}
void AbstractMethodMap::overrideAbstractVariant(const char* name, MethodVariantBase* f) {
amap_t::iterator i = amap_t::find(name);
if (i == end())
return;
i->second->override(f);
if (i->second->empty()) {
delete i->second;
erase(i);
}
}
DLLLOCAL QoreStringNode* AbstractMethodMap::checkAbstract(const char* name) const {
if (empty())
return 0;
QoreStringNode* desc = 0;
for (amap_t::const_iterator i = begin(), e = end(); i != e; ++i) {
AbstractMethod::checkAbstract(name, i->first.c_str(), i->second->vlist, desc);
AbstractMethod::checkAbstract(name, i->first.c_str(), i->second->pending_vlist, desc);
}
//printd(5, "AbstractMethodMap::parseCheckAbstractNew() class: %s desc: %p (%s)\n", name, desc, desc ? desc->getBuffer() : "n/a");
return desc;
}
int AbstractMethodMap::runtimeCheckRunClass(const char* name, ExceptionSink* xsink) const {
QoreStringNode* desc = checkAbstract(name);
if (desc) {
xsink->raiseException("ABSTRACT-CLASS-ERROR", desc);
return -1;
}
return 0;
}
// we check if there are any abstract method variants still in the committed lists
void AbstractMethodMap::parseCheckAbstractNew(const char* name) const {
QoreStringNode* desc = checkAbstract(name);
if (desc)
parseException("ABSTRACT-CLASS-ERROR", desc);
}
// FIXME: check private method variant access at runtime
struct SelfLocalVarParseHelper {
QoreProgramLocation loc;
DLLLOCAL SelfLocalVarParseHelper(LocalVar* selfid) { push_local_var(selfid, loc); }
DLLLOCAL ~SelfLocalVarParseHelper() { pop_local_var(); }
};
void raiseNonExistentMethodCallWarning(const QoreClass* qc, const char* method) {
qore_program_private::makeParseWarning(getProgram(), QP_WARN_NONEXISTENT_METHOD_CALL, "NON-EXISTENT-METHOD-CALL", "call to non-existent method '%s::%s()'; this call will be evaluated at run-time, so if the method is called on an object of a subclass that implements this method, then it could be a valid call, however in any other case it will result in a run-time exception. To avoid seeing this warning, use the cast<> operator (note that if the cast is invalid at run-time, a run-time exception will be raised) or turn off the warning by using '%%disable-warning non-existent-method-call' in your code", qc->getName(), method);
}
class VRMutexHelper {
private:
VRMutex* m;
public:
DLLLOCAL VRMutexHelper(VRMutex* n_m, ExceptionSink* xsink) : m(n_m) {
if (m && m->enter(xsink))
m = 0;
}
DLLLOCAL ~VRMutexHelper() {
if (m)
m->exit();
}
DLLLOCAL operator bool() const { return m != 0; }
};
qore_class_private::qore_class_private(QoreClass* n_cls, const char* nme, int64 dom, QoreTypeInfo* n_typeInfo)
: cls(n_cls),
ns(0),
scl(0),
pend_pub_const(this), // pending public constants
pend_priv_const(this), // pending private constants
pub_const(this), // committed public constants
priv_const(this), // committed private constants
system_constructor(0),
constructor(0),
destructor(0),
copyMethod(0),
methodGate(0),
memberGate(0),
deleteBlocker(0),
memberNotification(0),
classID(classIDSeq.next()),
methodID(classID),
sys(false),
initialized(false),
parse_init_called(false),
parse_init_partial_called(false),
has_delete_blocker(false),
has_public_memdecl(false),
pending_has_public_memdecl(false),
owns_typeinfo(n_typeInfo ? false : true),
resolve_copy_done(false),
has_new_user_changes(false),
has_sig_changes(false),
owns_ornothingtypeinfo(false),
pub(false),
final(false),
inject(false),
domain(dom),
num_methods(0),
num_user_methods(0),
num_static_methods(0),
num_static_user_methods(0),
typeInfo(n_typeInfo ? n_typeInfo : new QoreTypeInfo(cls)),
orNothingTypeInfo(0),
selfid("self", typeInfo),
ptr(0),
new_copy(0),
spgm(0) {
assert(methodID == classID);
if (nme)
name = nme;
else {
name = parse_pop_name();
}
printd(5, "qore_class_private::qore_class_private() this: %p creating '%s' ID:%d cls: %p pub: %d\n", this, name.c_str(), classID, cls, pub);
}
// only called while the parse lock for the QoreProgram owning "old" is held
qore_class_private::qore_class_private(const qore_class_private& old, QoreClass* n_cls)
: name(old.name),
cls(n_cls),
ns(0),
scl(0), // parent class list must be copied after new_copy set in old
ahm(old.ahm),
pend_pub_const(this), // pending public constants
pend_priv_const(this), // pending private constants
pub_const(old.pub_const, 0, this), // committed public constants
priv_const(old.priv_const, 0, this), // committed private constants
system_constructor(old.system_constructor ? old.system_constructor->copy(cls) : 0),
constructor(0), // method pointers set below when methods are copied
destructor(0),
copyMethod(0),
methodGate(0),
memberGate(0),
deleteBlocker(old.deleteBlocker ? old.deleteBlocker->copy(cls) : 0),
memberNotification(0),
classID(old.classID),
methodID(old.methodID),
sys(old.sys),
initialized(true),
parse_init_called(false),
parse_init_partial_called(false),
has_public_memdecl(old.has_public_memdecl),
pending_has_public_memdecl(false),
owns_typeinfo(false),
resolve_copy_done(false),
has_new_user_changes(false),
has_sig_changes(false),
owns_ornothingtypeinfo(false),
pub(false), // the public flag must be explicitly set if necessary after this constructor
final(old.final),
inject(old.inject),
domain(old.domain),
num_methods(old.num_methods),
num_user_methods(old.num_user_methods),
num_static_methods(old.num_static_methods),
num_static_user_methods(old.num_static_user_methods),
typeInfo(old.typeInfo),
orNothingTypeInfo(old.orNothingTypeInfo),
selfid(old.selfid),
hash(old.hash),
ptr(old.ptr),
new_copy(0),
spgm(old.spgm ? old.spgm->programRefSelf() : 0) {
QORE_TRACE("qore_class_private::qore_class_private(const qore_class_private& old)");
printd(5, "qore_class_private::qore_class_private() this: %p creating copy of '%s' ID:%d cls: %p old: %p\n", this, name.c_str(), classID, cls, old.cls);
if (!old.initialized)
const_cast<qore_class_private &>(old).initialize();
// must set after old class has been initialized
has_delete_blocker = old.has_delete_blocker;
// set pointer to new copy
old.new_copy = cls;
// copy parent class list, if any, after new_copy is set in old
scl = old.scl ? new BCList(*old.scl) : 0;
printd(5, "qore_class_private::qore_class_private() old name: %s (%p) new name: %s (%p)\n", old.name.c_str(), old.name.c_str(), name.c_str(), name.c_str());
// copy methods and maintain method pointers
for (hm_method_t::const_iterator i = old.hm.begin(), e = old.hm.end(); i != e; ++i) {
QoreMethod* nf = i->second->copy(cls);
hm[nf->getName()] = nf;
if (i->second == old.constructor)
constructor = nf;
else if (i->second == old.destructor)
destructor = nf;
else if (i->second == old.copyMethod)
copyMethod = nf;
else if (i->second == old.methodGate)
methodGate = nf;
else if (i->second == old.memberGate)
memberGate = nf;
else if (i->second == old.memberNotification)
memberNotification = nf;
}
// copy static methods
for (hm_method_t::const_iterator i = old.shm.begin(), e = old.shm.end(); i != e; ++i) {
QoreMethod* nf = i->second->copy(cls);
shm[nf->getName()] = nf;
}
// copy member list
for (member_map_t::const_iterator i = old.members.begin(), e = old.members.end(); i != e; ++i)
members[strdup(i->first)] = i->second->copy(this);
// copy static var list
for (var_map_t::const_iterator i = old.vars.begin(), e = old.vars.end(); i != e; ++i)
vars[strdup(i->first)] = i->second->copy();
}
qore_class_private::~qore_class_private() {
printd(5, "qore_class_private::~qore_class_private() this: %p %s\n", this, name.c_str());
assert(vars.empty());
assert(!spgm);
if (!pending_vars.empty())
pending_vars.del();
// delete normal methods
for (hm_method_t::iterator i = hm.begin(), e = hm.end(); i != e; ++i) {
//printd(5, "QoreClass::~QoreClass() deleting method %p %s::%s()\n", m, name, m->getName());
delete i->second;
}
// delete static methods
for (hm_method_t::iterator i = shm.begin(), e = shm.end(); i != e; ++i) {
//printd(5, "QoreClass::~QoreClass() deleting static method %p %s::%s()\n", m, name, m->getName());
delete i->second;
}
delete scl;
delete system_constructor;
if (owns_typeinfo)
delete typeInfo;
if (owns_ornothingtypeinfo)
delete orNothingTypeInfo;
}
void qore_class_private::initialize() {
//printd(5, "qore_class_private::initialize() this: %p '%s' initialized: %d scl: %p\n", this, name.c_str(), initialized, scl);
if (initialized)
return;
qcp_set_t qcp_set;
initializeIntern(qcp_set);
}
// process signature entries for base classes
static void do_sig(QoreString& csig, BCNode& n) {
qore_class_private* qc = qore_class_private::get(*n.sclass);
csig.sprintf("inherits %s %s ", n.isPrivate() ? "priv" : "pub", qc->name.c_str());
SignatureHash& h = qc->pend_hash ? qc->pend_hash : qc->hash;
if (h) {
csig.concat('[');
h.toString(csig);
csig.concat("]\n");
}
else
csig.sprintf("{%d}\n", qc->classID);
}
// process signature entries for class members
static void do_sig(QoreString& csig, member_map_t::iterator i) {
if (i->second)
csig.sprintf("%s mem %s %s %s\n", privpub(i->second->priv), i->second->getTypeInfo()->getName(), i->first, get_type_name(i->second->exp));
else
csig.sprintf("%s mem %s\n", privpub(i->second->priv), i->first);
}
// process signature entries for class static vars
static void do_sig(QoreString& csig, var_map_t::iterator i) {
if (i->second)
csig.sprintf("%s var %s %s %s\n", privpub(i->second->priv), i->second->getTypeInfo()->getName(), i->first, get_type_name(i->second->exp));
else
csig.sprintf("%s var %s\n", privpub(i->second->priv), i->first);
}
// process signature entries for class constants
static void do_sig(QoreString& csig, ConstantList& clist, const char* prot) {
ConstantListIterator cli(clist);
while (cli.next())
csig.sprintf("%s const %s %s\n", prot, cli.getName().c_str(), get_type_name(cli.getValue()));
}
int qore_class_private::initializeIntern(qcp_set_t& qcp_set) {
//printd(5, "QoreClass::initializeIntern() this: %p %s class: %p scl: %p initialized: %d\n", this, name.c_str(), cls, scl, initialized);
if (initialized)
return 0;
initialized = true;
assert(!name.empty());
//printd(5, "QoreClass::initializeIntern() %s class: %p scl: %p\n", name.c_str(), cls, scl);
// initialize static vars
if (scl) {
bool hdb = has_delete_blocker;
int rc = scl->initialize(cls, hdb, qcp_set);
has_delete_blocker = hdb;
if (rc)
return -1;
}
QoreParseClassHelper qpch(cls);
// first resolve types in pending variants in all method signatures (incl. return types)
// since abstract method functions are copied by reference from the normal list; this resolves all pending
// method function signatures as well
for (hm_method_t::iterator i = hm.begin(), e = hm.end(); i != e; ++i)
i->second->priv->func->resolvePendingSignatures();
for (hm_method_t::iterator i = shm.begin(), e = shm.end(); i != e; ++i)
i->second->priv->func->resolvePendingSignatures();
QoreProgram* pgm = getProgram();
if (pgm && !sys && (qore_program_private::parseAddDomain(pgm, domain)))
parseException("ILLEGAL-CLASS-DEFINITION", "class '%s' inherits functionality from base classes that is restricted by current parse options", name.c_str());
if (!qcp_set.insert(this).second) {
parse_error("circular reference in class hierarchy, '%s' is an ancestor of itself", name.c_str());
scl->valid = false;
return -1;
}
// signature string - also processed in parseCommit()
QoreString csig;
// initialize parent classes
if (scl) {
// merge direct base class abstract method lists to ourselves
for (BCList::iterator i = scl->begin(), e = scl->end(); i != e; ++i) {
if ((*i)->sclass) {
if (has_sig_changes)
do_sig(csig, **i);
// called during class initialization to copy committed abstract variants to our variant lists
AbstractMethodMap& mm = (*i)->sclass->priv->ahm;
//printd(5, "qore_class_private::initializeIntern() this: %p '%s' parent: %p '%s' mm empty: %d\n", this, name.c_str(), (*i)->sclass, (*i)->sclass->getName(), (int)mm.empty());
for (amap_t::iterator j = mm.begin(), e = mm.end(); j != e; ++j) {
// skip if vlist is empty
if (j->second->vlist.empty() && j->second->pending_vlist.empty()) {
//printd(5, "qore_class_private::initializeIntern() this: %p '%s' skipping %s::%s(): vlist empty (pending_vlist empty: %d)\n", this, name.c_str(), (*i)->sclass->getName(), j->first.c_str(), (int)j->second->pending_vlist.empty());
continue;
}
amap_t::iterator vi = ahm.find(j->first);
if (vi != ahm.end()) {
vi->second->parseMergeBase(*(j->second), true);
continue;
}
// now we import the abstract method to our class
AbstractMethod* m = new AbstractMethod;
// see if there are pending normal variants...
hm_method_t::iterator mi = hm.find(j->first);
// merge committed parent abstract variants with any pending local variants
m->parseMergeBase((*j->second), mi == hm.end() ? 0 : mi->second->getFunction(), true);
//if (m->vlist.empty())
//if (m->vlist.empty() && m->pending_vlist.empty())
if (m->empty())
delete m;
else {
ahm.insert(amap_t::value_type(j->first, m));
//printd(5, "qore_class_private::initializeIntern() this: %p '%s' insert abstract method variant %s::%s()\n", this, name.c_str(), (*i)->sclass->getName(), j->first.c_str());
}
}
}
}
}
if (has_sig_changes) {
// add methods to class signature
// pending "normal" (non-static) method variants
for (hm_method_t::iterator i = hm.begin(), e = hm.end(); i != e; ++i)
i->second->priv->func->parsePendingSignatures(csig, 0);
// pending static method variants
for (hm_method_t::iterator i = shm.begin(), e = shm.end(); i != e; ++i)
i->second->priv->func->parsePendingSignatures(csig, "static");
}
if (has_sig_changes) {
// add committed vars to signature first before members
for (var_map_t::iterator i = vars.begin(), e = vars.end(); i != e; ++i) {
do_sig(csig, i);
}
}
{
VariableBlockHelper vbh;
// initialize new static vars
for (var_map_t::iterator i = pending_vars.begin(), e = pending_vars.end(); i != e; ++i) {
if (has_sig_changes)
do_sig(csig, i);
if (i->second)
i->second->parseInit(i->first, true);
}
if ((has_sig_changes && !members.empty()) || !pending_members.empty()) {
SelfLocalVarParseHelper slvph(&selfid);
// add committed members to signature
if (has_sig_changes) {
for (member_map_t::iterator i = members.begin(), e = members.end(); i != e; ++i) {
do_sig(csig, i);
}
}
// initialize new members
for (member_map_t::iterator i = pending_members.begin(), e = pending_members.end(); i != e; ++i) {
if (has_sig_changes)
do_sig(csig, i);
if (i->second)
i->second->parseInit(i->first, true);
// check new members for conflicts in base classes
parseCheckMemberInBaseClasses(i->first, i->second);
}
}
}
if (has_sig_changes) {
// process constants for class signature, private first, then public
do_sig(csig, priv_const, "priv");
do_sig(csig, pend_priv_const, "priv");
do_sig(csig, pub_const, "pub");
do_sig(csig, pend_pub_const, "pub");
}
if (has_sig_changes) {
if (!csig.empty()) {
printd(5, "qore_class_private::initializeIntern() this: %p '%s' sig:\n%s", this, name.c_str(), csig.getBuffer());
pend_hash.update(csig);
}
has_sig_changes = false;
}
else
assert(csig.empty());
return 0;
}
// returns a non-static method if it exists in the local class and has been committed to the class
QoreMethod* qore_class_private::findLocalCommittedMethod(const char* nme) {
QoreMethod* m = parseFindLocalMethod(nme);
return m && !m->priv->func->committedEmpty() ? m : 0;
}
// returns a non-static method if it exists in the local class and has been committed to the class
const QoreMethod* qore_class_private::findLocalCommittedMethod(const char* nme) const {
const QoreMethod* m = parseFindLocalMethod(nme);
return m && !m->priv->func->committedEmpty() ? m : 0;
}
// returns a static method if it exists in the local class and has been committed to the class
QoreMethod* qore_class_private::findLocalCommittedStaticMethod(const char* nme) {
QoreMethod* m = parseFindLocalStaticMethod(nme);
return m && !m->priv->func->committedEmpty() ? m : 0;
}
// returns a static method if it exists in the local class and has been committed to the class
const QoreMethod* qore_class_private::findLocalCommittedStaticMethod(const char* nme) const {
const QoreMethod* m = parseFindLocalStaticMethod(nme);
return m && !m->priv->func->committedEmpty() ? m : 0;
}
int qore_class_private::initMembers(QoreObject& o, bool& need_scan, ExceptionSink* xsink) const {
if (members.empty())
return 0;
// make sure the object context is set before evaluating members
CodeContextHelper cch("constructor", &o, xsink);
SelfInstantiatorHelper sih(&selfid, &o);
for (member_map_t::const_iterator i = members.begin(), e = members.end(); i != e; ++i) {
if (i->second) {
AbstractQoreNode** v = o.getMemberValuePtrForInitialization(i->first);
assert(!*v);
if (i->second->exp) {
ReferenceHolder<AbstractQoreNode> val(i->second->exp->eval(xsink), xsink);
if (*xsink)
return -1;
// check types
AbstractQoreNode* nv = i->second->getTypeInfo()->acceptInputMember(i->first, *val, xsink);
if (*xsink)
return -1;
*v = nv;
val.release();
if (get_container_obj(nv)) {
qore_object_private::incObjectCount(o, 1);
if (!need_scan)
need_scan = true;
}
//printd(5, "qore_class_private::initMembers() '%s' obj: %d\n", i->first, get_container_obj(nv));
}
#ifdef QORE_ENFORCE_DEFAULT_LVALUE
else
*v = i->second->getTypeInfo()->getDefaultValue();
#endif
}
}
return 0;
}
void qore_class_private::execBaseClassConstructor(QoreObject* self, BCEAList* bceal, ExceptionSink* xsink) const {
// if there is no constructor, execute the superclass constructors directly
if (!constructor){
if (scl) // execute base class constructors if any
scl->execConstructors(self, bceal, xsink);
return;
}
// no lock is sent with constructor, because no variable has been assigned yet
bool already_executed;
const AbstractQoreFunctionVariant* variant;
QoreListNode* args = bceal->findArgs(cls->getID(), &already_executed, variant);
if (!already_executed) {
constructor->priv->evalConstructor(variant, self, args, bceal, xsink);
}
}
QoreObject* qore_class_private::execConstructor(const AbstractQoreFunctionVariant* variant, const QoreListNode* args, ExceptionSink* xsink) const {
#ifdef DEBUG
// instantiation checks have to be made at parse time
for (amap_t::const_iterator i = ahm.begin(), e = ahm.end(); i != e; ++i) {
printd(0, "qore_class_private::execConstructor() %s::constructor() abstract error '%s':\n", name.c_str(), i->first.c_str());
vmap_t& v = i->second->vlist;
for (vmap_t::const_iterator vi = v.begin(), ve = v.end(); vi != ve; ++vi) {
printd(0, " + vlist: %s\n", vi->first);
}
v = i->second->pending_vlist;
for (vmap_t::const_iterator vi = v.begin(), ve = v.end(); vi != ve; ++vi) {
printd(0, " + pending_vlist: %s\n", vi->first);
}
v = i->second->pending_save;
for (vmap_t::const_iterator vi = v.begin(), ve = v.end(); vi != ve; ++vi) {
printd(0, " + pending_save: %s\n", vi->first);
}
}
assert(ahm.empty());
#endif
// create new object
QoreObject* self = new QoreObject(cls, getProgram());
ReferenceHolder<BCEAList> bceal(scl ? new BCEAList : 0, xsink);
printd(5, "qore_class_private::execConstructor() class: %p %s::constructor() o: %p variant: %p\n", cls, name.c_str(), self, variant);
// if we made at least one assignment, then scan the object for recursive references after all assignments
bool need_scan = false;
// instantiate members first
initMembers(*self, need_scan, xsink);
// scan object for recursive references after all member assignments
if (need_scan) {
LValueHelper lvh(*self, xsink);
}
if (!*xsink) {
// it's possible for constructor = 0 and variant != 0, when a class is instantiated to initialize a constant
// and the matched variant is pending
if (!constructor && !variant) {
if (scl) { // execute superconstructors if any
CODE_CONTEXT_HELPER(CT_BUILTIN, "constructor", self, xsink);
scl->execConstructors(self, *bceal, xsink);
}
}
else {
if (!constructor) {
hm_method_t::const_iterator i = hm.find("constructor");
assert(i != hm.end());
i->second->priv->evalConstructor(variant, self, args, *bceal, xsink);
}
else