[ast-optimizer-branch] simplifying C++

Jason Merrill jason@redhat.com
Mon Jun 17 09:24:00 GMT 2002


I've been thinking about simplifying C++, and the complex scoping that C++
loops have.  In a for loop, a decl in the for-init-stmt is in scope for the
duration of the loop; a decl in the condition is in scope within an
iteration, goes out of scope, and back in.  The first can be dealt with
simply by wrapping the loop in a scope.  The second is more complex; I
think the best way to deal with it is to move the test into the loop body.

So given

  struct A {
    A(bool);
    operator bool();
    ~A();
  };

this

  for (int i = 42; A a (i == 24); --i)
    {
      ...
    }

becomes this pseudocode:

  {
    int i;
    for (i = 42; ; --i)
      {
        A* T.1;
        bool T.2;
        bool T.3;
        A a;
        T.1 = &a;
        T.2 = i == 24;
        A::ctor (T.1, T.2);
        __with_cleanup (A::dtor (T.1))
          { 
            T.3 = a.operator bool();
            if (T.3 == 0)
              break;
            ...
          }
      }
  }

I think it's best to leave the cleanup in this structured form initially,
and deal with adding it to the various block exits later.

Make sense?

Jason



More information about the Gcc mailing list