Skip to content

Commit

Permalink
WIP: updating ic_type_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisosaurus committed Apr 7, 2017
1 parent ba722c6 commit d74d5cc
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 43 deletions.
1 change: 0 additions & 1 deletion TYPE_REFACTOR_TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ TODO:

* kill ic_type with fire
* break decl_union and decl_type into decl_type, decl_type_struct, and decl_type_union
* 'fix' ic_type_ref

Should be:
==========
Expand Down
157 changes: 129 additions & 28 deletions src/parse/data/type_ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ struct ic_type_ref *ic_type_ref_new(void) {

type = calloc(1, sizeof(struct ic_type_ref));
if (!type) {
puts("ic_type_new: call to calloc failed");
puts("ic_type_ref_new: call to calloc failed");
return 0;
}

if (!ic_type_ref_init(type)) {
puts("ic_type_new: call to ic_type_init failed");
puts("ic_type_ref_new: call to ic_type_init failed");
return 0;
}

Expand All @@ -37,12 +37,12 @@ struct ic_type_ref *ic_type_ref_new(void) {
*/
unsigned int ic_type_ref_init(struct ic_type_ref *type) {
if (!type) {
puts("ic_type_init: type was null");
puts("ic_type_ref_init: type was null");
return 0;
}

/* default to unknown types
* other type_type(s) are set via methods
* other type_tag(s) are set via methods
*/
type->tag = ic_type_ref_unknown;

Expand All @@ -59,20 +59,20 @@ struct ic_type_ref *ic_type_ref_symbol_new(char *type_str, unsigned int type_len
struct ic_type_ref *type = 0;

if (!type_str) {
puts("ic_type_symbol_new: type_str was null");
puts("ic_type_ref_symbol_new: type_str was null");
return 0;
}

/* construct base type */
type = ic_type_ref_new();
if (!type) {
puts("ic_type_symbol_new: call to ic_type_new failed");
puts("ic_type_ref_symbol_new: call to ic_type_new failed");
return 0;
}

/* set to symbol */
if (!ic_type_ref_set_symbol(type, type_str, type_len)) {
puts("ic_type_symbol_new: call to ic_type_set_symbol failed");
puts("ic_type_ref_symbol_new: call to ic_type_set_symbol failed");
/* destroy type
* free type as allocated with new
*/
Expand All @@ -90,23 +90,23 @@ struct ic_type_ref *ic_type_ref_symbol_new(char *type_str, unsigned int type_len
*/
unsigned int ic_type_ref_symbol_init(struct ic_type_ref *type, char *type_str, unsigned int type_len) {
if (!type) {
puts("ic_type_symbol_init: type_str was null");
puts("ic_type_ref_symbol_init: type_str was null");
return 0;
}
if (!type_str) {
puts("ic_type_symbol_init: type_str was null");
puts("ic_type_ref_symbol_init: type_str was null");
return 0;
}

/* init base type */
if (!ic_type_ref_init(type)) {
puts("ic_type_symbol_init: call to ic_type_init failed");
puts("ic_type_ref_symbol_init: call to ic_type_init failed");
return 0;
}

/* set to symbol */
if (!ic_type_ref_set_symbol(type, type_str, type_len)) {
puts("ic_type_symbol_init: call to ic_type_set_symbol failed");
puts("ic_type_ref_symbol_init: call to ic_type_set_symbol failed");
return 0;
}

Expand All @@ -122,11 +122,11 @@ unsigned int ic_type_ref_symbol_init(struct ic_type_ref *type, char *type_str, u
*/
unsigned int ic_type_ref_destroy(struct ic_type_ref *type, unsigned int free_type) {
if (!type) {
puts("ic_type_destroy: type was null");
puts("ic_type_ref_destroy: type was null");
return 0;
}

/* cleanup depends on type_type */
/* cleanup depends on type_tag */
switch (type->tag) {
case ic_type_ref_unknown:
/* nothing to do */
Expand All @@ -135,13 +135,13 @@ unsigned int ic_type_ref_destroy(struct ic_type_ref *type, unsigned int free_typ
case ic_type_ref_symbol:
/* clean up symbol, do not free as member */
if (!ic_symbol_destroy(&(type->u.sym), 0)) {
puts("ic_type_destroy: call to ic_symbol_destroy failed");
puts("ic_type_ref_destroy: call to ic_symbol_destroy failed");
return 0;
}
break;

default:
puts("ic_type_destroy: type->type was impossible type_type");
puts("ic_type_ref_destroy: type->type was impossible type_tag");
return 0;
break;
}
Expand All @@ -155,19 +155,21 @@ unsigned int ic_type_ref_destroy(struct ic_type_ref *type, unsigned int free_typ
}

/* set the sym on this type from the provided string
* this will change type.type to sym
* this will change type_ref.tag to sym
*
* this is an error if type_ref.tag was already resolved
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int ic_type_ref_set_symbol(struct ic_type_ref *type, char *type_str, unsigned int type_len) {
if (!type) {
puts("ic_type_set_symbol: type was null");
puts("ic_type_ref_set_symbol: type was null");
return 0;
}

if (!type_str) {
puts("ic_type_set_symbol: type_str was null");
puts("ic_type_ref_set_symbol: type_str was null");
return 0;
}

Expand All @@ -181,12 +183,18 @@ unsigned int ic_type_ref_set_symbol(struct ic_type_ref *type, char *type_str, un

case ic_type_ref_symbol:
/* error, already a symbol */
puts("ic_type_set_symbol: type was already a symbol");
puts("ic_type_ref_set_symbol: type was already a symbol");
return 0;
break;

case ic_type_ref_resolved:
/* error, already a symbol */
puts("ic_type_ref_set_symbol: type was already resolved");
return 0;
break;

default:
puts("ic_type_set_symbol: type->type was impossible type_type");
puts("ic_type_ref_set_symbol: type->type was impossible type_tag");
return 0;
break;
}
Expand All @@ -196,7 +204,7 @@ unsigned int ic_type_ref_set_symbol(struct ic_type_ref *type, char *type_str, un

/* set our symbol from the provider char * and len */
if (!ic_symbol_init(&(type->u.sym), type_str, type_len)) {
puts("ic_type_set_symbol: call to ic_symbol_init failed");
puts("ic_type_ref_set_symbol: call to ic_symbol_init failed");
return 0;
}

Expand All @@ -205,21 +213,22 @@ unsigned int ic_type_ref_set_symbol(struct ic_type_ref *type, char *type_str, un

/* return a symbol representing this type
*
* if type is unknown then 0 is reuturned
* if type is symbol then the symbol is returned
* if type_ref is unknown then 0 is returned
* if type_ref is symbol then the symbol is returned
* if type_ref is resolved then the symbol for that type is returned
*
* returns 0 on failure
*/
struct ic_symbol *ic_type_ref_get_symbol(struct ic_type_ref *type) {
if (!type) {
puts("ic_type_get_symbol: type was null");
puts("ic_type_ref_get_symbol: type was null");
return 0;
}

switch (type->tag) {
case ic_type_ref_unknown:
/* error, nothing to return */
puts("ic_type_get_symbol: type was of type unknown");
puts("ic_type_ref_get_symbol: type was of type unknown");
return 0;
break;

Expand All @@ -228,8 +237,96 @@ struct ic_symbol *ic_type_ref_get_symbol(struct ic_type_ref *type) {
return &(type->u.sym);
break;

case ic_type_ref_resolved:
/* the decl_type already has a symbol name */
return &(type->u.tdecl->name);
break;

default:
puts("ic_type_get_symbol: type->type was impossible type_type");
puts("ic_type_ref_get_symbol: type->type was impossible type_tag");
return 0;
break;
}

return 0;
}

/* set the decl_type on this type_ref
* this will change type.tag to resolved
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int ic_type_ref_set_type_decl(struct ic_type_ref *type, struct ic_decl_type *tdecl) {
if (!type) {
puts("ic_type_ref_set_type_decl: type was null");
return 0;
}

if (!tdecl) {
puts("ic_type_ref_set_type_decl: tdecl was null");
return 0;
}

switch (type->tag) {
case ic_type_ref_unknown:
/* nothing to do */
break;

case ic_type_ref_symbol:
/* symbol is allocated as part of set_symbol, so need to destroy here */
if (!ic_symbol_destroy(&(type->u.sym), 1)) {
puts("ic_type_ref_set_type_decl: call to ic_symbol_destroy failed");
return 0;
}
break;

case ic_type_ref_resolved:
puts("ic_type_ref_set_symbol: type was already resolved");
return 0;
break;

default:
break;
}

type->tag = ic_type_ref_resolved;
type->u.tdecl = tdecl;

return 1;
}

/* return the underlying decl_type
*
* if type_ref.tag is not resolved then this is an error
*
* return * on success
* returns 0 on failure
*/
struct ic_decl_type *ic_type_ref_get_type_decl(struct ic_type_ref *type) {
if (!type) {
puts("ic_type_ref_get_type_decl: type was null");
return 0;
}

switch (type->tag) {
case ic_type_ref_unknown:
/* error, nothing to return */
puts("ic_type_ref_get_type_decl: type was of type unknown");
return 0;
break;

case ic_type_ref_symbol:
puts("ic_type_ref_get_type_decl: type was of type symbol");
return 0;
break;

case ic_type_ref_resolved:
return type->u.tdecl;
break;

default:
puts("ic_type_ref_get_type_decl: type->type was impossible type_ref.tag");
return 0;
break;
}
Expand All @@ -240,7 +337,7 @@ struct ic_symbol *ic_type_ref_get_symbol(struct ic_type_ref *type) {
/* print this this type */
void ic_type_ref_print(FILE *fd, struct ic_type_ref *type) {
if (!type) {
puts("ic_type_print: type was null");
puts("ic_type_ref_print: type was null");
return;
}
switch (type->tag) {
Expand All @@ -253,8 +350,12 @@ void ic_type_ref_print(FILE *fd, struct ic_type_ref *type) {
ic_symbol_print(fd, &(type->u.sym));
break;

case ic_type_ref_resolved:
ic_symbol_print(fd, &(type->u.tdecl->name));
break;

default:
puts("ic_type_print: type->type was impossible type_type");
puts("ic_type_ref_print: type->type was impossible type_tag");
return;
break;
}
Expand Down
43 changes: 29 additions & 14 deletions src/parse/data/type_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,19 @@ enum ic_type_ref_tag {
*/
ic_type_ref_symbol,

/* error type
* this is NOT a runtime error
*
* this value is used by the analysis module to
* indicate an error during inference such as
* being unable to find the type mentioned
*
* FIXME it may be that errors are moved out of here
/* we have found a concrete type for this
* this is filled in during the analysis phase
*/
ic_type_ref_error
ic_type_ref_resolved,

};

struct ic_type_ref {
enum ic_type_ref_tag tag;
union {
/* no value for unknown */
struct ic_symbol sym; /* value for ic_type_symbol */
/* FIXME need a structure for an error */
struct ic_symbol sym; /* value for ic_type_ref_symbol */
struct ic_decl_type *tdecl; /* value for ic_type_ref_resolved */
} u;
};

Expand Down Expand Up @@ -80,7 +75,9 @@ unsigned int ic_type_ref_symbol_init(struct ic_type_ref *type, char *type_str, u
unsigned int ic_type_ref_destroy(struct ic_type_ref *type, unsigned int free_type);

/* set the sym on this type from the provided string
* this will change type.type to sym
* this will change type_ref.tag to sym
*
* this is an error if type_ref.tag was already resolved
*
* returns 1 on success
* returns 0 on failure
Expand All @@ -89,13 +86,31 @@ unsigned int ic_type_ref_set_symbol(struct ic_type_ref *type, char *type_str, un

/* return a symbol representing this type
*
* if type is unknown then 0 is returned
* if type is symbol then the symbol is returned
* if type_ref is unknown then 0 is returned
* if type_ref is symbol then the symbol is returned
* if type_ref is resolved then the symbol for that type is returned
*
* returns 0 on failure
*/
struct ic_symbol *ic_type_ref_get_symbol(struct ic_type_ref *type);

/* set the decl_type on this type_ref
* this will change type.tag to resolved
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int ic_type_ref_set_type_decl(struct ic_type_ref *type, struct ic_decl_type *tdecl);

/* return the underlying decl_type
*
* if type_ref.tag is not resolved then this is an error
*
* return * on success
* returns 0 on failure
*/
struct ic_decl_type *ic_type_ref_get_type_decl(struct ic_type_ref *type);

/* print this this type */
void ic_type_ref_print(FILE *fd, struct ic_type_ref *type);

Expand Down

0 comments on commit d74d5cc

Please sign in to comment.