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
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Quine, Polyglot, 良いコード
qnighy
4
640
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
Remix on Hono on Cloudflare Workers
yusukebe
1
280
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Macとオーディオ再生 2024/11/02
yusukeito
0
370
CSC509 Lecture 09
javiergs
PRO
0
140
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Designing the Hi-DPI Web
ddemaree
280
34k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Typedesign – Prime Four
hannesfritz
40
2.4k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
GraphQLとの向き合い方2022年版
quramy
43
13k
Code Reviewing Like a Champion
maltzj
520
39k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
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ɺͲͷαϒ λΠϓؔΛͬͯܕνΣοΫΛ௨͔ͨ͠Λ֮ ͓͍͑ͯͯɺ͋ͱͰ࣮ࡍͷมॲཧΛૠೖ͢Δɻ
͓ΘΓ