Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Â
A Deeper Deep Dive into Swift Literal
1. A Deeper Deep dive into Swift Literals
freddi from LINE Fukuoka
at try! Swift NYC 2019 (9th.Sep)
2. About Me
freddi (Yuki Aki) from !
Developing LINE Creators Studio
at LINE Fukuoka
Organizer of HAKATA.swift
3. - What is a Literal in Computer Programming?
- Why I want you to deep dive into Swift Literals?
- Literals in Swift
- Object of Literals
- Architecture of Type from Literals
- Conclusion
Agenda
High-Level
Low-Level
5. What is a Literal in Computer Programming?
What is a Literal in Computer Programming?
Notations for representing a fixed value in source code.
Values we write in a conventional form whose is obvious
6. // Integer Literal
let assigningIntegerLiteral = 42
// String Literal
let assigningStringLiteral = "try! Swift"
What is a Literal in Computer Programming?
7. Literal is a minimal way to
represent the actual data for Programmer
What is a Literal in Computer Programming?
8. Why I want you to
deep-dive into Swift Literals?
9. Why I want you to deep-dive into Swift Literals?
We have more chances to touch low level Swift
compared to Other Languages
10. Why I want you to deep-dive into Swift Literals?
12. Compiler Code looks too difficult and is too long for reading
Why I want you to deep-dive into Swift Literals?
13. Why I talk about Literals?
Why I want you to deep-dive into Swift Literals?
14. Why I talk about Literals?
I learned Swift Compiler things
through understanding deep side of Swift Literals
Why I want you to deep-dive into Swift Literals?
18. Literals in Swift (Basic Literals with nested Type)
// Optional from Nil Literal
let assigningNilLiteral: Int? = nil
// Array Literal
let assigningArrayLiteralWithInt: [Int] = [42]
// Dictionary Literal
let assigningDictLiteral: [String: Int] = [:]
19. Context of Nested Values is necessary
- Nil Literal â null value
- Array Literal â sequencial value
- Dictionary Literal â value with key
Literals in Swift (Basic Literals with nested Type)
20. Literals in Swift (Basic Literals with nested Type)
// Not declared what type you want to make optional
â let assigningNilLiteral = nil
// Not declared what element type
â let assigningArrayLiteral = []
21. Literals in Swift (Special Literals)
// Color Literal
let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
// Image Literal
let image = #imageLiteral(resourceName: âcatâ)
// File Reference Literal
let file = #fileLiteral(resourceName: âcat.txtâ)
22. Swift also provides Special Literals for resources
- Color Literal â color
- Image Literal â image
- File Reference Literal â file location
Literals in Swift (Special Literals)
23. Literals in Swift (Special Literals)
Color/Image/File Reference Literal are
- rendered as Actual Data in Xcode
- File â select from file manager
- Color â adjusting color code
24. // Color Literal
let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
Literals in Swift (Special Literals)
25. Literals in Swift (Special Literals at Xcode!)
// Color Literal
let color =
30. Literals are a minimal way to represent value by programmer
There are many Literals in Swift,not only usual Literal!
- Integer,Array,Nil,Color etcâŠ
Literals in Swift
32. Object of Literals
The Ways of determining Type of value are âŠ
- Type Annotation
- Type Inference with Default Type of Literal
33. // Integer Literal
// assigningIntegerLiteral is Int
let assigningIntegerLiteral: Int = 42
Literals get Type from annotation after colon(:)
Object of Literals(Type Annotation)
34. // Integer Literal
// assigningIntegerLiteral is Float
let assigningIntegerLiteral: Float = 42
Literals get Type from annotation after colon(:)
Object of Literals(Type Annotation)
35. Object of Literals
The Ways of determining Type of value are âŠ
- Type Annotation
- Type Inference with Default Type of Literal
36. Object of Literals (Type Inference)
// Integer Literal
// assigningIntegerLiteral is Int
let assigningIntegerLiteral = 42
Each Literal has Default Type,can be inferenced
37. Object of Literals (Special Literals)
// Image Literal
let image = #imageLiteral(resourceName: âcatâ)
type(of: image) // UIImage at UIKit, NSImage at AppKit
AppKit UIKit
Image Literal NSImage UIImage
Color Literal NSColor UIColor
39. Swiftâs Compiling Flow
42 SIL(Swift Intermediate Language)
Abstract Syntax Tree(AST)
AST With Type information
40. Swiftâs Compiling Flow
42 SIL(Swift Intermediate Language)
Abstract Syntax Tree(AST)
AST With Type information
Front-End of Compiler
Back-End of Compiler
41. 42 SIL(Swift Intermediate Language)
Abstract Syntax Tree(AST)
AST With Type information
Swiftâs Compiling Flow
42. SIL (Swift Intermediate Language)
Intermediate Language between Swift and LLVM IR
- Swift code will be converted SIL after AST
- Some of optimization are operated at SIL
43. Format of SIL
SIL is conforming Static Single Assignment format
%a = some value 1 â
%b = some value 2 â
%a = some value 3 â
Value
- Object is assigned to value at each line
- Assigning to one value is allowed only once
45. SIL (Swift Intermediate Language)
Why do we need SIL when deep dive into Literals?
We can find how Swift Code is treated by Compiler
46. SIL (Swift Intermediate Language)
Why do we need SIL when deep dive into Literals?
We can find how Swift Code is treated by Compiler
More easy to read SIL than reading compiler code
48. let assigningLiteral: Int = 42
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Letâs read SIL
49. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
let assigningLiteral: Int = 42
Letâs read SIL
50. let assigningLiteral: Int = 42
Allocation of memory to %3
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Letâs read SIL
51. $Builtin.Int64 object is assigned %4
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
let assigningLiteral: Int = 42
Letâs read SIL
52. At first,Some Literals are âŠ
%4 = integer_literal $Builtin.Int64, 42
Literal Object
- treated as âLiteral Objectâ starts with $Builtin.***
- $Builtin.*** will be used in Actual Type Initializer
53. %4 = integer_literal $Builtin.Int64, 42
Literal Object
At first,Some Literals are âŠ
- treated as âLiteral Objectâ starts with $Builtin.***
- $Builtin.*** will be used in Actual Type Initializer
Un-official name!;(
59. $Builtin.Int64 object is assigned %4
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
let assigningLiteral: Int = 42
Letâs read SIL
60. let assigningLiteral: Int = 42
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Letâs read SIL
$Builtin.Int64 object is assigned %4
61. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object (%4) is passed to Int Initializer
Letâs read SIL let assigningLiteral: Int = 42
62. Some Type which can be generated from Literal
Letâs read SIL
- has initializer which receives Literal Object
- Initializer of default Type is used,if type not annotated
63. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object (%4) is passed to Int Initializer
Letâs read SIL let assigningLiteral: Int = 42
64. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
The generated Int Object is assigned to allocated place
let assigningLiteral: Int = 42Letâs read SIL
71. Swift Literals are treated as Literal Object at first
- Literal Object will be used for Initializers of actual Swift Type
Object of Literals
%5 = struct $Int (%4 : $Builtin.Int64)
73. Literal Object
At first,Some Literals are treated as âLiteral Objectâ
42
$Builtin.Int64
4.2
$Builtin.FPIEEE64
â42â
$Builtin.RawPointer
$Builtin.Word
Integer Float String
74. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object is passed to Int Initializer
Initializer which receives Literal Object
75. Implementation of this initializer is required by
_ExpressibleByBuiltin***Literal protocol
%5 = struct $Int (%4 : $Builtin.Int64)
Initializer which receives Literal Object
77. What is _ExpressibleByBuiltin***Literal protocol?
_ExpressibleByBuiltin***Literal
public protocol _ExpressibleByBuiltinIntegerLiteral {
init(_builtinIntegerLiteral value: Builtin.IntLiteral)
}
init(:Builtin.IntLiteral) is called on assigning Literal by =
- Type which conforming with this protocol is convertible by Literal Object
79. ExpressibleBy***Literal
You may know ExpressibleBy***Literal protocol
- makes Type be convertible by each Literal,using =
- ExpressibleByIntegerLiteral
- ExpressibleByFloatLiteral
- ExpressibleByBooleanLiteral âŠ
80. struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}
âš
let value: OriginalIntegerType = 10
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
81. struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}
âš
let value: OriginalIntegerType = 10
1.Set typealias to IntegerLiteralType
the type should be converted from Integer Literal
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
82. struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}
âš
let value: OriginalIntegerType = 10
2.Implement Initializer
which receives IntegerLiteralType value
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
83. struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}
âš
let value: OriginalIntegerType = 10
You can initialize your type by Literal!
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
84. ExpressibleBy***Literal
public struct CGFloat {
#if arch(i386) || arch(arm)
public typealias NativeType = Floatâš
#elseif arch(x86_64) || arch(arm64) || âŠ
public typealias NativeType = Double
...âš
typealias FloatLiteralType = NativeType
CGFloat is using ExpressibleByFloatLiteral
85. struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}
âš
let value: OriginalIntegerType = 10
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
1.Set typealias to IntegerLiteralType
the type should be converted from Integer Literal
86. Associated Type in ExpressibleBy***Literal should conform
with _ExpressibleByBuiltin***Literal protocol
public protocol ExpressibleByIntegerLiteral {
associatedtype IntegerLiteralType: _ExpressibleByBuiltinIntegerLiteral
ExpressibleBy***Literal
87. Flow of Converting Literal
Literal Object ( = Builtin.***)
Swift Type
Literal
88. Swift Type
Literal Object ( = Builtin.***)
Swift Type: _ExpressibleByBuiltin***
Swift Type: ExpressibleBy***
Literal
Optional â
Flow of Converting Literal
95. // static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
1.Getting _value from left value and right value
Literal Object
== operator on Int
96. // static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
2. Passing each _value to Back-End function
Literal Object
== operator on Int
97. Swift Type from Literal
Swiftâs Compiling Flow
Front-End of Compiler
Back-End of Compiler
$Builtin.*** Back-End Object
98. // static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
3.Return result as Bool in Swift
Literal Object
== operator on Int
99. Swift Type
Literal Object ( = Builtin.***)
Swift Type: _ExpressibleByBuiltin***
Swift Type: ExpressibleBy***
Literal
Flow of Converting Literal
100. Literal
Swift Type: ExpressibleBy***
Swift Type: _ExpressibleByBuiltin***
Literal Object ( = Builtin.***)
Architecture of Type from Literals
Int, String âŠ
CGFloat âŠ
102. Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Architecture of Type from Literals
103. $Builtin.Int64
Int UInt
Architecture of Type from Literals
Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
104. Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Architecture of Type from Literals
105. Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Architecture of Type from Literals
Swift Type from Literal
$Builtin.*** Back-End Object
107. Literals in Swift
Literals are a minimal way to represent value by programmer
There are many Literals in Swift,not only usual Literal!
- Integer,Array,Nil,Color etcâŠ
108. Literal Object
At first,Some Literals are treated as âLiteral Objectâ
42
$Builtin.Int64
4.2
$Builtin.FPIEEE64
â42â
$Builtin.RawPointer
$Builtin.Word
Integer Float String
109. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object is passed to Int Initializer
Initializer which receives Literal Object
110. Architecture of Type from Literals
Literal
Swift Type: ExpressibleBy***
Swift Type: _ExpressibleByBuiltin***
Literal Object ( = Builtin.***)
Int, String âŠ
CGFloat âŠ