Branch: Tag:

2002-05-11

2002-05-11 21:08:00 by Martin Stjernholm <mast@lysator.liu.se>

Cleaned up and fixed some bugs in the lookup of magic program identifiers
like this_program and ::`->. It's now possible to use e.g.
global::this_program. Added an optional level argument to this_object().

Added a magic identifier "this" that can be used instead of this_object().
Like this_program, it only exists when there's no other identifier with that
name. The object of a surrounding class can be adressed with Foo::this.

Rev: src/builtin_functions.c:1.428
Rev: src/interpret_functions.h:1.103
Rev: src/language.yacc:1.283
Rev: src/program.c:1.430
Rev: src/program.h:1.160

5:   \*/   /**/   #include "global.h" - RCSID("$Id: program.c,v 1.429 2002/05/11 00:29:58 nilsson Exp $"); + RCSID("$Id: program.c,v 1.430 2002/05/11 21:08:00 mast Exp $");   #include "program.h"   #include "object.h"   #include "dynamic_buffer.h"
100:   #define DECLARE   #include "compilation.h"    - struct pike_string *this_program_string=0; + struct pike_string *this_program_string = NULL, *this_string = NULL;   static struct pike_string *UNDEFINED_string=0;      char *lfun_names[] = {
1019:    }    }    -  /* Handle this_program */ -  if (ident == this_program_string) { -  if (compilation_depth > 0) -  return mkexternalnode(Pike_compiler->previous->new_program, -  Pike_compiler->previous->parent_identifier); -  else { -  struct svalue s; -  s.type=T_PROGRAM; -  s.u.program=Pike_compiler->new_program; -  return mkconstantsvaluenode(&s); -  } -  } +     /* Handle UNDEFINED */    if (ident == UNDEFINED_string) {    struct svalue s;
1121:    return 0;   }    + /* If the identifier is recognized as one of the magic identifiers, +  * like "this", "this_program" or "`->" when preceded by ::, then a +  * suitable node is returned, NULL otherwise. inherit_num is -1 when +  * accessing all inherits (i.e. when :: is used without any identifier +  * before). */ + struct node_s *program_magic_identifier (struct program_state *state, +  int state_depth, int inherit_num, +  struct pike_string *ident, +  int colon_colon_ref) + { + #if 0 +  fprintf (stderr, "magic_identifier (state, %d, %d, %s, %d)\n", +  state_depth, inherit_num, ident->str, colon_colon_ref); + #endif +  +  if (!inherit_num) { +  /* These are only recognized when looking in the current program +  * and not an inherited one. */ +  +  /* Handle this */ +  if (ident == this_string) +  return mkefuncallnode ("this_object", mknewintnode (state_depth)); +  +  /* Handle this_program */ +  if (ident == this_program_string) { +  if (compilation_depth > state_depth) +  return mkexternalnode(state->previous->new_program, +  state->previous->parent_identifier); +  else { +  struct svalue s; +  s.type=T_PROGRAM; +  s.u.program=state->new_program; +  return mkconstantsvaluenode(&s); +  } +  } +  } +  +  if (colon_colon_ref) { +  /* These are only recognized when prefixed with the :: operator. */ +  +  if (inherit_num < 0) inherit_num = 0; +  if(ident == lfun_strings[LFUN_ARROW] || +  ident == lfun_strings[LFUN_INDEX]) { +  return mknode(F_MAGIC_INDEX, mknewintnode(inherit_num), +  mknewintnode(state_depth)); +  } else if(ident == lfun_strings[LFUN_ASSIGN_ARROW] || +  ident == lfun_strings[LFUN_ASSIGN_INDEX]) { +  return mknode(F_MAGIC_SET_INDEX, mknewintnode(inherit_num), +  mknewintnode(state_depth)); +  } else if(ident == lfun_strings[LFUN__INDICES]) { +  return mknode(F_MAGIC_INDICES, mknewintnode(inherit_num), +  mknewintnode(state_depth)); +  } else if(ident == lfun_strings[LFUN__VALUES]) { +  return mknode(F_MAGIC_VALUES, mknewintnode(inherit_num), +  mknewintnode(state_depth)); +  } +  } +  +  return NULL; + } +    /* Fixme: allow level=0 to return the current level */   struct program *parent_compilation(int level)   {
3261:    */   int isidentifier(struct pike_string *s)   { -  INT32 e; +     return really_low_find_shared_string_identifier(s,    Pike_compiler->new_program,    SEE_STATIC|SEE_PRIVATE);
5870:    init_program_blocks();       MAKE_CONSTANT_SHARED_STRING(this_program_string,"this_program"); +  MAKE_CONSTANT_SHARED_STRING(this_string,"this");    MAKE_CONSTANT_SHARED_STRING(UNDEFINED_string,"UNDEFINED");       lfun_ids = allocate_mapping(NUM_LFUNS);
5932:    int e;       free_string(UNDEFINED_string); +  free_string(this_string);    free_string(this_program_string);       free_mapping(lfun_types);