Skip to content

Commit 9b1f238

Browse files
committed
SE-0139: Bridge all standard number types to NSNumber.
Extend NSNumber bridging to cover not only `Int`, `UInt`, `Double`, and `Bool`, but all of the standard types as well. Extend the `TypePreservingNSNumber` subclass to accommodate all of these types, so that we preserve type identity for `AnyHashable` and dynamic casting of Swift-bridged NSNumbers. If a pure Cocoa NSNumber is cast, just trust that the user knows what they're doing. This XFAILs a couple of serialization tests that attempt to build the Foundation overlay, but which don't properly handle `gyb` files.
1 parent 0375994 commit 9b1f238

14 files changed

+638
-508
lines changed

Diff for: include/swift/AST/ASTContext.h

+5-64
Original file line numberDiff line numberDiff line change
@@ -363,48 +363,14 @@ class ASTContext {
363363
ProtocolDecl *getErrorDecl() const;
364364
CanType getExceptionType() const;
365365

366-
/// Retrieve the declaration of Swift.Bool.
367-
NominalTypeDecl *getBoolDecl() const;
368-
369-
/// Retrieve the declaration of Swift.Int.
370-
NominalTypeDecl *getIntDecl() const;
371-
372-
/// Retrieve the declaration of Swift.UInt.
373-
NominalTypeDecl *getUIntDecl() const;
374-
375-
/// Retrieve the declaration of Swift.Float.
376-
NominalTypeDecl *getFloatDecl() const;
377-
378-
/// Retrieve the declaration of Swift.Double.
379-
NominalTypeDecl *getDoubleDecl() const;
380-
381-
/// Retrieve the declaration of Swift.String.
382-
NominalTypeDecl *getStringDecl() const;
383-
384-
/// Retrieve the declaration of Swift.Array<T>.
385-
NominalTypeDecl *getArrayDecl() const;
386-
387-
/// Retrieve the declaration of Swift.Set<T>.
388-
NominalTypeDecl *getSetDecl() const;
389-
390-
/// Retrieve the declaration of Swift.Sequence<T>.
391-
NominalTypeDecl *getSequenceDecl() const;
392-
393-
/// Retrieve the declaration of Swift.Dictionary<K, V>.
394-
NominalTypeDecl *getDictionaryDecl() const;
395-
396-
/// Retrieve the declaration of Swift.AnyHashable.
397-
NominalTypeDecl *getAnyHashableDecl() const;
366+
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
367+
/** Retrieve the declaration of Swift.NAME. */ \
368+
DECL_CLASS *get##NAME##Decl() const;
369+
#include "swift/AST/KnownStdlibTypes.def"
398370

399371
/// Retrieve the declaration of Swift.Optional or ImplicitlyUnwrappedOptional.
400372
EnumDecl *getOptionalDecl(OptionalTypeKind kind) const;
401373

402-
/// Retrieve the declaration of Swift.Optional<T>.
403-
EnumDecl *getOptionalDecl() const;
404-
405-
/// Retrieve the declaration of Swift.ImplicitlyUnwrappedOptional<T>.
406-
EnumDecl *getImplicitlyUnwrappedOptionalDecl() const;
407-
408374
/// Retrieve the declaration of Swift.Optional<T>.Some.
409375
EnumElementDecl *getOptionalSomeDecl() const;
410376

@@ -420,35 +386,10 @@ class ASTContext {
420386
EnumElementDecl *getOptionalSomeDecl(OptionalTypeKind kind) const;
421387
EnumElementDecl *getOptionalNoneDecl(OptionalTypeKind kind) const;
422388

423-
/// Retrieve the declaration of Swift.OptionSet.
424-
NominalTypeDecl *getOptionSetDecl() const;
425-
426-
/// Retrieve the declaration of Swift.COpaquePointer.
427-
NominalTypeDecl *getOpaquePointerDecl() const;
428-
429-
/// Retrieve the declaration of Swift.UnsafeMutableRawPointer.
430-
NominalTypeDecl *getUnsafeMutableRawPointerDecl() const;
431-
432-
/// Retrieve the declaration of Swift.UnsafeRawPointer.
433-
NominalTypeDecl *getUnsafeRawPointerDecl() const;
434-
435-
/// Retrieve the declaration of Swift.UnsafeMutablePointer<T>.
436-
NominalTypeDecl *getUnsafeMutablePointerDecl() const;
437-
438-
/// Retrieve the declaration of Swift.UnsafePointer<T>.
439-
NominalTypeDecl *getUnsafePointerDecl() const;
440-
441-
/// Retrieve the declaration of Swift.AutoreleasingUnsafeMutablePointer<T>.
442-
NominalTypeDecl *getAutoreleasingUnsafeMutablePointerDecl() const;
443-
444-
/// Retrieve the declaration of Swift.Unmanaged<T>.
445-
NominalTypeDecl *getUnmanagedDecl() const;
446-
447389
/// Retrieve the declaration of the "pointee" property of a pointer type.
448390
VarDecl *getPointerPointeePropertyDecl(PointerTypeKind ptrKind) const;
449391

450-
/// Retrieve the declaration of Swift.Never.
451-
NominalTypeDecl *getNeverDecl() const;
392+
/// Retrieve the type Swift.Never.
452393
CanType getNeverType() const;
453394

454395
/// Retrieve the declaration of Swift.Void.

Diff for: include/swift/AST/KnownStdlibTypes.def

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- KnownStdlibTypes.cpp - Common standard library types -------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This xmacro generates code for common standard library types the compiler
14+
// has special knowledge of.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef KNOWN_STDLIB_TYPE_DECL
19+
/// KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
20+
///
21+
/// The macro is expanded for each known standard library type. NAME is
22+
/// bound to the unqualified name of the type. DECL_CLASS is bound to the
23+
/// Decl subclass it is expected to be an instance of. NUM_GENERIC_PARAMS is
24+
/// bound to the number of generic parameters the type is expected to have.
25+
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
26+
#endif
27+
28+
KNOWN_STDLIB_TYPE_DECL(Bool, NominalTypeDecl, 0)
29+
30+
KNOWN_STDLIB_TYPE_DECL(Int, NominalTypeDecl, 0)
31+
KNOWN_STDLIB_TYPE_DECL(Int64, NominalTypeDecl, 0)
32+
KNOWN_STDLIB_TYPE_DECL(Int32, NominalTypeDecl, 0)
33+
KNOWN_STDLIB_TYPE_DECL(Int16, NominalTypeDecl, 0)
34+
KNOWN_STDLIB_TYPE_DECL(Int8, NominalTypeDecl, 0)
35+
36+
KNOWN_STDLIB_TYPE_DECL(UInt, NominalTypeDecl, 0)
37+
KNOWN_STDLIB_TYPE_DECL(UInt64, NominalTypeDecl, 0)
38+
KNOWN_STDLIB_TYPE_DECL(UInt32, NominalTypeDecl, 0)
39+
KNOWN_STDLIB_TYPE_DECL(UInt16, NominalTypeDecl, 0)
40+
KNOWN_STDLIB_TYPE_DECL(UInt8, NominalTypeDecl, 0)
41+
42+
KNOWN_STDLIB_TYPE_DECL(Float, NominalTypeDecl, 0)
43+
KNOWN_STDLIB_TYPE_DECL(Double, NominalTypeDecl, 0)
44+
45+
KNOWN_STDLIB_TYPE_DECL(String, NominalTypeDecl, 0)
46+
KNOWN_STDLIB_TYPE_DECL(Array, NominalTypeDecl, 1)
47+
KNOWN_STDLIB_TYPE_DECL(Set, NominalTypeDecl, 1)
48+
KNOWN_STDLIB_TYPE_DECL(Sequence, NominalTypeDecl, 1)
49+
KNOWN_STDLIB_TYPE_DECL(Dictionary, NominalTypeDecl, 2)
50+
KNOWN_STDLIB_TYPE_DECL(AnyHashable, NominalTypeDecl, 0)
51+
52+
KNOWN_STDLIB_TYPE_DECL(Optional, EnumDecl, 1)
53+
KNOWN_STDLIB_TYPE_DECL(ImplicitlyUnwrappedOptional, EnumDecl, 1)
54+
55+
KNOWN_STDLIB_TYPE_DECL(OptionSet, NominalTypeDecl, 1)
56+
57+
KNOWN_STDLIB_TYPE_DECL(UnsafeMutableRawPointer, NominalTypeDecl, 0)
58+
KNOWN_STDLIB_TYPE_DECL(UnsafeRawPointer, NominalTypeDecl, 0)
59+
KNOWN_STDLIB_TYPE_DECL(UnsafeMutablePointer, NominalTypeDecl, 1)
60+
KNOWN_STDLIB_TYPE_DECL(UnsafePointer, NominalTypeDecl, 1)
61+
KNOWN_STDLIB_TYPE_DECL(OpaquePointer, NominalTypeDecl, 0)
62+
KNOWN_STDLIB_TYPE_DECL(AutoreleasingUnsafeMutablePointer, NominalTypeDecl, 1)
63+
64+
KNOWN_STDLIB_TYPE_DECL(Unmanaged, NominalTypeDecl, 1)
65+
66+
KNOWN_STDLIB_TYPE_DECL(Never, NominalTypeDecl, 0)
67+
68+
#undef KNOWN_STDLIB_TYPE_DECL

0 commit comments

Comments
 (0)