Optimise typedargslist_name to not create a node if just an id.

This commit is contained in:
Damien 2013-10-06 00:28:28 +01:00
parent a2f2f7db1f
commit b14de21fc8
2 changed files with 62 additions and 52 deletions

View File

@ -643,9 +643,8 @@ void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_
}
void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) {
assert(PY_PARSE_NODE_IS_STRUCT(pn));
if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
// this parameter has a default value
// in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
@ -662,7 +661,8 @@ void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) {
}
}
}
} else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
} else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
// bare star
comp->have_bare_star = true;
@ -2206,10 +2206,18 @@ void compile_node(compiler_t *comp, py_parse_node_t pn) {
void compile_scope_func_lambda_param(compiler_t *comp, py_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
// TODO verify that *k and **k are last etc
assert(PY_PARSE_NODE_IS_STRUCT(pn));
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
qstr param_name = 0;
py_parse_node_t pn_annotation = PY_PARSE_NODE_NULL;
if (PY_PARSE_NODE_IS_ID(pn)) {
param_name = PY_PARSE_NODE_LEAF_ARG(pn);
if (comp->have_bare_star) {
// comes after a bare star, so doesn't count as a parameter
} else {
comp->scope_cur->num_params += 1;
}
} else {
assert(PY_PARSE_NODE_IS_STRUCT(pn));
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
//int node_index = 1; unused
@ -2266,6 +2274,7 @@ void compile_scope_func_lambda_param(compiler_t *comp, py_parse_node_t pn, pn_ki
// TODO anything to implement?
assert(0);
}
}
if (param_name != 0) {
if (!PY_PARSE_NODE_IS_NULL(pn_annotation)) {

View File

@ -414,7 +414,8 @@ py_parse_node_t py_parse(py_lexer_t *lex, int wanted_rule) {
// never emit these rules if they have only 1 argument
// NOTE: can't put atom_paren here because we need it to distinguisg, for example, [a,b] from [(a,b)]
if (rule->rule_id == RULE_else_stmt || rule->rule_id == RULE_testlist_comp_3b || rule->rule_id == RULE_import_as_names_paren || rule->rule_id == RULE_typedargslist_colon || rule->rule_id == RULE_typedargslist_equal || rule->rule_id == RULE_dictorsetmaker_colon || rule->rule_id == RULE_classdef_2 || rule->rule_id == RULE_with_item_as || rule->rule_id == RULE_assert_stmt_extra || rule->rule_id == RULE_as_name || rule->rule_id == RULE_raise_stmt_from || rule->rule_id == RULE_vfpdef) {
// TODO possibly put varargslist_name, varargslist_equal here as well
if (rule->rule_id == RULE_else_stmt || rule->rule_id == RULE_testlist_comp_3b || rule->rule_id == RULE_import_as_names_paren || rule->rule_id == RULE_typedargslist_name || rule->rule_id == RULE_typedargslist_colon || rule->rule_id == RULE_typedargslist_equal || rule->rule_id == RULE_dictorsetmaker_colon || rule->rule_id == RULE_classdef_2 || rule->rule_id == RULE_with_item_as || rule->rule_id == RULE_assert_stmt_extra || rule->rule_id == RULE_as_name || rule->rule_id == RULE_raise_stmt_from || rule->rule_id == RULE_vfpdef) {
emit_rule = false;
}