ukitaka
March 30, 2018
1.5k

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

March 30, 2018

## Transcript

5. ### ษڧ͕ͯΒ࡞ͬͨ΋ͷ • SwiftͰܕγεςϜ / ܕਪ࿦Λॻ͍ͯΈͨΓ • ukitaka/TypeSystems • ukitaka/AlgorithmW.swift •

SILOptimizerΛࣗ࡞ͯ͠ΈͨΓ • ukitaka/sil-opt-scala

10. ### swift/lib/Sema • Sema = Semantic Analysis (ҙຯղੳ) • ओʹܕਪ࿦ɾܕݕࠪΛ୲౰͢ΔϞδϡʔϧ •

ίʔυྔҰ൪ଟ͍ • ΊͪΌΊͪΌෳࡶ

16. ### Generics in Swift • δΣωϦΫεपΓͷ࣮૷͸ͱʹ͔͘೉͍͠… • ͔͠͠(੍໿Λؚ·ͳ͍)δΣωϦοΫͳؔ਺ͩ ͚ͳΒܕਪ࿦ؚΊཧ࿦΋࣮૷΋ͦΜͳʹ೉͘͠ ͳ͍ •

δΣωϦοΫͳؔ਺ͱͦͷܕਪ࿦ΛݟΔ͜ͱͰɺ SemaϞδϡʔϧΛ၆ᛌ͢Δ

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

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

\$T1 \$ swift -frontend -typecheck -debug-constraints test.swift \$T0 ͸੔਺Ϧςϥϧ \$T0 ͸\$T1ʹม׵Ͱ͖Δ
28. ### ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ 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
29. ### ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ ---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

33. ### δΣωϦοΫͳؔ਺ͷܕਪ࿦ ಺෦తͳܕͷදݱ: <τ_0_0> τ_0_0 → τ_0_0 ݟͨ໨ͷܕ: <A> A →

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

-> \$T4 \$ swift -frontend -typecheck -debug-constraints test.swift ͜Ε͕ଟ૬ੑΛ࣮ݱ͍ͯ͠ΔϙΠϯτ \$T1 -> \$T1 \$T4 -> \$T4
35. ### δΣωϦοΫͳؔ਺ͷܕਪ࿦ 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
36. ### δΣωϦοΫͳؔ਺ͷܕਪ࿦ • ͋ͱ͸ಉ͡Α͏ʹ੍໿ੜ੒ͯ͠ղ͚ͩ͘ ---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

40. ### ܕνΣοΫؔ࿈ͷओཁͳΫϥε • ίʔυྔଟ͍͚ͲɺओͳΫϥε͸2ͭͷΈ • TypeChecker → ܕݕࠪ୲౰ • ConstraintSystem →

ܕਪ࿦୲౰ • TypeChecker͕Expr͝ͱʹConstraintSystemΛ ΠϯελϯεԽͯ͠࢖͍ͬͯΔΠϝʔδ

44. ### ੍໿ͷ୯ҰԽ (solve) • جຊతʹ͸ީิΛͲΜͲΜࢼ͍ͯ͘͠ • PotentialBindings ( e.g. ੔਺Ϧςϥϧ͔ͩΒIntͩΖ͏ )

• Overloadͷީิͨͪ • ConstraintGraphͱ͍͏(ϋΠύʔ)άϥϑͰ੍໿ Λ؅ཧ • େ·͔ʹ͸BindingΛ௥Ճ → άϥϑΛ੔ཧͷ܁Γฦ͠

48. ### ͓·͚: ੍໿෇͖ͷδΣωϦΫε͸ʁ • ࣮૷ͱͯ͠͸جຊతʹ͸ಉ͡ • TʹׂΓ౰ͯΒΕͨܕม਺ʹ \$T1 conforms to EquatableͷΑ͏ͳ੍໿͕ՃΘΔ͘Β͍

• ͔͠͠എܠʹ͋Δཧ࿦͸·ͨผ…
49. ### ͓·͚: ੍໿෇͖ͷδΣωϦΫε͸ʁ • ݶఆܕ (Qualiﬁed type) • TaPLʹ͸໊લ͔͠ࡌͬͯͳ͍ • Jones,

