Skip to content

Commit 046e63e

Browse files
authored
Merge pull request #8265 from diffblue/complex_initialization1
initialisation of `_Complex` with an initializer list
2 parents e6e665f + 0c1c1b3 commit 046e63e

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
_Complex float var1 = 1 + 2i;
2+
_Complex float var2 = {1, 2};
3+
_Complex float var3 = {1 + 2i};
4+
_Complex float var4 = {1 + 3i, 2};
5+
6+
int main(void)
7+
{
8+
__CPROVER_assert(var1 == var2, "var1 vs var2");
9+
__CPROVER_assert(var1 == var3, "var1 vs var3");
10+
__CPROVER_assert(var1 == var4, "var1 vs var4");
11+
return 0;
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
complex_initialization1.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,32 @@ exprt c_typecheck_baset::do_initializer_list(
969969
result = *zero;
970970
}
971971
}
972+
else if(type.id() == ID_complex)
973+
{
974+
// These may be initialized with an expression, an initializer list
975+
// of size two, or an initializer list of size one.
976+
if(value.operands().size() == 1)
977+
{
978+
return do_initializer_rec(
979+
to_unary_expr(value).op(), type, force_constant);
980+
}
981+
else if(value.operands().size() == 2)
982+
{
983+
auto &complex_type = to_complex_type(type);
984+
auto &subtype = complex_type.subtype();
985+
auto real = do_initializer_rec(
986+
to_binary_expr(value).op0(), subtype, force_constant);
987+
auto imag = do_initializer_rec(
988+
to_binary_expr(value).op1(), subtype, force_constant);
989+
return complex_exprt(real, imag, complex_type)
990+
.with_source_location(value.source_location());
991+
}
992+
else
993+
{
994+
throw errort().with_location(value.source_location())
995+
<< "too many initializers for '" << to_string(type) << "'";
996+
}
997+
}
972998
else
973999
{
9741000
// The initializer for a scalar shall be a single expression,

0 commit comments

Comments
 (0)