| 1 |
/* Definitions for C++ contract levels |
| 2 |
Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Jeff Chapman II ([email protected]) |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
/* Design Notes |
| 22 |
|
| 23 |
A function is called a "guarded" function if it has pre or post contract |
| 24 |
attributes. A contract is considered an "active" contract if runtime code is |
| 25 |
needed for the contract under the current contract configuration. |
| 26 |
|
| 27 |
pre and post contract attributes are parsed and stored in DECL_ATTRIBUTES. |
| 28 |
assert contracts are parsed and wrapped in statements. When genericizing, all |
| 29 |
active and assumed contracts are transformed into an if block. An observed |
| 30 |
contract: |
| 31 |
|
| 32 |
[[ pre: v > 0 ]] |
| 33 |
|
| 34 |
is transformed into: |
| 35 |
|
| 36 |
if (!(v > 0)) { |
| 37 |
handle_contract_violation(__pseudo_contract_violation{ |
| 38 |
5, // line_number, |
| 39 |
"main.cpp", // file_name, |
| 40 |
"fun", // function_name, |
| 41 |
"v > 0", // comment, |
| 42 |
"default", // assertion_level, |
| 43 |
"default", // assertion_role, |
| 44 |
maybe_continue, // continuation_mode |
| 45 |
}); |
| 46 |
terminate (); // if never_continue |
| 47 |
} |
| 48 |
|
| 49 |
We use an internal type with the same layout as contract_violation rather |
| 50 |
than try to define the latter internally and somehow deal with its actual |
| 51 |
definition in a TU that includes <contract>. |
| 52 |
|
| 53 |
??? is it worth factoring out the calls to handle_contract_violation and |
| 54 |
terminate into a local function? |
| 55 |
|
| 56 |
Assumed contracts use the same implementation as C++23 [[assume]]. |
| 57 |
|
| 58 |
Parsing of pre and post contract conditions need to be deferred when the |
| 59 |
contracts are attached to a member function. The postcondition identifier |
| 60 |
cannot be used before the deduced return type of an auto function is used, |
| 61 |
except when used in a defining declaration in which case they conditions are |
| 62 |
fully parsed once the body is finished (see cpp2a/contracts-deduced{1,2}.C). |
| 63 |
|
| 64 |
A list of pre and post contracts can either be repeated in their entirety or |
| 65 |
completely absent in subsequent declarations. If contract lists appear on two |
| 66 |
matching declarations, their contracts have to be equivalent. In general this |
| 67 |
means that anything before the colon have to be token equivalent and the |
| 68 |
condition must be cp_tree_equal (primarily to allow for parameter renaming). |
| 69 |
|
| 70 |
Contracts on overrides must match those present on (all of) the overridee(s). |
| 71 |
|
| 72 |
Template specializations may have their own contracts. If no contracts are |
| 73 |
specified on the initial specialization they're assumed to be the same as |
| 74 |
the primary template. Specialization redeclarations must then match either |
| 75 |
the primary template (if they were unspecified originally), or those |
| 76 |
specified on the specialization. |
| 77 |
|
| 78 |
|
| 79 |
For non-cdtors two functions are generated for ease of implementation and to |
| 80 |
avoid some cases where code bloat may occurr. These are the DECL_PRE_FN and |
| 81 |
DECL_POST_FN. Each handles checking either the set of pre or post contracts |
| 82 |
of a guarded function. |
| 83 |
|
| 84 |
int fun(int v) |
| 85 |
[[ pre: v > 0 ]] |
| 86 |
[[ post r: r < 0 ]] |
| 87 |
{ |
| 88 |
return -v; |
| 89 |
} |
| 90 |
|
| 91 |
We implement the checking as follows: |
| 92 |
|
| 93 |
For functions with no post-conditions we wrap the original function body as |
| 94 |
follows: |
| 95 |
|
| 96 |
{ |
| 97 |
handle_pre_condition_checking (); |
| 98 |
original_function_body (); |
| 99 |
} |
| 100 |
|
| 101 |
This implements the intent that the preconditions are processed after the |
| 102 |
function parameters are initialised but before any other actions. |
| 103 |
|
| 104 |
For functions with post-conditions: |
| 105 |
|
| 106 |
if (preconditions_exist) |
| 107 |
handle_pre_condition_checking (); |
| 108 |
try |
| 109 |
{ |
| 110 |
original_function_body (); |
| 111 |
} |
| 112 |
finally |
| 113 |
{ |
| 114 |
handle_post_condition_checking (); |
| 115 |
} |
| 116 |
else [only if the function is not marked noexcept(true) ] |
| 117 |
{ |
| 118 |
; |
| 119 |
} |
| 120 |
|
| 121 |
In this, post-conditions [that might apply to the return value etc.] are |
| 122 |
evaluated on every non-exceptional edge out of the function. |
| 123 |
|
| 124 |
FIXME outlining contract checks into separate functions was motivated |
| 125 |
partly by wanting to call the postcondition function at each return |
| 126 |
statement, which we no longer do; at this point outlining doesn't seem to |
| 127 |
have any advantage over emitting the contracts directly in the function |
| 128 |
body. |
| 129 |
|
| 130 |
More helpful for optimization might be to make the contracts a wrapper |
| 131 |
function that could be inlined into the caller, the callee, or both. */ |
| 132 |
|
| 133 |
#include "config.h" |
| 134 |
#include "system.h" |
| 135 |
#include "coretypes.h" |
| 136 |
#include "cp-tree.h" |
| 137 |
#include "stringpool.h" |
| 138 |
#include "diagnostic.h" |
| 139 |
#include "options.h" |
| 140 |
#include "contracts.h" |
| 141 |
#include "tree.h" |
| 142 |
#include "tree-inline.h" |
| 143 |
#include "attribs.h" |
| 144 |
#include "tree-iterator.h" |
| 145 |
#include "print-tree.h" |
| 146 |
#include "stor-layout.h" |
| 147 |
#include "intl.h" |
| 148 |
#include "cgraph.h" |
| 149 |
|
| 150 |
const int max_custom_roles = 32; |
| 151 |
static contract_role contract_build_roles[max_custom_roles] = { |
| 152 |
}; |
| 153 |
|
| 154 |
bool valid_configs[CCS_MAYBE + 1][CCS_MAYBE + 1] = { |
| 155 |
{ 0, 0, 0, 0, 0, }, |
| 156 |
{ 0, 1, 0, 0, 0, }, |
| 157 |
{ 0, 1, 1, 1, 1, }, |
| 158 |
{ 0, 1, 1, 1, 1, }, |
| 159 |
{ 0, 1, 0, 0, 1, }, |
| 160 |
}; |
| 161 |
|
| 162 |
void |
| 163 |
validate_contract_role (contract_role *role) |
| 164 |
{ |
| 165 |
gcc_assert (role); |
| 166 |
if (!unchecked_contract_p (role->axiom_semantic)) |
| 167 |
error ("axiom contract semantic must be %<assume%> or %<ignore%>"); |
| 168 |
|
| 169 |
if (!valid_configs[role->default_semantic][role->audit_semantic] ) |
| 170 |
warning (0, "the %<audit%> semantic should be at least as strong as " |
| 171 |
"the %<default%> semantic"); |
| 172 |
} |
| 173 |
|
| 174 |
contract_semantic |
| 175 |
lookup_concrete_semantic (const char *name) |
| 176 |
{ |
| 177 |
if (strcmp (name, "ignore") == 0) |
| 178 |
return CCS_IGNORE; |
| 179 |
if (strcmp (name, "assume") == 0) |
| 180 |
return CCS_ASSUME; |
| 181 |
if (strcmp (name, "check_never_continue") == 0 |
| 182 |
|| strcmp (name, "never") == 0 |
| 183 |
|| strcmp (name, "abort") == 0) |
| 184 |
return CCS_NEVER; |
| 185 |
if (strcmp (name, "check_maybe_continue") == 0 |
| 186 |
|| strcmp (name, "maybe") == 0) |
| 187 |
return CCS_MAYBE; |
| 188 |
error ("'%s' is not a valid explicit concrete semantic", name); |
| 189 |
return CCS_INVALID; |
| 190 |
} |
| 191 |
|
| 192 |
/* Compare role and name up to either the NUL terminator or the first |
| 193 |
occurrence of colon. */ |
| 194 |
|
| 195 |
static bool |
| 196 |
role_name_equal (const char *role, const char *name) |
| 197 |
{ |
| 198 |
size_t role_len = strcspn (role, ":"); |
| 199 |
size_t name_len = strcspn (name, ":"); |
| 200 |
if (role_len != name_len) |
| 201 |
return false; |
| 202 |
return strncmp (role, name, role_len) == 0; |
| 203 |
} |
| 204 |
|
| 205 |
static bool |
| 206 |
role_name_equal (contract_role *role, const char *name) |
| 207 |
{ |
| 208 |
if (role->name == NULL) |
| 209 |
return false; |
| 210 |
return role_name_equal (role->name, name); |
| 211 |
} |
| 212 |
|
| 213 |
contract_role * |
| 214 |
get_contract_role (const char *name) |
| 215 |
{ |
| 216 |
for (int i = 0; i < max_custom_roles; ++i) |
| 217 |
{ |
| 218 |
contract_role *potential = contract_build_roles + i; |
| 219 |
if (role_name_equal (potential, name)) |
| 220 |
return potential; |
| 221 |
} |
| 222 |
if (role_name_equal (name, "default") || role_name_equal (name, "review")) |
| 223 |
{ |
| 224 |
setup_default_contract_role (false); |
| 225 |
return get_contract_role (name); |
| 226 |
} |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
|
| 230 |
contract_role * |
| 231 |
add_contract_role (const char *name, |
| 232 |
contract_semantic des, |
| 233 |
contract_semantic aus, |
| 234 |
contract_semantic axs, |
| 235 |
bool update) |
| 236 |
{ |
| 237 |
for (int i = 0; i < max_custom_roles; ++i) |
| 238 |
{ |
| 239 |
contract_role *potential = contract_build_roles + i; |
| 240 |
if (potential->name != NULL |
| 241 |
&& !role_name_equal (potential, name)) |
| 242 |
continue; |
| 243 |
if (potential->name != NULL && !update) |
| 244 |
return potential; |
| 245 |
potential->name = name; |
| 246 |
potential->default_semantic = des; |
| 247 |
potential->audit_semantic = aus; |
| 248 |
potential->axiom_semantic = axs; |
| 249 |
return potential; |
| 250 |
} |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
enum contract_build_level { OFF, DEFAULT, AUDIT }; |
| 255 |
static bool flag_contract_continuation_mode = false; |
| 256 |
static bool flag_contract_assumption_mode = true; |
| 257 |
static int flag_contract_build_level = DEFAULT; |
| 258 |
|
| 259 |
static bool contracts_p1332_default = false, contracts_p1332_review = false, |
| 260 |
contracts_std = false, contracts_p1429 = false; |
| 261 |
|
| 262 |
static contract_semantic |
| 263 |
get_concrete_check () |
| 264 |
{ |
| 265 |
return flag_contract_continuation_mode ? CCS_MAYBE : CCS_NEVER; |
| 266 |
} |
| 267 |
|
| 268 |
static contract_semantic |
| 269 |
get_concrete_axiom_semantic () |
| 270 |
{ |
| 271 |
return flag_contract_assumption_mode ? CCS_ASSUME : CCS_IGNORE; |
| 272 |
} |
| 273 |
|
| 274 |
void |
| 275 |
setup_default_contract_role (bool update) |
| 276 |
{ |
| 277 |
contract_semantic check = get_concrete_check (); |
| 278 |
contract_semantic axiom = get_concrete_axiom_semantic (); |
| 279 |
switch (flag_contract_build_level) |
| 280 |
{ |
| 281 |
case OFF: |
| 282 |
add_contract_role ("default", CCS_IGNORE, CCS_IGNORE, axiom, update); |
| 283 |
add_contract_role ("review", CCS_IGNORE, CCS_IGNORE, CCS_IGNORE, update); |
| 284 |
break; |
| 285 |
case DEFAULT: |
| 286 |
add_contract_role ("default", check, CCS_IGNORE, axiom, update); |
| 287 |
add_contract_role ("review", check, CCS_IGNORE, CCS_IGNORE, update); |
| 288 |
break; |
| 289 |
case AUDIT: |
| 290 |
add_contract_role ("default", check, check, axiom, update); |
| 291 |
add_contract_role ("review", check, check, CCS_IGNORE, update); |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
contract_semantic |
| 297 |
map_contract_semantic (const char *ident) |
| 298 |
{ |
| 299 |
if (strcmp (ident, "ignore") == 0) |
| 300 |
return CCS_IGNORE; |
| 301 |
else if (strcmp (ident, "assume") == 0) |
| 302 |
return CCS_ASSUME; |
| 303 |
else if (strcmp (ident, "check_never_continue") == 0) |
| 304 |
return CCS_NEVER; |
| 305 |
else if (strcmp (ident, "check_maybe_continue") == 0) |
| 306 |
return CCS_MAYBE; |
| 307 |
return CCS_INVALID; |
| 308 |
} |
| 309 |
|
| 310 |
contract_level |
| 311 |
map_contract_level (const char *ident) |
| 312 |
{ |
| 313 |
if (strcmp (ident, "default") == 0) |
| 314 |
return CONTRACT_DEFAULT; |
| 315 |
else if (strcmp (ident, "audit") == 0) |
| 316 |
return CONTRACT_AUDIT; |
| 317 |
else if (strcmp (ident, "axiom") == 0) |
| 318 |
return CONTRACT_AXIOM; |
| 319 |
return CONTRACT_INVALID; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
void |
| 324 |
handle_OPT_fcontract_build_level_ (const char *arg) |
| 325 |
{ |
| 326 |
if (contracts_p1332_default || contracts_p1332_review || contracts_p1429) |
| 327 |
{ |
| 328 |
error ("%<-fcontract-build-level=%> cannot be mixed with p1332/p1429"); |
| 329 |
return; |
| 330 |
} |
| 331 |
else |
| 332 |
contracts_std = true; |
| 333 |
|
| 334 |
if (strcmp (arg, "off") == 0) |
| 335 |
flag_contract_build_level = OFF; |
| 336 |
else if (strcmp (arg, "default") == 0) |
| 337 |
flag_contract_build_level = DEFAULT; |
| 338 |
else if (strcmp (arg, "audit") == 0) |
| 339 |
flag_contract_build_level = AUDIT; |
| 340 |
else |
| 341 |
error ("%<-fcontract-build-level=%> must be off|default|audit"); |
| 342 |
|
| 343 |
setup_default_contract_role (); |
| 344 |
} |
| 345 |
|
| 346 |
void |
| 347 |
handle_OPT_fcontract_assumption_mode_ (const char *arg) |
| 348 |
{ |
| 349 |
if (contracts_p1332_default || contracts_p1332_review || contracts_p1429) |
| 350 |
{ |
| 351 |
error ("%<-fcontract-assumption-mode=%> cannot be mixed with p1332/p1429"); |
| 352 |
return; |
| 353 |
} |
| 354 |
else |
| 355 |
contracts_std = true; |
| 356 |
|
| 357 |
if (strcmp (arg, "on") == 0) |
| 358 |
flag_contract_assumption_mode = true; |
| 359 |
else if (strcmp (arg, "off") == 0) |
| 360 |
flag_contract_assumption_mode = false; |
| 361 |
else |
| 362 |
error ("%<-fcontract-assumption-mode=%> must be %<on%> or %<off%>"); |
| 363 |
|
| 364 |
setup_default_contract_role (); |
| 365 |
} |
| 366 |
|
| 367 |
void |
| 368 |
handle_OPT_fcontract_continuation_mode_ (const char *arg) |
| 369 |
{ |
| 370 |
if (contracts_p1332_default || contracts_p1332_review || contracts_p1429) |
| 371 |
{ |
| 372 |
error ("%<-fcontract-continuation-mode=%> cannot be mixed with p1332/p1429"); |
| 373 |
return; |
| 374 |
} |
| 375 |
else |
| 376 |
contracts_std = true; |
| 377 |
|
| 378 |
if (strcmp (arg, "on") == 0) |
| 379 |
flag_contract_continuation_mode = true; |
| 380 |
else if (strcmp (arg, "off") == 0) |
| 381 |
flag_contract_continuation_mode = false; |
| 382 |
else |
| 383 |
error ("%<-fcontract-continuation-mode=%> must be %<on%> or %<off%>"); |
| 384 |
|
| 385 |
setup_default_contract_role (); |
| 386 |
} |
| 387 |
|
| 388 |
void |
| 389 |
handle_OPT_fcontract_role_ (const char *arg) |
| 390 |
{ |
| 391 |
const char *name = arg; |
| 392 |
const char *vals = strchr (name, ':'); |
| 393 |
if (vals == NULL) |
| 394 |
{ |
| 395 |
error ("%<-fcontract-role=%> must be in the form role:semantics"); |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
contract_semantic dess = CCS_INVALID, auss = CCS_INVALID, axss = CCS_INVALID; |
| 400 |
char *des = NULL, *aus = NULL, *axs = NULL; |
| 401 |
des = xstrdup (vals + 1); |
| 402 |
|
| 403 |
aus = strchr (des, ','); |
| 404 |
if (aus == NULL) |
| 405 |
{ |
| 406 |
error ("%<-fcontract-role=%> semantics must include default,audit,axiom values"); |
| 407 |
goto validate; |
| 408 |
} |
| 409 |
*aus = '\0'; // null terminate des |
| 410 |
aus = aus + 1; // move past null |
| 411 |
|
| 412 |
axs = strchr (aus, ','); |
| 413 |
if (axs == NULL) |
| 414 |
{ |
| 415 |
error ("%<-fcontract-role=%> semantics must include default,audit,axiom values"); |
| 416 |
goto validate; |
| 417 |
} |
| 418 |
*axs = '\0'; // null terminate aus |
| 419 |
axs = axs + 1; // move past null |
| 420 |
|
| 421 |
dess = lookup_concrete_semantic (des); |
| 422 |
auss = lookup_concrete_semantic (aus); |
| 423 |
axss = lookup_concrete_semantic (axs); |
| 424 |
validate: |
| 425 |
free (des); |
| 426 |
if (dess == CCS_INVALID || auss == CCS_INVALID || axss == CCS_INVALID) |
| 427 |
return; |
| 428 |
|
| 429 |
bool is_defalult_role = role_name_equal (name, "default"); |
| 430 |
bool is_review_role = role_name_equal (name, "review"); |
| 431 |
bool is_std_role = is_defalult_role || is_review_role; |
| 432 |
if ((contracts_std && is_std_role) || (contracts_p1429 && is_defalult_role)) |
| 433 |
{ |
| 434 |
error ("%<-fcontract-role=%> cannot be mixed with std/p1429 contract flags"); |
| 435 |
return; |
| 436 |
} |
| 437 |
else if (is_std_role) |
| 438 |
{ |
| 439 |
contracts_p1332_default |= is_defalult_role; |
| 440 |
contracts_p1332_review |= is_review_role; |
| 441 |
} |
| 442 |
|
| 443 |
contract_role *role = add_contract_role (name, dess, auss, axss); |
| 444 |
|
| 445 |
if (role == NULL) |
| 446 |
{ |
| 447 |
// TODO: not enough space? |
| 448 |
error ("%<-fcontract-level=%> too many custom roles"); |
| 449 |
return; |
| 450 |
} |
| 451 |
else |
| 452 |
validate_contract_role (role); |
| 453 |
} |
| 454 |
|
| 455 |
void |
| 456 |
handle_OPT_fcontract_semantic_ (const char *arg) |
| 457 |
{ |
| 458 |
if (!strchr (arg, ':')) |
| 459 |
{ |
| 460 |
error ("%<-fcontract-semantic=%> must be in the form level:semantic"); |
| 461 |
return; |
| 462 |
} |
| 463 |
|
| 464 |
if (contracts_std || contracts_p1332_default) |
| 465 |
{ |
| 466 |
error ("%<-fcontract-semantic=%> cannot be mixed with std/p1332 contract flags"); |
| 467 |
return; |
| 468 |
} |
| 469 |
contracts_p1429 = true; |
| 470 |
|
| 471 |
contract_role *role = get_contract_role ("default"); |
| 472 |
if (!role) |
| 473 |
{ |
| 474 |
error ("%<-fcontract-semantic=%> cannot find default role"); |
| 475 |
return; |
| 476 |
} |
| 477 |
|
| 478 |
const char *semantic = strchr (arg, ':') + 1; |
| 479 |
contract_semantic sem = lookup_concrete_semantic (semantic); |
| 480 |
if (sem == CCS_INVALID) |
| 481 |
return; |
| 482 |
|
| 483 |
if (strncmp ("default:", arg, 8) == 0) |
| 484 |
role->default_semantic = sem; |
| 485 |
else if (strncmp ("audit:", arg, 6) == 0) |
| 486 |
role->audit_semantic = sem; |
| 487 |
else if (strncmp ("axiom:", arg, 6) == 0) |
| 488 |
role->axiom_semantic = sem; |
| 489 |
else |
| 490 |
error ("%<-fcontract-semantic=%> level must be default, audit, or axiom"); |
| 491 |
validate_contract_role (role); |
| 492 |
} |
| 493 |
|
| 494 |
/* Convert a contract CONFIG into a contract_mode. */ |
| 495 |
|
| 496 |
static contract_mode |
| 497 |
contract_config_to_mode (tree config) |
| 498 |
{ |
| 499 |
if (config == NULL_TREE) |
| 500 |
return contract_mode (CONTRACT_DEFAULT, get_default_contract_role ()); |
| 501 |
|
| 502 |
/* TREE_LIST has TREE_VALUE is a level and TREE_PURPOSE is role. */ |
| 503 |
if (TREE_CODE (config) == TREE_LIST) |
| 504 |
{ |
| 505 |
contract_role *role = NULL; |
| 506 |
if (TREE_PURPOSE (config)) |
| 507 |
role = get_contract_role (IDENTIFIER_POINTER (TREE_PURPOSE (config))); |
| 508 |
if (!role) |
| 509 |
role = get_default_contract_role (); |
| 510 |
|
| 511 |
contract_level level = |
| 512 |
map_contract_level (IDENTIFIER_POINTER (TREE_VALUE (config))); |
| 513 |
return contract_mode (level, role); |
| 514 |
} |
| 515 |
|
| 516 |
/* Literal semantic. */ |
| 517 |
gcc_assert (TREE_CODE (config) == IDENTIFIER_NODE); |
| 518 |
contract_semantic semantic = |
| 519 |
map_contract_semantic (IDENTIFIER_POINTER (config)); |
| 520 |
return contract_mode (semantic); |
| 521 |
} |
| 522 |
|
| 523 |
/* Convert a contract's config into a concrete semantic using the current |
| 524 |
contract semantic mapping. */ |
| 525 |
|
| 526 |
static contract_semantic |
| 527 |
compute_concrete_semantic (tree contract) |
| 528 |
{ |
| 529 |
contract_mode mode = contract_config_to_mode (CONTRACT_MODE (contract)); |
| 530 |
/* Compute the concrete semantic for the contract. */ |
| 531 |
if (!flag_contract_mode) |
| 532 |
/* If contracts are off, treat all contracts as ignore. */ |
| 533 |
return CCS_IGNORE; |
| 534 |
else if (mode.kind == contract_mode::cm_invalid) |
| 535 |
return CCS_INVALID; |
| 536 |
else if (mode.kind == contract_mode::cm_explicit) |
| 537 |
return mode.get_semantic (); |
| 538 |
else |
| 539 |
{ |
| 540 |
gcc_assert (mode.get_role ()); |
| 541 |
gcc_assert (mode.get_level () != CONTRACT_INVALID); |
| 542 |
contract_level level = mode.get_level (); |
| 543 |
contract_role *role = mode.get_role (); |
| 544 |
if (level == CONTRACT_DEFAULT) |
| 545 |
return role->default_semantic; |
| 546 |
else if (level == CONTRACT_AUDIT) |
| 547 |
return role->audit_semantic; |
| 548 |
else if (level == CONTRACT_AXIOM) |
| 549 |
return role->axiom_semantic; |
| 550 |
} |
| 551 |
gcc_assert (false); |
| 552 |
} |
| 553 |
|
| 554 |
/* Return true if any contract in CONTRACT_ATTRs is not yet parsed. */ |
| 555 |
|
| 556 |
bool |
| 557 |
contract_any_deferred_p (tree contract_attr) |
| 558 |
{ |
| 559 |
for (; contract_attr; contract_attr = CONTRACT_CHAIN (contract_attr)) |
| 560 |
if (CONTRACT_CONDITION_DEFERRED_P (CONTRACT_STATEMENT (contract_attr))) |
| 561 |
return true; |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
/* Returns true if all attributes are contracts. */ |
| 566 |
|
| 567 |
bool |
| 568 |
all_attributes_are_contracts_p (tree attributes) |
| 569 |
{ |
| 570 |
for (; attributes; attributes = TREE_CHAIN (attributes)) |
| 571 |
if (!cxx_contract_attribute_p (attributes)) |
| 572 |
return false; |
| 573 |
return true; |
| 574 |
} |
| 575 |
|
| 576 |
/* Mark most of a contract as being invalid. */ |
| 577 |
|
| 578 |
tree |
| 579 |
invalidate_contract (tree t) |
| 580 |
{ |
| 581 |
if (TREE_CODE (t) == POSTCONDITION_STMT && POSTCONDITION_IDENTIFIER (t)) |
| 582 |
POSTCONDITION_IDENTIFIER (t) = error_mark_node; |
| 583 |
CONTRACT_CONDITION (t) = error_mark_node; |
| 584 |
CONTRACT_COMMENT (t) = error_mark_node; |
| 585 |
return t; |
| 586 |
} |
| 587 |
|
| 588 |
/* Returns an invented parameter declration of the form 'TYPE ID' for the |
| 589 |
purpose of parsing the postcondition. |
| 590 |
|
| 591 |
We use a PARM_DECL instead of a VAR_DECL so that tsubst forces a lookup |
| 592 |
in local specializations when we instantiate these things later. */ |
| 593 |
|
| 594 |
tree |
| 595 |
make_postcondition_variable (cp_expr id, tree type) |
| 596 |
{ |
| 597 |
if (id == error_mark_node) |
| 598 |
return id; |
| 599 |
|
| 600 |
tree decl = build_lang_decl (PARM_DECL, id, type); |
| 601 |
DECL_ARTIFICIAL (decl) = true; |
| 602 |
DECL_SOURCE_LOCATION (decl) = id.get_location (); |
| 603 |
|
| 604 |
pushdecl (decl); |
| 605 |
return decl; |
| 606 |
} |
| 607 |
|
| 608 |
/* As above, except that the type is unknown. */ |
| 609 |
|
| 610 |
tree |
| 611 |
make_postcondition_variable (cp_expr id) |
| 612 |
{ |
| 613 |
return make_postcondition_variable (id, make_auto ()); |
| 614 |
} |
| 615 |
|
| 616 |
/* Check that the TYPE is valid for a named postcondition variable. Emit a |
| 617 |
diagnostic if it is not. Returns TRUE if the result is OK and false |
| 618 |
otherwise. */ |
| 619 |
|
| 620 |
bool |
| 621 |
check_postcondition_result (tree decl, tree type, location_t loc) |
| 622 |
{ |
| 623 |
/* Do not be confused by targetm.cxx.cdtor_return_this (); |
| 624 |
conceptually, cdtors have no return value. */ |
| 625 |
if (VOID_TYPE_P (type) |
| 626 |
|| DECL_CONSTRUCTOR_P (decl) |
| 627 |
|| DECL_DESTRUCTOR_P (decl)) |
| 628 |
{ |
| 629 |
error_at (loc, |
| 630 |
DECL_CONSTRUCTOR_P (decl) |
| 631 |
? G_("constructor does not return a value to test") |
| 632 |
: DECL_DESTRUCTOR_P (decl) |
| 633 |
? G_("destructor does not return a value to test") |
| 634 |
: G_("function does not return a value to test")); |
| 635 |
return false; |
| 636 |
} |
| 637 |
|
| 638 |
return true; |
| 639 |
} |
| 640 |
|
| 641 |
/* Instantiate each postcondition with the return type to finalize the |
| 642 |
attribute. */ |
| 643 |
|
| 644 |
void |
| 645 |
rebuild_postconditions (tree decl) |
| 646 |
{ |
| 647 |
tree type = TREE_TYPE (TREE_TYPE (decl)); |
| 648 |
tree attributes = DECL_CONTRACTS (decl); |
| 649 |
|
| 650 |
for (; attributes ; attributes = TREE_CHAIN (attributes)) |
| 651 |
{ |
| 652 |
if (!cxx_contract_attribute_p (attributes)) |
| 653 |
continue; |
| 654 |
tree contract = TREE_VALUE (TREE_VALUE (attributes)); |
| 655 |
if (TREE_CODE (contract) != POSTCONDITION_STMT) |
| 656 |
continue; |
| 657 |
tree condition = CONTRACT_CONDITION (contract); |
| 658 |
|
| 659 |
/* If any conditions are deferred, they're all deferred. Note that |
| 660 |
we don't have to instantiate postconditions in that case because |
| 661 |
the type is available through the declaration. */ |
| 662 |
if (TREE_CODE (condition) == DEFERRED_PARSE) |
| 663 |
return; |
| 664 |
|
| 665 |
tree oldvar = POSTCONDITION_IDENTIFIER (contract); |
| 666 |
if (!oldvar) |
| 667 |
continue; |
| 668 |
|
| 669 |
/* Always update the context of the result variable so that it can |
| 670 |
be remapped by remap_contracts. */ |
| 671 |
DECL_CONTEXT (oldvar) = decl; |
| 672 |
|
| 673 |
/* If the return type is undeduced, defer until later. */ |
| 674 |
if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
| 675 |
return; |
| 676 |
|
| 677 |
/* Check the postcondition variable. */ |
| 678 |
location_t loc = DECL_SOURCE_LOCATION (oldvar); |
| 679 |
if (!check_postcondition_result (decl, type, loc)) |
| 680 |
{ |
| 681 |
invalidate_contract (contract); |
| 682 |
continue; |
| 683 |
} |
| 684 |
|
| 685 |
/* "Instantiate" the result variable using the known type. Also update |
| 686 |
the context so the inliner will actually remap this the parameter when |
| 687 |
generating contract checks. */ |
| 688 |
tree newvar = copy_node (oldvar); |
| 689 |
TREE_TYPE (newvar) = type; |
| 690 |
|
| 691 |
/* Make parameters and result available for substitution. */ |
| 692 |
local_specialization_stack stack (lss_copy); |
| 693 |
for (tree t = DECL_ARGUMENTS (decl); t != NULL_TREE; t = TREE_CHAIN (t)) |
| 694 |
register_local_identity (t); |
| 695 |
register_local_specialization (newvar, oldvar); |
| 696 |
|
| 697 |
++processing_contract_condition; |
| 698 |
condition = tsubst_expr (condition, make_tree_vec (0), |
| 699 |
tf_warning_or_error, decl); |
| 700 |
--processing_contract_condition; |
| 701 |
|
| 702 |
/* Update the contract condition and result. */ |
| 703 |
POSTCONDITION_IDENTIFIER (contract) = newvar; |
| 704 |
CONTRACT_CONDITION (contract) = finish_contract_condition (condition); |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
static tree |
| 709 |
build_comment (cp_expr condition) |
| 710 |
{ |
| 711 |
/* Try to get the actual source text for the condition; if that fails pretty |
| 712 |
print the resulting tree. */ |
| 713 |
char *str = get_source_text_between (global_dc->get_file_cache (), |
| 714 |
condition.get_start (), |
| 715 |
condition.get_finish ()); |
| 716 |
if (!str) |
| 717 |
{ |
| 718 |
/* FIXME cases where we end up here |
| 719 |
#line macro usage (oof) |
| 720 |
contracts10.C |
| 721 |
contracts11.C */ |
| 722 |
const char *str = expr_to_string (condition); |
| 723 |
return build_string_literal (strlen (str) + 1, str); |
| 724 |
} |
| 725 |
|
| 726 |
tree t = build_string_literal (strlen (str) + 1, str); |
| 727 |
free (str); |
| 728 |
return t; |
| 729 |
} |
| 730 |
|
| 731 |
/* Build a contract statement. */ |
| 732 |
|
| 733 |
tree |
| 734 |
grok_contract (tree attribute, tree mode, tree result, cp_expr condition, |
| 735 |
location_t loc) |
| 736 |
{ |
| 737 |
if (condition == error_mark_node) |
| 738 |
return error_mark_node; |
| 739 |
|
| 740 |
tree_code code; |
| 741 |
if (is_attribute_p ("assert", attribute)) |
| 742 |
code = ASSERTION_STMT; |
| 743 |
else if (is_attribute_p ("pre", attribute)) |
| 744 |
code = PRECONDITION_STMT; |
| 745 |
else if (is_attribute_p ("post", attribute)) |
| 746 |
code = POSTCONDITION_STMT; |
| 747 |
else |
| 748 |
gcc_unreachable (); |
| 749 |
|
| 750 |
/* Build the contract. The condition is added later. In the case that |
| 751 |
the contract is deferred, result an plain identifier, not a result |
| 752 |
variable. */ |
| 753 |
tree contract; |
| 754 |
tree type = void_type_node; |
| 755 |
if (code != POSTCONDITION_STMT) |
| 756 |
contract = build3_loc (loc, code, type, mode, NULL_TREE, NULL_TREE); |
| 757 |
else |
| 758 |
contract = build4_loc (loc, code, type, mode, NULL_TREE, NULL_TREE, result); |
| 759 |
|
| 760 |
/* Determine the concrete semantic. */ |
| 761 |
set_contract_semantic (contract, compute_concrete_semantic (contract)); |
| 762 |
|
| 763 |
/* If the contract is deferred, don't do anything with the condition. */ |
| 764 |
if (TREE_CODE (condition) == DEFERRED_PARSE) |
| 765 |
{ |
| 766 |
CONTRACT_CONDITION (contract) = condition; |
| 767 |
return contract; |
| 768 |
} |
| 769 |
|
| 770 |
/* Generate the comment from the original condition. */ |
| 771 |
CONTRACT_COMMENT (contract) = build_comment (condition); |
| 772 |
|
| 773 |
/* The condition is converted to bool. */ |
| 774 |
condition = finish_contract_condition (condition); |
| 775 |
|
| 776 |
if (condition == error_mark_node) |
| 777 |
return error_mark_node; |
| 778 |
|
| 779 |
CONTRACT_CONDITION (contract) = condition; |
| 780 |
|
| 781 |
return contract; |
| 782 |
} |
| 783 |
|
| 784 |
/* Build the contract attribute specifier where IDENTIFIER is one of 'pre', |
| 785 |
'post' or 'assert' and CONTRACT is the underlying statement. */ |
| 786 |
tree |
| 787 |
finish_contract_attribute (tree identifier, tree contract) |
| 788 |
{ |
| 789 |
if (contract == error_mark_node) |
| 790 |
return error_mark_node; |
| 791 |
|
| 792 |
tree attribute = build_tree_list (build_tree_list (NULL_TREE, identifier), |
| 793 |
build_tree_list (NULL_TREE, contract)); |
| 794 |
|
| 795 |
/* Mark the attribute as dependent if the condition is dependent. |
| 796 |
|
| 797 |
TODO: I'm not sure this is strictly necessary. It's going to be marked as |
| 798 |
such by a subroutine of cplus_decl_attributes. */ |
| 799 |
tree condition = CONTRACT_CONDITION (contract); |
| 800 |
if (TREE_CODE (condition) == DEFERRED_PARSE |
| 801 |
|| value_dependent_expression_p (condition)) |
| 802 |
ATTR_IS_DEPENDENT (attribute) = true; |
| 803 |
|
| 804 |
return attribute; |
| 805 |
} |
| 806 |
|
| 807 |
/* Update condition of a late-parsed contract and postcondition variable, |
| 808 |
if any. */ |
| 809 |
|
| 810 |
void |
| 811 |
update_late_contract (tree contract, tree result, tree condition) |
| 812 |
{ |
| 813 |
if (TREE_CODE (contract) == POSTCONDITION_STMT) |
| 814 |
POSTCONDITION_IDENTIFIER (contract) = result; |
| 815 |
|
| 816 |
/* Generate the comment from the original condition. */ |
| 817 |
CONTRACT_COMMENT (contract) = build_comment (condition); |
| 818 |
|
| 819 |
/* The condition is converted to bool. */ |
| 820 |
condition = finish_contract_condition (condition); |
| 821 |
CONTRACT_CONDITION (contract) = condition; |
| 822 |
} |
| 823 |
|
| 824 |
/* Return TRUE iff ATTR has been parsed by the front-end as a c++2a contract |
| 825 |
attribute. */ |
| 826 |
|
| 827 |
bool |
| 828 |
cxx_contract_attribute_p (const_tree attr) |
| 829 |
{ |
| 830 |
if (attr == NULL_TREE |
| 831 |
|| TREE_CODE (attr) != TREE_LIST) |
| 832 |
return false; |
| 833 |
|
| 834 |
if (!TREE_PURPOSE (attr) || TREE_CODE (TREE_PURPOSE (attr)) != TREE_LIST) |
| 835 |
return false; |
| 836 |
if (!TREE_VALUE (attr) || TREE_CODE (TREE_VALUE (attr)) != TREE_LIST) |
| 837 |
return false; |
| 838 |
if (!TREE_VALUE (TREE_VALUE (attr))) |
| 839 |
return false; |
| 840 |
|
| 841 |
return (TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == PRECONDITION_STMT |
| 842 |
|| TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == POSTCONDITION_STMT |
| 843 |
|| TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == ASSERTION_STMT); |
| 844 |
} |
| 845 |
|
| 846 |
/* True if ATTR is an assertion. */ |
| 847 |
|
| 848 |
bool |
| 849 |
cp_contract_assertion_p (const_tree attr) |
| 850 |
{ |
| 851 |
/* This is only an assertion if it is a valid cxx contract attribute and the |
| 852 |
statement is an ASSERTION_STMT. */ |
| 853 |
return cxx_contract_attribute_p (attr) |
| 854 |
&& TREE_CODE (CONTRACT_STATEMENT (attr)) == ASSERTION_STMT; |
| 855 |
} |
| 856 |
|
| 857 |
/* Remove all c++2a style contract attributes from the DECL_ATTRIBUTEs of the |
| 858 |
FUNCTION_DECL FNDECL. */ |
| 859 |
|
| 860 |
void |
| 861 |
remove_contract_attributes (tree fndecl) |
| 862 |
{ |
| 863 |
tree list = NULL_TREE; |
| 864 |
for (tree p = DECL_ATTRIBUTES (fndecl); p; p = TREE_CHAIN (p)) |
| 865 |
if (!cxx_contract_attribute_p (p)) |
| 866 |
{ |
| 867 |
tree nl = copy_node (p); |
| 868 |
TREE_CHAIN (nl) = list; |
| 869 |
list = nl; |
| 870 |
} |
| 871 |
DECL_ATTRIBUTES (fndecl) = nreverse (list); |
| 872 |
} |
| 873 |
|
| 874 |
static tree find_first_non_contract (tree attributes) |
| 875 |
{ |
| 876 |
tree head = attributes; |
| 877 |
tree p = find_contract (attributes); |
| 878 |
|
| 879 |
/* There are no contracts. */ |
| 880 |
if (!p) |
| 881 |
return head; |
| 882 |
|
| 883 |
/* There are leading contracts. */ |
| 884 |
if (p == head) |
| 885 |
{ |
| 886 |
while (cxx_contract_attribute_p (p)) |
| 887 |
p = TREE_CHAIN (p); |
| 888 |
head = p; |
| 889 |
} |
| 890 |
|
| 891 |
return head; |
| 892 |
} |
| 893 |
|
| 894 |
/* Remove contracts from ATTRIBUTES. */ |
| 895 |
|
| 896 |
tree splice_out_contracts (tree attributes) |
| 897 |
{ |
| 898 |
tree head = find_first_non_contract (attributes); |
| 899 |
if (!head) |
| 900 |
return NULL_TREE; |
| 901 |
|
| 902 |
/* Splice out remaining contracts. */ |
| 903 |
tree p = TREE_CHAIN (head); |
| 904 |
tree q = head; |
| 905 |
while (p) |
| 906 |
{ |
| 907 |
if (cxx_contract_attribute_p (p)) |
| 908 |
{ |
| 909 |
/* Skip a sequence of contracts and then link q to the next |
| 910 |
non-contract attribute. */ |
| 911 |
do |
| 912 |
p = TREE_CHAIN (p); |
| 913 |
while (cxx_contract_attribute_p (p)); |
| 914 |
TREE_CHAIN (q) = p; |
| 915 |
} |
| 916 |
else |
| 917 |
p = TREE_CHAIN (p); |
| 918 |
} |
| 919 |
|
| 920 |
return head; |
| 921 |
} |
| 922 |
|
| 923 |
/* Copy contract attributes from NEWDECL onto the attribute list of OLDDECL. */ |
| 924 |
|
| 925 |
void copy_contract_attributes (tree olddecl, tree newdecl) |
| 926 |
{ |
| 927 |
tree attrs = NULL_TREE; |
| 928 |
for (tree c = DECL_CONTRACTS (newdecl); c; c = TREE_CHAIN (c)) |
| 929 |
{ |
| 930 |
if (!cxx_contract_attribute_p (c)) |
| 931 |
continue; |
| 932 |
attrs = tree_cons (TREE_PURPOSE (c), TREE_VALUE (c), attrs); |
| 933 |
} |
| 934 |
attrs = chainon (DECL_ATTRIBUTES (olddecl), nreverse (attrs)); |
| 935 |
DECL_ATTRIBUTES (olddecl) = attrs; |
| 936 |
|
| 937 |
/* And update DECL_CONTEXT of the postcondition result identifier. */ |
| 938 |
rebuild_postconditions (olddecl); |
| 939 |
} |
| 940 |
|
| 941 |
/* Returns the parameter corresponding to the return value of a guarded |
| 942 |
function D. Returns NULL_TREE if D has no postconditions or is void. */ |
| 943 |
|
| 944 |
static tree |
| 945 |
get_postcondition_result_parameter (tree d) |
| 946 |
{ |
| 947 |
if (!d || d == error_mark_node) |
| 948 |
return NULL_TREE; |
| 949 |
|
| 950 |
if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (d)))) |
| 951 |
return NULL_TREE; |
| 952 |
|
| 953 |
tree post = DECL_POST_FN (d); |
| 954 |
if (!post || post == error_mark_node) |
| 955 |
return NULL_TREE; |
| 956 |
|
| 957 |
for (tree arg = DECL_ARGUMENTS (post); arg; arg = TREE_CHAIN (arg)) |
| 958 |
if (!TREE_CHAIN (arg)) |
| 959 |
return arg; |
| 960 |
|
| 961 |
return NULL_TREE; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
/* For use with the tree inliner. This preserves non-mapped local variables, |
| 966 |
such as postcondition result variables, during remapping. */ |
| 967 |
|
| 968 |
static tree |
| 969 |
retain_decl (tree decl, copy_body_data *) |
| 970 |
{ |
| 971 |
return decl; |
| 972 |
} |
| 973 |
|
| 974 |
/* Rewrite the condition of contract in place, so that references to SRC's |
| 975 |
parameters are updated to refer to DST's parameters. The postcondition |
| 976 |
result variable is left unchanged. |
| 977 |
|
| 978 |
This, along with remap_contracts, are subroutines of duplicate_decls. |
| 979 |
When declarations are merged, we sometimes need to update contracts to |
| 980 |
refer to new parameters. |
| 981 |
|
| 982 |
If DUPLICATE_P is true, this is called by duplicate_decls to rewrite contacts |
| 983 |
in terms of a new set of parameters. In this case, we can retain local |
| 984 |
variables appearing in the contract because the contract is not being |
| 985 |
prepared for insertion into a new function. Importantly, this preserves the |
| 986 |
references to postcondition results, which are not replaced during merging. |
| 987 |
|
| 988 |
If false, we're preparing to emit the contract condition into the body |
| 989 |
of a new function, so we need to make copies of all local variables |
| 990 |
appearing in the contract (e.g., if it includes a lambda expression). Note |
| 991 |
that in this case, postcondition results are mapped to the last parameter |
| 992 |
of DST. |
| 993 |
|
| 994 |
This is also used to reuse a parent type's contracts on virtual methods. */ |
| 995 |
|
| 996 |
static void |
| 997 |
remap_contract (tree src, tree dst, tree contract, bool duplicate_p) |
| 998 |
{ |
| 999 |
copy_body_data id; |
| 1000 |
hash_map<tree, tree> decl_map; |
| 1001 |
|
| 1002 |
memset (&id, 0, sizeof (id)); |
| 1003 |
id.src_fn = src; |
| 1004 |
id.dst_fn = dst; |
| 1005 |
id.src_cfun = DECL_STRUCT_FUNCTION (src); |
| 1006 |
id.decl_map = &decl_map; |
| 1007 |
|
| 1008 |
/* If we're merging contracts, don't copy local variables. */ |
| 1009 |
id.copy_decl = duplicate_p ? retain_decl : copy_decl_no_change; |
| 1010 |
|
| 1011 |
id.transform_call_graph_edges = CB_CGE_DUPLICATE; |
| 1012 |
id.transform_new_cfg = false; |
| 1013 |
id.transform_return_to_modify = false; |
| 1014 |
id.transform_parameter = true; |
| 1015 |
|
| 1016 |
/* Make sure not to unshare trees behind the front-end's back |
| 1017 |
since front-end specific mechanisms may rely on sharing. */ |
| 1018 |
id.regimplify = false; |
| 1019 |
id.do_not_unshare = true; |
| 1020 |
id.do_not_fold = true; |
| 1021 |
|
| 1022 |
/* We're not inside any EH region. */ |
| 1023 |
id.eh_lp_nr = 0; |
| 1024 |
|
| 1025 |
bool do_remap = false; |
| 1026 |
|
| 1027 |
/* Insert parameter remappings. */ |
| 1028 |
if (TREE_CODE (src) == FUNCTION_DECL) |
| 1029 |
src = DECL_ARGUMENTS (src); |
| 1030 |
if (TREE_CODE (dst) == FUNCTION_DECL) |
| 1031 |
dst = DECL_ARGUMENTS (dst); |
| 1032 |
|
| 1033 |
for (tree sp = src, dp = dst; |
| 1034 |
sp || dp; |
| 1035 |
sp = DECL_CHAIN (sp), dp = DECL_CHAIN (dp)) |
| 1036 |
{ |
| 1037 |
if (!sp && dp |
| 1038 |
&& TREE_CODE (contract) == POSTCONDITION_STMT |
| 1039 |
&& DECL_CHAIN (dp) == NULL_TREE) |
| 1040 |
{ |
| 1041 |
gcc_assert (!duplicate_p); |
| 1042 |
if (tree result = POSTCONDITION_IDENTIFIER (contract)) |
| 1043 |
{ |
| 1044 |
gcc_assert (DECL_P (result)); |
| 1045 |
insert_decl_map (&id, result, dp); |
| 1046 |
do_remap = true; |
| 1047 |
} |
| 1048 |
break; |
| 1049 |
} |
| 1050 |
gcc_assert (sp && dp); |
| 1051 |
|
| 1052 |
if (sp == dp) |
| 1053 |
continue; |
| 1054 |
|
| 1055 |
insert_decl_map (&id, sp, dp); |
| 1056 |
do_remap = true; |
| 1057 |
} |
| 1058 |
if (!do_remap) |
| 1059 |
return; |
| 1060 |
|
| 1061 |
walk_tree (&CONTRACT_CONDITION (contract), copy_tree_body_r, &id, NULL); |
| 1062 |
} |
| 1063 |
|
| 1064 |
/* Rewrite any references to SRC's PARM_DECLs to the corresponding PARM_DECL in |
| 1065 |
DST in all of the contract attributes in CONTRACTS by calling remap_contract |
| 1066 |
on each. |
| 1067 |
|
| 1068 |
This is used for two purposes: to rewrite contract attributes during |
| 1069 |
duplicate_decls, and to prepare contracts for emission into a function's |
| 1070 |
respective precondition and postcondition functions. DUPLICATE_P is used |
| 1071 |
to determine the context in which this function is called. See above for |
| 1072 |
the behavior described by this flag. */ |
| 1073 |
|
| 1074 |
void |
| 1075 |
remap_contracts (tree src, tree dst, tree contracts, bool duplicate_p) |
| 1076 |
{ |
| 1077 |
for (tree attr = contracts; attr; attr = CONTRACT_CHAIN (attr)) |
| 1078 |
{ |
| 1079 |
if (!cxx_contract_attribute_p (attr)) |
| 1080 |
continue; |
| 1081 |
tree contract = CONTRACT_STATEMENT (attr); |
| 1082 |
if (TREE_CODE (CONTRACT_CONDITION (contract)) != DEFERRED_PARSE) |
| 1083 |
remap_contract (src, dst, contract, duplicate_p); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* Helper to replace references to dummy this parameters with references to |
| 1088 |
the first argument of the FUNCTION_DECL DATA. */ |
| 1089 |
|
| 1090 |
static tree |
| 1091 |
remap_dummy_this_1 (tree *tp, int *, void *data) |
| 1092 |
{ |
| 1093 |
if (!is_this_parameter (*tp)) |
| 1094 |
return NULL_TREE; |
| 1095 |
tree fn = (tree)data; |
| 1096 |
*tp = DECL_ARGUMENTS (fn); |
| 1097 |
return NULL_TREE; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* Replace all references to dummy this parameters in EXPR with references to |
| 1101 |
the first argument of the FUNCTION_DECL FN. */ |
| 1102 |
|
| 1103 |
static void |
| 1104 |
remap_dummy_this (tree fn, tree *expr) |
| 1105 |
{ |
| 1106 |
walk_tree (expr, remap_dummy_this_1, fn, NULL); |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* Contract matching. */ |
| 1110 |
|
| 1111 |
/* True if the contract is valid. */ |
| 1112 |
|
| 1113 |
static bool |
| 1114 |
contract_valid_p (tree contract) |
| 1115 |
{ |
| 1116 |
return CONTRACT_CONDITION (contract) != error_mark_node; |
| 1117 |
} |
| 1118 |
|
| 1119 |
/* True if the contract attribute is valid. */ |
| 1120 |
|
| 1121 |
static bool |
| 1122 |
contract_attribute_valid_p (tree attribute) |
| 1123 |
{ |
| 1124 |
return contract_valid_p (TREE_VALUE (TREE_VALUE (attribute))); |
| 1125 |
} |
| 1126 |
|
| 1127 |
/* Compare the contract conditions of OLD_ATTR and NEW_ATTR. Returns false |
| 1128 |
if the conditions are equivalent, and true otherwise. */ |
| 1129 |
|
| 1130 |
static bool |
| 1131 |
check_for_mismatched_contracts (tree old_attr, tree new_attr, |
| 1132 |
contract_matching_context ctx) |
| 1133 |
{ |
| 1134 |
tree old_contract = CONTRACT_STATEMENT (old_attr); |
| 1135 |
tree new_contract = CONTRACT_STATEMENT (new_attr); |
| 1136 |
|
| 1137 |
/* Different kinds of contracts do not match. */ |
| 1138 |
if (TREE_CODE (old_contract) != TREE_CODE (new_contract)) |
| 1139 |
{ |
| 1140 |
auto_diagnostic_group d; |
| 1141 |
error_at (EXPR_LOCATION (new_contract), |
| 1142 |
ctx == cmc_declaration |
| 1143 |
? "mismatched contract attribute in declaration" |
| 1144 |
: "mismatched contract attribute in override"); |
| 1145 |
inform (EXPR_LOCATION (old_contract), "previous contract here"); |
| 1146 |
return true; |
| 1147 |
} |
| 1148 |
|
| 1149 |
/* A deferred contract tentatively matches. */ |
| 1150 |
if (CONTRACT_CONDITION_DEFERRED_P (new_contract)) |
| 1151 |
return false; |
| 1152 |
|
| 1153 |
/* Compare the conditions of the contracts. We fold immediately to avoid |
| 1154 |
issues comparing contracts on overrides that use parameters -- see |
| 1155 |
contracts-pre3. */ |
| 1156 |
tree t1 = cp_fully_fold_init (CONTRACT_CONDITION (old_contract)); |
| 1157 |
tree t2 = cp_fully_fold_init (CONTRACT_CONDITION (new_contract)); |
| 1158 |
|
| 1159 |
/* Compare the contracts. The fold doesn't eliminate conversions to members. |
| 1160 |
Set the comparing_override_contracts flag to ensure that references |
| 1161 |
through 'this' are equal if they designate the same member, regardless of |
| 1162 |
the path those members. */ |
| 1163 |
bool saved_comparing_contracts = comparing_override_contracts; |
| 1164 |
comparing_override_contracts = (ctx == cmc_override); |
| 1165 |
bool matching_p = cp_tree_equal (t1, t2); |
| 1166 |
comparing_override_contracts = saved_comparing_contracts; |
| 1167 |
|
| 1168 |
if (!matching_p) |
| 1169 |
{ |
| 1170 |
auto_diagnostic_group d; |
| 1171 |
error_at (EXPR_LOCATION (CONTRACT_CONDITION (new_contract)), |
| 1172 |
ctx == cmc_declaration |
| 1173 |
? "mismatched contract condition in declaration" |
| 1174 |
: "mismatched contract condition in override"); |
| 1175 |
inform (EXPR_LOCATION (CONTRACT_CONDITION (old_contract)), |
| 1176 |
"previous contract here"); |
| 1177 |
return true; |
| 1178 |
} |
| 1179 |
|
| 1180 |
return false; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* Compare the contract attributes of OLDDECL and NEWDECL. Returns true |
| 1184 |
if the contracts match, and false if they differ. */ |
| 1185 |
|
| 1186 |
bool |
| 1187 |
match_contract_conditions (location_t oldloc, tree old_attrs, |
| 1188 |
location_t newloc, tree new_attrs, |
| 1189 |
contract_matching_context ctx) |
| 1190 |
{ |
| 1191 |
/* Contracts only match if they are both specified. */ |
| 1192 |
if (!old_attrs || !new_attrs) |
| 1193 |
return true; |
| 1194 |
|
| 1195 |
/* Compare each contract in turn. */ |
| 1196 |
while (old_attrs && new_attrs) |
| 1197 |
{ |
| 1198 |
/* If either contract is ill-formed, skip the rest of the comparison, |
| 1199 |
since we've already diagnosed an error. */ |
| 1200 |
if (!contract_attribute_valid_p (new_attrs) |
| 1201 |
|| !contract_attribute_valid_p (old_attrs)) |
| 1202 |
return false; |
| 1203 |
|
| 1204 |
if (check_for_mismatched_contracts (old_attrs, new_attrs, ctx)) |
| 1205 |
return false; |
| 1206 |
old_attrs = CONTRACT_CHAIN (old_attrs); |
| 1207 |
new_attrs = CONTRACT_CHAIN (new_attrs); |
| 1208 |
} |
| 1209 |
|
| 1210 |
/* If we didn't compare all attributes, the contracts don't match. */ |
| 1211 |
if (old_attrs || new_attrs) |
| 1212 |
{ |
| 1213 |
auto_diagnostic_group d; |
| 1214 |
error_at (newloc, |
| 1215 |
ctx == cmc_declaration |
| 1216 |
? "declaration has a different number of contracts than " |
| 1217 |
"previously declared" |
| 1218 |
: "override has a different number of contracts than " |
| 1219 |
"previously declared"); |
| 1220 |
inform (oldloc, |
| 1221 |
new_attrs |
| 1222 |
? "original declaration with fewer contracts here" |
| 1223 |
: "original declaration with more contracts here"); |
| 1224 |
return false; |
| 1225 |
} |
| 1226 |
|
| 1227 |
return true; |
| 1228 |
} |
| 1229 |
|
| 1230 |
/* Deferred contract mapping. |
| 1231 |
|
| 1232 |
This is used to compare late-parsed contracts on overrides with their |
| 1233 |
base class functions. |
| 1234 |
|
| 1235 |
TODO: It seems like this could be replaced by a simple list that maps from |
| 1236 |
overrides to their base functions. It's not clear that we really need |
| 1237 |
a map to a function + a list of contracts. */ |
| 1238 |
|
| 1239 |
/* Map from FNDECL to a tree list of contracts that have not been matched or |
| 1240 |
diagnosed yet. The TREE_PURPOSE is the basefn we're overriding, and the |
| 1241 |
TREE_VALUE is the list of contract attrs for BASEFN. */ |
| 1242 |
|
| 1243 |
static hash_map<tree_decl_hash, tree> pending_guarded_decls; |
| 1244 |
|
| 1245 |
void |
| 1246 |
defer_guarded_contract_match (tree fndecl, tree fn, tree contracts) |
| 1247 |
{ |
| 1248 |
if (!pending_guarded_decls.get (fndecl)) |
| 1249 |
{ |
| 1250 |
pending_guarded_decls.put (fndecl, build_tree_list (fn, contracts)); |
| 1251 |
return; |
| 1252 |
} |
| 1253 |
for (tree pending = *pending_guarded_decls.get (fndecl); |
| 1254 |
pending; |
| 1255 |
pending = TREE_CHAIN (pending)) |
| 1256 |
{ |
| 1257 |
if (TREE_VALUE (pending) == contracts) |
| 1258 |
return; |
| 1259 |
if (TREE_CHAIN (pending) == NULL_TREE) |
| 1260 |
TREE_CHAIN (pending) = build_tree_list (fn, contracts); |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* If the FUNCTION_DECL DECL has any contracts that had their matching |
| 1265 |
deferred earlier, do that checking now. */ |
| 1266 |
|
| 1267 |
void |
| 1268 |
match_deferred_contracts (tree decl) |
| 1269 |
{ |
| 1270 |
tree *tp = pending_guarded_decls.get (decl); |
| 1271 |
if (!tp) |
| 1272 |
return; |
| 1273 |
|
| 1274 |
gcc_assert(!contract_any_deferred_p (DECL_CONTRACTS (decl))); |
| 1275 |
|
| 1276 |
processing_template_decl_sentinel ptds; |
| 1277 |
processing_template_decl = uses_template_parms (decl); |
| 1278 |
|
| 1279 |
/* Do late contract matching. */ |
| 1280 |
for (tree pending = *tp; pending; pending = TREE_CHAIN (pending)) |
| 1281 |
{ |
| 1282 |
tree new_contracts = TREE_VALUE (pending); |
| 1283 |
location_t new_loc = CONTRACT_SOURCE_LOCATION (new_contracts); |
| 1284 |
tree old_contracts = DECL_CONTRACTS (decl); |
| 1285 |
location_t old_loc = CONTRACT_SOURCE_LOCATION (old_contracts); |
| 1286 |
tree base = TREE_PURPOSE (pending); |
| 1287 |
match_contract_conditions (new_loc, new_contracts, |
| 1288 |
old_loc, old_contracts, |
| 1289 |
base ? cmc_override : cmc_declaration); |
| 1290 |
} |
| 1291 |
|
| 1292 |
/* Clear out deferred match list so we don't check it twice. */ |
| 1293 |
pending_guarded_decls.remove (decl); |
| 1294 |
} |
| 1295 |
|
| 1296 |
/* Map from FUNCTION_DECL to a FUNCTION_DECL for either the PRE_FN or POST_FN. |
| 1297 |
These are used to parse contract conditions and are called inside the body |
| 1298 |
of the guarded function. */ |
| 1299 |
static GTY(()) hash_map<tree, tree> *decl_pre_fn; |
| 1300 |
static GTY(()) hash_map<tree, tree> *decl_post_fn; |
| 1301 |
|
| 1302 |
/* Returns the precondition funtion for D, or null if not set. */ |
| 1303 |
|
| 1304 |
tree |
| 1305 |
get_precondition_function (tree d) |
| 1306 |
{ |
| 1307 |
hash_map_maybe_create<hm_ggc> (decl_pre_fn); |
| 1308 |
tree *result = decl_pre_fn->get (d); |
| 1309 |
return result ? *result : NULL_TREE; |
| 1310 |
} |
| 1311 |
|
| 1312 |
/* Returns the postcondition funtion for D, or null if not set. */ |
| 1313 |
|
| 1314 |
tree |
| 1315 |
get_postcondition_function (tree d) |
| 1316 |
{ |
| 1317 |
hash_map_maybe_create<hm_ggc> (decl_post_fn); |
| 1318 |
tree *result = decl_post_fn->get (d); |
| 1319 |
return result ? *result : NULL_TREE; |
| 1320 |
} |
| 1321 |
|
| 1322 |
/* Makes PRE the precondition function for D. */ |
| 1323 |
|
| 1324 |
void |
| 1325 |
set_precondition_function (tree d, tree pre) |
| 1326 |
{ |
| 1327 |
gcc_assert (pre); |
| 1328 |
hash_map_maybe_create<hm_ggc> (decl_pre_fn); |
| 1329 |
gcc_assert (!decl_pre_fn->get (d)); |
| 1330 |
decl_pre_fn->put (d, pre); |
| 1331 |
} |
| 1332 |
|
| 1333 |
/* Makes POST the postcondition function for D. */ |
| 1334 |
|
| 1335 |
void |
| 1336 |
set_postcondition_function (tree d, tree post) |
| 1337 |
{ |
| 1338 |
gcc_assert (post); |
| 1339 |
hash_map_maybe_create<hm_ggc> (decl_post_fn); |
| 1340 |
gcc_assert (!decl_post_fn->get (d)); |
| 1341 |
decl_post_fn->put (d, post); |
| 1342 |
} |
| 1343 |
|
| 1344 |
/* Set the PRE and POST functions for D. Note that PRE and POST can be |
| 1345 |
null in this case. If so the functions are not recorded. */ |
| 1346 |
|
| 1347 |
void |
| 1348 |
set_contract_functions (tree d, tree pre, tree post) |
| 1349 |
{ |
| 1350 |
if (pre) |
| 1351 |
set_precondition_function (d, pre); |
| 1352 |
if (post) |
| 1353 |
set_postcondition_function (d, post); |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* Return a copy of the FUNCTION_DECL IDECL with its own unshared |
| 1357 |
PARM_DECL and DECL_ATTRIBUTEs. */ |
| 1358 |
|
| 1359 |
static tree |
| 1360 |
copy_fn_decl (tree idecl) |
| 1361 |
{ |
| 1362 |
tree decl = copy_decl (idecl); |
| 1363 |
DECL_ATTRIBUTES (decl) = copy_list (DECL_ATTRIBUTES (idecl)); |
| 1364 |
|
| 1365 |
if (DECL_RESULT (idecl)) |
| 1366 |
{ |
| 1367 |
DECL_RESULT (decl) = copy_decl (DECL_RESULT (idecl)); |
| 1368 |
DECL_CONTEXT (DECL_RESULT (decl)) = decl; |
| 1369 |
} |
| 1370 |
if (!DECL_ARGUMENTS (idecl) || VOID_TYPE_P (DECL_ARGUMENTS (idecl))) |
| 1371 |
return decl; |
| 1372 |
|
| 1373 |
tree last = DECL_ARGUMENTS (decl) = copy_decl (DECL_ARGUMENTS (decl)); |
| 1374 |
DECL_CONTEXT (last) = decl; |
| 1375 |
for (tree p = TREE_CHAIN (DECL_ARGUMENTS (idecl)); p; p = TREE_CHAIN (p)) |
| 1376 |
{ |
| 1377 |
if (VOID_TYPE_P (p)) |
| 1378 |
{ |
| 1379 |
TREE_CHAIN (last) = void_list_node; |
| 1380 |
break; |
| 1381 |
} |
| 1382 |
last = TREE_CHAIN (last) = copy_decl (p); |
| 1383 |
DECL_CONTEXT (last) = decl; |
| 1384 |
} |
| 1385 |
return decl; |
| 1386 |
} |
| 1387 |
|
| 1388 |
/* Build a declaration for the pre- or postcondition of a guarded FNDECL. */ |
| 1389 |
|
| 1390 |
static tree |
| 1391 |
build_contract_condition_function (tree fndecl, bool pre) |
| 1392 |
{ |
| 1393 |
if (TREE_TYPE (fndecl) == error_mark_node) |
| 1394 |
return error_mark_node; |
| 1395 |
if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl) |
| 1396 |
&& !TYPE_METHOD_BASETYPE (TREE_TYPE (fndecl))) |
| 1397 |
return error_mark_node; |
| 1398 |
|
| 1399 |
/* Create and rename the unchecked function and give an internal name. */ |
| 1400 |
tree fn = copy_fn_decl (fndecl); |
| 1401 |
DECL_RESULT (fn) = NULL_TREE; |
| 1402 |
tree value_type = pre ? void_type_node : TREE_TYPE (TREE_TYPE (fn)); |
| 1403 |
|
| 1404 |
/* Don't propagate declaration attributes to the checking function, |
| 1405 |
including the original contracts. */ |
| 1406 |
DECL_ATTRIBUTES (fn) = NULL_TREE; |
| 1407 |
|
| 1408 |
tree arg_types = NULL_TREE; |
| 1409 |
tree *last = &arg_types; |
| 1410 |
|
| 1411 |
/* FIXME will later optimizations delete unused args to prevent extra arg |
| 1412 |
passing? do we care? */ |
| 1413 |
tree class_type = NULL_TREE; |
| 1414 |
for (tree arg_type = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 1415 |
arg_type && arg_type != void_list_node; |
| 1416 |
arg_type = TREE_CHAIN (arg_type)) |
| 1417 |
{ |
| 1418 |
if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl) |
| 1419 |
&& TYPE_ARG_TYPES (TREE_TYPE (fn)) == arg_type) |
| 1420 |
{ |
| 1421 |
class_type = TREE_TYPE (TREE_VALUE (arg_type)); |
| 1422 |
continue; |
| 1423 |
} |
| 1424 |
*last = build_tree_list (TREE_PURPOSE (arg_type), TREE_VALUE (arg_type)); |
| 1425 |
last = &TREE_CHAIN (*last); |
| 1426 |
} |
| 1427 |
|
| 1428 |
if (pre || VOID_TYPE_P (value_type)) |
| 1429 |
*last = void_list_node; |
| 1430 |
else |
| 1431 |
{ |
| 1432 |
tree name = get_identifier ("__r"); |
| 1433 |
tree parm = build_lang_decl (PARM_DECL, name, value_type); |
| 1434 |
DECL_CONTEXT (parm) = fn; |
| 1435 |
DECL_ARTIFICIAL (parm) = true; |
| 1436 |
DECL_ARGUMENTS (fn) = chainon (DECL_ARGUMENTS (fn), parm); |
| 1437 |
|
| 1438 |
*last = build_tree_list (NULL_TREE, value_type); |
| 1439 |
TREE_CHAIN (*last) = void_list_node; |
| 1440 |
|
| 1441 |
/* The handler is a void return. */ |
| 1442 |
value_type = void_type_node; |
| 1443 |
} |
| 1444 |
|
| 1445 |
TREE_TYPE (fn) = build_function_type (value_type, arg_types); |
| 1446 |
if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)) |
| 1447 |
TREE_TYPE (fn) = build_method_type (class_type, TREE_TYPE (fn)); |
| 1448 |
|
| 1449 |
DECL_NAME (fn) = copy_node (DECL_NAME (fn)); |
| 1450 |
DECL_INITIAL (fn) = error_mark_node; |
| 1451 |
DECL_ABSTRACT_ORIGIN (fn) = fndecl; |
| 1452 |
|
| 1453 |
IDENTIFIER_VIRTUAL_P (DECL_NAME (fn)) = false; |
| 1454 |
DECL_VIRTUAL_P (fn) = false; |
| 1455 |
|
| 1456 |
/* Make these functions internal if we can, i.e. if the guarded function is |
| 1457 |
not vague linkage, or if we can put them in a comdat group with the |
| 1458 |
guarded function. */ |
| 1459 |
if (!DECL_WEAK (fndecl) || HAVE_COMDAT_GROUP) |
| 1460 |
{ |
| 1461 |
TREE_PUBLIC (fn) = false; |
| 1462 |
DECL_EXTERNAL (fn) = false; |
| 1463 |
DECL_WEAK (fn) = false; |
| 1464 |
DECL_COMDAT (fn) = false; |
| 1465 |
|
| 1466 |
/* We may not have set the comdat group on the guarded function yet. |
| 1467 |
If we haven't, we'll add this to the same group in comdat_linkage |
| 1468 |
later. Otherwise, add it to the same comdat group now. */ |
| 1469 |
if (DECL_ONE_ONLY (fndecl)) |
| 1470 |
{ |
| 1471 |
symtab_node *n = symtab_node::get (fndecl); |
| 1472 |
cgraph_node::get_create (fn)->add_to_same_comdat_group (n); |
| 1473 |
} |
| 1474 |
|
| 1475 |
DECL_INTERFACE_KNOWN (fn) = true; |
| 1476 |
} |
| 1477 |
|
| 1478 |
DECL_ARTIFICIAL (fn) = true; |
| 1479 |
|
| 1480 |
/* Update various inline related declaration properties. */ |
| 1481 |
//DECL_DECLARED_INLINE_P (fn) = true; |
| 1482 |
DECL_DISREGARD_INLINE_LIMITS (fn) = true; |
| 1483 |
TREE_NO_WARNING (fn) = 1; |
| 1484 |
|
| 1485 |
return fn; |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* Return true if CONTRACT is checked or assumed under the current build |
| 1489 |
configuration. */ |
| 1490 |
|
| 1491 |
bool |
| 1492 |
contract_active_p (tree contract) |
| 1493 |
{ |
| 1494 |
return get_contract_semantic (contract) != CCS_IGNORE; |
| 1495 |
} |
| 1496 |
|
| 1497 |
static bool |
| 1498 |
has_active_contract_condition (tree d, tree_code c) |
| 1499 |
{ |
| 1500 |
for (tree as = DECL_CONTRACTS (d) ; as != NULL_TREE; as = CONTRACT_CHAIN (as)) |
| 1501 |
{ |
| 1502 |
tree contract = TREE_VALUE (TREE_VALUE (as)); |
| 1503 |
if (TREE_CODE (contract) == c && contract_active_p (contract)) |
| 1504 |
return true; |
| 1505 |
} |
| 1506 |
return false; |
| 1507 |
} |
| 1508 |
|
| 1509 |
/* True if D has any checked or assumed preconditions. */ |
| 1510 |
|
| 1511 |
static bool |
| 1512 |
has_active_preconditions (tree d) |
| 1513 |
{ |
| 1514 |
return has_active_contract_condition (d, PRECONDITION_STMT); |
| 1515 |
} |
| 1516 |
|
| 1517 |
/* True if D has any checked or assumed postconditions. */ |
| 1518 |
|
| 1519 |
static bool |
| 1520 |
has_active_postconditions (tree d) |
| 1521 |
{ |
| 1522 |
return has_active_contract_condition (d, POSTCONDITION_STMT); |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* Return true if any contract in the CONTRACT list is checked or assumed |
| 1526 |
under the current build configuration. */ |
| 1527 |
|
| 1528 |
bool |
| 1529 |
contract_any_active_p (tree contract) |
| 1530 |
{ |
| 1531 |
for (; contract != NULL_TREE; contract = CONTRACT_CHAIN (contract)) |
| 1532 |
if (contract_active_p (TREE_VALUE (TREE_VALUE (contract)))) |
| 1533 |
return true; |
| 1534 |
return false; |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* Do we need to mess with contracts for DECL1? */ |
| 1538 |
|
| 1539 |
static bool |
| 1540 |
handle_contracts_p (tree decl1) |
| 1541 |
{ |
| 1542 |
return (flag_contracts |
| 1543 |
&& !processing_template_decl |
| 1544 |
&& DECL_ABSTRACT_ORIGIN (decl1) == NULL_TREE |
| 1545 |
&& contract_any_active_p (DECL_CONTRACTS (decl1))); |
| 1546 |
} |
| 1547 |
|
| 1548 |
/* Should we break out DECL1's pre/post contracts into separate functions? |
| 1549 |
FIXME I'd like this to default to 0, but that will need an overhaul to the |
| 1550 |
return identifier handling to just refer to the RESULT_DECL. */ |
| 1551 |
|
| 1552 |
static bool |
| 1553 |
outline_contracts_p (tree decl1) |
| 1554 |
{ |
| 1555 |
return (!DECL_CONSTRUCTOR_P (decl1) |
| 1556 |
&& !DECL_DESTRUCTOR_P (decl1)); |
| 1557 |
} |
| 1558 |
|
| 1559 |
/* Build the precondition checking function for D. */ |
| 1560 |
|
| 1561 |
static tree |
| 1562 |
build_precondition_function (tree d) |
| 1563 |
{ |
| 1564 |
if (!has_active_preconditions (d)) |
| 1565 |
return NULL_TREE; |
| 1566 |
|
| 1567 |
return build_contract_condition_function (d, /*pre=*/true); |
| 1568 |
} |
| 1569 |
|
| 1570 |
/* Build the postcondition checking function for D. If the return |
| 1571 |
type is undeduced, don't build the function yet. We do that in |
| 1572 |
apply_deduced_return_type. */ |
| 1573 |
|
| 1574 |
static tree |
| 1575 |
build_postcondition_function (tree d) |
| 1576 |
{ |
| 1577 |
if (!has_active_postconditions (d)) |
| 1578 |
return NULL_TREE; |
| 1579 |
|
| 1580 |
tree type = TREE_TYPE (TREE_TYPE (d)); |
| 1581 |
if (is_auto (type)) |
| 1582 |
return NULL_TREE; |
| 1583 |
|
| 1584 |
return build_contract_condition_function (d, /*pre=*/false); |
| 1585 |
} |
| 1586 |
|
| 1587 |
static void |
| 1588 |
build_contract_function_decls (tree d) |
| 1589 |
{ |
| 1590 |
/* Constructors and destructors have their contracts inserted inline. */ |
| 1591 |
if (!outline_contracts_p (d)) |
| 1592 |
return; |
| 1593 |
|
| 1594 |
/* Build the pre/post functions (or not). */ |
| 1595 |
tree pre = build_precondition_function (d); |
| 1596 |
tree post = build_postcondition_function (d); |
| 1597 |
set_contract_functions (d, pre, post); |
| 1598 |
} |
| 1599 |
|
| 1600 |
static const char * |
| 1601 |
get_contract_level_name (tree contract) |
| 1602 |
{ |
| 1603 |
if (CONTRACT_LITERAL_MODE_P (contract)) |
| 1604 |
return ""; |
| 1605 |
if (tree mode = CONTRACT_MODE (contract)) |
| 1606 |
if (tree level = TREE_VALUE (mode)) |
| 1607 |
return IDENTIFIER_POINTER (level); |
| 1608 |
return "default"; |
| 1609 |
} |
| 1610 |
|
| 1611 |
static const char * |
| 1612 |
get_contract_role_name (tree contract) |
| 1613 |
{ |
| 1614 |
if (CONTRACT_LITERAL_MODE_P (contract)) |
| 1615 |
return ""; |
| 1616 |
if (tree mode = CONTRACT_MODE (contract)) |
| 1617 |
if (tree role = TREE_PURPOSE (mode)) |
| 1618 |
return IDENTIFIER_POINTER (role); |
| 1619 |
return "default"; |
| 1620 |
} |
| 1621 |
|
| 1622 |
/* Build a layout-compatible internal version of std::contract_violation. */ |
| 1623 |
|
| 1624 |
static tree |
| 1625 |
get_pseudo_contract_violation_type () |
| 1626 |
{ |
| 1627 |
if (!pseudo_contract_violation_type) |
| 1628 |
{ |
| 1629 |
/* Must match <contract>: |
| 1630 |
class contract_violation { |
| 1631 |
const char* _M_file; |
| 1632 |
const char* _M_function; |
| 1633 |
const char* _M_comment; |
| 1634 |
const char* _M_level; |
| 1635 |
const char* _M_role; |
| 1636 |
uint_least32_t _M_line; |
| 1637 |
signed char _M_continue; |
| 1638 |
If this changes, also update the initializer in |
| 1639 |
build_contract_violation. */ |
| 1640 |
struct field_info { tree type; const char* name; }; |
| 1641 |
const field_info info[] = { |
| 1642 |
{ const_string_type_node, "_M_file" }, |
| 1643 |
{ const_string_type_node, "_M_function" }, |
| 1644 |
{ const_string_type_node, "_M_comment" }, |
| 1645 |
{ const_string_type_node, "_M_level" }, |
| 1646 |
{ const_string_type_node, "_M_role" }, |
| 1647 |
{ uint_least32_type_node, "_M_line" }, |
| 1648 |
{ signed_char_type_node, "_M_continue" } |
| 1649 |
}; |
| 1650 |
tree fields = NULL_TREE; |
| 1651 |
for (const field_info& i : info) |
| 1652 |
{ |
| 1653 |
/* finish_builtin_struct wants fieldss chained in reverse. */ |
| 1654 |
tree next = build_decl (BUILTINS_LOCATION, FIELD_DECL, |
| 1655 |
get_identifier (i.name), i.type); |
| 1656 |
DECL_CHAIN (next) = fields; |
| 1657 |
fields = next; |
| 1658 |
} |
| 1659 |
iloc_sentinel ils (input_location); |
| 1660 |
input_location = BUILTINS_LOCATION; |
| 1661 |
pseudo_contract_violation_type = make_class_type (RECORD_TYPE); |
| 1662 |
finish_builtin_struct (pseudo_contract_violation_type, |
| 1663 |
"__pseudo_contract_violation", |
| 1664 |
fields, NULL_TREE); |
| 1665 |
CLASSTYPE_AS_BASE (pseudo_contract_violation_type) |
| 1666 |
= pseudo_contract_violation_type; |
| 1667 |
DECL_CONTEXT (TYPE_NAME (pseudo_contract_violation_type)) |
| 1668 |
= FROB_CONTEXT (global_namespace); |
| 1669 |
TREE_PUBLIC (TYPE_NAME (pseudo_contract_violation_type)) = true; |
| 1670 |
CLASSTYPE_LITERAL_P (pseudo_contract_violation_type) = true; |
| 1671 |
CLASSTYPE_LAZY_COPY_CTOR (pseudo_contract_violation_type) = true; |
| 1672 |
xref_basetypes (pseudo_contract_violation_type, /*bases=*/NULL_TREE); |
| 1673 |
pseudo_contract_violation_type |
| 1674 |
= cp_build_qualified_type (pseudo_contract_violation_type, |
| 1675 |
TYPE_QUAL_CONST); |
| 1676 |
} |
| 1677 |
return pseudo_contract_violation_type; |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* Return a VAR_DECL to pass to handle_contract_violation. */ |
| 1681 |
|
| 1682 |
static tree |
| 1683 |
build_contract_violation (tree contract, contract_continuation cmode) |
| 1684 |
{ |
| 1685 |
expanded_location loc = expand_location (EXPR_LOCATION (contract)); |
| 1686 |
const char *function = fndecl_name (DECL_ORIGIN (current_function_decl)); |
| 1687 |
const char *level = get_contract_level_name (contract); |
| 1688 |
const char *role = get_contract_role_name (contract); |
| 1689 |
|
| 1690 |
/* Must match the type layout in get_pseudo_contract_violation_type. */ |
| 1691 |
tree ctor = build_constructor_va |
| 1692 |
(init_list_type_node, 7, |
| 1693 |
NULL_TREE, build_string_literal (loc.file), |
| 1694 |
NULL_TREE, build_string_literal (function), |
| 1695 |
NULL_TREE, CONTRACT_COMMENT (contract), |
| 1696 |
NULL_TREE, build_string_literal (level), |
| 1697 |
NULL_TREE, build_string_literal (role), |
| 1698 |
NULL_TREE, build_int_cst (uint_least32_type_node, loc.line), |
| 1699 |
NULL_TREE, build_int_cst (signed_char_type_node, cmode)); |
| 1700 |
|
| 1701 |
ctor = finish_compound_literal (get_pseudo_contract_violation_type (), |
| 1702 |
ctor, tf_none); |
| 1703 |
protected_set_expr_location (ctor, EXPR_LOCATION (contract)); |
| 1704 |
return ctor; |
| 1705 |
} |
| 1706 |
|
| 1707 |
/* Return handle_contract_violation(), declaring it if needed. */ |
| 1708 |
|
| 1709 |
static tree |
| 1710 |
declare_handle_contract_violation () |
| 1711 |
{ |
| 1712 |
tree fnname = get_identifier ("handle_contract_violation"); |
| 1713 |
tree viol_name = get_identifier ("contract_violation"); |
| 1714 |
tree l = lookup_qualified_name (global_namespace, fnname, |
| 1715 |
LOOK_want::HIDDEN_FRIEND); |
| 1716 |
for (tree f: lkp_range (l)) |
| 1717 |
if (TREE_CODE (f) == FUNCTION_DECL) |
| 1718 |
{ |
| 1719 |
tree parms = TYPE_ARG_TYPES (TREE_TYPE (f)); |
| 1720 |
if (remaining_arguments (parms) != 1) |
| 1721 |
continue; |
| 1722 |
tree parmtype = non_reference (TREE_VALUE (parms)); |
| 1723 |
if (CLASS_TYPE_P (parmtype) |
| 1724 |
&& TYPE_IDENTIFIER (parmtype) == viol_name) |
| 1725 |
return f; |
| 1726 |
} |
| 1727 |
|
| 1728 |
tree id_exp = get_identifier ("experimental"); |
| 1729 |
tree ns_exp = lookup_qualified_name (std_node, id_exp); |
| 1730 |
|
| 1731 |
tree violation = error_mark_node; |
| 1732 |
if (TREE_CODE (ns_exp) == NAMESPACE_DECL) |
| 1733 |
violation = lookup_qualified_name (ns_exp, viol_name, |
| 1734 |
LOOK_want::TYPE |
| 1735 |
|LOOK_want::HIDDEN_FRIEND); |
| 1736 |
|
| 1737 |
if (TREE_CODE (violation) == TYPE_DECL) |
| 1738 |
violation = TREE_TYPE (violation); |
| 1739 |
else |
| 1740 |
{ |
| 1741 |
push_nested_namespace (std_node); |
| 1742 |
push_namespace (id_exp, /*inline*/false); |
| 1743 |
violation = make_class_type (RECORD_TYPE); |
| 1744 |
create_implicit_typedef (viol_name, violation); |
| 1745 |
DECL_SOURCE_LOCATION (TYPE_NAME (violation)) = BUILTINS_LOCATION; |
| 1746 |
DECL_CONTEXT (TYPE_NAME (violation)) = current_namespace; |
| 1747 |
TREE_PUBLIC (TYPE_NAME (violation)) = true; |
| 1748 |
pushdecl_namespace_level (TYPE_NAME (violation), /*hidden*/true); |
| 1749 |
pop_namespace (); |
| 1750 |
pop_nested_namespace (std_node); |
| 1751 |
} |
| 1752 |
|
| 1753 |
tree argtype = cp_build_qualified_type (violation, TYPE_QUAL_CONST); |
| 1754 |
argtype = cp_build_reference_type (argtype, /*rval*/false); |
| 1755 |
tree fntype = build_function_type_list (void_type_node, argtype, NULL_TREE); |
| 1756 |
|
| 1757 |
push_nested_namespace (global_namespace); |
| 1758 |
tree fn = build_cp_library_fn_ptr ("handle_contract_violation", fntype, |
| 1759 |
ECF_COLD); |
| 1760 |
pushdecl_namespace_level (fn, /*hiding*/true); |
| 1761 |
pop_nested_namespace (global_namespace); |
| 1762 |
|
| 1763 |
return fn; |
| 1764 |
} |
| 1765 |
|
| 1766 |
/* Build the call to handle_contract_violation for CONTRACT. */ |
| 1767 |
|
| 1768 |
static void |
| 1769 |
build_contract_handler_call (tree contract, |
| 1770 |
contract_continuation cmode) |
| 1771 |
{ |
| 1772 |
/* We may need to declare new types, ensure they are not considered |
| 1773 |
attached to a named module. */ |
| 1774 |
auto module_kind_override = make_temp_override |
| 1775 |
(module_kind, module_kind & ~(MK_PURVIEW | MK_ATTACH | MK_EXPORTING)); |
| 1776 |
|
| 1777 |
tree violation = build_contract_violation (contract, cmode); |
| 1778 |
tree violation_fn = declare_handle_contract_violation (); |
| 1779 |
tree call = build_call_n (violation_fn, 1, build_address (violation)); |
| 1780 |
finish_expr_stmt (call); |
| 1781 |
} |
| 1782 |
|
| 1783 |
/* Generate the code that checks or assumes a contract, but do not attach |
| 1784 |
it to the current context. This is called during genericization. */ |
| 1785 |
|
| 1786 |
tree |
| 1787 |
build_contract_check (tree contract) |
| 1788 |
{ |
| 1789 |
contract_semantic semantic = get_contract_semantic (contract); |
| 1790 |
if (semantic == CCS_INVALID) |
| 1791 |
return NULL_TREE; |
| 1792 |
|
| 1793 |
/* Ignored contracts are never checked or assumed. */ |
| 1794 |
if (semantic == CCS_IGNORE) |
| 1795 |
return void_node; |
| 1796 |
|
| 1797 |
remap_dummy_this (current_function_decl, &CONTRACT_CONDITION (contract)); |
| 1798 |
tree condition = CONTRACT_CONDITION (contract); |
| 1799 |
if (condition == error_mark_node) |
| 1800 |
return NULL_TREE; |
| 1801 |
|
| 1802 |
location_t loc = EXPR_LOCATION (contract); |
| 1803 |
|
| 1804 |
if (semantic == CCS_ASSUME) |
| 1805 |
return build_assume_call (loc, condition); |
| 1806 |
|
| 1807 |
tree if_stmt = begin_if_stmt (); |
| 1808 |
tree cond = build_x_unary_op (loc, |
| 1809 |
TRUTH_NOT_EXPR, |
| 1810 |
condition, NULL_TREE, |
| 1811 |
tf_warning_or_error); |
| 1812 |
finish_if_stmt_cond (cond, if_stmt); |
| 1813 |
|
| 1814 |
/* Get the continuation mode. */ |
| 1815 |
contract_continuation cmode; |
| 1816 |
switch (semantic) |
| 1817 |
{ |
| 1818 |
case CCS_NEVER: cmode = NEVER_CONTINUE; break; |
| 1819 |
case CCS_MAYBE: cmode = MAYBE_CONTINUE; break; |
| 1820 |
default: gcc_unreachable (); |
| 1821 |
} |
| 1822 |
|
| 1823 |
build_contract_handler_call (contract, cmode); |
| 1824 |
if (cmode == NEVER_CONTINUE) |
| 1825 |
finish_expr_stmt (build_call_a (terminate_fn, 0, nullptr)); |
| 1826 |
|
| 1827 |
finish_then_clause (if_stmt); |
| 1828 |
tree scope = IF_SCOPE (if_stmt); |
| 1829 |
IF_SCOPE (if_stmt) = NULL; |
| 1830 |
return do_poplevel (scope); |
| 1831 |
} |
| 1832 |
|
| 1833 |
/* Add the contract statement CONTRACT to the current block if valid. */ |
| 1834 |
|
| 1835 |
static void |
| 1836 |
emit_contract_statement (tree contract) |
| 1837 |
{ |
| 1838 |
/* Only add valid contracts. */ |
| 1839 |
if (get_contract_semantic (contract) != CCS_INVALID |
| 1840 |
&& CONTRACT_CONDITION (contract) != error_mark_node) |
| 1841 |
add_stmt (contract); |
| 1842 |
} |
| 1843 |
|
| 1844 |
/* Generate the statement for the given contract attribute by adding the |
| 1845 |
statement to the current block. Returns the next contract in the chain. */ |
| 1846 |
|
| 1847 |
static tree |
| 1848 |
emit_contract_attr (tree attr) |
| 1849 |
{ |
| 1850 |
gcc_assert (TREE_CODE (attr) == TREE_LIST); |
| 1851 |
|
| 1852 |
emit_contract_statement (CONTRACT_STATEMENT (attr)); |
| 1853 |
|
| 1854 |
return CONTRACT_CHAIN (attr); |
| 1855 |
} |
| 1856 |
|
| 1857 |
/* Add the statements of contract attributes ATTRS to the current block. */ |
| 1858 |
|
| 1859 |
static void |
| 1860 |
emit_contract_conditions (tree attrs, tree_code code) |
| 1861 |
{ |
| 1862 |
if (!attrs) return; |
| 1863 |
gcc_assert (TREE_CODE (attrs) == TREE_LIST); |
| 1864 |
gcc_assert (code == PRECONDITION_STMT || code == POSTCONDITION_STMT); |
| 1865 |
while (attrs) |
| 1866 |
{ |
| 1867 |
tree contract = CONTRACT_STATEMENT (attrs); |
| 1868 |
if (TREE_CODE (contract) == code) |
| 1869 |
attrs = emit_contract_attr (attrs); |
| 1870 |
else |
| 1871 |
attrs = CONTRACT_CHAIN (attrs); |
| 1872 |
} |
| 1873 |
} |
| 1874 |
|
| 1875 |
/* Emit the statement for an assertion attribute. */ |
| 1876 |
|
| 1877 |
void |
| 1878 |
emit_assertion (tree attr) |
| 1879 |
{ |
| 1880 |
emit_contract_attr (attr); |
| 1881 |
} |
| 1882 |
|
| 1883 |
/* Emit statements for precondition attributes. */ |
| 1884 |
|
| 1885 |
static void |
| 1886 |
emit_preconditions (tree attr) |
| 1887 |
{ |
| 1888 |
return emit_contract_conditions (attr, PRECONDITION_STMT); |
| 1889 |
} |
| 1890 |
|
| 1891 |
/* Emit statements for postcondition attributes. */ |
| 1892 |
|
| 1893 |
static void |
| 1894 |
emit_postconditions (tree attr) |
| 1895 |
{ |
| 1896 |
return emit_contract_conditions (attr, POSTCONDITION_STMT); |
| 1897 |
} |
| 1898 |
|
| 1899 |
/* We're compiling the pre/postcondition function CONDFN; remap any FN |
| 1900 |
attributes that match CODE and emit them. */ |
| 1901 |
|
| 1902 |
static void |
| 1903 |
remap_and_emit_conditions (tree fn, tree condfn, tree_code code) |
| 1904 |
{ |
| 1905 |
gcc_assert (code == PRECONDITION_STMT || code == POSTCONDITION_STMT); |
| 1906 |
for (tree attr = DECL_CONTRACTS (fn); attr; |
| 1907 |
attr = CONTRACT_CHAIN (attr)) |
| 1908 |
{ |
| 1909 |
tree contract = CONTRACT_STATEMENT (attr); |
| 1910 |
if (TREE_CODE (contract) == code) |
| 1911 |
{ |
| 1912 |
contract = copy_node (contract); |
| 1913 |
remap_contract (fn, condfn, contract, /*duplicate_p=*/false); |
| 1914 |
emit_contract_statement (contract); |
| 1915 |
} |
| 1916 |
} |
| 1917 |
} |
| 1918 |
|
| 1919 |
/* Converts a contract condition to bool and ensures it has a locaiton. */ |
| 1920 |
|
| 1921 |
tree |
| 1922 |
finish_contract_condition (cp_expr condition) |
| 1923 |
{ |
| 1924 |
/* Ensure we have the condition location saved in case we later need to |
| 1925 |
emit a conversion error during template instantiation and wouldn't |
| 1926 |
otherwise have it. */ |
| 1927 |
if (!CAN_HAVE_LOCATION_P (condition) || EXCEPTIONAL_CLASS_P (condition)) |
| 1928 |
{ |
| 1929 |
condition = build1_loc (condition.get_location (), VIEW_CONVERT_EXPR, |
| 1930 |
TREE_TYPE (condition), condition); |
| 1931 |
EXPR_LOCATION_WRAPPER_P (condition) = 1; |
| 1932 |
} |
| 1933 |
|
| 1934 |
if (condition == error_mark_node || type_dependent_expression_p (condition)) |
| 1935 |
return condition; |
| 1936 |
|
| 1937 |
return condition_conversion (condition); |
| 1938 |
} |
| 1939 |
|
| 1940 |
void |
| 1941 |
maybe_update_postconditions (tree fco) |
| 1942 |
{ |
| 1943 |
/* Update any postconditions and the postcondition checking function |
| 1944 |
as needed. If there are postconditions, we'll use those to rewrite |
| 1945 |
return statements to check postconditions. */ |
| 1946 |
if (has_active_postconditions (fco)) |
| 1947 |
{ |
| 1948 |
rebuild_postconditions (fco); |
| 1949 |
tree post = build_postcondition_function (fco); |
| 1950 |
set_postcondition_function (fco, post); |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
/* Called on attribute lists that must not contain contracts. If any |
| 1955 |
contracts are present, issue an error diagnostic and return true. */ |
| 1956 |
|
| 1957 |
bool |
| 1958 |
diagnose_misapplied_contracts (tree attributes) |
| 1959 |
{ |
| 1960 |
if (attributes == NULL_TREE) |
| 1961 |
return false; |
| 1962 |
|
| 1963 |
tree contract_attr = find_contract (attributes); |
| 1964 |
if (!contract_attr) |
| 1965 |
return false; |
| 1966 |
|
| 1967 |
error_at (EXPR_LOCATION (CONTRACT_STATEMENT (contract_attr)), |
| 1968 |
"contracts must appertain to a function type"); |
| 1969 |
|
| 1970 |
/* Invalidate the contract so we don't treat it as valid later on. */ |
| 1971 |
invalidate_contract (TREE_VALUE (TREE_VALUE (contract_attr))); |
| 1972 |
|
| 1973 |
return true; |
| 1974 |
} |
| 1975 |
|
| 1976 |
/* Build and return an argument list containing all the parameters of the |
| 1977 |
(presumably guarded) FUNCTION_DECL FN. This can be used to forward all of |
| 1978 |
FN's arguments to a function taking the same list of arguments -- namely |
| 1979 |
the unchecked form of FN. |
| 1980 |
|
| 1981 |
We use CALL_FROM_THUNK_P instead of forward_parm for forwarding |
| 1982 |
semantics. */ |
| 1983 |
|
| 1984 |
static vec<tree, va_gc> * |
| 1985 |
build_arg_list (tree fn) |
| 1986 |
{ |
| 1987 |
vec<tree, va_gc> *args = make_tree_vector (); |
| 1988 |
for (tree t = DECL_ARGUMENTS (fn); t; t = DECL_CHAIN (t)) |
| 1989 |
vec_safe_push (args, t); |
| 1990 |
return args; |
| 1991 |
} |
| 1992 |
|
| 1993 |
void |
| 1994 |
start_function_contracts (tree decl1) |
| 1995 |
{ |
| 1996 |
if (!handle_contracts_p (decl1)) |
| 1997 |
return; |
| 1998 |
|
| 1999 |
/* For cdtors, we evaluate the contracts check inline. */ |
| 2000 |
if (!outline_contracts_p (decl1)) |
| 2001 |
return; |
| 2002 |
|
| 2003 |
/* Contracts may have just been added without a chance to parse them, though |
| 2004 |
we still need the PRE_FN available to generate a call to it. */ |
| 2005 |
if (!DECL_PRE_FN (decl1)) |
| 2006 |
build_contract_function_decls (decl1); |
| 2007 |
|
| 2008 |
} |
| 2009 |
|
| 2010 |
/* If we have a precondition function and it's valid, call it. */ |
| 2011 |
|
| 2012 |
static void |
| 2013 |
add_pre_condition_fn_call (tree fndecl) |
| 2014 |
{ |
| 2015 |
/* If we're starting a guarded function with valid contracts, we need to |
| 2016 |
insert a call to the pre function. */ |
| 2017 |
gcc_checking_assert (DECL_PRE_FN (fndecl) |
| 2018 |
&& DECL_PRE_FN (fndecl) != error_mark_node); |
| 2019 |
|
| 2020 |
releasing_vec args = build_arg_list (fndecl); |
| 2021 |
tree call = build_call_a (DECL_PRE_FN (fndecl), args->length (), |
| 2022 |
args->address ()); |
| 2023 |
CALL_FROM_THUNK_P (call) = true; |
| 2024 |
finish_expr_stmt (call); |
| 2025 |
} |
| 2026 |
|
| 2027 |
/* Build and add a call to the post-condition checking function, when that |
| 2028 |
is in use. */ |
| 2029 |
|
| 2030 |
static void |
| 2031 |
add_post_condition_fn_call (tree fndecl) |
| 2032 |
{ |
| 2033 |
gcc_checking_assert (DECL_POST_FN (fndecl) |
| 2034 |
&& DECL_POST_FN (fndecl) != error_mark_node); |
| 2035 |
|
| 2036 |
releasing_vec args = build_arg_list (fndecl); |
| 2037 |
if (get_postcondition_result_parameter (fndecl)) |
| 2038 |
vec_safe_push (args, DECL_RESULT (fndecl)); |
| 2039 |
tree call = build_call_a (DECL_POST_FN (fndecl), args->length (), |
| 2040 |
args->address ()); |
| 2041 |
CALL_FROM_THUNK_P (call) = true; |
| 2042 |
finish_expr_stmt (call); |
| 2043 |
} |
| 2044 |
|
| 2045 |
/* Add a call or a direct evaluation of the pre checks. */ |
| 2046 |
|
| 2047 |
static void |
| 2048 |
apply_preconditions (tree fndecl) |
| 2049 |
{ |
| 2050 |
if (outline_contracts_p (fndecl)) |
| 2051 |
add_pre_condition_fn_call (fndecl); |
| 2052 |
else |
| 2053 |
emit_preconditions (DECL_CONTRACTS (fndecl)); |
| 2054 |
} |
| 2055 |
|
| 2056 |
/* Add a call or a direct evaluation of the post checks. */ |
| 2057 |
|
| 2058 |
static void |
| 2059 |
apply_postconditions (tree fndecl) |
| 2060 |
{ |
| 2061 |
if (outline_contracts_p (fndecl)) |
| 2062 |
add_post_condition_fn_call (fndecl); |
| 2063 |
else |
| 2064 |
emit_postconditions (DECL_CONTRACTS (fndecl)); |
| 2065 |
} |
| 2066 |
|
| 2067 |
/* Add contract handling to the function in FNDECL. |
| 2068 |
|
| 2069 |
When we have only pre-conditions, this simply prepends a call (or a direct |
| 2070 |
evaluation, for cdtors) to the existing function body. |
| 2071 |
|
| 2072 |
When we have post conditions we build a try-finally block. |
| 2073 |
If the function might throw then the handler in the try-finally is an |
| 2074 |
EH_ELSE expression, where the post condition check is applied to the |
| 2075 |
non-exceptional path, and an empty statement is added to the EH path. If |
| 2076 |
the function has a non-throwing eh spec, then the handler is simply the |
| 2077 |
post-condition checker. */ |
| 2078 |
|
| 2079 |
void |
| 2080 |
maybe_apply_function_contracts (tree fndecl) |
| 2081 |
{ |
| 2082 |
if (!handle_contracts_p (fndecl)) |
| 2083 |
/* We did nothing and the original function body statement list will be |
| 2084 |
popped by our caller. */ |
| 2085 |
return; |
| 2086 |
|
| 2087 |
bool do_pre = has_active_preconditions (fndecl); |
| 2088 |
bool do_post = has_active_postconditions (fndecl); |
| 2089 |
/* We should not have reached here with nothing to do... */ |
| 2090 |
gcc_checking_assert (do_pre || do_post); |
| 2091 |
|
| 2092 |
/* This copies the approach used for function try blocks. */ |
| 2093 |
tree fnbody = pop_stmt_list (DECL_SAVED_TREE (fndecl)); |
| 2094 |
DECL_SAVED_TREE (fndecl) = push_stmt_list (); |
| 2095 |
tree compound_stmt = begin_compound_stmt (0); |
| 2096 |
current_binding_level->artificial = 1; |
| 2097 |
|
| 2098 |
/* Do not add locations for the synthesised code. */ |
| 2099 |
location_t loc = UNKNOWN_LOCATION; |
| 2100 |
|
| 2101 |
/* For other cases, we call a function to process the check. */ |
| 2102 |
|
| 2103 |
/* If we have a pre, but not a post, then just emit that and we are done. */ |
| 2104 |
if (!do_post) |
| 2105 |
{ |
| 2106 |
apply_preconditions (fndecl); |
| 2107 |
add_stmt (fnbody); |
| 2108 |
finish_compound_stmt (compound_stmt); |
| 2109 |
return; |
| 2110 |
} |
| 2111 |
|
| 2112 |
if (do_pre) |
| 2113 |
/* Add a precondition call, if we have one. */ |
| 2114 |
apply_preconditions (fndecl); |
| 2115 |
tree try_fin = build_stmt (loc, TRY_FINALLY_EXPR, fnbody, NULL_TREE); |
| 2116 |
add_stmt (try_fin); |
| 2117 |
TREE_OPERAND (try_fin, 1) = push_stmt_list (); |
| 2118 |
/* If we have exceptions, and a function that might throw, then add |
| 2119 |
an EH_ELSE clause that allows the exception to propagate upwards |
| 2120 |
without encountering the post-condition checks. */ |
| 2121 |
if (flag_exceptions && !type_noexcept_p (TREE_TYPE (fndecl))) |
| 2122 |
{ |
| 2123 |
tree eh_else = build_stmt (loc, EH_ELSE_EXPR, NULL_TREE, NULL_TREE); |
| 2124 |
add_stmt (eh_else); |
| 2125 |
TREE_OPERAND (eh_else, 0) = push_stmt_list (); |
| 2126 |
apply_postconditions (fndecl); |
| 2127 |
TREE_OPERAND (eh_else, 0) = pop_stmt_list (TREE_OPERAND (eh_else, 0)); |
| 2128 |
TREE_OPERAND (eh_else, 1) = build_empty_stmt (loc); |
| 2129 |
} |
| 2130 |
else |
| 2131 |
apply_postconditions (fndecl); |
| 2132 |
TREE_OPERAND (try_fin, 1) = pop_stmt_list (TREE_OPERAND (try_fin, 1)); |
| 2133 |
finish_compound_stmt (compound_stmt); |
| 2134 |
/* The DECL_SAVED_TREE stmt list will be popped by our caller. */ |
| 2135 |
} |
| 2136 |
|
| 2137 |
/* Finish up the pre & post function definitions for a guarded FNDECL, |
| 2138 |
and compile those functions all the way to assembler language output. */ |
| 2139 |
|
| 2140 |
void |
| 2141 |
finish_function_contracts (tree fndecl) |
| 2142 |
{ |
| 2143 |
if (!handle_contracts_p (fndecl) |
| 2144 |
|| !outline_contracts_p (fndecl)) |
| 2145 |
return; |
| 2146 |
|
| 2147 |
for (tree ca = DECL_CONTRACTS (fndecl); ca; ca = CONTRACT_CHAIN (ca)) |
| 2148 |
{ |
| 2149 |
tree contract = CONTRACT_STATEMENT (ca); |
| 2150 |
if (!CONTRACT_CONDITION (contract) |
| 2151 |
|| CONTRACT_CONDITION_DEFERRED_P (contract) |
| 2152 |
|| CONTRACT_CONDITION (contract) == error_mark_node) |
| 2153 |
return; |
| 2154 |
} |
| 2155 |
|
| 2156 |
int flags = SF_DEFAULT | SF_PRE_PARSED; |
| 2157 |
|
| 2158 |
/* If either the pre or post functions are bad, don't bother emitting |
| 2159 |
any contracts. The program is already ill-formed. */ |
| 2160 |
tree pre = DECL_PRE_FN (fndecl); |
| 2161 |
tree post = DECL_POST_FN (fndecl); |
| 2162 |
if (pre == error_mark_node || post == error_mark_node) |
| 2163 |
return; |
| 2164 |
|
| 2165 |
if (pre && DECL_INITIAL (fndecl) != error_mark_node) |
| 2166 |
{ |
| 2167 |
DECL_PENDING_INLINE_P (pre) = false; |
| 2168 |
start_preparsed_function (pre, DECL_ATTRIBUTES (pre), flags); |
| 2169 |
remap_and_emit_conditions (fndecl, pre, PRECONDITION_STMT); |
| 2170 |
tree finished_pre = finish_function (false); |
| 2171 |
expand_or_defer_fn (finished_pre); |
| 2172 |
} |
| 2173 |
|
| 2174 |
if (post && DECL_INITIAL (fndecl) != error_mark_node) |
| 2175 |
{ |
| 2176 |
DECL_PENDING_INLINE_P (post) = false; |
| 2177 |
start_preparsed_function (post, |
| 2178 |
DECL_ATTRIBUTES (post), |
| 2179 |
flags); |
| 2180 |
remap_and_emit_conditions (fndecl, post, POSTCONDITION_STMT); |
| 2181 |
if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (post)))) |
| 2182 |
finish_return_stmt (get_postcondition_result_parameter (fndecl)); |
| 2183 |
|
| 2184 |
tree finished_post = finish_function (false); |
| 2185 |
expand_or_defer_fn (finished_post); |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
|
| 2190 |
/* A subroutine of duplicate_decls. Diagnose issues in the redeclaration of |
| 2191 |
guarded functions. */ |
| 2192 |
|
| 2193 |
void |
| 2194 |
duplicate_contracts (tree newdecl, tree olddecl) |
| 2195 |
{ |
| 2196 |
if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
| 2197 |
newdecl = DECL_TEMPLATE_RESULT (newdecl); |
| 2198 |
if (TREE_CODE (olddecl) == TEMPLATE_DECL) |
| 2199 |
olddecl = DECL_TEMPLATE_RESULT (olddecl); |
| 2200 |
|
| 2201 |
/* Compare contracts to see if they match. */ |
| 2202 |
tree old_contracts = DECL_CONTRACTS (olddecl); |
| 2203 |
tree new_contracts = DECL_CONTRACTS (newdecl); |
| 2204 |
|
| 2205 |
if (!old_contracts && !new_contracts) |
| 2206 |
return; |
| 2207 |
|
| 2208 |
location_t old_loc = DECL_SOURCE_LOCATION (olddecl); |
| 2209 |
location_t new_loc = DECL_SOURCE_LOCATION (newdecl); |
| 2210 |
|
| 2211 |
/* If both declarations specify contracts, ensure they match. |
| 2212 |
|
| 2213 |
TODO: This handles a potential error a little oddly. Consider: |
| 2214 |
|
| 2215 |
struct B { |
| 2216 |
virtual void f(int n) [[pre: n == 0]]; |
| 2217 |
}; |
| 2218 |
struct D : B { |
| 2219 |
void f(int n) override; // inherits contracts |
| 2220 |
}; |
| 2221 |
void D::f(int n) [[pre: n == 0]] // OK |
| 2222 |
{ } |
| 2223 |
|
| 2224 |
It's okay because we're explicitly restating the inherited contract. |
| 2225 |
Changing the precondition on the definition D::f causes match_contracts |
| 2226 |
to complain about the mismatch. |
| 2227 |
|
| 2228 |
This would previously have been diagnosed as adding contracts to an |
| 2229 |
override, but this seems like it should be well-formed. */ |
| 2230 |
if (old_contracts && new_contracts) |
| 2231 |
{ |
| 2232 |
if (!match_contract_conditions (old_loc, old_contracts, |
| 2233 |
new_loc, new_contracts, |
| 2234 |
cmc_declaration)) |
| 2235 |
return; |
| 2236 |
if (DECL_UNIQUE_FRIEND_P (newdecl)) |
| 2237 |
/* Newdecl's contracts are still DEFERRED_PARSE, and we're about to |
| 2238 |
collapse it into olddecl, so stash away olddecl's contracts for |
| 2239 |
later comparison. */ |
| 2240 |
defer_guarded_contract_match (olddecl, olddecl, old_contracts); |
| 2241 |
} |
| 2242 |
|
| 2243 |
/* Handle cases where contracts are omitted in one or the other |
| 2244 |
declaration. */ |
| 2245 |
if (old_contracts) |
| 2246 |
{ |
| 2247 |
/* Contracts have been previously specified by are no omitted. The |
| 2248 |
new declaration inherits the existing contracts. */ |
| 2249 |
if (!new_contracts) |
| 2250 |
copy_contract_attributes (newdecl, olddecl); |
| 2251 |
|
| 2252 |
/* In all cases, remove existing contracts from OLDDECL to prevent the |
| 2253 |
attribute merging function from adding excess contracts. */ |
| 2254 |
remove_contract_attributes (olddecl); |
| 2255 |
} |
| 2256 |
else if (!old_contracts) |
| 2257 |
{ |
| 2258 |
/* We are adding contracts to a declaration. */ |
| 2259 |
if (new_contracts) |
| 2260 |
{ |
| 2261 |
/* We can't add to a previously defined function. */ |
| 2262 |
if (DECL_INITIAL (olddecl)) |
| 2263 |
{ |
| 2264 |
auto_diagnostic_group d; |
| 2265 |
error_at (new_loc, "cannot add contracts after definition"); |
| 2266 |
inform (DECL_SOURCE_LOCATION (olddecl), "original definition here"); |
| 2267 |
return; |
| 2268 |
} |
| 2269 |
|
| 2270 |
/* We can't add to an unguarded virtual function declaration. */ |
| 2271 |
if (DECL_VIRTUAL_P (olddecl) && new_contracts) |
| 2272 |
{ |
| 2273 |
auto_diagnostic_group d; |
| 2274 |
error_at (new_loc, "cannot add contracts to a virtual function"); |
| 2275 |
inform (DECL_SOURCE_LOCATION (olddecl), "original declaration here"); |
| 2276 |
return; |
| 2277 |
} |
| 2278 |
|
| 2279 |
/* Depending on the "first declaration" rule, we may not be able |
| 2280 |
to add contracts to a function after the fact. */ |
| 2281 |
if (flag_contract_strict_declarations) |
| 2282 |
{ |
| 2283 |
warning_at (new_loc, |
| 2284 |
OPT_fcontract_strict_declarations_, |
| 2285 |
"declaration adds contracts to %q#D", |
| 2286 |
olddecl); |
| 2287 |
return; |
| 2288 |
} |
| 2289 |
|
| 2290 |
/* Copy the contracts from NEWDECL to OLDDECL. We shouldn't need to |
| 2291 |
remap them because NEWDECL's parameters will replace those of |
| 2292 |
OLDDECL. Remove the contracts from NEWDECL so they aren't |
| 2293 |
cloned when merging. */ |
| 2294 |
copy_contract_attributes (olddecl, newdecl); |
| 2295 |
remove_contract_attributes (newdecl); |
| 2296 |
} |
| 2297 |
} |
| 2298 |
} |
| 2299 |
|
| 2300 |
/* Replace the any contract attributes on OVERRIDER with a copy where any |
| 2301 |
references to BASEFN's PARM_DECLs have been rewritten to the corresponding |
| 2302 |
PARM_DECL in OVERRIDER. */ |
| 2303 |
|
| 2304 |
void |
| 2305 |
inherit_base_contracts (tree overrider, tree basefn) |
| 2306 |
{ |
| 2307 |
tree last = NULL_TREE, contract_attrs = NULL_TREE; |
| 2308 |
for (tree a = DECL_CONTRACTS (basefn); |
| 2309 |
a != NULL_TREE; |
| 2310 |
a = CONTRACT_CHAIN (a)) |
| 2311 |
{ |
| 2312 |
tree c = copy_node (a); |
| 2313 |
TREE_VALUE (c) = build_tree_list (TREE_PURPOSE (TREE_VALUE (c)), |
| 2314 |
copy_node (CONTRACT_STATEMENT (c))); |
| 2315 |
|
| 2316 |
tree src = basefn; |
| 2317 |
tree dst = overrider; |
| 2318 |
remap_contract (src, dst, CONTRACT_STATEMENT (c), /*duplicate_p=*/true); |
| 2319 |
|
| 2320 |
CONTRACT_COMMENT (CONTRACT_STATEMENT (c)) = |
| 2321 |
copy_node (CONTRACT_COMMENT (CONTRACT_STATEMENT (c))); |
| 2322 |
|
| 2323 |
chainon (last, c); |
| 2324 |
last = c; |
| 2325 |
if (!contract_attrs) |
| 2326 |
contract_attrs = c; |
| 2327 |
} |
| 2328 |
|
| 2329 |
set_decl_contracts (overrider, contract_attrs); |
| 2330 |
} |
| 2331 |
|
| 2332 |
#include "gt-cp-contracts.h" |