Mark P. Qualiﬁed Types: Theory and Practice. Cambridge University Press, 1994. • Haskellͷ Eq a => a -> a -> Bool • ༗քྔԽ(bounded qualiﬁcation)Ͱ͸ͳ͍

52. ### T is subtype of Optional<T> • ౴͑: αϒλΠϓɻ • αϒλΠϐϯάنଇ͸υΩϡϝϯτʹ͸ॻ͍

͍ͯͳ͍͕ɺιʔείʔυதͷίϝϯτʹͪΐ ͪ͜ΐ͜ఆٛ͞Ε͍ͯΔɻ(͢΂͕ͯॻ͍ͯ͋ΔΘ͚ Ͱ͸ͳ͍) • lib/Sema/CSSimplify.cpp Λࢀর

54. ### αϒλΠϓؔ܎ • ͲΜͳαϒλΠϓؔ܎Λ࣋ͨͤΔ͔͸ͦͷݴޠͰ ͷܾΊͷ໰୊ • ྫ͑͹ Int < Float ͱ͔

Int < String ͱ͔࣋ͨͤͯ ΋(੔߹ੑ͕औΕΔͷͳΒ) ྑ͍ • Swift͸OptionalΛָʹѻ͏࢓૊Έͱͯ͠ ɺ T < Optional<T>Λ࠾༻͍ͯ͠Δ

62. ### ͜ͷ໰୊ʹର͢ΔΞϓϩʔν αϒλΠϐϯάͷ࣮૷ํࣜ͸େ͖͘෼͚ͯ2ͭ 1. inclusiveͳ࣮૷ • ڞ௨ͷ࣮ߦ࣌ͷදݱΛ࢖͏ • JavaͳͲ 2.coerciveͳ࣮૷ •

ม׵ॲཧ(ܕڧ੍)ΛڬΈࠐΉ ← Swift͸ͬͪ͜
63. ### ܕڧ੍ҙຯ࿦(coercion semantics) • ؆୯ʹݴ͏ͱɺ̍ͭͷαϒλΠϐϯάنଇʹ͍ͭͯɺ1 ͭͷม׵ॲཧ(ܕڧ੍)͕ଘࡏ͢Δɻ • TypeChecker͸ɺͲͷαϒλΠϓؔ܎Λ࢖ͬͯܕνΣο ΫΛ௨͔ͨ͠Λه࿥͓͍ͯͯ͠ɺ͋ͱͰ࣮ࡍͷม׵ॲཧ Λૠೖ͢Δɻ •

lib/Sema/CSApply.cppͷExprRewriterͱ͍͏Ϋϥε Λࢀߟɻ

66. ### ྫ: Optionalͷ৔߹ Constraint restrictions: Int to Optional<Int> is [value-to-optional] \$

swift -frontend -typecheck -debug-constraints test.swift
67. ### ྫ: Optionalͷ৔߹ \$ swiftc -dump-ast test.swift (inject_into_optional implicit type=‘Optional<Int>’ …

(declref_expr type=‘Int' … ) )
68. ### ྫ: 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ͰแΉʯ

71. ### ྫ: Classͷ৔߹ Constraint restrictions: Dog to Animal is [superclass] \$

swift -frontend -typecheck -debug-constraints test.swift
72. ### ྫ: Classͷ৔߹ \$ swiftc -dump-ast test.swift (derived_to_base_expr implicit type=‘Animal’ …

(declref_expr type=‘Dog' … ) )
73. ### ྫ: 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) ͭ·ΓରԠ͢Δܕڧ੍͸ʮΞοϓΩϟετʯ
74. ### ྫ:Existentialͷ৔߹ աఔ͸লུ (erasure_expr implicit type=‘Animal’ … (declref_expr type=‘Dog' … )

) ରԠ͢Δܕڧ੍͸ʮଘࡏܕ΁ͷύοέʔδԽʯ ( = type erase)