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.7k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
420
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
6.6k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
890
マルチスレッド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
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
From Translations to Multi Dimension Entities
alexanderschranz
2
130
42 best practices for Symfony, a decade later
tucksaun
1
180
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
400
useSyncExternalStoreを使いまくる
ssssota
6
1k
103 Early Hints
sugi_0000
1
230
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
return文におけるstd::moveについて
onihusube
1
1.1k
Go の GC の不得意な部分を克服したい
taiyow
3
790
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Building Adaptive Systems
keathley
38
2.3k
Faster Mobile Websites
deanohume
305
30k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Visualization
eitanlees
146
15k
Rails Girls Zürich Keynote
gr2m
94
13k
Done Done
chrislema
181
16k
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ɺͲͷαϒ λΠϓؔΛͬͯܕνΣοΫΛ௨͔ͨ͠Λ֮ ͓͍͑ͯͯɺ͋ͱͰ࣮ࡍͷมॲཧΛૠೖ͢Δɻ
͓ΘΓ