<commit_before># Swift Type Representation In C++

This document describes in details how Swift types are represented in C++.
It also covers related topics, like debug info representation for Swift types in
C++.

## Type Categories

### Value Types

1) Primitive Swift types like `Int`, `Float` , `OpaquePointer`, `UnsafePointer<int>?` are mapped to primitive C++ types. `int` , `float`, `void *`, int `* _Nullable` .

* Debug info: Does C++ debug info suffices?

2) Non-resilient fixed-layout Swift value type, e.g. `String` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:

```c++
class swift::String {
  ...
  alignas(8) char buffer[24]; // Swift value is stored here.
}
```

* Debug info: ...


3) Resilient (or opaque layout) inline-allocated Swift value type small enough to fit into inline buffer. e.g `URL` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:

```c++
class Foundation::URL {
  ...
  union {
    alignas(8) char buffer[8]; // Swift value is stored here.
    void *resilientPtr;
  };
};
```

* Debug info: ...


4) Resilient (or opaque layout) boxed Swift value , e.g. `SHA256` is mapped to a C++ class that stores the value boxed up on the heap, e.g.:

```c++
class CryptoKit::SHA256 {
  ...
  union {
    alignas(8) char buffer[8]; 
    void *resilientPtr;        // Swift value is stored on the heap pointed by this pointer.
  };
};
```

* Debug info: ...


5) Generic non-resilient fixed-layout Swift value type, e.g. `Array<Int>`, `String?`, is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:

```c++
class swift::Array<swift::Int> {
  ...
  alignas(8) char buffer[8]; // Swift value is stored here.
}
```

* Debug info: ...


6) Generic opaque-layout / resilient / opaque-layout template type params Swift value type, e.g. SHA256`?`, is mapped to a C++ class that stores the value boxed up on the heap, e.g.:

```c++
class swift::Optional<CryptoKit::SHA256> {
  ...
  union {
    alignas(8) char buffer[8]; 
    void *resilientPtr;        // Swift value is stored on the heap pointed by this pointer.
  };
}
```

* Debug info: ...

### Class Types
<commit_msg>[interop][SwiftToCxx] update swift type representation docs with different opaque type layout<commit_after># Swift Type Representation In C++

This document describes in details how Swift types are represented in C++.
It also covers related topics, like debug info representation for Swift types in
C++.

## Type Categories

### Value Types

1) Primitive Swift types like `Int`, `Float` , `OpaquePointer`, `UnsafePointer<int>?` are mapped to primitive C++ types. `int` , `float`, `void *`, `int * _Nullable`.

* Debug info: Does C++ debug info suffices?

2) Non-resilient fixed-layout Swift value type, e.g. `String` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:

```c++
class swift::String {
  ...
  alignas(8) char buffer[24]; // Swift value is stored here.
}
```

* Debug info: ...


3) Resilient (or opaque layout) inline-allocated Swift value type small enough to fit into inline buffer. e.g `URL` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:

```c++
class Foundation::URL {
  ...
  uintptr_t pointer;         // pointer has alignment to compute buffer offset?
  alignas(N) char buffer[M]; // Swift value is stored here.
};
```

concrete examples:

```c++
// representation for buffer aligned at 8:
{/*pointer=*/0x3, ....}; // buffer is alignas(2^3 = 8)

// representation for buffer aligned at 16:
{/*pointer=*/0x4, ....}; // buffer is alignas(2^4 = 16)

// where pointer < 10 for inline stores.
```

* Debug info: ...


4) Resilient (or opaque layout) boxed Swift value , e.g. `SHA256` is mapped to a C++ class that stores the value boxed up on the heap, e.g.:

```c++
class CryptoKit::SHA256 {
  ...
  uintptr_t pointer;         // Swift value is stored on the heap pointed by this pointer.
  alignas(8) char buffer[8];
};
```

* Debug info: ...


5) Generic non-resilient fixed-layout Swift value type, e.g. `Array<Int>`, `String?`, is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:

```c++
class swift::Array<swift::Int> {
  ...
  alignas(8) char buffer[8]; // Swift value is stored here.
}
```

* Debug info: ...


6) Generic opaque-layout / resilient / opaque-layout template type params Swift value type, e.g. `SHA256?`, is mapped to a C++ class that stores the value boxed up on the heap, e.g.:

```c++
class swift::Optional<CryptoKit::SHA256> {
  ...
  uintptr_t pointer;        // Swift value is stored on the heap pointed by this pointer.
  alignas(N) char buffer[M];
}
```

* Debug info: ...

### Class Types

{'fraction_non_alphanumeric': 0.14072494669509594, 'fraction_numerical': 0.014925373134328358, 'mean_word_length': 3.3395004625346902, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6716950', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1457, 'n_words': 619}