Implementing Interfaces. Marwan Burelle. July 20, 2012

Similar documents
Short Notes of CS201

CS201 - Introduction to Programming Glossary By

C++ Inheritance and Encapsulation

Inheritance, Polymorphism and the Object Memory Model

Does anyone actually do this?

C++ Important Questions with Answers

Data Abstraction. Hwansoo Han

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

D Programming Language

Inheritance and Polymorphism

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Inheritance and Polymorphism

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

CS558 Programming Languages

15: Polymorphism & Virtual Functions

ECE 3574: Dynamic Polymorphism using Inheritance

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

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

Review and Recursion

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

Names, Scopes, and Bindings II. Hwansoo Han

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

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CSE 401/M501 Compilers

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 10: building large projects, beginning C++, C++ and structs

And Even More and More C++ Fundamentals of Computer Science

CS11 Introduction to C++ Fall Lecture 7

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

C++ Programming: Polymorphism

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

CSE 431S Type Checking. Washington University Spring 2013

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

C++ Yanyan SHEN. slide 1

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

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Subtyping (Dynamic Polymorphism)

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Big Bang. Designing a Statically Typed Scripting Language. Pottayil Harisanker Menon, Zachary Palmer, Scott F. Smith, Alexander Rozenshteyn

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011


September 10,

19 File Structure, Disk Scheduling

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Lecture Notes on Programming Languages

Java: introduction to object-oriented features

COURSE 2 DESIGN PATTERNS

Polymorphism. Zimmer CSCI 330

Term Paper. Daniel Sarnow. Interface-based Programming in C++ Fakultät Technik und Informatik Studiendepartment Informatik

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University


Today s lecture. CS 314 fall 01 C++ 1, page 1

Chapter 1: Object-Oriented Programming Using C++

Introduction to Programming Using Java (98-388)

CPS 506 Comparative Programming Languages. Programming Language

Variables, Memory and Pointers

CSCI 171 Chapter Outlines

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Instantiation of Template class

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

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

G Programming Languages - Fall 2012

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Chapter 5. Names, Bindings, and Scopes

Object-Oriented Concepts and Design Principles

Principles of Programming Languages. Objective-C. Joris Kluivers

QUIZ. What is wrong with this code that uses default arguments?

Heap Arrays. Steven R. Bagley

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

Inheritance, and Polymorphism.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

CSE 12 Abstract Syntax Trees

CSE 504: Compiler Design. Runtime Environments

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

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

Intermediate Code Generation

Kernel Scalability. Adam Belay

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Fast Introduction to Object Oriented Programming and C++

Chapter 1 Getting Started

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

CSE 303: Concepts and Tools for Software Development

Semantic Analysis. Lecture 9. February 7, 2018

Part VIII. More classes. Philip Blakely (LSC) C++ Introduction 226 / 370

What are the characteristics of Object Oriented programming language?

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Type Inference auto for Note: Note:

Types II. Hwansoo Han

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Strict Inheritance. Object-Oriented Programming Winter

1. Write two major differences between Object-oriented programming and procedural programming?

Object Oriented Programming with c++ Question Bank

Transcription:

Implementing marwan.burelle@lse.epita.fr http://www.lse.epita.fr/ July 20, 2012

Outline 1 2 3 4

Quick Overview of System oriented programming language Variant of C with a rationnalized syntax. Syntactic Sugar. Object Oriented extensions. is a compiler-to-compiler producing standard C code

Motivation was designed for kernel programming The aim of the project is to provide modern extensions to C (like OOP) without breaking the aspects that make C a good kernel programming language. has no run-time depencies: make it easier to integrate in a non-standard environment. We try to minimize the amount of hidden code: almost all expensive operations are explicit (no hidden object copy for example.) Since we produce C code, it is quite easy to integrate pieces of code in existing C projects.

Object Model The OOP model provides: Class definitions with virtual methods (true methods) Simple inheritance and subtyping based polymorphism and abstract methods Method overriding No overloading No visibility control In addition, objects instances follow some rules: are always pointer You can define local object on the stack You have to provide your own allocation scheme

and Abstract Methods You can define interfaces: pure abstract classes with no state and no implementation for methods. can inherit other interfaces and unlike classes inheritance, you can have multiple inheritance. Class can explicitely support interfaces (no structural typing) When implementing an interface you (as usual) must provides an implementation for each methods in the interface. You can have abstract methods in class: methods with no implementation with abstract methods can t be instantiated

in C While C does not provides objects, we can use pure C constructions to represent them. We only need structure and function pointers. Thus, OOP extensions can be encoded as (almost) pure syntactic sugar. Since we produce C code, we take advantage of automatic offset computation in structure. Using structures rather than annonymous table generate more readable code.

Basic An object is the association of some data (attributes) and operations on this data (methods) Example: We just put function pointers in a structure. class obj { get() : int; set( int x) : void; int content; } struct s_obj { int (*get)(struct s_obj*); void (* set)( struct s_obj*, int); int content; };

