Skip to content

Commit 545c243

Browse files
authored
Merge pull request #70446 from kubamracek/embedded-string
[embedded] Port Swift.String to embedded Swift
2 parents 4f83c09 + e68195b commit 545c243

40 files changed

+535
-304
lines changed

stdlib/public/core/ArrayShared.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ func _allocateUninitializedArray<Element>(_ builtinCount: Builtin.Word)
4141
if count > 0 {
4242
// Doing the actual buffer allocation outside of the array.uninitialized
4343
// semantics function enables stack propagation of the buffer.
44-
let storageType: _ContiguousArrayStorage<Element>.Type
4544
#if !$Embedded
46-
storageType = getContiguousArrayStorageType(for: Element.self)
45+
let bufferObject = Builtin.allocWithTailElems_1(
46+
getContiguousArrayStorageType(for: Element.self), builtinCount, Element.self)
4747
#else
48-
storageType = _ContiguousArrayStorage<Element>.self
49-
#endif
5048
let bufferObject = Builtin.allocWithTailElems_1(
51-
storageType, builtinCount, Element.self)
49+
_ContiguousArrayStorage<Element>.self, builtinCount, Element.self)
50+
#endif
5251

5352
let (array, ptr) = Array<Element>._adoptStorage(bufferObject, count: count)
5453
return (array, ptr._rawValue)
@@ -122,7 +121,11 @@ extension Collection {
122121
} else {
123122
result += ", "
124123
}
124+
#if !$Embedded
125125
debugPrint(item, terminator: "", to: &result)
126+
#else
127+
"(cannot print value in embedded Swift)".write(to: &result)
128+
#endif
126129
}
127130
result += type != nil ? "])" : "]"
128131
return result

