【WIP】apple/swift - stdlib/public/runtime コードリーディング(全体)

概要

ErrorObject.h

https://cdn-ak.f.st-hatena.com/images/fotolife/m/moapp/20180504/20180504232543.png?1525443977

CFRuntimeBase

struct CFRuntimeBase {
  void *opaque1;
  void *opaque2;
};

CoreFoundation/CFRuntime.hからコピーしてくるための定義とあり

swift-corelibs-foundation/CFRuntime.h at master · apple/swift-corelibs-foundation · GitHub

そこの定義はこうなっている

typedef struct __CFRuntimeBase {
    // This matches the isa and retain count storage in Swift
    uintptr_t _cfisa;
    uint32_t _swift_strong_rc;
    uint32_t _swift_weak_rc;
    // This is for CF's use, and must match _NSCFType layout
    _Atomic(uint64_t) _cfinfoa;
} CFRuntimeBase;

SwiftErrorHeader

struct SwiftErrorHeader {
  // CFError has a CF refcounting header. NSError reserves a word after the
  // 'isa' in order to be layout-compatible.
  CFRuntimeBase base;
  // The NSError part of the object is lazily initialized, so we need atomic
  // semantics.
  std::atomic<CFIndex> code;
  std::atomic<CFStringRef> domain;
  std::atomic<CFDictionaryRef> userInfo;
};

SwiftError

Swift.Errorのboxing用のレイアウト定義

type

ボックスに含まれるSwiftエラー値型

このメンバはnative Swift errorsでのみ使用できる

const Metadata *type;

errorConformance

Error準拠のwitnessテーブル

このメンバはnative Swift errorsでのみ使用できる

const WitnessTable *errorConformance;

hashableBaseType

Hashable準拠を導入する基本型

このメンバはnative Swift errorsでのみ使用でき、遅延初期化されている

直接使用する代わりにgetHashableBaseType()を呼び出す

mutable std::atomic<const Metadata *> hashableBaseType;

hashableConformance

Hashable準拠のwitnessテーブル

このメンバはnative Swift errorsでのみ使用でき、遅延初期化されている

直接使用する代わりにgetHashableConformance()を呼び出す

mutable std::atomic<const hashable_support::HashableWitnessTable *> hashableConformance;

getIndirectValue

二次的な参照がされるボックス参照内に含まれる値へのポインタを取得する

static const OpaqueValue *getIndirectValue(const SwiftError * const *ptr) {
  if ((*ptr)->isPureNSError())
    return reinterpret_cast<const OpaqueValue *>(ptr);
  return (*ptr)->getValue();
}
static OpaqueValue *getIndirectValue(SwiftError * const *ptr) {
  return const_cast<OpaqueValue *>(getIndirectValue(
                                const_cast<const SwiftError * const *>(ptr)));
}

getValue

値へのポインタを取得する

値は固定ヘッダの末尾に割り当てられる

const OpaqueValue *getValue() const {
  assert(!isPureNSError());

  auto baseAddr = reinterpret_cast<uintptr_t>(this + 1);
  unsigned alignMask = type->getValueWitnesses()->getAlignmentMask();
  baseAddr = (baseAddr + alignMask) & ~(uintptr_t)alignMask;
  return reinterpret_cast<const OpaqueValue *>(baseAddr);
}
OpaqueValue *getValue() {
  return const_cast<OpaqueValue*>(
           const_cast<const SwiftError *>(this)->getValue());
}

swift_allocError

TBD

swift_deallocError

TBD

ErrorValueResult

TBD

swift_getErrorValue

TBD

swift_errorRetain

TBD

swift_errorRelease

TBD

swift_willThrow

TBD

swift_errorInMain

TBD

swift_unexpectedError

TBD

_swift_stdlib_bridgeErrorToNSError

TBD

tryDynamicCastNSErrorToValue

TBD

getNSErrorClass

TBD

getNSErrorMetadata

TBD

_swift_lldb_offsetof_SwiftError_typeMetadata

TBD

_swift_lldb_sizeof_SwiftError

TBD