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
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
170
Criando Commits Incríveis no Git
marcelgsantos
2
170
testcontainers のススメ
sgash708
1
120
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
710
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
GraphQLとの向き合い方2022年版
quramy
44
13k
Being A Developer After 40
akosma
87
590k
Adopting Sorbet at Scale
ufuk
73
9.1k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
It's Worth the Effort
3n
183
28k
How GitHub (no longer) Works
holman
311
140k
RailsConf 2023
tenderlove
29
940
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ɺͲͷαϒ λΠϓؔΛͬͯܕνΣοΫΛ௨͔ͨ͠Λ֮ ͓͍͑ͯͯɺ͋ͱͰ࣮ࡍͷมॲཧΛૠೖ͢Δɻ
͓ΘΓ