An <strong>Object</strong> <strong>Oriented</strong> Language 10Table 1: rCos/g Syntaxprogram ::= cdecl • Mainprogramcdecl ::= class Cclass declaration[〈α cnstr〉]type parameters[extends Cc]direct superclass{adef ; mdef }memberscnstr ::=constraints[extends Cu]superclass constraint[implements Cn] member constraintadef ::= visib Te a attribute definitionvisib ::= private | protect | public visibilitymdef ::= m(Te x) {c} method definitionCu ::= Cc | αsuperclass upper boundTe ::=type expressionZ | B | Sprimitive types| CeCe ::= Cc | Cn | αclass expressionsCn ::= ⋓ Ccclass conjunctionCc ::= C | Cjconcrete classesCj ::= C〈α ↦→ Te〉class instantiationc ::=commandskip| var Te x variable declaration| end x variable undeclaration| Cc.new(le) object creation| le := e assignment| e.m(ve • x • re) method invocation| c; c | c ⊳ e ⊲ c | e ∗ c structural commandsve ::= evalue argumentre ::= leresult argumente ::=expressionle | self | null | l| (Ce)e type cast| f(e) built-in function appl.le ::=l-expressionx| e.a object attributeextv ::= Te xexternal variable decl.Main ::= {extv; c}main method definitionAn attribute definition adef consists of a visibility specification visib and an attribute typing.A method definition mdef is a method signature consisting of a method name and a <strong>for</strong>malparameter typing list, followed by a command as the method body.A type is specified by a type expression Te, which is either a primitive type or a class expression.Class expressions Ce are <strong>for</strong>med from classes, type variables, class conjunctions and class instantiations.Among class expressions, classes and class instantiations are called concrete classes,Report No. 448, June 2011UNU-IIST, P.O. Box 3058, Macao
<strong>Type</strong> <strong>Graph</strong>s 11denoted by Cc. Only concrete classes can have class instances, thus we can only invoke the newcommand on concrete classes. A class conjunction Cn is a collection of member signatures of aset of concrete classes. Class conjunctions are used as class interfaces to describe memberwiserequirements and assumptions. A class instantiation Cj represents a new instance of a genericclass, in the <strong>for</strong>m of the generic class name followed by a substitution of type variables with typeexpressions.A command c in rcos/g is similar to those in common imperative languages. We provide structuralconstructors of commands including sequential compositions c; c, conditionals c ⊳ e ⊲ cand while-loops e ∗ c. We require that commands, including assignments, object creations andmethod invocations, should not occur in expressions, so that the evaluation of expressions has noside-effects. We also explicitly <strong>for</strong>mulate variable declarations and undeclarations as commands<strong>for</strong> local scope introductions and cleanups, respectively.A method invocation specifies the method name and the object upon which the method isinvoked, as well as how the value arguments ve are passed to and how the result arguments reare retrieved from the <strong>for</strong>mal parameters x. Upon entering the method, the value of a valueargument is copied to the corresponding <strong>for</strong>mal parameter. And upon leaving the method, thevalue of a <strong>for</strong>mal parameter is copied to the corresponding result argument. For example, themethod invocationo.m(true, 4 • x, y • p.a, q.b)invokes method m upon object o, passing true to x, 4 to y when entering, and retrieving p.afrom x, q.b from y when leaving.Expressions are <strong>for</strong>med from variables, attributes and literals through applications of membernavigations e.a, typecasts (Ce)e and primitive-type functions f(e). We denote the null objectby null, and use the special variable self to point to the currently active object. The set ofl-expressions, ranged over by le, includes variables and navigation paths, which are allowed tooccur on the left-hand side of assignments.To simplify our discussion, all variables, parameters and attributes are initialized with the defaultvalue of their types, specifically, 0 <strong>for</strong> integers, false <strong>for</strong> booleans, ε (empty) <strong>for</strong> text strings andnull <strong>for</strong> all class types.3 <strong>Type</strong> <strong>Graph</strong>sThe class declarations cdecl of an oo program define the structure of the objects of the programin terms of their types and relations. This structure can be <strong>for</strong>mally represented by a directedand labeled graph [9], called a type graph. In this paper, we develop a graph-based type system<strong>for</strong> rcos/g. For this, we extend the model in [9] to capture the extended features of the language.Report No. 448, June 2011UNU-IIST, P.O. Box 3058, Macao