Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
理論から入門するswift/lib/Sema - わいわいswiftc #1
Search
ukitaka
March 30, 2018
Programming
5
1.6k
理論から入門するswift/lib/Sema - わいわいswiftc #1
https://iosdiscord.connpass.com/event/82436/
での発表資料です。
ukitaka
March 30, 2018
Tweet
Share
More Decks by ukitaka
See All by ukitaka
CSの生産性改善を支える分析環境 Mercari CS/CRE Tech Talk #1
ukitaka
1
2.6k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
410
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
6.1k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
880
マルチスレッドRxSwift @ 社内RxSwift勉強会
ukitaka
5
1.2k
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
RxSwift コードリーディングの勘所@社内RxSwift勉強会
ukitaka
3
1k
Other Decks in Programming
See All in Programming
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.7k
Ethereum_.pdf
nekomatu
0
460
イベント駆動で成長して委員会
happymana
1
330
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
役立つログに取り組もう
irof
28
9.6k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
960
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
受け取る人から提供する人になるということ
little_rubyist
0
250
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
Featured
See All Featured
BBQ
matthewcrist
85
9.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Designing the Hi-DPI Web
ddemaree
280
34k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Making Projects Easy
brettharned
115
5.9k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Transcript
ཧ͔Βೖ͢Δ swift/lib/Sema Θ͍Θ͍swiftc #1 @ukitaka
twitter: @ukitaka_ github: @ukitaka Yuki Takahashi
ࣗݾհ ❤ ؔܕϓϩάϥϛϯάɺDDD ✏ ܕγεςϜɺίϯύΠϥ ϨΠϠʔ
ϒϩάॻ͍ͯΔ https://blog.waft.me
ษڧ͕ͯΒ࡞ͬͨͷ • SwiftͰܕγεςϜ / ܕਪΛॻ͍ͯΈͨΓ • ukitaka/TypeSystems • ukitaka/AlgorithmW.swift •
SILOptimizerΛࣗ࡞ͯ͠ΈͨΓ • ukitaka/sil-opt-scala
contribute…
ࣗݾհऴΘΓ
ࠓͷ
swift/lib/Sema
swift/lib/Sema • Sema = Semantic Analysis (ҙຯղੳ) • ओʹܕਪɾܕݕࠪΛ୲͢ΔϞδϡʔϧ •
ίʔυྔҰ൪ଟ͍ • ΊͪΌΊͪΌෳࡶ
ܕͷָͦ͠͏…Ͱ Ͳ͔͜ΒखΛ͚Α͏…
swift/lib/Sema • ෳࡶʹབྷΈ߹͍ͬͯΔ͕ɺ͍͔ͭ͘ͷτϐο Ϋͷू·Γɻͦͯ͠୯ମͰݟΔͱཧ௨Γ࣮ ͞Ε͍ͯΔ߹͕ଟ͍ɻ • ֓೦Λ͍ͬͯΔ͚ͩͰ͍ͩͿೖΓ͍͢ • ͳͷͰ… ʮཧ͔Βೖ͢Δʯ
ࠓѻ͏τϐοΫ • δΣωϦοΫͳؔ (letଟ૬)ͱܕਪ • αϒλΠϐϯάͱܕڧ੍ҙຯ • ଘࡏܕ (࣌ؒͷ߹Ͱলུ)
ଘࡏܕͷղઆQiitaͰ…
δΣωϦοΫͳؔ(letଟ૬) ͱܕਪ
Generics in Swift • δΣωϦΫεपΓͷ࣮ͱʹ͔͍͘͠… • ͔͠͠(੍Λؚ·ͳ͍)δΣωϦοΫͳؔͩ ͚ͳΒܕਪؚΊཧ࣮ͦΜͳʹ͘͠ ͳ͍ •
δΣωϦοΫͳؔͱͦͷܕਪΛݟΔ͜ͱͰɺ SemaϞδϡʔϧΛ၆ᛌ͢Δ
δΣωϦοΫͳؔ
= ଟ૬ͳؔ
ଟ૬ੑͱ…
ଟ૬ੑ(polymorphism) • ϓϩάϥϜͷҰ෦ΛɺҟͳΔจ຺Ͱҟͳ Δܕͱͯ͠༻͍Δ͜ͱΛՄೳʹ͢Δ֤छݴޠ ػೳ
ଟ૬ੑ(polymorphism) Bool → Bool ಉ͡idؔͰɺҟͳΔจ຺ͰҟͳΔܕ Int → Int
ଟ૬ͳؔSwiftͰ Ͳ͏࣮ݱ͞Ε͍ͯΔʁ
ଟ૬ͳؔͱܕਪ • ࣮ଟ૬ͳؔͱܕਪ͋ΔҙຯηοτͰ ఏڙ͞Ε͍ͯΔػೳ • ܕਪΛͬͯϙϦϞʔϑΟζϜΛ࣮ݱ • δΣωϦοΫͳؔΛΔʹܕਪΔ ඞཁ͕͋Δ
Swiftʹ͓͚Δ (ϩʔΧϧ)ܕਪ
ܕਪͷେ·͔ͳྲྀΕ 1. ܕ͕໌ࣔ͞Ε͍ͯͳ͍ͱ͜ΖΛܕม(Type Variable)Ͱ͓͘ 2. ܕมͷ੍(Constraint)Λੜ͢Δ 3. ੍Λղ͘
ܕਪͷେ·͔ͳྲྀΕ • ܕ͕ܾ·͍ͬͯͳ͍ͱ͜ΖΛܕมͰ͓͘ Type Variables: #0 = $T0 [inout allowed]
#1 = $T1 [inout allowed] $ swift -frontend -typecheck -debug-constraints test.swift $T1 $T0
• ੍(Constraint)Λੜ͢Δ ܕਪͷେ·͔ͳྲྀΕ $T0 literal conforms to ExpressibleByIntegerLiteral $T0 conv
$T1 $ swift -frontend -typecheck -debug-constraints test.swift $T0 Ϧςϥϧ $T0 $T1ʹมͰ͖Δ
ܕਪͷେ·͔ͳྲྀΕ • ੍Λղ͘ Active bindings: $T0 := Int (trying $T0
:= Int ($T1 bindings=(supertypes of) Int) Active bindings: $T1 := Int (trying $T1 := Int (found solution 0 0 0 0 0 0 0 0 0 0 0 0 0) ) ) $ swift -frontend -typecheck -debug-constraints test.swift
ܕਪͷେ·͔ͳྲྀΕ • ੍Λղ͘ ---Solution--- Fixed score: 0 0 0 0
0 0 0 0 0 0 0 0 0 Type variables: $T1 as Int $T0 as Int $ swift -frontend -typecheck -debug-constraints test.swift Int Int
δΣωϦοΫͳؔͷ߹
ܕਪͷେ·͔ͳྲྀΕ(࠶ܝ) 1. ܕ͕໌ࣔ͞Ε͍ͯͳ͍ͱ͜ΖΛܕม(Type Variable)Ͱ͓͘ ←ଟ૬ͳؔͷ࣮ݱʹ͜͜ʹϙΠϯτ 2. ܕมͷ੍(Constraint)Λੜ͢Δ 3. ੍Λղ͘
δΣωϦοΫͳؔͷܕਪ
δΣωϦοΫͳؔͷܕਪ ෦తͳܕͷදݱ: <τ_0_0> τ_0_0 → τ_0_0 ݟͨͷܕ: <A> A →
A Aͱ͍͏໊લࣺͯΒΕͯdepthͱindexʹΑΔ໊ແ͠දݱʹͳ͍ͬͯΔ
δΣωϦοΫͳؔͷܕਪ ग़ݱͷͨͼʹτ_0_0ʹ৽͍͠ܕมΛׂΓͯΔ Opened types: opens τ_0_0 -> $T1 opens τ_0_0
-> $T4 $ swift -frontend -typecheck -debug-constraints test.swift ͜Ε͕ଟ૬ੑΛ࣮ݱ͍ͯ͠ΔϙΠϯτ $T1 -> $T1 $T4 -> $T4
δΣωϦοΫͳؔͷܕਪ Type Variables: #0 = $T0 as ($T1) -> $T1
#1 = $T1 #2 = $T2 #3 = $T3 as ($T4) -> $T4 #4 = $T4 #5 = $T5 #6 = $T6 $ swift -frontend -typecheck -debug-constraints test.swift
δΣωϦοΫͳؔͷܕਪ • ͋ͱಉ͡Α͏ʹ੍ੜͯ͠ղ͚ͩ͘ ---Solution--- Fixed score: 0 0 0 0
0 0 0 0 0 0 0 0 0 Type variables: $T6 as (Int, Bool) $T3 as (Bool) -> Bool $T4 as Bool $T0 as (Int) -> Int $T1 as Int $T5 as Bool $T2 as Int $ swift -frontend -typecheck -debug-constraints test.swift
δΣωϦοΫͳؔͷܕਪ • ग़ݱͷͨͼʹ৽͍͠ܕมΛׂΓ্ͯͨͰɺ ܕΛਪ͢Δ͜ͱʹΑͬͯଟ૬ੑΛ࣮ݱͯ͠ ͍Δɻ • ͜ΕΛletଟ૬ͱݺͿ
δΣωϦοΫͳؔͱܕਪ The secret life of types in Swift https://medium.com/@slavapestov/the-secret-life-of-types-in-swift-ff83c3c000a5
͏গ࣮͠دΓͷ
ܕνΣοΫؔ࿈ͷओཁͳΫϥε • ίʔυྔଟ͍͚ͲɺओͳΫϥε2ͭͷΈ • TypeChecker → ܕݕࠪ୲ • ConstraintSystem →
ܕਪ୲ • TypeChecker͕Expr͝ͱʹConstraintSystemΛ ΠϯελϯεԽ͍ͯͬͯ͠ΔΠϝʔδ
TypeChecker / ConstraintSystem
ܕมɺ੍ੜͷྫ
੍ͷ୯ҰԽ (solve)
੍ͷ୯ҰԽ (solve) • جຊతʹީิΛͲΜͲΜࢼ͍ͯ͘͠ • PotentialBindings ( e.g. Ϧςϥϧ͔ͩΒIntͩΖ͏ )
• Overloadͷީิͨͪ • ConstraintGraphͱ͍͏(ϋΠύʔ)άϥϑͰ੍ Λཧ • େ·͔ʹBindingΛՃ → άϥϑΛཧͷ܁Γฦ͠
ղͷద༻(apply)ͱASTॻ͖͑ • ExprRewriter͕ܕ͖ͷASTͱॻ͖͑Δ
letଟ૬Λ࣮ݱ͍ͯ͠Δ෦ δΣωϦοΫͳؔͳΒ ܕύϥϝʔλʹ ϑϨογϡͳܕมΛ ׂΓͯΔͱ͍͏ ཧͦͷ··ͷ࣮ɻ
·ͱΊ • ଟ૬ͳ͕ؔܕਪʹΑ࣮ͬͯݱ͞Ε͍ͯΔ ༷ࢠΛݟͨ • ConstraintSystem͕ܕਪΛ͢Δͷେ·͔ͳ ྲྀΕΛΈͨ
͓·͚: ੍͖ͷδΣωϦΫεʁ • ࣮ͱͯ͠جຊతʹಉ͡ • TʹׂΓͯΒΕͨܕมʹ $T1 conforms to EquatableͷΑ͏ͳ੍͕ՃΘΔ͘Β͍
• ͔͠͠എܠʹ͋Δཧ·ͨผ…
͓·͚: ੍͖ͷδΣωϦΫεʁ • ݶఆܕ (Qualified type) • TaPLʹ໊લ͔͠ࡌͬͯͳ͍ • Jones,
Mark P. Qualified Types: Theory and Practice. Cambridge University Press, 1994. • Haskellͷ Eq a => a -> a -> Bool • ༗քྔԽ(bounded qualification)Ͱͳ͍
αϒλΠϐϯάͱ ܕڧ੍ҙຯ
ΫΠζ • Swiftʹ͓͍ͯɺT Optional<T>ͷαϒλΠ ϓ͔ʁʁ
T is subtype of Optional<T> • ͑: αϒλΠϓɻ • αϒλΠϐϯάنଇυΩϡϝϯτʹॻ͍
͍ͯͳ͍͕ɺιʔείʔυதͷίϝϯτʹͪΐ ͪ͜ΐ͜ఆٛ͞Ε͍ͯΔɻ(͕ͯ͢ॻ͍ͯ͋ΔΘ͚ Ͱͳ͍) • lib/Sema/CSSimplify.cpp Λࢀর
T is subtype of Optional<T>
αϒλΠϓؔ • ͲΜͳαϒλΠϓؔΛ࣋ͨͤΔ͔ͦͷݴޠͰ ͷܾΊͷ • ྫ͑ Int < Float ͱ͔
Int < String ͱ͔࣋ͨͤͯ (߹ੑ͕औΕΔͷͳΒ) ྑ͍ • SwiftOptionalΛָʹѻ͏Έͱͯ͠ ɺ T < Optional<T>Λ࠾༻͍ͯ͠Δ
ଞʹͲΜͳ αϒλΠϓ͕ؔ͋Δʁ
Optional-to-optional
Metatype
Existential
αϒλΠϓ͔ͩΒ ͳΜͰOKʁ
ྫ: Existential
࣮ߦ࣌ͷϝϞϦ্ͷදݱ͕ ҧ͏Մೳੑ͕͋Δ
͜ͷʹର͢ΔΞϓϩʔν αϒλΠϐϯάͷ࣮ํࣜେ͖͚ͯ͘2ͭ 1. inclusiveͳ࣮ • ڞ௨ͷ࣮ߦ࣌ͷදݱΛ͏ • JavaͳͲ 2.coerciveͳ࣮ •
มॲཧ(ܕڧ੍)ΛڬΈࠐΉ ← Swiftͬͪ͜
ܕڧ੍ҙຯ(coercion semantics) • ؆୯ʹݴ͏ͱɺ̍ͭͷαϒλΠϐϯάنଇʹ͍ͭͯɺ1 ͭͷมॲཧ(ܕڧ੍)͕ଘࡏ͢Δɻ • TypeCheckerɺͲͷαϒλΠϓؔΛͬͯܕνΣο ΫΛ௨͔ͨ͠Λه͓͍ͯͯ͠ɺ͋ͱͰ࣮ࡍͷมॲཧ Λૠೖ͢Δɻ •
lib/Sema/CSApply.cppͷExprRewriterͱ͍͏Ϋϥε Λࢀߟɻ
ྫ: Optionalͷ߹
ྫ: Optionalͷ߹
ྫ: Optionalͷ߹ Constraint restrictions: Int to Optional<Int> is [value-to-optional] $
swift -frontend -typecheck -debug-constraints test.swift
ྫ: Optionalͷ߹ $ swiftc -dump-ast test.swift (inject_into_optional implicit type=‘Optional<Int>’ …
(declref_expr type=‘Int' … ) )
ྫ: Optionalͷ߹ $ swiftc -emit-sil test.swift %9 = load %3
: $*Int %10 = enum $Optional<Int>, #Optional.some!enumelt.1, %9 : $Int store %10 to %8 : $*Optional<Int> %12 = integer_literal $Builtin.Int32, 0 %13 = struct $Int32 (%12 : $Builtin.Int32) ͭ·ΓରԠ͢Δܕڧ੍ʮ.someͰแΉʯ
Classͷ߹
ྫ: Classͷ߹
ྫ: Classͷ߹ Constraint restrictions: Dog to Animal is [superclass] $
swift -frontend -typecheck -debug-constraints test.swift
ྫ: Classͷ߹ $ swiftc -dump-ast test.swift (derived_to_base_expr implicit type=‘Animal’ …
(declref_expr type=‘Dog' … ) )
ྫ: Classͷ߹ $ swiftc -emit-sil test.swift … %10 = load
%3 : $*Dog strong_retain %10 : $Dog %12 = upcast %10 : $Dog to $Animal store %12 to %9 : $*Animal %14 = integer_literal $Builtin.Int32, 0 %15 = struct $Int32 (%14 : $Builtin.Int32) ͭ·ΓରԠ͢Δܕڧ੍ʮΞοϓΩϟετʯ
ྫ:Existentialͷ߹ աఔলུ (erasure_expr implicit type=‘Animal’ … (declref_expr type=‘Dog' … )
) ରԠ͢Δܕڧ੍ʮଘࡏܕͷύοέʔδԽʯ ( = type erase)
Swiftʹ͓͚Δʮ҉มʯ • Α͘ʮ҉มʯͳͲͱݺΕΔΞϨͷਖ਼ମ ΄΅΄΅αϒλΠϓؔʹج͍ͮͨܕڧ੍ • ҉ม͋Δͱ͜ΖʹαϒλΠϐϯά͋Γ
࣮Λগ͚ͩ͠ݟΔ
αϒλΠϐϯάʙܕڧ੍ • ConstraintSystemͷConstraintRestrictionsʹ ͬͨαϒλΠϓ͕ؔه͞ΕΔ • ExprRewriter::coerceToTypeͰܕڧ੍
ExprRewriter::coerceToType ܕڧ੍(coercion)ͷॲཧΛ͍ͯ͠Δؔ
ExprRewriter::coerceToType ConversionRestrictionKindʹԠͨ͡ॲཧ ͨͱ͑ValueToOptionalͳΒInjectIntoOptionalͱ͍͏ ܕڧ੍Λૠೖ
αϒλΠϐϯά·ͱΊ • αϒλΠϐϯάنଇʹΑͬͯఆٛ͞Ε͍ͯΔ • ͦΕͧΕنଇʹରԠ͢Δܕڧ੍͕ଘࡏ͢Δ • TypeChecker/ConstraintSystemɺͲͷαϒ λΠϓؔΛͬͯܕνΣοΫΛ௨͔ͨ͠Λ֮ ͓͍͑ͯͯɺ͋ͱͰ࣮ࡍͷมॲཧΛૠೖ͢Δɻ
͓ΘΓ