Difference between revisions of "DIP29"

From D Wiki
Jump to: navigation, search
(major revamp)
Line 16: Line 16:
 
|-
 
|-
 
|Last Modified:
 
|Last Modified:
|2013-02-28
+
|2013-06-24
 
|-
 
|-
 
|Language:
 
|Language:
Line 38: Line 38:
 
== Definition ==
 
== Definition ==
  
A UniqueExpression is defined as an expression yielding a pointer that can be implicitly converted to mutable, immutable, or shared.
+
Unique Value
  
== Expression Case 1: ptr+offset ==
+
Rvalues are always unique.
  
If an expression ptr+offset yields a pointer, that expression is convertible iff ptr is convertible.
+
Unique Reference
  
== Expression Case 2: ptr-offset ==
+
A reference is unique if there are no other references to the same object
 +
(including references to the object's interior).
  
If an expression ptr-offset yields a pointer, that expression is convertible iff ptr is convertible.
+
Transitively Unique Reference
  
== Expression Case 3: new T ==
+
A Unique Reference and there are no external references to any
 +
values transitively accessible through it.
  
This is convertible if T is convertible. (Not sure this is completely true.)
+
Implicitly Convertible To Immutable
  
A lot more work is needed to enumerate all the cases.
+
One of:
  
== Enhancements ==
+
1. The type is implicitly convertible to Immutable.
  
For simplicity, this proposal is restricted to pointers only. It can be enhanced to cover other reference types
+
2. Transitively Unique Reference
such as:
+
 
 +
3. Transitive graph is reachable only via immutable references
 +
 
 +
Implicitly Convertible To Shared
 +
 
 +
One of:
 +
 
 +
1. Type is implicitly convertible to Shared
 +
 
 +
2. Transitively Unique Reference; head is not immutable
 +
 
 +
3. Transitive graph is reachable only via shared or immutable references;
 +
head is not immutable
 +
 
 +
== Library Types ==
 +
 
 +
Each of these statically verifies that Expression e has the desired properties.
 +
Using the result as an rvalue results in overwriting the value with T.init.
 +
The compiler statically recognizes these types as having the desired characteristic,
 +
i.e. they are magic types.
 +
 
 +
Each has a static assume(T e) method which unsafely assumes the properties of the argument
 +
Expression e and returns an instance of the type.
 +
 
 +
Unique(T)(T e)
 +
 
 +
T: class, pointer, dynamic array, delegate
 +
 
 +
Forms a transitively unique reference
 +
 
 +
UniqueImmutable(T)(T e)
 +
 
 +
T: class, pointer, dynamic array, delegate
 +
 
 +
Implicitly convertible to immutable(T)
 +
 
 +
UniqueShared(T)(T e)
 +
 
 +
T: class, pointer, dynamic array, delegate
 +
 
 +
Implicitly convertible to shared(T)
 +
 
 +
 
 +
== Expressions ==
 +
 
 +
Value
 +
This is tried first. If it produces false, the checks for particular
 +
Expression types are then tried.
 +
 
 +
Unique
 +
If all the fields are non-reference types, then true
 +
 
 +
UniqueImmutable
 +
If type can be implicitly cast to immutable
 +
 
 +
UniqueShared
 +
If type can be implicitly cast to shared
 +
 
 +
Variable
 +
If type is Unique, UniqueImmutable, or UniqueShared
 +
 
 +
CommaExpression
 +
result of right operand
 +
 
 +
=
 +
+=
 +
-=
 +
result of left operand
 +
 
 +
ConditionalExpression
 +
result of left operand and'ed with result of right operand
 +
 
 +
AddExpression
 +
if one operand is a pointer, and the other an integral constant,
 +
then the result is the result of the pointer operand
 +
 
 +
CatExpression
 +
CatAssignExpression
 +
result is the and'ing of all the elements in the operands
 +
 
 +
CallExpression
 +
if function is pure, then result is the and'ing of all the arguments to the function
 +
 
 +
NewExpression
 +
result is the and'ing of all the arguments. If a constructor is called, and it is
 +
pure, the result includes the and'ing of all the default initializers for the fields
 +
 
 +
CastExpression
 +
result of operand being cast
 +
 
 +
Lambda
 +
FunctionLiteral
 +
result of the expression used to initialize the .ptr field of delegates
 +
 
 +
IndexExpression
 +
result of operand being indexed
 +
 
 +
SliceExpression
 +
result of operand being sliced
 +
 
 +
ArrayLiteral
 +
result is the and'ing of all the array elements
 +
 
 +
AssocArrayLiteral
 +
result is the and'ing of all the array keys and values
 +
 
 +
StructLiteral
 +
result is the and'ing of all the expressions coupled with
 +
the initializers for the rest of the fields
 +
 
 +
 
 +
== Implementation ==
 +
 
 +
Add following member functions to Expression:
 +
 
 +
bool isUniqueReference();
 +
 
 +
bool isTransitivelyUniqueReference();
 +
 
 +
bool isImplictlyConvertibleToImmutable();
 +
 
 +
bool isImplictlyConvertibleToShared();
  
* class references
 
* delegates
 
* dynamic arrays
 
  
It can also be enhanced to allow some non-unique pointers to be converted to shared pointers, if the non-unique
 
references are also shared pointers.
 
  
 
== Copyright ==
 
== Copyright ==

Revision as of 04:19, 25 June 2013

Title: Unique Pointers
DIP: 29
Version: 1
Status: Draft
Created: 2013-02-28
Last Modified: 2013-06-24
Language: D2
Breaks: Nothing, it enables code that doesn't compile at the moment
Links: Discussion on github

Discussion on n.g. More Discussion on n.g.

Abstract

Currently, pointers cannot be converted to and from shared and immutable without an explicit and unsafe cast. Unique pointers can be implicitly cast to and from shared and immutable in safety. A unique pointer can be discovered by examining the expression that generated the pointer.

Rationale

Requiring switching to unsafe code to do routine things like create immutable references is a glaring problem. By recognizing unique pointers much of this can be done safely. More advanced analysis can uncover more cases that can be done safely.

Definition

Unique Value

Rvalues are always unique.

Unique Reference

A reference is unique if there are no other references to the same object (including references to the object's interior).

Transitively Unique Reference

A Unique Reference and there are no external references to any values transitively accessible through it.

Implicitly Convertible To Immutable

One of:

1. The type is implicitly convertible to Immutable.

2. Transitively Unique Reference

3. Transitive graph is reachable only via immutable references

Implicitly Convertible To Shared

One of:

1. Type is implicitly convertible to Shared

2. Transitively Unique Reference; head is not immutable

3. Transitive graph is reachable only via shared or immutable references; head is not immutable

Library Types

Each of these statically verifies that Expression e has the desired properties. Using the result as an rvalue results in overwriting the value with T.init. The compiler statically recognizes these types as having the desired characteristic, i.e. they are magic types.

Each has a static assume(T e) method which unsafely assumes the properties of the argument Expression e and returns an instance of the type.

Unique(T)(T e)

T: class, pointer, dynamic array, delegate

Forms a transitively unique reference

UniqueImmutable(T)(T e)

T: class, pointer, dynamic array, delegate

Implicitly convertible to immutable(T)

UniqueShared(T)(T e)

T: class, pointer, dynamic array, delegate

Implicitly convertible to shared(T)


Expressions

Value This is tried first. If it produces false, the checks for particular Expression types are then tried.

Unique If all the fields are non-reference types, then true

UniqueImmutable If type can be implicitly cast to immutable

UniqueShared If type can be implicitly cast to shared

Variable If type is Unique, UniqueImmutable, or UniqueShared

CommaExpression result of right operand

= += -= result of left operand

ConditionalExpression result of left operand and'ed with result of right operand

AddExpression if one operand is a pointer, and the other an integral constant, then the result is the result of the pointer operand

CatExpression CatAssignExpression result is the and'ing of all the elements in the operands

CallExpression if function is pure, then result is the and'ing of all the arguments to the function

NewExpression result is the and'ing of all the arguments. If a constructor is called, and it is pure, the result includes the and'ing of all the default initializers for the fields

CastExpression result of operand being cast

Lambda FunctionLiteral result of the expression used to initialize the .ptr field of delegates

IndexExpression result of operand being indexed

SliceExpression result of operand being sliced

ArrayLiteral result is the and'ing of all the array elements

AssocArrayLiteral result is the and'ing of all the array keys and values

StructLiteral result is the and'ing of all the expressions coupled with the initializers for the rest of the fields


Implementation

Add following member functions to Expression:

bool isUniqueReference();

bool isTransitivelyUniqueReference();

bool isImplictlyConvertibleToImmutable();

bool isImplictlyConvertibleToShared();


Copyright

This document has been placed in the Public Domain.