/
class-inl.h
726 lines (571 loc) · 19.5 KB
/
class-inl.h
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_VM_CLASS_INL_H_
#error "class-inl.h should only be included by class.h"
#endif
#include "hphp/runtime/base/runtime-error.h"
#include "hphp/runtime/base/strings.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
inline bool Class::isZombie() const {
return !m_cachedClass.bound();
}
inline bool Class::validate() const {
#ifndef NDEBUG
assertx(m_magic == kMagic);
#endif
assertx(name()->checkSane());
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Class::PropInitVec.
template <bool is_const>
Class::PropInitVec::iterator_impl<is_const>::iterator_impl(
tv_iter_t tv,
bit_iter_t bit
) : m_val(tv), m_bit(bit) {}
template <bool is_const>
bool Class::PropInitVec::iterator_impl<is_const>::operator==(
const Class::PropInitVec::iterator_impl<is_const>& o
) const {
return m_val == o.m_val;
}
template <bool is_const>
bool Class::PropInitVec::iterator_impl<is_const>::operator!=(
const Class::PropInitVec::iterator_impl<is_const>& o
) const {
return !(*this == o);
}
template <bool is_const>
Class::PropInitVec::iterator_impl<is_const>&
Class::PropInitVec::iterator_impl<is_const>::operator++() {
++m_bit;
++m_val;
return *this;
}
template <bool is_const>
Class::PropInitVec::iterator_impl<is_const>
Class::PropInitVec::iterator_impl<is_const>::operator++(int) {
auto const ret = *this;
++(*this);
return ret;
}
template <bool is_const>
Class::PropInitVec::Entry<is_const>
Class::PropInitVec::iterator_impl<is_const>::operator*() const {
return Entry<is_const>{m_val, *m_bit};
}
template <bool is_const>
Class::PropInitVec::Entry<is_const>
Class::PropInitVec::iterator_impl<is_const>::operator->() const {
return *(*this);
}
inline Class::PropInitVec::PropInitVec() : m_data(nullptr),
m_size(0),
m_capacity(0) {}
template <typename T>
inline Class::PropInitVec::Entry<false>
Class::PropInitVec::operator[](T i) {
auto lval = m_data->at(i);
auto deepInit = deepInitBits()[i];
return Entry<false>{lval, deepInit};
}
template <typename T>
inline Class::PropInitVec::Entry<true>
Class::PropInitVec::operator[](T i) const {
auto lval = m_data->at(i);
auto deepInit = deepInitBits()[i];
return Entry<true>{lval, deepInit};
}
inline Class::PropInitVec::iterator Class::PropInitVec::begin() {
return iterator{m_data->iteratorAt(0),
deepInitBits().iteratorAt(0)};
}
inline Class::PropInitVec::iterator Class::PropInitVec::end() {
return iterator{m_data->iteratorAt(m_size),
deepInitBits().iteratorAt(m_size)};
}
inline Class::PropInitVec::const_iterator Class::PropInitVec::cbegin() const {
return const_iterator{
const_cast<const ObjectProps*>(m_data)->iteratorAt(0),
deepInitBits().iteratorAt(0)};
}
inline Class::PropInitVec::const_iterator Class::PropInitVec::cend() const {
return const_iterator{
const_cast<const ObjectProps*>(m_data)->iteratorAt(m_size),
deepInitBits().iteratorAt(m_size)};
}
inline size_t Class::PropInitVec::size() const {
return m_size;
}
inline bool Class::PropInitVec::reqAllocated() const {
return m_capacity < 0;
}
inline const ObjectProps* Class::PropInitVec::data() const {
return m_data;
}
inline BitsetView<false> Class::PropInitVec::deepInitBits() {
auto const cap = m_capacity < 0 ? ~m_capacity : m_capacity;
return BitsetView<false>{reinterpret_cast<unsigned char*>(m_data) +
ObjectProps::sizeFor(cap)};
}
inline BitsetView<true>
Class::PropInitVec::deepInitBits() const {
auto const cap = m_capacity < 0 ? ~m_capacity : m_capacity;
return BitsetView<true>{reinterpret_cast<const unsigned char*>(m_data) +
ObjectProps::sizeFor(cap)};
}
template <bool is_const>
template <typename Dummy, typename>
Class::PropInitVec::Entry<is_const>&
Class::PropInitVec::Entry<is_const>::operator=(TypedValueAux tva) {
tvCopy(tva, val);
deepInit = tva.deepInit();
return *this;
}
template <bool is_const>
Class::PropInitVec::Entry<is_const>::operator TypedValueAux() const {
TypedValueAux tva;
tvCopy(val.tv(), tva);
tva.deepInit() = deepInit;
return tva;
}
///////////////////////////////////////////////////////////////////////////////
// Pre- and post-allocations.
inline LowPtr<Func>* Class::funcVec() const {
return reinterpret_cast<LowPtr<Func>*>(
reinterpret_cast<uintptr_t>(this) -
m_funcVecLen * sizeof(LowPtr<Func>)
);
}
inline void* Class::mallocPtr() const {
return reinterpret_cast<void*>(
reinterpret_cast<uintptr_t>(funcVec()) & ~(alignof(Class) - 1)
);
}
inline const void* Class::mallocEnd() const {
return reinterpret_cast<const char*>(this)
+ Class::classVecOff()
+ classVecLen() * sizeof(*classVec());
}
inline const LowPtr<Class>* Class::classVec() const {
return m_classVec;
}
inline Class::veclen_t Class::classVecLen() const {
return m_classVecLen;
}
///////////////////////////////////////////////////////////////////////////////
// Ancestry.
inline bool Class::classofNonIFace(const Class* cls) const {
assertx(!(cls->attrs() & AttrInterface));
if (m_classVecLen >= cls->m_classVecLen) {
return (m_classVec[cls->m_classVecLen-1] == cls);
}
return false;
}
inline bool Class::classof(const Class* cls) const {
// If `cls' is an interface, we can simply check to see if cls is in
// this->m_interfaces. Otherwise, if `this' is not an interface, the
// classVec check will determine whether it's an instance of cls (including
// the case where this and cls are the same trait). Otherwise, `this' is an
// interface, and `cls' is not, so we need to return false. But the classVec
// check can never return true in that case (cls's classVec contains only
// non-interfaces, while this->classVec is either empty, or contains
// interfaces).
if (UNLIKELY(cls->attrs() & AttrInterface)) {
return this == cls ||
m_interfaces.lookupDefault(cls->m_preClass->name(), nullptr) == cls;
}
return classofNonIFace(cls);
}
inline bool Class::subtypeOf(const Class* cls) const { return classof(cls); }
inline bool Class::ifaceofDirect(const StringData* name) const {
return m_interfaces.contains(name);
}
///////////////////////////////////////////////////////////////////////////////
// Basic info.
inline const StringData* Class::name() const {
return m_preClass->name();
}
inline const PreClass* Class::preClass() const {
return m_preClass.get();
}
inline Class* Class::parent() const {
return m_parent.get();
}
inline StrNR Class::nameStr() const {
return m_preClass->nameStr();
}
inline StrNR Class::parentStr() const {
return m_preClass->parentStr();
}
inline Attr Class::attrs() const {
assertx(Attr(m_attrCopy) == m_preClass->attrs());
return Attr(m_attrCopy);
}
inline bool Class::rtAttribute(RuntimeAttribute a) const {
return m_RTAttrs & a;
}
inline void Class::initRTAttributes(uint8_t a) {
m_RTAttrs |= a;
}
inline bool Class::isUnique() const {
return attrs() & AttrUnique;
}
inline bool Class::isPersistent() const {
return attrs() & AttrPersistent;
}
inline bool Class::isDynamicallyConstructible() const {
return attrs() & AttrDynamicallyConstructible;
}
///////////////////////////////////////////////////////////////////////////////
// Magic methods.
inline const Func* Class::getCtor() const {
return m_ctor;
}
inline const Func* Class::getToString() const {
return m_toString;
}
inline const Func* Class::get86pinit() const {
return m_pinitVec.back();
}
inline const Func* Class::get86sinit() const {
return m_sinitVec.back();
}
inline const Func* Class::get86linit() const {
return m_linitVec.back();
}
///////////////////////////////////////////////////////////////////////////////
// Builtin classes.
inline bool Class::isBuiltin() const {
return attrs() & AttrBuiltin;
}
template <bool Unlocked>
inline BuiltinCtorFunction Class::instanceCtor() const {
return Unlocked ? m_extra->m_instanceCtorUnlocked : m_extra->m_instanceCtor;
}
inline BuiltinDtorFunction Class::instanceDtor() const {
return m_extra->m_instanceDtor;
}
///////////////////////////////////////////////////////////////////////////////
// Object release.
inline ObjReleaseFunc Class::releaseFunc() const {
return m_releaseFunc;
}
inline uint32_t Class::memoSize() const {
return m_memoSize;
}
inline uint8_t Class::sizeIdx() const {
return m_sizeIdx;
}
///////////////////////////////////////////////////////////////////////////////
// Methods.
inline size_t Class::numMethods() const {
return m_methods.size();
}
inline Func* Class::getMethod(Slot idx) const {
auto funcVec = (LowPtr<Func>*)this;
return funcVec[-((int32_t)idx + 1)];
}
inline void Class::setMethod(Slot idx, Func* func) {
auto funcVec = (LowPtr<Func>*)this;
funcVec[-((int32_t)idx + 1)] = func;
}
inline Func* Class::lookupMethod(const StringData* methName) const {
Slot* idx = m_methods.find(methName);
if (!idx) return nullptr;
return getMethod(*idx);
}
///////////////////////////////////////////////////////////////////////////////
// Property metadata.
inline size_t Class::numDeclProperties() const {
return m_declProperties.size();
}
inline size_t Class::numStaticProperties() const {
return m_staticProperties.size();
}
inline uint32_t Class::declPropNumAccessible() const {
return m_declPropNumAccessible;
}
inline folly::Range<const Class::Prop*> Class::declProperties() const {
return m_declProperties.range();
}
inline folly::Range<const Class::SProp*> Class::staticProperties() const {
return m_staticProperties.range();
}
inline Slot Class::lookupDeclProp(const StringData* propName) const {
return m_declProperties.findIndex(propName);
}
inline Slot Class::lookupSProp(const StringData* sPropName) const {
return m_staticProperties.findIndex(sPropName);
}
inline Slot Class::lookupReifiedInitProp() const {
return m_declProperties.findIndex(s_86reified_prop.get());
}
inline bool Class::hasReifiedGenerics() const {
return m_hasReifiedGenerics;
}
inline bool Class::hasReifiedParent() const {
return m_hasReifiedParent;
}
inline RepoAuthType Class::declPropRepoAuthType(Slot index) const {
return m_declProperties[index].repoAuthType;
}
inline RepoAuthType Class::staticPropRepoAuthType(Slot index) const {
return m_staticProperties[index].repoAuthType;
}
inline const TypeConstraint& Class::declPropTypeConstraint(Slot index) const {
return m_declProperties[index].typeConstraint;
}
inline const TypeConstraint& Class::staticPropTypeConstraint(Slot index) const {
return m_staticProperties[index].typeConstraint;
}
inline bool Class::hasDeepInitProps() const {
return m_hasDeepInitProps;
}
inline bool Class::forbidsDynamicProps() const {
return attrs() & AttrForbidDynamicProps;
}
inline bool Class::serialize() const {
if (m_serialized) return false;
m_serialized = true;
return true;
}
inline bool Class::wasSerialized() const {
return m_serialized;
}
///////////////////////////////////////////////////////////////////////////////
// Property initialization.
inline bool Class::needInitialization() const {
return m_needInitialization;
}
inline bool Class::maybeRedefinesPropTypes() const {
return m_maybeRedefsPropTy;
}
inline bool Class::needsPropInitialValueCheck() const {
return m_needsPropInitialCheck;
}
inline const Class::PropInitVec& Class::declPropInit() const {
return m_declPropInit;
}
inline const VMFixedVector<const Func*>& Class::pinitVec() const {
return m_pinitVec;
}
inline rds::Handle Class::checkedPropTypeRedefinesHandle() const {
assertx(m_maybeRedefsPropTy);
m_extra->m_checkedPropTypeRedefs.bind(
rds::Mode::Normal,
rds::LinkName{"PropTypeRedefs", name()}
);
return m_extra->m_checkedPropTypeRedefs.handle();
}
inline rds::Handle Class::checkedPropInitialValuesHandle() const {
assertx(m_needsPropInitialCheck);
m_extra->m_checkedPropInitialValues.bind(
rds::Mode::Normal,
rds::LinkName{"PropInitialValues", name()}
);
return m_extra->m_checkedPropInitialValues.handle();
}
///////////////////////////////////////////////////////////////////////////////
// Property storage.
inline void Class::initPropHandle() const {
m_propDataCache.bind(
rds::Mode::Normal,
rds::LinkName{"PropDataCache", name()}
);
}
inline rds::Handle Class::propHandle() const {
return m_propDataCache.handle();
}
inline rds::Handle Class::sPropInitHandle() const {
return m_sPropCacheInit.handle();
}
inline rds::Handle Class::sPropHandle(Slot index) const {
return sPropLink(index).handle();
}
inline rds::Link<StaticPropData, rds::Mode::NonNormal>
Class::sPropLink(Slot index) const {
assertx(m_sPropCacheInit.bound());
assertx(numStaticProperties() > index);
return m_sPropCache[index];
}
inline rds::Link<bool, rds::Mode::NonLocal> Class::sPropInitLink() const {
return m_sPropCacheInit;
}
///////////////////////////////////////////////////////////////////////////////
// Constants.
inline size_t Class::numConstants() const {
return m_constants.size();
}
inline const Class::Const* Class::constants() const {
return m_constants.accessList();
}
inline bool Class::hasConstant(const StringData* clsCnsName) const {
// m_constants.contains(clsCnsName) returns abstract constants
auto clsCnsInd = m_constants.findIndex(clsCnsName);
return (clsCnsInd != kInvalidSlot) &&
!m_constants[clsCnsInd].isAbstract() &&
!m_constants[clsCnsInd].isType();
}
inline bool Class::hasTypeConstant(const StringData* typeConstName,
bool includeAbs) const {
auto typeConstInd = m_constants.findIndex(typeConstName);
return (typeConstInd != kInvalidSlot) &&
(!m_constants[typeConstInd].isAbstract() || includeAbs) &&
m_constants[typeConstInd].isType();
}
///////////////////////////////////////////////////////////////////////////////
// Interfaces and traits.
inline folly::Range<const ClassPtr*> Class::declInterfaces() const {
return folly::range(m_declInterfaces.begin(),
m_declInterfaces.end());
}
inline const Class::InterfaceMap& Class::allInterfaces() const {
return m_interfaces;
}
inline Slot Class::traitsBeginIdx() const {
return m_extra->m_traitsBeginIdx;
}
inline Slot Class::traitsEndIdx() const {
return m_extra->m_traitsEndIdx;
}
inline const VMCompactVector<ClassPtr>& Class::usedTraitClasses() const {
return m_extra->m_usedTraits;
}
inline const Class::TraitAliasVec& Class::traitAliases() const {
return m_extra->m_traitAliases;
}
inline void Class::addTraitAlias(const PreClass::TraitAliasRule& rule) const {
allocExtraData();
m_extra.raw()->m_traitAliases.push_back(rule.asNamePair());
}
inline const Class::RequirementMap& Class::allRequirements() const {
return m_requirements;
}
///////////////////////////////////////////////////////////////////////////////
// Instance bits.
inline bool Class::checkInstanceBit(unsigned int bit) const {
assertx(bit > 0);
return m_instanceBits[bit];
}
///////////////////////////////////////////////////////////////////////////////
// Throwable initialization.
inline bool Class::needsInitThrowable() const {
return m_needsInitThrowable;
}
///////////////////////////////////////////////////////////////////////////////
// JIT data.
inline rds::Handle Class::classHandle() const {
return m_cachedClass.handle();
}
inline void Class::setClassHandle(rds::Link<LowPtr<Class>,
rds::Mode::NonLocal> link) const {
assertx(!m_cachedClass.bound());
m_cachedClass = link;
}
inline Class* Class::getCached() const {
return m_cachedClass.isInit() ? *m_cachedClass : nullptr;
}
inline void Class::setCached() {
m_cachedClass.initWith(this);
}
///////////////////////////////////////////////////////////////////////////////
// Native data.
inline const Native::NativeDataInfo* Class::getNativeDataInfo() const {
return m_extra->m_nativeDataInfo;
}
///////////////////////////////////////////////////////////////////////////////
// Closure subclasses.
inline bool Class::isScopedClosure() const {
return m_scoped;
}
inline const Class::ScopedClonesMap& Class::scopedClones() const {
return m_extra->m_scopedClones;
}
/////////////////////////////////////////////////////////////////////////////
// Memoization
inline size_t Class::numMemoSlots() const {
return m_extra->m_nextMemoSlot;
}
inline bool Class::hasMemoSlots() const {
return numMemoSlots() > 0;
}
inline std::pair<Slot, bool> Class::memoSlotForFunc(FuncId func) const {
assertx(hasMemoSlots());
auto const it = m_extra->m_memoMappings.find(func);
if (it != m_extra->m_memoMappings.end()) return it->second;
// Each mapping is only stored in the class which defines it, so recurse up to
// the parent. We should only be calling this with functions which have a memo
// slot, so assert if we reach the end without finding a slot.
if (m_parent) return m_parent->memoSlotForFunc(func);
always_assert(false);
}
///////////////////////////////////////////////////////////////////////////////
// Other methods.
inline MaybeDataType Class::enumBaseTy() const {
return m_enumBaseTy;
}
inline EnumValues* Class::getEnumValues() const {
return m_extra->m_enumValues.load(std::memory_order_relaxed);
}
///////////////////////////////////////////////////////////////////////////////
// ExtraData.
inline void Class::allocExtraData() const {
if (!m_extra) {
m_extra = new ExtraData();
}
}
///////////////////////////////////////////////////////////////////////////////
// Non-member functions.
inline Attr classKindAsAttr(ClassKind kind) {
return static_cast<Attr>(kind);
}
inline bool isTrait(const Class* cls) {
return cls->attrs() & AttrTrait;
}
inline bool isEnum(const Class* cls) {
return cls->attrs() & AttrEnum;
}
inline bool isInterface(const Class* cls) {
return cls->attrs() & AttrInterface;
}
inline bool isNormalClass(const Class* cls) {
return !(cls->attrs() & (AttrTrait | AttrInterface | AttrEnum));
}
inline bool isAbstract(const Class* cls) {
return cls->attrs() & AttrAbstract;
}
inline bool classHasPersistentRDS(const Class* cls) {
return cls != nullptr &&
rds::isPersistentHandle(cls->classHandle());
}
inline bool classMayHaveMagicPropMethods(const Class* cls) {
auto constexpr no_overrides =
AttrNoOverrideMagicGet |
AttrNoOverrideMagicSet |
AttrNoOverrideMagicIsset |
AttrNoOverrideMagicUnset;
return (cls->attrs() & no_overrides) != no_overrides;
}
inline const StringData* classToStringHelper(const Class* cls) {
if (RuntimeOption::EvalRaiseClassConversionWarning) {
raise_warning(Strings::CLASS_TO_STRING);
}
return cls->name();
}
///////////////////////////////////////////////////////////////////////////////
}