Skip to content

Commit 442a22f

Browse files
Evaluate the arguments of a constructor call before checking the constructor in constant evaluation [GH #527, GH #494]
1 parent fd29a1a commit 442a22f

19 files changed

Lines changed: 217 additions & 100 deletions

File tree

‎src/interpret.c‎

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25681,83 +25681,23 @@ compound statement of that handler.
2568125681
goto continue_mem_inits;
2568225682
} /* if */
2568325683

25684-
/* Retrieve the routine scope, or issue an error. */
2568525684
if (callee == NULL) {
2568625685
info_with_pos(ec_constexpr_expression_cannot_be_interpreted, pos, ips);
2568725686
do_constexpr_fail(result);
2568825687
goto done;
25689-
} else if (!callee->is_constexpr) {
25690-
info_with_pos_sym(ec_constexpr_call_to_nonconstexpr_function,
25691-
pos, symbol_for(callee), ips);
25692-
do_constexpr_fail(result);
25693-
goto done;
25694-
} /* if */
25695-
if (!callee->defined) {
25696-
if (has_routine_definition_from_module(callee)) {
25697-
/* The definition was stored in a module file. */
25698-
if (!load_routine_definition_from_module(callee)) {
25699-
ips->input_error = TRUE;
25700-
do_constexpr_fail(result);
25701-
} /* if */
25702-
} else if ((callee->is_inheriting_ctor || callee->compiler_generated) &&
25703-
!callee->is_deleted) {
25704-
force_definition_of_compiler_generated_routine(callee);
25705-
} else {
25706-
set_instance_required(symbol_for(callee), TRUE, SIR_CONSTANT_CONTEXT);
25707-
} /* if */
25708-
} /* if */
25709-
if (callee->function_def_number == NULL_function_def_number) {
25710-
info_with_pos_sym(ec_constexpr_function_undefined, pos,
25711-
symbol_for(callee), ips);
25712-
do_constexpr_fail(result);
25713-
if (callee->is_deleted && ips->is_constant_evaluated) {
25714-
/* An error has presumably been issued earlier (use of a deleted
25715-
function). Treat this as an input error to avoid extraneous
25716-
diagnostics. */
25717-
expect_error();
25718-
ips->input_error = TRUE;
25719-
} /* if */
25720-
goto done;
25721-
} else if (callee->is_prototype_instantiation) {
25722-
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
25723-
symbol_for(callee), ips);
25724-
do_constexpr_fail(result);
25725-
goto done;
25726-
} else if (cost_exceeded(ips)) {
25727-
report_excessive_cost(ips);
25728-
do_constexpr_fail(result);
25729-
goto done;
2573025688
} else {
2573125689
a_byte *result_storage = cap.address;
2573225690
a_byte *complete_object = cap.complete_object;
25733-
a_scope_ptr callee_scope = scope_for_routine(callee);
25691+
a_scope_ptr callee_scope;
2573425692
an_expr_node_ptr args = dip->variant.constructor.args, arg;
25735-
a_variable_ptr params = callee_scope->variant.routine.parameters,
25736-
param, this_var;
25693+
a_variable_ptr params, param, this_var;
2573725694
a_base_class_ptr bcp;
2573825695
a_byte_count n_args = 1, n_params = 1;
2573925696
a_byte_count *arg_size;
2574025697
a_byte *arg_ptrs, **p_arg_ptr, *this_bytes, *arg_sizes;
2574125698
an_alloc_seq_number alloc_seq_number;
2574225699
a_type_ptr class_type = parent_class_of(callee);
2574325700
unsigned long up_front_cost;
25744-
/* Don't attempt to interpret a non-constexpr function. The flag
25745-
scope->is_constexpr_routine is set at the end of a constexpr function
25746-
definition, so this also prevents the interpretation of a function that
25747-
is not fully parsed (e.g., requested due to a recursive call in a
25748-
constexpr function). */
25749-
if (!callee_scope->is_constexpr_routine) {
25750-
do_constexpr_fail(result);
25751-
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
25752-
symbol_for(callee), ips);
25753-
goto done;
25754-
} /* if */
25755-
if (class_type->variant.class_struct_union.any_virtual_base_classes) {
25756-
do_constexpr_fail(result);
25757-
info_with_pos_type(ec_constexpr_object_with_virtual_base, pos,
25758-
class_type, ips);
25759-
goto done;
25760-
} /* if */
2576125701
/* Set up arguments, starting with "this" if applicable. */
2576225702
/* This process must happen in two phases. First, the arguments must be
2576325703
allocated and evaluated. Only then can we map parameter variables onto
@@ -25775,17 +25715,9 @@ compound statement of that handler.
2577525715
result = FALSE;
2577625716
goto done;
2577725717
} /* if */
25778-
/* Count the parameters (including "this") to make sure there are enough
25779-
arguments for the parameters. */
25780-
for (param = params; param != NULL; param = param->next) {
25781-
n_params += 1;
25782-
} /* if */
25783-
if (n_args < n_params) {
25784-
info_with_pos(ec_too_few_arguments, pos, ips);
25785-
do_constexpr_fail(result);
25786-
goto done;
25787-
} /* if */
25788-
/* Phase 1: Allocate and evaluate the arguments. */
25718+
/* Phase 1: Allocate and evaluate the arguments. That precedes the checks
25719+
of the constructor below: If the evaluation of an argument exits by
25720+
throwing an exception, the constructor is not invoked. */
2578925721
/* The "+1" below is to account for the "this" pointer (handled later). */
2579025722
p_arg_ptr = (a_byte**)arg_ptrs+1;
2579125723
arg_size = (a_byte_count*)arg_sizes;
@@ -25794,13 +25726,16 @@ compound statement of that handler.
2579425726
/* The first non-this argument is implicit (i.e., not represented in
2579525727
the IL). Since it always corresponds to the reference parameter of
2579625728
a copy/move constructor, we know it is a constexpr address. */
25729+
a_type_ptr routine_type = skip_typerefs(callee->type);
25730+
a_type_ptr param_type = routine_type->variant.routine.extra_info->
25731+
param_type_list->type;
2579725732
a_byte_count n_bytes = sizeof(a_constexpr_address);
2579825733
a_byte *arg_bytes;
2579925734
do_host_alignment(&n_bytes);
2580025735
*arg_size = n_bytes;
2580125736
arg_size += 1;
2580225737
n_bytes += sizeof(a_var_postfix);
25803-
if (!alloc_complete_object(ips, n_bytes, params->type, arg_bytes)) {
25738+
if (!alloc_complete_object(ips, n_bytes, param_type, arg_bytes)) {
2580425739
goto done;
2580525740
} /* if */
2580625741
*(a_constexpr_address*)arg_bytes = *implied_src;
@@ -25863,6 +25798,79 @@ compound statement of that handler.
2586325798
goto done;
2586425799
} /* if */
2586525800
} /* for */
25801+
/* Check that the constructor can be interpreted, and retrieve its routine
25802+
scope. */
25803+
if (!callee->is_constexpr) {
25804+
info_with_pos_sym(ec_constexpr_call_to_nonconstexpr_function,
25805+
pos, symbol_for(callee), ips);
25806+
do_constexpr_fail(result);
25807+
goto done;
25808+
} /* if */
25809+
if (!callee->defined) {
25810+
if (has_routine_definition_from_module(callee)) {
25811+
/* The definition was stored in a module file. */
25812+
if (!load_routine_definition_from_module(callee)) {
25813+
ips->input_error = TRUE;
25814+
do_constexpr_fail(result);
25815+
} /* if */
25816+
} else if ((callee->is_inheriting_ctor || callee->compiler_generated) &&
25817+
!callee->is_deleted) {
25818+
force_definition_of_compiler_generated_routine(callee);
25819+
} else {
25820+
set_instance_required(symbol_for(callee), TRUE, SIR_CONSTANT_CONTEXT);
25821+
} /* if */
25822+
} /* if */
25823+
if (callee->function_def_number == NULL_function_def_number) {
25824+
info_with_pos_sym(ec_constexpr_function_undefined, pos,
25825+
symbol_for(callee), ips);
25826+
do_constexpr_fail(result);
25827+
if (callee->is_deleted && ips->is_constant_evaluated) {
25828+
/* An error has presumably been issued earlier (use of a deleted
25829+
function). Treat this as an input error to avoid extraneous
25830+
diagnostics. */
25831+
expect_error();
25832+
ips->input_error = TRUE;
25833+
} /* if */
25834+
goto done;
25835+
} else if (callee->is_prototype_instantiation) {
25836+
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
25837+
symbol_for(callee), ips);
25838+
do_constexpr_fail(result);
25839+
goto done;
25840+
} else if (cost_exceeded(ips)) {
25841+
report_excessive_cost(ips);
25842+
do_constexpr_fail(result);
25843+
goto done;
25844+
} /* if */
25845+
callee_scope = scope_for_routine(callee);
25846+
params = callee_scope->variant.routine.parameters;
25847+
/* Don't attempt to interpret a non-constexpr function. The flag
25848+
scope->is_constexpr_routine is set at the end of a constexpr function
25849+
definition, so this also prevents the interpretation of a function that
25850+
is not fully parsed (e.g., requested due to a recursive call in a
25851+
constexpr function). */
25852+
if (!callee_scope->is_constexpr_routine) {
25853+
do_constexpr_fail(result);
25854+
info_with_pos_sym(ec_constexpr_call_not_interpretable, pos,
25855+
symbol_for(callee), ips);
25856+
goto done;
25857+
} /* if */
25858+
if (class_type->variant.class_struct_union.any_virtual_base_classes) {
25859+
do_constexpr_fail(result);
25860+
info_with_pos_type(ec_constexpr_object_with_virtual_base, pos,
25861+
class_type, ips);
25862+
goto done;
25863+
} /* if */
25864+
/* Count the parameters (including "this") to make sure there are enough
25865+
arguments for the parameters. */
25866+
for (param = params; param != NULL; param = param->next) {
25867+
n_params += 1;
25868+
} /* if */
25869+
if (n_args < n_params) {
25870+
info_with_pos(ec_too_few_arguments, pos, ips);
25871+
do_constexpr_fail(result);
25872+
goto done;
25873+
} /* if */
2586625874
/* Phase 2: Map the parameters to the arguments. */
2586725875
/* Associate with the parameter variables a new allocation number. For
2586825876
ordinary calls, we just use the allocation number about to be created

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx0x-initializer-stdinitializerlist.rto/default.1.1.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
112112
"Test_name.c", line 314: error: expression must have a constant value
113113
constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
114114
^
115-
"Test_name.c", line 317: note: cannot call non-constexpr function "TemporaryInitListSourceRange_PR22367::A::A(std::initializer_list<int>)" (declared at line 311)
115+
"Test_name.c", line 317: note: cannot call non-constexpr function "std::initializer_list<_E>::initializer_list(const _E *, std::size_t) [with _E=int]" (declared at line 34)
116116
std::initializer_list // expected-note {{constructor}}
117117
^
118118

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx0x-initializer-stdinitializerlist.rto/default.2.1.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
112112
"Test_name.c", line 314: error: expression must have a constant value
113113
constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
114114
^
115-
"Test_name.c", line 317: note: cannot call non-constexpr function "TemporaryInitListSourceRange_PR22367::A::A(std::initializer_list<int>)" (declared at line 311)
115+
"Test_name.c", line 317: note: cannot call non-constexpr function "std::initializer_list<_E>::initializer_list(const _E *, std::size_t) [with _E=int]" (declared at line 34)
116116
std::initializer_list // expected-note {{constructor}}
117117
^
118118

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/default.1.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8585
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
8686
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
8787
^
88-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
88+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
8989
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
90-
^
90+
^
9191
detected during implicit generation of "GH112677::C::C(int) [with Anything=int]" at line 520
9292

9393
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/default.2.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8585
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
8686
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
8787
^
88-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
88+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
8989
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
90-
^
90+
^
9191
detected during implicit generation of "GH112677::C::C(int) [with Anything=int]" at line 520
9292

9393
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/default.3.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ fe_only -DTEST_NUMBER=3 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
120120
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
121121
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
122122
^
123-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
123+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
124124
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
125-
^
125+
^
126126
detected during implicit generation of "GH112677::C::C(int) [with Anything=int]" at line 520
127127

128128
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/default.4.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ fe_only -DTEST_NUMBER=4 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
120120
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
121121
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
122122
^
123-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
123+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
124124
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
125-
^
125+
^
126126
detected during implicit generation of "GH112677::C::C(int) [with Anything=int]" at line 520
127127

128128
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/edg_x86_32_cp.1.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8585
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
8686
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
8787
^
88-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
88+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
8989
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
90-
^
90+
^
9191

9292
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression
9393
C c(0); // expected-note{{in implicit initialization for inherited constructor of 'C'}}

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/edg_x86_32_cp.2.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8585
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
8686
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
8787
^
88-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
88+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
8989
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
90-
^
90+
^
9191

9292
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression
9393
C c(0); // expected-note{{in implicit initialization for inherited constructor of 'C'}}

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2b-consteval-propagate.rto/edg_x86_32_cp.3.1.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ fe_only -DTEST_NUMBER=3 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
120120
"Test_name.c", line 514: error: call to consteval function "GH112677::ConstEval::ConstEval(int)" did not produce a valid constant expression
121121
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
122122
^
123-
"Test_name.c", line 514: note: constexpr function "GH112677::ConstEval::ConstEval(int)" (declared at line 508) is not defined
123+
"Test_name.c", line 514: note: the value of parameter "arg" (declared at line 514) cannot be used as a constant
124124
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
125-
^
125+
^
126126

127127
"Test_name.c", line 520: error: call to consteval function "GH112677::C::C(int) [with Anything=int]" did not produce a valid constant expression
128128
C c(0); // expected-note{{in implicit initialization for inherited constructor of 'C'}}

0 commit comments

Comments
 (0)