Skip to content

Commit

Permalink
updates, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Jul 11, 2024
1 parent 16c69ec commit 9acc8c3
Show file tree
Hide file tree
Showing 55 changed files with 6,103 additions and 4,844 deletions.
4 changes: 2 additions & 2 deletions ownership.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Last Updated 28 June 2024
Last Updated 11 July 2024

This is a work in progress, both design and implementation. Cake source itself is being used to validate the concepts.

Expand Down Expand Up @@ -346,7 +346,7 @@ When an owner object is copied to a view object, the ownership is not transferre

#include <stdio.h>

void use_file(FILE *f) {}
void use_file(FILE * f) {}

int main() {
FILE * _Owner _Opt f = fopen("file.txt", "r");
Expand Down
5 changes: 5 additions & 0 deletions src/console.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/

#pragma safety enable


Expand Down
5 changes: 5 additions & 0 deletions src/console.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/

#pragma once

#include <stdbool.h>
Expand Down
5 changes: 5 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/


#pragma safety enable

Expand Down
5 changes: 5 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/

#pragma once

#ifndef __CAKE__
Expand Down
28 changes: 18 additions & 10 deletions src/expressions.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
//#pragma safety enable
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/

#pragma safety enable

#include "ownership.h"
#include <limits.h>
Expand Down Expand Up @@ -1937,7 +1942,7 @@ struct expression* _Owner _Opt postfix_expression_tail(struct parser_ctx* ctx, s

if (p_expression_node_new->left->type.type_specifier_flags & TYPE_SPECIFIER_STRUCT_OR_UNION)
{
struct struct_or_union_specifier* p =
struct struct_or_union_specifier* _Opt p =
find_struct_or_union_specifier(ctx, p_expression_node_new->left->type.struct_or_union_specifier->tag_name);
p = get_complete_struct_or_union_specifier(p);
if (p)
Expand Down Expand Up @@ -2655,17 +2660,20 @@ struct expression* _Owner _Opt unary_expression(struct parser_ctx* ctx)

if (check_sizeof_argument(ctx, new_expression, &new_expression->type_name->type) != 0)
{
expression_delete(new_expression);
throw;
}

if (type_is_vla(&new_expression->type_name->declarator->type))
{
//not constant
//not fatal error
//fatal will be if someone need the sizeof at compile time
//but we don't have the constant_value set here
}
else
{
new_expression->constant_value = make_constant_value_ll(type_get_sizeof(&new_expression->type_name->declarator->type), false);
if (type_is_vla(&new_expression->type_name->declarator->type))
{
//not constant
}
else
{
new_expression->constant_value = make_constant_value_ll(type_get_sizeof(&new_expression->type_name->declarator->type), false);
}
}
}
else
Expand Down
11 changes: 8 additions & 3 deletions src/expressions.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/

#pragma once

#include "type.h"
Expand Down Expand Up @@ -223,9 +228,9 @@ bool expression_is_calloc(const struct expression* p);

void expression_delete(struct expression* _Owner _Opt p);

struct expression* _Owner assignment_expression(struct parser_ctx* ctx);
struct expression* _Owner expression(struct parser_ctx* ctx);
struct expression* _Owner constant_expression(struct parser_ctx* ctx, bool show_error_if_not_constant);
struct expression* _Owner _Opt assignment_expression(struct parser_ctx* ctx);
struct expression* _Owner _Opt expression(struct parser_ctx* ctx);
struct expression* _Owner _Opt constant_expression(struct parser_ctx* ctx, bool show_error_if_not_constant);
bool expression_is_subjected_to_lvalue_conversion(const struct expression*);
bool expression_is_zero(const struct expression*);
bool expression_is_lvalue(const struct expression* expr);
Expand Down
20 changes: 15 additions & 5 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#pragma safety enable


#pragma cake diagnostic check "-E74"

struct X{
int b[sizeof(struct X)];
struct X {
void * _Owner _Opt text;
};
int main(){}

void x_change(struct X* list);
void x_destroy(struct X* _Obj_owner p);

int main()
{
struct X x = {};
x_change(&x);
static_debug(x);
}
//memory pointed by 'x.text' was not released.
#pragma cake diagnostic check "-Wmissing-destructor"
89 changes: 60 additions & 29 deletions src/flow_object.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/

//#pragma safety enable

#include "ownership.h"
Expand Down Expand Up @@ -911,10 +916,10 @@ void print_object_core(int ident,
visitor.p_object = p_visitor->p_object->current.ref.data[i];
print_object_core(ident + 1, &visitor, buffer, is_pointer, short_version, visit_number);
}
}
}
#endif
type_destroy(&t2);
}
}
else
{
printf("%*c", ident, ' ');
Expand Down Expand Up @@ -1091,7 +1096,7 @@ int object_merge_current_state_with_state_number_core(struct flow_object* object
object_merge_current_state_with_state_number_core(pointed, state_number, visit_number);
}

}
}
#endif
}
return 1;
Expand Down Expand Up @@ -1243,9 +1248,9 @@ void object_set_uninitialized_core(struct object_visitor* p_visitor)
object_set_nothing(&t2, pointed);
}
type_destroy(&t2);
}
}
#endif
}
}
else
{
p_visitor->p_object->current.state = OBJECT_STATE_UNINITIALIZED;
Expand Down Expand Up @@ -1636,9 +1641,9 @@ static void object_set_deleted_core(struct type* p_type, struct flow_object* p_o
object_set_deleted_core(&t2, pointed, visit_number);
type_destroy(&t2);
}
}
}
#endif
}
}
else
{
if (!type_is_struct_or_union(p_type))
Expand Down Expand Up @@ -1943,11 +1948,11 @@ void object_get_name_core(
outname,
out_size,
visit_number);
}
}
#endif
}
}
type_destroy(&t2);
}
}
}
}