Calling Methods To call methods, you just have to find the pointer in the object The only things that you need is to provide the this pointer to the method. For cascade of methods call you will need temporary variables to cache results of method s calls to avoid multiple calls to the same method. Example: o. set(42); o->set(o,42);

and inheritance Defining classes is straightforward: a classes is a structure together with the initialization code to fill the structure. To avoid unnecessary duplication of pointers to methods, we build an external structure with only methods that will be shared by all instances. Inheritance is also straightforward: structures (state and methods) are ordered so that methods described in parent class come first. Example: struct s_obj_vtbl { int (* get)( struct s_obj*, void); void (* set)( struct s_obj*, int); }; struct s_obj { struct s_obj_vtbl * vtbl; int content; };

And Inheritance

Issues Unlike class inheritance, you can implement several interfaces. You can inherit from a class and implement interface. We can t rely on structures layout. What if we use more than one table of methods?

Insufficient information are used to pass objects to function without knowing their real type. So, we won t be able to access specific object content that is not described in the interface. So, we need to found a way to access some specific fields of an object without knowing the object layout.

Dynamic Dispatcher A possible solution is to have a function that select the right table of methods. This function is just a a big switch using a hash of the interface name to select the right table. Example: Now an object will look like: struct s_obj { void *( interface_table)( int); struct s_obj_vtbl * vtbl; // object content };

Avoiding Dispatch Overhead The previous method induces an overhead at each method call (a function call) The idea to avoid this overhead by transforming the object when we still have the needed information. Once the object is transformed, we don t need the dynamic dispatcher that select the correct method s table. Example: Let s take a look at the context first: interface A { get() : cint; } class B support A { get() : cint { return 42; } } f1(o : A) : cint { return o. get(); } f2(o : B) : cint { return f1(o); }

Injecting Transformation While there s various ways to implement objects transformation, the way we inject information is usually always the same. The transformation must be injected where we still know the real type of the object. Example: The previous function: f2(o : B) : cint { return f1(o); } Into the following: f2(o : B) : cint { return f1( CAST(o,A) ); }

How to transform an object There s various way to transform an object: You can change the pointer to the table of methods You can build an helper object around your object You can have all interfaces in your object and replace the pointer of the real object First two methods has a lot of drawbacks (overhead, inconsistency in concurrent context... ) The last method need some refinement

Hosting All The idea is to have inside the object a block with, for each supported interface, a mini-object with a pointer to corresponding table of methods. Example: struct s_a_vtbl { int (* get)( void); }; struct s_obj_interface_table { struct { s_a_vtbl * vtbl; } A; }; struct s_obj { struct s_a_vtbl * interfaces; struct s_obj_vtbl * vtbl; // object state struct s_a_vtbl _interfaces; }; #define CAST(_OBJ, _INTER_) \ (&((_OBJ)->interfaces ->_INTER_))

And What About this? We need to pass a this pointer to our methods. But the pointer we have in the calling context does not correspond to the real object. So, we provide a pointer to the real object inside the structure associated to the interface. Example: struct s_obj_interface_table { struct { s_a_vtbl * vtbl; void * real_this; } A; }; // Calling a method from interface context o->vtbl ->mymethod(o->real_this);

Full Object Layout

Transitive Inheritance When you support an interface that is itself derived from various interfaces, you can be transformed to any of these interfaces. So we need to provide access to these interfaces, but we can be transformed in context where we don t know how: Example: interface A { /* Some defs */ } interface B : A { /* More defs */ } class C support B { /* implem */ } f1(o : A) { /* some code */ } f2(o : B) { f1(o); } f3(o : C) { f2(o); }

Solutions? Provide a function (similar to the dynamic dispatcher) that provide a pointer to the good interfaces (dynamic cast.) Embed recursively interfaces description (probably a lot of space overhead.) We also need conversion for comparison operators.

Why Pointers To? C++ offers to manipulate object as pointer or directly. I choose to not follow this approach. There s almost no reason to transmit objects by copy. When you need copy, you should obviously do it explicitly. Copy also introduce new issues: when using an object throught one of its parent types or throught an interface, you can t simply rely on C to correctly copy the struct since it doesn t know the size.

Why Do We Have And Structs in You may use structures for other reason than structured data A good example is when you want a bunch of contiguous heterogeneous data For a lot of cases you don t want meta-data in your structure. There s also cases where you want data to be copied to called functions without overhead. So, classes and structures serve different purposes and we need both. If you want member functions (or non-virtual methods) acting on structures you can use macro-class in

More on Git repository: http://git.lse.epita.fr/?p=cbang.git Redmine Project: http://redmine.lse.epita.fr/projects/cbang Global Overview Blog s article: http://blog.lse.epita.fr/articles/ 12-c---system-oriented-programming.html syntax overview: http://blog.lse.epita.fr/articles/ 23-c---system-oriented-programming---syntax-explanati. html

QUESTIONS? Figs/questionMark