Commit Graph

425 Commits

Author SHA1 Message Date
Daniel Rodríguez Troitiño
c02fa2dd45 [android] Add check for __aarch64__ besides __arm__ and __arm64__.
The spelling __arm64__ seems to be an Apple-only thing, which is not
provided when targetting Android, and probably other AArch64 Linuxes.
2020-04-11 17:30:16 -07:00
Stephen Canon
248c554524 Add Float16 to stdlib (#30130)
Add Float16 (IEEE 754 binary16) to the standard library, plus assorted runtime support operations.

Swift Evolution thread: https://forums.swift.org/t/se-0277-float16/33546
2020-04-06 17:57:44 -04:00
Doug Gregor
ab97d50b0f Revert "PrintAsObjC: use header path relative to usr/include when importing non-framework headers"
This reverts commit ebe0a45583.
2020-03-27 21:00:51 -07:00
Xi Ge
ebe0a45583 PrintAsObjC: use header path relative to usr/include when importing non-framework headers
We could assume usr/include belongs to header search paths. If a header
is located in a deeper location inside this directory, we need to print
the additional path components.

rdar://60857172
2020-03-25 23:30:28 -07:00
Slava Pestov
7c3cf1d17b ClangImporter: Fix mirroring of instance properties as static methods on NSObject
Because all metaclasses ultimately inherit from NSObject, instance
members of NSObject are also visible as static members of NSObject.

If the instance member is a property, we import the getter as an
ordinary static method, and not a static property.

The lazy loading path normally checks for the presence of alternate
decls with the same name, but it was failing to do this check if the
imported decl was a property and the alternate decl was attached to
the accessor and not the property itself.

This wasn't a problem until recently, because we weren't lazy loading
members of NSObject itself, since it had protocol conformances; now
that we are, this problem was exposed.

Fixes <rdar://problem/59170514>.
2020-02-08 00:06:21 -05:00
John McCall
faee21b626 Implement Swift serialization and deserialization of Clang types.
As part of this, we have to change the type export rules to
prevent `@convention(c)` function types from being used in
exported interfaces if they aren't serializable.  This is a
more conservative version of the original rule I had, which
was to import such function-pointer types as opaque pointers.
That rule would've completely prevented importing function-pointer
types defined in bridging headers and so simply doesn't work,
so we're left trying to catch the unsupportable cases
retroactively.  This has the unfortunate consequence that we
can't necessarily serialize the internal state of the compiler,
but that was already true due to normal type uses of aggregate
types from bridging headers; if we can teach the compiler to
reliably serialize such types, we should be able to use the
same mechanisms for function types.

This PR doesn't flip the switch to use Clang function types
by default, so many of the clang-function-type-serialization
FIXMEs are still in place.
2020-02-06 22:09:00 -05:00
Varun Gandhi
afc6ccdeb5 Re-land parsing and printing for Clang function types.
This reverts commit e805fe486e, which reverted
the change earlier. The problem was caused due to a simultaneous change to some
code by the PR with parsing and printing for Clang function types (#28737)
and the PR which introduced Located<T> (#28643).

This commit also includes a small change to make sure the intersecting region
is fixed: the change is limited to using the fields of Located<T> in the
`tryParseClangType` lambda.
2020-01-07 15:58:32 -08:00
Saleem Abdulrasool
e805fe486e Revert "Clang function types v2: Electric Boogaloo (parsing + printing)" 2020-01-06 16:26:08 -08:00
Varun Gandhi
96604470ae [AST] Add printing for Clang function types in the AST. 2020-01-06 13:00:04 -08:00
Varun Gandhi
58304137d4 Adds an expected-error test characterizing the issue in SR-9482. 2019-07-15 13:36:38 -07:00
Saleem Abdulrasool
83b290438c Windows: bridge BOOL to Bool
This allows the conversion of the Windows `BOOL` type to be converted to
`Bool` implicitly.  The implicit bridging allows for a more ergonomic
use of the native Windows APIs in Swift.

Due to the ambiguity between the Objective C `BOOL` and the Windows
`BOOL`, we must manually map the `BOOL` type to the appropriate type.
This required lifting the mapping entry for `ObjCBool` from the mapped
types XMACRO definition into the inline definition in the importer.

Take the opportunity to simplify the mapping code.

Adjust the standard library usage of the `BOOL` type which is now
eclipsed by the new `WindowsBool` type, preferring to use `Bool`
whenever possible.

Thanks to Jordan Rose for the suggestion to do this and a couple of
hints along the way.
2019-04-25 17:52:08 -07:00
Saleem Abdulrasool
09bc471e87 test: add a test for ll suffixed types being promoted to ULL
This addresses the follow up test case discussed in PR23651.  Windows
will not promote a macro literal suffixed with `ll` or `i64` to an
unsigned long long even upon an overflow.  This tests that the corner
case behaviour for importing a long long literal matches the platform
expectations.
2019-04-01 10:55:57 -07:00
Saleem Abdulrasool
8a1457c431 test: explicitly make a value unsigned on Windows
Windows will keep the imported type as `signed long long` rather than
`unsigned long long` as per the Microsoft compiler behaviour.  This
breaks the tests for this case.  Unfortunately, this is one of those
areas which must differ.
2019-03-29 16:18:09 -07:00
Saleem Abdulrasool
27801ce0d2 Merge pull request #23653 from compnerd/anonymous-is-unwritten
test: be more flexible about anonymous enumeration imports
2019-03-29 12:18:40 -07:00
Saleem Abdulrasool
13a7ca0f5a test: be more flexible about anonymous enumeration imports
This test ensures that we correctly translate the anonymous enumeration
value. However, this is an odd case within the specification.

C11 6.7.2.2p2:
"The expression that defines the value of an enumeration constant shall
be an integer constant expression that has a value representable as an
`int`."

C11 6.7.2.2p4:
"Each enumerated type shall be compatible with `char`, a signed integer
type, or an unsigned integer type.  The choice of type is
implementation-defined, but shall be capable of representing the values
of all the members of the enumeration."

C11 6.7.2.2p3:
"The identifiers in an enumerator list are declared as constants that
have type `int` and may appear wherever such are permitted."

Because the enumeration is anonymous, the value could never be written
as spelled.  This type is imported as an `int` on Windows as per the ABI
and as an `unsigned long` on LP64 targets.
2019-03-29 08:55:51 -07:00
Saleem Abdulrasool
106aa5d26b test: augment the SDK sysroot for Windows
Windows does not have `intptr_t` and `uintptr_t` defined properly.
Define the types using the clang builtin types in the SDK.
2019-03-28 21:49:15 -07:00
Saleem Abdulrasool
d090497ff9 test: improve ClangImporter.ctypes_parse on Windows
Windows uses signed enumeration types, which means that the value si
imported as `Int` rather than `CInt`.
2019-03-28 17:39:28 -07:00
Stephen Canon
1a9ae47d06 Teach the importer to import any clang vector type as SIMDn<Scalar>. (#23454)
* Teach the importer to import any vector type as SIMDN<Scalar>.

Instead of having a known set of vector types, check to see if the
element type conforms to SIMDScalar; if it does, see if we have a
SIMDN defined with the right number of elements. If both are satisfied,
import the vector type as that Swift type.

By making this change, we gain the ability to import vector types
that aren't defined in terms of the Darwin simd module, which lets
us use C API with vector types on other platforms. It also lets us
import *every* vector type that Swift can represent, rather than the
small subset that are currently hardcoded.

* Increased test coverage for increased SIMD types that we can import.

Includes some minor cleanup from review. Also eliminates the old
simd_sans_simd test, since we can now import all of these types even when the simd module isn't imported.
2019-03-22 22:11:22 -04:00
Saleem Abdulrasool
fd75ed7a29 tests: adjustments for Windows
Alter the path separators for Windows paths.  This makes 66% of the
remaining tests pass on Windows.
2019-02-17 13:55:52 -08:00
Jordan Rose
99e0c1d8a4 Merge pull request #20912 from dnadoba/master
Refuse to import C arrays that are longer than 4096
2018-12-04 14:19:33 -08:00
Jordan Rose
0ba51b0eff [test] Remove overlays with extra NS stripping from the mock SDK (#20954)
There were only two tests still using them, and they weren't hard to
migrate over to the normal mock SDK overlays.
2018-12-03 10:48:08 -08:00
David Nadoba
9e19dc918f add test with a struct that contains a large array 2018-12-03 02:45:56 +01:00
David Nadoba
e24b16651d Add test for just a too large array as a function parameter 2018-12-03 02:45:06 +01:00
David Nadoba
fcb13f7c53 refuse to import C arrays that are longer than 4096 2018-11-30 14:34:07 +01:00
Thomas Roughton
89d6cdfc4b Define mappings for CF types on LLP64
As per https://forums.swift.org/t/llp64-targets-and-integral-types/18253/15, define mappings for `CFTypeID`, `CFOptionFlags`, `CFHashCode`  to `UInt` for LLP64 support.
2018-11-27 12:11:43 +13:00
Jordan Rose
c8b61f990b Merge pull request #17615 from maniramezan/master
[SR-2250] Include Foundation framework instead of minimal NSObject.h
2018-07-11 19:14:49 -07:00
Mani Ramezan
a3b30e2396 Update mock CoreFoundation to be c++ friendly 2018-07-11 19:16:22 -04:00
Doug Gregor
d529002009 [Clang importer] Don't bridge blocks to Swift functions in ObjC generic args.
While the compiler can bridge C block types to Swift function types,
the Swift runtime cannot. Don't bridge block types to Swift function
types in Objective-C generic arguments, so

  NSArray<some-block-type>

will get imported as

  [@convention(block) (...) -> Whatever]

rather than

  [(...) -> Whatever]

Fixes rdar://problem/40879067 in a fairly narrow way; the Clang
importer's approach to adjusting types based on context needs a
cleanup, but this is the safe, localized fix suitable for 4.2.
2018-06-15 13:04:30 -07:00
Mark Lacey
803fbee0dd [ConstraintSystem] Allow LValues for the bindings of an IUO @optional requirement.
We were failing to bind the alternatives for an IUO @optional
requirement because we forgot to set the appropriate type variable option.

Fixes: rdar://problem/40868990
2018-06-11 23:44:00 -07:00
Saleem Abdulrasool
bc27395838 Inputs: sprinkle _Null_unspecified to silence nullability-completness
When trying to figure out errors from an import failure, the nullability
completeness warnings would clutter the output making it difficult to
identify the errors.  Sprinkle the declaarations with
`_Null_unspecified` to maintain the current nullability semantics and
silence the warnings.  NFC.
2018-06-11 09:23:22 -07:00
Saleem Abdulrasool
c0de9acdaa test: enable more IRGen tests on Windows
This set of tests also pass on Windows now.  These are mostly invocation
adjustments or renaming/reordering that is needed.
2018-06-07 10:45:35 -07:00
Doug Gregor
b0fc56d8ff [Clang importer] Honor swift_bridged_typedef attribute.
When the swift_bridged_typedef attribute is present on a typedef,
import the underlying type as bridged (e.g., String) rather than as
its unbridged type (e.g., NSString).

Fixes rdar://problem/39497900.
2018-04-27 17:02:58 -07:00
Saleem Abdulrasool
88003cfed8 tests: enable a few tests on Linux, u-opt
Avoid a temporary file and executing FileCheck multiple types and prefer
multiple check prefixes and streaming.  Additionally, enable some of
previously XFAIL'ed tests on Linux as well as tests that were marked as
requiring Objective-C interop.
2018-04-25 14:09:11 -07:00
Joe Groff
e49fa5c4db SILGen: Guard against unexpected nulls passed into ObjC overrides.
We want to treat arguments to ObjC override and protocol conformance thunks like "call results", since they might be called from ObjC code that doesn't fulfill its nullability promises in practice. Fixes SR-7240 | rdar://problem/38675815.
2018-03-23 16:04:29 -07:00
Jordan Rose
520d6b9b91 Make NS_TYPED_ENUMS ObjectiveCBridgeable when they wrap an object (#15270)
This allows them to be used in generic arguments for NSArray et al.
We already do this for the ones that wrap bridged values (like
NSString/String), but failed to do it for objects that /weren't/
bridged to Swift values (class instances and protocol compositions),
or for Error-which-is-special.

In addition to this being a sensible thing to do, /not/ doing this led
to IRGen getting very confused (i.e. crashing) when we imported a
Objective-C protocol that actually used an NS_TYPED_ENUM in this way.

(We actually shouldn't be using Swift's IRGen logic to emit protocol
descriptors for imported protocols at all, because it's possible we
weren't able to import all the requirements. But that's a separate
issue.)

https://bugs.swift.org/browse/SR-6844
2018-03-15 16:17:38 -07:00
Stephen Canon
f8a9e56957 Importer support for float80 (#14971)
* First pass at implementing support for mapping between long double and Float80.

* Only define CLongDouble on platforms where I know what it is.

* remove some hacks that are no longer necessary.
2018-03-07 16:49:21 -05:00
Mark Lacey
4ee6ead47e [ConstraintSystem] Implicitly force results of @optional protocol requirements.
We were not handling IUO results of @optional protocol methods
properly, sometimes forcing the @optional requirement rather than the
result of the call.

Fixes rdar://problem/37240984.
2018-02-16 16:58:52 -08:00
Andrew Trick
062be8f309 Exclusivity test case for NS_NOESCAPE import.
<rdar://problem/36989789> [SR-6862]: "Unexpected partial_apply use".
2018-02-05 18:43:30 -08:00
Jordan Rose
413e7db918 [ClangImporter] Handle diagnostics about cast types in macros
The importer handles these by first trying to look up the type by name
using Clang's Sema, but that lookup can cause diagnostics to be
emitted (usually availability diagnostics). We could try to figure out
how to propagate that to the macro when we import it, but for now just
drop the macro instead if there are any diagnostics emitted when
looking up the type.

This will be a small source compatibility break if anyone was using a
macro defined in terms of a type that's deprecated or that has partial
availability; the macro will now silently not be imported instead of
producing an unsilenceable warning.

rdar://problem/36528212
2018-01-16 13:44:22 -08:00
Jordan Rose
59b5e2ec0d Mangle an anonymous C enum using its enclosing typedef, like C++ does
Without this, we would fall back to a Swift name even for C types that
we can expect to have stable names for linkage purposes.
2018-01-10 15:38:07 -08:00
Jordan Rose
5edbefcc69 [ClangImporter] Break circularity when importing macros (#13099)
Rather than being smart about this, just record an import failure when
we start importing a particular macro and update it at the end. Also
add a PrettyStackTrace to make this a little easier to track down in
the future.

The old logic for importing macros that just aliased other macros
managed to handle this in a clever way, but that was never tested for
the newer logic that evaluates expressions (fa834e2f80). Macro
importing in general probably deserves some cleanup, but meanwhile we
should make sure not to crash!

rdar://problem/34986930
2017-12-01 11:42:49 -08:00
John McCall
ad0f8ab3b2 Weaken an assertion: conversions can go through Any but still end up
at AnyObject?.

Also, ensure that @convention(block) functions can be erased to
AnyObject, and teach SILGen how to do this with unchecked_ref_cast.

Fixes a source compatibility suite regression.
2017-11-29 02:58:04 -05:00
John McCall
549efbc240 Don't naively apply the bridging peephole to AnyObject? -> Any? -> AnyObject.
Also, fix some logic in the peephole that was incorrectly recognizing
T? -> U? -> AnyObject as a force instead of a request to create NSNull.

In both cases, we ought to still be able to do much better than the
unpeepholed code by just applying the peephole and then merging in NSNull
in the nil case, but in the short term we need to fix this bug.

Fixes rdar://35402853
2017-11-27 19:33:38 -05:00
Saleem Abdulrasool
bdb7901a1c test: modernise nullability attributes (NFC)
Use the modern spelling for the nullability attributes in the test mock
headers.  Currently, this was relying on the predefined macros from
clang to work.  However, those are only available on Darwin targets.
This is needed to make the mock environments more portable.
2017-11-01 23:27:33 -07:00
Slava Pestov
ac8e0376a2 Sema: Fix requirement/witness disambiguation for subclass existentials
When performing a lookup into a class C, we might find a member of
C that witnesses a requirement in a protocol Q that C conforms to.

In this case, AST name lookup returns both results.

There are two further levels of fitering which can eliminate the
ambiguity:

- Sema name lookup maps requirements to witnesses if the base type
  of the lookup is a nominal type or a class-constrained archetype.

  Imported conformances don't have this mapping recorded, but there's
  another hack in this code path where if a requirement maps to
  itself inside a conformance, it is dropped from the results
  altogether.

- If the duplicate results were not filtered out in Sema name lookup,
  there was another hack in CSRanking which would a witness higher
  than it's requirement when comparing solutions. This doesn't work
  for imported conformances, but usually name lookup filters out
  the duplicate results sooner, which also eliminates the exponential
  behavior from having multiple constraint system solutions.

However, if we have a subclass existential C & P where C conforms
to Q and we find a member of C that witnesses a requirement of Q,
then (C & P) does not conform to Q.

So if the conformance was imported, *both* of the above checks
would fail to disambiguate the witness and requirement, and the
member access would fail to type check.

To make this work with imported conformances, teach Sema name lookup
to extract the superclass from an existential before performing the
conformance check. If the conformance check fails, it means we
found the protocol member via the 'existential' part of the subclass
existential (eg, in our case, a member of P), and we proceed as
before.

Fixes <rdar://problem/33291112>.
2017-07-27 23:18:01 -07:00
John McCall
c0b3bf1711 Suppress access enforcement when an l-value is converted to a pointer
just for pointer identity.

The current technique for deciding whether that's the case is *extremely*
hacky and need to be replaced with an attribute, but I'm reluctant to
take that on so late in the schedule.  The hack is terrible but not too
hard to back out in the future.  Anyone who names a method like this just
to get the magic behavior knows well that they are not on the side of
righteousness.

rdar://33265254
2017-07-21 23:40:04 -04:00
John McCall
80b180a9a1 Implement a syntactic peephole to recognize explicit bridging
conversions that reverse an implicit conversion done to align
foreign declarations with their imported types.

For example, consider an Objective-C method that returns an NSString*:
  - (nonnull NSString*) foo;
This will be imported into Swift as a method returning a String:
  func foo() -> String
A call to this method will implicitly convert the result to String
behind the scenes.  If the user then casts the result back to NSString*,
that would normally be compiled as an additional conversion.  The
compiler cannot simply eliminate the conversion because that is not
necessarily semantically equivalent.

This peephole recognizes as-casts that immediately reverse a bridging
conversion as a special case and gives them special power to eliminate
both conversions.  For example, 'foo() as NSString' will simply return
the original return value.  In addition to call results, this also
applies to call arguments, property accesses, and subscript accesses.
2017-07-15 01:13:41 -04:00
John McCall
0e89efa1c8 Bridge types during import only if we are in a fully-bridgeable context.
Somehow the logic had slipped so that we were basing this decision purely
on the ImportTypeKind and not on whether the broader context is bridgeable.
This was allowing us to use bridged types when e.g. importing the results
and parameters of C function pointer types, which is really bad.

Also, when importing a reference to a typedef of block type, do not use
the typedef in a non-bridgeable context.  We import typedefs of block type
as fully-bridged types, but this means that it is invalid to import a type
using the typedef in a context where the original C type must be used.
Similarly, make sure we use a properly-imported underlying type of the
typedef when the typedef itself is unavailable.

Also, extend the special behavior of block typedefs to abstract-function
typedefs, which seems to be consistent with the expected behavior of the
tests.

Finally, I changed importType to take a new Bridgeability enum instead of
a raw canFullyBridgeTypes bool.  At the time, I was doing that because I
was going to make it tri-valued; that turned out to be unnecessary, but I
think it's an improvement anyway.
2017-07-09 13:53:19 -04:00
Jordan Rose
5c21e12fdb [API notes] Remove all API notes present in the Xcode 9 SDKs. (#10615)
The framework versions already superseded the files here, so let's not
even bother building and packaging them. There /are/ still a few
frameworks that aren't shipping their own API notes at the moment,
however, though some of them are deprecated in their entirety.

rdar://problem/32908357
2017-07-07 19:52:49 -07:00
Doug Gregor
6a26f5f044 [Clang importer/module printing] Correctly print NS_ERROR_ENUMs.
Ever since we stopped associating the top-level struct of an imported
NS_ERROR_ENUM with the Clang enum declaration, we've been unable to
print imported NS_ERROR_ENUMs. The module-printing infrastructure
would drop them thinking they aren't imported declarations.

This also affected NS_ERROR_ENUMs that were imported as members of
another type, as well as other types imported as members.

Fixes rdar://problem/32497693.
2017-06-27 17:01:29 -07:00