Core C++

7.2. Operator members🔗

Definition7.2.1
Group: Overloading, polymorphism, subtyping and inference. (8)
Group member previews
Preview
Definition 7.1.1
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
Statement uses 2
Statement dependency previews
Preview
Definition 6.2.3
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
used by 1✓L∃∀N

An infix operator whose left operand is an object, and the indexing of an object, are the calls of members. The left operand decides, so no operator on int or bool changes meaning, and && and || are not overloaded.

\dfrac{\begin{array}{c} \Gamma \vdash e_1 : C\qquad C \text{ has } \mathtt{operator}\oplus \text{ visible from } \Gamma \\ \Gamma \vdash e_1.\mathtt{operator}\oplus(e_2) : \tau \end{array}}{\Gamma \vdash e_1 \oplus e_2 : \tau}\;\textsf{(T-OpBin)}

\dfrac{\begin{array}{c} \Gamma \vdash e : C\qquad C \text{ has } \mathtt{operator[]} \text{ visible from } \Gamma \\ \Gamma \vdash e.\mathtt{operator[]}(i) : \tau \end{array}}{\Gamma \vdash e[i] : \tau}\;\textsf{(T-OpIndex)}

The type checker rewrites both forms into the call of the member, and from there they are ordinary method calls, with the visibility, the overload resolution and the dispatch of a method. The trace shows the rule MethodCall under the infix form, which is the point.

Some of the implementations are partial, so Lean records opaque constants that carry the type and not the body, and no property of them is proved here.

Lean code for Definition7.2.1●2 definitions
  • defdefined in CoreCpp/Parser.lean
    complete
    def CoreCpp.operatorName : CoreCpp.P String
    def CoreCpp.operatorName : CoreCpp.P String
    The operators a class may overload, the nonterminal `Op`.
    
    ```
    Op = "+" | "-" | "*" | "/" | "%" | "==" | "!="
       | "<" | "<=" | ">" | ">=" | "[" "]" ;
    ```
    
  • opaquedefined in CoreCpp/Typing.lean
    complete
    opaque CoreCpp.Typing.methodCall (p : CoreCpp.Program) (Γ : CoreCpp.TEnv)
      (recv : CoreCpp.Expr) (arrow : Bool) (m : String)
      (es : List CoreCpp.Expr) : CoreCpp.T CoreCpp.Ty
    opaque CoreCpp.Typing.methodCall
      (p : CoreCpp.Program) (Γ : CoreCpp.TEnv)
      (recv : CoreCpp.Expr) (arrow : Bool)
      (m : String) (es : List CoreCpp.Expr) :
      CoreCpp.T CoreCpp.Ty
    The rules T-Method and T-MethodArrow, see `expr`. 
Definition7.2.2
Group: Overloading, polymorphism, subtyping and inference. (8)
Group member previews
Preview
Definition 7.1.1
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
Statement uses 2
Statement dependency previews
Preview
Definition 2.3.2
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
used by 0✓L∃∀N

A member may return \tau\&. Every return of its body is then over an expression that denotes a location, checked by a walk that does not enter the body of a lambda, because the return of a lambda is the lambda's.

\dfrac{\tau \text{ has values} \qquad \text{every return of } c \text{ is return } e \text{ with } \Gamma \vdash_{\ell} e : \tau}{\vdash \tau\&\ m(\ldots)\ \{c\} \text{ in } C}\;\textsf{(T-RetRef)}

\dfrac{\Gamma \vdash e : C \qquad C \text{ has } \tau\&\ \mathtt{operator[]} \text{ visible from } \Gamma}{\Gamma \vdash_{\ell} e[i] : \tau}\;\textsf{(T-LocOpIndex)}

\dfrac{\Gamma \vdash e : C \qquad C \text{ has } \tau\&\ m(\ldots) \text{ visible from } \Gamma}{\Gamma \vdash_{\ell} e.m(e_1, \ldots, e_k) : \tau}\;\textsf{(T-LocMethod)}

The annotation rewrites the return e of such a member as return &e, the location as a value, an expression no program writes. Its type is the type of the location it denotes.

\dfrac{\Gamma \vdash_{\ell} e : \tau}{\Gamma \vdash \&e : \tau}\;\textsf{(T-LocOf)}

\dfrac{\rho, \sigma \vdash e \Rightarrow_{\ell} \ell, \sigma'}{\rho, \sigma \vdash \&e \Rightarrow \mathsf{loc}\ \ell, \sigma'}\;\textsf{(LocOf)}

