Skip to content

Commit

Permalink
Make function literals work at the namespace level
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed Jan 6, 2017
1 parent 8f36c4e commit dc2ddfc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/parse/dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,7 @@ void p_read_paren_type( struct parse* parse, struct paren_reading* reading ) {
if ( dec.object == DECOBJ_FUNC || dec.spec == SPEC_AUTO ||
parse->tk == TK_PAREN_L ) {
func = alloc_func( parse, &dec );
func->literal = true;
p_test_tk( parse, TK_PAREN_L );
p_read_tk( parse );
read_func_param_list( parse, func );
Expand All @@ -1790,6 +1791,11 @@ void p_read_paren_type( struct parse* parse, struct paren_reading* reading ) {
// Function literal.
if ( func ) {
read_func_body( parse, &dec, func );
if ( dec.area == DEC_TOP ) {
p_add_unresolved( parse, &func->object );
list_append( &parse->lib->funcs, func );
list_append( &parse->ns_fragment->funcs, func );
}
reading->func = func;
}
// Compound literal.
Expand Down Expand Up @@ -1872,6 +1878,7 @@ struct func* alloc_func( struct parse* parse, struct dec* dec ) {
func->imported = parse->lib->imported;
func->external = dec->external;
func->force_local_scope = dec->force_local_scope;
func->literal = false;
return func;
}

Expand Down
2 changes: 0 additions & 2 deletions src/parse/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1275,8 +1275,6 @@ void read_paren( struct parse* parse, struct expr_reading* reading ) {

void read_func_literal( struct parse* parse, struct expr_reading* reading,
struct paren_reading* paren ) {
struct func_user* impl = paren->func->impl;
impl->nested = true;
reading->node = &paren->func->object.node;
}

Expand Down
2 changes: 1 addition & 1 deletion src/semantic/dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,7 @@ bool test_func_ref( struct semantic* semantic, struct func* func ) {
}

bool test_func_name( struct semantic* semantic, struct func* func ) {
if ( func->name && semantic->in_localscope ) {
if ( func->name && semantic->in_localscope && ! func->literal ) {
s_bind_local_name( semantic, func->name, &func->object,
func->force_local_scope );
}
Expand Down
4 changes: 3 additions & 1 deletion src/semantic/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2845,7 +2845,9 @@ void test_compound_literal( struct semantic* semantic, struct expr_test* test,

void test_anon_func( struct semantic* semantic, struct result* result,
struct func* func ) {
s_test_nested_func( semantic, func );
if ( semantic->in_localscope ) {
s_test_nested_func( semantic, func );
}
select_func( semantic, result, func );
}

Expand Down
1 change: 1 addition & 0 deletions src/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ struct func {
bool imported;
bool external;
bool force_local_scope;
bool literal;
};

struct func_aspec {
Expand Down

0 comments on commit dc2ddfc

Please sign in to comment.