Skip to content

Commit

Permalink
Demote __SCRIPT__ to an identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed May 20, 2017
1 parent e1e024d commit d6cd8e9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/parse/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static void read_magic_id( struct parse* parse, struct expr_reading* reading );

void p_init_expr_reading( struct expr_reading* reading, bool in_constant,
bool skip_assign, bool skip_call, bool expect_expr ) {
t_init_pos_id( &reading->pos, INTERNALFILE_COMPILER );
reading->node = NULL;
reading->output_node = NULL;
reading->in_constant = in_constant;
Expand Down Expand Up @@ -591,7 +592,6 @@ static void read_primary( struct parse* parse, struct expr_reading* reading ) {
break;
case TK_NAMESPACENAME:
case TK_FUNCTIONNAME:
case TK_SCRIPTNAME:
read_magic_id( parse, reading );
break;
default:
Expand Down
1 change: 0 additions & 1 deletion src/parse/token/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ static void read_token_bcs( struct parse* parse ) {
} table[] = {
{ "__function__", TK_FUNCTIONNAME },
{ "__namespace__", TK_NAMESPACENAME },
{ "__script__", TK_SCRIPTNAME },
{ "assert", TK_ASSERT },
{ "auto", TK_AUTO },
{ "bool", TK_BOOL },
Expand Down
44 changes: 44 additions & 0 deletions src/semantic/dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ struct param_list_test {
bool need_public_spec;
};

struct builtin_aliases {
struct {
struct alias alias;
struct object object;
} name;
};

struct builtin_script_aliases {
struct {
struct alias alias;
struct temp_magic_id magic_id;
} name;
};

struct value_list {
struct value* head;
struct value* tail;
Expand Down Expand Up @@ -223,6 +237,9 @@ static void default_value_mismatch( struct semantic* semantic,
struct type_info* type, struct pos* pos );
static int get_param_number( struct func* func, struct param* target );
static bool test_external_func( struct semantic* semantic, struct func* func );
static void init_builtin_aliases( struct semantic* semantic,
struct func* func );
static void init_magic_id( struct temp_magic_id* magic_id, int name );
static void init_func_test( struct func_test* test, struct func_test* parent,
struct func* func, struct list* labels, struct list* funcscope_vars,
struct script* script );
Expand All @@ -243,6 +260,8 @@ static void test_script_param_list( struct semantic* semantic,
struct script* script );
static void test_script_body( struct semantic* semantic,
struct script* script );
static void init_builtin_script_aliases( struct semantic* semantic,
struct builtin_script_aliases* aliases, struct script* script );

void s_test_constant( struct semantic* semantic, struct constant* constant ) {
struct expr_test expr;
Expand Down Expand Up @@ -2307,6 +2326,18 @@ void s_test_func_body( struct semantic* semantic, struct func* func ) {
}
}

static void init_builtin_aliases( struct semantic* semantic,
struct func* func ) {

}

static void init_magic_id( struct temp_magic_id* magic_id, int name ) {
t_init_object( &magic_id->object, NODE_TEMPMAGICID );
magic_id->object.resolved = true;
magic_id->string = NULL;
magic_id->name = name;
}

static void init_func_test( struct func_test* test, struct func_test* parent,
struct func* func, struct list* labels, struct list* funcscope_vars,
struct script* script ) {
Expand Down Expand Up @@ -2419,6 +2450,8 @@ static void test_script_param_list( struct semantic* semantic,
static void test_script_body( struct semantic* semantic,
struct script* script ) {
s_add_scope( semantic, true );
struct builtin_script_aliases aliases;
init_builtin_script_aliases( semantic, &aliases, script );
struct param* param = script->params;
while ( param ) {
if ( param->name ) {
Expand All @@ -2438,6 +2471,17 @@ static void test_script_body( struct semantic* semantic,
s_pop_scope( semantic );
}

static void init_builtin_script_aliases( struct semantic* semantic,
struct builtin_script_aliases* aliases, struct script* script ) {
init_magic_id( &aliases->name.magic_id, MAGICID_SCRIPT );
struct alias* alias = &aliases->name.alias;
s_init_alias( alias );
alias->object.resolved = true;
alias->target = &aliases->name.magic_id.object;
struct name* name = t_extend_name( semantic->ns->body, "__script__" );
s_bind_local_name( semantic, name, &aliases->name.alias.object, true );
}

void s_calc_var_size( struct var* var ) {
var->size = calc_size( var->dim, var->structure, var->ref );
}
Expand Down
58 changes: 56 additions & 2 deletions src/semantic/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ static void select_ns( struct semantic* semantic, struct result* result,
struct ns* ns );
static void select_alias( struct semantic* semantic, struct expr_test* test,
struct result* result, struct alias* alias );
static void select_magic_id( struct semantic* semantic, struct expr_test* test,
struct result* result, struct name_usage* usage,
struct temp_magic_id* magic_id );
static void expand_magic_id_once( struct semantic* semantic,
struct temp_magic_id* magic_id );
static void expand_temp_magic_id( struct semantic* semantic,
struct temp_magic_id* magic_id );
static void test_strcpy( struct semantic* semantic, struct expr_test* test,
struct result* result, struct strcpy_call* call );
static void test_memcpy( struct semantic* semantic, struct expr_test* test,
Expand Down Expand Up @@ -2644,8 +2651,15 @@ static void test_found_object( struct semantic* semantic,
s_bail( semantic );
}
}
select_object( semantic, test, result, object );
usage->object = &object->node;
switch ( object->node.type ) {
case NODE_TEMPMAGICID:
select_magic_id( semantic, test, result, usage,
( struct temp_magic_id* ) object );
break;
default:
select_object( semantic, test, result, object );
usage->object = &object->node;
}
}

static struct ref* find_map_ref( struct ref* ref,
Expand Down Expand Up @@ -2725,6 +2739,7 @@ static void select_object( struct semantic* semantic, struct expr_test* test,
break;
default:
UNREACHABLE();
s_bail( semantic );
}
}

Expand Down Expand Up @@ -2918,6 +2933,45 @@ static void select_alias( struct semantic* semantic, struct expr_test* test,
select_object( semantic, test, result, alias->target );
}

static void select_magic_id( struct semantic* semantic, struct expr_test* test,
struct result* result, struct name_usage* usage,
struct temp_magic_id* magic_id ) {
expand_magic_id_once( semantic, magic_id );
struct indexed_string_usage* string_usage = t_alloc_indexed_string_usage();
string_usage->string = magic_id->string;
usage->object = &string_usage->node;
test_string( semantic, test, result, string_usage->string );
}

static void expand_magic_id_once( struct semantic* semantic,
struct temp_magic_id* magic_id ) {
if ( ! magic_id->string ) {
expand_temp_magic_id( semantic, magic_id );
}
}

static void expand_temp_magic_id( struct semantic* semantic,
struct temp_magic_id* magic_id ) {
struct str name;
str_init( &name );
switch ( magic_id->name ) {
case MAGICID_SCRIPT:
if ( semantic->func_test->script->named_script ) {
struct indexed_string* string = t_lookup_string( semantic->task,
semantic->func_test->script->number->value );
str_append( &name, string->value );
}
else {
str_append_number( &name,
semantic->func_test->script->number->value );
}
break;
}
magic_id->string = t_intern_string_copy( semantic->task,
name.value, name.length );
str_deinit( &name );
}

static void test_strcpy( struct semantic* semantic, struct expr_test* test,
struct result* result, struct strcpy_call* call ) {
// Array.
Expand Down
8 changes: 8 additions & 0 deletions src/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ struct node {
NODE_COMPOUNDLITERAL,
NODE_MAGICID,
NODE_BUILDMSG,
// 60
NODE_TEMPMAGICID,
} type;
};

Expand Down Expand Up @@ -327,6 +329,12 @@ struct magic_id {
} name;
};

struct temp_magic_id {
struct object object;
struct indexed_string* string;
int name;
};

struct unary {
struct node node;
enum {
Expand Down

0 comments on commit d6cd8e9

Please sign in to comment.