Upgrade to Pro — share decks privately, control downloads, hide ads and more …

理論から入門するswift/lib/Sema - わいわいswiftc #1

ukitaka
March 30, 2018

理論から入門するswift/lib/Sema - わいわいswiftc #1

https://iosdiscord.connpass.com/event/82436/
での発表資料です。

ukitaka

March 30, 2018
Tweet

More Decks by ukitaka

Other Decks in Programming

Transcript

  1. ܕਪ࿦ͷେ·͔ͳྲྀΕ • ܕ͕ܾ·͍ͬͯͳ͍ͱ͜ΖΛܕม਺Ͱ͓͘ Type Variables: #0 = $T0 [inout allowed]

    #1 = $T1 [inout allowed] $ swift -frontend -typecheck -debug-constraints test.swift $T1 $T0
  2. • ੍໿(Constraint)Λੜ੒͢Δ ܕਪ࿦ͷେ·͔ͳྲྀΕ $T0 literal conforms to ExpressibleByIntegerLiteral $T0 conv

    $T1 $ swift -frontend -typecheck -debug-constraints test.swift $T0 ͸੔਺Ϧςϥϧ $T0 ͸$T1ʹม׵Ͱ͖Δ
  3. ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ 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
  4. ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ ---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
  5. δΣωϦοΫͳؔ਺ͷܕਪ࿦ ಺෦తͳܕͷදݱ: <τ_0_0> τ_0_0 → τ_0_0 ݟͨ໨ͷܕ: <A> A →

    A Aͱ͍͏໊લ͸ࣺͯΒΕͯdepthͱindexʹΑΔ໊ແ͠දݱʹͳ͍ͬͯΔ
  6. δΣωϦοΫͳؔ਺ͷܕਪ࿦ ग़ݱͷͨͼʹτ_0_0ʹ৽͍͠ܕม਺ΛׂΓ౰ͯΔ Opened types: opens τ_0_0 -> $T1 opens τ_0_0

    -> $T4 $ swift -frontend -typecheck -debug-constraints test.swift ͜Ε͕ଟ૬ੑΛ࣮ݱ͍ͯ͠ΔϙΠϯτ $T1 -> $T1 $T4 -> $T4
  7. δΣωϦοΫͳؔ਺ͷܕਪ࿦ 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
  8. δΣωϦοΫͳؔ਺ͷܕਪ࿦ • ͋ͱ͸ಉ͡Α͏ʹ੍໿ੜ੒ͯ͠ղ͚ͩ͘ ---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
  9. ܕνΣοΫؔ࿈ͷओཁͳΫϥε • ίʔυྔଟ͍͚ͲɺओͳΫϥε͸2ͭͷΈ • TypeChecker → ܕݕࠪ୲౰ • ConstraintSystem →

    ܕਪ࿦୲౰ • TypeChecker͕Expr͝ͱʹConstraintSystemΛ ΠϯελϯεԽͯ͠࢖͍ͬͯΔΠϝʔδ
  10. ੍໿ͷ୯ҰԽ (solve) • جຊతʹ͸ީิΛͲΜͲΜࢼ͍ͯ͘͠ • PotentialBindings ( e.g. ੔਺Ϧςϥϧ͔ͩΒIntͩΖ͏ )

    • Overloadͷީิͨͪ • ConstraintGraphͱ͍͏(ϋΠύʔ)άϥϑͰ੍໿ Λ؅ཧ • େ·͔ʹ͸BindingΛ௥Ճ → άϥϑΛ੔ཧͷ܁Γฦ͠
  11. ͓·͚: ੍໿෇͖ͷδΣωϦΫε͸ʁ • ݶఆܕ (Qualified type) • TaPLʹ͸໊લ͔͠ࡌͬͯͳ͍ • Jones,

    Mark P. Qualified Types: Theory and Practice. Cambridge University Press, 1994. • Haskellͷ Eq a => a -> a -> Bool • ༗քྔԽ(bounded qualification)Ͱ͸ͳ͍
  12. T is subtype of Optional<T> • ౴͑: αϒλΠϓɻ • αϒλΠϐϯάنଇ͸υΩϡϝϯτʹ͸ॻ͍

    ͍ͯͳ͍͕ɺιʔείʔυதͷίϝϯτʹͪΐ ͪ͜ΐ͜ఆٛ͞Ε͍ͯΔɻ(͢΂͕ͯॻ͍ͯ͋ΔΘ͚ Ͱ͸ͳ͍) • lib/Sema/CSSimplify.cpp Λࢀর
  13. αϒλΠϓؔ܎ • ͲΜͳαϒλΠϓؔ܎Λ࣋ͨͤΔ͔͸ͦͷݴޠͰ ͷܾΊͷ໰୊ • ྫ͑͹ Int < Float ͱ͔

    Int < String ͱ͔࣋ͨͤͯ ΋(੔߹ੑ͕औΕΔͷͳΒ) ྑ͍ • Swift͸OptionalΛָʹѻ͏࢓૊Έͱͯ͠ ɺ T < Optional<T>Λ࠾༻͍ͯ͠Δ
  14. ྫ: 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ͰแΉʯ
  15. ྫ: Classͷ৔߹ Constraint restrictions: Dog to Animal is [superclass] $

    swift -frontend -typecheck -debug-constraints test.swift
  16. ྫ: 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) ͭ·ΓରԠ͢Δܕڧ੍͸ʮΞοϓΩϟετʯ