Expand Down Expand Up @@ -2062,11 +2067,11 @@ void checked_moved_core(struct flow_visit_ctx* ctx,
p_object->current.ref.data[i],
position_token,
visit_number);
}
}
#endif
type_destroy(&t2);
}
}
}

if (p_object->current.state & OBJECT_STATE_MOVED)
{
Expand Down Expand Up @@ -2104,7 +2109,7 @@ void checked_moved_core(struct flow_visit_ctx* ctx,
compiler_diagnostic_message(W_LOCATION, ctx->ctx, name_pos, NULL, "parameter", name);
}
}
}
}
}

void checked_moved(struct flow_visit_ctx* ctx,
Expand Down Expand Up @@ -2579,7 +2584,8 @@ static void flow_assignment_core(
bool a_type_is_view,
bool a_type_is_nullable,
struct object_visitor* p_visitor_a,
struct object_visitor* p_visitor_b)
struct object_visitor* p_visitor_b,
bool* set_argument_to_unkown)
{
if (p_visitor_a->p_object == NULL || p_visitor_b->p_object == NULL)
{
Expand Down Expand Up @@ -2907,6 +2913,7 @@ static void flow_assignment_core(
p_visitor_a->p_object->current.state &= ~OBJECT_STATE_MOVED;
}


if (assigment_type == ASSIGMENT_TYPE_PARAMETER)
{
struct type t3 = type_remove_pointer(p_visitor_a->p_type);
Expand All @@ -2915,11 +2922,31 @@ static void flow_assignment_core(

if (p_visitor_b->p_object->current.pointed)
{
struct flow_object* pointed = p_visitor_b->p_object->current.pointed;
//struct flow_object* pointed = p_visitor_b->p_object->current.pointed;

//bool nullable_enabled = ctx->ctx->options.null_checks_enabled;
//const bool t3_is_nullable = type_is_nullable(&t3, nullable_enabled);

bool nullable_enabled = ctx->ctx->options.null_checks_enabled;
const bool t3_is_nullable = type_is_nullable(&t3, nullable_enabled);
object_set_unknown(&t3, t3_is_nullable, pointed, nullable_enabled);
if (set_argument_to_unkown)
{
/*
//Consider this sample...
void f(struct X *p, int * p);
int main()
{
struct X * pX = make();
if (pX->p)
{
f(pX, pX->p (not unknown yet));
//pX->p is unknown here...
}
}
*/

//Tells the caller it must make argument unknown
*set_argument_to_unkown = true;
}
// object_set_unknown(&t3, t3_is_nullable, pointed, nullable_enabled);
}


Expand Down Expand Up @@ -2983,7 +3010,8 @@ static void flow_assignment_core(
a_type_is_view,
a_type_is_nullable,
&visitor_a,
&visitor_b);
&visitor_b,
set_argument_to_unkown);
}
else
{
Expand Down Expand Up @@ -3020,7 +3048,8 @@ static void flow_assignment_core(
a_type_is_view,
a_type_is_nullable,
p_visitor_a,
p_visitor_b);
p_visitor_b,
set_argument_to_unkown);

//restore
p_visitor_a->p_type = temp1;
Expand Down Expand Up @@ -3164,13 +3193,13 @@ struct flow_object* _Opt expression_get_object(struct flow_visit_ctx* ctx, stru
//return NULL;
}
}
}
return p_object;
}
#endif
return p_object;
}
return NULL;
#endif
}
return NULL;
}
else if (p_expression->expression_type == POSTFIX_ARROW)
{
struct flow_object* p_obj = expression_get_object(ctx, p_expression->left, nullable_enabled);
Expand Down Expand Up @@ -3228,13 +3257,13 @@ struct flow_object* _Opt expression_get_object(struct flow_visit_ctx* ctx, stru
//return NULL;
}
}
}
}
return p_object;
}
#endif
}
return NULL;
}
}
else if (p_expression->expression_type == UNARY_EXPRESSION_CONTENT)
{
struct flow_object* p_obj = expression_get_object(ctx, p_expression->right, nullable_enabled);
Expand Down Expand Up @@ -3378,7 +3407,7 @@ struct flow_object* _Opt expression_get_object(struct flow_visit_ctx* ctx, stru
printf("null object");
//assert(false);
return NULL;
}
}

