Programming Languages

Size: px
Start display at page:

Download "Programming Languages"

Transcription

1 TEHNISHE UNIVERSITÄT MÜNHEN FKULTÄT FÜR INFORMTIK Programming Languages Multiple Inheritance Dr. Michael Petter Winter term 2014 Multiple Inheritance 1 / 44

2 Inheritance Principles 1 Interface Inheritance Outline 2 Implementation Inheritance 3 Liskov Substition Principle and Shapes ++ Object Heap Layout 1 asics 2 Single-Inheritance 3 Virtual Methods ++ Multiple Parents Heap Layout 1 Multiple-Inheritance 2 Virtual Methods 3 ommon Parents Discussion & Learning Outcomes Multiple Inheritance 2 / 44

3 Inheritance Principles 1 Interface Inheritance Outline 2 Implementation Inheritance 3 Liskov Substition Principle and Shapes ++ Object Heap Layout 1 asics 2 Single-Inheritance 3 Virtual Methods ++ Multiple Parents Heap Layout 1 Multiple-Inheritance Excursion: Linearization 1 mbiguous common parents 2 Principles of Linearization 3 Linearization algorithms 2 Virtual Methods 3 ommon Parents Discussion & Learning Outcomes Multiple Inheritance 2 / 44

4 Wouldn t it be nice to inherit from several parents? Multiple Inheritance Inheritance Principles 3 / 44

5 Interface vs. Implementation inheritance The classic motivation for inheritance is implementation inheritance ode reusage hild specializes parents, replacing particular methods with custom ones Parent acts as library of common behaviours Implemented in languages like ++ or Lisp ode sharing in interface inheritance inverts this relation ehaviour contract hild provides methods, with signatures predetermined by the parent Parent acts as generic code frame with room for customization Implemented in languages like Java or # Multiple Inheritance Inheritance Principles 4 / 44

6 Interface Inheritance PowerShovel actuate() FrontLoader actuate() House actuate() ulldozer Undercarriage moveto(x,y) Tracks moveto(x,y) Wheels moveto(x,y) Multiple Inheritance Inheritance Principles 5 / 44

7 Implementation inheritance Ship toot() irport moveto(x,y) shelter(plane) ircraft arrier striket(x,y) Multiple Inheritance Inheritance Principles 6 / 44

8 Excursion: LSP and Square-Rect-Problem The Liskov Substitution Principle Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. class Rectangle { void setwidth (int w){ this.w=w; } void setheight(int h){ this.h=h; } void getwidth () { return w; } void getheight() { return h; } } class Square extends Rectangle { void setwidth (int w){ this.w=w;h=w; } void setheight(int h){ this.h=h;w=h; } } Rectangle r = new Square(2); r.setwidth(3); r.setheight(4); assert r.getheight()* r.getwidth()==12; Multiple Inheritance Inheritance Principles 7 / 44

9 Excursion: LSP and Square-Rect-Problem The Liskov Substitution Principle Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. class Rectangle { void setwidth (int w){ this.w=w; } void setheight(int h){ this.h=h; } void getwidth () { return w; } void getheight() { return h; } } class Square extends Rectangle { void setwidth (int w){ this.w=w;h=w; } void setheight(int h){ this.h=h;w=h; } } Rectangle r = new Square(2); r.setwidth(3); r.setheight(4); assert r.getheight()* r.getwidth()==12;! ehavioural assumptions Multiple Inheritance Inheritance Principles 7 / 44

10 So how do we lay out objects in heap anyway? Multiple Inheritance Standard Object Heap Layout 8 / 44

11 Excursion: rief introduction to LLVM IR Low Level Virtual Machine as reference semantics: ;(recursive) struct definitions %struct. = type { i32, %struct., i32(i32)* } %struct. = type { i64, [10 x [20 x i32]], i8 } ;allocation of objects %a = alloca %struct. ;adress computation for selection in structure (pointers): %1 = getelementptr %struct.* %a, i64 0, i64 2 ;load from memory %2 = load i32(i32)* %1 ;indirect call %retval = call i32 (i32)* %2(i32 42) Retrieve the memory layout of a compilation unit with: clang -cc1 -x c++ -v -fdump-record-layouts -emit-llvm source.cpp Retrieve the IR ode of a compilation unit with: clang -O1 -S -emit-llvm source.cpp -o IR.llvm Multiple Inheritance Standard Object Heap Layout Object layout & inheritance 9 / 44

