Skip to content

Commit

Permalink
Rename MaybePointer to NullablePointer (#3293)
Browse files Browse the repository at this point in the history
Resolves #3273 as of [RFC 63](https://github.com/ponylang/rfcs/blob/master/text/0063-rename-maybe-pointer.md)

According to my research all the changes in files relates to MaybePointer and only MaybePointer.

Excerps from the old code follows as a proof of thought:

codegen.h
```
typedef struct compile_t
{
  const char* str_Maybe;
}
```
codegen.c
```
static void init_runtime(compile_t* c)
{
  c->str_Maybe = stringtab("MaybePointer");
}
```
genserialise.c
```
if(package == c->str_builtin)
{
  // Don't serialise MaybePointer[A]
  if(name == c->str_Maybe)
    return true;
}
```
gentype.c
```
if(package == c->str_builtin)
{
  if(name == c->str_Pointer)
    genprim_pointer_methods(c, t);
  else if(name == c->str_Maybe)
    genprim_maybe_methods(c, t);
}
```
ffi.c
```
// Parameter type is Pointer[None]
// If the argument is Pointer[A], MaybePointer[A] or USize, allow it
while(ast_id(arg_type) == TK_ARROW)
  arg_type = ast_childidx(arg_type, 1);

if(is_pointer(arg_type) ||
  is_maybe(arg_type) ||
  is_literal(arg_type, "USize"))
  return true;
```
reference.c
```
if(is_maybe(ast))
{
  // MaybePointer[A] must be bound to a struct.
  ...
```
subtype.c
```
bool is_maybe(ast_t* type)
{
  return is_literal(type, "MaybePointer");
}
```
gentrace.c
```
if(is_maybe(type))
  return TRACE_MAYBE;
```
  • Loading branch information
avitkauskas authored and SeanTAllen committed Sep 3, 2019
1 parent 9a7c564 commit b1fe1a0
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to the Pony compiler and standard library will be documented

### Changed

- Rename MaybePointer to NullablePointer ([PR #3293](https://github.com/ponylang/ponyc/pull/3293))

## [0.31.0] - 2019-08-31

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
struct MaybePointer[A]
struct NullablePointer[A]
"""
A MaybePointer[A] is used to encode a possibly-null type. It should
A NullablePointer[A] is used to encode a possibly-null type. It should
_only_ be used for structs that need to be passed to and from the C FFI.
An optional type for anything that isn't a struct should be encoded as a
union type, for example (A | None).
"""
new create(that: A) =>
"""
This re-encodes the type of `that` from A to MaybePointer[A], allowing
`that` to be assigned to a field or variable of type MaybePointer[A]. It
This re-encodes the type of `that` from A to NullablePointer[A], allowing
`that` to be assigned to a field or variable of type NullablePointer[A]. It
doesn't allocate a wrapper object: there is no containing object for `that`.
"""
compile_intrinsic

new none() =>
"""
This returns a null pointer typed as a MaybePointer[A].
This returns a null pointer typed as a NullablePointer[A].
"""
compile_intrinsic

fun apply(): this->A ? =>
"""
This re-encodes the type of `this` from MaybePointer[A] to A, allowing
This re-encodes the type of `this` from NullablePointer[A] to A, allowing
`this` to be assigned to a field of variable of type A. If `this` is a null
pointer, an error is raised.
"""
Expand Down
12 changes: 6 additions & 6 deletions packages/builtin_test/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ actor Main is TestList
test(_TestUnsignedPartialArithmetic)
test(_TestNextPow2)
test(_TestNumberConversionSaturation)
test(_TestMaybePointer)
test(_TestNullablePointer)
test(_TestLambdaCapture)
test(_TestValtrace)

Expand Down Expand Up @@ -2688,22 +2688,22 @@ struct _TestStruct
var i: U32 = 0
new create() => None

class iso _TestMaybePointer is UnitTest
class iso _TestNullablePointer is UnitTest
"""
Test the MaybePointer type.
Test the NullablePointer type.
"""
fun name(): String => "builtin/MaybePointer"
fun name(): String => "builtin/NullablePointer"

fun apply(h: TestHelper) ? =>
let a = MaybePointer[_TestStruct].none()
let a = NullablePointer[_TestStruct].none()
h.assert_true(a.is_none())

h.assert_error({() ? => let from_a = a()? })

let s = _TestStruct
s.i = 7

let b = MaybePointer[_TestStruct](s)
let b = NullablePointer[_TestStruct](s)
h.assert_false(b.is_none())

let from_b = b()?
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static void init_runtime(compile_t* c)
c->str_F32 = stringtab("F32");
c->str_F64 = stringtab("F64");
c->str_Pointer = stringtab("Pointer");
c->str_Maybe = stringtab("MaybePointer");
c->str_NullablePointer = stringtab("NullablePointer");
c->str_DoNotOptimise = stringtab("DoNotOptimise");
c->str_Array = stringtab("Array");
c->str_String = stringtab("String");
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ typedef struct compile_t
const char* str_F32;
const char* str_F64;
const char* str_Pointer;
const char* str_Maybe;
const char* str_NullablePointer;
const char* str_DoNotOptimise;
const char* str_Array;
const char* str_String;
Expand Down
8 changes: 4 additions & 4 deletions src/libponyc/codegen/gencall.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ static bool call_needs_receiver(ast_t* postfix, reach_type_t* t)
if(((compile_type_t*)t->c_type)->primitive != NULL)
return false;

// No receiver if a new Pointer or Maybe.
if(is_pointer(t->ast) || is_maybe(t->ast))
// No receiver if a new Pointer or NullablePointer.
if(is_pointer(t->ast) || is_nullable_pointer(t->ast))
return false;

return true;
Expand Down Expand Up @@ -1330,8 +1330,8 @@ LLVMValueRef gencall_alloc(compile_t* c, reach_type_t* t)
if(c_t->primitive != NULL)
return NULL;

// Do nothing for Pointer and Maybe.
if(is_pointer(t->ast) || is_maybe(t->ast))
// Do nothing for Pointer and NullablePointer.
if(is_pointer(t->ast) || is_nullable_pointer(t->ast))
return NULL;

// Use the global instance if we have one.
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/codegen/genfun.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ static bool genfun_allocator(compile_t* c, reach_type_t* t)
compile_type_t* c_t = (compile_type_t*)t->c_type;

// No allocator for machine word types or pointers.
if((c_t->primitive != NULL) || is_pointer(t->ast) || is_maybe(t->ast))
if((c_t->primitive != NULL) || is_pointer(t->ast) || is_nullable_pointer(t->ast))
return true;

const char* funname = genname_alloc(t->name);
Expand Down
6 changes: 3 additions & 3 deletions src/libponyc/codegen/genheader.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static int print_pointer_type(compile_t* c, printbuf_t* buf, ast_t* type)
ast_t* typeargs = ast_childidx(type, 2);
ast_t* elem = ast_child(typeargs);

if(is_pointer(elem) || is_maybe(elem))
if(is_pointer(elem) || is_nullable_pointer(elem))
return print_pointer_type(c, buf, elem) + 1;

print_base_type(c, buf, elem);
Expand All @@ -79,7 +79,7 @@ static int print_pointer_type(compile_t* c, printbuf_t* buf, ast_t* type)

static void print_type_name(compile_t* c, printbuf_t* buf, ast_t* type)
{
if(is_pointer(type) || is_maybe(type))
if(is_pointer(type) || is_nullable_pointer(type))
{
int depth = print_pointer_type(c, buf, type);

Expand Down Expand Up @@ -235,7 +235,7 @@ static void print_types(compile_t* c, FILE* fp, printbuf_t* buf)
fprintf(fp, "/*\n%s*/\n", ast_name(docstring));
}

if(!is_pointer(t->ast) && !is_maybe(t->ast) && !is_machine_word(t->ast))
if(!is_pointer(t->ast) && !is_nullable_pointer(t->ast) && !is_machine_word(t->ast))
{
// Forward declare an opaque type.
fprintf(fp, "typedef struct %s %s;\n\n", t->name, t->name);
Expand Down
18 changes: 9 additions & 9 deletions src/libponyc/codegen/genprim.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ void genprim_pointer_methods(compile_t* c, reach_type_t* t)
pointer_lt(c, t);
}

static void maybe_create(compile_t* c, reach_type_t* t, compile_type_t* t_elem)
static void nullable_pointer_create(compile_t* c, reach_type_t* t, compile_type_t* t_elem)
{
FIND_METHOD("create", TK_NONE);

Expand All @@ -500,7 +500,7 @@ static void maybe_create(compile_t* c, reach_type_t* t, compile_type_t* t_elem)
codegen_finishfun(c);
}

static void maybe_none(compile_t* c, reach_type_t* t)
static void nullable_pointer_none(compile_t* c, reach_type_t* t)
{
FIND_METHOD("none", TK_NONE);
start_function(c, t, m, c_t->use_type, &c_t->use_type, 1);
Expand All @@ -509,7 +509,7 @@ static void maybe_none(compile_t* c, reach_type_t* t)
codegen_finishfun(c);
}

static void maybe_apply(compile_t* c, void* data, token_id cap)
static void nullable_pointer_apply(compile_t* c, void* data, token_id cap)
{
// Returns the receiver if it isn't null.
reach_type_t* t = ((reach_type_t**)data)[0];
Expand All @@ -536,7 +536,7 @@ static void maybe_apply(compile_t* c, void* data, token_id cap)
codegen_finishfun(c);
}

static void maybe_is_none(compile_t* c, reach_type_t* t, token_id cap)
static void nullable_pointer_is_none(compile_t* c, reach_type_t* t, token_id cap)
{
// Returns true if the receiver is null.
FIND_METHOD("is_none", cap);
Expand All @@ -549,7 +549,7 @@ static void maybe_is_none(compile_t* c, reach_type_t* t, token_id cap)
codegen_finishfun(c);
}

void genprim_maybe_methods(compile_t* c, reach_type_t* t)
void genprim_nullable_pointer_methods(compile_t* c, reach_type_t* t)
{
ast_t* typeargs = ast_childidx(t->ast, 2);
ast_t* typearg = ast_child(typeargs);
Expand All @@ -560,10 +560,10 @@ void genprim_maybe_methods(compile_t* c, reach_type_t* t)
box_args[0] = t;
box_args[1] = t_elem;

maybe_create(c, t, t_elem);
maybe_none(c, t);
BOX_FUNCTION(maybe_apply, box_args);
BOX_FUNCTION(maybe_is_none, t);
nullable_pointer_create(c, t, t_elem);
nullable_pointer_none(c, t);
BOX_FUNCTION(nullable_pointer_apply, box_args);
BOX_FUNCTION(nullable_pointer_is_none, t);
}

static void donotoptimise_apply(compile_t* c, reach_type_t* t,
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/codegen/genprim.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PONY_EXTERN_C_BEGIN

void genprim_pointer_methods(compile_t* c, reach_type_t* t);

void genprim_maybe_methods(compile_t* c, reach_type_t* t);
void genprim_nullable_pointer_methods(compile_t* c, reach_type_t* t);

void genprim_donotoptimise_methods(compile_t* c, reach_type_t* t);

Expand Down
4 changes: 2 additions & 2 deletions src/libponyc/codegen/genserialise.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ bool genserialise(compile_t* c, reach_type_t* t)

if(package == c->str_builtin)
{
// Don't serialise MaybePointer[A]
if(name == c->str_Maybe)
// Don't serialise NullablePointer[A]
if(name == c->str_NullablePointer)
return true;

// Don't serialise Pointer[A]
Expand Down
20 changes: 10 additions & 10 deletions src/libponyc/codegen/gentrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
typedef enum
{
TRACE_NONE,
TRACE_MAYBE,
TRACE_NULLABLE_POINTER,
TRACE_MACHINE_WORD,
TRACE_PRIMITIVE,
TRACE_VAL_KNOWN,
Expand Down Expand Up @@ -194,7 +194,7 @@ static trace_t trace_type_union(ast_t* type)
trace = t;
break;

case TRACE_MAYBE:
case TRACE_NULLABLE_POINTER:
// Can't be in a union.
pony_assert(0);
return TRACE_NONE;
Expand Down Expand Up @@ -249,7 +249,7 @@ static trace_t trace_type_isect(ast_t* type)
switch(t)
{
case TRACE_NONE:
case TRACE_MAYBE:
case TRACE_NULLABLE_POINTER:
// Can't be in an isect.
pony_assert(0);
return TRACE_NONE;
Expand Down Expand Up @@ -322,8 +322,8 @@ static trace_t trace_type_nominal(ast_t* type)

case TK_STRUCT:
case TK_CLASS:
if(is_maybe(type))
return TRACE_MAYBE;
if(is_nullable_pointer(type))
return TRACE_NULLABLE_POINTER;

switch(cap_single(type))
{
Expand Down Expand Up @@ -379,7 +379,7 @@ static trace_t trace_type_dst_cap(trace_t src_trace, trace_t dst_trace,
{
case TRACE_NONE:
case TRACE_MACHINE_WORD:
case TRACE_MAYBE:
case TRACE_NULLABLE_POINTER:
case TRACE_PRIMITIVE:
case TRACE_DYNAMIC:
case TRACE_TAG_KNOWN:
Expand Down Expand Up @@ -467,8 +467,8 @@ static trace_t trace_type_dst_cap(trace_t src_trace, trace_t dst_trace,
}
}

static void trace_maybe(compile_t* c, LLVMValueRef ctx, LLVMValueRef object,
ast_t* type)
static void trace_nullable_pointer(compile_t* c, LLVMValueRef ctx,
LLVMValueRef object, ast_t* type)
{
// Only trace the element if it isn't NULL.
ast_t* type_args = ast_childidx(type, 2);
Expand Down Expand Up @@ -983,8 +983,8 @@ void gentrace(compile_t* c, LLVMValueRef ctx, LLVMValueRef src_value,
case TRACE_PRIMITIVE:
return;

case TRACE_MAYBE:
trace_maybe(c, ctx, dst_value, src_type);
case TRACE_NULLABLE_POINTER:
trace_nullable_pointer(c, ctx, dst_value, src_type);
return;

case TRACE_VAL_KNOWN:
Expand Down
8 changes: 4 additions & 4 deletions src/libponyc/codegen/gentype.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static bool make_opaque_struct(compile_t* c, reach_type_t* t)
c_t->mem_type = c->void_ptr;
return true;
}
else if(name == c->str_Maybe)
else if(name == c->str_NullablePointer)
{
c_t->use_type = c->void_ptr;
c_t->mem_type = c->void_ptr;
Expand Down Expand Up @@ -426,7 +426,7 @@ static bool make_struct(compile_t* c, reach_type_t* t)

case TK_STRUCT:
{
// Pointer and Maybe will have no structure.
// Pointer and NullablePointer will have no structure.
if(c_t->structure == NULL)
return true;

Expand Down Expand Up @@ -678,8 +678,8 @@ static void make_intrinsic_methods(compile_t* c, reach_type_t* t)
{
if(name == c->str_Pointer)
genprim_pointer_methods(c, t);
else if(name == c->str_Maybe)
genprim_maybe_methods(c, t);
else if(name == c->str_NullablePointer)
genprim_nullable_pointer_methods(c, t);
else if(name == c->str_DoNotOptimise)
genprim_donotoptimise_methods(c, t);
else if(name == c->str_Platform)
Expand Down
4 changes: 2 additions & 2 deletions src/libponyc/expr/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ static bool void_star_param(ast_t* param_type, ast_t* arg_type)
return false;

// Parameter type is Pointer[None]
// If the argument is Pointer[A], MaybePointer[A] or USize, allow it
// If the argument is Pointer[A], NullablePointer[A] or USize, allow it
while(ast_id(arg_type) == TK_ARROW)
arg_type = ast_childidx(arg_type, 1);

if(is_pointer(arg_type) ||
is_maybe(arg_type) ||
is_nullable_pointer(arg_type) ||
is_literal(arg_type, "USize"))
return true;

Expand Down
6 changes: 3 additions & 3 deletions src/libponyc/expr/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,9 @@ bool expr_nominal(pass_opt_t* opt, ast_t** astp)
if(!reify_defaults(typeparams, typeargs, true, opt))
return false;

if(is_maybe(ast))
if(is_nullable_pointer(ast))
{
// MaybePointer[A] must be bound to a struct.
// NullablePointer[A] must be bound to a struct.
pony_assert(ast_childcount(typeargs) == 1);
ast_t* typeparam = ast_child(typeparams);
ast_t* typearg = ast_child(typeargs);
Expand Down Expand Up @@ -945,7 +945,7 @@ bool expr_nominal(pass_opt_t* opt, ast_t** astp)
{
ast_error(opt->check.errors, ast,
"%s is not allowed: "
"the type argument to MaybePointer must be a struct",
"the type argument to NullablePointer must be a struct",
ast_print_type(ast));

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/pass/flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ ast_result_t pass_flatten(ast_t** astp, pass_opt_t* options)
AST_GET_CHILDREN(ast, id, type, init);
bool ok = true;

if(ast_id(type) != TK_NOMINAL || is_pointer(type) || is_maybe(type))
if(ast_id(type) != TK_NOMINAL || is_pointer(type) || is_nullable_pointer(type))
ok = false;

ast_t* def = (ast_t*)ast_data(type);
Expand Down
4 changes: 2 additions & 2 deletions src/libponyc/type/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -1742,9 +1742,9 @@ bool is_pointer(ast_t* type)
return is_literal(type, "Pointer");
}

bool is_maybe(ast_t* type)
bool is_nullable_pointer(ast_t* type)
{
return is_literal(type, "MaybePointer");
return is_literal(type, "NullablePointer");
}

bool is_none(ast_t* type)
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/type/subtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool is_sub_provides(ast_t* type, ast_t* provides, errorframe_t* errorf,

bool is_pointer(ast_t* type);

bool is_maybe(ast_t* type);
bool is_nullable_pointer(ast_t* type);

bool is_none(ast_t* type);

Expand Down
Loading

0 comments on commit b1fe1a0

Please sign in to comment.