\dfrac{\text{the member } m \text{ of } C \text{ returns } \tau\& \qquad \text{member } \ell\ (e_1, \ldots, e_k) \Rightarrow \mathsf{loc}\ \ell', \sigma'}{\rho, \sigma \vdash e.m(e_1, \ldots, e_k) \Rightarrow_{\ell} \ell', \sigma'}\;\textsf{(MethodLoc)}

In value position the same call reads \sigma'(\ell'). A location a member returns is that of a field or of an element of a vector of the receiver, and it survives the call because the object lives in \sigma. A member that returned the location of a parameter by value would return a freed location, and reading it is error, by the general rule of locations outside \sigma.

Some of the implementations are partial, so Lean records opaque constants that carry the type and not the body, and no property of them is proved here.

Lean code for Definition7.2.2●4 definitions
  • structure(8 fields)defined in CoreCpp/Syntax.lean
    complete
    structure CoreCpp.Method : Type
    structure CoreCpp.Method : Type
    A method, defined inside the class. `isVirtual` marks `virtual`, which
    makes the call dispatch by the class tag of the receiver, and `isOverride`
    marks `override`, required on a method that redefines a `virtual` one. An
    operator member is a method whose name is `operator` followed by the
    operator, `operator+` or `operator[]`, which the type checker reaches by
    rewriting the infix form. 
    name : String
    The name of the method. 
    ret : CoreCpp.Ty
    The result type. 
    params : List CoreCpp.Param
    The parameters. 
    body : List CoreCpp.Cmd
    The body, run with `this` bound to the receiver. 
    vis : CoreCpp.Vis
    The visibility of the method. 
    isVirtual : Bool
    Whether the method is declared `virtual`. 
    isOverride : Bool
    Whether the method is declared `override`. 
    retRef : Bool
    Whether the method returns a reference, `τ& m(…)`, so that a call to it
    denotes the location the body returns. 
  • opaquedefined in CoreCpp/Typing.lean
    complete
    opaque CoreCpp.Typing.refReturns (p : CoreCpp.Program) (Γ : CoreCpp.TEnv)
      (m : String) : List CoreCpp.Cmd → CoreCpp.T Unit
    opaque CoreCpp.Typing.refReturns
      (p : CoreCpp.Program) (Γ : CoreCpp.TEnv)
      (m : String) :
      List CoreCpp.Cmd → CoreCpp.T Unit
    Every `return` of a member that returns a reference is a `return e` with
    `e` denoting a location. The walk does not enter the body of a lambda, whose
    `return` is the lambda's own.
    
    ```
    τ has values    every return of c is return e with Γ ⊢ₗ e : τ
    ─────────────────────────────────────────────────────────────── (T-RetRef)
    ⊢ τ& m(…) { c } in C
    ```
    
  • opaquedefined in CoreCpp/Typing.lean
    complete
    opaque CoreCpp.Typing.refRets : List CoreCpp.Cmd → List CoreCpp.Cmd
    opaque CoreCpp.Typing.refRets :
      List CoreCpp.Cmd → List CoreCpp.Cmd
    The `return e` of a member that returns a reference becomes `return &e`,
    the location as a value, which the caller reads or uses as a location. The
    walk does not enter the body of a lambda. 
  • opaquedefined in CoreCpp/Eval.lean
    complete
    opaque CoreCpp.Eval.callMethod (fs : CoreCpp.FunEnv) (ρ : CoreCpp.Env)
      (σ : CoreCpp.Store) (recv : CoreCpp.Expr) (arrow : Bool) (m : String)
      (es : List CoreCpp.Expr) (static : Option String)
      (sig : Option (List CoreCpp.Ty)) :
      CoreCpp.M (CoreCpp.Method × CoreCpp.Val × CoreCpp.Store)
    opaque CoreCpp.Eval.callMethod
      (fs : CoreCpp.FunEnv) (ρ : CoreCpp.Env)
      (σ : CoreCpp.Store)
      (recv : CoreCpp.Expr) (arrow : Bool)
      (m : String) (es : List CoreCpp.Expr)
      (static : Option String)
      (sig : Option (List CoreCpp.Ty)) :
      CoreCpp.M
        (CoreCpp.Method ×
          CoreCpp.Val × CoreCpp.Store)
    The receiver, the dispatch and the call of a method, shared by the value
    position and the location position. The result of a member that returns a
    reference is the location, `loc ℓ`, which the value position reads and the
    location position takes as it is.