12 Object layout class { int a; int f(int); class : public { int b; int g(int); class : public { int c; int h(int);... c; c.g(42); int a int b int c %class. = type { %class., i32 } %class. = type { %class., i32 } %class. = type { i32 } %c = alloca %class. %1 = bitcast %class.* %c to %class.* %2 = call %1, i32 42) ; g is statically known Multiple Inheritance Standard Object Heap Layout Object layout & inheritance 10 / 44

13 Translation of a method body class { int a; int f(int); class : public { int b; int g(int); class : public { int c; int h(int); int ::g(int p) { return p+b; define %this, i32 %p) { %1 = getelementptr %class.* %this, i64 0, i32 1 %2 = load i32* %1 %3 = add i32 %2, %p ret i32 %3 } Multiple Inheritance Standard Object Heap Layout Object layout & inheritance 11 / 44 int a int b int c %class. = type { %class., i32 } %class. = type { %class., i32 } %class. = type { i32 }

14 Object layout virtual methods class { int a; virtual int f(int); virtual int g(int); virtual int h(int); class : public { int b; int g(int); class : public { int c; int h(int);... c; c.g(42); %c.vptr = bitcast %class.* %c to i32 (%class.*, i32)*** ; vtbl %1 = load (%class.*, i32)*** %c.vptr ; dereference vptr %2 = getelementptr %1, i64 1 ; select g()-entry %3 = load (%class.*, i32)** %2 ; dereference g()-entry %4 = call i32 %3(%class.* %c, i32 42) Multiple Inheritance Standard Object Heap Layout Virtual Methods 12 / 44 vptr int a int b int c ::f ::g ::h %class. = type { %class., i32, [4 x i8] } %class. = type { [12 x i8], i32 } %class. = type { i32 (...)**, i32 }

15 So how do we include several parent objects? Multiple Inheritance Implementation of Multiple inheritance 13 / 44

16 Multiple inheritance class diagram int f(int) int g(int) int a int b int h(int) int c Multiple Inheritance Implementation of Multiple inheritance 14 / 44

17 Static Type asts class { int a; int f(int); class { int b; int g(int); class : public, public { int c; int h(int);... * b = (*) new (); int a int b int c } %class. = type { %class., %class., i32 } %class. = type { i32 } %class. = type { i32 } %1 = call 12) call %1, i8 0, i64 12, i32 4, i1 false) %2 = getelementptr i8* %1, i64 4 ; select -offset in %b = bitcast i8* %2 to %class.* Multiple Inheritance Implementation of Multiple inheritance Multiple base classes in layout 15 / 44

18 Static Type asts class { int a; int f(int); class { int b; int g(int); class : public, public { int c; int h(int);... * b = (*) new (); int a int b int c } %class. = type { %class., %class., i32 } %class. = type { i32 } %class. = type { i32 } %1 = call 12) call %1, i8 0, i64 12, i32 4, i1 false) %2 = getelementptr i8* %1, i64 4 ; select -offset in %b = bitcast i8* %2 to %class.*! implicit casts potentially add a constant to the object pointer. Multiple Inheritance Implementation of Multiple inheritance Multiple base classes in layout 15 / 44

19 Static Type asts class { int a; int f(int); class { int b; int g(int); class : public, public { int c; int h(int);... * b = (*) new (); int a int b int c } %class. = type { %class., %class., i32 } %class. = type { i32 } %class. = type { i32 } %1 = call 12) call %1, i8 0, i64 12, i32 4, i1 false) %2 = getelementptr i8* %1, i64 4 ; select -offset in %b = bitcast i8* %2 to %class.*! implicit casts potentially add a constant to the object pointer.! getelementptr implements as 4 i8! Multiple Inheritance Implementation of Multiple inheritance Multiple base classes in layout 15 / 44

20 Keeping alling onventions class { int a; int f(int); class { int b; int g(int); class : public, public { int c; int h(int);... c; c.g(42); %c = alloca %class. %1 = bitcast %class.* %c to i8* %2 = getelementptr i8* %1, i64 4 ; select -offset in %3 = call %2, i32 42) ; g is statically known int a int b int c } %class. = type { %class., %class., i32 } %class. = type { i32 } %class. = type { i32 } Multiple Inheritance Implementation of Multiple inheritance Multiple base classes in layout 16 / 44

21 mbiguities class { void f(int); class { void f(int); class : public, public { * pc; pc->f(42);! Which method is called? Solution I: Explicit qualification pc->::f(42); pc->::f(42); Solution II: utomagical resolution Idea: The ompiler introduces a linear order on the nodes of the inheritance graph Multiple Inheritance Implementation of Multiple inheritance mbiguities 17 / 44

22 Linearization Principle 1: Inheritance Relation Defined by parent-child. Example: (, ) = Principle 2: Multiplicity Relation Defined by the succession of multiple parents. Example: (, ) = In General: 1 Inheritance is a uniform mechanism, and its searches ( total order) apply identically for all object fields or methods 2 In the literature, we also find the set of constraints to create a linearization as Method Resolution Order 3 Linearization is a best-effort approach at best Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 18 / 44

23 MRO via DFS Leftmost Preorder Depth-First Search W (, ) (W ) (W ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 19 / 44

24 MRO via DFS Leftmost Preorder Depth-First Search L[] = W! Principle 1 inheritance is violated W Python: classical python objects ( 2.1) use LPDFS! LPDFS with Duplicate ancellation (, ) (W ) (W ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 19 / 44

25 MRO via DFS Leftmost Preorder Depth-First Search L[] = W! Principle 1 inheritance is violated W Python: classical python objects ( 2.1) use LPDFS! LPDFS with Duplicate ancellation L[] = W (, ) (W ) (W ) Principle 1 inheritance is fixed V W LPDFS with Duplicate ancellation Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 19 / 44 (, )(V, W )(W, V )

26 MRO via DFS Leftmost Preorder Depth-First Search L[] = W! Principle 1 inheritance is violated W Python: classical python objects ( 2.1) use LPDFS! LPDFS with Duplicate ancellation L[] = W (, ) (W ) (W ) Principle 1 inheritance is fixed Python: new python objects (2.2) use LPDFS(D)! LPDFS with Duplicate ancellation L[] = W V V W! Principle 2 multiplicity not fulfillable! However = W V?? (, )(V, W )(W, V ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 19 / 44

27 MRO via Refined Postorder DFS Reverse Postorder Rightmost DFS F W G D E H (, ) (F, D) (E, H) D(G) E(G) F (W ) G(W ) H(W ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 20 / 44

28 MRO via Refined Postorder DFS Reverse Postorder Rightmost DFS L[] = F D E G H W Linear extension of inheritance relation Topological sorting RPRDFS Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 20 / 44 F W G D E H (, ) (F, D) (E, H) D(G) E(G) F (W ) G(W ) H(W ) F D G E (, ) (F, G) (D, E) D(G) E(F )

29 MRO via Refined Postorder DFS Reverse Postorder Rightmost DFS L[] = F D E G H W Linear extension of inheritance relation Topological sorting RPRDFS L[] = D G E F F W G D E H (, ) (F, D) (E, H) D(G) E(G) F (W ) G(W ) H(W )! ut principle 2 multiplicity is violated! F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 20 / 44

30 MRO via Refined Postorder DFS Reverse Postorder Rightmost DFS L[] = F D E G H W Linear extension of inheritance relation Topological sorting RPRDFS L[] = D G E F F W G D E H (, ) (F, D) (E, H) D(G) E(G) F (W ) G(W ) H(W )! ut principle 2 multiplicity is violated! Refined RPRDFS F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 20 / 44

31 MRO via Refined Postorder DFS Reverse Postorder Rightmost DFS L[] = F D E G H W Linear extension of inheritance relation Topological sorting RPRDFS L[] = D G E F F W G D E H (, ) (F, D) (E, H) D(G) E(G) F (W ) G(W ) H(W )! ut principle 2 multiplicity is violated! LOS: uses Refined RPDFS [3] Refined RPRDFS L[] = D E F G Refine graph with conflict edge & rerun RPRDFS! F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 20 / 44

32 MRO via Refined Postorder DFS Extension Principle: Monotonicity If 1 2 in s linearization, then 1 2 for every linearization of s children. F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 21 / 44

33 MRO via Refined Postorder DFS Refined RPRDFS! Monotonicity is not guaranteed! Extension Principle: Monotonicity If 1 2 in s linearization, then 1 2 for every linearization of s children. F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 21 / 44

34 MRO via Refined Postorder DFS Refined RPRDFS! Monotonicity is not guaranteed! Extension Principle: Monotonicity If 1 2 in s linearization, then 1 2 for every linearization of s children. L[] = D E F G = F G F D G E (, ) (F, G) (D, E) D(G) E(F ) L[] = D G E F = G F Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 21 / 44

35 MRO via 3 Linearization linearization L is an attribute L[] of a class. lasses 1... n are superclasses to child class, defined in the local precedence order ( 1... n ). Then (L i ) = i L[( 1... n )] = (L[ 1 ],..., L[ n ], 1... n ) L[Object] = Object with { c ( i (L i \ c)) if min k j c = head(l k ) / tail(l j )! fail else Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 22 / 44

36 MRO via 3 Linearization L[G] L[F ] L[E(F )] L[D(G)] L[(F, G)] L[(D, E)] L[(, )] G F F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

37 MRO via 3 Linearization L[G] L[F ] L[E(F )] L[D(G)] L[(F, G)] L[(D, E)] L[(, )] G F E F D G F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

38 MRO via 3 Linearization L[G] L[F ] L[E(F )] L[D(G)] L[(F, G)] L[(D, E)] L[(, )] G F E F D G (L[F ] L[G] {F, G}) F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

39 MRO via 3 Linearization L[G] L[F ] L[E(F )] L[D(G)] L[(F, G)] L[(D, E)] L[(, )] G F E F D G ({F } {G} {F, G}) F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

40 MRO via 3 Linearization L[G] L[F ] L[E(F )] L[D(G)] L[(F, G)] L[(D, E)] L[(, )] G F E F D G F G F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

41 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] (L[D] L[E] {D, E}) L[(, )] F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

42 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] ({D, G} {E, F } {D, E}) L[(, )] F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

43 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] D ({G} {E, F } {E}) L[(, )] F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

44 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] D G E F L[(, )] F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

45 MRO via 3 Linearization L[G] L[F ] L[E(F )] L[D(G)] L[(F, G)] L[(D, E)] L[(, )] G F E F D G (, ) (F, G) (D, E) D(G) E(F ) F G D G E F ({, F, G} {, D, G, E, F } {, }) F D G E Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

46 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] D G E F L[(, )] D ({F, G} {G, E, F }) F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

47 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] D G E F L[(, )]! fail F D G E (, ) (F, G) (D, E) D(G) E(F ) Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

48 MRO via 3 Linearization L[G] G L[F ] F L[E(F )] E F L[D(G)] D G L[(F, G)] F G L[(D, E)] D G E F L[(, )]! fail F D G E (, ) (F, G) (D, E) D(G) E(F ) 3 detects and reports a violation of monotonicity with the addition of (,) to the class set. 3 linearization [1]: is used in OpenDylan, Python, and Perl 6 Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 23 / 44

49 Linearization vs. explicit qualification Linearization No switch/duplexer code necessary No explicit naming of qualifiers Unique super reference Reduces number of multi-dispatching conflicts Qualification More flexible, fine-grained Linearization choices may be awkward or unexpected Languages with automatic linearization exist LOS ommon Lisp Object System Dylan, Python and Perl 6 with 3 Prerequisite for Mixins Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 24 / 44

50 nd what about dynamic dispatching in Multiple Inheritance? Multiple Inheritance Implementation of Multiple inheritance Method Resolution Order 25 / 44

51 Virtual Tables for Multiple Inheritance class { int a; virtual int f(int); class { int b; virtual int f(int); virtual int g(int); class : public, public { int c; int f(int);... c; * pb = &c; pb->f(42); ; * pb = &c; %0 = bitcast %class.* %c to i8* ; type fumbling %1 = getelementptr i8* %0, i64 16 ; offset of in %2 = bitcast i8* %1 to %class.* ; get typing right store %class.* %2, %class.** %pb ; store to pb } vptr int a vptr int b int c 0 RTTI ::f RTTI ::f ::g %class. = type { %class., [12 x i8], i32 } %class. = type { i32 (...)**, i32 } %class. = type { i32 (...)**, i32 } Multiple Inheritance Implementation of Multiple inheritance Virtual Table 26 / 44

52 Virtual Tables for Multiple Inheritance class { int a; virtual int f(int); class { int b; virtual int f(int); virtual int g(int); class : public, public { int c; int f(int);... c; * pb = &c; pb->f(42); vptr int a vptr int b int c ; pb->f(42); %0 = load %class.** %pb ;load the b-pointer %1 = bitcast %class.* %0 to i32 (%class.*, i32)*** ;cast to vtable %2 = load i32(%class.*, i32)*** %1 ;load vptr %3 = getelementptr i32 (%class.*, i32)** %2, i64 0 ;select f() entry %4 = load i32(%class.*, i32)** %3 ;load f()-thunk %5 = call i32 %4(%class.* %0, i32 42) } 0 RTTI ::f RTTI ::f ::g %class. = type { %class., [12 x i8], i32 } %class. = type { i32 (...)**, i32 } %class. = type { i32 (...)**, i32 } Multiple Inheritance Implementation of Multiple inheritance Virtual Table 27 / 44

53 asic Virtual Tables ( ++-I) asic Virtual Table consists of different parts: 1 offset to top of an enclosing objects heap representation 2 typeinfo pointer to an RTTI object (not relevant for us) 3 virtual function pointers for resolving virtual methods 0 RTTI ::f RTTI ::f ::g Virtual tables are composed when multiple inheritance is used The vptr fields in objects are pointers to their corresponding virtual-subtables asting preserves the link between an object and its ocrresponding virtual-subtable clang -cc1 -fdump-vtable-layouts -emit-llvm code.cpp yields the vtables of a compilation unit Multiple Inheritance Implementation of Multiple inheritance Virtual Table 28 / 44

54 asting Issues class { int a; class { virtual int f(int); class : public, public { int c; int f(int); * c = new (); c->f(42); * b = new (); b->f(42); 0 RTTI f RTTI f ::f Multiple Inheritance Implementation of Multiple inheritance Virtual Table 29 / 44

55 asting Issues class { int a; class { virtual int f(int); class : public, public { int c; int f(int); * c = new (); c->f(42); 0 RTTI f RTTI f * b = new (); b->f(42);! this-pointer for ::f is expected to point to ::f Multiple Inheritance Implementation of Multiple inheritance Virtual Table 29 / 44

56 asting Issues class { int a; class { virtual int f(int); class : public, public { int c; int f(int); * c = new (); c->f(42); 0 RTTI ::f RTTI ::f ::f * b = new (); b->f(42); ::f! this-pointer for ::f is expected to point to Multiple Inheritance Implementation of Multiple inheritance Virtual Table 29 / 44

57 Thunks Solution: thunks... are trampoline methods, delegating the virtual method to its original implementation with an adapted this-reference define f(%class.* %this, i32 %i) { %1 = bitcast %class.* %this to i8* %2 = getelementptr i8* %1, i64-16 ; sizeof()=16 %3 = bitcast i8* %2 to %class.* %4 = call %3, i32 %i) ret i32 %4 } -in--vtable entry for f(int) is the thunk _f(int) Multiple Inheritance Implementation of Multiple inheritance Virtual Table 30 / 44

58 Thunks Solution: thunks... are trampoline methods, delegating the virtual method to its original implementation with an adapted this-reference define f(%class.* %this, i32 %i) { %1 = bitcast %class.* %this to i8* %2 = getelementptr i8* %1, i64-16 ; sizeof()=16 %3 = bitcast i8* %2 to %class.* %4 = call %3, i32 %i) ret i32 %4 } -in--vtable entry for f(int) is the thunk _f(int) _f(int) adds the statically constant to this before the call to f(int) Multiple Inheritance Implementation of Multiple inheritance Virtual Table 30 / 44

59 Thunks Solution: thunks... are trampoline methods, delegating the virtual method to its original implementation with an adapted this-reference define f(%class.* %this, i32 %i) { %1 = bitcast %class.* %this to i8* %2 = getelementptr i8* %1, i64-16 ; sizeof()=16 %3 = bitcast i8* %2 to %class.* %4 = call %3, i32 %i) ret i32 %4 } -in--vtable entry for f(int) is the thunk _f(int) _f(int) adds the statically constant to this before the call to f(int) f(int) addresses its locals relative to what it assumes to be a pointer Multiple Inheritance Implementation of Multiple inheritance Virtual Table 30 / 44

60 ut what if there are common ancestors? Multiple Inheritance Implementation of Multiple inheritance Virtual Table 31 / 44

61 ommon ases Duplicated ases Standard ++ multiple inheritance conceptually duplicates representations for common ancestors: L int f(int) int l int f(int) int a int f(int) int b int c Multiple Inheritance Implementation of Multiple inheritance Duplicated base classes 32 / 44

62 ommon ases Duplicated ases Standard ++ multiple inheritance conceptually duplicates representations for common ancestors: L int f(int) int l L int f(int) int l int f(int) int a int f(int) int b int c Multiple Inheritance Implementation of Multiple inheritance Duplicated base classes 32 / 44

63 Duplicated ase lasses class L { int l; virtual void f(int); class : public L { int a; void f(int); class : public L { int b; void f(int); class : public, public { int c;... c; L* pl = &c; pl->f(42); * pc = (*)pl; } vptr L int l int a vptr L int l int b int c 0 RTTI ::f RTTI ::f %class. = type { %class., %class., i32, [4 x i8] } %class. = type { [12 x i8], i32 } %class. = type { [12 x i8], i32 } %class.l = type { i32 (...)**, i32 }! mbiguity! L* pl = (*)&c; * pc = (*)(*)pl; Multiple Inheritance Implementation of Multiple inheritance Duplicated base classes 33 / 44

64 ommon ases Shared ase lass Optionally, ++ multiple inheritance enables a shared representation for common ancestors, creating the diamond pattern: W int f(int) int g(int) int h(int) int w virtual virtual int f(int) int a int g(int) int b int h(int) int c Multiple Inheritance Implementation of Multiple inheritance ommon base classes 34 / 44

65 Shared ase lass class W { int w; virtual void f(int); virtual void g(int); virtual void h(int); class : public virtual W { int a; void f(int); class : public virtual W { int b; void g(int); class : public, public { int c; void h(int);... * pc; pc->f(42); ((W*)pc)->h(42); ((*)pc)->f(42); vptr int a vptr int b int c vptr W int w W 0 RTTI ::f ::h W- RTTI ::g W RTTI ::Wf ::Wg ::Wh! Offsets to virtual base! mbiguities e.g. overwriting f in and Multiple Inheritance Implementation of Multiple inheritance ommon base classes 35 / 44

66 Dynamic Type asts class : public virtual W {... class : public virtual W {... class : public, public {... class D : public, public { c; W* pw = &c; * pc = (*)pw; // ompile error vs. * pc = dynamic_cast<*>(pw); W vptr int a vptr int b int c vptr int w vptr int a vptr int b int c vptr int b D int d vptr W int w Multiple Inheritance Implementation of Multiple inheritance ommon base classes 36 / 44

67 Dynamic Type asts class : public virtual W {... class : public virtual W {... class : public, public {... class D : public, public { c; W* pw = &c; * pc = (*)pw; // ompile error vs. * pc = dynamic_cast<*>(pw); W vptr int a vptr int b int c vptr int w vptr int a vptr int b int c vptr int b D int d vptr W int w! No guaranteed constant offsets between virtual bases and subclasses No static casting! Multiple Inheritance Implementation of Multiple inheritance ommon base classes 36 / 44

68 Dynamic Type asts class : public virtual W {... class : public virtual W {... class : public, public {... class D : public, public { c; W* pw = &c; * pc = (*)pw; // ompile error vs. * pc = dynamic_cast<*>(pw); W vptr int a vptr int b int c vptr int w vptr int a vptr int b int c vptr int b D int d vptr W int w! No guaranteed constant offsets between virtual bases and subclasses No static casting!! Dynamic casting makes use of offset-to-top Multiple Inheritance Implementation of Multiple inheritance ommon base classes 36 / 44

69 gain: asting Issues class W { virtual int f(int); class : virtual W { int a; class : virtual W { int b; class : public, public { int c; int f(int); * b = new (); b->f(42); vptr RTTI ::f ::f? { W RTTI ::Wf ::f W* w = new (); w->f(42); vptr ::Wf Multiple Inheritance Implementation of Multiple inheritance ommon base classes 37 / 44

70 gain: asting Issues class W { virtual int f(int); class : virtual W { int a; class : virtual W { int b; class : public, public { int c; int f(int); * b = new (); b->f(42); vptr RTTI ::f ::f? { W RTTI ::Wf ::f W* w = new (); w->f(42); vptr ::Wf! In a conventional thunk ::f adjusts the this-pointer with a statically known constant to point to Multiple Inheritance Implementation of Multiple inheritance ommon base classes 37 / 44

71 Virtual Thunks class W {... virtual void g(int); class : public virtual W {... class : public virtual W { int b; void g(int i){ class : public,public {... c; W* pw = &c; pw->g(42); Multiple Inheritance Implementation of Multiple inheritance ommon base classes 38 / 44 vptr int a vptr int b int c vptr W int w define g(%class.* %this, i32 %i) { ; virtual thunk to ::g %1 = bitcast %class.* %this to i8* %2 = bitcast i8* %1 to i8** %3 = load i8** %2 ; load W-vtable ptr %4 = getelementptr i8* %3, i64-32 ; -32 bytes is g-entry in vcalls %5 = bitcast i8* %4 to i64* %6 = load i64* %5 ; load g s vcall offset %7 = getelementptr i8* %1, i64 %6 ; navigate to vcalloffset+ Wtop %8 = bitcast i8* %7 to %class.* call %8, i32 %i) ret void } W 0 RTTI ::f ::h W- RTTI ::g W- W- W- W RTTI ::Wf ::Wg ::Wh

72 Virtual Tables for Virtual ases ( ++-I) Virtual Table for a Virtual Subclass gets a virtual base pointer Virtual Table for a Virtual ase consists of different parts: 1 virtual call offsets per virtual function for adjusting this dynamically 2 offset to top of an enclosing objects heap representation 3 typeinfo pointer to an RTTI object (not relevant for us) 4 virtual function pointers for resolving virtual methods Virtual ase classes have virtual thunks which look up the offset to adjust the this pointer to the correct value in the virtual table! W- RTTI ::g W- W- W- W RTTI ::Wf ::Wg ::Wh Multiple Inheritance Implementation of Multiple inheritance ommon base classes 39 / 44

73 ompiler and Runtime ollaboration ompiler generates: 1... one code block for each method 2... one virtual table for each class-composition, with references to the most recent implementations of methods of a unique common signature ( single dispatching) sub-tables for the composed subclasses static top-of-object and virtual bases offsets per sub-table (virtual) thunks as this-adapters per method and subclass if needed Runtime: 1 t program startup virtual tables are globally created 2 llocation of memory space for each object followed by constructor calls 3 onstructor stores pointers to virtual table (or fragments) in the objects 4 Method calls transparently call methods statically or from virtual tables, unaware of real class identity 5 Dynamic casts may use offset-to-top field in objects Multiple Inheritance Implementation of Multiple inheritance ompiler and Runtime ollaboration 40 / 44

74 Polemics of Multiple Inheritance Full Multiple Inheritance (FMI) Removes constraints on parents in inheritance More convenient and simple in the common cases Occurance of diamond pattern not as frequent as discussions indicate Multiple Interface Inheritance (MII) simpler implementation Interfaces and aggregation already quite expressive Too frequent use of FMI considered as flaw in the class hierarchy design Multiple Inheritance Discussion 41 / 44

75 Lessons Learned Lessons Learned 1 Different purposes of inheritance 2 Heap Layouts of hierarchically constructed objects in ++ 3 Virtual Table layout 4 LLVM IR representation of object access code 5 Linearization as alternative to explicit disambiguation 6 Pitfalls of Multiple Inheritance Multiple Inheritance Discussion 42 / 44

76 Sidenote for MS V++ the presented approach is implemented in GNU ++ and LLVM Microsoft s MS V++ approaches multiple inheritance differently splits the virtual table into several smaller tables keeps a vbptr (virtual base pointer) in the object representation, pointing to the virtual base of a subclass. Multiple Inheritance Discussion 43 / 44

77 Further reading... K. arrett,. assels, P. Haahr, D. Moon, K. Playford, and T. Withington. monotonic superclass linearization for dylan. In Object Oriented Programming Systems, Languages, and pplications, odesourcery, ompaq, EDG, HP, IM, Intel, R. Hat, and SGI. Itanium ++ I. URL: R. Ducournau and M. Habib. On some algorithms for multiple inheritance in object-oriented programming. In Proceedings of the European onference on Object-Oriented Programming (EOOP), R. Kleckner. ringing clang and llvm to visual c++ users. URL: Liskov. Keynote address data abstraction and hierarchy. In ddendum to the proceedings on Object-oriented programming systems, languages and applications, OOPSL 87, pages 17 34, L. L. R. Manual. Llvm project. URL: R. Multiple. Martin. Inheritance Further materials 44 / 44

Programming Languages

Programming Languages TEHNISHE UNIVERSITÄT MÜNHEN FKULTÄT FÜR INFORMTIK Programming Languages Multiple Inheritance Dr. Michael Petter Winter term 2015 Multiple Inheritance 1 / 44 Inheritance Principles 1 Interface Inheritance

More information

C++ Yanyan SHEN. slide 1

C++ Yanyan SHEN. slide 1 C++ Yanyan SHEN slide 1 History C++ is an object-oriented extension of C Designed by Bjarne Stroustrup at Bell Labs His original interest at Bell Labs was research on simulation Early extensions to C are

More information

Inheritance, Polymorphism and the Object Memory Model

Inheritance, Polymorphism and the Object Memory Model Inheritance, Polymorphism and the Object Memory Model 1 how objects are stored in memory at runtime? compiler - operations such as access to a member of an object are compiled runtime - implementation

More information

Last Class: Multiple Inheritance. Implementing Polymorphism. Criteria. Problem. Smalltalk Message Passing. Smalltalk

Last Class: Multiple Inheritance. Implementing Polymorphism. Criteria. Problem. Smalltalk Message Passing. Smalltalk Last lass: Multiple Inheritance Multiple Inheritance Renaming Delegation Tricky cases in inheritance (stretchable circles) Implementing Polymorphism Not in the book... Problem How to efficiently find and

More information

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Week 7. Statically-typed OO languages: C++ Closer look at subtyping C++ & Subtyping Week 7 Statically-typed OO languages: C++ Closer look at subtyping Why talk about C++? C++ is an OO extension of C Efficiency and flexibility from C OO program organization from Simula

More information

Pointer Analysis for C/C++ with cclyzer. George Balatsouras University of Athens

Pointer Analysis for C/C++ with cclyzer. George Balatsouras University of Athens Pointer Analysis for C/C++ with cclyzer George Balatsouras University of Athens Analyzes C/C++ programs translated to LLVM Bitcode Declarative framework Analyzes written in Datalog

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers Code Shape II Objects & Classes Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 L-1 Administrivia Semantics/type check due next Thur. 11/15 How s it going? Reminder: if you want

More information

Liskov Substitution Principle

Liskov Substitution Principle Liskov Substitution Principle Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhán Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/ SOLID Class Design Principles

More information

Objects, Encapsulation, Inheritance (2)

Objects, Encapsulation, Inheritance (2) CS 242 2012 Objects, Encapsulation, Inheritance (2) Reading (two lectures) Chapter 10, except section 10.4 Chapter 11, sections 11.1, 11.2, 11.3.1 and 11.4., 11.5, 11.6 only Chapter 12, sections 12.1,

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

DEVIRTUALIZATION IN LLVM

DEVIRTUALIZATION IN LLVM DEVIRTUALIZATION IN LLVM Piotr Padlewski piotr.padlewski@gmail.com University of Warsaw IIIT @PiotrPadlewski CURRENT DEVIRTUALIZATION IN THE FRONTEND struct A { ; virtual void foo(); void f() { A a; a.foo();

More information

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading How C++ Works 1 Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++

More information

An Insight Into Inheritance, Object Oriented Programming, Run-Time Type Information, and Exceptions PV264 Advanced Programming in C++

An Insight Into Inheritance, Object Oriented Programming, Run-Time Type Information, and Exceptions PV264 Advanced Programming in C++ An Insight Into Inheritance, Object Oriented Programming, Run-Time Type Information, and Exceptions PV264 Advanced Programming in C++ Nikola Beneš Jan Mrázek Vladimír Štill Faculty of Informatics, Masaryk

More information

What about Object-Oriented Languages?

What about Object-Oriented Languages? What about Object-Oriented Languages? What is an OOL? A language that supports object-oriented programming How does an OOL differ from an ALL? (ALGOL-Like Language) Data-centric name scopes for values

More information

Devirtualize Documentation

Devirtualize Documentation Devirtualize Documentation Release 0.1 Adam Schwalm January 25, 2017 Contents 1 The basics 1 1.1 Requirements............................................... 1 1.2 Installation................................................

More information

Principles of Object-Oriented Design

Principles of Object-Oriented Design Principles of Object-Oriented Design 1 The Object-Oriented... Hype What are object-oriented (OO) methods? OO methods provide a set of techniques for analysing, decomposing, and modularising software system

More information

Object Oriented Software Design - I

Object Oriented Software Design - I Object Oriented Software Design - I Object Oriented Design Principles Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2011 G. Lipari (Scuola Superiore Sant Anna)

More information

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading HOW C++ WORKS Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++

More information

Object typing and subtypes

Object typing and subtypes CS 242 2012 Object typing and subtypes Reading Chapter 10, section 10.2.3 Chapter 11, sections 11.3.2 and 11.7 Chapter 12, section 12.4 Chapter 13, section 13.3 Subtyping and Inheritance Interface The

More information

S.O.L.I.D: Software Engineering Principles

S.O.L.I.D: Software Engineering Principles DCC / ICEx / UFMG S.O.L.I.D: Software Engineering Principles Eduardo Figueiredo http://www.dcc.ufmg.br/~figueiredo S.O.L.I.D Principles These principles intend to create systems that are easier to maintain

More information

Subtyping (Dynamic Polymorphism)

Subtyping (Dynamic Polymorphism) Fall 2018 Subtyping (Dynamic Polymorphism) Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References PFPL - Chapter 24 Structural Subtyping - Chapter 27 Inheritance TAPL (pdf) - Chapter

More information

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design Dynamic Dispatch and Duck Typing L25: Modern Compiler Design Late Binding Static dispatch (e.g. C function calls) are jumps to specific addresses Object-oriented languages decouple method name from method

More information

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

More information

Programming Languages

Programming Languages TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Programming Languages Mixins Dr. Michael Petter Winter 2014 Mixins 1 / 30 What advanced techiques are there besides multiple implementation inheritance?

More information

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08 Francesco Nidito Programmazione Avanzata AA 2007/08 Outline 1 2 3 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 10 , type systems and type checking A type type is an abstraction

More information

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08 Francesco Nidito Programmazione Avanzata AA 2007/08 Outline 1 2 3 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 10 , type systems and type checking A type type is an abstraction

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

History C++ Design Goals. How successful? Significant constraints. Overview of C++

History C++ Design Goals. How successful? Significant constraints. Overview of C++ 1 CS 242 History C++ John Mitchell C++ is an object-oriented extension of C C was designed by Dennis Ritchie at Bell Labs used to write Unix based on BCPL C++ designed by Bjarne Stroustrup at Bell Labs

More information

CSE 504: Compiler Design. Runtime Environments

CSE 504: Compiler Design. Runtime Environments Runtime Environments Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Procedure Abstractions Mechanisms to manage procedures and procedure calls from compiler s perspective Runtime Environment Choices

More information

CS558 Programming Languages Winter 2013 Lecture 8

CS558 Programming Languages Winter 2013 Lecture 8 OBJECT-ORIENTED PROGRAMMING CS558 Programming Languages Winter 2013 Lecture 8 Object-oriented programs are structured in terms of objects: collections of variables ( fields ) and functions ( methods ).

More information

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Design issues for objectoriented. languages. Objects-only pure language vs mixed. Are subclasses subtypes of the superclass? Encapsulation Encapsulation grouping of subprograms and the data they manipulate Information hiding abstract data types type definition is hidden from the user variables of the type can be declared variables

More information

Intermediate Code Generation (ICG)

Intermediate Code Generation (ICG) Intermediate Code Generation (ICG) Transform AST to lower-level intermediate representation Basic Goals: Separation of Concerns Generate efficient code sequences for individual operations Keep it fast

More information

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017 Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 6 Some project extensions. Pointers and heap allocation. Object-oriented languages. Module systems. Memory structure Javalette restrictions Only local variables and parameters

More information

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for

More information

Formal Verication of C++ Object Construction and Destruction

Formal Verication of C++ Object Construction and Destruction Formal Verication of C++ Object Construction and Destruction Tahina Ramananandro 1 1 INRIA Paris-Rocquencourt November 18th, 2011 Ramananandro (INRIA) Formal verif. of C++ object constr. and destr.november

More information

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy CPSC 410? Advanced Software Engineering Mid-term Examination (Term I 2001-2002) SOLUTION Instructor: Gail Murphy Do NOT start until you are informed you can start! This examination has 7 questions. The

More information

Runtime Environment. Part II May 8th Wednesday, May 8, 13

Runtime Environment. Part II May 8th Wednesday, May 8, 13 Runtime Environment Part II May 8th 2013 Wednesday, May 8, 13 Where We Are Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

The Procedure Abstraction

The Procedure Abstraction The Procedure Abstraction Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing procedures

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Run-time Environments - 3

Run-time Environments - 3 Run-time Environments - 3 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

Homework I - Solution

Homework I - Solution CS 426 Fall 2017 1 Homework I - Solution Homework I - Solution CS 426 Compiler Construction Fall Semester 2017 1. (50 points) Intermediate representations: (a) (25 points) Construct a Control-Flow Graph

More information

The structure of a compiler

The structure of a compiler The structure of a compiler O(n) A compiler is a lot of fast stuff followed by hard problems scanning parsing intermediate code generation Polynomial time Code optimization: local dataflow, global dataflow,

More information

Fuzzing Clang to find ABI Bugs. David Majnemer

Fuzzing Clang to find ABI Bugs. David Majnemer Fuzzing Clang to find ABI Bugs David Majnemer What s in an ABI? The size, alignment, etc. of types Layout of records, RTTI, virtual tables, etc. The decoration of types, functions, etc. To generalize:

More information

Overriding Variables: Shadowing

Overriding Variables: Shadowing Overriding Variables: Shadowing We can override methods, can we override instance variables too? Answer: Yes, it is possible, but not recommended Overriding an instance variable is called shadowing, because

More information

CSE351 Winter 2016, Final Examination March 16, 2016

CSE351 Winter 2016, Final Examination March 16, 2016 CSE351 Winter 2016, Final Examination March 16, 2016 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. Please stop promptly at 4:20. There are 125 (not 100) points,

More information

Programming Languages

Programming Languages TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Programming Languages Traits Dr. Michael Petter Winter 2014 Traits 1 / 27 Is Multiple Inheritance the Ultimate Principle in Reusability? Learning

More information

Reusing Classes. Hendrik Speleers

Reusing Classes. Hendrik Speleers Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development

More information

Naming in OOLs and Storage Layout Comp 412

Naming in OOLs and Storage Layout Comp 412 COMP 412 FALL 2018 Naming in OOLs and Storage Layout Comp 412 source IR IR target Front End Optimizer Back End Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in

More information

Procedure and Object- Oriented Abstraction

Procedure and Object- Oriented Abstraction Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks

More information

Expressing high level optimizations within LLVM. Artur Pilipenko

Expressing high level optimizations within LLVM. Artur Pilipenko Expressing high level optimizations within LLVM Artur Pilipenko artur.pilipenko@azul.com This presentation describes advanced development work at Azul Systems and is for informational purposes only. Any

More information

Design Principles: Part 2

Design Principles: Part 2 Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation Principle (ISP) Design Principles: Part 2 ENGI 5895: Software Design Andrew Vardy Faculty of Engineering &

More information

Targeting LLVM IR. LLVM IR, code emission, assignment 4

Targeting LLVM IR. LLVM IR, code emission, assignment 4 Targeting LLVM IR LLVM IR, code emission, assignment 4 LLVM Overview Common set of tools & optimizations for compiling many languages to many architectures (x86, ARM, PPC, ASM.js). Integrates AOT & JIT

More information

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism. Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

Inheritance and object compatibility

Inheritance and object compatibility Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can

More information

A Context-Sensitive Memory Model for Verification of C/C++ Programs

A Context-Sensitive Memory Model for Verification of C/C++ Programs A Context-Sensitive Memory Model for Verification of C/C++ Programs Arie Gurfinkel and Jorge A. Navas University of Waterloo and SRI International SAS 17, August 30th, 2017 Gurfinkel and Navas (UWaterloo/SRI)

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Test Code Patterns. How to design your test code

Test Code Patterns. How to design your test code Test Code Patterns How to design your test code Testing and Inheritance n Should you retest inherited methods? n Can you reuse superclass tests for inherited and overridden methods? n To what extent should

More information

The Liskov Substitution Principle

The Liskov Substitution Principle Agile Design Principles: The Liskov Substitution Principle Based on Chapter 10 of Robert C. Martin, Agile Software Development: Principles, Patterns, and Practices, Prentice Hall, 2003 and on Barbara Liskov

More information

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs Test Code Patterns How to design your test code Testing and Inheritance n Should you retest inherited methods? n Can you reuse superclass tests for inherited and overridden methods? n To what extent should

More information

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

More information

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia Object Oriented Programming in Java Jaanus Pöial, PhD Tallinn, Estonia Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture TREE is hierarchical (non linear) data structure Binary trees Definitions Full tree,

More information

Types II. Hwansoo Han

Types II. Hwansoo Han Types II Hwansoo Han Arrays Most common and important composite data types Homogeneous elements, unlike records Fortran77 requires element type be scalar Elements can be any type (Fortran90, etc.) A mapping

More information

Weeks 6&7: Procedures and Parameter Passing

Weeks 6&7: Procedures and Parameter Passing CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45

More information

LLVM and IR Construction

LLVM and IR Construction LLVM and IR Construction Fabian Ritter based on slides by Christoph Mallon and Johannes Doerfert http://compilers.cs.uni-saarland.de Compiler Design Lab Saarland University 1 Project Progress source code

More information

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms

More information

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued COMP322 - Introduction to C++ Lecture 09 - Inheritance continued Dan Pomerantz School of Computer Science 11 March 2012 Recall from last time Inheritance describes the creation of derived classes from

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM. IR Generation Announcements My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM. This is a hard deadline and no late submissions will be

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Inheritance, Part 2 of 2 Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Inheritance, Part 2 of 2 1 / 14 Access Modifiers Modifier Class

More information

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli Identify and overcome the difficulties encountered by students when learning how to program List and explain the software development roles played by students List and explain the phases of the tight spiral

More information

Java and C CSE 351 Spring

Java and C CSE 351 Spring Java and C CSE 351 Spring 2018 https://xkcd.com/801/ Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq

More information

Programming Languages

Programming Languages TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Programming Languages Mixins Dr. Axel Simon and Dr. Michael Petter Winter term 2012 Mixins 1 / 35 What advanced techiques are there besides multiple

More information

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

More information

Object Oriented Programming. Spring 2008

Object Oriented Programming. Spring 2008 Dynamic Binding Implementation Object Oriented Programming 236703 Spring 2008 1 Implementation of Virtual Functions class Ellipse { //... public: E 1 virtual void draw() const; draw E + virtual void hide()

More information

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Java and C I. CSE 351 Spring Instructor: Ruth Anderson Java and C I CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Homework 5 Due TONIGHT Wed

More information

Design Principles: Part 2

Design Principles: Part 2 Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation Principle (ISP) Design Principles: Part 2 ENGI 5895: Software Design Andrew Vardy Faculty of Engineering &

More information

C++ Modern and Lucid C++ for Professional Programmers

C++ Modern and Lucid C++ for Professional Programmers Informatik C++ Modern and Lucid C++ for Professional Programmers part 13 Prof. Peter Sommerlad Institutsleiter IFS Institute for Software Rapperswil, HS 2015 Inheritance and dynamic Polymorphism base classes,

More information

Chapter 5. Names, Bindings, and Scopes

Chapter 5. Names, Bindings, and Scopes Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative

More information

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types and Computer Sciences Computer Science Division CS 164 Fall 2006 P. N. Hilfinger Static Analysis: Scope and Types 1 Terminology Programs, in general, are simply collections of definitions of terms, which

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 10a Andrew Tolmach Portland State University 1994-2017 Object-oriented Programming Programs are structured in terms of objects: collections of variables

More information

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CPSC 3740 Programming Languages University of Lethbridge. Data Types Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type

More information

Inheritance (Chapter 7)

Inheritance (Chapter 7) Inheritance (Chapter 7) Prof. Dr. Wolfgang Pree Department of Computer Science University of Salzburg cs.uni-salzburg.at Inheritance the soup of the day?! Inheritance combines three aspects: inheritance

More information

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design. Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation Principle (ISP) Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 520 Theory and Practice of Software Engineering Fall 2018 Today CS 520 Theory and Practice of Software Engineering Fall 2018 Object Oriented (OO) Design Principles September 13, 2018 Code review and (re)design of an MVC application (and encapsulation) Polymorphism

More information

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412 COMP 412 FALL 2017 Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412 source IR Front End Optimizer Back End IR target Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

More information

CPS 506 Comparative Programming Languages. Programming Language

CPS 506 Comparative Programming Languages. Programming Language CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support

More information

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns Last Time: Object Design Comp435 Object-Oriented Design Week 7 Computer Science PSU HBG The main idea RDD: Responsibility-Driven Design Identify responsibilities Assign them to classes and objects Responsibilities

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

More On inheritance. What you can do in subclass regarding methods:

More On inheritance. What you can do in subclass regarding methods: More On inheritance What you can do in subclass regarding methods: The inherited methods can be used directly as they are. You can write a new static method in the subclass that has the same signature

More information

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4 Structural Programming and Data Structures Winter 2000 CMPUT 102: Inheritance Dr. Osmar R. Zaïane Course Content Introduction Objects Methods Tracing Programs Object State Sharing resources Selection Repetition

More information