+ All Categories
Home > Documents > CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science &...

CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science &...

Date post: 19-Dec-2015
Category:
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
68
CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Chap 6: Type Checking/Semantic Analysis Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield Way, Unit 2155 Storrs, CT 06269-3155 [email protected] http://www.engr.uconn.edu/~steve (860) 486 - 4818 Material for course thanks to: Laurent Michel Aggelos Kiayias Robert LeBarre
Transcript
Page 1: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.1

CSE4100

Chap 6: Type Checking/Semantic AnalysisChap 6: Type Checking/Semantic Analysis

Prof. Steven A. Demurjian Computer Science & Engineering Department

The University of Connecticut371 Fairfield Way, Unit 2155

Storrs, CT [email protected]

http://www.engr.uconn.edu/~steve(860) 486 - 4818

Material for course thanks to:Laurent MichelAggelos KiayiasRobert LeBarre

Page 2: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.2

CSE4100

OverviewOverview Type Checking and Semantic Analysis is a Critical Type Checking and Semantic Analysis is a Critical

Features of Compilers and CompilationFeatures of Compilers and Compilation Passing a Syntax Check (Parsing) not SufficientPassing a Syntax Check (Parsing) not Sufficient

Type Checking Provides Vital Input Software Engineers Assisted in Debugging Process

We’ll Focus on Classical Type Checking IssuesWe’ll Focus on Classical Type Checking Issues Background and Motivation Type Analysis The Notion of a Type System Examining a Simple Type Checker Other Key Typing Concepts

Concluding Remarks/Looking AheadConcluding Remarks/Looking Ahead

Page 3: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.3

CSE4100

Background and MotivationBackground and Motivation Recall....Recall....

Page 4: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.4

CSE4100

Background and MotivationBackground and Motivation What we have achievedWhat we have achieved

All the “words” (Tokens) are known The tree is syntactically correct

What we still do not know...What we still do not know... Does the program make sense ?

What we will not try to find outWhat we will not try to find out Is the program correct ? This is Impossible!

Our Concern:Our Concern: Does it Compile? Are all Semantic Errors Removed? Do all Types and their Usage Make Sense?

Page 5: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.5

CSE4100

Background and MotivationBackground and Motivation The program makes “sense”The program makes “sense”

Programmer’s intent is clear Program is semantically unambiguous

Data-wise– We know what each name denotes

– We know how to represent everything

Flow-wise– We know how to execute all the statements

Structure-wise– Nothing is missing

– Nothing is multiply defined The program is correctThe program is correct

It will produce the expected input

Page 6: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.6

CSE4100

Tasks To PerformTasks To Perform Scope AnalysisScope Analysis

Figure out what each name refers to Understand where Scope Exists (See Chapter 7)

Type AnalysisType Analysis Figure out the type of each name Names are functions, variables, types, etc.

Completeness AnalysisCompleteness Analysis Check that everything is defined Check that nothing is multiply defined

Page 7: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.7

CSE4100

Output ?Output ? What the analysis produceWhat the analysis produce

Data structures “on the side” To describe the types(resolve the types) To describe what each name denotes (resolve the

scopes) A Decorated tree

Add annotations in the tree Possibly.... Semantic Errors!

Page 8: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.8

CSE4100

PictoriallyPictorially

Page 9: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.9

CSE4100

Type AnalysisType Analysis PurposePurpose

Find the type of every construction Local variables Actuals for calls Formals of method calls Objects Methods Expressions

RationaleRationale Types are useful to catch bugs!

Page 10: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.10

CSE4100

Type AnalysisType Analysis Why Bother ?Why Bother ?

A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kind of values they compute.

Page 11: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.11

CSE4100

UsesUses Many!Many!

Error detection Detect early Detect automatically Detect obvious and subtle flaws

Abstraction The skeleton to provide modularity Signature/structure/interface/class/ADT/....

Documentation Program are easier to read

Language Safety guarantee Memory layout Bound checking

Efficiency

Page 12: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.12

CSE4100

How It works ?How It works ? Classify programs according to the kind of values Classify programs according to the kind of values

computedcomputed

