/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* vim9class.c: Vim9 script class support
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
// When not generating protos this is included in proto.h
#ifdef PROTO
# include "vim9.h"
#endif
/*
* Parse a member declaration, both object and class member.
* Returns OK or FAIL. When OK then "varname_end" is set to just after the
* variable name and "type_ret" is set to the decleared or detected type.
* "init_expr" is set to the initialisation expression (allocated), if there is
* one.
*/
static int
parse_member(
exarg_T *eap,
char_u *line,
char_u *varname,
int has_public, // TRUE if "public" seen before "varname"
char_u **varname_end,
garray_T *type_list,
type_T **type_ret,
char_u **init_expr)
{
*varname_end = to_name_end(varname, FALSE);
if (*varname == '_' && has_public)
{
semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
return FAIL;
}
char_u *colon = skipwhite(*varname_end);
char_u *type_arg = colon;
type_T *type = NULL;
if (*colon == ':')
{
if (VIM_ISWHITE(**varname_end))
{
semsg(_(e_no_white_space_allowed_before_colon_str), varname);
return FAIL;
}
if (!VIM_ISWHITE(colon[1]))
{
semsg(_(e_white_space_required_after_str_str), ":", varname);
return FAIL;
}
type_arg = skipwhite(colon + 1);
type = parse_type(&type_arg, type_list, TRUE);
if (type == NULL)
return FAIL;
}
char_u *expr_start = skipwhite(type_arg);
char_u *expr_end = expr_start;
if (type == NULL && *expr_start != '=')
{
emsg(_(e_type_or_initialization_required));
return FAIL;
}
if (*expr_start == '=')
{
if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1]))
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
"=", type_arg);
return FAIL;
}
expr_start = skipwhite(expr_start + 1);
expr_end = expr_start;
evalarg_T evalarg;
fill_evalarg_from_eap(&evalarg, eap, FALSE);
skip_expr(&expr_end, NULL);
if (type == NULL)
{
// No type specified, use the type of the initializer.
typval_T tv;
tv.v_type = VAR_UNKNOWN;
char_u *expr = expr_start;
int res = eval0(expr, &tv, eap, &evalarg);
if (res == OK)
type = typval2type(&tv, get_copyID(), type_list,
TVTT_DO_MEMBER);
if (type == NULL)
{
semsg(_(e_cannot_get_object_member_type_from_initializer_str),
expr_start);
clear_evalarg(&evalarg, NULL);
return FAIL;
}
}
clear_evalarg(&evalarg, NULL);
}
if (!valid_declaration_type(type))
return FAIL;
*type_ret = type;
if (expr_end > expr_start)
*init_expr = vim_strnsave(expr_start, expr_end - expr_start);
return OK;
}
/*
* Add a member to an object or a class.
* Returns OK when successful, "init_expr" will be consumed then.
* Returns FAIL otherwise, caller might need to free "init_expr".
*/
static int
add_member(
garray_T *gap,
char_u *varname,
char_u *varname_end,
int has_public,
type_T *type,
char_u *init_expr)
{
if (ga_grow(gap, 1) == FAIL)
return FAIL;
ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
m->ocm_name = vim_strnsave(varname, varname_end - varname);