void flow_check_assignment(
struct flow_visit_ctx* ctx,
Expand All @@ -3388,7 +3417,8 @@ void flow_check_assignment(
bool a_type_is_view,
bool a_type_is_nullable,
struct type* p_a_type, struct flow_object* p_a_object,
struct type* p_b_type, struct flow_object* p_b_object)
struct type* p_b_type, struct flow_object* p_b_object,
bool* _Opt set_argument_to_unkown)
{
if (type_is_pointer(p_b_type) && object_is_expansible(p_b_object))
{
Expand All @@ -3415,7 +3445,8 @@ void flow_check_assignment(
a_type_is_view,
a_type_is_nullable,
&visitor_a,
&visitor_b);
&visitor_b,
set_argument_to_unkown);
}


Expand Down
8 changes: 7 additions & 1 deletion src/flow_object.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This file is part of cake compiler
* https://github.com/thradams/cake
*/


/*
Object represents "memory" and state. Used by flow analysis
Expand Down Expand Up @@ -168,7 +173,8 @@ void flow_check_assignment(struct flow_visit_ctx* ctx,
bool a_type_is_view,
bool a_type_is_nullable,
struct type* p_a_type, struct flow_object* p_a_object,
struct type* p_b_type, struct flow_object* p_b_object);
struct type* p_b_type, struct flow_object* p_b_object,
bool * _Opt set_argument_to_unkown);


void object_set_zero(struct type* p_type, struct flow_object* p_object);
Expand Down
Loading

0 comments on commit 9acc8c3

Please sign in to comment.