stdlib/public/core/Assert.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,10 @@ internal func _internalInvariant_5_1(
413413
// FIXME: The below won't run the assert on 5.1 stdlib if testing on older
414414
// OSes, which means that testing may not test the assertion. We need a real
415415
// solution to this.
416+
#if !$Embedded
416417
guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) //SwiftStdlib 5.1
417418
else { return }
419+
#endif
418420
_internalInvariant(condition(), message, file: file, line: line)
419421
#endif
420422
}
@@ -427,7 +429,6 @@ internal func _internalInvariant_5_1(
427429
/// **and** the current executable was built with a Swift Standard Library
428430
/// version equal to or greater than the supplied version.
429431
@_transparent
430-
@_unavailableInEmbedded
431432
internal func _precondition(
432433
ifLinkedOnOrAfter version: _SwiftStdlibVersion,
433434
_ condition: @autoclosure () -> Bool,
@@ -443,17 +444,22 @@ internal func _precondition(
443444
// unusual configuration.
444445
if _isDebugAssertConfiguration() {
445446
if _slowPath(!condition()) {
447+
#if !$Embedded
446448
guard _isExecutableLinkedOnOrAfter(version) else { return }
449+
#endif
447450
_assertionFailure("Fatal error", message, file: file, line: line,
448451
flags: _fatalErrorFlags())
449452
}
450453
} else if _isReleaseAssertConfiguration() {
454+
#if !$Embedded
451455
let error = (!condition() && _isExecutableLinkedOnOrAfter(version))
456+
#else
457+
let error = !condition()
458+
#endif
452459
Builtin.condfail_message(error._value, message.unsafeRawPointer)
453460
}
454461
}
455462

456-
457463
@usableFromInline @_transparent
458464
internal func _internalInvariantFailure(
459465
_ message: StaticString = StaticString(),

stdlib/public/core/Builtin.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,9 @@ func _isUnique_native<T>(_ object: inout T) -> Bool {
731731
_internalInvariant(
732732
(_bitPattern(Builtin.reinterpretCast(object)) & _objectPointerSpareBits)
733733
== 0)
734-
#endif
735734
_internalInvariant(_usesNativeSwiftReferenceCounting(
736735
type(of: Builtin.reinterpretCast(object) as AnyObject)))
736+
#endif
737737
return Bool(Builtin.isUnique_native(&object))
738738
}
739739

@@ -1093,7 +1093,6 @@ func __abi_openExistential<ExistentialType, ContainedType, ResultType>(
10931093
/// in the function containing the call to this SPI.
10941094
@_transparent
10951095
@_alwaysEmitIntoClient
1096-
@_unavailableInEmbedded
10971096
public // @SPI(OSLog)
10981097
func _getGlobalStringTablePointer(_ constant: String) -> UnsafePointer<CChar> {
10991098
return UnsafePointer<CChar>(Builtin.globalStringTablePointer(constant));

stdlib/public/core/CMakeLists.txt

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ split_embedded_sources(
3434
EMBEDDED ArrayBody.swift
3535
EMBEDDED ArrayBuffer.swift
3636
EMBEDDED ArrayBufferProtocol.swift
37-
NORMAL ArrayCast.swift
37+
EMBEDDED ArrayCast.swift
3838
EMBEDDED Array.swift
3939
EMBEDDED ArrayShared.swift
4040
EMBEDDED ArraySlice.swift
4141
EMBEDDED ArrayType.swift
42-
NORMAL ASCII.swift
42+
EMBEDDED ASCII.swift
4343
EMBEDDED Assert.swift
4444
EMBEDDED AssertCommon.swift
4545
EMBEDDED BidirectionalCollection.swift
@@ -50,7 +50,7 @@ split_embedded_sources(
5050
NORMAL BridgingBuffer.swift
5151
EMBEDDED Builtin.swift
5252
EMBEDDED BuiltinMath.swift
53-
NORMAL Character.swift
53+
EMBEDDED Character.swift
5454
NORMAL CocoaArray.swift
5555
NORMAL Codable.swift
5656
EMBEDDED Collection.swift
@@ -59,10 +59,10 @@ split_embedded_sources(
5959
EMBEDDED CompilerProtocols.swift
6060
EMBEDDED Sendable.swift
6161
EMBEDDED ContiguousArray.swift
62-
NORMAL ContiguouslyStored.swift
62+
EMBEDDED ContiguouslyStored.swift
6363
EMBEDDED ClosedRange.swift
6464
EMBEDDED ContiguousArrayBuffer.swift
65-
NORMAL CString.swift
65+
EMBEDDED CString.swift
6666
EMBEDDED CTypes.swift
6767
NORMAL DebuggerSupport.swift
6868
EMBEDDED Dictionary.swift
@@ -93,7 +93,7 @@ split_embedded_sources(
9393
EMBEDDED HashTable.swift
9494
EMBEDDED Identifiable.swift
9595
EMBEDDED Indices.swift
96-
NORMAL InputStream.swift
96+
EMBEDDED InputStream.swift
9797
NORMAL IntegerParsing.swift
9898
EMBEDDED Integers.swift
9999
NORMAL Join.swift
@@ -114,12 +114,12 @@ split_embedded_sources(
114114
EMBEDDED NativeDictionary.swift
115115
EMBEDDED NativeSet.swift
116116
NORMAL NewtypeWrapper.swift
117-
NORMAL NFC.swift
118-
NORMAL NFD.swift
117+
EMBEDDED NFC.swift
118+
EMBEDDED NFD.swift
119119
EMBEDDED ObjectIdentifier.swift
120120
EMBEDDED Optional.swift
121121
EMBEDDED OptionSet.swift
122-
NORMAL OutputStream.swift
122+
EMBEDDED OutputStream.swift
123123
EMBEDDED Pointer.swift
124124
EMBEDDED Policy.swift
125125
NORMAL PrefixWhile.swift
@@ -153,68 +153,68 @@ split_embedded_sources(
153153
EMBEDDED ShadowProtocols.swift
154154
NORMAL Shims.swift
155155
EMBEDDED Slice.swift
156-
NORMAL SmallString.swift
156+
EMBEDDED SmallString.swift
157157
EMBEDDED Sort.swift
158158
EMBEDDED StaticString.swift
159-
NORMAL StaticPrint.swift
159+
EMBEDDED StaticPrint.swift
160160
EMBEDDED Stride.swift
161-
NORMAL StringHashable.swift # ORDER DEPENDENCY: Must precede String.swift
162-
NORMAL String.swift
163-
NORMAL StringBreadcrumbs.swift
164-
NORMAL StringBridge.swift
165-
NORMAL StringCharacterView.swift
166-
NORMAL StringComparable.swift
167-
NORMAL StringComparison.swift
168-
NORMAL StringCreate.swift
169-
NORMAL StringGuts.swift
170-
NORMAL StringGutsSlice.swift
171-
NORMAL StringGutsRangeReplaceable.swift
172-
NORMAL StringObject.swift
173-
NORMAL StringProtocol.swift
174-
NORMAL StringIndex.swift
175-
NORMAL StringIndexConversions.swift
176-
NORMAL StringIndexValidation.swift
177-
NORMAL StringInterpolation.swift
178-
NORMAL StringLegacy.swift
179-
NORMAL StringNormalization.swift
180-
NORMAL StringRangeReplaceableCollection.swift
181-
NORMAL StringStorage.swift
182-
NORMAL StringStorageBridge.swift
161+
EMBEDDED StringHashable.swift # ORDER DEPENDENCY: Must precede String.swift
162+
EMBEDDED String.swift
163+
EMBEDDED StringBreadcrumbs.swift
164+
EMBEDDED StringBridge.swift
165+
EMBEDDED StringCharacterView.swift
166+
EMBEDDED StringComparable.swift
167+
EMBEDDED StringComparison.swift
168+
EMBEDDED StringCreate.swift
169+
EMBEDDED StringGuts.swift
170+
EMBEDDED StringGutsSlice.swift
171+
EMBEDDED StringGutsRangeReplaceable.swift
172+
EMBEDDED StringObject.swift
173+
EMBEDDED StringProtocol.swift
174+
EMBEDDED StringIndex.swift
175+
EMBEDDED StringIndexConversions.swift
176+
EMBEDDED StringIndexValidation.swift
177+
EMBEDDED StringInterpolation.swift
178+
EMBEDDED StringLegacy.swift
179+
EMBEDDED StringNormalization.swift
180+
EMBEDDED StringRangeReplaceableCollection.swift
181+
EMBEDDED StringStorage.swift
182+
EMBEDDED StringStorageBridge.swift
183183
NORMAL StringSwitch.swift
184-
NORMAL StringTesting.swift
185-
NORMAL StringUnicodeScalarView.swift
186-
NORMAL StringUTF16View.swift
187-
NORMAL StringUTF8View.swift
188-
NORMAL StringUTF8Validation.swift
189-
NORMAL StringWordBreaking.swift
190-
NORMAL Substring.swift
184+
EMBEDDED StringTesting.swift
185+
EMBEDDED StringUnicodeScalarView.swift
186+
EMBEDDED StringUTF16View.swift
187+
EMBEDDED StringUTF8View.swift
188+
EMBEDDED StringUTF8Validation.swift
189+
EMBEDDED StringWordBreaking.swift
190+
EMBEDDED Substring.swift
191191
EMBEDDED SwiftNativeNSArray.swift
192192
EMBEDDED TemporaryAllocation.swift
193193
NORMAL ThreadLocalStorage.swift
194-
NORMAL UIntBuffer.swift
195-
NORMAL UnavailableStringAPIs.swift
196-
NORMAL UnicodeData.swift
197-
NORMAL UnicodeEncoding.swift
198-
NORMAL UnicodeBreakProperty.swift
199-
NORMAL UnicodeHelpers.swift
200-
NORMAL UnicodeParser.swift
201-
NORMAL UnicodeScalarProperties.swift
202-
NORMAL CharacterProperties.swift # ORDER DEPENDENCY: UnicodeScalarProperties.swift
203-
NORMAL UnicodeSPI.swift
194+
EMBEDDED UIntBuffer.swift
195+
EMBEDDED UnavailableStringAPIs.swift
196+
EMBEDDED UnicodeData.swift
197+
EMBEDDED UnicodeEncoding.swift
198+
EMBEDDED UnicodeBreakProperty.swift
199+
EMBEDDED UnicodeHelpers.swift
200+
EMBEDDED UnicodeParser.swift
201+
EMBEDDED UnicodeScalarProperties.swift
202+
EMBEDDED CharacterProperties.swift # ORDER DEPENDENCY: UnicodeScalarProperties.swift
203+
EMBEDDED UnicodeSPI.swift
204204
EMBEDDED Unmanaged.swift
205-
NORMAL UnmanagedOpaqueString.swift
206-
NORMAL UnmanagedString.swift
205+
EMBEDDED UnmanagedOpaqueString.swift
206+
EMBEDDED UnmanagedString.swift
207207
EMBEDDED UnsafePointer.swift
208208
EMBEDDED UnsafeRawPointer.swift
209-
NORMAL UTFEncoding.swift
210-
NORMAL UTF8.swift
211-
NORMAL UTF16.swift
212-
NORMAL UTF32.swift
213-
NORMAL Unicode.swift # ORDER DEPENDENCY: must follow new unicode support
214-
NORMAL StringGraphemeBreaking.swift # ORDER DEPENDENCY: Must follow UTF16.swift
215-
NORMAL ValidUTF8Buffer.swift
209+
EMBEDDED UTFEncoding.swift
210+
EMBEDDED UTF8.swift
211+
EMBEDDED UTF16.swift
212+
EMBEDDED UTF32.swift
213+
EMBEDDED Unicode.swift # ORDER DEPENDENCY: must follow new unicode support
214+
EMBEDDED StringGraphemeBreaking.swift # ORDER DEPENDENCY: Must follow UTF16.swift
215+
EMBEDDED ValidUTF8Buffer.swift
216216
EMBEDDED WriteBackMutableSlice.swift
217-
NORMAL MigrationSupport.swift
217+
EMBEDDED MigrationSupport.swift
218218

219219
### "NON-ESSENTIAL" SOURCES, LAYERED ON TOP OF THE "ESSENTIAL" ONES
220220
### -- PLEASE KEEP THIS LIST IN ALPHABETICAL ORDER ###
@@ -234,7 +234,7 @@ split_embedded_sources(
234234
NORMAL StaticBigInt.swift
235235
EMBEDDED UInt128.swift
236236
NORMAL UnfoldSequence.swift
237-
NORMAL UnsafeBufferPointerSlice.swift
237+
EMBEDDED UnsafeBufferPointerSlice.swift
238238
NORMAL VarArgs.swift
239239
EMBEDDED Zip.swift
240240
NORMAL "${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
@@ -420,6 +420,7 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
420420
set(SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT OFF)
421421
set(SWIFT_STDLIB_STABLE_ABI OFF)
422422
set(SWIFT_STDLIB_ENABLE_OBJC_INTEROP OFF)
423+
set(SWIFT_STDLIB_ENABLE_VECTOR_TYPES OFF)
423424

424425
foreach(entry ${EMBEDDED_STDLIB_TARGET_TRIPLES})
425426
string(REGEX REPLACE "[ \t]+" ";" list "${entry}")

stdlib/public/core/CompilerProtocols.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,6 @@ public protocol ExpressibleByStringInterpolation
792792
init(stringInterpolation: StringInterpolation)
793793
}
794794

795-
#if !$Embedded
796795
extension ExpressibleByStringInterpolation
797796
where StringInterpolation == DefaultStringInterpolation {
798797

@@ -815,7 +814,6 @@ extension ExpressibleByStringInterpolation
815814
self.init(stringLiteral: stringInterpolation.make())
816815
}
817816
}
818-
#endif
819817

820818
/// Represents the contents of a string literal with interpolations while it's
821819
/// being built up.

stdlib/public/core/Dictionary.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,9 +1702,15 @@ extension Collection {
17021702
} else {
17031703
result += ", "
17041704
}
1705+
#if !$Embedded
17051706
debugPrint(k, terminator: "", to: &result)
17061707
result += ": "
17071708
debugPrint(v, terminator: "", to: &result)
1709+
#else
1710+
"(cannot print value in embedded Swift)".write(to: &result)
1711+
result += ": "
1712+
"(cannot print value in embedded Swift)".write(to: &result)
1713+
#endif
17081714
}
17091715
result += "]"
17101716
return result

0 commit comments

Comments
 (0)