Mercurial > vim
comparison src/vim9type.c @ 26935:ccb9be1cdd71 v8.2.3996
patch 8.2.3996: Vim9: type checking lacks information about declared type
Commit: https://github.com/vim/vim/commit/078a46161e8b1b30bf306d6c1f4f0af7c616a989
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Jan 4 15:17:03 2022 +0000
patch 8.2.3996: Vim9: type checking lacks information about declared type
Problem: Vim9: type checking for list and dict lacks information about
declared type.
Solution: Add dv_decl_type and lv_decl_type. Refactor the type stack to
store two types in each entry.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 04 Jan 2022 16:30:06 +0100 |
parents | 4e77f9961650 |
children | ae2324aca26b |
comparison
equal
deleted
inserted
replaced
26934:2d3dd8065e25 | 26935:ccb9be1cdd71 |
---|---|
1190 | 1190 |
1191 *dest = &t_any; | 1191 *dest = &t_any; |
1192 } | 1192 } |
1193 | 1193 |
1194 /* | 1194 /* |
1195 * Get the member type of a dict or list from the items on the stack. | 1195 * Push an entry onto the type stack. "type" used both for the current type |
1196 * "stack_top" points just after the last type on the type stack. | 1196 * and the declared type. |
1197 * Returns FAIL when out of memory. | |
1198 */ | |
1199 int | |
1200 push_type_stack(cctx_T *cctx, type_T *type) | |
1201 { | |
1202 return push_type_stack2(cctx, type, type); | |
1203 } | |
1204 | |
1205 /* | |
1206 * Push an entry onto the type stack. "type" is the current type, "decl_type" | |
1207 * is the declared type. | |
1208 * Returns FAIL when out of memory. | |
1209 */ | |
1210 int | |
1211 push_type_stack2(cctx_T *cctx, type_T *type, type_T *decl_type) | |
1212 { | |
1213 garray_T *stack = &cctx->ctx_type_stack; | |
1214 type2_T *typep; | |
1215 | |
1216 if (GA_GROW_FAILS(stack, 1)) | |
1217 return FAIL; | |
1218 typep = ((type2_T *)stack->ga_data) + stack->ga_len; | |
1219 typep->type_curr = type; | |
1220 typep->type_decl = decl_type; | |
1221 ++stack->ga_len; | |
1222 return OK; | |
1223 } | |
1224 | |
1225 /* | |
1226 * Set the type of the top of the stack to "type". | |
1227 */ | |
1228 void | |
1229 set_type_on_stack(cctx_T *cctx, type_T *type, int offset) | |
1230 { | |
1231 garray_T *stack = &cctx->ctx_type_stack; | |
1232 type2_T *typep = ((type2_T *)stack->ga_data) | |
1233 + stack->ga_len - 1 - offset; | |
1234 | |
1235 typep->type_curr = type; | |
1236 typep->type_decl = &t_any; | |
1237 } | |
1238 | |
1239 /* | |
1240 * Get the type from the type stack. If "offset" is zero the one at the top, | |
1241 * if "offset" is one the type above that, etc. | |
1242 * Returns &t_unknown if there is no such stack entry. | |
1243 */ | |
1244 type_T * | |
1245 get_type_on_stack(cctx_T *cctx, int offset) | |
1246 { | |
1247 garray_T *stack = &cctx->ctx_type_stack; | |
1248 | |
1249 if (offset + 1 > stack->ga_len) | |
1250 return &t_unknown; | |
1251 return (((type2_T *)stack->ga_data) + stack->ga_len - offset - 1) | |
1252 ->type_curr; | |
1253 } | |
1254 | |
1255 /* | |
1256 * Get the member type of a dict or list from the items on the stack of "cctx". | |
1257 * The declared type is stored in "decl_type". | |
1197 * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped. | 1258 * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped. |
1198 * Returns &t_void for an empty list or dict. | 1259 * Returns &t_void for an empty list or dict. |
1199 * Otherwise finds the common type of all items. | 1260 * Otherwise finds the common type of all items. |
1200 */ | 1261 */ |
1201 type_T * | 1262 type_T * |
1202 get_member_type_from_stack( | 1263 get_member_type_from_stack( |
1203 type_T **stack_top, | |
1204 int count, | 1264 int count, |
1205 int skip, | 1265 int skip, |
1206 garray_T *type_gap) | 1266 type_T **decl_type, |
1207 { | 1267 cctx_T *cctx) |
1208 int i; | 1268 { |
1209 type_T *result; | 1269 garray_T *stack = &cctx->ctx_type_stack; |
1210 type_T *type; | 1270 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len; |
1211 | 1271 garray_T *type_gap = cctx->ctx_type_list; |
1212 // Use "any" for an empty list or dict. | 1272 int i; |
1273 type_T *result; | |
1274 type_T *decl_result; | |
1275 type_T *type; | |
1276 | |
1277 // Use "unknown" for an empty list or dict. | |
1213 if (count == 0) | 1278 if (count == 0) |
1279 { | |
1280 *decl_type = &t_unknown; | |
1214 return &t_unknown; | 1281 return &t_unknown; |
1282 } | |
1215 | 1283 |
1216 // Use the first value type for the list member type, then find the common | 1284 // Use the first value type for the list member type, then find the common |
1217 // type from following items. | 1285 // type from following items. |
1218 result = *(stack_top -(count * skip) + skip - 1); | 1286 result = (typep -(count * skip) + skip - 1)->type_curr; |
1287 decl_result = (typep -(count * skip) + skip - 1)->type_decl; | |
1219 for (i = 1; i < count; ++i) | 1288 for (i = 1; i < count; ++i) |
1220 { | 1289 { |
1221 if (result == &t_any) | 1290 if (result == &t_any) |
1222 break; // won't get more common | 1291 break; // won't get more common |
1223 type = *(stack_top -((count - i) * skip) + skip - 1); | 1292 type = (typep -((count - i) * skip) + skip - 1)->type_curr; |
1224 common_type(type, result, &result, type_gap); | 1293 common_type(type, result, &result, type_gap); |
1225 } | 1294 type = (typep -((count - i) * skip) + skip - 1)->type_decl; |
1226 | 1295 common_type(type, decl_result, &decl_result, type_gap); |
1296 } | |
1297 | |
1298 *decl_type = decl_result; | |
1227 return result; | 1299 return result; |
1228 } | 1300 } |
1229 | 1301 |
1230 char * | 1302 char * |
1231 vartype_name(vartype_T type) | 1303 vartype_name(vartype_T type) |