Mercurial > vim
comparison src/vim9compile.c @ 30584:ee039a6049ff v9.0.0627
patch 9.0.0627: "const" and "final" both make the type a constant
Commit: https://github.com/vim/vim/commit/6586a015144f15a979d573a79d91e700e4b3009f
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Sep 30 11:04:50 2022 +0100
patch 9.0.0627: "const" and "final" both make the type a constant
Problem: "const" and "final" both make the type a constant. (Daniel
Steinberg)
Solution: Only have "const" make the type a constant.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 30 Sep 2022 12:15:04 +0200 |
parents | 72e6073a2822 |
children | d914a3812d5b |
comparison
equal
deleted
inserted
replaced
30583:fc88f4597670 | 30584:ee039a6049ff |
---|---|
469 static void | 469 static void |
470 set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx) | 470 set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx) |
471 { | 471 { |
472 type_T *type = type_arg; | 472 type_T *type = type_arg; |
473 | 473 |
474 if (lvar->lv_const && (type->tt_flags & TTFLAG_CONST) == 0) | 474 if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0) |
475 { | 475 { |
476 if (type->tt_flags & TTFLAG_STATIC) | 476 if (type->tt_flags & TTFLAG_STATIC) |
477 // entry in static_types[] is followed by const type | 477 // entry in static_types[] is followed by const type |
478 type = type + 1; | 478 type = type + 1; |
479 else | 479 else |
485 lvar->lv_type = type; | 485 lvar->lv_type = type; |
486 } | 486 } |
487 | 487 |
488 /* | 488 /* |
489 * Reserve space for a local variable. | 489 * Reserve space for a local variable. |
490 * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and | |
491 * ASSIGN_FINAL for :final. | |
490 * Return the variable or NULL if it failed. | 492 * Return the variable or NULL if it failed. |
491 */ | 493 */ |
492 lvar_T * | 494 lvar_T * |
493 reserve_local( | 495 reserve_local( |
494 cctx_T *cctx, | 496 cctx_T *cctx, |
495 char_u *name, | 497 char_u *name, |
496 size_t len, | 498 size_t len, |
497 int isConst, | 499 int assign, |
498 type_T *type) | 500 type_T *type) |
499 { | 501 { |
500 lvar_T *lvar; | 502 lvar_T *lvar; |
501 dfunc_T *dfunc; | 503 dfunc_T *dfunc; |
502 | 504 |
517 // entries. This is less efficient, but memory is cheap these days. | 519 // entries. This is less efficient, but memory is cheap these days. |
518 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx; | 520 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx; |
519 lvar->lv_idx = dfunc->df_var_names.ga_len; | 521 lvar->lv_idx = dfunc->df_var_names.ga_len; |
520 | 522 |
521 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); | 523 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
522 lvar->lv_const = isConst; | 524 lvar->lv_const = assign; |
523 if (type == &t_unknown || type == &t_any) | 525 if (type == &t_unknown || type == &t_any) |
524 // type not known yet, may be inferred from RHS | 526 // type not known yet, may be inferred from RHS |
525 lvar->lv_type = type; | 527 lvar->lv_type = type; |
526 else | 528 else |
527 // may use TTFLAG_CONST | 529 // may use TTFLAG_CONST |
991 } | 993 } |
992 else | 994 else |
993 { | 995 { |
994 // Define a local variable for the function reference. | 996 // Define a local variable for the function reference. |
995 lvar = reserve_local(cctx, func_name, name_end - name_start, | 997 lvar = reserve_local(cctx, func_name, name_end - name_start, |
996 TRUE, ufunc->uf_func_type); | 998 ASSIGN_CONST, ufunc->uf_func_type); |
997 if (lvar == NULL) | 999 if (lvar == NULL) |
998 goto theend; | 1000 goto theend; |
999 if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL) | 1001 if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL) |
1000 goto theend; | 1002 goto theend; |
1001 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); | 1003 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
1689 || lhs->lhs_type->tt_type == VAR_PARTIAL) | 1691 || lhs->lhs_type->tt_type == VAR_PARTIAL) |
1690 && var_wrong_func_name(lhs->lhs_name, TRUE)) | 1692 && var_wrong_func_name(lhs->lhs_name, TRUE)) |
1691 return FAIL; | 1693 return FAIL; |
1692 | 1694 |
1693 // New local variable. | 1695 // New local variable. |
1696 int assign = cmdidx == CMD_final ? ASSIGN_FINAL | |
1697 : cmdidx == CMD_const ? ASSIGN_CONST : ASSIGN_VAR; | |
1694 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, | 1698 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, |
1695 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type); | 1699 assign, lhs->lhs_type); |
1696 if (lhs->lhs_lvar == NULL) | 1700 if (lhs->lhs_lvar == NULL) |
1697 return FAIL; | 1701 return FAIL; |
1698 lhs->lhs_new_local = TRUE; | 1702 lhs->lhs_new_local = TRUE; |
1699 } | 1703 } |
1700 | 1704 |
1767 { | 1771 { |
1768 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name); | 1772 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name); |
1769 return FAIL; | 1773 return FAIL; |
1770 } | 1774 } |
1771 if (!is_decl && lhs->lhs_lvar != NULL | 1775 if (!is_decl && lhs->lhs_lvar != NULL |
1772 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index) | 1776 && lhs->lhs_lvar->lv_const != ASSIGN_VAR |
1777 && !lhs->lhs_has_index) | |
1773 { | 1778 { |
1774 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name); | 1779 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name); |
1775 return FAIL; | 1780 return FAIL; |
1776 } | 1781 } |
1777 return OK; | 1782 return OK; |