Set of All Programs

Set of All Reasonable Programs

Set of All Type-Safe ProgramsSet of All Correct

Programs

Page 13: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.13

CSE4100

How do we do this ?How do we do this ? Compute the type of every sentenceCompute the type of every sentence

On the tree With a tree traversal

Some information will flow up (synthesized) Some information will flow down (inherited)

Questions to answerQuestions to answer What is a type ? How do I create types ? How do I compute types ?

Page 14: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.14

CSE4100

Types...Types... Types form a language!Types form a language!

With Terminals... Non-terminals.... And a grammar!

AlternativelyAlternatively Types can be defined inductively

Base types (a.k.a. the terminals) Inductive types (a.k.a. grammatical productions)

Page 15: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.15

CSE4100

Base TypesBase Types What are the base types ?What are the base types ?

int float double char void bool error

Page 16: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.16

CSE4100

Inductive Type DefinitionInductive Type Definition PurposePurpose

Define a type in terms of other simple/smaller types ExampleExample

array pointer reference Pair (products in the book) structure function methods classes ...

Page 17: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.17

CSE4100

Relation to Grammar ?Relation to Grammar ?

Type → array ( Type , Type )→ pair ( Type , Type )→ tuple ( Type+ )→ struct ( FieldType+ )→ fun ( Type ) : Type→ method ( ClassType , Type ) : Type→ pointer( Type )→ reference( Type )→ ClassType→ BasicType

ClassType → class ( name [ , Type ] )FieldType → name : TypeBasicType → int | bool | char | float | double | void | error

Page 18: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.18

CSE4100

Type TermsType Terms What is that ?What is that ?

It is a sentence in the type language ExampleExample

int pair(int,int) tuple(int,bool,float) array(int,int) fun(int) : int fun(tuple(int,char)) : int class(“Foo”) method(class(“Foo”), tuple(int,char)) : int

Page 19: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.19

CSE4100

So...So... IfIf

we have Type Term we have a Type Language

We can parse it and obtain....We can parse it and obtain.... Type Trees!

fun(tuple(int,char)) : int

Page 20: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.20

CSE4100

The Notion of a Type SystemThe Notion of a Type System Logical Placement of Type Checker:Logical Placement of Type Checker:

Role of Type Checker is to Verify Semantic ContextsRole of Type Checker is to Verify Semantic Contexts Incompatible Operator/Operands Existence of Flow-of Control Info (Goto/Labels) Uniqueness w.r.t. Variable Definition, Case

Statement Labels/Ranges, etc. Naming Checks Across Blocks (Begin/End) Function Definition vs. Function Call

Type Checking can Occur as “Side-Effect” of Parsing Type Checking can Occur as “Side-Effect” of Parsing via a Judicious Use of Attribute Grammars!via a Judicious Use of Attribute Grammars!

Employ Type SynthesisEmploy Type Synthesis

ParserType

CheckerInt. CodeGenerator

TokenStream

SyntaxTree

SyntaxTree

Interm.Repres.

Page 21: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.21

CSE4100

Example of type synthesisExample of type synthesis Assume the programAssume the program

if (1+1 == 2) then 1 + 3 else 2 * 3

Page 22: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.22

CSE4100

Yes... But....Yes... But.... What about identifiers ?What about identifiers ? Key IdeaKey Idea

Type for identifiers are inherited attributes! Inherits

– From the definition

– To the use site.

int n;....if (n == 0) then 1 else n

Page 23: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.23

CSE4100

ExampleExample

int n;....if (n == 0) then 1 else n

Page 24: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.24

CSE4100

The Notion of a Type SystemThe Notion of a Type System Type System/Checker Based on:Type System/Checker Based on:

Syntactic Language Construct The Notion of Types Rules for Assigning Types to Language Constructs Strength of Type Checking (Strong vs. Weak)

Strong vs. Weak Dynamic vs. Static OODBS/OOPLS Offer Many Variants

All Expression in Language MUST have Associated All Expression in Language MUST have Associated TypeType Basic (int, real, char, etc.) Constructed (from basic and constructed types)

