Commit Graph

30 Commits

Author SHA1 Message Date
Erik Eckstein
a768037d0b IRGen: fix failing unconditional class casts
When unconditionally casting from a base to a final derived class, e.g. `base as! Derived`, the program did not abort with a trap.
Instead the resulting null-pointer caused a crash later in the program.
This fix inserts a trap condition for the failing case of such a cast.

rdar://151462303
2025-05-19 16:56:47 +02:00
Doug Gregor
43df05a89c [SE-0470] Prohibit isolated conformances in dynamic casts marked as such
Certain dynamic casts cannot work safely with isolated conformances,
regardless of what executor the code runs on. For such cases, reject
all attempts to conform to the type.
2025-03-26 22:31:52 -07:00
Slava Pestov
0c2f28fd3d AST: Remove GenericSignature parameter from OpenedArchetypeType::get() 2024-08-20 12:15:27 -04:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Nate Chandler
70cfa6a05c [IRGen] Check for nil in final class cast opt.
As an optimization, there is a fast-cast to non-final classes that just
compares isa pointers.  If the source of the cast is an Optional<class>,
though, it's not allowed to "directly compare the isa-pointer"; because
the source might be Optional.none, i.e. null.

Add a check for nil when emitting the fast class cast.

rdar://108614878
2023-05-03 18:39:08 -07:00
Erik Eckstein
860078a79a IRGen: fast casting to final classes
When casting a class instance to a final class, we can directly compare the isa-pointer with address of the metadata.
This avoids a call to `swift_dynamicCastClass`.
It also avoids a call to the metadata accessor of the class (which calls `swift_getInitializedObjCClass`).
For comparing the metadata pointers it's not required that the metadata is fully initialized.
2022-04-07 16:36:21 +02:00
Robert Widmann
f694bf585b Plumb GenericSignature of SIL Function Through IRGen 2022-03-08 03:09:19 -08:00
Slava Pestov
c8da1583cf IRGen: Correctly use formal type for class downcasts
Fixes <https://bugs.swift.org/browse/SR-11818>, <rdar://problem/57369535>.
2019-11-22 16:39:17 -05:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Sho Ikeda
422136e1a2 [gardening][enum class] Replace unsigned char with uint8_t for consistency
Before the changes:

- `git grep -E "enum class .+ : uint8_t \{" | wc -l`: 90
- `git grep -E "enum class .+ : unsigned char \{" | wc -l`: 26
2018-03-12 13:57:36 +09:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
John McCall
44f71b1f11 Handle all the cases of scalar dynamic casts allowed by SIL in IRGen.
In particular, reliably look through a single level of optional
types and handle metatype-to-class casts.

Fixes rdar://24924966.
2016-04-06 19:10:02 -07:00
John McCall
4f669fe563 Move some cast-emission code to GenCast.cpp. NFC. 2016-04-06 13:31:18 -07:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Johan K. Jensen
fa76656c82 Remove instances of duplicated words 2015-12-03 20:00:29 +01:00
Nadav Rotem
16bd644825 [IRGen] Remove an extra null comparison in 'exact' checked_cast_br.
rdar://22666588

This change removes a comparison and a branch on every virtual call. Before this
change we were generating code for comparing the metadata to figure out if the
incoming instance is an 'exact' cast, and then we checked if the result of the
cast was zero. This is unnecessary because we can simply reuse the result of the
exact metadata comparison. Moreover, we know that the metadata instance can't be
zero because we've emitted a load to that address that did not trap.

  %1 = getelementptr inbounds %C4main1X, %C4main1X* %0 ...
  %.metadata = load %swift.type*, %swift.type** %1  // Loading %0
  %2 = icmp eq %swift.type* %.metadata, bitcast (...)
  %3 = icmp ne %C4main1X* %0, null   ; <-----------  %0 can't be null.
  %4 = and i1 %3, %2
  br i1 %4, label %5, label %7

Swift SVN r31920
2015-09-12 05:10:53 +00:00
Joe Groff
7e78ff68d1 IRGen/Runtime: Make dynamic metatype-to-AnyObject casts work.
Bonus fix for rdar://problem/19624697.

Swift SVN r24808
2015-01-28 23:06:45 +00:00
Joe Groff
5ed6e2731b IRGen: Code gen for casts to existential metatype.
The code path here is mostly the same as the class cast case--we have to test the ObjC class (if it is a class) against any ObjC protocols, then look up conformances for the native protocols.

Swift SVN r23184
2014-11-09 02:40:25 +00:00
Joe Groff
f229840161 IRGen: Code generation for general class existential casts.
Emit a helper that invokes conformsToProtocol the appropriate number of times for the destination protocol type.

Swift SVN r23176
2014-11-08 04:53:46 +00:00
Joe Groff
2047cec286 IRGen: Remove more obsolete casting entry points.
Swift SVN r23137
2014-11-06 22:09:48 +00:00
Joe Groff
64a2838ff9 Remove dead function.
Swift SVN r23136
2014-11-06 21:50:06 +00:00
Joe Groff
1c4e08a9c8 Fix up a tangle of issues related to reabstraction.
- A spot fix in SILGen for reabstracting the result of a downcast, which fixes checked casts to function types.
- Associate the layout information in type metadata records with the most abstract representation of the type. This is the correct thing to do in cases where we need the metadata as a tag for an opaque value--if we store a value in an Any, or pass it as an unconstrained generic parameter, we must maximally reabstract it. This fixes the value semantics of existentials containing trivial metatypes.
- To ensure that we get runtime layout of structs and enums correct when they contain reabstractable types, introduce a "metadata for layout" concept, which doesn't need to describe the canonical metadata for the type, but only needs to describe a type with equivalent layout and value semantics. This is a correctness fix that allows us to correctly lay out generic types containing dependent tuples and functions, and although we don't really take advantage of it here, it's also a potential runtime performance win down the road, because we could potentially produce direct metadata for a primitive type that's layout-equivalent with a runtime-instantiated type. To aid in type safety here, push SILType deeper into IRGen in places where we potentially care about specific representations of types.
- Finally, fix an inconsistency between the runtime and IRGen's concept of what spare bits unmanaged references and thick metatypes have.

Together, these fixes address rdar://problem/16406907, rdar://problem/17822208, rdar://problem/18189508, and likely many other related issues, and also fixes crash suite cases 012 and 024.

Swift SVN r21963
2014-09-16 01:44:34 +00:00
John McCall
f0e14f7e65 Carry the formal types of the arguments in the indirect-cast
instructions.

Swift SVN r18934
2014-06-16 17:35:43 +00:00
Nadav Rotem
bb0438f27b [Early Binding of Virtual Calls] Add a new kind of unconditional_checked_cast: Identical cast. This cast checks that the class that we inspect is exactly the type we expect, and not a subclass of that type.
Swift SVN r18859
2014-06-13 06:15:14 +00:00
Joe Groff
1f203ab6a9 IRGen: Lower metatype conversion instructions.
Implement IR lowering for the metatype conversion SIL instructions.

Swift SVN r18844
2014-06-12 23:17:43 +00:00
John McCall
9f5736880e Basic IR-generation support for checked_cast_addr
in terms of swift_dynamicCast.

Swift SVN r18825
2014-06-12 08:08:20 +00:00
John McCall
6fb1dc2db1 Centralize more dynamic-casting code into GenCast.cpp. NFC.
Swift SVN r18818
2014-06-12 01:00:23 +00:00