Error: expected ',' in !foreach/!filter AND error: expected ';' after declaration

I was studying TableGen from this link. The following code was mentioned:

def Defs {
    int num;
}

class Example<list<int> nums> {
    list<int> doubled = !foreach(Defs.num, nums, !add(Defs.num, Defs.num));
    /* !foreach(var, sequence, expr)
     * This operator creates a new list/dag in which each element
     * is a function of the corresponding element in the sequence list/dag.
     * To perform the function, TableGen binds the variable var to an element
     * and then evaluates the expression. The expression presumably refers
     * to the variable var and calculates the result value.
     * If you simply want to create a list of a certain length containing
     * the same value repeated multiple times, see !listsplat.
     */
}

def MyNums : Example<[4, 1, 9, -3]>;

However, upon parsing it via llvm-tblgen, I am getting this error:

2_functional.td:20:38: error: expected ',' in !foreach/!filter
    list<int> doubled = !foreach(Defs.num, nums, !add(Defs.num, Defs.num));
                                     ^
2_functional.td:20:38: error: expected ';' after declaration
    list<int> doubled = !foreach(Defs.num, nums, !add(Defs.num, Defs.num));
                                     ^

How do I fix this error?

The code snippet following the original is as follows:

class Example<list<int> nums> {
    list<int> doubled = !foreach(x, nums, !add(x, x));
}

def MyNums : Example<[4, 1, 9, -3]>;

Which generates:

class Example<list<int> Example:nums = ?> {
  list<int> doubled = !foreach(x, Example:nums, !add(x, x));
}

def MyNums {    // Example
  list<int> doubled = [8, 2, 18, -6];
}

So is this the case: the syntax in the question has been changed to the above?

The 1st arg of !foreach is “the accumulator var”.
Assume (as C++) for (auto num : nums)

You don’t require def Defs to declare the accumulator.
Simply, !foreach(num, nums, !add(num, num))
num is handled as a local (temporary) variable in !foreach scope.

Consider that where x is declared in the original snippet.

1 Like