How are Type Expression Defined and Constructed?How are Type Expression Defined and Constructed?

Page 25: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.25

CSE4100

Type ExpressionsType Expressions A Basic Type is a Type ExpressionA Basic Type is a Type Expression

Examples: Boolean, Integer, Char, Real Note: TypeError is Basic Type to Represent Errors

A Type Expression may have a Type Name which is A Type Expression may have a Type Name which is also a Type Expressionalso a Type Expression

A Type Constructor Applied to Type Expression A Type Constructor Applied to Type Expression Results in a Type ExpressionResults in a Type Expression Array(I,T): I is Integer Range, T is Type Expr. Product: T1T2 is Type Expr if T1, T2 Type Exprs. Record: Tuple of Field Names & Respective Type Pointer(T): T is a Type Expr., Pointer(T) also

Page 26: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.26

CSE4100

Type ExpressionsType Expressions A Type Constructor Applied to Type Expression A Type Constructor Applied to Type Expression

Results in a Type Expression (Continued)Results in a Type Expression (Continued) Functions:

May be Mathematically Characterized with Domain Type D and Range Type R

F: D R int int int char char pointer(int)

A Type Expression May Contain Variables whose A Type Expression May Contain Variables whose Values are Type ExpressionsValues are Type Expressions Called Type Variables We’ll Omit from our Discussion …

Page 27: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.27

CSE4100

Key Issues for Type SystemKey Issues for Type System Classical Type System ApproachesClassical Type System Approaches

Static Type Checking (Compile Time) Dynamic Type Checking (Run Time) How is each Handled in C? C++? Java?

Language Level Issues:Language Level Issues: Sound Type System (ML)

No Dynamic Type Checking is Required All Type Errors are Determined Statically

Strongly Typed Language (Java, Ada) Compiler Guarantees no Type Errors During Execution

Weakly Typed Language (C, LISP) Allows you to Break Rules at Runtime

What about Today’s Web-based Languages? What about Today’s Web-based Languages?

Page 28: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.28

CSE4100

The Notion of a Type SystemThe Notion of a Type System Types System: Rules Used by the Type Checker to Types System: Rules Used by the Type Checker to

Asign Types to Expressions and Verify ConsistencyAsign Types to Expressions and Verify Consistency Type Systems are Language/Compiler DependentType Systems are Language/Compiler Dependent

Different Versions of Pascal have Different Type Systems

Same Language Can have Multiple Levels of Type Systems (C Compiler vs. Lint in Unix)

Different Compilers for Same Language May Implement Type Checking Differently GNU C++ vs. MS Studio C++ Sun Java vs. MS Java (until Sun forced off market)

What are the Key Issues?What are the Key Issues?

Page 29: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.29

CSE4100

First Example: Simple Type CheckerFirst Example: Simple Type Checker Consider Simplistic Language:Consider Simplistic Language:

What does this Represent?What does this Represent?

P → D ; EP → D ; ED → D ; D | D → D ; D | id: id: TTT → T → charchar | | intint | | array [ num] of array [ num] of T | T | TTE → E → literalliteral | | numbernumber | | idid | E | E modmod E | E E | E [[ E E ] ] | E | E

Key: integer;Key MOD 999;

X: character;B: integer;B MOD X;

A: array [100] of char;A[20]A[200]

Are all of these Legal?Are all of these Legal?

Page 30: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.30

CSE4100

First: Add Typing into Symbol TableFirst: Add Typing into Symbol TableP → D ; ED → D ; D D → id: T {addtype(id.entry, T.type)}T → char {T.type:= char}T → int {T.type:= int}T → array [ num] of T1

{T.Type:= array(1..num.val, T1.type}T → T {T.type:= pointer(T1.type)}

Notes:Notes: Assume Lexical recognition of id (in Lexical Analyzer)

Adds id to Symbol Table Thus – we Augment this with T.Type

E → literal {E.type:= char}E → number {E.type:= integer} E → id {E.type:= lookup(id.entry) }

Page 31: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.31

CSE4100

Remaining Typing More ComplexRemaining Typing More Complex

E → E1 mod E2 {E.type := if E1.type = integer and

E2.type = integer then integer else type_error}

E → E1 [E2 ] {E.type := if E2.type = integer and

E1.type = array(s, t) then t else type_error}

E → E1 {E.type := if E1.type = pointer(t) then t

else type_error} E1 E2 May be Mod, Array, or Ptr ExpressionMay be Mod, Array, or Ptr Expression Useful Extensions would Include Boolean Type and Useful Extensions would Include Boolean Type and

Extending Expression with Rel Ops, AND, OR, etc.Extending Expression with Rel Ops, AND, OR, etc.

Page 32: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.32

CSE4100

Extending Example to StatementsExtending Example to Statements

P → D ; SS → id =: E {S.type:=if id.type =E.type

then void else type_error)}

S → if E then S1 {S.type:=if E.type = boolean then S1.type else type_error)}

S → while E do S1 {S.type:=if E.type = boolean then S1.type else type_error)}

S → S1 ; S2 {S.type:=if S1.type = void and S2.type = void

then void else type_error)}

These Extensions are More Complex from a Type These Extensions are More Complex from a Type Checking PerspectiveChecking Perspective

Right Now, only Individual Statements are CheckedRight Now, only Individual Statements are Checked

Page 33: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.33

CSE4100

What are Main Issues in Type Checking?What are Main Issues in Type Checking? Type Equivalence:

Conditions under which Types are the Same Tracking of Scoping – Nested DeclarationsTracking of Scoping – Nested Declarations Type Compatibility

Conversion/casting, Nonconverting casts, Coercion Type Inference

Determining the Type of a Complex Expression Reviewing Remaining Concepts of NoteReviewing Remaining Concepts of Note

Overloading, Polymorphism, Generics In OO Case:

Classes are OO version of a type Issues Need to Consider Way Program in OO In Older Languages like C, these are Critical

Page 34: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.34

CSE4100

Structural vs. Name Equivalence of TypesStructural vs. Name Equivalence of Types Two Types are “Structurally Equivalent” iff they are Two Types are “Structurally Equivalent” iff they are

Equivalent Under Following 3 Rules:Equivalent Under Following 3 Rules: SE1: A Type Name is Structurally Equivalent to

Itself SE2: T1 and T2 are Structurally Equivalent if they

are Formed by Applying the Same Type Constructors to Structurally Equivalent Types

SE3: After a Type Declaration: Type n=T, the Type Name n is Structurally Equivalent to T

SE3 is “Name Equivalence” What Do Programming Languages Use?What Do Programming Languages Use?

C: All Three Rules Pascal: Omits SE2 and Restricts SE3 to be a Type

Name can only be Structurally Equivalent to Other Type Names

Page 35: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.35

CSE4100

Type EquivalenceType Equivalence Structural equivalence: equivalent if built in the same

way (same parts, same order) Name equivalence: distinctly named types are always

different Structural equivalence questions

What parts constitute a structural difference? Storage: record fields, array size Naming of storage: field names, array indices Field order

How to distinguish between intentional vs. incidental structural similarities?

An argument for name equivalence: “They’re different because the programmer said so; if they’re

Page 36: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.36

CSE4100

Type Equivalence Records and ArraysType Equivalence Records and Arrays Would record types with identical fields, but different

name order, be structurally equivalent?

When are arrays with the same number of elements structurally equivalent?

type PascalRec = record a : integer; b : integer end;val MLRec = { a = 1, b = 2 };val OtherRec = { b = 2, a = 1 };

type str = array [1..10] of integer;type str = array [1..2 * 5] of integer;type str = array [0..9] of integer;

Page 37: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.37

CSE4100

Consider Name Equivalence in PascalConsider Name Equivalence in Pascal How are Following Compared:How are Following Compared: By Rules SE1, SE2, SE3, allBy Rules SE1, SE2, SE3, all

are Equivalent!are Equivalent! However:However:

Some Implementations of Pascal next, last – Equivalent p, q, r, - Equivalent

Other Implementations of Pascal next, last – Equivalent q, r, - Equivalent

How is Following Interpreted?How is Following Interpreted?

type link = cell;var next : link;

last : link;p : cell;q, r : cell;

type link = cell;np = cell;npr = cell;

var next : link;last : link;p : np;q, r : npr;

Page 38: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.38

CSE4100

What about Classes and Equivalence?What about Classes and Equivalence?

public class person {

private String lastname,

firstname;

private String loginID;

private String password;

};

public class user {

private String lastname,

firstname;

private String loginID;

private String password;

};

Are these SE1? SE2? Or SE3?Are these SE1? SE2? Or SE3?

What Does Java Require?What Does Java Require?

Page 39: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.39

CSE4100

Checking Structural EquivalenceChecking Structural Equivalence Employ a Recursive Algorithm to Check SE2: Employ a Recursive Algorithm to Check SE2:

Algorithm Adaptable for Other Versions of SEAlgorithm Adaptable for Other Versions of SE Constructive Equivalence Means Following are Same:Constructive Equivalence Means Following are Same:

X: array[1..10] of int; Y: array[1..10] of int;

Page 40: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.40

CSE4100

Alias Types and Name EquivalenceAlias Types and Name Equivalence Alias types are types that purely consist of a different

name for another type

Is Integer assignable to a Stack_Element? Levels? Can a Celsius and Fahrenheit be assigned to each

other? Strict name equivalence: aliased types are distinct Loose name equivalence: aliased types are equivalence Ada allows additional explicit equivalence control:

TYPE Stack_Element = INTEGER;TYPE Level = INTEGER;TYPE Celsius = REAL;TYPE Fahrenheit = REAL;

subtype Stack_Element is integer;type Celsius is new real;type Fahrenheit is new real;

Page 41: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.41

CSE4100

Why is Degree of Type Equivalence Critical?Why is Degree of Type Equivalence Critical? Governs how Software Engineers Develop Code… Governs how Software Engineers Develop Code…

Why?Why?

SE2 Alone Doesn’t Promote Well Designed, Thought SE2 Alone Doesn’t Promote Well Designed, Thought Out, Software … Why?Out, Software … Why?

Impacts on Team-Oriented Software Development… Impacts on Team-Oriented Software Development… How?How?

With SE2 Alone, Errors are Harder to Locate and With SE2 Alone, Errors are Harder to Locate and Correct… Why?Correct… Why?

Increases Compilation Time with SE2 Alone … Why?Increases Compilation Time with SE2 Alone … Why?

Page 42: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.42

CSE4100

ScopingScoping What is the problem ?What is the problem ?

Consider this example program

class Foo {int n;Foo() { n = 0;}int run(int n) {

int i;int j;

i = 0;j = 0;while (i < n) {

int n;n = i * 2;j = j + n;

}return j;

}};

class Foo {int n;Foo() { n = 0;}int run(int n) {

int i;int j;

i = 0;j = 0;while (i < n) {

int n;n = i * 2;j = j + n;

}return j;

}};

Page 43: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.43

CSE4100

Resolving the IssueResolving the Issue ObservationObservation

Scopes are always properly nested Each new definition could have a different type

IdeaIdea Make the typing environment sensitive to scopes

New operations on typing env.New operations on typing env. Entering a scope

Effect: New declarations overload previous one Leaving a scope

Effect: Old declarations become current again What are the Issues?What are the Issues?

Activating and Tracking Scopes!

Page 44: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.44

CSE4100

ScopingScoping The ScopesThe Scopes

class Foo {int n;Foo() { n = 0;}int run(int n) {

int i;int j;

i = 0;j = 0;while (i < n) {

int n;n = i * 2;j = j + n;

}return j;

}};

class Foo {int n;Foo() { n = 0;}int run(int n) {

int i;int j;

i = 0;j = 0;while (i < n) {

int n;n = i * 2;j = j + n;

}return j;

}};

Class Scope

Method Scope

Body Scope

Block Scope

Key point: Non-shadowed names remain visibleKey point: Non-shadowed names remain visible

Page 45: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.45

CSE4100

Handling ScopesHandling Scopes From a declarative standpointFrom a declarative standpoint

Introduce a new typing environment Initially equal to the copy of the original Then augmented with the new declarations

