Rescoped Expressions

Sometimes the normal language visibility rules are not sufficient for specifying the variable, function, and so on, to which you may want to refer. The debugger extends the language's idea of an expression with an additional possibility called a rescoped expression.

Rescoped expressions cause the debugger to look up the identifiers and so on in the qual-symbol-opt, as though it were in the source file specified by the preceding filename-tick  or qual-symbol-opt.

 

rescoped-expression

        : filename-tick qual-symbol-opt

        | TICK qual-symbol-opt

 

rescoped-typedef

        : filename-tick qual-typedef-opt

        | TICK qual-typedef-opt

 

filename-tick

        : string-tick

 

string-tick

        : string TICK

 

qual-symbol-opt

        : expression                        /* Base (global) name */

        | qual-symbol-opt TICK expression   /* Qualified name */

 

qual-typedef-opt

        : qual-typedef-opt for C

        | qual-typedef-opt for C++

        | qual-typedef-opt for Fortran

 

In the following example, rescoped expressions are used to distinguish which x the user is querying, because there are two variables named x (one local to main and one global):

 

(idb) list $curline - 10:20

      1 long x = 5;             // global x

      2

      3 int main()

      4 {

      5     int x = 7;          // local x

      6     int y = x - ::x;

>     7     return (y);

      8 }

 

By default, a local variable is found before a global one, so that the plain x refers to the local variable.

 

(idb) whatis x

int x

(idb) which x

"x_rescoped.cxx"`main`x

(idb) whatis "x_rescoped.cxx"`main`x

int x

 

You may use the C++ :: operator to specify the global x in C++ code or rescoped expressions in any language.

 

(idb) whatis ::x

long x

(idb) whatis "x_rescoped.cxx"`x

long x

(idb) print "x_rescoped.cxx"`x

5

 

In the following example, the x variable is used in the following places to demonstrate how rescoping expressions can find the correct variable:

 

(idb) list $curline - 10:20

     10 double x = 3.1415;

     11

     12 int CastAndAdd(char x) {

     13     int result = ((int)::x) + x;

     14     return result;

     15 }

     16

     17 float Foo::SetandGet() {    // multiple scopes!

     18     long x = (long)::x;     // local x = global x

     19     Foo::x = (float)x;      // member x = local x

>    20     return Foo::x;          // return member x

     21 }

     22

     23 int main () {

     24     int x = 7;

     25     x -= CastAndAdd((char)1);

     26

     27     Foo thefoo;

     28     x -= (int)thefoo.SetandGet();

     29     return x;

(idb) whatis x

long x

(idb) which x

"src/x_rescoped2.cxx"`Foo::SetandGet`x

(idb) whatis ::x

double x

(idb) whatis Foo::x

float Foo::x

(idb) whatis `main`x

int x

(idb) whatis `CastAndAdd`x

char x