Merge pull request #19329 from github/idrissrio/designated-initializer

C++: add predicate to distinguish designator-based initializations
This commit is contained in:
Jeroen Ketema
2025-04-17 13:15:42 +02:00
committed by GitHub
14 changed files with 10705 additions and 458 deletions

View File

@@ -0,0 +1,11 @@
class Expr extends @expr {
string toString() { none() }
}
class AggregateLiteral extends Expr, @aggregateliteral {
override string toString() { none() }
}
from AggregateLiteral aggregate, Expr initializer, int element_index, int position
where aggregate_array_init(aggregate, initializer, element_index, position, _)
select aggregate, initializer, element_index, position

View File

@@ -0,0 +1,15 @@
class Expr extends @expr {
string toString() { none() }
}
class AggregateLiteral extends Expr, @aggregateliteral {
override string toString() { none() }
}
class MemberVariable extends @membervariable {
string toString() { none() }
}
from AggregateLiteral aggregate, Expr initializer, MemberVariable field, int position
where aggregate_field_init(aggregate, initializer, field, position, _)
select aggregate, initializer, field, position

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
description: add `hasDesignator` predicate to `ArrayOrVectorAggregateLiteral` and `ClassAggregateLiteral`
compatibility: backwards
aggregate_array_init.rel: run aggregate_array_init.qlo
aggregate_field_init.rel: run aggregate_field_init.qlo

View File

@@ -0,0 +1,4 @@
---
category: feature
---
* Introduced `hasDesignator()` predicates to distinguish between designated and positional initializations for both struct/union fields and array elements.

View File

@@ -213,7 +213,24 @@ class ClassAggregateLiteral extends AggregateLiteral {
Expr getFieldExpr(Field field, int position) {
field = classType.getAField() and
aggregate_field_init(underlyingElement(this), unresolveElement(result), unresolveElement(field),
position)
position, _)
}
/**
* Holds if the `position`-th initialization of `field` in this aggregate initializer
* uses a designated (e.g., `.x = ...`) rather than a positional initializer.
*
* For example, in:
* ```c
* struct S { int x, y; };
* struct S s = { .x = 1, 2 };
* ```
* - `.x = 1` is a designated initializer, therefore `hasDesignator(x, 0)` holds.
* - `2` is a positional initializer for `s.y`, therefore `hasDesignator(y, 1)` does not hold.
*/
predicate hasDesignator(Field field, int position) {
field = classType.getAField() and
aggregate_field_init(underlyingElement(this), _, unresolveElement(field), position, true)
}
/**
@@ -304,7 +321,24 @@ class ArrayOrVectorAggregateLiteral extends AggregateLiteral {
* - `a.getElementExpr(0, 2)` gives `789`.
*/
Expr getElementExpr(int elementIndex, int position) {
aggregate_array_init(underlyingElement(this), unresolveElement(result), elementIndex, position)
aggregate_array_init(underlyingElement(this), unresolveElement(result), elementIndex, position,
_)
}
/**
* Holds if the `position`-th initialization of the array element at `elementIndex`
* in this aggregate initializer uses a designated (e.g., `[0] = ...`) rather than
* a positional initializer.
*
* For example, in:
* ```c
* int x[] = { [0] = 1, 2 };
* ```
* - `[0] = 1` is a designated initializer, therefore `hasDesignator(0, 0)` holds.
* - `2` is a positional initializer for `x[1]`, therefore `hasDesignator(1, 1)` does not hold.
*/
predicate hasDesignator(int elementIndex, int position) {
aggregate_array_init(underlyingElement(this), _, elementIndex, position, true)
}
/**

View File

@@ -2039,7 +2039,8 @@ aggregate_field_init(
int aggregate: @aggregateliteral ref,
int initializer: @expr ref,
int field: @membervariable ref,
int position: int ref
int position: int ref,
boolean is_designated: boolean ref
);
/**
@@ -2051,7 +2052,8 @@ aggregate_array_init(
int aggregate: @aggregateliteral ref,
int initializer: @expr ref,
int element_index: int ref,
int position: int ref
int position: int ref,
boolean is_designated: boolean ref
);
@ctorinit = @ctordirectinit

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
class Expr extends @expr {
string toString() { none() }
}
class AggregateLiteral extends Expr, @aggregateliteral {
override string toString() { none() }
}
from AggregateLiteral aggregate, Expr initializer, int element_index, int position
where aggregate_array_init(aggregate, initializer, element_index, position)
select aggregate, initializer, element_index, position, false

View File

@@ -0,0 +1,15 @@
class Expr extends @expr {
string toString() { none() }
}
class AggregateLiteral extends Expr, @aggregateliteral {
override string toString() { none() }
}
class MemberVariable extends @membervariable {
string toString() { none() }
}
from AggregateLiteral aggregate, Expr initializer, MemberVariable field, int position
where aggregate_field_init(aggregate, initializer, field, position)
select aggregate, initializer, field, position, false

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
description: add `hasDesignator` predicate to `ArrayOrVectorAggregateLiteral` and `ClassAggregateLiteral`
compatibility: backwards
aggregate_array_init.rel: run aggregate_array_init.qlo
aggregate_field_init.rel: run aggregate_field_init.qlo