Discard environment when leaving the scope From an implementation point of viewFrom an implementation point of view

Environment directly accounts for scoping How ?

Scope chaining!

Page 46: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.46

CSE4100

Scope ChainingScope Chaining Key IdeasKey Ideas

One scope = One hashtable Scope chaining = A linked list of scopes

Abstract Data TypeAbstract Data Type Semantic Environment

pushScope– Add a new scope in front of the linked list

popScope– Remove the scope at the front of the list

lookup(name)– Search for an entry for name. If nothing in first scope, start

scanning the subsequent scopes in the linked list.

Page 47: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.47

CSE4100

Scope ChainingScope Chaining AdvantagesAdvantages

Updates are non-destructive When we pop a scope, the previous list is unchanged

since addition are only done in the top scope The current list of scopes can be saved (when

needed)

Page 48: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.48

CSE4100

Entering & Leaving ScopesEntering & Leaving Scopes Easy to find out...Easy to find out... Use the tree structure!Use the tree structure!

Entering scope When entering a class When entering a method When entering a block

Leaving scope End of class End of method End of block

We’ll Revisit in Chapter 7 on Runtime Environment!We’ll Revisit in Chapter 7 on Runtime Environment!

Page 49: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.49

CSE4100

Type ConversionType Conversion Certain contexts in certain languages may require exact

matches with respect to types: aVar := anExpression value1 + value2 foo(arg1, arg2, arg3, … , argN)

Type conversion seeks to follow these exact match rules while allowing programmers some flexibility in the values used Using structurally-equivalent types in a name-

equivalent language Types whose value ranges may be distinct but

intersect (e.g. subranges) Distinct types with sensible/meaningful

corresponding values (e.g. integers and floats)

Page 50: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.50

CSE4100

Type ConversionType Conversion Refers to the Conversion Between Different Types to Refers to the Conversion Between Different Types to

Carry out Some Action in a Program Carry out Some Action in a Program Often Abused within a Programming Language (C)Often Abused within a Programming Language (C) Typically Used in Arithmetic/Boolean ExpressionsTypically Used in Arithmetic/Boolean Expressions

r := i + r; (Pascal) f := i + c; (C)

Two Kinds of Conversion:Two Kinds of Conversion: Implicit: Automatically done by Compiler Explicit: Type-Casts: Programmer Initiated (Ord,

Chr, Trunc) If X is a real array, which works faster? WhyIf X is a real array, which works faster? Why

for I:=1 to N do X[I] := 1; for I:=1 to N do X[I] := 1.0;

A Good Optimizing Compiler will Convert 1A Good Optimizing Compiler will Convert 1stst option! option!

Page 51: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.51

CSE4100

Type Casting SyntaxType Casting Syntax AdaAda

C/C++/JavaC/C++/Java

Some SQLsSome SQLs

n : integer;r : real;...r := real(n);

// Sample is specific to Java, but shares common syntax.Object n;String s;...s = (String)n;

-- Timestamp is a built-in data type; charField is-- a varchar (string) field of some table.select charField::timestamp from…

Page 52: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.52

CSE4100

Type ConversionType Conversion Accomplished via an Attribute GrammarAccomplished via an Attribute Grammar

Double Underline is a Coercion which must be Tracked and Double Underline is a Coercion which must be Tracked and Recognized Later During Code Generation. Why?Recognized Later During Code Generation. Why? Real := Real * Integer; What Kind of “*” are there to Utilize? This is Overloading!

Page 53: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.53

CSE4100

Non-Converting Type CastsNon-Converting Type Casts Type casts that explicitly preserve the internal bit-level

representation of values Common in manipulating allocated blocks of memory

Same block of memory may be viewed as arrays of characters, integers, or even records/structures

Block of memory may be read from a file or other external source that is initially viewed as a “raw” set of bytes

Page 54: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.54

CSE4100

Non-Converting Type Casts - ExamplesNon-Converting Type Casts - Examples

Ada – Explicit Unchecked Conversion SubroutineAda – Explicit Unchecked Conversion Subroutine

C/C++ (Not Java): Pointer GamesC/C++ (Not Java): Pointer Games

