Re: Retrieving initializer list values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 03/10/2017 08:40 AM, Anubhav Sharma wrote:
Hello,

Suppose I have the following array declaration inside a function:

    static const int arr[3] = {1,2,3};

Given the tree of variable arr, what functions should I use to find the
list of values with which the array was initialized?

I think it goes something like this:

  // Get arr's initializer expression:
  tree init = DECL_INITIAL (arr);

  // Get the CONSTRUCTOR from the initializer.
  tree ctor = TREE_OPERAND (init, 1)

  // Get a pointer to the vector of CONSTRUCTOR elements.
  vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);

  unsigned HOST_WIDE_INT i;
  tree purpose;
  tree value;

  // Iterate over the elements in the vector, with i as
  // the zero-based loop counter, purpose as the index into
  // the array, and value as the initializer for the array
  // element at the index given by purpose.
  FOR_EACH_CONSTRUCTOR_ELT (elts, i, purpose, value)
    {
    }

The elements may not be in the same order as in the initializer
list.

There are examples of the FOR_EACH_CONSTRUCTOR_ELT in the code
that can serve as examples to step through to understand how
it works.  For instance, when the declaration of arr above is
being completed, the initializer is checked to verify that
each element is constant in initializer_constant_valid_p_1
by iterating over the elements using the similar
FOR_EACH_CONSTRUCTOR_VALUE() macro.

Martin



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux