COMP 520: Compilers!
Lecture 11: Contextual Analysis Continued!
1
Announcements + Logistics
2
Type Checking - Theory!
3
Type Checking: Review
4
Type-Checking Table?
𝛼 × 𝛽 → 𝛾
E.g., int × int → int
5
Type-Checking Table?
𝛼 × 𝛽 → 𝛾
E.g., int × int → int
6
Type-Checking Table?
( 𝛽 × 𝛼 × op ) → 𝛾 �
E.g., int × int × {+ ,-,*,/} → int
int × int × {== ,!=,>,>=,<,<=} → boolean
7
Operator Overloading
Operator Overloading: give custom definitions to ops
Example, consider a “MyData” object that can accumulate integers and sorts them.
In C++, can create a method that defines:
MyData × int × {+} → MyData
Where adding integers to MyData types will call that method
8
Bad Types
9
Bad Types
Consider miniJava:
(myObj + 3) + 5;
ClassType × int × {+} → ErrorType (report error here)
ErrorType × int × {+} → ErrorType (no need to report)
Second type check was valid.
10
Bad Types
Generalization:
( {ErrorType} × AnyType × AnyOp ) → ErrorType
( AnyType × {ErrorType} × AnyOp ) → ErrorType
( (TypeA,TypeB,Op) ∉ TypeTable ) → (Result=ErrorType)
( (TypeA,TypeB,Op) ∈ TypeTable ) → ( Result = GetEntry(TypeA,TypeB,Op) )
11
Does Order Matter?
12
TypeCasting / Type Conversions
𝛼 × 𝛽 → 𝛼
Thus, the table for typecasting is:
(TypeA,TypeB) → TypeA
E.g. ( (int)3.4f ) → 3 (an int), which is (int,float)→int
13
TypeCasting / Type Conversions
𝛼 × 𝛽 → 𝛼
14
Automatic Conversions
15
Automatic Conversions
16
Automatic Conversions
private int somefun( int a ) { … }
…
somefun( true );
… does not require explicitly typecasting (int)true
17
Automatic Conversions
18
Automatic Type Casting Note
19
Manual Typecasting
20
Manual Typecasting
21
Please note: miniJava
22
Type Checking - PA3
23
miniJava TypeChecking
24
Simple Approach to TypeChecking
25
Type-Checking Strategy Details
26
Type-Checking Strategy Details
27
Type Values at Leaves
28
Type Checking in PA3
29
Special Types
30
Type-Checking Table
31
miniJava – Types must match
32
miniJava – Types must match
33
miniJava – Types must match
34
Type-Checking Table (2)
35
Type Checking Rules | ||
Operand Types | Operand | Result |
| &&, || | boolean |
| >, >=, <, <= | boolean |
| +, -, *, / | int |
| ==, != | boolean |
int | (Unary) - | int |
boolean | (Unary) ! | boolean |
ClassType
36
ClassType (No polymorphism in miniJava)
37
What type is ArrayType?
38
What type is ArrayType?
39
Type-Checking Methods
40
Type-Checking Methods
41
Back to Identification
42
Pre-defined Names
��
43
Out-of-order References
44
Out-of-order References
45
Strategy: OOO References
46
Strategy: OOO References
Alternatively: check private later when resolving declarations. (this is probably easier than changing your level 1)
47
Identification of QualRef
48
Left-most Reference
49
Left-most Reference
50
Left-most Reference
51
Left-most Reference
52
QualRef Strategy
53
No one strategy dominates all others
54
A.b, where A is also a class name
55
A.b, where A is also a class name
56
A.b, where A is also a class name
57
Static Reference
58
Static Context
59
Other Contextual Constraints
60
Contextual Analysis
61
Contextual Analysis
62
Not allowed!
Contextual Analysis
63
Not allowed!