| 1 |
/* d-codegen.cc -- Code generation and routines for manipulation of GCC trees. |
| 2 |
Copyright (C) 2006-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
GCC is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
GCC is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GCC; see the file COPYING3. If not see |
| 16 |
<http://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
#include "system.h" |
| 20 |
#include "coretypes.h" |
| 21 |
|
| 22 |
#include "dmd/aggregate.h" |
| 23 |
#include "dmd/ctfe.h" |
| 24 |
#include "dmd/declaration.h" |
| 25 |
#include "dmd/identifier.h" |
| 26 |
#include "dmd/module.h" |
| 27 |
#include "dmd/target.h" |
| 28 |
#include "dmd/template.h" |
| 29 |
|
| 30 |
#include "tree.h" |
| 31 |
#include "tree-iterator.h" |
| 32 |
#include "fold-const.h" |
| 33 |
#include "diagnostic.h" |
| 34 |
#include "langhooks.h" |
| 35 |
#include "target.h" |
| 36 |
#include "stringpool.h" |
| 37 |
#include "varasm.h" |
| 38 |
#include "stor-layout.h" |
| 39 |
#include "attribs.h" |
| 40 |
#include "function.h" |
| 41 |
#include "gimple-expr.h" |
| 42 |
|
| 43 |
#include "d-tree.h" |
| 44 |
|
| 45 |
|
| 46 |
/* Return the GCC location for the D frontend source location LOC. */ |
| 47 |
|
| 48 |
location_t |
| 49 |
make_location_t (const SourceLoc &loc) |
| 50 |
{ |
| 51 |
location_t gcc_location = input_location; |
| 52 |
|
| 53 |
if (loc.filename.length != 0) |
| 54 |
{ |
| 55 |
linemap_add (line_table, LC_ENTER, 0, loc.filename.ptr, loc.line); |
| 56 |
linemap_line_start (line_table, loc.line, 0); |
| 57 |
gcc_location = linemap_position_for_column (line_table, loc.column); |
| 58 |
linemap_add (line_table, LC_LEAVE, 0, NULL, 0); |
| 59 |
} |
| 60 |
|
| 61 |
return gcc_location; |
| 62 |
} |
| 63 |
|
| 64 |
/* Likewise, but converts LOC from a compact opaque location. */ |
| 65 |
|
| 66 |
location_t |
| 67 |
make_location_t (const Loc loc) |
| 68 |
{ |
| 69 |
const SourceLoc sloc = loc.toSourceLoc (); |
| 70 |
return make_location_t (sloc); |
| 71 |
} |
| 72 |
|
| 73 |
/* Return the DECL_CONTEXT for symbol DSYM. */ |
| 74 |
|
| 75 |
tree |
| 76 |
d_decl_context (Dsymbol *dsym) |
| 77 |
{ |
| 78 |
Dsymbol *parent = dsym; |
| 79 |
Declaration *decl = dsym->isDeclaration (); |
| 80 |
AggregateDeclaration *ad = dsym->isAggregateDeclaration (); |
| 81 |
|
| 82 |
while ((parent = parent->toParent2 ())) |
| 83 |
{ |
| 84 |
/* We've reached the top-level module namespace. |
| 85 |
Set DECL_CONTEXT as the NAMESPACE_DECL of the enclosing module, |
| 86 |
but only for extern(D) symbols. */ |
| 87 |
if (parent->isModule ()) |
| 88 |
{ |
| 89 |
if ((decl != NULL && decl->resolvedLinkage () != LINK::d) |
| 90 |
|| (ad != NULL && ad->classKind != ClassKind::d)) |
| 91 |
return NULL_TREE; |
| 92 |
|
| 93 |
return build_import_decl (parent); |
| 94 |
} |
| 95 |
|
| 96 |
/* Declarations marked as `static' or `__gshared' are never |
| 97 |
part of any context except at module level. */ |
| 98 |
if (decl != NULL && decl->isDataseg ()) |
| 99 |
continue; |
| 100 |
|
| 101 |
/* Nested functions. */ |
| 102 |
FuncDeclaration *fd = parent->isFuncDeclaration (); |
| 103 |
if (fd != NULL) |
| 104 |
return get_symbol_decl (fd); |
| 105 |
|
| 106 |
/* Methods of classes or structs. */ |
| 107 |
AggregateDeclaration *ad = parent->isAggregateDeclaration (); |
| 108 |
if (ad != NULL) |
| 109 |
{ |
| 110 |
tree context = build_ctype (ad->type); |
| 111 |
/* Want the underlying RECORD_TYPE. */ |
| 112 |
if (ad->isClassDeclaration ()) |
| 113 |
context = TREE_TYPE (context); |
| 114 |
|
| 115 |
return context; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return NULL_TREE; |
| 120 |
} |
| 121 |
|
| 122 |
/* Return a copy of record TYPE but safe to modify in any way. */ |
| 123 |
|
| 124 |
tree |
| 125 |
copy_aggregate_type (tree type) |
| 126 |
{ |
| 127 |
tree newtype = build_distinct_type_copy (type); |
| 128 |
TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype); |
| 129 |
TYPE_FIELDS (newtype) = copy_list (TYPE_FIELDS (type)); |
| 130 |
|
| 131 |
for (tree f = TYPE_FIELDS (newtype); f; f = DECL_CHAIN (f)) |
| 132 |
DECL_FIELD_CONTEXT (f) = newtype; |
| 133 |
|
| 134 |
return newtype; |
| 135 |
} |
| 136 |
|
| 137 |
/* Return TRUE if declaration DECL is a reference type. */ |
| 138 |
|
| 139 |
bool |
| 140 |
declaration_reference_p (Declaration *decl) |
| 141 |
{ |
| 142 |
Type *tb = decl->type->toBasetype (); |
| 143 |
|
| 144 |
/* Declaration is a reference type. */ |
| 145 |
if (tb->ty == TY::Treference || decl->storage_class & (STCout | STCref)) |
| 146 |
return true; |
| 147 |
|
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
/* Returns the real type for declaration DECL. */ |
| 152 |
|
| 153 |
tree |
| 154 |
declaration_type (Declaration *decl) |
| 155 |
{ |
| 156 |
/* Lazy declarations are converted to delegates. */ |
| 157 |
if (decl->storage_class & STClazy) |
| 158 |
{ |
| 159 |
TypeFunction *tf = TypeFunction::create (NULL, decl->type, |
| 160 |
VARARGnone, LINK::d); |
| 161 |
TypeDelegate *t = TypeDelegate::create (tf); |
| 162 |
return build_ctype (dmd::merge2 (t)); |
| 163 |
} |
| 164 |
|
| 165 |
/* Static array va_list have array->pointer conversions applied. */ |
| 166 |
if (decl->isParameter () && valist_array_p (decl->type)) |
| 167 |
{ |
| 168 |
Type *valist = dmd::pointerTo (decl->type->nextOf ()); |
| 169 |
valist = dmd::castMod (valist, decl->type->mod); |
| 170 |
return build_ctype (valist); |
| 171 |
} |
| 172 |
|
| 173 |
tree type = build_ctype (decl->type); |
| 174 |
|
| 175 |
/* Parameter is passed by reference. */ |
| 176 |
if (declaration_reference_p (decl)) |
| 177 |
return build_reference_type (type); |
| 178 |
|
| 179 |
/* The `this' parameter is always const. */ |
| 180 |
if (decl->isThisDeclaration ()) |
| 181 |
return insert_type_modifiers (type, MODconst); |
| 182 |
|
| 183 |
return type; |
| 184 |
} |
| 185 |
|
| 186 |
/* These should match the Declaration versions above |
| 187 |
Return TRUE if parameter ARG is a reference type. */ |
| 188 |
|
| 189 |
bool |
| 190 |
parameter_reference_p (Parameter *arg) |
| 191 |
{ |
| 192 |
Type *tb = arg->type->toBasetype (); |
| 193 |
|
| 194 |
/* Parameter is a reference type. */ |
| 195 |
if (tb->ty == TY::Treference || arg->storageClass & (STCout | STCref)) |
| 196 |
return true; |
| 197 |
|
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
/* Returns the real type for parameter ARG. */ |
| 202 |
|
| 203 |
tree |
| 204 |
parameter_type (Parameter *arg) |
| 205 |
{ |
| 206 |
/* Lazy parameters are converted to delegates. */ |
| 207 |
if (arg->storageClass & STClazy) |
| 208 |
{ |
| 209 |
TypeFunction *tf = TypeFunction::create (NULL, arg->type, |
| 210 |
VARARGnone, LINK::d); |
| 211 |
TypeDelegate *t = TypeDelegate::create (tf); |
| 212 |
return build_ctype (dmd::merge2 (t)); |
| 213 |
} |
| 214 |
|
| 215 |
/* Static array va_list have array->pointer conversions applied. */ |
| 216 |
if (valist_array_p (arg->type)) |
| 217 |
{ |
| 218 |
Type *valist = dmd::pointerTo (arg->type->nextOf ()); |
| 219 |
valist = dmd::castMod (valist, arg->type->mod); |
| 220 |
return build_ctype (valist); |
| 221 |
} |
| 222 |
|
| 223 |
tree type = build_ctype (arg->type); |
| 224 |
|
| 225 |
/* Parameter is passed by reference. */ |
| 226 |
if (parameter_reference_p (arg)) |
| 227 |
return build_reference_type (type); |
| 228 |
|
| 229 |
/* Pass non-POD structs by invisible reference. */ |
| 230 |
if (TREE_ADDRESSABLE (type)) |
| 231 |
{ |
| 232 |
type = build_reference_type (type); |
| 233 |
/* There are no other pointer to this temporary. */ |
| 234 |
type = build_qualified_type (type, TYPE_QUAL_RESTRICT); |
| 235 |
} |
| 236 |
|
| 237 |
/* Front-end has already taken care of type promotions. */ |
| 238 |
return type; |
| 239 |
} |
| 240 |
|
| 241 |
/* Build INTEGER_CST of type TYPE with the value VALUE. */ |
| 242 |
|
| 243 |
tree |
| 244 |
build_integer_cst (dinteger_t value, tree type) |
| 245 |
{ |
| 246 |
/* The type is error_mark_node, we can't do anything. */ |
| 247 |
if (error_operand_p (type)) |
| 248 |
return type; |
| 249 |
|
| 250 |
return build_int_cst_type (type, value); |
| 251 |
} |
| 252 |
|
| 253 |
/* Build REAL_CST of type TOTYPE with the value VALUE. */ |
| 254 |
|
| 255 |
tree |
| 256 |
build_float_cst (const real_t &value, Type *totype) |
| 257 |
{ |
| 258 |
real_value new_value; |
| 259 |
TypeBasic *tb = totype->isTypeBasic (); |
| 260 |
|
| 261 |
gcc_assert (tb != NULL); |
| 262 |
|
| 263 |
tree type_node = build_ctype (tb); |
| 264 |
real_convert (&new_value, TYPE_MODE (type_node), &value.rv ()); |
| 265 |
|
| 266 |
return build_real (type_node, new_value); |
| 267 |
} |
| 268 |
|
| 269 |
/* Returns the .length component from the D dynamic array EXP. */ |
| 270 |
|
| 271 |
tree |
| 272 |
d_array_length (tree exp) |
| 273 |
{ |
| 274 |
if (error_operand_p (exp)) |
| 275 |
return exp; |
| 276 |
|
| 277 |
gcc_assert (TYPE_DYNAMIC_ARRAY (TREE_TYPE (exp))); |
| 278 |
|
| 279 |
/* Get the back-end type for the array and pick out the array |
| 280 |
length field (assumed to be the first field). */ |
| 281 |
tree len_field = TYPE_FIELDS (TREE_TYPE (exp)); |
| 282 |
return component_ref (exp, len_field); |
| 283 |
} |
| 284 |
|
| 285 |
/* Returns the .ptr component from the D dynamic array EXP. */ |
| 286 |
|
| 287 |
tree |
| 288 |
d_array_ptr (tree exp) |
| 289 |
{ |
| 290 |
if (error_operand_p (exp)) |
| 291 |
return exp; |
| 292 |
|
| 293 |
gcc_assert (TYPE_DYNAMIC_ARRAY (TREE_TYPE (exp))); |
| 294 |
|
| 295 |
/* Get the back-end type for the array and pick out the array |
| 296 |
data pointer field (assumed to be the second field). */ |
| 297 |
tree ptr_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))); |
| 298 |
return component_ref (exp, ptr_field); |
| 299 |
} |
| 300 |
|
| 301 |
/* Returns a constructor for D dynamic array type TYPE of .length LEN |
| 302 |
and .ptr pointing to DATA. */ |
| 303 |
|
| 304 |
tree |
| 305 |
d_array_value (tree type, tree len, tree data) |
| 306 |
{ |
| 307 |
tree len_field, ptr_field; |
| 308 |
vec <constructor_elt, va_gc> *ce = NULL; |
| 309 |
|
| 310 |
gcc_assert (TYPE_DYNAMIC_ARRAY (type)); |
| 311 |
len_field = TYPE_FIELDS (type); |
| 312 |
ptr_field = TREE_CHAIN (len_field); |
| 313 |
|
| 314 |
len = convert (TREE_TYPE (len_field), len); |
| 315 |
data = convert (TREE_TYPE (ptr_field), data); |
| 316 |
|
| 317 |
CONSTRUCTOR_APPEND_ELT (ce, len_field, len); |
| 318 |
CONSTRUCTOR_APPEND_ELT (ce, ptr_field, data); |
| 319 |
|
| 320 |
return build_padded_constructor (type, ce); |
| 321 |
} |
| 322 |
|
| 323 |
/* Returns value representing the array length of expression EXP. |
| 324 |
TYPE could be a dynamic or static array. */ |
| 325 |
|
| 326 |
tree |
| 327 |
get_array_length (tree exp, Type *type) |
| 328 |
{ |
| 329 |
Type *tb = type->toBasetype (); |
| 330 |
|
| 331 |
switch (tb->ty) |
| 332 |
{ |
| 333 |
case TY::Tsarray: |
| 334 |
return size_int (tb->isTypeSArray ()->dim->toUInteger ()); |
| 335 |
|
| 336 |
case TY::Tarray: |
| 337 |
return d_array_length (exp); |
| 338 |
|
| 339 |
default: |
| 340 |
error ("cannot determine the length of a %qs", type->toChars ()); |
| 341 |
return error_mark_node; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/* Create BINFO for a ClassDeclaration's inheritance tree. |
| 346 |
InterfaceDeclaration's are not included. */ |
| 347 |
|
| 348 |
tree |
| 349 |
build_class_binfo (tree super, ClassDeclaration *cd) |
| 350 |
{ |
| 351 |
tree binfo = make_tree_binfo (1); |
| 352 |
tree ctype = build_ctype (cd->type); |
| 353 |
|
| 354 |
/* Want RECORD_TYPE, not POINTER_TYPE. */ |
| 355 |
BINFO_TYPE (binfo) = TREE_TYPE (ctype); |
| 356 |
BINFO_INHERITANCE_CHAIN (binfo) = super; |
| 357 |
BINFO_OFFSET (binfo) = integer_zero_node; |
| 358 |
|
| 359 |
if (cd->baseClass) |
| 360 |
BINFO_BASE_APPEND (binfo, build_class_binfo (binfo, cd->baseClass)); |
| 361 |
|
| 362 |
return binfo; |
| 363 |
} |
| 364 |
|
| 365 |
/* Create BINFO for an InterfaceDeclaration's inheritance tree. |
| 366 |
In order to access all inherited methods in the debugger, |
| 367 |
the entire tree must be described. |
| 368 |
This function makes assumptions about interface layout. */ |
| 369 |
|
| 370 |
tree |
| 371 |
build_interface_binfo (tree super, ClassDeclaration *cd, unsigned &offset) |
| 372 |
{ |
| 373 |
tree binfo = make_tree_binfo (cd->baseclasses->length); |
| 374 |
tree ctype = build_ctype (cd->type); |
| 375 |
|
| 376 |
/* Want RECORD_TYPE, not POINTER_TYPE. */ |
| 377 |
BINFO_TYPE (binfo) = TREE_TYPE (ctype); |
| 378 |
BINFO_INHERITANCE_CHAIN (binfo) = super; |
| 379 |
BINFO_OFFSET (binfo) = size_int (offset * target.ptrsize); |
| 380 |
BINFO_VIRTUAL_P (binfo) = 1; |
| 381 |
|
| 382 |
for (size_t i = 0; i < cd->baseclasses->length; i++, offset++) |
| 383 |
{ |
| 384 |
BaseClass *bc = (*cd->baseclasses)[i]; |
| 385 |
BINFO_BASE_APPEND (binfo, build_interface_binfo (binfo, bc->sym, offset)); |
| 386 |
} |
| 387 |
|
| 388 |
return binfo; |
| 389 |
} |
| 390 |
|
| 391 |
/* Returns the .funcptr component from the D delegate EXP. */ |
| 392 |
|
| 393 |
tree |
| 394 |
delegate_method (tree exp) |
| 395 |
{ |
| 396 |
/* Get the back-end type for the delegate and pick out the funcptr field |
| 397 |
(assumed to be the second field). */ |
| 398 |
gcc_assert (TYPE_DELEGATE (TREE_TYPE (exp))); |
| 399 |
tree method_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))); |
| 400 |
return component_ref (exp, method_field); |
| 401 |
} |
| 402 |
|
| 403 |
/* Returns the .object component from the delegate EXP. */ |
| 404 |
|
| 405 |
tree |
| 406 |
delegate_object (tree exp) |
| 407 |
{ |
| 408 |
/* Get the back-end type for the delegate and pick out the object field |
| 409 |
(assumed to be the first field). */ |
| 410 |
gcc_assert (TYPE_DELEGATE (TREE_TYPE (exp))); |
| 411 |
tree obj_field = TYPE_FIELDS (TREE_TYPE (exp)); |
| 412 |
return component_ref (exp, obj_field); |
| 413 |
} |
| 414 |
|
| 415 |
/* Build a delegate literal of type TYPE whose pointer function is |
| 416 |
METHOD, and hidden object is OBJECT. */ |
| 417 |
|
| 418 |
tree |
| 419 |
build_delegate_cst (tree method, tree object, Type *type) |
| 420 |
{ |
| 421 |
tree ctor = make_node (CONSTRUCTOR); |
| 422 |
tree ctype; |
| 423 |
|
| 424 |
Type *tb = type->toBasetype (); |
| 425 |
if (tb->ty == TY::Tdelegate) |
| 426 |
ctype = build_ctype (type); |
| 427 |
else |
| 428 |
{ |
| 429 |
/* Convert a function method into an anonymous delegate. */ |
| 430 |
ctype = make_struct_type ("delegate()", 2, |
| 431 |
get_identifier ("ptr"), TREE_TYPE (object), |
| 432 |
get_identifier ("funcptr"), TREE_TYPE (method)); |
| 433 |
TYPE_DELEGATE (ctype) = 1; |
| 434 |
} |
| 435 |
|
| 436 |
vec <constructor_elt, va_gc> *ce = NULL; |
| 437 |
CONSTRUCTOR_APPEND_ELT (ce, TYPE_FIELDS (ctype), object); |
| 438 |
CONSTRUCTOR_APPEND_ELT (ce, TREE_CHAIN (TYPE_FIELDS (ctype)), method); |
| 439 |
|
| 440 |
CONSTRUCTOR_ELTS (ctor) = ce; |
| 441 |
TREE_TYPE (ctor) = ctype; |
| 442 |
|
| 443 |
return ctor; |
| 444 |
} |
| 445 |
|
| 446 |
/* Builds a temporary tree to store the CALLEE and OBJECT |
| 447 |
of a method call expression of type TYPE. */ |
| 448 |
|
| 449 |
tree |
| 450 |
build_method_call (tree callee, tree object, Type *type) |
| 451 |
{ |
| 452 |
tree t = build_delegate_cst (callee, object, type); |
| 453 |
METHOD_CALL_EXPR (t) = 1; |
| 454 |
return t; |
| 455 |
} |
| 456 |
|
| 457 |
/* Extract callee and object from T and return in to CALLEE and OBJECT. */ |
| 458 |
|
| 459 |
void |
| 460 |
extract_from_method_call (tree t, tree &callee, tree &object) |
| 461 |
{ |
| 462 |
gcc_assert (METHOD_CALL_EXPR (t)); |
| 463 |
object = CONSTRUCTOR_ELT (t, 0)->value; |
| 464 |
callee = CONSTRUCTOR_ELT (t, 1)->value; |
| 465 |
} |
| 466 |
|
| 467 |
/* Build a typeof(null) constant of type TYPE. Handles certain special case |
| 468 |
conversions, where the underlying type is an aggregate with a nullable |
| 469 |
interior pointer. */ |
| 470 |
|
| 471 |
tree |
| 472 |
build_typeof_null_value (Type *type) |
| 473 |
{ |
| 474 |
Type *tb = type->toBasetype (); |
| 475 |
tree value; |
| 476 |
|
| 477 |
/* For dynamic arrays, set length and pointer fields to zero. */ |
| 478 |
if (tb->ty == TY::Tarray) |
| 479 |
value = d_array_value (build_ctype (type), size_int (0), null_pointer_node); |
| 480 |
|
| 481 |
/* For associative arrays, set the pointer field to null. */ |
| 482 |
else if (tb->ty == TY::Taarray) |
| 483 |
{ |
| 484 |
tree ctype = build_ctype (type); |
| 485 |
gcc_assert (TYPE_ASSOCIATIVE_ARRAY (ctype)); |
| 486 |
|
| 487 |
value = build_constructor_single (ctype, TYPE_FIELDS (ctype), |
| 488 |
null_pointer_node); |
| 489 |
} |
| 490 |
|
| 491 |
/* For delegates, set the frame and function pointer fields to null. */ |
| 492 |
else if (tb->ty == TY::Tdelegate) |
| 493 |
value = build_delegate_cst (null_pointer_node, null_pointer_node, type); |
| 494 |
|
| 495 |
/* Simple zero constant for all other types. */ |
| 496 |
else |
| 497 |
value = build_zero_cst (build_ctype (type)); |
| 498 |
|
| 499 |
TREE_CONSTANT (value) = 1; |
| 500 |
return value; |
| 501 |
} |
| 502 |
|
| 503 |
/* Build a dereference into the virtual table for OBJECT to retrieve |
| 504 |
a function pointer of type FNTYPE at position INDEX. */ |
| 505 |
|
| 506 |
tree |
| 507 |
build_vindex_ref (tree object, tree fntype, size_t index) |
| 508 |
{ |
| 509 |
/* The vtable is the first field. Interface methods are also in the class's |
| 510 |
vtable, so we don't need to convert from a class to an interface. */ |
| 511 |
tree result = build_deref (object); |
| 512 |
result = component_ref (result, TYPE_FIELDS (TREE_TYPE (result))); |
| 513 |
|
| 514 |
gcc_assert (POINTER_TYPE_P (fntype)); |
| 515 |
|
| 516 |
return build_memref (fntype, result, size_int (target.ptrsize * index)); |
| 517 |
} |
| 518 |
|
| 519 |
/* Return TRUE if EXP is a valid lvalue. Lvalue references cannot be |
| 520 |
made into temporaries, otherwise any assignments will be lost. */ |
| 521 |
|
| 522 |
static bool |
| 523 |
lvalue_p (tree exp) |
| 524 |
{ |
| 525 |
const enum tree_code code = TREE_CODE (exp); |
| 526 |
|
| 527 |
switch (code) |
| 528 |
{ |
| 529 |
case SAVE_EXPR: |
| 530 |
return false; |
| 531 |
|
| 532 |
case ARRAY_REF: |
| 533 |
case INDIRECT_REF: |
| 534 |
case VAR_DECL: |
| 535 |
case PARM_DECL: |
| 536 |
case RESULT_DECL: |
| 537 |
return !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (exp)); |
| 538 |
|
| 539 |
case IMAGPART_EXPR: |
| 540 |
case REALPART_EXPR: |
| 541 |
case COMPONENT_REF: |
| 542 |
CASE_CONVERT: |
| 543 |
return lvalue_p (TREE_OPERAND (exp, 0)); |
| 544 |
|
| 545 |
case COND_EXPR: |
| 546 |
return (lvalue_p (TREE_OPERAND (exp, 1) |
| 547 |
? TREE_OPERAND (exp, 1) |
| 548 |
: TREE_OPERAND (exp, 0)) |
| 549 |
&& lvalue_p (TREE_OPERAND (exp, 2))); |
| 550 |
|
| 551 |
case TARGET_EXPR: |
| 552 |
return true; |
| 553 |
|
| 554 |
case COMPOUND_EXPR: |
| 555 |
return lvalue_p (TREE_OPERAND (exp, 1)); |
| 556 |
|
| 557 |
default: |
| 558 |
return false; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
/* Create a SAVE_EXPR if EXP might have unwanted side effects if referenced |
| 563 |
more than once in an expression. */ |
| 564 |
|
| 565 |
tree |
| 566 |
d_save_expr (tree exp) |
| 567 |
{ |
| 568 |
if (TREE_SIDE_EFFECTS (exp)) |
| 569 |
{ |
| 570 |
if (lvalue_p (exp)) |
| 571 |
return stabilize_reference (exp); |
| 572 |
|
| 573 |
return save_expr (exp); |
| 574 |
} |
| 575 |
|
| 576 |
return exp; |
| 577 |
} |
| 578 |
|
| 579 |
/* VALUEP is an expression we want to pre-evaluate or perform a computation on. |
| 580 |
The expression returned by this function is the part whose value we don't |
| 581 |
care about, storing the value in VALUEP. Callers must ensure that the |
| 582 |
returned expression is evaluated before VALUEP. */ |
| 583 |
|
| 584 |
tree |
| 585 |
stabilize_expr (tree *valuep) |
| 586 |
{ |
| 587 |
tree expr = *valuep; |
| 588 |
const enum tree_code code = TREE_CODE (expr); |
| 589 |
tree lhs; |
| 590 |
tree rhs; |
| 591 |
|
| 592 |
switch (code) |
| 593 |
{ |
| 594 |
case COMPOUND_EXPR: |
| 595 |
/* Given ((e1, ...), eN): |
| 596 |
Store the last RHS 'eN' expression in VALUEP. */ |
| 597 |
lhs = TREE_OPERAND (expr, 0); |
| 598 |
rhs = TREE_OPERAND (expr, 1); |
| 599 |
lhs = compound_expr (lhs, stabilize_expr (&rhs)); |
| 600 |
*valuep = rhs; |
| 601 |
return lhs; |
| 602 |
|
| 603 |
default: |
| 604 |
return NULL_TREE; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
/* Return a TARGET_EXPR, initializing the DECL with EXP. */ |
| 609 |
|
| 610 |
tree |
| 611 |
build_target_expr (tree decl, tree exp) |
| 612 |
{ |
| 613 |
tree type = TREE_TYPE (decl); |
| 614 |
tree result = build4 (TARGET_EXPR, type, decl, exp, NULL_TREE, NULL_TREE); |
| 615 |
|
| 616 |
if (EXPR_HAS_LOCATION (exp)) |
| 617 |
SET_EXPR_LOCATION (result, EXPR_LOCATION (exp)); |
| 618 |
|
| 619 |
/* If decl must always reside in memory. */ |
| 620 |
if (TREE_ADDRESSABLE (type)) |
| 621 |
d_mark_addressable (decl); |
| 622 |
|
| 623 |
/* Always set TREE_SIDE_EFFECTS so that expand_expr does not ignore the |
| 624 |
TARGET_EXPR. If there really turn out to be no side effects, then the |
| 625 |
optimizer should be able to remove it. */ |
| 626 |
TREE_SIDE_EFFECTS (result) = 1; |
| 627 |
|
| 628 |
return result; |
| 629 |
} |
| 630 |
|
| 631 |
/* Like the above function, but initializes a new temporary. */ |
| 632 |
|
| 633 |
tree |
| 634 |
force_target_expr (tree exp) |
| 635 |
{ |
| 636 |
tree decl = create_tmp_var_raw (TREE_TYPE (exp)); |
| 637 |
DECL_CONTEXT (decl) = current_function_decl; |
| 638 |
layout_decl (decl, 0); |
| 639 |
|
| 640 |
return build_target_expr (decl, exp); |
| 641 |
} |
| 642 |
|
| 643 |
/* Determine whether expression EXP can have a copy of its value elided. */ |
| 644 |
|
| 645 |
static bool |
| 646 |
can_elide_copy_p (Expression *exp) |
| 647 |
{ |
| 648 |
/* Explicit `__rvalue(exp)'. */ |
| 649 |
if (exp->rvalue ()) |
| 650 |
return true; |
| 651 |
|
| 652 |
/* Look for variable expression. */ |
| 653 |
Expression *last = exp; |
| 654 |
while (CommaExp *ce = last->isCommaExp ()) |
| 655 |
last = ce->e2; |
| 656 |
|
| 657 |
if (VarExp *ve = last->isVarExp ()) |
| 658 |
{ |
| 659 |
if (VarDeclaration *vd = ve->var->isVarDeclaration ()) |
| 660 |
{ |
| 661 |
/* Variable is an implicit copy of an lvalue. */ |
| 662 |
if (vd->storage_class & STCrvalue) |
| 663 |
return true; |
| 664 |
|
| 665 |
/* The destructor is going to run on the variable. */ |
| 666 |
if (vd->isArgDtorVar ()) |
| 667 |
return true; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
return false; |
| 672 |
} |
| 673 |
|
| 674 |
/* Returns the address of the expression EXP. */ |
| 675 |
|
| 676 |
tree |
| 677 |
build_address (tree exp) |
| 678 |
{ |
| 679 |
if (error_operand_p (exp)) |
| 680 |
return exp; |
| 681 |
|
| 682 |
tree ptrtype; |
| 683 |
tree type = TREE_TYPE (exp); |
| 684 |
|
| 685 |
if (TREE_CODE (exp) == STRING_CST) |
| 686 |
{ |
| 687 |
/* Just convert string literals (char[]) to C-style strings (char *), |
| 688 |
otherwise the latter method (char[]*) causes conversion problems |
| 689 |
during gimplification. */ |
| 690 |
ptrtype = build_pointer_type (TREE_TYPE (type)); |
| 691 |
} |
| 692 |
else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (va_list_type_node) |
| 693 |
&& TREE_CODE (TYPE_MAIN_VARIANT (type)) == ARRAY_TYPE) |
| 694 |
{ |
| 695 |
/* Special case for va_list, allow arrays to decay to a pointer. */ |
| 696 |
ptrtype = build_pointer_type (TREE_TYPE (type)); |
| 697 |
} |
| 698 |
else |
| 699 |
ptrtype = build_pointer_type (type); |
| 700 |
|
| 701 |
/* Maybe rewrite: &(e1, e2) => (e1, &e2). */ |
| 702 |
tree init = stabilize_expr (&exp); |
| 703 |
|
| 704 |
/* Can't take the address of a manifest constant, instead use its value. */ |
| 705 |
if (TREE_CODE (exp) == CONST_DECL) |
| 706 |
exp = DECL_INITIAL (exp); |
| 707 |
|
| 708 |
/* Type `noreturn' has no storage, return `null' for the expression. */ |
| 709 |
if (DECL_P (exp) && TREE_CODE (exp) != FIELD_DECL |
| 710 |
&& TYPE_MAIN_VARIANT (TREE_TYPE (exp)) == noreturn_type_node) |
| 711 |
return compound_expr (init, null_pointer_node); |
| 712 |
|
| 713 |
/* Some expression lowering may request an address of a compile-time constant, |
| 714 |
or other non-lvalue expression. Make sure it is assigned to a location we |
| 715 |
can reference. */ |
| 716 |
if (CONSTANT_CLASS_P (exp) && TREE_CODE (exp) != STRING_CST) |
| 717 |
exp = force_target_expr (exp); |
| 718 |
else if (TREE_CODE (exp) == CALL_EXPR) |
| 719 |
{ |
| 720 |
/* When a struct or array is returned in registers, we need to again fill |
| 721 |
in all alignment holes. */ |
| 722 |
if (AGGREGATE_TYPE_P (TREE_TYPE (exp)) |
| 723 |
&& !aggregate_value_p (TREE_TYPE (exp), exp)) |
| 724 |
{ |
| 725 |
tree tmp = build_local_temp (TREE_TYPE (exp)); |
| 726 |
init = compound_expr (init, build_memset_call (tmp)); |
| 727 |
init = compound_expr (init, modify_expr (tmp, exp)); |
| 728 |
exp = tmp; |
| 729 |
} |
| 730 |
else |
| 731 |
exp = force_target_expr (exp); |
| 732 |
} |
| 733 |
|
| 734 |
d_mark_addressable (exp); |
| 735 |
exp = build_fold_addr_expr_with_type_loc (input_location, exp, ptrtype); |
| 736 |
|
| 737 |
if (TREE_CODE (exp) == ADDR_EXPR) |
| 738 |
TREE_NO_TRAMPOLINE (exp) = 1; |
| 739 |
|
| 740 |
return compound_expr (init, exp); |
| 741 |
} |
| 742 |
|
| 743 |
/* Mark EXP saying that we need to be able to take the address of it; it should |
| 744 |
not be allocated in a register. When COMPLAIN is true, issue an error if we |
| 745 |
are marking a register variable. */ |
| 746 |
|
| 747 |
tree |
| 748 |
d_mark_addressable (tree exp, bool complain) |
| 749 |
{ |
| 750 |
switch (TREE_CODE (exp)) |
| 751 |
{ |
| 752 |
case COMPONENT_REF: |
| 753 |
if (complain && DECL_BIT_FIELD (TREE_OPERAND (exp, 1))) |
| 754 |
error ("cannot take address of bit-field %qD", TREE_OPERAND (exp, 1)); |
| 755 |
|
| 756 |
/* Fall through. */ |
| 757 |
case ADDR_EXPR: |
| 758 |
case ARRAY_REF: |
| 759 |
case REALPART_EXPR: |
| 760 |
case IMAGPART_EXPR: |
| 761 |
d_mark_addressable (TREE_OPERAND (exp, 0)); |
| 762 |
break; |
| 763 |
|
| 764 |
case VAR_DECL: |
| 765 |
if (complain && DECL_REGISTER (exp)) |
| 766 |
{ |
| 767 |
if (DECL_HARD_REGISTER (exp) || DECL_EXTERNAL (exp)) |
| 768 |
error ("address of explicit register variable %qD requested", exp); |
| 769 |
else |
| 770 |
error ("address of register variable %qD requested", exp); |
| 771 |
} |
| 772 |
|
| 773 |
/* Fall through. */ |
| 774 |
case PARM_DECL: |
| 775 |
case RESULT_DECL: |
| 776 |
case CONST_DECL: |
| 777 |
case FUNCTION_DECL: |
| 778 |
if (!VAR_P (exp) || !DECL_HARD_REGISTER (exp)) |
| 779 |
TREE_ADDRESSABLE (exp) = 1; |
| 780 |
break; |
| 781 |
|
| 782 |
case CONSTRUCTOR: |
| 783 |
TREE_ADDRESSABLE (exp) = 1; |
| 784 |
break; |
| 785 |
|
| 786 |
case TARGET_EXPR: |
| 787 |
TREE_ADDRESSABLE (exp) = 1; |
| 788 |
d_mark_addressable (TREE_OPERAND (exp, 0)); |
| 789 |
break; |
| 790 |
|
| 791 |
default: |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
return exp; |
| 796 |
} |
| 797 |
|
| 798 |
/* Mark EXP as "used" in the program for the benefit of |
| 799 |
-Wunused warning purposes. */ |
| 800 |
|
| 801 |
tree |
| 802 |
d_mark_used (tree exp) |
| 803 |
{ |
| 804 |
switch (TREE_CODE (exp)) |
| 805 |
{ |
| 806 |
case VAR_DECL: |
| 807 |
case CONST_DECL: |
| 808 |
case PARM_DECL: |
| 809 |
case RESULT_DECL: |
| 810 |
case FUNCTION_DECL: |
| 811 |
TREE_USED (exp) = 1; |
| 812 |
break; |
| 813 |
|
| 814 |
case ARRAY_REF: |
| 815 |
case COMPONENT_REF: |
| 816 |
case MODIFY_EXPR: |
| 817 |
case REALPART_EXPR: |
| 818 |
case IMAGPART_EXPR: |
| 819 |
case NOP_EXPR: |
| 820 |
case CONVERT_EXPR: |
| 821 |
case ADDR_EXPR: |
| 822 |
d_mark_used (TREE_OPERAND (exp, 0)); |
| 823 |
break; |
| 824 |
|
| 825 |
case COMPOUND_EXPR: |
| 826 |
d_mark_used (TREE_OPERAND (exp, 0)); |
| 827 |
d_mark_used (TREE_OPERAND (exp, 1)); |
| 828 |
break; |
| 829 |
|
| 830 |
default: |
| 831 |
break; |
| 832 |
} |
| 833 |
return exp; |
| 834 |
} |
| 835 |
|
| 836 |
/* Mark EXP as read, not just set, for set but not used -Wunused |
| 837 |
warning purposes. */ |
| 838 |
|
| 839 |
tree |
| 840 |
d_mark_read (tree exp) |
| 841 |
{ |
| 842 |
switch (TREE_CODE (exp)) |
| 843 |
{ |
| 844 |
case VAR_DECL: |
| 845 |
case PARM_DECL: |
| 846 |
TREE_USED (exp) = 1; |
| 847 |
DECL_READ_P (exp) = 1; |
| 848 |
break; |
| 849 |
|
| 850 |
case ARRAY_REF: |
| 851 |
case COMPONENT_REF: |
| 852 |
case MODIFY_EXPR: |
| 853 |
case REALPART_EXPR: |
| 854 |
case IMAGPART_EXPR: |
| 855 |
case NOP_EXPR: |
| 856 |
case CONVERT_EXPR: |
| 857 |
case ADDR_EXPR: |
| 858 |
d_mark_read (TREE_OPERAND (exp, 0)); |
| 859 |
break; |
| 860 |
|
| 861 |
case COMPOUND_EXPR: |
| 862 |
d_mark_read (TREE_OPERAND (exp, 1)); |
| 863 |
break; |
| 864 |
|
| 865 |
default: |
| 866 |
break; |
| 867 |
} |
| 868 |
return exp; |
| 869 |
} |
| 870 |
|
| 871 |
/* Build a call to memcmp(), compares the first NUM bytes of PTR1 with PTR2. */ |
| 872 |
|
| 873 |
tree |
| 874 |
build_memcmp_call (tree ptr1, tree ptr2, tree num) |
| 875 |
{ |
| 876 |
return build_call_expr (builtin_decl_explicit (BUILT_IN_MEMCMP), 3, |
| 877 |
ptr1, ptr2, num); |
| 878 |
} |
| 879 |
|
| 880 |
/* Build a call to memcpy(), copies the first NUM bytes of SRC into DST. */ |
| 881 |
|
| 882 |
tree |
| 883 |
build_memcpy_call (tree dst, tree src, tree num) |
| 884 |
{ |
| 885 |
return build_call_expr (builtin_decl_explicit (BUILT_IN_MEMCPY), 3, |
| 886 |
dst, src, num); |
| 887 |
} |
| 888 |
|
| 889 |
/* Build a call to memset(), fills the first NUM bytes of PTR with zeros. |
| 890 |
If NUM is NULL, then we expect PTR to be object that requires filling. */ |
| 891 |
|
| 892 |
tree |
| 893 |
build_memset_call (tree ptr, tree num) |
| 894 |
{ |
| 895 |
if (num == NULL_TREE) |
| 896 |
{ |
| 897 |
gcc_assert (TREE_CODE (ptr) != ADDR_EXPR); |
| 898 |
num = TYPE_SIZE_UNIT (TREE_TYPE (ptr)); |
| 899 |
ptr = build_address (ptr); |
| 900 |
} |
| 901 |
|
| 902 |
/* Use a zero constant to fill the destination if setting the entire object. |
| 903 |
For CONSTRUCTORs, the memcpy() is lowered to a ref-all pointer assignment, |
| 904 |
which can then be merged with other stores to the object. */ |
| 905 |
tree valtype = TREE_TYPE (TREE_TYPE (ptr)); |
| 906 |
if (tree_int_cst_equal (TYPE_SIZE_UNIT (valtype), num)) |
| 907 |
{ |
| 908 |
tree cst = build_zero_cst (valtype); |
| 909 |
if (TREE_CODE (cst) == CONSTRUCTOR) |
| 910 |
{ |
| 911 |
CONSTRUCTOR_ZERO_PADDING_BITS (cst) = 1; |
| 912 |
return build_memcpy_call (ptr, build_address (cst), num); |
| 913 |
} |
| 914 |
|
| 915 |
return modify_expr (build_deref (ptr), cst); |
| 916 |
} |
| 917 |
|
| 918 |
return build_call_expr (builtin_decl_explicit (BUILT_IN_MEMSET), 3, |
| 919 |
ptr, integer_zero_node, num); |
| 920 |
} |
| 921 |
|
| 922 |
/* Return TRUE if the struct SD is suitable for comparison using memcmp. |
| 923 |
This is because we don't guarantee that padding is zero-initialized for |
| 924 |
a stack variable, so we can't use memcmp to compare struct values. */ |
| 925 |
|
| 926 |
bool |
| 927 |
identity_compare_p (StructDeclaration *sd) |
| 928 |
{ |
| 929 |
if (sd->isUnionDeclaration ()) |
| 930 |
return true; |
| 931 |
|
| 932 |
unsigned offset = 0; |
| 933 |
|
| 934 |
for (size_t i = 0; i < sd->fields.length; i++) |
| 935 |
{ |
| 936 |
VarDeclaration *vd = sd->fields[i]; |
| 937 |
Type *tb = vd->type->toBasetype (); |
| 938 |
|
| 939 |
/* Check inner data structures. */ |
| 940 |
if (TypeStruct *ts = tb->isTypeStruct ()) |
| 941 |
{ |
| 942 |
if (!identity_compare_p (ts->sym)) |
| 943 |
return false; |
| 944 |
} |
| 945 |
|
| 946 |
/* Check for types that may have padding. */ |
| 947 |
if ((tb->ty == TY::Tcomplex80 |
| 948 |
|| tb->ty == TY::Tfloat80 |
| 949 |
|| tb->ty == TY::Timaginary80) |
| 950 |
&& target.realpad != 0) |
| 951 |
return false; |
| 952 |
|
| 953 |
if (offset <= vd->offset) |
| 954 |
{ |
| 955 |
/* There's a hole in the struct. */ |
| 956 |
if (offset != vd->offset) |
| 957 |
return false; |
| 958 |
|
| 959 |
offset += dmd::size (vd->type); |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
/* Any trailing padding may not be zero. */ |
| 964 |
if (offset < sd->structsize) |
| 965 |
return false; |
| 966 |
|
| 967 |
return true; |
| 968 |
} |
| 969 |
|
| 970 |
/* Build a floating-point identity comparison between T1 and T2, ignoring any |
| 971 |
excessive padding in the type. CODE is EQ_EXPR or NE_EXPR comparison. */ |
| 972 |
|
| 973 |
tree |
| 974 |
build_float_identity (tree_code code, tree t1, tree t2) |
| 975 |
{ |
| 976 |
tree size = size_int (TYPE_PRECISION (TREE_TYPE (t1)) / BITS_PER_UNIT); |
| 977 |
tree result = build_memcmp_call (build_address (t1), |
| 978 |
build_address (t2), size); |
| 979 |
return build_boolop (code, result, integer_zero_node); |
| 980 |
} |
| 981 |
|
| 982 |
/* Lower a field-by-field equality expression between T1 and T2 of type SD. |
| 983 |
CODE is the EQ_EXPR or NE_EXPR comparison. */ |
| 984 |
|
| 985 |
static tree |
| 986 |
lower_struct_comparison (tree_code code, StructDeclaration *sd, |
| 987 |
tree t1, tree t2) |
| 988 |
{ |
| 989 |
tree_code tcode = (code == EQ_EXPR) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR; |
| 990 |
tree tmemcmp = NULL_TREE; |
| 991 |
|
| 992 |
/* We can skip the compare if the structs are empty. */ |
| 993 |
if (sd->fields.length == 0) |
| 994 |
{ |
| 995 |
tmemcmp = build_boolop (code, integer_zero_node, integer_zero_node); |
| 996 |
if (TREE_SIDE_EFFECTS (t2)) |
| 997 |
tmemcmp = compound_expr (t2, tmemcmp); |
| 998 |
if (TREE_SIDE_EFFECTS (t1)) |
| 999 |
tmemcmp = compound_expr (t1, tmemcmp); |
| 1000 |
|
| 1001 |
return tmemcmp; |
| 1002 |
} |
| 1003 |
|
| 1004 |
/* Let back-end take care of union comparisons. */ |
| 1005 |
if (sd->isUnionDeclaration ()) |
| 1006 |
{ |
| 1007 |
tmemcmp = build_memcmp_call (build_address (t1), build_address (t2), |
| 1008 |
size_int (sd->structsize)); |
| 1009 |
return build_boolop (code, tmemcmp, integer_zero_node); |
| 1010 |
} |
| 1011 |
|
| 1012 |
for (size_t i = 0; i < sd->fields.length; i++) |
| 1013 |
{ |
| 1014 |
VarDeclaration *vd = sd->fields[i]; |
| 1015 |
Type *type = vd->type->toBasetype (); |
| 1016 |
tree sfield = get_symbol_decl (vd); |
| 1017 |
|
| 1018 |
tree t1ref = component_ref (t1, sfield); |
| 1019 |
tree t2ref = component_ref (t2, sfield); |
| 1020 |
tree tcmp; |
| 1021 |
|
| 1022 |
if (TypeStruct *ts = type->isTypeStruct ()) |
| 1023 |
{ |
| 1024 |
/* Compare inner data structures. */ |
| 1025 |
tcmp = lower_struct_comparison (code, ts->sym, t1ref, t2ref); |
| 1026 |
} |
| 1027 |
else if (type->ty != TY::Tvector && type->isIntegral ()) |
| 1028 |
{ |
| 1029 |
/* Integer comparison, no special handling required. */ |
| 1030 |
tcmp = build_boolop (code, t1ref, t2ref); |
| 1031 |
} |
| 1032 |
else if (type->ty != TY::Tvector && type->isFloating ()) |
| 1033 |
{ |
| 1034 |
/* Floating-point comparison, don't compare padding in type. */ |
| 1035 |
if (!type->isComplex ()) |
| 1036 |
tcmp = build_float_identity (code, t1ref, t2ref); |
| 1037 |
else |
| 1038 |
{ |
| 1039 |
tree req = build_float_identity (code, real_part (t1ref), |
| 1040 |
real_part (t2ref)); |
| 1041 |
tree ieq = build_float_identity (code, imaginary_part (t1ref), |
| 1042 |
imaginary_part (t2ref)); |
| 1043 |
|
| 1044 |
tcmp = build_boolop (tcode, req, ieq); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
else |
| 1048 |
{ |
| 1049 |
tree stype = build_ctype (type); |
| 1050 |
opt_scalar_int_mode mode = int_mode_for_mode (TYPE_MODE (stype)); |
| 1051 |
|
| 1052 |
if (mode.exists ()) |
| 1053 |
{ |
| 1054 |
/* Compare field bits as their corresponding integer type. |
| 1055 |
*((T*) &t1) == *((T*) &t2) */ |
| 1056 |
tree tmode = lang_hooks.types.type_for_mode (mode.require (), 1); |
| 1057 |
|
| 1058 |
if (tmode == NULL_TREE) |
| 1059 |
tmode = make_unsigned_type (GET_MODE_BITSIZE (mode.require ())); |
| 1060 |
|
| 1061 |
tmode = build_aligned_type (tmode, TYPE_ALIGN (stype)); |
| 1062 |
t1ref = build_vconvert (tmode, t1ref); |
| 1063 |
t2ref = build_vconvert (tmode, t2ref); |
| 1064 |
|
| 1065 |
tcmp = build_boolop (code, t1ref, t2ref); |
| 1066 |
} |
| 1067 |
else |
| 1068 |
{ |
| 1069 |
/* Simple memcmp between types. */ |
| 1070 |
tcmp = build_memcmp_call (build_address (t1ref), |
| 1071 |
build_address (t2ref), |
| 1072 |
TYPE_SIZE_UNIT (stype)); |
| 1073 |
tcmp = build_boolop (code, tcmp, integer_zero_node); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
tmemcmp = (tmemcmp) ? build_boolop (tcode, tmemcmp, tcmp) : tcmp; |
| 1078 |
} |
| 1079 |
|
| 1080 |
return tmemcmp; |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
/* Build an equality expression between two RECORD_TYPES T1 and T2 of type SD. |
| 1085 |
If possible, use memcmp, otherwise field-by-field comparison is done. |
| 1086 |
CODE is the EQ_EXPR or NE_EXPR comparison. */ |
| 1087 |
|
| 1088 |
tree |
| 1089 |
build_struct_comparison (tree_code code, StructDeclaration *sd, |
| 1090 |
tree t1, tree t2) |
| 1091 |
{ |
| 1092 |
/* We can skip the compare if the structs are empty. */ |
| 1093 |
if (sd->fields.length == 0) |
| 1094 |
{ |
| 1095 |
tree exp = build_boolop (code, integer_zero_node, integer_zero_node); |
| 1096 |
if (TREE_SIDE_EFFECTS (t2)) |
| 1097 |
exp = compound_expr (t2, exp); |
| 1098 |
if (TREE_SIDE_EFFECTS (t1)) |
| 1099 |
exp = compound_expr (t1, exp); |
| 1100 |
|
| 1101 |
return exp; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/* Make temporaries to prevent multiple evaluations. */ |
| 1105 |
tree t1init = stabilize_expr (&t1); |
| 1106 |
tree t2init = stabilize_expr (&t2); |
| 1107 |
tree result; |
| 1108 |
|
| 1109 |
t1 = d_save_expr (t1); |
| 1110 |
t2 = d_save_expr (t2); |
| 1111 |
|
| 1112 |
/* Bitwise comparison of structs not returned in memory may not work |
| 1113 |
due to data holes loosing its zero padding upon return. |
| 1114 |
As a heuristic, small structs are not compared using memcmp either. */ |
| 1115 |
if (TYPE_MODE (TREE_TYPE (t1)) != BLKmode || !identity_compare_p (sd)) |
| 1116 |
result = lower_struct_comparison (code, sd, t1, t2); |
| 1117 |
else |
| 1118 |
{ |
| 1119 |
/* Do bit compare of structs. */ |
| 1120 |
tree tmemcmp = build_memcmp_call (build_address (t1), build_address (t2), |
| 1121 |
size_int (sd->structsize)); |
| 1122 |
result = build_boolop (code, tmemcmp, integer_zero_node); |
| 1123 |
} |
| 1124 |
|
| 1125 |
return compound_expr (compound_expr (t1init, t2init), result); |
| 1126 |
} |
| 1127 |
|
| 1128 |
/* Build an equality expression between two ARRAY_TYPES of size LENGTH. |
| 1129 |
The pointer references are T1 and T2, and the element type is SD. |
| 1130 |
CODE is the EQ_EXPR or NE_EXPR comparison. */ |
| 1131 |
|
| 1132 |
tree |
| 1133 |
build_array_struct_comparison (tree_code code, StructDeclaration *sd, |
| 1134 |
tree length, tree t1, tree t2) |
| 1135 |
{ |
| 1136 |
tree_code tcode = (code == EQ_EXPR) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR; |
| 1137 |
|
| 1138 |
/* Build temporary for the result of the comparison. |
| 1139 |
Initialize as either 0 or 1 depending on operation. */ |
| 1140 |
tree result = build_local_temp (d_bool_type); |
| 1141 |
tree init = build_boolop (code, integer_zero_node, integer_zero_node); |
| 1142 |
add_stmt (build_assign (INIT_EXPR, result, init)); |
| 1143 |
|
| 1144 |
/* Cast pointer-to-array to pointer-to-struct. */ |
| 1145 |
tree ptrtype = build_ctype (dmd::pointerTo (sd->type)); |
| 1146 |
tree lentype = TREE_TYPE (length); |
| 1147 |
|
| 1148 |
push_binding_level (level_block); |
| 1149 |
push_stmt_list (); |
| 1150 |
|
| 1151 |
/* Build temporary locals for length and pointers. */ |
| 1152 |
tree t = build_local_temp (size_type_node); |
| 1153 |
add_stmt (build_assign (INIT_EXPR, t, length)); |
| 1154 |
length = t; |
| 1155 |
|
| 1156 |
t = build_local_temp (ptrtype); |
| 1157 |
add_stmt (build_assign (INIT_EXPR, t, d_convert (ptrtype, t1))); |
| 1158 |
t1 = t; |
| 1159 |
|
| 1160 |
t = build_local_temp (ptrtype); |
| 1161 |
add_stmt (build_assign (INIT_EXPR, t, d_convert (ptrtype, t2))); |
| 1162 |
t2 = t; |
| 1163 |
|
| 1164 |
/* Build loop for comparing each element. */ |
| 1165 |
push_stmt_list (); |
| 1166 |
|
| 1167 |
/* Exit logic for the loop. |
| 1168 |
if (length == 0 || result OP 0) break; */ |
| 1169 |
t = build_boolop (EQ_EXPR, length, d_convert (lentype, integer_zero_node)); |
| 1170 |
t = build_boolop (TRUTH_ORIF_EXPR, t, build_boolop (code, result, |
| 1171 |
d_bool_false_node)); |
| 1172 |
t = build1 (EXIT_EXPR, void_type_node, t); |
| 1173 |
add_stmt (t); |
| 1174 |
|
| 1175 |
/* Do comparison, caching the value. |
| 1176 |
result = result OP (*t1 == *t2); */ |
| 1177 |
t = build_struct_comparison (code, sd, build_deref (t1), build_deref (t2)); |
| 1178 |
t = build_boolop (tcode, result, t); |
| 1179 |
t = modify_expr (result, t); |
| 1180 |
add_stmt (t); |
| 1181 |
|
| 1182 |
/* Move both pointers to next element position. |
| 1183 |
t1++, t2++; */ |
| 1184 |
tree size = d_convert (ptrtype, TYPE_SIZE_UNIT (TREE_TYPE (ptrtype))); |
| 1185 |
t = build2 (POSTINCREMENT_EXPR, ptrtype, t1, size); |
| 1186 |
add_stmt (t); |
| 1187 |
t = build2 (POSTINCREMENT_EXPR, ptrtype, t2, size); |
| 1188 |
add_stmt (t); |
| 1189 |
|
| 1190 |
/* Decrease loop counter. |
| 1191 |
length -= 1; */ |
| 1192 |
t = build2 (POSTDECREMENT_EXPR, lentype, length, |
| 1193 |
d_convert (lentype, integer_one_node)); |
| 1194 |
add_stmt (t); |
| 1195 |
|
| 1196 |
/* Pop statements and finish loop. */ |
| 1197 |
tree body = pop_stmt_list (); |
| 1198 |
add_stmt (build1 (LOOP_EXPR, void_type_node, body)); |
| 1199 |
|
| 1200 |
/* Wrap it up into a bind expression. */ |
| 1201 |
tree stmt_list = pop_stmt_list (); |
| 1202 |
tree block = pop_binding_level (); |
| 1203 |
|
| 1204 |
body = build3 (BIND_EXPR, void_type_node, |
| 1205 |
BLOCK_VARS (block), stmt_list, block); |
| 1206 |
|
| 1207 |
return compound_expr (body, result); |
| 1208 |
} |
| 1209 |
|
| 1210 |
/* Build a constructor for a variable of aggregate type TYPE using the |
| 1211 |
initializer INIT, an ordered flat list of fields and values provided |
| 1212 |
by the frontend. The returned constructor should be a value that |
| 1213 |
matches the layout of TYPE. */ |
| 1214 |
|
| 1215 |
tree |
| 1216 |
build_struct_literal (tree type, vec <constructor_elt, va_gc> *init) |
| 1217 |
{ |
| 1218 |
/* If the initializer was empty, use default zero initialization. */ |
| 1219 |
if (vec_safe_is_empty (init)) |
| 1220 |
return build_padded_constructor (type, NULL); |
| 1221 |
|
| 1222 |
/* Struct literals can be seen for special enums representing `_Complex', |
| 1223 |
make sure to reinterpret the literal as the correct type. */ |
| 1224 |
if (COMPLEX_FLOAT_TYPE_P (type)) |
| 1225 |
{ |
| 1226 |
gcc_assert (vec_safe_length (init) == 2); |
| 1227 |
return complex_expr (type, (*init)[0].value, (*init)[1].value); |
| 1228 |
} |
| 1229 |
|
| 1230 |
vec <constructor_elt, va_gc> *ve = NULL; |
| 1231 |
HOST_WIDE_INT bitoffset = 0; |
| 1232 |
bool constant_p = true; |
| 1233 |
bool finished = false; |
| 1234 |
|
| 1235 |
/* Walk through each field, matching our initializer list. */ |
| 1236 |
for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
| 1237 |
{ |
| 1238 |
bool is_initialized = false; |
| 1239 |
tree value; |
| 1240 |
|
| 1241 |
if (DECL_NAME (field) == NULL_TREE |
| 1242 |
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)) |
| 1243 |
&& ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| 1244 |
{ |
| 1245 |
/* Search all nesting aggregates, if nothing is found, then |
| 1246 |
this will return an empty initializer to fill the hole. */ |
| 1247 |
value = build_struct_literal (TREE_TYPE (field), init); |
| 1248 |
|
| 1249 |
if (!initializer_zerop (value)) |
| 1250 |
is_initialized = true; |
| 1251 |
} |
| 1252 |
else |
| 1253 |
{ |
| 1254 |
/* Search for the value to initialize the next field. Once found, |
| 1255 |
pop it from the init list so we don't look at it again. */ |
| 1256 |
unsigned HOST_WIDE_INT idx; |
| 1257 |
tree index; |
| 1258 |
|
| 1259 |
FOR_EACH_CONSTRUCTOR_ELT (init, idx, index, value) |
| 1260 |
{ |
| 1261 |
/* If the index is NULL, then just assign it to the next field. |
| 1262 |
This comes from layout_typeinfo(), which generates a flat |
| 1263 |
list of values that we must shape into the record type. */ |
| 1264 |
if (index == field || index == NULL_TREE) |
| 1265 |
{ |
| 1266 |
init->ordered_remove (idx); |
| 1267 |
if (!finished) |
| 1268 |
is_initialized = true; |
| 1269 |
break; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (is_initialized) |
| 1275 |
{ |
| 1276 |
HOST_WIDE_INT fieldpos = int_bit_position (field); |
| 1277 |
gcc_assert (value != NULL_TREE); |
| 1278 |
|
| 1279 |
/* Must not initialize fields that overlap. */ |
| 1280 |
if (fieldpos < bitoffset) |
| 1281 |
{ |
| 1282 |
/* Find the nearest user defined type and field. */ |
| 1283 |
tree vtype = type; |
| 1284 |
while (ANON_AGGR_TYPE_P (vtype)) |
| 1285 |
vtype = TYPE_CONTEXT (vtype); |
| 1286 |
|
| 1287 |
tree vfield = field; |
| 1288 |
if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (vfield)) |
| 1289 |
&& ANON_AGGR_TYPE_P (TREE_TYPE (vfield))) |
| 1290 |
vfield = TYPE_FIELDS (TREE_TYPE (vfield)); |
| 1291 |
|
| 1292 |
/* Must not generate errors for compiler generated fields. */ |
| 1293 |
gcc_assert (TYPE_NAME (vtype) && DECL_NAME (vfield)); |
| 1294 |
error ("overlapping initializer for field %qT.%qD", |
| 1295 |
TYPE_NAME (vtype), DECL_NAME (vfield)); |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (!TREE_CONSTANT (value)) |
| 1299 |
constant_p = false; |
| 1300 |
|
| 1301 |
CONSTRUCTOR_APPEND_ELT (ve, field, value); |
| 1302 |
|
| 1303 |
/* For unions, only the first field is initialized, any other field |
| 1304 |
initializers found for this union are drained and ignored. */ |
| 1305 |
if (TREE_CODE (type) == UNION_TYPE) |
| 1306 |
finished = true; |
| 1307 |
} |
| 1308 |
|
| 1309 |
/* Move bit offset to the next position in the struct. */ |
| 1310 |
if (TREE_CODE (type) == RECORD_TYPE && DECL_SIZE (field)) |
| 1311 |
bitoffset = int_bit_position (field) + tree_to_shwi (DECL_SIZE (field)); |
| 1312 |
|
| 1313 |
/* If all initializers have been assigned, there's nothing else to do. */ |
| 1314 |
if (vec_safe_is_empty (init)) |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
|
| 1318 |
/* Ensure that we have consumed all values. */ |
| 1319 |
gcc_assert (vec_safe_is_empty (init) || ANON_AGGR_TYPE_P (type)); |
| 1320 |
|
| 1321 |
tree ctor = build_padded_constructor (type, ve); |
| 1322 |
|
| 1323 |
if (constant_p) |
| 1324 |
TREE_CONSTANT (ctor) = 1; |
| 1325 |
|
| 1326 |
return ctor; |
| 1327 |
} |
| 1328 |
|
| 1329 |
/* Return a new zero padded CONSTRUCTOR node whose type is TYPE and values are |
| 1330 |
in the vec pointed to by VALS. */ |
| 1331 |
|
| 1332 |
tree |
| 1333 |
build_padded_constructor (tree type, vec<constructor_elt, va_gc> *vals) |
| 1334 |
{ |
| 1335 |
tree ctor = build_constructor (type, vals); |
| 1336 |
CONSTRUCTOR_ZERO_PADDING_BITS (ctor) = 1; |
| 1337 |
return ctor; |
| 1338 |
} |
| 1339 |
|
| 1340 |
/* Given the TYPE of an anonymous field inside T, return the |
| 1341 |
FIELD_DECL for the field. If not found return NULL_TREE. |
| 1342 |
Because anonymous types can nest, we must also search all |
| 1343 |
anonymous fields that are directly reachable. */ |
| 1344 |
|
| 1345 |
static tree |
| 1346 |
lookup_anon_field (tree t, tree type) |
| 1347 |
{ |
| 1348 |
t = TYPE_MAIN_VARIANT (t); |
| 1349 |
|
| 1350 |
for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
| 1351 |
{ |
| 1352 |
if (DECL_NAME (field) == NULL_TREE) |
| 1353 |
{ |
| 1354 |
/* If we find it directly, return the field. */ |
| 1355 |
if (type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) |
| 1356 |
return field; |
| 1357 |
|
| 1358 |
/* Otherwise, it could be nested, search harder. */ |
| 1359 |
if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)) |
| 1360 |
&& ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| 1361 |
{ |
| 1362 |
tree subfield = lookup_anon_field (TREE_TYPE (field), type); |
| 1363 |
if (subfield) |
| 1364 |
return subfield; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
return NULL_TREE; |
| 1370 |
} |
| 1371 |
|
| 1372 |
/* Builds OBJECT.FIELD component reference. */ |
| 1373 |
|
| 1374 |
tree |
| 1375 |
component_ref (tree object, tree field) |
| 1376 |
{ |
| 1377 |
if (error_operand_p (object) || error_operand_p (field)) |
| 1378 |
return error_mark_node; |
| 1379 |
|
| 1380 |
gcc_assert (TREE_CODE (field) == FIELD_DECL); |
| 1381 |
|
| 1382 |
/* Maybe rewrite: (e1, e2).field => (e1, e2.field) */ |
| 1383 |
tree init = stabilize_expr (&object); |
| 1384 |
|
| 1385 |
/* If the FIELD is from an anonymous aggregate, generate a reference |
| 1386 |
to the anonymous data member, and recur to find FIELD. */ |
| 1387 |
if (ANON_AGGR_TYPE_P (DECL_CONTEXT (field))) |
| 1388 |
{ |
| 1389 |
tree anonymous_field = lookup_anon_field (TREE_TYPE (object), |
| 1390 |
DECL_CONTEXT (field)); |
| 1391 |
object = component_ref (object, anonymous_field); |
| 1392 |
} |
| 1393 |
|
| 1394 |
tree result = fold_build3_loc (input_location, COMPONENT_REF, |
| 1395 |
TREE_TYPE (field), object, field, NULL_TREE); |
| 1396 |
|
| 1397 |
return compound_expr (init, result); |
| 1398 |
} |
| 1399 |
|
| 1400 |
/* Build an assignment expression of lvalue LHS from value RHS. |
| 1401 |
CODE is the code for a binary operator that we use to combine |
| 1402 |
the old value of LHS with RHS to get the new value. */ |
| 1403 |
|
| 1404 |
tree |
| 1405 |
build_assign (tree_code code, tree lhs, tree rhs) |
| 1406 |
{ |
| 1407 |
tree result; |
| 1408 |
tree init = stabilize_expr (&lhs); |
| 1409 |
init = compound_expr (init, stabilize_expr (&rhs)); |
| 1410 |
|
| 1411 |
/* If initializing the LHS using a function that returns via NRVO. */ |
| 1412 |
if (code == INIT_EXPR && TREE_CODE (rhs) == CALL_EXPR |
| 1413 |
&& AGGREGATE_TYPE_P (TREE_TYPE (rhs)) |
| 1414 |
&& aggregate_value_p (TREE_TYPE (rhs), rhs)) |
| 1415 |
{ |
| 1416 |
/* Mark as addressable here, which should ensure the return slot is the |
| 1417 |
address of the LHS expression, taken care of by back-end. */ |
| 1418 |
d_mark_addressable (lhs); |
| 1419 |
CALL_EXPR_RETURN_SLOT_OPT (rhs) = true; |
| 1420 |
} |
| 1421 |
/* If modifying an LHS whose type is marked TREE_ADDRESSABLE. */ |
| 1422 |
else if (code == MODIFY_EXPR && TREE_ADDRESSABLE (TREE_TYPE (lhs)) |
| 1423 |
&& TREE_SIDE_EFFECTS (rhs) && TREE_CODE (rhs) != TARGET_EXPR) |
| 1424 |
{ |
| 1425 |
/* LHS may be referenced by the RHS expression, so force a temporary. */ |
| 1426 |
rhs = force_target_expr (rhs); |
| 1427 |
} |
| 1428 |
|
| 1429 |
/* The LHS assignment replaces the temporary in TARGET_EXPR_SLOT. */ |
| 1430 |
if (TREE_CODE (rhs) == TARGET_EXPR) |
| 1431 |
{ |
| 1432 |
/* If CODE is not INIT_EXPR, can't initialize LHS directly, |
| 1433 |
since that would cause the LHS to be constructed twice. */ |
| 1434 |
if (code != INIT_EXPR) |
| 1435 |
{ |
| 1436 |
init = compound_expr (init, rhs); |
| 1437 |
result = build_assign (code, lhs, TARGET_EXPR_SLOT (rhs)); |
| 1438 |
} |
| 1439 |
else |
| 1440 |
{ |
| 1441 |
d_mark_addressable (lhs); |
| 1442 |
TARGET_EXPR_INITIAL (rhs) = build_assign (code, lhs, |
| 1443 |
TARGET_EXPR_INITIAL (rhs)); |
| 1444 |
result = rhs; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
else |
| 1448 |
{ |
| 1449 |
/* Simple assignment. */ |
| 1450 |
result = fold_build2_loc (input_location, code, |
| 1451 |
TREE_TYPE (lhs), lhs, rhs); |
| 1452 |
} |
| 1453 |
|
| 1454 |
return compound_expr (init, result); |
| 1455 |
} |
| 1456 |
|
| 1457 |
/* Build an assignment expression of lvalue LHS from value RHS. */ |
| 1458 |
|
| 1459 |
tree |
| 1460 |
modify_expr (tree lhs, tree rhs) |
| 1461 |
{ |
| 1462 |
return build_assign (MODIFY_EXPR, lhs, rhs); |
| 1463 |
} |
| 1464 |
|
| 1465 |
/* Return EXP represented as TYPE. */ |
| 1466 |
|
| 1467 |
tree |
| 1468 |
build_nop (tree type, tree exp) |
| 1469 |
{ |
| 1470 |
if (error_operand_p (exp)) |
| 1471 |
return exp; |
| 1472 |
|
| 1473 |
/* Maybe rewrite: cast(TYPE)(e1, e2) => (e1, cast(TYPE) e2) */ |
| 1474 |
tree init = stabilize_expr (&exp); |
| 1475 |
exp = fold_build1_loc (input_location, NOP_EXPR, type, exp); |
| 1476 |
|
| 1477 |
return compound_expr (init, exp); |
| 1478 |
} |
| 1479 |
|
| 1480 |
/* Return EXP to be viewed as being another type TYPE. Same as build_nop, |
| 1481 |
except that EXP is type-punned, rather than a straight-forward cast. */ |
| 1482 |
|
| 1483 |
tree |
| 1484 |
build_vconvert (tree type, tree exp) |
| 1485 |
{ |
| 1486 |
/* Building *(cast(TYPE *)&e1) directly rather then using VIEW_CONVERT_EXPR |
| 1487 |
makes sure this works for vector-to-array viewing, or if EXP ends up being |
| 1488 |
used as the LHS of a MODIFY_EXPR. */ |
| 1489 |
return indirect_ref (type, build_address (exp)); |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* Maybe warn about ARG being an address that can never be null. */ |
| 1493 |
|
| 1494 |
static void |
| 1495 |
warn_for_null_address (tree arg) |
| 1496 |
{ |
| 1497 |
if (TREE_CODE (arg) == ADDR_EXPR |
| 1498 |
&& decl_with_nonnull_addr_p (TREE_OPERAND (arg, 0))) |
| 1499 |
warning (OPT_Waddress, |
| 1500 |
"the address of %qD will never be %<null%>", |
| 1501 |
TREE_OPERAND (arg, 0)); |
| 1502 |
} |
| 1503 |
|
| 1504 |
/* Build a boolean ARG0 op ARG1 expression. */ |
| 1505 |
|
| 1506 |
tree |
| 1507 |
build_boolop (tree_code code, tree arg0, tree arg1) |
| 1508 |
{ |
| 1509 |
/* Aggregate comparisons may get lowered to a call to builtin memcmp, |
| 1510 |
so need to remove all side effects incase its address is taken. */ |
| 1511 |
if (AGGREGATE_TYPE_P (TREE_TYPE (arg0))) |
| 1512 |
arg0 = d_save_expr (arg0); |
| 1513 |
if (AGGREGATE_TYPE_P (TREE_TYPE (arg1))) |
| 1514 |
arg1 = d_save_expr (arg1); |
| 1515 |
|
| 1516 |
if (VECTOR_TYPE_P (TREE_TYPE (arg0)) && VECTOR_TYPE_P (TREE_TYPE (arg1))) |
| 1517 |
{ |
| 1518 |
/* Build a vector comparison. |
| 1519 |
VEC_COND_EXPR <e1 op e2, { -1, -1, -1, -1 }, { 0, 0, 0, 0 }>; */ |
| 1520 |
tree cmptype = truth_type_for (TREE_TYPE (arg0)); |
| 1521 |
tree cmp = fold_build2_loc (input_location, code, cmptype, arg0, arg1); |
| 1522 |
|
| 1523 |
return fold_build3_loc (input_location, VEC_COND_EXPR, cmptype, cmp, |
| 1524 |
build_minus_one_cst (cmptype), |
| 1525 |
build_zero_cst (cmptype)); |
| 1526 |
} |
| 1527 |
|
| 1528 |
if (code == EQ_EXPR || code == NE_EXPR) |
| 1529 |
{ |
| 1530 |
/* Check if comparing the address of a variable to null. */ |
| 1531 |
if (POINTER_TYPE_P (TREE_TYPE (arg0)) && integer_zerop (arg1)) |
| 1532 |
warn_for_null_address (arg0); |
| 1533 |
if (POINTER_TYPE_P (TREE_TYPE (arg1)) && integer_zerop (arg0)) |
| 1534 |
warn_for_null_address (arg1); |
| 1535 |
} |
| 1536 |
|
| 1537 |
return fold_build2_loc (input_location, code, d_bool_type, |
| 1538 |
arg0, d_convert (TREE_TYPE (arg0), arg1)); |
| 1539 |
} |
| 1540 |
|
| 1541 |
/* Return a COND_EXPR. ARG0, ARG1, and ARG2 are the three |
| 1542 |
arguments to the conditional expression. */ |
| 1543 |
|
| 1544 |
tree |
| 1545 |
build_condition (tree type, tree arg0, tree arg1, tree arg2) |
| 1546 |
{ |
| 1547 |
if (arg1 == void_node) |
| 1548 |
arg1 = build_empty_stmt (input_location); |
| 1549 |
|
| 1550 |
if (arg2 == void_node) |
| 1551 |
arg2 = build_empty_stmt (input_location); |
| 1552 |
|
| 1553 |
return fold_build3_loc (input_location, COND_EXPR, |
| 1554 |
type, arg0, arg1, arg2); |
| 1555 |
} |
| 1556 |
|
| 1557 |
tree |
| 1558 |
build_vcondition (tree arg0, tree arg1, tree arg2) |
| 1559 |
{ |
| 1560 |
return build_condition (void_type_node, arg0, arg1, arg2); |
| 1561 |
} |
| 1562 |
|
| 1563 |
/* Build a compound expr to join ARG0 and ARG1 together. */ |
| 1564 |
|
| 1565 |
tree |
| 1566 |
compound_expr (tree arg0, tree arg1) |
| 1567 |
{ |
| 1568 |
if (arg1 == NULL_TREE) |
| 1569 |
return arg0; |
| 1570 |
|
| 1571 |
if (arg0 == NULL_TREE || !TREE_SIDE_EFFECTS (arg0)) |
| 1572 |
return arg1; |
| 1573 |
|
| 1574 |
/* Remove intermediate expressions that have no side-effects. */ |
| 1575 |
while (TREE_CODE (arg0) == COMPOUND_EXPR |
| 1576 |
&& !TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1))) |
| 1577 |
arg0 = TREE_OPERAND (arg0, 0); |
| 1578 |
|
| 1579 |
if (TREE_CODE (arg1) == TARGET_EXPR) |
| 1580 |
{ |
| 1581 |
/* If the rhs is a TARGET_EXPR, then build the compound expression |
| 1582 |
inside the target_expr's initializer. This helps the compiler |
| 1583 |
to eliminate unnecessary temporaries. */ |
| 1584 |
tree init = compound_expr (arg0, TARGET_EXPR_INITIAL (arg1)); |
| 1585 |
TARGET_EXPR_INITIAL (arg1) = init; |
| 1586 |
|
| 1587 |
return arg1; |
| 1588 |
} |
| 1589 |
|
| 1590 |
return fold_build2_loc (input_location, COMPOUND_EXPR, |
| 1591 |
TREE_TYPE (arg1), arg0, arg1); |
| 1592 |
} |
| 1593 |
|
| 1594 |
/* Build a return expression. */ |
| 1595 |
|
| 1596 |
tree |
| 1597 |
return_expr (tree ret) |
| 1598 |
{ |
| 1599 |
/* Same as build_assign, the DECL_RESULT assignment replaces the temporary |
| 1600 |
in TARGET_EXPR_SLOT. */ |
| 1601 |
if (ret != NULL_TREE && TREE_CODE (ret) == TARGET_EXPR) |
| 1602 |
{ |
| 1603 |
tree exp = TARGET_EXPR_INITIAL (ret); |
| 1604 |
tree init = stabilize_expr (&exp); |
| 1605 |
|
| 1606 |
exp = fold_build1_loc (input_location, RETURN_EXPR, void_type_node, exp); |
| 1607 |
TARGET_EXPR_INITIAL (ret) = compound_expr (init, exp); |
| 1608 |
|
| 1609 |
return ret; |
| 1610 |
} |
| 1611 |
|
| 1612 |
return fold_build1_loc (input_location, RETURN_EXPR, |
| 1613 |
void_type_node, ret); |
| 1614 |
} |
| 1615 |
|
| 1616 |
/* Return the product of ARG0 and ARG1 as a size_type_node. */ |
| 1617 |
|
| 1618 |
tree |
| 1619 |
size_mult_expr (tree arg0, tree arg1) |
| 1620 |
{ |
| 1621 |
return fold_build2_loc (input_location, MULT_EXPR, size_type_node, |
| 1622 |
d_convert (size_type_node, arg0), |
| 1623 |
d_convert (size_type_node, arg1)); |
| 1624 |
|
| 1625 |
} |
| 1626 |
|
| 1627 |
/* Return the real part of CE, which should be a complex expression. */ |
| 1628 |
|
| 1629 |
tree |
| 1630 |
real_part (tree ce) |
| 1631 |
{ |
| 1632 |
return fold_build1_loc (input_location, REALPART_EXPR, |
| 1633 |
TREE_TYPE (TREE_TYPE (ce)), ce); |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* Return the imaginary part of CE, which should be a complex expression. */ |
| 1637 |
|
| 1638 |
tree |
| 1639 |
imaginary_part (tree ce) |
| 1640 |
{ |
| 1641 |
return fold_build1_loc (input_location, IMAGPART_EXPR, |
| 1642 |
TREE_TYPE (TREE_TYPE (ce)), ce); |
| 1643 |
} |
| 1644 |
|
| 1645 |
/* Build a complex expression of type TYPE using RE and IM. */ |
| 1646 |
|
| 1647 |
tree |
| 1648 |
complex_expr (tree type, tree re, tree im) |
| 1649 |
{ |
| 1650 |
return fold_build2_loc (input_location, COMPLEX_EXPR, |
| 1651 |
type, re, im); |
| 1652 |
} |
| 1653 |
|
| 1654 |
/* Build a two-field record TYPE representing the complex expression EXPR. */ |
| 1655 |
|
| 1656 |
tree |
| 1657 |
underlying_complex_expr (tree type, tree expr) |
| 1658 |
{ |
| 1659 |
gcc_assert (list_length (TYPE_FIELDS (type)) == 2); |
| 1660 |
|
| 1661 |
expr = d_save_expr (expr); |
| 1662 |
|
| 1663 |
/* Build a constructor from the real and imaginary parts. */ |
| 1664 |
if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (expr)) && |
| 1665 |
(!INDIRECT_REF_P (expr) |
| 1666 |
|| !CONVERT_EXPR_P (TREE_OPERAND (expr, 0)))) |
| 1667 |
{ |
| 1668 |
vec <constructor_elt, va_gc> *ve = NULL; |
| 1669 |
CONSTRUCTOR_APPEND_ELT (ve, TYPE_FIELDS (type), |
| 1670 |
real_part (expr)); |
| 1671 |
CONSTRUCTOR_APPEND_ELT (ve, TREE_CHAIN (TYPE_FIELDS (type)), |
| 1672 |
imaginary_part (expr)); |
| 1673 |
return build_padded_constructor (type, ve); |
| 1674 |
} |
| 1675 |
|
| 1676 |
/* Replace type in the reinterpret cast with a cast to the record type. */ |
| 1677 |
return build_vconvert (type, expr); |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* Cast EXP (which should be a pointer) to TYPE* and then indirect. |
| 1681 |
The back-end requires this cast in many cases. */ |
| 1682 |
|
| 1683 |
tree |
| 1684 |
indirect_ref (tree type, tree exp) |
| 1685 |
{ |
| 1686 |
if (error_operand_p (exp)) |
| 1687 |
return exp; |
| 1688 |
|
| 1689 |
/* Maybe rewrite: *(e1, e2) => (e1, *e2) */ |
| 1690 |
tree init = stabilize_expr (&exp); |
| 1691 |
|
| 1692 |
if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE) |
| 1693 |
exp = fold_build1 (INDIRECT_REF, type, exp); |
| 1694 |
else |
| 1695 |
{ |
| 1696 |
exp = build_nop (build_pointer_type (type), exp); |
| 1697 |
exp = build_deref (exp); |
| 1698 |
} |
| 1699 |
|
| 1700 |
return compound_expr (init, exp); |
| 1701 |
} |
| 1702 |
|
| 1703 |
/* Returns indirect reference of EXP, which must be a pointer type. */ |
| 1704 |
|
| 1705 |
tree |
| 1706 |
build_deref (tree exp) |
| 1707 |
{ |
| 1708 |
if (error_operand_p (exp)) |
| 1709 |
return exp; |
| 1710 |
|
| 1711 |
/* Maybe rewrite: *(e1, e2) => (e1, *e2) */ |
| 1712 |
tree init = stabilize_expr (&exp); |
| 1713 |
|
| 1714 |
gcc_assert (POINTER_TYPE_P (TREE_TYPE (exp))); |
| 1715 |
|
| 1716 |
if (TREE_CODE (exp) == ADDR_EXPR) |
| 1717 |
exp = TREE_OPERAND (exp, 0); |
| 1718 |
else |
| 1719 |
exp = build_fold_indirect_ref (exp); |
| 1720 |
|
| 1721 |
return compound_expr (init, exp); |
| 1722 |
} |
| 1723 |
|
| 1724 |
/* Builds pointer offset expression PTR[INDEX]. */ |
| 1725 |
|
| 1726 |
tree |
| 1727 |
build_pointer_index (tree ptr, tree index) |
| 1728 |
{ |
| 1729 |
if (error_operand_p (ptr) || error_operand_p (index)) |
| 1730 |
return error_mark_node; |
| 1731 |
|
| 1732 |
tree ptr_type = TREE_TYPE (ptr); |
| 1733 |
tree target_type = TREE_TYPE (ptr_type); |
| 1734 |
|
| 1735 |
tree type = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype), |
| 1736 |
TYPE_UNSIGNED (sizetype)); |
| 1737 |
|
| 1738 |
/* Array element size. */ |
| 1739 |
tree size_exp = size_in_bytes (target_type); |
| 1740 |
|
| 1741 |
if (integer_zerop (size_exp) || integer_onep (size_exp)) |
| 1742 |
{ |
| 1743 |
/* Array of void or bytes -- No need to multiply. */ |
| 1744 |
index = fold_convert (type, index); |
| 1745 |
} |
| 1746 |
else |
| 1747 |
{ |
| 1748 |
index = d_convert (type, index); |
| 1749 |
index = fold_build2 (MULT_EXPR, TREE_TYPE (index), |
| 1750 |
index, d_convert (TREE_TYPE (index), size_exp)); |
| 1751 |
index = fold_convert (type, index); |
| 1752 |
} |
| 1753 |
|
| 1754 |
if (integer_zerop (index)) |
| 1755 |
return ptr; |
| 1756 |
|
| 1757 |
return fold_build2 (POINTER_PLUS_EXPR, ptr_type, ptr, index); |
| 1758 |
} |
| 1759 |
|
| 1760 |
/* Builds pointer offset expression *(PTR OP OFFSET) |
| 1761 |
OP could be a plus or minus expression. */ |
| 1762 |
|
| 1763 |
tree |
| 1764 |
build_offset_op (tree_code op, tree ptr, tree offset) |
| 1765 |
{ |
| 1766 |
gcc_assert (op == MINUS_EXPR || op == PLUS_EXPR); |
| 1767 |
|
| 1768 |
tree type = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype), |
| 1769 |
TYPE_UNSIGNED (sizetype)); |
| 1770 |
offset = fold_convert (type, offset); |
| 1771 |
|
| 1772 |
if (op == MINUS_EXPR) |
| 1773 |
offset = fold_build1 (NEGATE_EXPR, type, offset); |
| 1774 |
|
| 1775 |
return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, offset); |
| 1776 |
} |
| 1777 |
|
| 1778 |
/* Builds pointer offset expression *(PTR + OFFSET). */ |
| 1779 |
|
| 1780 |
tree |
| 1781 |
build_offset (tree ptr, tree offset) |
| 1782 |
{ |
| 1783 |
return build_offset_op (PLUS_EXPR, ptr, offset); |
| 1784 |
} |
| 1785 |
|
| 1786 |
tree |
| 1787 |
build_memref (tree type, tree ptr, tree offset) |
| 1788 |
{ |
| 1789 |
return fold_build2 (MEM_REF, type, ptr, fold_convert (type, offset)); |
| 1790 |
} |
| 1791 |
|
| 1792 |
/* Create a tree node to set multiple elements to a single value. */ |
| 1793 |
|
| 1794 |
tree |
| 1795 |
build_array_set (tree ptr, tree length, tree value) |
| 1796 |
{ |
| 1797 |
tree ptrtype = TREE_TYPE (ptr); |
| 1798 |
tree lentype = TREE_TYPE (length); |
| 1799 |
|
| 1800 |
push_binding_level (level_block); |
| 1801 |
push_stmt_list (); |
| 1802 |
|
| 1803 |
/* Build temporary locals for length and ptr, and maybe value. */ |
| 1804 |
tree t = build_local_temp (size_type_node); |
| 1805 |
add_stmt (build_assign (INIT_EXPR, t, length)); |
| 1806 |
length = t; |
| 1807 |
|
| 1808 |
t = build_local_temp (ptrtype); |
| 1809 |
add_stmt (build_assign (INIT_EXPR, t, ptr)); |
| 1810 |
ptr = t; |
| 1811 |
|
| 1812 |
if (TREE_SIDE_EFFECTS (value)) |
| 1813 |
{ |
| 1814 |
t = build_local_temp (TREE_TYPE (value)); |
| 1815 |
add_stmt (build_assign (INIT_EXPR, t, value)); |
| 1816 |
value = t; |
| 1817 |
} |
| 1818 |
|
| 1819 |
/* Build loop to initialize { .length=length, .ptr=ptr } with value. */ |
| 1820 |
push_stmt_list (); |
| 1821 |
|
| 1822 |
/* Exit logic for the loop. |
| 1823 |
if (length == 0) break; */ |
| 1824 |
t = build_boolop (EQ_EXPR, length, d_convert (lentype, integer_zero_node)); |
| 1825 |
t = build1 (EXIT_EXPR, void_type_node, t); |
| 1826 |
add_stmt (t); |
| 1827 |
|
| 1828 |
/* Assign value to the current pointer position. |
| 1829 |
*ptr = value; */ |
| 1830 |
t = modify_expr (build_deref (ptr), value); |
| 1831 |
add_stmt (t); |
| 1832 |
|
| 1833 |
/* Move pointer to next element position. |
| 1834 |
ptr++; */ |
| 1835 |
tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptrtype)); |
| 1836 |
t = build2 (POSTINCREMENT_EXPR, ptrtype, ptr, d_convert (ptrtype, size)); |
| 1837 |
add_stmt (t); |
| 1838 |
|
| 1839 |
/* Decrease loop counter. |
| 1840 |
length -= 1; */ |
| 1841 |
t = build2 (POSTDECREMENT_EXPR, lentype, length, |
| 1842 |
d_convert (lentype, integer_one_node)); |
| 1843 |
add_stmt (t); |
| 1844 |
|
| 1845 |
/* Pop statements and finish loop. */ |
| 1846 |
tree loop_body = pop_stmt_list (); |
| 1847 |
add_stmt (build1 (LOOP_EXPR, void_type_node, loop_body)); |
| 1848 |
|
| 1849 |
/* Wrap it up into a bind expression. */ |
| 1850 |
tree stmt_list = pop_stmt_list (); |
| 1851 |
tree block = pop_binding_level (); |
| 1852 |
|
| 1853 |
return build3 (BIND_EXPR, void_type_node, |
| 1854 |
BLOCK_VARS (block), stmt_list, block); |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
/* Build an array of type TYPE where all the elements are VAL. */ |
| 1859 |
|
| 1860 |
tree |
| 1861 |
build_array_from_val (Type *type, tree val) |
| 1862 |
{ |
| 1863 |
tree etype = build_ctype (type->nextOf ()); |
| 1864 |
|
| 1865 |
/* Initializing a multidimensional array. */ |
| 1866 |
if (TREE_CODE (etype) == ARRAY_TYPE && TREE_TYPE (val) != etype) |
| 1867 |
val = build_array_from_val (type->nextOf (), val); |
| 1868 |
|
| 1869 |
size_t dims = type->isTypeSArray ()->dim->toInteger (); |
| 1870 |
vec <constructor_elt, va_gc> *elms = NULL; |
| 1871 |
vec_safe_reserve (elms, dims); |
| 1872 |
|
| 1873 |
val = d_convert (etype, val); |
| 1874 |
|
| 1875 |
for (size_t i = 0; i < dims; i++) |
| 1876 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), val); |
| 1877 |
|
| 1878 |
return build_padded_constructor (build_ctype (type), elms); |
| 1879 |
} |
| 1880 |
|
| 1881 |
/* Build a static array of type TYPE from an array of EXPS. |
| 1882 |
If CONST_P is true, then all elements in EXPS are constants. */ |
| 1883 |
|
| 1884 |
tree |
| 1885 |
build_array_from_exprs (Type *type, Expressions *exps, bool const_p) |
| 1886 |
{ |
| 1887 |
/* Build a CONSTRUCTOR from all expressions. */ |
| 1888 |
vec <constructor_elt, va_gc> *elms = NULL; |
| 1889 |
vec_safe_reserve (elms, exps->length); |
| 1890 |
|
| 1891 |
Type *etype = type->nextOf (); |
| 1892 |
tree satype = make_array_type (etype, exps->length); |
| 1893 |
|
| 1894 |
for (size_t i = 0; i < exps->length; i++) |
| 1895 |
{ |
| 1896 |
Expression *expr = (*exps)[i]; |
| 1897 |
tree t = build_expr (expr, const_p); |
| 1898 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), |
| 1899 |
convert_expr (t, expr->type, etype)); |
| 1900 |
} |
| 1901 |
|
| 1902 |
/* Create a new temporary to store the array. */ |
| 1903 |
tree var = build_local_temp (satype); |
| 1904 |
|
| 1905 |
/* Fill any alignment holes with zeroes. */ |
| 1906 |
TypeStruct *ts = etype->baseElemOf ()->isTypeStruct (); |
| 1907 |
tree init = NULL; |
| 1908 |
if (ts && (!identity_compare_p (ts->sym) || ts->sym->isUnionDeclaration ())) |
| 1909 |
init = build_memset_call (var); |
| 1910 |
|
| 1911 |
/* Initialize the temporary. */ |
| 1912 |
tree assign = modify_expr (var, build_padded_constructor (satype, elms)); |
| 1913 |
return compound_expr (compound_expr (init, assign), var); |
| 1914 |
} |
| 1915 |
|
| 1916 |
|
| 1917 |
/* Implicitly converts void* T to byte* as D allows { void[] a; &a[3]; } */ |
| 1918 |
|
| 1919 |
tree |
| 1920 |
void_okay_p (tree t) |
| 1921 |
{ |
| 1922 |
tree type = TREE_TYPE (t); |
| 1923 |
|
| 1924 |
if (VOID_TYPE_P (TREE_TYPE (type))) |
| 1925 |
{ |
| 1926 |
tree totype = build_ctype (dmd::pointerTo (Type::tuns8)); |
| 1927 |
return fold_convert (totype, t); |
| 1928 |
} |
| 1929 |
|
| 1930 |
return t; |
| 1931 |
} |
| 1932 |
|
| 1933 |
/* Builds a STRING_CST representing the filename of location LOC. When the |
| 1934 |
location is not valid, the name of the source module is used instead. */ |
| 1935 |
|
| 1936 |
static tree |
| 1937 |
build_filename_from_loc (const Loc &loc) |
| 1938 |
{ |
| 1939 |
const char *filename = loc.filename (); |
| 1940 |
|
| 1941 |
if (filename == NULL) |
| 1942 |
filename = d_function_chain->module->srcfile.toChars (); |
| 1943 |
|
| 1944 |
unsigned length = strlen (filename); |
| 1945 |
tree str = build_string (length, filename); |
| 1946 |
TREE_TYPE (str) = make_array_type (Type::tchar, length + 1); |
| 1947 |
|
| 1948 |
return build_address (str); |
| 1949 |
} |
| 1950 |
|
| 1951 |
/* Builds a CALL_EXPR at location LOC in the source file to call LIBCALL when |
| 1952 |
an assert check fails. When calling the msg variant functions, MSG is the |
| 1953 |
error message supplied by the user. */ |
| 1954 |
|
| 1955 |
tree |
| 1956 |
build_assert_call (const Loc &loc, libcall_fn libcall, tree msg) |
| 1957 |
{ |
| 1958 |
tree file; |
| 1959 |
tree line = size_int (loc.linnum ()); |
| 1960 |
|
| 1961 |
switch (libcall) |
| 1962 |
{ |
| 1963 |
case LIBCALL_ASSERT_MSG: |
| 1964 |
case LIBCALL_UNITTEST_MSG: |
| 1965 |
/* File location is passed as a D string. */ |
| 1966 |
if (const char *filename = loc.filename ()) |
| 1967 |
{ |
| 1968 |
unsigned len = strlen (filename); |
| 1969 |
tree str = build_string (len, filename); |
| 1970 |
TREE_TYPE (str) = make_array_type (Type::tchar, len); |
| 1971 |
|
| 1972 |
file = d_array_value (build_ctype (dmd::arrayOf (Type::tchar)), |
| 1973 |
size_int (len), build_address (str)); |
| 1974 |
} |
| 1975 |
else |
| 1976 |
file = null_array_node; |
| 1977 |
break; |
| 1978 |
|
| 1979 |
case LIBCALL_ASSERTP: |
| 1980 |
case LIBCALL_UNITTESTP: |
| 1981 |
file = build_filename_from_loc (loc); |
| 1982 |
break; |
| 1983 |
|
| 1984 |
default: |
| 1985 |
gcc_unreachable (); |
| 1986 |
} |
| 1987 |
|
| 1988 |
|
| 1989 |
if (msg != NULL_TREE) |
| 1990 |
return build_libcall (libcall, Type::tvoid, 3, msg, file, line); |
| 1991 |
else |
| 1992 |
return build_libcall (libcall, Type::tvoid, 2, file, line); |
| 1993 |
} |
| 1994 |
|
| 1995 |
/* Builds a CALL_EXPR at location LOC in the source file to execute when an |
| 1996 |
array bounds check fails. */ |
| 1997 |
|
| 1998 |
tree |
| 1999 |
build_array_bounds_call (const Loc &loc) |
| 2000 |
{ |
| 2001 |
/* Terminate the program with a trap if no D runtime present. */ |
| 2002 |
if (checkaction_trap_p ()) |
| 2003 |
return build_call_expr (builtin_decl_explicit (BUILT_IN_TRAP), 0); |
| 2004 |
else |
| 2005 |
{ |
| 2006 |
return build_libcall (LIBCALL_ARRAYBOUNDSP, Type::tvoid, 2, |
| 2007 |
build_filename_from_loc (loc), |
| 2008 |
size_int (loc.linnum ())); |
| 2009 |
} |
| 2010 |
} |
| 2011 |
|
| 2012 |
/* Builds a bounds condition checking that INDEX is between 0 and LENGTH |
| 2013 |
in the index expression IE. The condition returns the INDEX if true, or |
| 2014 |
throws a `ArrayIndexError`. */ |
| 2015 |
|
| 2016 |
tree |
| 2017 |
build_bounds_index_condition (IndexExp *ie, tree index, tree length) |
| 2018 |
{ |
| 2019 |
if (ie->indexIsInBounds || !array_bounds_check ()) |
| 2020 |
return index; |
| 2021 |
|
| 2022 |
/* Prevent multiple evaluations of the index. */ |
| 2023 |
index = d_save_expr (index); |
| 2024 |
|
| 2025 |
/* Generate INDEX >= LENGTH && throw RangeError. |
| 2026 |
No need to check whether INDEX >= 0 as the front-end should |
| 2027 |
have already taken care of implicit casts to unsigned. */ |
| 2028 |
tree condition = fold_build2 (GE_EXPR, d_bool_type, index, length); |
| 2029 |
tree boundserr; |
| 2030 |
|
| 2031 |
if (checkaction_trap_p ()) |
| 2032 |
boundserr = build_call_expr (builtin_decl_explicit (BUILT_IN_TRAP), 0); |
| 2033 |
else |
| 2034 |
{ |
| 2035 |
boundserr = build_libcall (LIBCALL_ARRAYBOUNDS_INDEXP, Type::tvoid, 4, |
| 2036 |
build_filename_from_loc (ie->e2->loc), |
| 2037 |
size_int (ie->e2->loc.linnum ()), |
| 2038 |
index, length); |
| 2039 |
} |
| 2040 |
|
| 2041 |
return build_condition (TREE_TYPE (index), condition, boundserr, index); |
| 2042 |
} |
| 2043 |
|
| 2044 |
/* Builds a bounds condition checking that the range LOWER..UPPER do not overlap |
| 2045 |
the slice expression SE of the source array length LENGTH. The condition |
| 2046 |
returns the new array length if true, or throws an `ArraySliceError`. */ |
| 2047 |
|
| 2048 |
tree |
| 2049 |
build_bounds_slice_condition (SliceExp *se, tree lower, tree upper, tree length) |
| 2050 |
{ |
| 2051 |
if (array_bounds_check ()) |
| 2052 |
{ |
| 2053 |
tree condition = NULL_TREE; |
| 2054 |
|
| 2055 |
/* Enforces that `upper <= length`. */ |
| 2056 |
if (!se->upperIsInBounds () && length != NULL_TREE) |
| 2057 |
condition = fold_build2 (GT_EXPR, d_bool_type, upper, length); |
| 2058 |
else |
| 2059 |
length = integer_zero_node; |
| 2060 |
|
| 2061 |
/* Enforces that `lower <= upper`. No need to check `lower <= length` as |
| 2062 |
we've already ensured that `upper <= length`. */ |
| 2063 |
if (!se->lowerIsLessThanUpper ()) |
| 2064 |
{ |
| 2065 |
tree lwr_cond = fold_build2 (GT_EXPR, d_bool_type, lower, upper); |
| 2066 |
|
| 2067 |
if (condition != NULL_TREE) |
| 2068 |
condition = build_boolop (TRUTH_ORIF_EXPR, condition, lwr_cond); |
| 2069 |
else |
| 2070 |
condition = lwr_cond; |
| 2071 |
} |
| 2072 |
|
| 2073 |
if (condition != NULL_TREE) |
| 2074 |
{ |
| 2075 |
tree boundserr; |
| 2076 |
|
| 2077 |
if (checkaction_trap_p ()) |
| 2078 |
{ |
| 2079 |
boundserr = |
| 2080 |
build_call_expr (builtin_decl_explicit (BUILT_IN_TRAP), 0); |
| 2081 |
} |
| 2082 |
else |
| 2083 |
{ |
| 2084 |
boundserr = build_libcall (LIBCALL_ARRAYBOUNDS_SLICEP, |
| 2085 |
Type::tvoid, 5, |
| 2086 |
build_filename_from_loc (se->loc), |
| 2087 |
size_int (se->loc.linnum ()), |
| 2088 |
lower, upper, length); |
| 2089 |
} |
| 2090 |
|
| 2091 |
upper = build_condition (TREE_TYPE (upper), condition, |
| 2092 |
boundserr, upper); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
|
| 2096 |
/* Need to ensure lower always gets evaluated first, as it may be a function |
| 2097 |
call. Generates (lower, upper) - lower. */ |
| 2098 |
return fold_build2 (MINUS_EXPR, TREE_TYPE (upper), |
| 2099 |
compound_expr (lower, upper), lower); |
| 2100 |
} |
| 2101 |
|
| 2102 |
/* Returns TRUE if array bounds checking code generation is turned on. */ |
| 2103 |
|
| 2104 |
bool |
| 2105 |
array_bounds_check (void) |
| 2106 |
{ |
| 2107 |
FuncDeclaration *fd; |
| 2108 |
|
| 2109 |
switch (global.params.useArrayBounds) |
| 2110 |
{ |
| 2111 |
case CHECKENABLEoff: |
| 2112 |
return false; |
| 2113 |
|
| 2114 |
case CHECKENABLEon: |
| 2115 |
return true; |
| 2116 |
|
| 2117 |
case CHECKENABLEsafeonly: |
| 2118 |
/* For D2 safe functions only. */ |
| 2119 |
fd = d_function_chain->function; |
| 2120 |
if (fd && fd->type->ty == TY::Tfunction) |
| 2121 |
{ |
| 2122 |
if (fd->type->isTypeFunction ()->trust == TRUST::safe) |
| 2123 |
return true; |
| 2124 |
} |
| 2125 |
return false; |
| 2126 |
|
| 2127 |
default: |
| 2128 |
gcc_unreachable (); |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
/* Returns TRUE if we terminate the program with a trap if an array bounds or |
| 2133 |
contract check fails. */ |
| 2134 |
|
| 2135 |
bool |
| 2136 |
checkaction_trap_p (void) |
| 2137 |
{ |
| 2138 |
switch (global.params.checkAction) |
| 2139 |
{ |
| 2140 |
case CHECKACTION_D: |
| 2141 |
case CHECKACTION_context: |
| 2142 |
return false; |
| 2143 |
|
| 2144 |
case CHECKACTION_C: |
| 2145 |
case CHECKACTION_halt: |
| 2146 |
return true; |
| 2147 |
|
| 2148 |
default: |
| 2149 |
gcc_unreachable (); |
| 2150 |
} |
| 2151 |
} |
| 2152 |
|
| 2153 |
/* Returns the TypeFunction class for Type T. |
| 2154 |
Assumes T is already ->toBasetype(). */ |
| 2155 |
|
| 2156 |
TypeFunction * |
| 2157 |
get_function_type (Type *t) |
| 2158 |
{ |
| 2159 |
TypeFunction *tf = NULL; |
| 2160 |
if (t->ty == TY::Tpointer) |
| 2161 |
t = t->nextOf ()->toBasetype (); |
| 2162 |
if (t->ty == TY::Tfunction) |
| 2163 |
tf = t->isTypeFunction (); |
| 2164 |
else if (t->ty == TY::Tdelegate) |
| 2165 |
tf = t->isTypeDelegate ()->next->isTypeFunction (); |
| 2166 |
return tf; |
| 2167 |
} |
| 2168 |
|
| 2169 |
/* Returns TRUE if calling the function FUNC, or calling a function or delegate |
| 2170 |
object of type TYPE is be free of side effects. */ |
| 2171 |
|
| 2172 |
bool |
| 2173 |
call_side_effect_free_p (FuncDeclaration *func, Type *type) |
| 2174 |
{ |
| 2175 |
gcc_assert (func != NULL || type != NULL); |
| 2176 |
|
| 2177 |
if (func != NULL) |
| 2178 |
{ |
| 2179 |
/* Constructor and invariant calls can't be `pure'. */ |
| 2180 |
if (func->isCtorDeclaration () || func->isInvariantDeclaration ()) |
| 2181 |
return false; |
| 2182 |
|
| 2183 |
/* Must be a `nothrow' function. */ |
| 2184 |
TypeFunction *tf = func->type->toTypeFunction (); |
| 2185 |
if (!tf->isNothrow ()) |
| 2186 |
return false; |
| 2187 |
|
| 2188 |
/* Return type can't be `void' or `noreturn', as that implies all work is |
| 2189 |
done via side effects. */ |
| 2190 |
if (tf->next->ty == TY::Tvoid || tf->next->ty == TY::Tnoreturn) |
| 2191 |
return false; |
| 2192 |
|
| 2193 |
/* Only consider it as `pure' if it can't modify its arguments. */ |
| 2194 |
if (dmd::isPure (func) == PURE::const_) |
| 2195 |
return true; |
| 2196 |
} |
| 2197 |
|
| 2198 |
if (type != NULL) |
| 2199 |
{ |
| 2200 |
TypeFunction *tf = get_function_type (type); |
| 2201 |
|
| 2202 |
/* Must be a `nothrow` function type. */ |
| 2203 |
if (tf == NULL || !tf->isNothrow ()) |
| 2204 |
return false; |
| 2205 |
|
| 2206 |
/* Return type can't be `void' or `noreturn', as that implies all work is |
| 2207 |
done via side effects. */ |
| 2208 |
if (tf->next->ty == TY::Tvoid || tf->next->ty == TY::Tnoreturn) |
| 2209 |
return false; |
| 2210 |
|
| 2211 |
/* Delegates that can modify its context can't be `pure'. */ |
| 2212 |
if (type->isTypeDelegate () && tf->isMutable ()) |
| 2213 |
return false; |
| 2214 |
|
| 2215 |
/* Only consider it as `pure' if it can't modify its arguments. */ |
| 2216 |
if (tf->purity == PURE::const_) |
| 2217 |
return true; |
| 2218 |
} |
| 2219 |
|
| 2220 |
return false; |
| 2221 |
} |
| 2222 |
|
| 2223 |
/* Returns TRUE if CALLEE is a plain nested function outside the scope of |
| 2224 |
CALLER. In which case, CALLEE is being called through an alias that was |
| 2225 |
passed to CALLER. */ |
| 2226 |
|
| 2227 |
bool |
| 2228 |
call_by_alias_p (FuncDeclaration *caller, FuncDeclaration *callee) |
| 2229 |
{ |
| 2230 |
if (!callee->isNested ()) |
| 2231 |
return false; |
| 2232 |
|
| 2233 |
if (caller->toParent () == callee->toParent ()) |
| 2234 |
return false; |
| 2235 |
|
| 2236 |
Dsymbol *dsym = callee; |
| 2237 |
|
| 2238 |
while (dsym) |
| 2239 |
{ |
| 2240 |
if (dsym->isTemplateInstance ()) |
| 2241 |
return false; |
| 2242 |
else if (dsym->isFuncDeclaration () == caller) |
| 2243 |
return false; |
| 2244 |
dsym = dsym->toParent (); |
| 2245 |
} |
| 2246 |
|
| 2247 |
return true; |
| 2248 |
} |
| 2249 |
|
| 2250 |
/* Entry point for call routines. Builds a function call to FD. |
| 2251 |
OBJECT is the `this' reference passed and ARGS are the arguments to FD. */ |
| 2252 |
|
| 2253 |
tree |
| 2254 |
d_build_call_expr (FuncDeclaration *fd, tree object, Expressions *arguments) |
| 2255 |
{ |
| 2256 |
return d_build_call (get_function_type (fd->type), |
| 2257 |
build_address (get_symbol_decl (fd)), object, arguments); |
| 2258 |
} |
| 2259 |
|
| 2260 |
/* Builds a CALL_EXPR of type TF to CALLABLE. OBJECT holds the `this' pointer, |
| 2261 |
ARGUMENTS are evaluated in left to right order, saved and promoted |
| 2262 |
before passing. */ |
| 2263 |
|
| 2264 |
tree |
| 2265 |
d_build_call (TypeFunction *tf, tree callable, tree object, |
| 2266 |
Expressions *arguments) |
| 2267 |
{ |
| 2268 |
tree ctype = TREE_TYPE (callable); |
| 2269 |
tree callee = callable; |
| 2270 |
|
| 2271 |
if (POINTER_TYPE_P (ctype)) |
| 2272 |
ctype = TREE_TYPE (ctype); |
| 2273 |
else |
| 2274 |
callee = build_address (callable); |
| 2275 |
|
| 2276 |
gcc_assert (FUNC_OR_METHOD_TYPE_P (ctype)); |
| 2277 |
gcc_assert (tf != NULL); |
| 2278 |
gcc_assert (tf->ty == TY::Tfunction); |
| 2279 |
|
| 2280 |
if (TREE_CODE (ctype) != FUNCTION_TYPE && object == NULL_TREE) |
| 2281 |
{ |
| 2282 |
/* Front-end apparently doesn't check this. */ |
| 2283 |
if (TREE_CODE (callable) == FUNCTION_DECL) |
| 2284 |
{ |
| 2285 |
error ("need %<this%> to access member %qE", DECL_NAME (callable)); |
| 2286 |
return error_mark_node; |
| 2287 |
} |
| 2288 |
|
| 2289 |
/* Probably an internal error. */ |
| 2290 |
gcc_unreachable (); |
| 2291 |
} |
| 2292 |
|
| 2293 |
/* Build the argument list for the call. */ |
| 2294 |
vec <tree, va_gc> *args = NULL; |
| 2295 |
bool noreturn_call = false; |
| 2296 |
|
| 2297 |
/* If this is a delegate call or a nested function being called as |
| 2298 |
a delegate, the object should not be NULL. */ |
| 2299 |
if (object != NULL_TREE) |
| 2300 |
vec_safe_push (args, object); |
| 2301 |
|
| 2302 |
if (arguments) |
| 2303 |
{ |
| 2304 |
const size_t nparams = tf->parameterList.length (); |
| 2305 |
/* if _arguments[] is the first argument. */ |
| 2306 |
const size_t varargs = tf->isDstyleVariadic (); |
| 2307 |
|
| 2308 |
/* Assumes arguments->length <= formal_args->length if (!tf->varargs). */ |
| 2309 |
for (size_t i = 0; i < arguments->length; ++i) |
| 2310 |
{ |
| 2311 |
Expression *arg = (*arguments)[i]; |
| 2312 |
tree targ; |
| 2313 |
|
| 2314 |
if (i - varargs < nparams && i >= varargs) |
| 2315 |
{ |
| 2316 |
/* Actual arguments for declared formal arguments. */ |
| 2317 |
Parameter *parg = tf->parameterList[i - varargs]; |
| 2318 |
targ = convert_for_argument (arg, parg); |
| 2319 |
} |
| 2320 |
else |
| 2321 |
targ = build_expr (arg); |
| 2322 |
|
| 2323 |
/* Don't pass empty aggregates by value. */ |
| 2324 |
if (empty_aggregate_p (TREE_TYPE (targ)) && !TREE_ADDRESSABLE (targ) |
| 2325 |
&& TREE_CODE (targ) != CONSTRUCTOR) |
| 2326 |
{ |
| 2327 |
tree t = build_padded_constructor (TREE_TYPE (targ), NULL); |
| 2328 |
targ = build2 (COMPOUND_EXPR, TREE_TYPE (t), targ, t); |
| 2329 |
} |
| 2330 |
|
| 2331 |
/* Parameter is a struct or array passed by invisible reference. */ |
| 2332 |
if (TREE_ADDRESSABLE (TREE_TYPE (targ))) |
| 2333 |
{ |
| 2334 |
Type *t = arg->type->toBasetype (); |
| 2335 |
StructDeclaration *sd = t->baseElemOf ()->isTypeStruct ()->sym; |
| 2336 |
|
| 2337 |
/* Need to take care of copying its value before passing the |
| 2338 |
argument in the following scenarios: |
| 2339 |
- The argument is a literal expression; a CONSTRUCTOR can't |
| 2340 |
have its address taken. |
| 2341 |
- The type has neither a copy constructor nor a destructor |
| 2342 |
available; nested structs also have ADDRESSABLE set. |
| 2343 |
- The ABI of the function expects the callee to destroy its |
| 2344 |
arguments; when the caller is handles destruction, then `targ' |
| 2345 |
has already been made into a temporary. */ |
| 2346 |
if (!can_elide_copy_p (arg) |
| 2347 |
&& (arg->op == EXP::structLiteral |
| 2348 |
|| (!sd->postblit && !sd->dtor) |
| 2349 |
|| target.isCalleeDestroyingArgs (tf))) |
| 2350 |
targ = force_target_expr (targ); |
| 2351 |
|
| 2352 |
targ = convert (build_reference_type (TREE_TYPE (targ)), |
| 2353 |
build_address (targ)); |
| 2354 |
} |
| 2355 |
|
| 2356 |
/* Complex types are exposed as special types with an underlying |
| 2357 |
struct representation, if we are passing the native type to a |
| 2358 |
function that accepts the library-defined version, then ensure |
| 2359 |
it is properly reinterpreted as the underlying struct type. */ |
| 2360 |
if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (targ)) |
| 2361 |
&& arg->type->isTypeStruct ()) |
| 2362 |
targ = underlying_complex_expr (build_ctype (arg->type), targ); |
| 2363 |
|
| 2364 |
/* Type `noreturn` is a terminator, as no other arguments can possibly |
| 2365 |
be evaluated after it. */ |
| 2366 |
if (TYPE_MAIN_VARIANT (TREE_TYPE (targ)) == noreturn_type_node) |
| 2367 |
noreturn_call = true; |
| 2368 |
|
| 2369 |
vec_safe_push (args, targ); |
| 2370 |
} |
| 2371 |
} |
| 2372 |
|
| 2373 |
/* If we saw a `noreturn` parameter, any unreachable argument evaluations |
| 2374 |
after it are discarded, as well as the function call itself. */ |
| 2375 |
if (noreturn_call) |
| 2376 |
{ |
| 2377 |
tree saved_args = NULL_TREE; |
| 2378 |
|
| 2379 |
if (TREE_SIDE_EFFECTS (callee)) |
| 2380 |
saved_args = compound_expr (callee, saved_args); |
| 2381 |
|
| 2382 |
tree arg; |
| 2383 |
unsigned int ix; |
| 2384 |
|
| 2385 |
FOR_EACH_VEC_SAFE_ELT (args, ix, arg) |
| 2386 |
{ |
| 2387 |
saved_args = compound_expr (saved_args, arg); |
| 2388 |
|
| 2389 |
if (TYPE_MAIN_VARIANT (TREE_TYPE (arg)) == noreturn_type_node) |
| 2390 |
break; |
| 2391 |
} |
| 2392 |
|
| 2393 |
/* Add a stub result type for the expression. */ |
| 2394 |
tree result = build_zero_cst (TREE_TYPE (ctype)); |
| 2395 |
return compound_expr (saved_args, result); |
| 2396 |
} |
| 2397 |
|
| 2398 |
tree result = build_call_vec (TREE_TYPE (ctype), callee, args); |
| 2399 |
SET_EXPR_LOCATION (result, input_location); |
| 2400 |
|
| 2401 |
result = maybe_expand_intrinsic (result); |
| 2402 |
|
| 2403 |
/* Return the value in a temporary slot so that it can be evaluated |
| 2404 |
multiple times by the caller. */ |
| 2405 |
if (TREE_CODE (result) == CALL_EXPR |
| 2406 |
&& AGGREGATE_TYPE_P (TREE_TYPE (result)) |
| 2407 |
&& TREE_ADDRESSABLE (TREE_TYPE (result))) |
| 2408 |
{ |
| 2409 |
CALL_EXPR_RETURN_SLOT_OPT (result) = true; |
| 2410 |
result = force_target_expr (result); |
| 2411 |
} |
| 2412 |
|
| 2413 |
return result; |
| 2414 |
} |
| 2415 |
|
| 2416 |
/* Build and return the correct call to fmod depending on TYPE. |
| 2417 |
ARG0 and ARG1 are the arguments pass to the function. */ |
| 2418 |
|
| 2419 |
tree |
| 2420 |
build_float_modulus (tree type, tree arg0, tree arg1) |
| 2421 |
{ |
| 2422 |
tree fmodfn = NULL_TREE; |
| 2423 |
tree basetype = type; |
| 2424 |
|
| 2425 |
if (COMPLEX_FLOAT_TYPE_P (basetype)) |
| 2426 |
basetype = TREE_TYPE (basetype); |
| 2427 |
|
| 2428 |
if (TYPE_MAIN_VARIANT (basetype) == double_type_node |
| 2429 |
|| TYPE_MAIN_VARIANT (basetype) == idouble_type_node) |
| 2430 |
fmodfn = builtin_decl_explicit (BUILT_IN_FMOD); |
| 2431 |
else if (TYPE_MAIN_VARIANT (basetype) == float_type_node |
| 2432 |
|| TYPE_MAIN_VARIANT (basetype) == ifloat_type_node) |
| 2433 |
fmodfn = builtin_decl_explicit (BUILT_IN_FMODF); |
| 2434 |
else if (TYPE_MAIN_VARIANT (basetype) == long_double_type_node |
| 2435 |
|| TYPE_MAIN_VARIANT (basetype) == ireal_type_node) |
| 2436 |
fmodfn = builtin_decl_explicit (BUILT_IN_FMODL); |
| 2437 |
|
| 2438 |
if (!fmodfn) |
| 2439 |
{ |
| 2440 |
error ("tried to perform floating-point modulo division on %qT", type); |
| 2441 |
return error_mark_node; |
| 2442 |
} |
| 2443 |
|
| 2444 |
if (COMPLEX_FLOAT_TYPE_P (type)) |
| 2445 |
{ |
| 2446 |
tree re = build_call_expr (fmodfn, 2, real_part (arg0), arg1); |
| 2447 |
tree im = build_call_expr (fmodfn, 2, imaginary_part (arg0), arg1); |
| 2448 |
|
| 2449 |
return complex_expr (type, re, im); |
| 2450 |
} |
| 2451 |
|
| 2452 |
if (SCALAR_FLOAT_TYPE_P (type)) |
| 2453 |
return build_call_expr (fmodfn, 2, arg0, arg1); |
| 2454 |
|
| 2455 |
/* Should have caught this above. */ |
| 2456 |
gcc_unreachable (); |
| 2457 |
} |
| 2458 |
|
| 2459 |
/* Build a function type whose first argument is a pointer to BASETYPE, |
| 2460 |
which is to be used for the `vthis' context parameter for TYPE. |
| 2461 |
The base type may be a record for member functions, or a void for |
| 2462 |
nested functions and delegates. */ |
| 2463 |
|
| 2464 |
tree |
| 2465 |
build_vthis_function (tree basetype, tree type) |
| 2466 |
{ |
| 2467 |
gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); |
| 2468 |
|
| 2469 |
tree argtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), |
| 2470 |
TYPE_ARG_TYPES (type)); |
| 2471 |
tree fntype = build_function_type (TREE_TYPE (type), argtypes); |
| 2472 |
|
| 2473 |
/* Copy volatile qualifiers from the original function type. */ |
| 2474 |
if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE) |
| 2475 |
fntype = build_qualified_type (fntype, TYPE_QUAL_VOLATILE); |
| 2476 |
|
| 2477 |
if (RECORD_OR_UNION_TYPE_P (basetype)) |
| 2478 |
TYPE_METHOD_BASETYPE (fntype) = TYPE_MAIN_VARIANT (basetype); |
| 2479 |
else |
| 2480 |
gcc_assert (VOID_TYPE_P (basetype)); |
| 2481 |
|
| 2482 |
return fntype; |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* Raise an error at that the context pointer of the function or object SYM is |
| 2486 |
not accessible from the current scope. */ |
| 2487 |
|
| 2488 |
tree |
| 2489 |
error_no_frame_access (Dsymbol *sym) |
| 2490 |
{ |
| 2491 |
error_at (input_location, "cannot get frame pointer to %qs", |
| 2492 |
sym->toPrettyChars ()); |
| 2493 |
return null_pointer_node; |
| 2494 |
} |
| 2495 |
|
| 2496 |
/* If SYM is a nested function, return the static chain to be |
| 2497 |
used when calling that function from the current function. |
| 2498 |
|
| 2499 |
If SYM is a nested class or struct, return the static chain |
| 2500 |
to be used when creating an instance of the class from CFUN. */ |
| 2501 |
|
| 2502 |
tree |
| 2503 |
get_frame_for_symbol (Dsymbol *sym) |
| 2504 |
{ |
| 2505 |
FuncDeclaration *thisfd |
| 2506 |
= d_function_chain ? d_function_chain->function : NULL; |
| 2507 |
FuncDeclaration *fd = sym->isFuncDeclaration (); |
| 2508 |
FuncDeclaration *fdparent = NULL; |
| 2509 |
FuncDeclaration *fdoverride = NULL; |
| 2510 |
|
| 2511 |
if (fd != NULL) |
| 2512 |
{ |
| 2513 |
/* Check that the nested function is properly defined. */ |
| 2514 |
if (!fd->fbody) |
| 2515 |
{ |
| 2516 |
/* Should instead error on line that references `fd'. */ |
| 2517 |
error_at (make_location_t (fd->loc), "nested function missing body"); |
| 2518 |
return null_pointer_node; |
| 2519 |
} |
| 2520 |
|
| 2521 |
fdparent = fd->toParent2 ()->isFuncDeclaration (); |
| 2522 |
|
| 2523 |
/* Special case for __ensure and __require. */ |
| 2524 |
if ((fd->ident == Identifier::idPool ("__ensure") |
| 2525 |
|| fd->ident == Identifier::idPool ("__require")) |
| 2526 |
&& fdparent != thisfd) |
| 2527 |
{ |
| 2528 |
fdoverride = fdparent; |
| 2529 |
fdparent = thisfd; |
| 2530 |
} |
| 2531 |
} |
| 2532 |
else |
| 2533 |
{ |
| 2534 |
/* It's a class (or struct). NewExp codegen has already determined its |
| 2535 |
outer scope is not another class, so it must be a function. */ |
| 2536 |
while (sym && !sym->isFuncDeclaration ()) |
| 2537 |
sym = sym->toParent2 (); |
| 2538 |
|
| 2539 |
fdparent = (FuncDeclaration *) sym; |
| 2540 |
} |
| 2541 |
|
| 2542 |
/* Not a nested function, there is no frame pointer to pass. */ |
| 2543 |
if (fdparent == NULL) |
| 2544 |
{ |
| 2545 |
/* Only delegate literals report as being nested, even if they are in |
| 2546 |
global scope. */ |
| 2547 |
gcc_assert (fd && fd->isFuncLiteralDeclaration ()); |
| 2548 |
return null_pointer_node; |
| 2549 |
} |
| 2550 |
|
| 2551 |
gcc_assert (thisfd != NULL); |
| 2552 |
|
| 2553 |
if (thisfd != fdparent) |
| 2554 |
{ |
| 2555 |
/* If no frame pointer for this function. */ |
| 2556 |
if (!thisfd->vthis) |
| 2557 |
{ |
| 2558 |
error_at (make_location_t (sym->loc), |
| 2559 |
"%qs is a nested function and cannot be accessed from %qs", |
| 2560 |
fdparent->toPrettyChars (), thisfd->toPrettyChars ()); |
| 2561 |
return null_pointer_node; |
| 2562 |
} |
| 2563 |
|
| 2564 |
/* Make sure we can get the frame pointer to the outer function. |
| 2565 |
Go up each nesting level until we find the enclosing function. */ |
| 2566 |
Dsymbol *dsym = thisfd; |
| 2567 |
|
| 2568 |
while (fd != dsym) |
| 2569 |
{ |
| 2570 |
/* Check if enclosing function is a function. */ |
| 2571 |
FuncDeclaration *fdp = dsym->isFuncDeclaration (); |
| 2572 |
Dsymbol *parent = dsym->toParent2 (); |
| 2573 |
|
| 2574 |
if (fdp != NULL) |
| 2575 |
{ |
| 2576 |
if (fdparent == parent) |
| 2577 |
break; |
| 2578 |
|
| 2579 |
gcc_assert (fdp->isNested () || fdp->vthis); |
| 2580 |
dsym = parent; |
| 2581 |
continue; |
| 2582 |
} |
| 2583 |
|
| 2584 |
/* Check if enclosed by an aggregate. That means the current |
| 2585 |
function must be a member function of that aggregate. */ |
| 2586 |
AggregateDeclaration *adp = dsym->isAggregateDeclaration (); |
| 2587 |
|
| 2588 |
if (adp != NULL) |
| 2589 |
{ |
| 2590 |
if ((adp->isClassDeclaration () || adp->isStructDeclaration ()) |
| 2591 |
&& fdparent == parent) |
| 2592 |
break; |
| 2593 |
} |
| 2594 |
|
| 2595 |
/* No frame to outer function found. */ |
| 2596 |
if (!adp || !adp->isNested () || !adp->vthis) |
| 2597 |
return error_no_frame_access (sym); |
| 2598 |
|
| 2599 |
dsym = parent; |
| 2600 |
} |
| 2601 |
} |
| 2602 |
|
| 2603 |
tree ffo = get_frameinfo (fdparent); |
| 2604 |
if (FRAMEINFO_CREATES_FRAME (ffo) || FRAMEINFO_STATIC_CHAIN (ffo)) |
| 2605 |
{ |
| 2606 |
tree frame_ref = get_framedecl (thisfd, fdparent); |
| 2607 |
|
| 2608 |
/* If `thisfd' is a derived member function, then `fdparent' is the |
| 2609 |
overridden member function in the base class. Even if there's a |
| 2610 |
closure environment, we should give the original stack data as the |
| 2611 |
nested function frame. */ |
| 2612 |
if (fdoverride) |
| 2613 |
{ |
| 2614 |
ClassDeclaration *cdo = fdoverride->isThis ()->isClassDeclaration (); |
| 2615 |
ClassDeclaration *cd = thisfd->isThis ()->isClassDeclaration (); |
| 2616 |
gcc_assert (cdo && cd); |
| 2617 |
|
| 2618 |
int offset; |
| 2619 |
if (cdo->isBaseOf (cd, &offset) && offset != 0) |
| 2620 |
{ |
| 2621 |
/* Generate a new frame to pass to the overriden function that |
| 2622 |
has the `this' pointer adjusted. */ |
| 2623 |
gcc_assert (offset != OFFSET_RUNTIME); |
| 2624 |
|
| 2625 |
tree type = FRAMEINFO_TYPE (get_frameinfo (fdoverride)); |
| 2626 |
tree fields = TYPE_FIELDS (type); |
| 2627 |
/* The `this' field comes immediately after the `__chain'. */ |
| 2628 |
tree thisfield = chain_index (1, fields); |
| 2629 |
vec <constructor_elt, va_gc> *ve = NULL; |
| 2630 |
|
| 2631 |
tree framefields = TYPE_FIELDS (FRAMEINFO_TYPE (ffo)); |
| 2632 |
frame_ref = build_deref (frame_ref); |
| 2633 |
|
| 2634 |
for (tree field = fields; field; field = DECL_CHAIN (field)) |
| 2635 |
{ |
| 2636 |
tree value = component_ref (frame_ref, framefields); |
| 2637 |
if (field == thisfield) |
| 2638 |
value = build_offset (value, size_int (offset)); |
| 2639 |
|
| 2640 |
CONSTRUCTOR_APPEND_ELT (ve, field, value); |
| 2641 |
framefields = DECL_CHAIN (framefields); |
| 2642 |
} |
| 2643 |
|
| 2644 |
frame_ref = build_address (build_padded_constructor (type, ve)); |
| 2645 |
} |
| 2646 |
} |
| 2647 |
|
| 2648 |
return frame_ref; |
| 2649 |
} |
| 2650 |
|
| 2651 |
return null_pointer_node; |
| 2652 |
} |
| 2653 |
|
| 2654 |
/* Return the parent function of a nested class or struct AD. */ |
| 2655 |
|
| 2656 |
static FuncDeclaration * |
| 2657 |
get_outer_function (AggregateDeclaration *ad) |
| 2658 |
{ |
| 2659 |
FuncDeclaration *fd = NULL; |
| 2660 |
while (ad && ad->isNested ()) |
| 2661 |
{ |
| 2662 |
Dsymbol *dsym = ad->toParent2 (); |
| 2663 |
if ((fd = dsym->isFuncDeclaration ())) |
| 2664 |
return fd; |
| 2665 |
else |
| 2666 |
ad = dsym->isAggregateDeclaration (); |
| 2667 |
} |
| 2668 |
|
| 2669 |
return NULL; |
| 2670 |
} |
| 2671 |
|
| 2672 |
/* Starting from the current function FD, try to find a suitable value of |
| 2673 |
`this' in nested function instances. A suitable `this' value is an |
| 2674 |
instance of OCD or a class that has OCD as a base. */ |
| 2675 |
|
| 2676 |
static tree |
| 2677 |
find_this_tree (ClassDeclaration *ocd) |
| 2678 |
{ |
| 2679 |
FuncDeclaration *fd = d_function_chain ? d_function_chain->function : NULL; |
| 2680 |
|
| 2681 |
while (fd) |
| 2682 |
{ |
| 2683 |
AggregateDeclaration *ad = fd->isThis (); |
| 2684 |
ClassDeclaration *cd = ad ? ad->isClassDeclaration () : NULL; |
| 2685 |
|
| 2686 |
if (cd != NULL) |
| 2687 |
{ |
| 2688 |
if (ocd == cd) |
| 2689 |
return get_decl_tree (fd->vthis); |
| 2690 |
else if (ocd->isBaseOf (cd, NULL)) |
| 2691 |
return convert_expr (get_decl_tree (fd->vthis), |
| 2692 |
cd->type, ocd->type); |
| 2693 |
|
| 2694 |
fd = get_outer_function (cd); |
| 2695 |
continue; |
| 2696 |
} |
| 2697 |
|
| 2698 |
if (fd->isNested ()) |
| 2699 |
{ |
| 2700 |
fd = fd->toParent2 ()->isFuncDeclaration (); |
| 2701 |
continue; |
| 2702 |
} |
| 2703 |
|
| 2704 |
fd = NULL; |
| 2705 |
} |
| 2706 |
|
| 2707 |
return NULL_TREE; |
| 2708 |
} |
| 2709 |
|
| 2710 |
/* Retrieve the outer class/struct `this' value of DECL from |
| 2711 |
the current function. */ |
| 2712 |
|
| 2713 |
tree |
| 2714 |
build_vthis (AggregateDeclaration *decl) |
| 2715 |
{ |
| 2716 |
ClassDeclaration *cd = decl->isClassDeclaration (); |
| 2717 |
StructDeclaration *sd = decl->isStructDeclaration (); |
| 2718 |
|
| 2719 |
/* If an aggregate nested in a function has no methods and there are no |
| 2720 |
other nested functions, any static chain created here will never be |
| 2721 |
translated. Use a null pointer for the link in this case. */ |
| 2722 |
tree vthis_value = null_pointer_node; |
| 2723 |
|
| 2724 |
if (cd != NULL || sd != NULL) |
| 2725 |
{ |
| 2726 |
Dsymbol *outer = decl->toParent2 (); |
| 2727 |
|
| 2728 |
/* If the parent is a templated struct, the outer context is instead |
| 2729 |
the enclosing symbol of where the instantiation happened. */ |
| 2730 |
if (outer->isStructDeclaration ()) |
| 2731 |
{ |
| 2732 |
gcc_assert (outer->parent && outer->parent->isTemplateInstance ()); |
| 2733 |
outer = ((TemplateInstance *) outer->parent)->enclosing; |
| 2734 |
} |
| 2735 |
|
| 2736 |
/* For outer classes, get a suitable `this' value. |
| 2737 |
For outer functions, get a suitable frame/closure pointer. */ |
| 2738 |
ClassDeclaration *cdo = outer->isClassDeclaration (); |
| 2739 |
FuncDeclaration *fdo = outer->isFuncDeclaration (); |
| 2740 |
|
| 2741 |
if (cdo) |
| 2742 |
{ |
| 2743 |
vthis_value = find_this_tree (cdo); |
| 2744 |
gcc_assert (vthis_value != NULL_TREE); |
| 2745 |
} |
| 2746 |
else if (fdo) |
| 2747 |
{ |
| 2748 |
tree ffo = get_frameinfo (fdo); |
| 2749 |
if (FRAMEINFO_CREATES_FRAME (ffo) || FRAMEINFO_STATIC_CHAIN (ffo) |
| 2750 |
|| fdo->hasNestedFrameRefs ()) |
| 2751 |
vthis_value = get_frame_for_symbol (decl); |
| 2752 |
else if (cd != NULL) |
| 2753 |
{ |
| 2754 |
/* Classes nested in methods are allowed to access any outer |
| 2755 |
class fields, use the function chain in this case. */ |
| 2756 |
if (fdo->vthis && fdo->vthis->type != Type::tvoidptr) |
| 2757 |
vthis_value = get_decl_tree (fdo->vthis); |
| 2758 |
} |
| 2759 |
} |
| 2760 |
else |
| 2761 |
gcc_unreachable (); |
| 2762 |
} |
| 2763 |
|
| 2764 |
return vthis_value; |
| 2765 |
} |
| 2766 |
|
| 2767 |
/* Build the RECORD_TYPE that describes the function frame or closure type for |
| 2768 |
the function FD. FFI is the tree holding all frame information. */ |
| 2769 |
|
| 2770 |
static tree |
| 2771 |
build_frame_type (tree ffi, FuncDeclaration *fd) |
| 2772 |
{ |
| 2773 |
if (FRAMEINFO_TYPE (ffi)) |
| 2774 |
return FRAMEINFO_TYPE (ffi); |
| 2775 |
|
| 2776 |
tree frame_rec_type = make_node (RECORD_TYPE); |
| 2777 |
char *name = concat (FRAMEINFO_IS_CLOSURE (ffi) ? "CLOSURE." : "FRAME.", |
| 2778 |
fd->toPrettyChars (), NULL); |
| 2779 |
TYPE_NAME (frame_rec_type) = get_identifier (name); |
| 2780 |
free (name); |
| 2781 |
|
| 2782 |
tree fields = NULL_TREE; |
| 2783 |
|
| 2784 |
/* Function is a member or nested, so must have field for outer context. */ |
| 2785 |
if (fd->vthis) |
| 2786 |
{ |
| 2787 |
tree ptr_field = build_decl (BUILTINS_LOCATION, FIELD_DECL, |
| 2788 |
get_identifier ("__chain"), ptr_type_node); |
| 2789 |
DECL_FIELD_CONTEXT (ptr_field) = frame_rec_type; |
| 2790 |
fields = chainon (NULL_TREE, ptr_field); |
| 2791 |
DECL_NONADDRESSABLE_P (ptr_field) = 1; |
| 2792 |
} |
| 2793 |
|
| 2794 |
/* The __ensure and __require are called directly, so never make the outer |
| 2795 |
functions closure, but nevertheless could still be referencing parameters |
| 2796 |
of the calling function non-locally. So we add all parameters with nested |
| 2797 |
refs to the function frame, this should also mean overriding methods will |
| 2798 |
have the same frame layout when inheriting a contract. */ |
| 2799 |
if ((global.params.useIn == CHECKENABLEon && fd->frequire ()) |
| 2800 |
|| (global.params.useOut == CHECKENABLEon && fd->fensure ())) |
| 2801 |
{ |
| 2802 |
if (fd->parameters) |
| 2803 |
{ |
| 2804 |
for (size_t i = 0; fd->parameters && i < fd->parameters->length; i++) |
| 2805 |
{ |
| 2806 |
VarDeclaration *v = (*fd->parameters)[i]; |
| 2807 |
/* Remove if already in closureVars so can push to front. */ |
| 2808 |
size_t j = fd->closureVars.find (v); |
| 2809 |
|
| 2810 |
if (j < fd->closureVars.length) |
| 2811 |
fd->closureVars.remove (j); |
| 2812 |
|
| 2813 |
fd->closureVars.insert (i, v); |
| 2814 |
} |
| 2815 |
} |
| 2816 |
|
| 2817 |
/* Also add hidden `this' to outer context. */ |
| 2818 |
if (fd->vthis) |
| 2819 |
{ |
| 2820 |
size_t i = fd->closureVars.find (fd->vthis); |
| 2821 |
|
| 2822 |
if (i < fd->closureVars.length) |
| 2823 |
fd->closureVars.remove (i); |
| 2824 |
|
| 2825 |
fd->closureVars.insert (0, fd->vthis); |
| 2826 |
} |
| 2827 |
} |
| 2828 |
|
| 2829 |
for (size_t i = 0; i < fd->closureVars.length; i++) |
| 2830 |
{ |
| 2831 |
VarDeclaration *v = fd->closureVars[i]; |
| 2832 |
tree vsym = get_symbol_decl (v); |
| 2833 |
tree ident = v->ident |
| 2834 |
? get_identifier (v->ident->toChars ()) : NULL_TREE; |
| 2835 |
|
| 2836 |
tree field = build_decl (make_location_t (v->loc), FIELD_DECL, ident, |
| 2837 |
TREE_TYPE (vsym)); |
| 2838 |
SET_DECL_LANG_FRAME_FIELD (vsym, field); |
| 2839 |
DECL_FIELD_CONTEXT (field) = frame_rec_type; |
| 2840 |
fields = chainon (fields, field); |
| 2841 |
TREE_USED (vsym) = 1; |
| 2842 |
|
| 2843 |
TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (vsym); |
| 2844 |
DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (vsym); |
| 2845 |
TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (vsym); |
| 2846 |
SET_DECL_ALIGN (field, DECL_ALIGN (vsym)); |
| 2847 |
|
| 2848 |
/* Update alignment for frame record type. */ |
| 2849 |
if (TYPE_ALIGN (frame_rec_type) < DECL_ALIGN (field)) |
| 2850 |
SET_TYPE_ALIGN (frame_rec_type, DECL_ALIGN (field)); |
| 2851 |
|
| 2852 |
if (DECL_LANG_NRVO (vsym)) |
| 2853 |
{ |
| 2854 |
/* Store the nrvo variable in the frame by reference. */ |
| 2855 |
TREE_TYPE (field) = build_reference_type (TREE_TYPE (field)); |
| 2856 |
|
| 2857 |
/* Can't do nrvo if the variable is put in a closure, since what the |
| 2858 |
return slot points to may no longer exist. */ |
| 2859 |
gcc_assert (!FRAMEINFO_IS_CLOSURE (ffi)); |
| 2860 |
} |
| 2861 |
|
| 2862 |
if (FRAMEINFO_IS_CLOSURE (ffi)) |
| 2863 |
{ |
| 2864 |
/* Because the value needs to survive the end of the scope. */ |
| 2865 |
if ((v->edtor && (v->storage_class & STCparameter)) |
| 2866 |
|| v->needsScopeDtor ()) |
| 2867 |
error_at (make_location_t (v->loc), |
| 2868 |
"variable %qs has scoped destruction, " |
| 2869 |
"cannot build closure", v->toChars ()); |
| 2870 |
} |
| 2871 |
|
| 2872 |
if (DECL_REGISTER (vsym)) |
| 2873 |
{ |
| 2874 |
/* Because the value will be in memory, not a register. */ |
| 2875 |
error_at (make_location_t (v->loc), |
| 2876 |
"explicit register variable %qs cannot be used in nested " |
| 2877 |
"function", v->toChars ()); |
| 2878 |
} |
| 2879 |
} |
| 2880 |
|
| 2881 |
TYPE_FIELDS (frame_rec_type) = fields; |
| 2882 |
TYPE_READONLY (frame_rec_type) = 1; |
| 2883 |
TYPE_CXX_ODR_P (frame_rec_type) = 1; |
| 2884 |
layout_type (frame_rec_type); |
| 2885 |
d_keep (frame_rec_type); |
| 2886 |
|
| 2887 |
return frame_rec_type; |
| 2888 |
} |
| 2889 |
|
| 2890 |
/* Closures are implemented by taking the local variables that |
| 2891 |
need to survive the scope of the function, and copying them |
| 2892 |
into a GC allocated chuck of memory. That chunk, called the |
| 2893 |
closure here, is inserted into the linked list of stack |
| 2894 |
frames instead of the usual stack frame. |
| 2895 |
|
| 2896 |
If a closure is not required, but FD still needs a frame to lower |
| 2897 |
nested refs, then instead build custom static chain decl on stack. */ |
| 2898 |
|
| 2899 |
void |
| 2900 |
build_closure (FuncDeclaration *fd) |
| 2901 |
{ |
| 2902 |
tree ffi = get_frameinfo (fd); |
| 2903 |
|
| 2904 |
if (!FRAMEINFO_CREATES_FRAME (ffi)) |
| 2905 |
return; |
| 2906 |
|
| 2907 |
tree type = FRAMEINFO_TYPE (ffi); |
| 2908 |
gcc_assert (COMPLETE_TYPE_P (type)); |
| 2909 |
|
| 2910 |
tree decl, decl_ref; |
| 2911 |
|
| 2912 |
if (FRAMEINFO_IS_CLOSURE (ffi)) |
| 2913 |
{ |
| 2914 |
decl = build_local_temp (build_pointer_type (type)); |
| 2915 |
DECL_NAME (decl) = get_identifier ("__closptr"); |
| 2916 |
decl_ref = build_deref (decl); |
| 2917 |
|
| 2918 |
/* Allocate memory for closure. */ |
| 2919 |
tree arg = convert (build_ctype (Type::tsize_t), TYPE_SIZE_UNIT (type)); |
| 2920 |
tree init = build_libcall (LIBCALL_ALLOCMEMORY, Type::tvoidptr, 1, arg); |
| 2921 |
|
| 2922 |
tree init_exp = build_assign (INIT_EXPR, decl, |
| 2923 |
build_nop (TREE_TYPE (decl), init)); |
| 2924 |
add_stmt (init_exp); |
| 2925 |
} |
| 2926 |
else |
| 2927 |
{ |
| 2928 |
decl = build_local_temp (type); |
| 2929 |
DECL_NAME (decl) = get_identifier ("__frame"); |
| 2930 |
decl_ref = decl; |
| 2931 |
} |
| 2932 |
|
| 2933 |
/* Set the first entry to the parent closure/frame, if any. */ |
| 2934 |
if (fd->vthis) |
| 2935 |
{ |
| 2936 |
tree chain_field = component_ref (decl_ref, TYPE_FIELDS (type)); |
| 2937 |
tree chain_expr = modify_expr (chain_field, |
| 2938 |
d_function_chain->static_chain); |
| 2939 |
add_stmt (chain_expr); |
| 2940 |
} |
| 2941 |
|
| 2942 |
/* Copy parameters that are referenced nonlocally. */ |
| 2943 |
for (size_t i = 0; i < fd->closureVars.length; i++) |
| 2944 |
{ |
| 2945 |
VarDeclaration *v = fd->closureVars[i]; |
| 2946 |
tree vsym = get_symbol_decl (v); |
| 2947 |
|
| 2948 |
if (TREE_CODE (vsym) != PARM_DECL && !DECL_LANG_NRVO (vsym)) |
| 2949 |
continue; |
| 2950 |
|
| 2951 |
tree field = component_ref (decl_ref, DECL_LANG_FRAME_FIELD (vsym)); |
| 2952 |
|
| 2953 |
/* Variable is an alias for the NRVO slot, store the reference. */ |
| 2954 |
if (DECL_LANG_NRVO (vsym)) |
| 2955 |
vsym = build_address (DECL_LANG_NRVO (vsym)); |
| 2956 |
|
| 2957 |
tree expr = modify_expr (field, vsym); |
| 2958 |
add_stmt (expr); |
| 2959 |
} |
| 2960 |
|
| 2961 |
if (!FRAMEINFO_IS_CLOSURE (ffi)) |
| 2962 |
decl = build_address (decl); |
| 2963 |
|
| 2964 |
d_function_chain->static_chain = decl; |
| 2965 |
} |
| 2966 |
|
| 2967 |
/* Return the frame of FD. This could be a static chain or a closure |
| 2968 |
passed via the hidden `this' pointer. */ |
| 2969 |
|
| 2970 |
tree |
| 2971 |
get_frameinfo (FuncDeclaration *fd) |
| 2972 |
{ |
| 2973 |
tree fds = get_symbol_decl (fd); |
| 2974 |
if (DECL_LANG_FRAMEINFO (fds)) |
| 2975 |
return DECL_LANG_FRAMEINFO (fds); |
| 2976 |
|
| 2977 |
tree ffi = make_node (FUNCFRAME_INFO); |
| 2978 |
|
| 2979 |
DECL_LANG_FRAMEINFO (fds) = ffi; |
| 2980 |
|
| 2981 |
const bool requiresClosure = fd->requiresClosure; |
| 2982 |
if (fd->needsClosure ()) |
| 2983 |
{ |
| 2984 |
/* This can shift due to templates being expanded that access alias |
| 2985 |
symbols, give it a decent error for now. */ |
| 2986 |
if (requiresClosure != fd->requiresClosure |
| 2987 |
&& (fd->nrvo_var || !global.params.useGC)) |
| 2988 |
dmd::checkClosure (fd); |
| 2989 |
|
| 2990 |
/* Set-up a closure frame, this will be allocated on the heap. */ |
| 2991 |
FRAMEINFO_CREATES_FRAME (ffi) = 1; |
| 2992 |
FRAMEINFO_IS_CLOSURE (ffi) = 1; |
| 2993 |
} |
| 2994 |
else if (fd->hasNestedFrameRefs ()) |
| 2995 |
{ |
| 2996 |
/* Functions with nested refs must create a static frame for local |
| 2997 |
variables to be referenced from. */ |
| 2998 |
FRAMEINFO_CREATES_FRAME (ffi) = 1; |
| 2999 |
} |
| 3000 |
else |
| 3001 |
{ |
| 3002 |
/* For nested functions, default to creating a frame. Even if there are |
| 3003 |
no fields to populate the frame, create it anyway, as this will be |
| 3004 |
used as the record type instead of `void*` for the this parameter. */ |
| 3005 |
if (fd->vthis && fd->vthis->type == Type::tvoidptr) |
| 3006 |
FRAMEINFO_CREATES_FRAME (ffi) = 1; |
| 3007 |
|
| 3008 |
/* In checkNestedReference, references from contracts are not added to the |
| 3009 |
closureVars array, so assume all parameters referenced. */ |
| 3010 |
if ((global.params.useIn == CHECKENABLEon && fd->frequire ()) |
| 3011 |
|| (global.params.useOut == CHECKENABLEon && fd->fensure ())) |
| 3012 |
FRAMEINFO_CREATES_FRAME (ffi) = 1; |
| 3013 |
|
| 3014 |
/* If however `fd` is nested (deeply) in a function that creates a |
| 3015 |
closure, then `fd` instead inherits that closure via hidden vthis |
| 3016 |
pointer, and doesn't create a stack frame at all. */ |
| 3017 |
FuncDeclaration *ff = fd; |
| 3018 |
|
| 3019 |
while (ff) |
| 3020 |
{ |
| 3021 |
tree ffo = get_frameinfo (ff); |
| 3022 |
|
| 3023 |
if (ff != fd && FRAMEINFO_CREATES_FRAME (ffo)) |
| 3024 |
{ |
| 3025 |
gcc_assert (FRAMEINFO_TYPE (ffo)); |
| 3026 |
FRAMEINFO_CREATES_FRAME (ffi) = 0; |
| 3027 |
FRAMEINFO_STATIC_CHAIN (ffi) = 1; |
| 3028 |
FRAMEINFO_IS_CLOSURE (ffi) = FRAMEINFO_IS_CLOSURE (ffo); |
| 3029 |
gcc_assert (COMPLETE_TYPE_P (FRAMEINFO_TYPE (ffo))); |
| 3030 |
FRAMEINFO_TYPE (ffi) = FRAMEINFO_TYPE (ffo); |
| 3031 |
break; |
| 3032 |
} |
| 3033 |
|
| 3034 |
/* Stop looking if no frame pointer for this function. */ |
| 3035 |
if (ff->vthis == NULL) |
| 3036 |
break; |
| 3037 |
|
| 3038 |
AggregateDeclaration *ad = ff->isThis (); |
| 3039 |
if (ad && ad->isNested ()) |
| 3040 |
{ |
| 3041 |
while (ad->isNested ()) |
| 3042 |
{ |
| 3043 |
Dsymbol *d = ad->toParent2 (); |
| 3044 |
ad = d->isAggregateDeclaration (); |
| 3045 |
ff = d->isFuncDeclaration (); |
| 3046 |
|
| 3047 |
if (ad == NULL) |
| 3048 |
break; |
| 3049 |
} |
| 3050 |
} |
| 3051 |
else |
| 3052 |
ff = ff->toParent2 ()->isFuncDeclaration (); |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
/* Build type now as may be referenced from another module. */ |
| 3057 |
if (FRAMEINFO_CREATES_FRAME (ffi)) |
| 3058 |
FRAMEINFO_TYPE (ffi) = build_frame_type (ffi, fd); |
| 3059 |
|
| 3060 |
return ffi; |
| 3061 |
} |
| 3062 |
|
| 3063 |
/* Return a pointer to the frame/closure block of OUTER |
| 3064 |
so can be accessed from the function INNER. */ |
| 3065 |
|
| 3066 |
tree |
| 3067 |
get_framedecl (FuncDeclaration *inner, FuncDeclaration *outer) |
| 3068 |
{ |
| 3069 |
tree result = d_function_chain->static_chain; |
| 3070 |
FuncDeclaration *fd = inner; |
| 3071 |
|
| 3072 |
while (fd && fd != outer) |
| 3073 |
{ |
| 3074 |
/* Parent frame link is the first field. */ |
| 3075 |
if (FRAMEINFO_CREATES_FRAME (get_frameinfo (fd))) |
| 3076 |
result = indirect_ref (ptr_type_node, result); |
| 3077 |
|
| 3078 |
if (fd->isNested ()) |
| 3079 |
fd = fd->toParent2 ()->isFuncDeclaration (); |
| 3080 |
/* The frame/closure record always points to the outer function's |
| 3081 |
frame, even if there are intervening nested classes or structs. |
| 3082 |
So, we can just skip over these. */ |
| 3083 |
else |
| 3084 |
fd = get_outer_function (fd->isThis ()); |
| 3085 |
} |
| 3086 |
|
| 3087 |
if (fd != outer) |
| 3088 |
return error_no_frame_access (outer); |
| 3089 |
|
| 3090 |
/* Go get our frame record. */ |
| 3091 |
tree frame_type = FRAMEINFO_TYPE (get_frameinfo (outer)); |
| 3092 |
|
| 3093 |
if (frame_type != NULL_TREE) |
| 3094 |
{ |
| 3095 |
result = build_nop (build_pointer_type (frame_type), result); |
| 3096 |
return result; |
| 3097 |
} |
| 3098 |
else |
| 3099 |
{ |
| 3100 |
error_at (make_location_t (inner->loc), |
| 3101 |
"forward reference to frame of %qs", outer->toChars ()); |
| 3102 |
return null_pointer_node; |
| 3103 |
} |
| 3104 |
} |