Commit Graph

32 Commits

Author SHA1 Message Date
Rintaro Ishizaki
6980509f8c [ASTGen] Generate DefaultArgumentInitializer
Rework ParamDecl contextualization.
2025-02-18 11:13:39 -08:00
Doug Gregor
ee9c066050 Introduce a new Initializer subclass for the arguments of custom attributes
Since the introduction of custom attributes (as part of property
wrappers), we've modeled the context of expressions within these
attributes as PatternBindingInitializers. These
PatternBindingInitializers would get wired in to the variable
declarations they apply to, establishing the appropriate declaration
context hierarchy. This worked because property wrappers only every
applied to---you guessed it!---properties, so the
PatternBindingInitializer would always get filled in.

When custom attributes were extended to apply to anything for the
purposes of macros, the use of PatternBindingInitializer became less
appropriate. Specifically, the binding declaration would never get
filled in (it's always NULL), so any place in the compiler that
accesses the binding might have to deal with it being NULL, which is a
new requirement. Few did, crashes ensued.

Rather than continue to play whack-a-mole with the abused
PatternBindingInitializer, introduce a new CustomAttributeInitializer
to model the context of custom attribute arguments. When the
attributes are assigned to a declaration that has a
PatternBindingInitializer, we reparent this new initializer to the
PatternBindingInitializer. This helps separate out the logic for
custom attributes vs. actual initializers.

Fixes https://github.com/swiftlang/swift/issues/76409 / rdar://136997841
2024-12-06 17:40:32 -08:00
Hamish Knight
af82121286 [AST] Make PatternBindingInitializer::setBinding private
And add some assertions to ensure we don't attempt
to re-set the binding or index (the parent context
can unfortunately change currently).
2024-01-17 16:02:33 +00:00
Hamish Knight
47f7a31095 [AST] Tighten up type of InitContext
Switch from promising a DeclContext to a
PatternBindingInitializer.

This has a couple of benefits:
- It eliminates a few places where we were force
`cast`'ing to PatternBindingInitializer.
- It improves the clarity of what's being stored,
it's not whatever the parent context of the
initializer is, it's specifically the
PatternBindingInitializer context if it exists.
2024-01-17 16:02:33 +00:00
Hamish Knight
3399b79c81 [AST] NFC: Remove some unused code 2024-01-17 16:02:33 +00:00
Hamish Knight
28c7d26683 [AST] Remove SerializedDefaultArgumentInitializer
This stores the same state as
DefaultArgumentInitializer, use that instead.
2024-01-17 16:02:32 +00:00
Hamish Knight
e3261f6b04 [AST] Remove SerializedPatternBindingInitializer
This stores the same state as
PatternBindingInitializer, we can use that
instead.
2024-01-17 16:02:32 +00:00
Hamish Knight
fb0a75fe55 [AST] NFC: Introduce PatternBindingInitializer::create 2024-01-17 16:02:32 +00:00
Pavel Yaskevich
b21e8426a0 [AST] NFC: Remove @runtimeMetadata related code 2023-08-15 12:16:40 -07:00
Pavel Yaskevich
b15e9aaf7a [AST] RuntimeMetadata: Generator context should match that of the decl its attached to
This makes it possible to to reference to `Self` if attribute is
attached to a method or a property of a nominal type.

Resolves: rdar://104210108
2023-01-16 15:07:27 -08:00
Pavel Yaskevich
7d54af84ea [AST] RuntimeMetadata: Add new initializer context to cover runtime attrs 2022-12-20 09:45:01 -08:00
Holly Borla
28caecbc0f [Sema] Generalize PropertyWrapperInitializer to work with regular wrapped
properties.
2021-08-05 11:54:00 -04:00
Holly Borla
b821c8d76b [Sema] Add an implicit applied property wrapper expression and a new
dedicated initializer context for this expressions.
2021-02-25 18:35:14 -08:00
Robert Widmann
d8092f4aa9 [NFC] Mark PatternBindingInitializer::getImplicitSelfDecl const
Even though it is not physically const because of the lazy initialization, it is logically const and deserves to be marked as such.
2020-07-23 20:43:27 -07:00
Hamish Knight
25015a5a5f [AST] Remove some dead code 2019-10-13 12:10:25 -07:00
Slava Pestov
df9f7f979a Parse: Remove uses of getParameterLists() from default argument parsing 2018-07-22 20:56:56 -07:00
John McCall
9bee3cac5a Generalize storage implementations to support generalized accessors.
The storage kind has been replaced with three separate "impl kinds",
one for each of the basic access kinds (read, write, and read/write).
This makes it far easier to mix-and-match implementations of different
accessors, as well as subtleties like implementing both a setter
and an independent read/write operation.

AccessStrategy has become a bit more explicit about how exactly the
access should be implemented.  For example, the accessor-based kinds
now carry the exact accessor intended to be used.  Also, I've shifted
responsibilities slightly between AccessStrategy and AccessSemantics
so that AccessSemantics::Ordinary can be used except in the sorts of
semantic-bypasses that accessor synthesis wants.  This requires
knowing the correct DC of the access when computing the access strategy;
the upshot is that SILGenFunction now needs a DC.

Accessor synthesis has been reworked so that only the declarations are
built immediately; body synthesis can be safely delayed out of the main
decl-checking path.  This caused a large number of ramifications,
especially for lazy properties, and greatly inflated the size of this
patch.  That is... really regrettable.  The impetus for changing this
was necessity: I needed to rework accessor synthesis to end its reliance
on distinctions like Stored vs. StoredWithTrivialAccessors, and those
fixes were exposing serious re-entrancy problems, and fixing that... well.
Breaking the fixes apart at this point would be a serious endeavor.
2018-06-30 05:19:03 -04:00
Robert Widmann
28a8fb1c0b [NFC] Drop mutability from parameter context changes
Refactoring in this area means we no longer mutate the array, just
call non-const members on the elements.
2018-05-24 12:15:14 -07:00
Robert Widmann
03580d2fe5 Add a parameter list to EnumElementDecl
This models, but does not plumb through, default arguments.
2018-03-28 00:05:56 -04:00
Slava Pestov
400693b811 AST: Store the default argument resilience expansion in the AbstractFunctionDecl
Previously we stored this inside each default argument
initializer context. This was overkill, because it is
the same for all default arguments in a single function,
and also insufficient, because initializer contexts are
not serialized and thus not available in SILGen when
the function is in a different module.

Instead store it directly inside the function and
serialize it.

NFC for now, since SILGen isn't using this yet.
2017-09-15 16:20:45 -07:00
Slava Pestov
51aebd2127 AST: Fix name lookup from within lazy property initializers
Allow instance properties and methods to be referenced from
within a lazy property initializer, with or without explicit
'self.' qualification.

The old behavior in Swift 3 was an incredible combination
of odd quirks:

- If the lazy property had an explicitly-written type, it was
  possible to reference instance members from the initializer
  expression by explicitly prefixing 'self.'.

- However, if the lazy property type is inferred, it would
  first be type checked in the initializer context, which
  has no 'self' available.

- Unqualified references to instance members did not work
  at all, because name lookup thought the "location" of the
  lookup was outside of the body of the getter.

- Unqualified references to static properties worked, however
  unqualified references to static methods did not, and
  produced a bogus diagnostic, because one part of the name
  lookup code thought that initializers were "instance
  context" and another thought they were "static context".

This patch improves on the old behavior with the following
fixes:

- Give PatternBindingInitializers associated with lazy
  properties an implicit 'self' declaration for use by
  name lookup.

- In order to allow "re-parenting" the initializer after it
  has been type checked into the body of the getter, "steal"
  the initializer's 'self' when buiding the getter.

- Fix up name lookup and make it aware of the implicit
  'self' decl of a PatternBindingInitializer.

This improves upon an earlier fix for this issue by Doug Gregor
which only worked with ASTScope enabled; the new fix is more
general and shares logic between the two name lookup
implementations.

Fixes <rdar://problem/16888679>, <https://bugs.swift.org/browse/SR-48>,
<https://bugs.swift.org/browse/SR-2203>,
<https://bugs.swift.org/browse/SR-4663>, and the countless other
dupes of this issue.
2017-05-24 18:59:48 -07:00
Slava Pestov
d65d1d25f9 Sema: Diagnostics for @_inlineable, @_versioned and Swift 4 default arguments model
Piggybacks some resilience diagnostics onto the availability
checking code.

Public and versioned functions with inlineable bodies can only
reference other public and internal entities, since the SIL code
for the function body is serialized and stored as part of the
module.

This includes @_transparent functions, @_inlineable functions,
accessors for @_inlineable storage, @inline(__always) functions,
and in Swift 4 mode, default argument expressions.

The new checks are a source-breaking change, however we don't
guarantee source compatibility for underscored attributes.

The new ABI and tests for the default argument model will come in
subsequent commits.
2017-01-09 16:59:13 -08:00
Slava Pestov
5eb16e6b34 Include-what-you-use: Initializer.h should not be pulled in from AST.h 2017-01-09 16:46:31 -08: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
Doug Gregor
78b007f178 Always create a DefaultArgumentInitializer for a parameter with a default argument.
As with pattern binding initializer contexts, we were trying to
optimize away these contexts, leading to an unpredictable AST.
2016-09-02 13:51:00 -07:00
Doug Gregor
5e25d25c96 [AST] Separate the DeclContexts for different pattern binding entries in a pattern binding decl. 2016-09-02 10:39:19 -07:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Chris Lattner
59c22383fb Rework PatternBindingDecl to maintain a list of pattern/initexpr pairs inside of it.
Previously, a multi-pattern var/let decl like:
  var x = 4, y = 17

would produce two pattern binding decls (one for x=4 one for y=17).  This is convenient
in some ways, but is bad for source reproducibility from the ASTs (see, e.g. the improvements
in test/IDE/structure.swift and test/decl/inherit/initializer.swift).

The hardest part of this change was to get parseDeclVar to set up the AST in a way
compatible with our existing assumptions. I ended up with an approach that forms PBDs in 
more erroneous cases than before.  One downside of this is that we now produce a spurious
  "type annotation missing in pattern"
diagnostic in some cases.  I'll take care of that in a follow-on patch.





Swift SVN r26224
2015-03-17 16:14:18 +00:00
David Farler
51f8070abe Serialize local types
Local type declarations are saved in the source file during parsing,
now serialized as decls. Some of these may be defined in DeclContexts
which aren't Decls and previously weren't serialized. Create four new
record kinds:

* PatternBindingInitializer
* DefaultArgumentInitializer
* AbstractClosureExpr
* TopLevelCodeDecl

These new records are used to only preserve enough information for
remangling in the debugger, and parental context relationships.

Finally, provide a lookup API in the module to search by mangled name.
With the new remangling API, the debugging lifecycle for local types
should be complete.

The extra LOCAL_CONTEXT record will compressed back down in a
subsequent patch.

Swift SVN r24739
2015-01-27 01:49:54 +00:00
John McCall
342a8b35db Parse default-argument expressions in an initializer context.
Swift SVN r11178
2013-12-12 03:36:27 +00:00
John McCall
0bfa86673f Introduce (but don't yet create) a new kind of DeclContext for
various kinds of expressions that appear outside of local
evaluation contexts.

Swift SVN r11176
2013-12-12 03:36:17 +00:00