work on error handling for structs and struct_type, and fix bug in env_struct_type_exists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index dd82df2..5f0beba 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -3154,9 +3154,10 @@ sw buf_parse_struct (s_buf *buf, s_struct *dest)
if (r > 0) {
result += r;
if (! struct_init(&tmp, module)) {
- err_write_1("buf_parse_struct: struct_init ");
+ err_write_1("buf_parse_struct: struct_init(");
err_inspect_sym(&module);
- err_puts(".");
+ err_puts(")");
+ assert(! "buf_parse_struct: struct init");
r = -2;
goto clean;
}
diff --git a/libc3/data.c b/libc3/data.c
index 75b3383..c237816 100644
--- a/libc3/data.c
+++ b/libc3/data.c
@@ -82,7 +82,8 @@ sw data_buf_inspect (const s_sym *type, s_buf *buf, const void *data)
return buf_inspect_var(buf, data);
if (type == &g_sym_Void)
return buf_inspect_void(buf, data);
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return -1;
if (st) {
s_struct s = {0};
s.type = st;
@@ -167,7 +168,8 @@ sw data_buf_inspect_size (const s_sym *type, const void *data)
return buf_inspect_var_size(data);
if (type == &g_sym_Void)
return buf_inspect_void_size(data);
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return -1;
if (st) {
s.type = st;
s.data = (void *) data;
@@ -302,7 +304,8 @@ bool data_clean (const s_sym *type, void *data)
if (type == &g_sym_Void) {
return true;
}
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return false;
if (st) {
s.type = st;
s.data = data;
@@ -388,7 +391,8 @@ bool data_compare (const s_sym *type, const void *a, const void *b)
return compare_ptr(a, b);
if (type == &g_sym_Void)
return 0;
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return COMPARE_ERROR;
if (st) {
sa.type = st;
sa.data = (void *) a;
@@ -400,7 +404,7 @@ bool data_compare (const s_sym *type, const void *a, const void *b)
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_compare: unknown type");
- return false;
+ return COMPARE_ERROR;
}
bool data_hash_update (const s_sym *type, t_hash *hash, const void *data)
@@ -474,7 +478,8 @@ bool data_hash_update (const s_sym *type, t_hash *hash, const void *data)
return hash_update_var(hash, data);
if (type == &g_sym_Void)
return hash_update_void(hash, data);
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return false;
if (st) {
s.type = st;
s.data = (void *) data;
@@ -566,7 +571,8 @@ void * data_init_cast (void *data, const s_sym * const *type,
/*
if (sym_is_array_type(t)) {
*/
- st = struct_type_find(t);
+ if (! struct_type_find(t, &st))
+ return NULL;
if (st) {
s.type = st;
s.data = data;
@@ -651,7 +657,8 @@ void * data_init_copy (const s_sym *type, void *data, const void *src)
return var_init_copy(data, src);
if (type == &g_sym_Void)
return data;
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return NULL;
if (st) {
t.type = s.type = st;
s.data = data;
diff --git a/libc3/env.c b/libc3/env.c
index e07c93c..42525cc 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -2209,8 +2209,6 @@ bool env_module_maybe_reload (s_env *env, const s_sym *module)
bool r = false;
s_tag tag_load_time = {0};
s_tag tag_mtime;
- if (! env_module_load_time(env, module, &tag_load_time))
- return env_module_load(env, module);
if (module_path(module, &env->module_path, C3_EXT, &path)) {
if (file_access(&path, &g_sym_r))
r = true;
@@ -2223,12 +2221,10 @@ bool env_module_maybe_reload (s_env *env, const s_sym *module)
else
str_clean(&path);
}
- if (! r) {
- err_write_1("env_module_maybe_reload: module not found: ");
- err_inspect_sym(&module);
- err_write_1("\n");
- return false;
- }
+ if (! r)
+ return true;
+ if (! env_module_load_time(env, module, &tag_load_time))
+ return env_module_load(env, module);
if (! file_mtime(&path, &tag_mtime)) {
str_clean(&path);
return false;
@@ -2563,36 +2559,33 @@ bool * env_struct_type_exists (s_env *env, const s_sym *module,
{
s_facts_cursor cursor;
const s_fact *fact;
- s_tag tag_defstruct;
+ s_tag tag_struct_type;
s_tag tag_module;
s_tag tag_var;
assert(env);
assert(module);
assert(dest);
tag_init_sym(&tag_module, module);
- tag_init_sym(&tag_defstruct, &g_sym_defstruct);
+ tag_init_sym(&tag_struct_type, &g_sym_struct_type);
tag_init_var(&tag_var, &g_sym_Tag);
if (! env_module_maybe_reload(env, module))
return NULL;
if (! facts_with_tags(&env->facts, &cursor, &tag_module,
- &tag_defstruct, &tag_var))
+ &tag_struct_type, &tag_var))
return NULL;
if (! facts_cursor_next(&cursor, &fact))
return NULL;
- if (! fact) {
- facts_save_file(&env->facts, "env_struct_type_exists.facts");
- }
*dest = fact ? true : false;
facts_cursor_clean(&cursor);
return dest;
}
-const s_struct_type * env_struct_type_find (s_env *env,
- const s_sym *module)
+const s_struct_type ** env_struct_type_find (s_env *env,
+ const s_sym *module,
+ const s_struct_type **dest)
{
s_facts_with_cursor cursor;
const s_fact *found;
- const s_struct_type *result;
s_tag tag_struct_type;
s_tag tag_module;
s_tag tag_var;
@@ -2602,8 +2595,12 @@ const s_struct_type * env_struct_type_find (s_env *env,
tag_init_sym(&tag_module, module);
tag_init_sym(&tag_struct_type, &g_sym_struct_type);
tag_init_var(&tag_var, &g_sym_Tag);
- if (! env_module_maybe_reload(env, module))
+ if (! env_module_maybe_reload(env, module)) {
+ err_write_1("env_struct_type_find: env_module_maybe_reload(");
+ err_inspect_sym(&module);
+ err_puts(")");
return NULL;
+ }
if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
&tag_module, &tag_struct_type, &tag_var,
NULL, NULL }))
@@ -2612,7 +2609,8 @@ const s_struct_type * env_struct_type_find (s_env *env,
return NULL;
if (! found) {
facts_with_cursor_clean(&cursor);
- return NULL;
+ *dest = NULL;
+ return dest;
}
if (found->object->type != TAG_STRUCT_TYPE) {
tag_type(found->object, &type);
@@ -2624,9 +2622,9 @@ const s_struct_type * env_struct_type_find (s_env *env,
assert(! "env_struct_type_find: invalid struct_type");
return NULL;
}
- result = &found->object->data.struct_type;
+ *dest = &found->object->data.struct_type;
facts_with_cursor_clean(&cursor);
- return result;
+ return dest;
}
f_clean env_struct_type_get_clean (s_env *env, const s_sym *module)
diff --git a/libc3/env.h b/libc3/env.h
index bc76226..84eebf4 100644
--- a/libc3/env.h
+++ b/libc3/env.h
@@ -198,8 +198,9 @@ u8 env_special_operator_arity (s_env *env,
const s_ident *ident);
bool * env_struct_type_exists (s_env *env, const s_sym *module,
bool *dest);
-const s_struct_type *
- env_struct_type_find (s_env *env, const s_sym *module);
+const s_struct_type **
+ env_struct_type_find (s_env *env, const s_sym *module,
+ const s_struct_type **dest);
f_clean env_struct_type_get_clean (s_env *env,
const s_sym *module);
s_list ** env_struct_type_get_spec (s_env *env, const s_sym *module,
diff --git a/libc3/struct.c b/libc3/struct.c
index 62dfb9b..832d254 100644
--- a/libc3/struct.c
+++ b/libc3/struct.c
@@ -122,7 +122,12 @@ s_struct * struct_init (s_struct *s, const s_sym *module)
s_struct tmp = {0};
assert(s);
assert(module);
- tmp.type = struct_type_find(module);
+ if (! struct_type_find(module, &tmp.type)) {
+ err_write_1("struct_init: struct_type_find(");
+ err_inspect_sym(&module);
+ err_puts(")");
+ return NULL;
+ }
if (! tmp.type) {
err_write_1("struct_init: struct_type not found: ");
err_inspect_sym(&module);
@@ -263,9 +268,14 @@ s_struct * struct_init_with_data (s_struct *s, const s_sym *module,
s_struct tmp = {0};
assert(s);
assert(module);
- tmp.type = struct_type_find(module);
- if (! tmp.type)
+ if (! struct_type_find(module, &tmp.type))
+ return NULL;
+ if (! tmp.type) {
+ err_write_1("struct_init_with_data: struct_type not found: ");
+ err_inspect_sym(&module);
+ err_write_1("\n");
return NULL;
+ }
tmp.free_data = free_data;
tmp.data = data;
*s = tmp;
diff --git a/libc3/struct_type.c b/libc3/struct_type.c
index 9de6ad1..9377aed 100644
--- a/libc3/struct_type.c
+++ b/libc3/struct_type.c
@@ -42,9 +42,10 @@ bool * struct_type_exists (const s_sym *module, bool *dest)
return env_struct_type_exists(&g_c3_env, module, dest);
}
-const s_struct_type * struct_type_find (const s_sym *module)
+const s_struct_type ** struct_type_find (const s_sym *module,
+ const s_struct_type **dest)
{
- return env_struct_type_find(&g_c3_env, module);
+ return env_struct_type_find(&g_c3_env, module, dest);
}
s_struct_type * struct_type_init (s_struct_type *st,
diff --git a/libc3/struct_type.h b/libc3/struct_type.h
index 351e7dc..0930e40 100644
--- a/libc3/struct_type.h
+++ b/libc3/struct_type.h
@@ -41,9 +41,10 @@ s_struct_type * struct_type_new (const s_sym *module,
const s_list *spec);
/* Observers. */
-bool * struct_type_exists (const s_sym *module,
- bool *dest);
-const s_struct_type * struct_type_find (const s_sym *module);
-uw struct_type_padding (uw offset, uw size);
+bool * struct_type_exists (const s_sym *module,
+ bool *dest);
+const s_struct_type ** struct_type_find (const s_sym *module,
+ const s_struct_type **dest);
+uw struct_type_padding (uw offset, uw size);
#endif /* LIBC3_STRUCT_TYPE_H */
diff --git a/libc3/sym.c b/libc3/sym.c
index 92b2862..3b284b9 100644
--- a/libc3/sym.c
+++ b/libc3/sym.c
@@ -626,7 +626,8 @@ bool sym_must_clean (const s_sym *sym, bool *must_clean)
*must_clean = false;
return true;
}
- st = struct_type_find(sym);
+ if (! struct_type_find(sym, &st))
+ return false;
if (st) {
*must_clean = st->must_clean;
return true;
@@ -1156,7 +1157,8 @@ bool sym_type_size (const s_sym *type, uw *dest)
*dest = 0;
return true;
}
- st = struct_type_find(type);
+ if (! struct_type_find(type, &st))
+ return false;
if (st) {
*dest = st->size;
return true;