• C++: explicit cast types static_cast, reinterpret_cast, dynamic_cast

function cast_float_to_int is new unchecked_conversion(float, integer);

void *block; // Gets loaded up with some datafrom a file.Record *header = (Record *)block; // Record is struct.

int i = static_cast<int>(d); // Assume d is double.Record *header = reinterpret_cast<Record *>(block);Derived *dObj = dynamic_cast<Derived *>(baseObj); // Derived is a subclass of Base.

Page 55: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.55

CSE4100

Type Coercion Sometimes absolute type equivalence is too strict; type

compatibility is sufficient Type equivalence vs. type compatibility in Ada

(strict): Types must be equivalent One type must be a subtype of another, or both are

subtypes of the same base type Types are arrays with the same sizes and element types

in each dimension Pascal extends slightly, also allowing:

Base and subrange types are cross-compatible Integers may be used where a real is expected

Type coercion is an implicit type conversion between compatible but not necessarily equivalent types

Page 56: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.56

CSE4100

Type Coercion Issues Sometimes viewed as a weakening of type securitY

Mixing of types without explicit indication of intent

Opposite end of the spectrum: C and Fortran Allow interchangeable use of numeric types Fortran: arithmetic can be performed on entire arrays C: arrays and pointers are roughly interchangeable

C++ Add Programmer Extensible Coercion Rulesclass ctr { public: ctr(int i = 0, char* x = "ctr") { n = i; strcpy(s, x); } ctr& operator++(int) { n++; return *this; } operator int() { return n; } // Coercion to int operator char*() { return s; } // Coercion to char * private: int n; char s[64];};

Page 57: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.57

CSE4100

Type InferenceType Inference Type inference refers to the process of determining the

type of an arbitrarily complex expression Generally not a huge issue — most of the time, the

type for the result of a given operation or function is clearly known, and you just “build up” to the final type as you evaluate the expression

In languages where an assignment is also an expression, the convention is to have the “result” type be the type of the lefthandside

But, there are occasional issues, specifically with subrange and composite types

Page 58: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.58

CSE4100

Examples of Type InferenceExamples of Type Inference Subranges — in languages that can define types as

subranges of base types (Ada, Pascal), type inference can be an issue:

What should c’s type be? Easy answer: always go back to the base type

(integer in this case)

type Atype = 0..20; Btype = 10..20;var a : Atype; b : Btype; c : ????;c := a + b;

Page 59: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.59

CSE4100

Examples of Type InferenceExamples of Type Inference What if the result of an expression is assigned to a

subrange? a := 5 + b; (* a and b are defined on last slide *) The primary question is bounds checking —

operations on subranges can certainly produce results that break away from their defined bounds

Static checks: include code that infers the lowest and highest possible results from an expression

Dynamic check: static checks are not always possible, so the last resort is to check the result at runtime

Page 60: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.60

CSE4100

Examples of Type InferenceExamples of Type Inference Composite types

What is the type of operators on arrays? We know it’s an array, but what specifically?

(particularly for languages where the index range is part of the array definition)

Examples: Strings in languages where strings are exactly

character arrays (Pascal, Ada)

Page 61: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.61

CSE4100

Examples of Type InferenceExamples of Type Inference Sets

In languages that encode a base type with a set (e.g. set of integer), what is the “type” of unions, intersections, and differences of sets?

Examples: Particularly tricky when a set is combined with a

subrange Same as subrange handling: static checks are

possible in some cases, but dynamic checks are not completely avoidable

var A : set of 1..10; B : set of 10..20; C : set of 1..15; i : 1..30;... C := A + B * [1..5, i];

Page 62: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.62

CSE4100

OverloadingOverloading The Same Symbol has Different Meanings in Different The Same Symbol has Different Meanings in Different

ContextsContexts Many Examples:Many Examples:

+ : int int int + : real real real + : set set set (union) + : string string string (concatenate) == (compares multiple types) … >> cout (outputs multiple types)

Impacts on Conversion since During Code Generation Impacts on Conversion since During Code Generation we must Choose “Correct” Option based on Typewe must Choose “Correct” Option based on Type

Page 63: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.63

CSE4100

OverloadingOverloading Coercion Requires the Need to Convert Expression Coercion Requires the Need to Convert Expression

Before Generating CodeBefore Generating Code real := real * int – need to use real * real := real * int_to_real(int) – do the conversion After Conversion, Code Generation can Occur

Overloading has Increased Attention with Emergence Overloading has Increased Attention with Emergence of Object-Oriented Langaugesof Object-Oriented Langauges C++ and Java Allow User Defined Overloaded

Definitions for +, -, *, etc. Programmer Definable Routines (e.g., SORT) can

be Overloaded based on Type

Page 64: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.64

CSE4100

OverloadingOverloading A very handy mechanismA very handy mechanism

Available in C++/Java/... What is it?What is it?

Provide multiple definition of the same function over different types.

Example [C++]Example [C++]

int operator+(int a,int b);float operator+(float a,float b);float operator+(float a,int b);float operator+(int a,float b);Complex operator+(Complex a,Complex b);....

int operator+(int a,int b);float operator+(float a,float b);float operator+(float a,int b);float operator+(int a,float b);Complex operator+(Complex a,Complex b);....

Page 65: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.65

CSE4100

PolymorphismPolymorphism Essential Concept: A Function is Polymorphic if it can Essential Concept: A Function is Polymorphic if it can

be Utilized with Arguments/Parameters of More than 1 be Utilized with Arguments/Parameters of More than 1 TypeType

The EXACT, SAME, Piece of Code is being UtilizedThe EXACT, SAME, Piece of Code is being Utilized Why is Polymorphism Important?Why is Polymorphism Important?

Consider a List of Items in C:struct item { int info;

struct item *next; } Write a Length Function

function LEN(list: *item) : integer;In theory – only need to access *next …

However, in C, you can’t reuse this Length Function for Different Structures

Write a Similar Version for Each Different Structure

Page 66: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.66

CSE4100

PolymorphismPolymorphism Allows us to write Type Independent CodeAllows us to write Type Independent Code Polymorphism is Supported in ML, a Strongly Type Polymorphism is Supported in ML, a Strongly Type

Functional Programming Language:Functional Programming Language:fun length (lptr) =fun length (lptr) =

if null (lptr) then 0if null (lptr) then 0else length(tail(lptr)) + 1;else length(tail(lptr)) + 1;

Overloading is an Example of ad-hoc PolymorphismOverloading is an Example of ad-hoc Polymorphism Parametric Polymorphism Essentially has Type as a Parametric Polymorphism Essentially has Type as a

Parameter to FunctionParameter to Function Stack Operations: Create (T: stack_type) Arithmetic Operations: Plus (X, Y: T: T is a type)

This leads to Generics!This leads to Generics!

Page 67: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.67

CSE4100

GenericsGenerics Imagine a code fragment that Imagine a code fragment that

Computes the length of a list Java-style

class ListNode<T> {T data;Node<T> next;Node<T>(T d,Node<T> n) {

data = d;next = n;

} int length() {

if (next != null)return 1 + next.length();

else return 1;}

}

This does not refer to T at all, it can be implemented only once!

So... What is the type of this method ?

Page 68: CH6.1 CSE 4100 Chap 6: Type Checking/Semantic Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.

CH6.68

CSE4100

Concluding Remarks/Looking AheadConcluding Remarks/Looking Ahead Type Checking/Semantic Analysis/Type Systems are Type Checking/Semantic Analysis/Type Systems are

Important Part of Compilation ProcessImportant Part of Compilation Process Check/Verify Context Sensitive Aspects of LanguageCheck/Verify Context Sensitive Aspects of Language

Compatible Operations Definition of Variable Before Use Array Access Etc.

Other Issues Impacting Type Checking include Other Issues Impacting Type Checking include Conversion, Scoping, Polymorphism, etc.Conversion, Scoping, Polymorphism, etc.

How Does Type Checking Apply to Project?How Does Type Checking Apply to Project? Looking Ahead:Looking Ahead:

Exploration of Runtime Organization and Environment


Recommended