trans.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. /* Code translation -- generate GCC trees from gfc_code.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "gfortran.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "gimple-expr.h" /* For create_tmp_var_raw. */
  33. #include "stringpool.h"
  34. #include "tree-iterator.h"
  35. #include "diagnostic-core.h" /* For internal_error. */
  36. #include "flags.h"
  37. #include "trans.h"
  38. #include "trans-stmt.h"
  39. #include "trans-array.h"
  40. #include "trans-types.h"
  41. #include "trans-const.h"
  42. /* Naming convention for backend interface code:
  43. gfc_trans_* translate gfc_code into STMT trees.
  44. gfc_conv_* expression conversion
  45. gfc_get_* get a backend tree representation of a decl or type */
  46. static gfc_file *gfc_current_backend_file;
  47. const char gfc_msg_fault[] = N_("Array reference out of bounds");
  48. const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
  49. /* Advance along TREE_CHAIN n times. */
  50. tree
  51. gfc_advance_chain (tree t, int n)
  52. {
  53. for (; n > 0; n--)
  54. {
  55. gcc_assert (t != NULL_TREE);
  56. t = DECL_CHAIN (t);
  57. }
  58. return t;
  59. }
  60. /* Strip off a legitimate source ending from the input
  61. string NAME of length LEN. */
  62. static inline void
  63. remove_suffix (char *name, int len)
  64. {
  65. int i;
  66. for (i = 2; i < 8 && len > i; i++)
  67. {
  68. if (name[len - i] == '.')
  69. {
  70. name[len - i] = '\0';
  71. break;
  72. }
  73. }
  74. }
  75. /* Creates a variable declaration with a given TYPE. */
  76. tree
  77. gfc_create_var_np (tree type, const char *prefix)
  78. {
  79. tree t;
  80. t = create_tmp_var_raw (type, prefix);
  81. /* No warnings for anonymous variables. */
  82. if (prefix == NULL)
  83. TREE_NO_WARNING (t) = 1;
  84. return t;
  85. }
  86. /* Like above, but also adds it to the current scope. */
  87. tree
  88. gfc_create_var (tree type, const char *prefix)
  89. {
  90. tree tmp;
  91. tmp = gfc_create_var_np (type, prefix);
  92. pushdecl (tmp);
  93. return tmp;
  94. }
  95. /* If the expression is not constant, evaluate it now. We assign the
  96. result of the expression to an artificially created variable VAR, and
  97. return a pointer to the VAR_DECL node for this variable. */
  98. tree
  99. gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
  100. {
  101. tree var;
  102. if (CONSTANT_CLASS_P (expr))
  103. return expr;
  104. var = gfc_create_var (TREE_TYPE (expr), NULL);
  105. gfc_add_modify_loc (loc, pblock, var, expr);
  106. return var;
  107. }
  108. tree
  109. gfc_evaluate_now (tree expr, stmtblock_t * pblock)
  110. {
  111. return gfc_evaluate_now_loc (input_location, expr, pblock);
  112. }
  113. /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
  114. A MODIFY_EXPR is an assignment:
  115. LHS <- RHS. */
  116. void
  117. gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
  118. {
  119. tree tmp;
  120. #ifdef ENABLE_CHECKING
  121. tree t1, t2;
  122. t1 = TREE_TYPE (rhs);
  123. t2 = TREE_TYPE (lhs);
  124. /* Make sure that the types of the rhs and the lhs are the same
  125. for scalar assignments. We should probably have something
  126. similar for aggregates, but right now removing that check just
  127. breaks everything. */
  128. gcc_assert (t1 == t2
  129. || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
  130. #endif
  131. tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
  132. rhs);
  133. gfc_add_expr_to_block (pblock, tmp);
  134. }
  135. void
  136. gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
  137. {
  138. gfc_add_modify_loc (input_location, pblock, lhs, rhs);
  139. }
  140. /* Create a new scope/binding level and initialize a block. Care must be
  141. taken when translating expressions as any temporaries will be placed in
  142. the innermost scope. */
  143. void
  144. gfc_start_block (stmtblock_t * block)
  145. {
  146. /* Start a new binding level. */
  147. pushlevel ();
  148. block->has_scope = 1;
  149. /* The block is empty. */
  150. block->head = NULL_TREE;
  151. }
  152. /* Initialize a block without creating a new scope. */
  153. void
  154. gfc_init_block (stmtblock_t * block)
  155. {
  156. block->head = NULL_TREE;
  157. block->has_scope = 0;
  158. }
  159. /* Sometimes we create a scope but it turns out that we don't actually
  160. need it. This function merges the scope of BLOCK with its parent.
  161. Only variable decls will be merged, you still need to add the code. */
  162. void
  163. gfc_merge_block_scope (stmtblock_t * block)
  164. {
  165. tree decl;
  166. tree next;
  167. gcc_assert (block->has_scope);
  168. block->has_scope = 0;
  169. /* Remember the decls in this scope. */
  170. decl = getdecls ();
  171. poplevel (0, 0);
  172. /* Add them to the parent scope. */
  173. while (decl != NULL_TREE)
  174. {
  175. next = DECL_CHAIN (decl);
  176. DECL_CHAIN (decl) = NULL_TREE;
  177. pushdecl (decl);
  178. decl = next;
  179. }
  180. }
  181. /* Finish a scope containing a block of statements. */
  182. tree
  183. gfc_finish_block (stmtblock_t * stmtblock)
  184. {
  185. tree decl;
  186. tree expr;
  187. tree block;
  188. expr = stmtblock->head;
  189. if (!expr)
  190. expr = build_empty_stmt (input_location);
  191. stmtblock->head = NULL_TREE;
  192. if (stmtblock->has_scope)
  193. {
  194. decl = getdecls ();
  195. if (decl)
  196. {
  197. block = poplevel (1, 0);
  198. expr = build3_v (BIND_EXPR, decl, expr, block);
  199. }
  200. else
  201. poplevel (0, 0);
  202. }
  203. return expr;
  204. }
  205. /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
  206. natural type is used. */
  207. tree
  208. gfc_build_addr_expr (tree type, tree t)
  209. {
  210. tree base_type = TREE_TYPE (t);
  211. tree natural_type;
  212. if (type && POINTER_TYPE_P (type)
  213. && TREE_CODE (base_type) == ARRAY_TYPE
  214. && TYPE_MAIN_VARIANT (TREE_TYPE (type))
  215. == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
  216. {
  217. tree min_val = size_zero_node;
  218. tree type_domain = TYPE_DOMAIN (base_type);
  219. if (type_domain && TYPE_MIN_VALUE (type_domain))
  220. min_val = TYPE_MIN_VALUE (type_domain);
  221. t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
  222. t, min_val, NULL_TREE, NULL_TREE));
  223. natural_type = type;
  224. }
  225. else
  226. natural_type = build_pointer_type (base_type);
  227. if (TREE_CODE (t) == INDIRECT_REF)
  228. {
  229. if (!type)
  230. type = natural_type;
  231. t = TREE_OPERAND (t, 0);
  232. natural_type = TREE_TYPE (t);
  233. }
  234. else
  235. {
  236. tree base = get_base_address (t);
  237. if (base && DECL_P (base))
  238. TREE_ADDRESSABLE (base) = 1;
  239. t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
  240. }
  241. if (type && natural_type != type)
  242. t = convert (type, t);
  243. return t;
  244. }
  245. /* Build an ARRAY_REF with its natural type. */
  246. tree
  247. gfc_build_array_ref (tree base, tree offset, tree decl)
  248. {
  249. tree type = TREE_TYPE (base);
  250. tree tmp;
  251. tree span;
  252. if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
  253. {
  254. gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
  255. return fold_convert (TYPE_MAIN_VARIANT (type), base);
  256. }
  257. /* Scalar coarray, there is nothing to do. */
  258. if (TREE_CODE (type) != ARRAY_TYPE)
  259. {
  260. gcc_assert (decl == NULL_TREE);
  261. gcc_assert (integer_zerop (offset));
  262. return base;
  263. }
  264. type = TREE_TYPE (type);
  265. if (DECL_P (base))
  266. TREE_ADDRESSABLE (base) = 1;
  267. /* Strip NON_LVALUE_EXPR nodes. */
  268. STRIP_TYPE_NOPS (offset);
  269. /* If the array reference is to a pointer, whose target contains a
  270. subreference, use the span that is stored with the backend decl
  271. and reference the element with pointer arithmetic. */
  272. if (decl && (TREE_CODE (decl) == FIELD_DECL
  273. || TREE_CODE (decl) == VAR_DECL
  274. || TREE_CODE (decl) == PARM_DECL)
  275. && ((GFC_DECL_SUBREF_ARRAY_P (decl)
  276. && !integer_zerop (GFC_DECL_SPAN(decl)))
  277. || GFC_DECL_CLASS (decl)))
  278. {
  279. if (GFC_DECL_CLASS (decl))
  280. {
  281. /* Allow for dummy arguments and other good things. */
  282. if (POINTER_TYPE_P (TREE_TYPE (decl)))
  283. decl = build_fold_indirect_ref_loc (input_location, decl);
  284. /* Check if '_data' is an array descriptor. If it is not,
  285. the array must be one of the components of the class object,
  286. so return a normal array reference. */
  287. if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_class_data_get (decl))))
  288. return build4_loc (input_location, ARRAY_REF, type, base,
  289. offset, NULL_TREE, NULL_TREE);
  290. span = gfc_class_vtab_size_get (decl);
  291. }
  292. else if (GFC_DECL_SUBREF_ARRAY_P (decl))
  293. span = GFC_DECL_SPAN(decl);
  294. else
  295. gcc_unreachable ();
  296. offset = fold_build2_loc (input_location, MULT_EXPR,
  297. gfc_array_index_type,
  298. offset, span);
  299. tmp = gfc_build_addr_expr (pvoid_type_node, base);
  300. tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
  301. tmp = fold_convert (build_pointer_type (type), tmp);
  302. if (!TYPE_STRING_FLAG (type))
  303. tmp = build_fold_indirect_ref_loc (input_location, tmp);
  304. return tmp;
  305. }
  306. else
  307. /* Otherwise use a straightforward array reference. */
  308. return build4_loc (input_location, ARRAY_REF, type, base, offset,
  309. NULL_TREE, NULL_TREE);
  310. }
  311. /* Generate a call to print a runtime error possibly including multiple
  312. arguments and a locus. */
  313. static tree
  314. trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
  315. va_list ap)
  316. {
  317. stmtblock_t block;
  318. tree tmp;
  319. tree arg, arg2;
  320. tree *argarray;
  321. tree fntype;
  322. char *message;
  323. const char *p;
  324. int line, nargs, i;
  325. location_t loc;
  326. /* Compute the number of extra arguments from the format string. */
  327. for (p = msgid, nargs = 0; *p; p++)
  328. if (*p == '%')
  329. {
  330. p++;
  331. if (*p != '%')
  332. nargs++;
  333. }
  334. /* The code to generate the error. */
  335. gfc_start_block (&block);
  336. if (where)
  337. {
  338. line = LOCATION_LINE (where->lb->location);
  339. message = xasprintf ("At line %d of file %s", line,
  340. where->lb->file->filename);
  341. }
  342. else
  343. message = xasprintf ("In file '%s', around line %d",
  344. gfc_source_file, LOCATION_LINE (input_location) + 1);
  345. arg = gfc_build_addr_expr (pchar_type_node,
  346. gfc_build_localized_cstring_const (message));
  347. free (message);
  348. message = xasprintf ("%s", _(msgid));
  349. arg2 = gfc_build_addr_expr (pchar_type_node,
  350. gfc_build_localized_cstring_const (message));
  351. free (message);
  352. /* Build the argument array. */
  353. argarray = XALLOCAVEC (tree, nargs + 2);
  354. argarray[0] = arg;
  355. argarray[1] = arg2;
  356. for (i = 0; i < nargs; i++)
  357. argarray[2 + i] = va_arg (ap, tree);
  358. /* Build the function call to runtime_(warning,error)_at; because of the
  359. variable number of arguments, we can't use build_call_expr_loc dinput_location,
  360. irectly. */
  361. if (error)
  362. fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
  363. else
  364. fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
  365. loc = where ? where->lb->location : input_location;
  366. tmp = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
  367. fold_build1_loc (loc, ADDR_EXPR,
  368. build_pointer_type (fntype),
  369. error
  370. ? gfor_fndecl_runtime_error_at
  371. : gfor_fndecl_runtime_warning_at),
  372. nargs + 2, argarray);
  373. gfc_add_expr_to_block (&block, tmp);
  374. return gfc_finish_block (&block);
  375. }
  376. tree
  377. gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
  378. {
  379. va_list ap;
  380. tree result;
  381. va_start (ap, msgid);
  382. result = trans_runtime_error_vararg (error, where, msgid, ap);
  383. va_end (ap);
  384. return result;
  385. }
  386. /* Generate a runtime error if COND is true. */
  387. void
  388. gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
  389. locus * where, const char * msgid, ...)
  390. {
  391. va_list ap;
  392. stmtblock_t block;
  393. tree body;
  394. tree tmp;
  395. tree tmpvar = NULL;
  396. if (integer_zerop (cond))
  397. return;
  398. if (once)
  399. {
  400. tmpvar = gfc_create_var (boolean_type_node, "print_warning");
  401. TREE_STATIC (tmpvar) = 1;
  402. DECL_INITIAL (tmpvar) = boolean_true_node;
  403. gfc_add_expr_to_block (pblock, tmpvar);
  404. }
  405. gfc_start_block (&block);
  406. /* For error, runtime_error_at already implies PRED_NORETURN. */
  407. if (!error && once)
  408. gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
  409. NOT_TAKEN));
  410. /* The code to generate the error. */
  411. va_start (ap, msgid);
  412. gfc_add_expr_to_block (&block,
  413. trans_runtime_error_vararg (error, where,
  414. msgid, ap));
  415. va_end (ap);
  416. if (once)
  417. gfc_add_modify (&block, tmpvar, boolean_false_node);
  418. body = gfc_finish_block (&block);
  419. if (integer_onep (cond))
  420. {
  421. gfc_add_expr_to_block (pblock, body);
  422. }
  423. else
  424. {
  425. if (once)
  426. cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
  427. long_integer_type_node, tmpvar, cond);
  428. else
  429. cond = fold_convert (long_integer_type_node, cond);
  430. tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
  431. cond, body,
  432. build_empty_stmt (where->lb->location));
  433. gfc_add_expr_to_block (pblock, tmp);
  434. }
  435. }
  436. /* Call malloc to allocate size bytes of memory, with special conditions:
  437. + if size == 0, return a malloced area of size 1,
  438. + if malloc returns NULL, issue a runtime error. */
  439. tree
  440. gfc_call_malloc (stmtblock_t * block, tree type, tree size)
  441. {
  442. tree tmp, msg, malloc_result, null_result, res, malloc_tree;
  443. stmtblock_t block2;
  444. size = gfc_evaluate_now (size, block);
  445. if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
  446. size = fold_convert (size_type_node, size);
  447. /* Create a variable to hold the result. */
  448. res = gfc_create_var (prvoid_type_node, NULL);
  449. /* Call malloc. */
  450. gfc_start_block (&block2);
  451. size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
  452. build_int_cst (size_type_node, 1));
  453. malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
  454. gfc_add_modify (&block2, res,
  455. fold_convert (prvoid_type_node,
  456. build_call_expr_loc (input_location,
  457. malloc_tree, 1, size)));
  458. /* Optionally check whether malloc was successful. */
  459. if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
  460. {
  461. null_result = fold_build2_loc (input_location, EQ_EXPR,
  462. boolean_type_node, res,
  463. build_int_cst (pvoid_type_node, 0));
  464. msg = gfc_build_addr_expr (pchar_type_node,
  465. gfc_build_localized_cstring_const ("Memory allocation failed"));
  466. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  467. null_result,
  468. build_call_expr_loc (input_location,
  469. gfor_fndecl_os_error, 1, msg),
  470. build_empty_stmt (input_location));
  471. gfc_add_expr_to_block (&block2, tmp);
  472. }
  473. malloc_result = gfc_finish_block (&block2);
  474. gfc_add_expr_to_block (block, malloc_result);
  475. if (type != NULL)
  476. res = fold_convert (type, res);
  477. return res;
  478. }
  479. /* Allocate memory, using an optional status argument.
  480. This function follows the following pseudo-code:
  481. void *
  482. allocate (size_t size, integer_type stat)
  483. {
  484. void *newmem;
  485. if (stat requested)
  486. stat = 0;
  487. newmem = malloc (MAX (size, 1));
  488. if (newmem == NULL)
  489. {
  490. if (stat)
  491. *stat = LIBERROR_ALLOCATION;
  492. else
  493. runtime_error ("Allocation would exceed memory limit");
  494. }
  495. return newmem;
  496. } */
  497. void
  498. gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
  499. tree size, tree status)
  500. {
  501. tree tmp, error_cond;
  502. stmtblock_t on_error;
  503. tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
  504. /* Evaluate size only once, and make sure it has the right type. */
  505. size = gfc_evaluate_now (size, block);
  506. if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
  507. size = fold_convert (size_type_node, size);
  508. /* If successful and stat= is given, set status to 0. */
  509. if (status != NULL_TREE)
  510. gfc_add_expr_to_block (block,
  511. fold_build2_loc (input_location, MODIFY_EXPR, status_type,
  512. status, build_int_cst (status_type, 0)));
  513. /* The allocation itself. */
  514. gfc_add_modify (block, pointer,
  515. fold_convert (TREE_TYPE (pointer),
  516. build_call_expr_loc (input_location,
  517. builtin_decl_explicit (BUILT_IN_MALLOC), 1,
  518. fold_build2_loc (input_location,
  519. MAX_EXPR, size_type_node, size,
  520. build_int_cst (size_type_node, 1)))));
  521. /* What to do in case of error. */
  522. gfc_start_block (&on_error);
  523. if (status != NULL_TREE)
  524. {
  525. gfc_add_expr_to_block (&on_error,
  526. build_predict_expr (PRED_FORTRAN_FAIL_ALLOC,
  527. NOT_TAKEN));
  528. tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type, status,
  529. build_int_cst (status_type, LIBERROR_ALLOCATION));
  530. gfc_add_expr_to_block (&on_error, tmp);
  531. }
  532. else
  533. {
  534. /* Here, os_error already implies PRED_NORETURN. */
  535. tmp = build_call_expr_loc (input_location, gfor_fndecl_os_error, 1,
  536. gfc_build_addr_expr (pchar_type_node,
  537. gfc_build_localized_cstring_const
  538. ("Allocation would exceed memory limit")));
  539. gfc_add_expr_to_block (&on_error, tmp);
  540. }
  541. error_cond = fold_build2_loc (input_location, EQ_EXPR,
  542. boolean_type_node, pointer,
  543. build_int_cst (prvoid_type_node, 0));
  544. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  545. error_cond, gfc_finish_block (&on_error),
  546. build_empty_stmt (input_location));
  547. gfc_add_expr_to_block (block, tmp);
  548. }
  549. /* Allocate memory, using an optional status argument.
  550. This function follows the following pseudo-code:
  551. void *
  552. allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
  553. {
  554. void *newmem;
  555. newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
  556. return newmem;
  557. } */
  558. static void
  559. gfc_allocate_using_lib (stmtblock_t * block, tree pointer, tree size,
  560. tree token, tree status, tree errmsg, tree errlen,
  561. bool lock_var)
  562. {
  563. tree tmp, pstat;
  564. gcc_assert (token != NULL_TREE);
  565. /* Evaluate size only once, and make sure it has the right type. */
  566. size = gfc_evaluate_now (size, block);
  567. if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
  568. size = fold_convert (size_type_node, size);
  569. /* The allocation itself. */
  570. if (status == NULL_TREE)
  571. pstat = null_pointer_node;
  572. else
  573. pstat = gfc_build_addr_expr (NULL_TREE, status);
  574. if (errmsg == NULL_TREE)
  575. {
  576. gcc_assert(errlen == NULL_TREE);
  577. errmsg = null_pointer_node;
  578. errlen = build_int_cst (integer_type_node, 0);
  579. }
  580. tmp = build_call_expr_loc (input_location,
  581. gfor_fndecl_caf_register, 6,
  582. fold_build2_loc (input_location,
  583. MAX_EXPR, size_type_node, size,
  584. build_int_cst (size_type_node, 1)),
  585. build_int_cst (integer_type_node,
  586. lock_var ? GFC_CAF_LOCK_ALLOC
  587. : GFC_CAF_COARRAY_ALLOC),
  588. token, pstat, errmsg, errlen);
  589. tmp = fold_build2_loc (input_location, MODIFY_EXPR,
  590. TREE_TYPE (pointer), pointer,
  591. fold_convert ( TREE_TYPE (pointer), tmp));
  592. gfc_add_expr_to_block (block, tmp);
  593. }
  594. /* Generate code for an ALLOCATE statement when the argument is an
  595. allocatable variable. If the variable is currently allocated, it is an
  596. error to allocate it again.
  597. This function follows the following pseudo-code:
  598. void *
  599. allocate_allocatable (void *mem, size_t size, integer_type stat)
  600. {
  601. if (mem == NULL)
  602. return allocate (size, stat);
  603. else
  604. {
  605. if (stat)
  606. stat = LIBERROR_ALLOCATION;
  607. else
  608. runtime_error ("Attempting to allocate already allocated variable");
  609. }
  610. }
  611. expr must be set to the original expression being allocated for its locus
  612. and variable name in case a runtime error has to be printed. */
  613. void
  614. gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size, tree token,
  615. tree status, tree errmsg, tree errlen, tree label_finish,
  616. gfc_expr* expr)
  617. {
  618. stmtblock_t alloc_block;
  619. tree tmp, null_mem, alloc, error;
  620. tree type = TREE_TYPE (mem);
  621. if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
  622. size = fold_convert (size_type_node, size);
  623. null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
  624. boolean_type_node, mem,
  625. build_int_cst (type, 0)),
  626. PRED_FORTRAN_FAIL_ALLOC);
  627. /* If mem is NULL, we call gfc_allocate_using_malloc or
  628. gfc_allocate_using_lib. */
  629. gfc_start_block (&alloc_block);
  630. if (flag_coarray == GFC_FCOARRAY_LIB
  631. && gfc_expr_attr (expr).codimension)
  632. {
  633. tree cond;
  634. bool lock_var = expr->ts.type == BT_DERIVED
  635. && expr->ts.u.derived->from_intmod
  636. == INTMOD_ISO_FORTRAN_ENV
  637. && expr->ts.u.derived->intmod_sym_id
  638. == ISOFORTRAN_LOCK_TYPE;
  639. /* In the front end, we represent the lock variable as pointer. However,
  640. the FE only passes the pointer around and leaves the actual
  641. representation to the library. Hence, we have to convert back to the
  642. number of elements. */
  643. if (lock_var)
  644. size = fold_build2_loc (input_location, TRUNC_DIV_EXPR, size_type_node,
  645. size, TYPE_SIZE_UNIT (ptr_type_node));
  646. gfc_allocate_using_lib (&alloc_block, mem, size, token, status,
  647. errmsg, errlen, lock_var);
  648. if (status != NULL_TREE)
  649. {
  650. TREE_USED (label_finish) = 1;
  651. tmp = build1_v (GOTO_EXPR, label_finish);
  652. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  653. status, build_zero_cst (TREE_TYPE (status)));
  654. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  655. gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
  656. tmp, build_empty_stmt (input_location));
  657. gfc_add_expr_to_block (&alloc_block, tmp);
  658. }
  659. }
  660. else
  661. gfc_allocate_using_malloc (&alloc_block, mem, size, status);
  662. alloc = gfc_finish_block (&alloc_block);
  663. /* If mem is not NULL, we issue a runtime error or set the
  664. status variable. */
  665. if (expr)
  666. {
  667. tree varname;
  668. gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
  669. varname = gfc_build_cstring_const (expr->symtree->name);
  670. varname = gfc_build_addr_expr (pchar_type_node, varname);
  671. error = gfc_trans_runtime_error (true, &expr->where,
  672. "Attempting to allocate already"
  673. " allocated variable '%s'",
  674. varname);
  675. }
  676. else
  677. error = gfc_trans_runtime_error (true, NULL,
  678. "Attempting to allocate already allocated"
  679. " variable");
  680. if (status != NULL_TREE)
  681. {
  682. tree status_type = TREE_TYPE (status);
  683. error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
  684. status, build_int_cst (status_type, LIBERROR_ALLOCATION));
  685. }
  686. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
  687. error, alloc);
  688. gfc_add_expr_to_block (block, tmp);
  689. }
  690. /* Free a given variable, if it's not NULL. */
  691. tree
  692. gfc_call_free (tree var)
  693. {
  694. stmtblock_t block;
  695. tree tmp, cond, call;
  696. if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
  697. var = fold_convert (pvoid_type_node, var);
  698. gfc_start_block (&block);
  699. var = gfc_evaluate_now (var, &block);
  700. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
  701. build_int_cst (pvoid_type_node, 0));
  702. call = build_call_expr_loc (input_location,
  703. builtin_decl_explicit (BUILT_IN_FREE),
  704. 1, var);
  705. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
  706. build_empty_stmt (input_location));
  707. gfc_add_expr_to_block (&block, tmp);
  708. return gfc_finish_block (&block);
  709. }
  710. /* Build a call to a FINAL procedure, which finalizes "var". */
  711. static tree
  712. gfc_build_final_call (gfc_typespec ts, gfc_expr *final_wrapper, gfc_expr *var,
  713. bool fini_coarray, gfc_expr *class_size)
  714. {
  715. stmtblock_t block;
  716. gfc_se se;
  717. tree final_fndecl, array, size, tmp;
  718. symbol_attribute attr;
  719. gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
  720. gcc_assert (var);
  721. gfc_start_block (&block);
  722. gfc_init_se (&se, NULL);
  723. gfc_conv_expr (&se, final_wrapper);
  724. final_fndecl = se.expr;
  725. if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
  726. final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
  727. if (ts.type == BT_DERIVED)
  728. {
  729. tree elem_size;
  730. gcc_assert (!class_size);
  731. elem_size = gfc_typenode_for_spec (&ts);
  732. elem_size = TYPE_SIZE_UNIT (elem_size);
  733. size = fold_convert (gfc_array_index_type, elem_size);
  734. gfc_init_se (&se, NULL);
  735. se.want_pointer = 1;
  736. if (var->rank)
  737. {
  738. se.descriptor_only = 1;
  739. gfc_conv_expr_descriptor (&se, var);
  740. array = se.expr;
  741. }
  742. else
  743. {
  744. gfc_conv_expr (&se, var);
  745. gcc_assert (se.pre.head == NULL_TREE && se.post.head == NULL_TREE);
  746. array = se.expr;
  747. /* No copy back needed, hence set attr's allocatable/pointer
  748. to zero. */
  749. gfc_clear_attr (&attr);
  750. gfc_init_se (&se, NULL);
  751. array = gfc_conv_scalar_to_descriptor (&se, array, attr);
  752. gcc_assert (se.post.head == NULL_TREE);
  753. }
  754. }
  755. else
  756. {
  757. gfc_expr *array_expr;
  758. gcc_assert (class_size);
  759. gfc_init_se (&se, NULL);
  760. gfc_conv_expr (&se, class_size);
  761. gfc_add_block_to_block (&block, &se.pre);
  762. gcc_assert (se.post.head == NULL_TREE);
  763. size = se.expr;
  764. array_expr = gfc_copy_expr (var);
  765. gfc_init_se (&se, NULL);
  766. se.want_pointer = 1;
  767. if (array_expr->rank)
  768. {
  769. gfc_add_class_array_ref (array_expr);
  770. se.descriptor_only = 1;
  771. gfc_conv_expr_descriptor (&se, array_expr);
  772. array = se.expr;
  773. }
  774. else
  775. {
  776. gfc_add_data_component (array_expr);
  777. gfc_conv_expr (&se, array_expr);
  778. gfc_add_block_to_block (&block, &se.pre);
  779. gcc_assert (se.post.head == NULL_TREE);
  780. array = se.expr;
  781. if (TREE_CODE (array) == ADDR_EXPR
  782. && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (array, 0))))
  783. tmp = TREE_OPERAND (array, 0);
  784. if (!gfc_is_coarray (array_expr))
  785. {
  786. /* No copy back needed, hence set attr's allocatable/pointer
  787. to zero. */
  788. gfc_clear_attr (&attr);
  789. gfc_init_se (&se, NULL);
  790. array = gfc_conv_scalar_to_descriptor (&se, array, attr);
  791. }
  792. gcc_assert (se.post.head == NULL_TREE);
  793. }
  794. gfc_free_expr (array_expr);
  795. }
  796. if (!POINTER_TYPE_P (TREE_TYPE (array)))
  797. array = gfc_build_addr_expr (NULL, array);
  798. gfc_add_block_to_block (&block, &se.pre);
  799. tmp = build_call_expr_loc (input_location,
  800. final_fndecl, 3, array,
  801. size, fini_coarray ? boolean_true_node
  802. : boolean_false_node);
  803. gfc_add_block_to_block (&block, &se.post);
  804. gfc_add_expr_to_block (&block, tmp);
  805. return gfc_finish_block (&block);
  806. }
  807. bool
  808. gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
  809. bool fini_coarray)
  810. {
  811. gfc_se se;
  812. stmtblock_t block2;
  813. tree final_fndecl, size, array, tmp, cond;
  814. symbol_attribute attr;
  815. gfc_expr *final_expr = NULL;
  816. if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
  817. return false;
  818. gfc_init_block (&block2);
  819. if (comp->ts.type == BT_DERIVED)
  820. {
  821. if (comp->attr.pointer)
  822. return false;
  823. gfc_is_finalizable (comp->ts.u.derived, &final_expr);
  824. if (!final_expr)
  825. return false;
  826. gfc_init_se (&se, NULL);
  827. gfc_conv_expr (&se, final_expr);
  828. final_fndecl = se.expr;
  829. size = gfc_typenode_for_spec (&comp->ts);
  830. size = TYPE_SIZE_UNIT (size);
  831. size = fold_convert (gfc_array_index_type, size);
  832. array = decl;
  833. }
  834. else /* comp->ts.type == BT_CLASS. */
  835. {
  836. if (CLASS_DATA (comp)->attr.class_pointer)
  837. return false;
  838. gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
  839. final_fndecl = gfc_class_vtab_final_get (decl);
  840. size = gfc_class_vtab_size_get (decl);
  841. array = gfc_class_data_get (decl);
  842. }
  843. if (comp->attr.allocatable
  844. || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
  845. {
  846. tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
  847. ? gfc_conv_descriptor_data_get (array) : array;
  848. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  849. tmp, fold_convert (TREE_TYPE (tmp),
  850. null_pointer_node));
  851. }
  852. else
  853. cond = boolean_true_node;
  854. if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
  855. {
  856. gfc_clear_attr (&attr);
  857. gfc_init_se (&se, NULL);
  858. array = gfc_conv_scalar_to_descriptor (&se, array, attr);
  859. gfc_add_block_to_block (&block2, &se.pre);
  860. gcc_assert (se.post.head == NULL_TREE);
  861. }
  862. if (!POINTER_TYPE_P (TREE_TYPE (array)))
  863. array = gfc_build_addr_expr (NULL, array);
  864. if (!final_expr)
  865. {
  866. tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  867. final_fndecl,
  868. fold_convert (TREE_TYPE (final_fndecl),
  869. null_pointer_node));
  870. cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
  871. boolean_type_node, cond, tmp);
  872. }
  873. if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
  874. final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
  875. tmp = build_call_expr_loc (input_location,
  876. final_fndecl, 3, array,
  877. size, fini_coarray ? boolean_true_node
  878. : boolean_false_node);
  879. gfc_add_expr_to_block (&block2, tmp);
  880. tmp = gfc_finish_block (&block2);
  881. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
  882. build_empty_stmt (input_location));
  883. gfc_add_expr_to_block (block, tmp);
  884. return true;
  885. }
  886. /* Add a call to the finalizer, using the passed *expr. Returns
  887. true when a finalizer call has been inserted. */
  888. bool
  889. gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2)
  890. {
  891. tree tmp;
  892. gfc_ref *ref;
  893. gfc_expr *expr;
  894. gfc_expr *final_expr = NULL;
  895. gfc_expr *elem_size = NULL;
  896. bool has_finalizer = false;
  897. if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
  898. return false;
  899. if (expr2->ts.type == BT_DERIVED)
  900. {
  901. gfc_is_finalizable (expr2->ts.u.derived, &final_expr);
  902. if (!final_expr)
  903. return false;
  904. }
  905. /* If we have a class array, we need go back to the class
  906. container. */
  907. expr = gfc_copy_expr (expr2);
  908. if (expr->ref && expr->ref->next && !expr->ref->next->next
  909. && expr->ref->next->type == REF_ARRAY
  910. && expr->ref->type == REF_COMPONENT
  911. && strcmp (expr->ref->u.c.component->name, "_data") == 0)
  912. {
  913. gfc_free_ref_list (expr->ref);
  914. expr->ref = NULL;
  915. }
  916. else
  917. for (ref = expr->ref; ref; ref = ref->next)
  918. if (ref->next && ref->next->next && !ref->next->next->next
  919. && ref->next->next->type == REF_ARRAY
  920. && ref->next->type == REF_COMPONENT
  921. && strcmp (ref->next->u.c.component->name, "_data") == 0)
  922. {
  923. gfc_free_ref_list (ref->next);
  924. ref->next = NULL;
  925. }
  926. if (expr->ts.type == BT_CLASS)
  927. {
  928. has_finalizer = gfc_is_finalizable (expr->ts.u.derived, NULL);
  929. if (!expr2->rank && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
  930. expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
  931. final_expr = gfc_copy_expr (expr);
  932. gfc_add_vptr_component (final_expr);
  933. gfc_add_component_ref (final_expr, "_final");
  934. elem_size = gfc_copy_expr (expr);
  935. gfc_add_vptr_component (elem_size);
  936. gfc_add_component_ref (elem_size, "_size");
  937. }
  938. gcc_assert (final_expr->expr_type == EXPR_VARIABLE);
  939. tmp = gfc_build_final_call (expr->ts, final_expr, expr,
  940. false, elem_size);
  941. if (expr->ts.type == BT_CLASS && !has_finalizer)
  942. {
  943. tree cond;
  944. gfc_se se;
  945. gfc_init_se (&se, NULL);
  946. se.want_pointer = 1;
  947. gfc_conv_expr (&se, final_expr);
  948. cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  949. se.expr, build_int_cst (TREE_TYPE (se.expr), 0));
  950. /* For CLASS(*) not only sym->_vtab->_final can be NULL
  951. but already sym->_vtab itself. */
  952. if (UNLIMITED_POLY (expr))
  953. {
  954. tree cond2;
  955. gfc_expr *vptr_expr;
  956. vptr_expr = gfc_copy_expr (expr);
  957. gfc_add_vptr_component (vptr_expr);
  958. gfc_init_se (&se, NULL);
  959. se.want_pointer = 1;
  960. gfc_conv_expr (&se, vptr_expr);
  961. gfc_free_expr (vptr_expr);
  962. cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  963. se.expr,
  964. build_int_cst (TREE_TYPE (se.expr), 0));
  965. cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
  966. boolean_type_node, cond2, cond);
  967. }
  968. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  969. cond, tmp, build_empty_stmt (input_location));
  970. }
  971. gfc_add_expr_to_block (block, tmp);
  972. return true;
  973. }
  974. /* User-deallocate; we emit the code directly from the front-end, and the
  975. logic is the same as the previous library function:
  976. void
  977. deallocate (void *pointer, GFC_INTEGER_4 * stat)
  978. {
  979. if (!pointer)
  980. {
  981. if (stat)
  982. *stat = 1;
  983. else
  984. runtime_error ("Attempt to DEALLOCATE unallocated memory.");
  985. }
  986. else
  987. {
  988. free (pointer);
  989. if (stat)
  990. *stat = 0;
  991. }
  992. }
  993. In this front-end version, status doesn't have to be GFC_INTEGER_4.
  994. Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
  995. even when no status variable is passed to us (this is used for
  996. unconditional deallocation generated by the front-end at end of
  997. each procedure).
  998. If a runtime-message is possible, `expr' must point to the original
  999. expression being deallocated for its locus and variable name.
  1000. For coarrays, "pointer" must be the array descriptor and not its
  1001. "data" component. */
  1002. tree
  1003. gfc_deallocate_with_status (tree pointer, tree status, tree errmsg,
  1004. tree errlen, tree label_finish,
  1005. bool can_fail, gfc_expr* expr, bool coarray)
  1006. {
  1007. stmtblock_t null, non_null;
  1008. tree cond, tmp, error;
  1009. tree status_type = NULL_TREE;
  1010. tree caf_decl = NULL_TREE;
  1011. if (coarray)
  1012. {
  1013. gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)));
  1014. caf_decl = pointer;
  1015. pointer = gfc_conv_descriptor_data_get (caf_decl);
  1016. STRIP_NOPS (pointer);
  1017. }
  1018. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
  1019. build_int_cst (TREE_TYPE (pointer), 0));
  1020. /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
  1021. we emit a runtime error. */
  1022. gfc_start_block (&null);
  1023. if (!can_fail)
  1024. {
  1025. tree varname;
  1026. gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
  1027. varname = gfc_build_cstring_const (expr->symtree->name);
  1028. varname = gfc_build_addr_expr (pchar_type_node, varname);
  1029. error = gfc_trans_runtime_error (true, &expr->where,
  1030. "Attempt to DEALLOCATE unallocated '%s'",
  1031. varname);
  1032. }
  1033. else
  1034. error = build_empty_stmt (input_location);
  1035. if (status != NULL_TREE && !integer_zerop (status))
  1036. {
  1037. tree cond2;
  1038. status_type = TREE_TYPE (TREE_TYPE (status));
  1039. cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  1040. status, build_int_cst (TREE_TYPE (status), 0));
  1041. tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
  1042. fold_build1_loc (input_location, INDIRECT_REF,
  1043. status_type, status),
  1044. build_int_cst (status_type, 1));
  1045. error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  1046. cond2, tmp, error);
  1047. }
  1048. gfc_add_expr_to_block (&null, error);
  1049. /* When POINTER is not NULL, we free it. */
  1050. gfc_start_block (&non_null);
  1051. gfc_add_finalizer_call (&non_null, expr);
  1052. if (!coarray || flag_coarray != GFC_FCOARRAY_LIB)
  1053. {
  1054. tmp = build_call_expr_loc (input_location,
  1055. builtin_decl_explicit (BUILT_IN_FREE), 1,
  1056. fold_convert (pvoid_type_node, pointer));
  1057. gfc_add_expr_to_block (&non_null, tmp);
  1058. if (status != NULL_TREE && !integer_zerop (status))
  1059. {
  1060. /* We set STATUS to zero if it is present. */
  1061. tree status_type = TREE_TYPE (TREE_TYPE (status));
  1062. tree cond2;
  1063. cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  1064. status,
  1065. build_int_cst (TREE_TYPE (status), 0));
  1066. tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
  1067. fold_build1_loc (input_location, INDIRECT_REF,
  1068. status_type, status),
  1069. build_int_cst (status_type, 0));
  1070. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  1071. gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
  1072. tmp, build_empty_stmt (input_location));
  1073. gfc_add_expr_to_block (&non_null, tmp);
  1074. }
  1075. }
  1076. else
  1077. {
  1078. tree caf_type, token, cond2;
  1079. tree pstat = null_pointer_node;
  1080. if (errmsg == NULL_TREE)
  1081. {
  1082. gcc_assert (errlen == NULL_TREE);
  1083. errmsg = null_pointer_node;
  1084. errlen = build_zero_cst (integer_type_node);
  1085. }
  1086. else
  1087. {
  1088. gcc_assert (errlen != NULL_TREE);
  1089. if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
  1090. errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
  1091. }
  1092. caf_type = TREE_TYPE (caf_decl);
  1093. if (status != NULL_TREE && !integer_zerop (status))
  1094. {
  1095. gcc_assert (status_type == integer_type_node);
  1096. pstat = status;
  1097. }
  1098. if (GFC_DESCRIPTOR_TYPE_P (caf_type)
  1099. && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
  1100. token = gfc_conv_descriptor_token (caf_decl);
  1101. else if (DECL_LANG_SPECIFIC (caf_decl)
  1102. && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
  1103. token = GFC_DECL_TOKEN (caf_decl);
  1104. else
  1105. {
  1106. gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
  1107. && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
  1108. token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
  1109. }
  1110. token = gfc_build_addr_expr (NULL_TREE, token);
  1111. tmp = build_call_expr_loc (input_location,
  1112. gfor_fndecl_caf_deregister, 4,
  1113. token, pstat, errmsg, errlen);
  1114. gfc_add_expr_to_block (&non_null, tmp);
  1115. if (status != NULL_TREE)
  1116. {
  1117. tree stat = build_fold_indirect_ref_loc (input_location, status);
  1118. TREE_USED (label_finish) = 1;
  1119. tmp = build1_v (GOTO_EXPR, label_finish);
  1120. cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  1121. stat, build_zero_cst (TREE_TYPE (stat)));
  1122. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  1123. gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
  1124. tmp, build_empty_stmt (input_location));
  1125. gfc_add_expr_to_block (&non_null, tmp);
  1126. }
  1127. }
  1128. return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
  1129. gfc_finish_block (&null),
  1130. gfc_finish_block (&non_null));
  1131. }
  1132. /* Generate code for deallocation of allocatable scalars (variables or
  1133. components). Before the object itself is freed, any allocatable
  1134. subcomponents are being deallocated. */
  1135. tree
  1136. gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
  1137. gfc_expr* expr, gfc_typespec ts)
  1138. {
  1139. stmtblock_t null, non_null;
  1140. tree cond, tmp, error;
  1141. bool finalizable;
  1142. cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
  1143. build_int_cst (TREE_TYPE (pointer), 0));
  1144. /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
  1145. we emit a runtime error. */
  1146. gfc_start_block (&null);
  1147. if (!can_fail)
  1148. {
  1149. tree varname;
  1150. gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
  1151. varname = gfc_build_cstring_const (expr->symtree->name);
  1152. varname = gfc_build_addr_expr (pchar_type_node, varname);
  1153. error = gfc_trans_runtime_error (true, &expr->where,
  1154. "Attempt to DEALLOCATE unallocated '%s'",
  1155. varname);
  1156. }
  1157. else
  1158. error = build_empty_stmt (input_location);
  1159. if (status != NULL_TREE && !integer_zerop (status))
  1160. {
  1161. tree status_type = TREE_TYPE (TREE_TYPE (status));
  1162. tree cond2;
  1163. cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  1164. status, build_int_cst (TREE_TYPE (status), 0));
  1165. tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
  1166. fold_build1_loc (input_location, INDIRECT_REF,
  1167. status_type, status),
  1168. build_int_cst (status_type, 1));
  1169. error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  1170. cond2, tmp, error);
  1171. }
  1172. gfc_add_expr_to_block (&null, error);
  1173. /* When POINTER is not NULL, we free it. */
  1174. gfc_start_block (&non_null);
  1175. /* Free allocatable components. */
  1176. finalizable = gfc_add_finalizer_call (&non_null, expr);
  1177. if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
  1178. {
  1179. tmp = build_fold_indirect_ref_loc (input_location, pointer);
  1180. tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
  1181. gfc_add_expr_to_block (&non_null, tmp);
  1182. }
  1183. tmp = build_call_expr_loc (input_location,
  1184. builtin_decl_explicit (BUILT_IN_FREE), 1,
  1185. fold_convert (pvoid_type_node, pointer));
  1186. gfc_add_expr_to_block (&non_null, tmp);
  1187. if (status != NULL_TREE && !integer_zerop (status))
  1188. {
  1189. /* We set STATUS to zero if it is present. */
  1190. tree status_type = TREE_TYPE (TREE_TYPE (status));
  1191. tree cond2;
  1192. cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  1193. status, build_int_cst (TREE_TYPE (status), 0));
  1194. tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
  1195. fold_build1_loc (input_location, INDIRECT_REF,
  1196. status_type, status),
  1197. build_int_cst (status_type, 0));
  1198. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
  1199. tmp, build_empty_stmt (input_location));
  1200. gfc_add_expr_to_block (&non_null, tmp);
  1201. }
  1202. return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
  1203. gfc_finish_block (&null),
  1204. gfc_finish_block (&non_null));
  1205. }
  1206. /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
  1207. following pseudo-code:
  1208. void *
  1209. internal_realloc (void *mem, size_t size)
  1210. {
  1211. res = realloc (mem, size);
  1212. if (!res && size != 0)
  1213. _gfortran_os_error ("Allocation would exceed memory limit");
  1214. return res;
  1215. } */
  1216. tree
  1217. gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
  1218. {
  1219. tree msg, res, nonzero, null_result, tmp;
  1220. tree type = TREE_TYPE (mem);
  1221. size = gfc_evaluate_now (size, block);
  1222. if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
  1223. size = fold_convert (size_type_node, size);
  1224. /* Create a variable to hold the result. */
  1225. res = gfc_create_var (type, NULL);
  1226. /* Call realloc and check the result. */
  1227. tmp = build_call_expr_loc (input_location,
  1228. builtin_decl_explicit (BUILT_IN_REALLOC), 2,
  1229. fold_convert (pvoid_type_node, mem), size);
  1230. gfc_add_modify (block, res, fold_convert (type, tmp));
  1231. null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
  1232. res, build_int_cst (pvoid_type_node, 0));
  1233. nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
  1234. build_int_cst (size_type_node, 0));
  1235. null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
  1236. null_result, nonzero);
  1237. msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
  1238. ("Allocation would exceed memory limit"));
  1239. tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  1240. null_result,
  1241. build_call_expr_loc (input_location,
  1242. gfor_fndecl_os_error, 1, msg),
  1243. build_empty_stmt (input_location));
  1244. gfc_add_expr_to_block (block, tmp);
  1245. return res;
  1246. }
  1247. /* Add an expression to another one, either at the front or the back. */
  1248. static void
  1249. add_expr_to_chain (tree* chain, tree expr, bool front)
  1250. {
  1251. if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
  1252. return;
  1253. if (*chain)
  1254. {
  1255. if (TREE_CODE (*chain) != STATEMENT_LIST)
  1256. {
  1257. tree tmp;
  1258. tmp = *chain;
  1259. *chain = NULL_TREE;
  1260. append_to_statement_list (tmp, chain);
  1261. }
  1262. if (front)
  1263. {
  1264. tree_stmt_iterator i;
  1265. i = tsi_start (*chain);
  1266. tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
  1267. }
  1268. else
  1269. append_to_statement_list (expr, chain);
  1270. }
  1271. else
  1272. *chain = expr;
  1273. }
  1274. /* Add a statement at the end of a block. */
  1275. void
  1276. gfc_add_expr_to_block (stmtblock_t * block, tree expr)
  1277. {
  1278. gcc_assert (block);
  1279. add_expr_to_chain (&block->head, expr, false);
  1280. }
  1281. /* Add a statement at the beginning of a block. */
  1282. void
  1283. gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
  1284. {
  1285. gcc_assert (block);
  1286. add_expr_to_chain (&block->head, expr, true);
  1287. }
  1288. /* Add a block the end of a block. */
  1289. void
  1290. gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
  1291. {
  1292. gcc_assert (append);
  1293. gcc_assert (!append->has_scope);
  1294. gfc_add_expr_to_block (block, append->head);
  1295. append->head = NULL_TREE;
  1296. }
  1297. /* Save the current locus. The structure may not be complete, and should
  1298. only be used with gfc_restore_backend_locus. */
  1299. void
  1300. gfc_save_backend_locus (locus * loc)
  1301. {
  1302. loc->lb = XCNEW (gfc_linebuf);
  1303. loc->lb->location = input_location;
  1304. loc->lb->file = gfc_current_backend_file;
  1305. }
  1306. /* Set the current locus. */
  1307. void
  1308. gfc_set_backend_locus (locus * loc)
  1309. {
  1310. gfc_current_backend_file = loc->lb->file;
  1311. input_location = loc->lb->location;
  1312. }
  1313. /* Restore the saved locus. Only used in conjunction with
  1314. gfc_save_backend_locus, to free the memory when we are done. */
  1315. void
  1316. gfc_restore_backend_locus (locus * loc)
  1317. {
  1318. gfc_set_backend_locus (loc);
  1319. free (loc->lb);
  1320. }
  1321. /* Translate an executable statement. The tree cond is used by gfc_trans_do.
  1322. This static function is wrapped by gfc_trans_code_cond and
  1323. gfc_trans_code. */
  1324. static tree
  1325. trans_code (gfc_code * code, tree cond)
  1326. {
  1327. stmtblock_t block;
  1328. tree res;
  1329. if (!code)
  1330. return build_empty_stmt (input_location);
  1331. gfc_start_block (&block);
  1332. /* Translate statements one by one into GENERIC trees until we reach
  1333. the end of this gfc_code branch. */
  1334. for (; code; code = code->next)
  1335. {
  1336. if (code->here != 0)
  1337. {
  1338. res = gfc_trans_label_here (code);
  1339. gfc_add_expr_to_block (&block, res);
  1340. }
  1341. gfc_set_backend_locus (&code->loc);
  1342. switch (code->op)
  1343. {
  1344. case EXEC_NOP:
  1345. case EXEC_END_BLOCK:
  1346. case EXEC_END_NESTED_BLOCK:
  1347. case EXEC_END_PROCEDURE:
  1348. res = NULL_TREE;
  1349. break;
  1350. case EXEC_ASSIGN:
  1351. if (code->expr1->ts.type == BT_CLASS)
  1352. res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
  1353. else
  1354. res = gfc_trans_assign (code);
  1355. break;
  1356. case EXEC_LABEL_ASSIGN:
  1357. res = gfc_trans_label_assign (code);
  1358. break;
  1359. case EXEC_POINTER_ASSIGN:
  1360. if (code->expr1->ts.type == BT_CLASS)
  1361. res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
  1362. else if (UNLIMITED_POLY (code->expr2)
  1363. && code->expr1->ts.type == BT_DERIVED
  1364. && (code->expr1->ts.u.derived->attr.sequence
  1365. || code->expr1->ts.u.derived->attr.is_bind_c))
  1366. /* F2003: C717 */
  1367. res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
  1368. else
  1369. res = gfc_trans_pointer_assign (code);
  1370. break;
  1371. case EXEC_INIT_ASSIGN:
  1372. if (code->expr1->ts.type == BT_CLASS)
  1373. res = gfc_trans_class_init_assign (code);
  1374. else
  1375. res = gfc_trans_init_assign (code);
  1376. break;
  1377. case EXEC_CONTINUE:
  1378. res = NULL_TREE;
  1379. break;
  1380. case EXEC_CRITICAL:
  1381. res = gfc_trans_critical (code);
  1382. break;
  1383. case EXEC_CYCLE:
  1384. res = gfc_trans_cycle (code);
  1385. break;
  1386. case EXEC_EXIT:
  1387. res = gfc_trans_exit (code);
  1388. break;
  1389. case EXEC_GOTO:
  1390. res = gfc_trans_goto (code);
  1391. break;
  1392. case EXEC_ENTRY:
  1393. res = gfc_trans_entry (code);
  1394. break;
  1395. case EXEC_PAUSE:
  1396. res = gfc_trans_pause (code);
  1397. break;
  1398. case EXEC_STOP:
  1399. case EXEC_ERROR_STOP:
  1400. res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
  1401. break;
  1402. case EXEC_CALL:
  1403. /* For MVBITS we've got the special exception that we need a
  1404. dependency check, too. */
  1405. {
  1406. bool is_mvbits = false;
  1407. if (code->resolved_isym)
  1408. {
  1409. res = gfc_conv_intrinsic_subroutine (code);
  1410. if (res != NULL_TREE)
  1411. break;
  1412. }
  1413. if (code->resolved_isym
  1414. && code->resolved_isym->id == GFC_ISYM_MVBITS)
  1415. is_mvbits = true;
  1416. res = gfc_trans_call (code, is_mvbits, NULL_TREE,
  1417. NULL_TREE, false);
  1418. }
  1419. break;
  1420. case EXEC_CALL_PPC:
  1421. res = gfc_trans_call (code, false, NULL_TREE,
  1422. NULL_TREE, false);
  1423. break;
  1424. case EXEC_ASSIGN_CALL:
  1425. res = gfc_trans_call (code, true, NULL_TREE,
  1426. NULL_TREE, false);
  1427. break;
  1428. case EXEC_RETURN:
  1429. res = gfc_trans_return (code);
  1430. break;
  1431. case EXEC_IF:
  1432. res = gfc_trans_if (code);
  1433. break;
  1434. case EXEC_ARITHMETIC_IF:
  1435. res = gfc_trans_arithmetic_if (code);
  1436. break;
  1437. case EXEC_BLOCK:
  1438. res = gfc_trans_block_construct (code);
  1439. break;
  1440. case EXEC_DO:
  1441. res = gfc_trans_do (code, cond);
  1442. break;
  1443. case EXEC_DO_CONCURRENT:
  1444. res = gfc_trans_do_concurrent (code);
  1445. break;
  1446. case EXEC_DO_WHILE:
  1447. res = gfc_trans_do_while (code);
  1448. break;
  1449. case EXEC_SELECT:
  1450. res = gfc_trans_select (code);
  1451. break;
  1452. case EXEC_SELECT_TYPE:
  1453. /* Do nothing. SELECT TYPE statements should be transformed into
  1454. an ordinary SELECT CASE at resolution stage.
  1455. TODO: Add an error message here once this is done. */
  1456. res = NULL_TREE;
  1457. break;
  1458. case EXEC_FLUSH:
  1459. res = gfc_trans_flush (code);
  1460. break;
  1461. case EXEC_SYNC_ALL:
  1462. case EXEC_SYNC_IMAGES:
  1463. case EXEC_SYNC_MEMORY:
  1464. res = gfc_trans_sync (code, code->op);
  1465. break;
  1466. case EXEC_LOCK:
  1467. case EXEC_UNLOCK:
  1468. res = gfc_trans_lock_unlock (code, code->op);
  1469. break;
  1470. case EXEC_FORALL:
  1471. res = gfc_trans_forall (code);
  1472. break;
  1473. case EXEC_WHERE:
  1474. res = gfc_trans_where (code);
  1475. break;
  1476. case EXEC_ALLOCATE:
  1477. res = gfc_trans_allocate (code);
  1478. break;
  1479. case EXEC_DEALLOCATE:
  1480. res = gfc_trans_deallocate (code);
  1481. break;
  1482. case EXEC_OPEN:
  1483. res = gfc_trans_open (code);
  1484. break;
  1485. case EXEC_CLOSE:
  1486. res = gfc_trans_close (code);
  1487. break;
  1488. case EXEC_READ:
  1489. res = gfc_trans_read (code);
  1490. break;
  1491. case EXEC_WRITE:
  1492. res = gfc_trans_write (code);
  1493. break;
  1494. case EXEC_IOLENGTH:
  1495. res = gfc_trans_iolength (code);
  1496. break;
  1497. case EXEC_BACKSPACE:
  1498. res = gfc_trans_backspace (code);
  1499. break;
  1500. case EXEC_ENDFILE:
  1501. res = gfc_trans_endfile (code);
  1502. break;
  1503. case EXEC_INQUIRE:
  1504. res = gfc_trans_inquire (code);
  1505. break;
  1506. case EXEC_WAIT:
  1507. res = gfc_trans_wait (code);
  1508. break;
  1509. case EXEC_REWIND:
  1510. res = gfc_trans_rewind (code);
  1511. break;
  1512. case EXEC_TRANSFER:
  1513. res = gfc_trans_transfer (code);
  1514. break;
  1515. case EXEC_DT_END:
  1516. res = gfc_trans_dt_end (code);
  1517. break;
  1518. case EXEC_OMP_ATOMIC:
  1519. case EXEC_OMP_BARRIER:
  1520. case EXEC_OMP_CANCEL:
  1521. case EXEC_OMP_CANCELLATION_POINT:
  1522. case EXEC_OMP_CRITICAL:
  1523. case EXEC_OMP_DISTRIBUTE:
  1524. case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
  1525. case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
  1526. case EXEC_OMP_DISTRIBUTE_SIMD:
  1527. case EXEC_OMP_DO:
  1528. case EXEC_OMP_DO_SIMD:
  1529. case EXEC_OMP_FLUSH:
  1530. case EXEC_OMP_MASTER:
  1531. case EXEC_OMP_ORDERED:
  1532. case EXEC_OMP_PARALLEL:
  1533. case EXEC_OMP_PARALLEL_DO:
  1534. case EXEC_OMP_PARALLEL_DO_SIMD:
  1535. case EXEC_OMP_PARALLEL_SECTIONS:
  1536. case EXEC_OMP_PARALLEL_WORKSHARE:
  1537. case EXEC_OMP_SECTIONS:
  1538. case EXEC_OMP_SIMD:
  1539. case EXEC_OMP_SINGLE:
  1540. case EXEC_OMP_TARGET:
  1541. case EXEC_OMP_TARGET_DATA:
  1542. case EXEC_OMP_TARGET_TEAMS:
  1543. case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
  1544. case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
  1545. case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  1546. case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
  1547. case EXEC_OMP_TARGET_UPDATE:
  1548. case EXEC_OMP_TASK:
  1549. case EXEC_OMP_TASKGROUP:
  1550. case EXEC_OMP_TASKWAIT:
  1551. case EXEC_OMP_TASKYIELD:
  1552. case EXEC_OMP_TEAMS:
  1553. case EXEC_OMP_TEAMS_DISTRIBUTE:
  1554. case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
  1555. case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  1556. case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
  1557. case EXEC_OMP_WORKSHARE:
  1558. res = gfc_trans_omp_directive (code);
  1559. break;
  1560. case EXEC_OACC_CACHE:
  1561. case EXEC_OACC_WAIT:
  1562. case EXEC_OACC_UPDATE:
  1563. case EXEC_OACC_LOOP:
  1564. case EXEC_OACC_HOST_DATA:
  1565. case EXEC_OACC_DATA:
  1566. case EXEC_OACC_KERNELS:
  1567. case EXEC_OACC_KERNELS_LOOP:
  1568. case EXEC_OACC_PARALLEL:
  1569. case EXEC_OACC_PARALLEL_LOOP:
  1570. case EXEC_OACC_ENTER_DATA:
  1571. case EXEC_OACC_EXIT_DATA:
  1572. res = gfc_trans_oacc_directive (code);
  1573. break;
  1574. default:
  1575. gfc_internal_error ("gfc_trans_code(): Bad statement code");
  1576. }
  1577. gfc_set_backend_locus (&code->loc);
  1578. if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
  1579. {
  1580. if (TREE_CODE (res) != STATEMENT_LIST)
  1581. SET_EXPR_LOCATION (res, input_location);
  1582. /* Add the new statement to the block. */
  1583. gfc_add_expr_to_block (&block, res);
  1584. }
  1585. }
  1586. /* Return the finished block. */
  1587. return gfc_finish_block (&block);
  1588. }
  1589. /* Translate an executable statement with condition, cond. The condition is
  1590. used by gfc_trans_do to test for IO result conditions inside implied
  1591. DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
  1592. tree
  1593. gfc_trans_code_cond (gfc_code * code, tree cond)
  1594. {
  1595. return trans_code (code, cond);
  1596. }
  1597. /* Translate an executable statement without condition. */
  1598. tree
  1599. gfc_trans_code (gfc_code * code)
  1600. {
  1601. return trans_code (code, NULL_TREE);
  1602. }
  1603. /* This function is called after a complete program unit has been parsed
  1604. and resolved. */
  1605. void
  1606. gfc_generate_code (gfc_namespace * ns)
  1607. {
  1608. ompws_flags = 0;
  1609. if (ns->is_block_data)
  1610. {
  1611. gfc_generate_block_data (ns);
  1612. return;
  1613. }
  1614. gfc_generate_function_code (ns);
  1615. }
  1616. /* This function is called after a complete module has been parsed
  1617. and resolved. */
  1618. void
  1619. gfc_generate_module_code (gfc_namespace * ns)
  1620. {
  1621. gfc_namespace *n;
  1622. struct module_htab_entry *entry;
  1623. gcc_assert (ns->proc_name->backend_decl == NULL);
  1624. ns->proc_name->backend_decl
  1625. = build_decl (ns->proc_name->declared_at.lb->location,
  1626. NAMESPACE_DECL, get_identifier (ns->proc_name->name),
  1627. void_type_node);
  1628. entry = gfc_find_module (ns->proc_name->name);
  1629. if (entry->namespace_decl)
  1630. /* Buggy sourcecode, using a module before defining it? */
  1631. entry->decls->empty ();
  1632. entry->namespace_decl = ns->proc_name->backend_decl;
  1633. gfc_generate_module_vars (ns);
  1634. /* We need to generate all module function prototypes first, to allow
  1635. sibling calls. */
  1636. for (n = ns->contained; n; n = n->sibling)
  1637. {
  1638. gfc_entry_list *el;
  1639. if (!n->proc_name)
  1640. continue;
  1641. gfc_create_function_decl (n, false);
  1642. DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
  1643. gfc_module_add_decl (entry, n->proc_name->backend_decl);
  1644. for (el = ns->entries; el; el = el->next)
  1645. {
  1646. DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
  1647. gfc_module_add_decl (entry, el->sym->backend_decl);
  1648. }
  1649. }
  1650. for (n = ns->contained; n; n = n->sibling)
  1651. {
  1652. if (!n->proc_name)
  1653. continue;
  1654. gfc_generate_function_code (n);
  1655. }
  1656. }
  1657. /* Initialize an init/cleanup block with existing code. */
  1658. void
  1659. gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
  1660. {
  1661. gcc_assert (block);
  1662. block->init = NULL_TREE;
  1663. block->code = code;
  1664. block->cleanup = NULL_TREE;
  1665. }
  1666. /* Add a new pair of initializers/clean-up code. */
  1667. void
  1668. gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
  1669. {
  1670. gcc_assert (block);
  1671. /* The new pair of init/cleanup should be "wrapped around" the existing
  1672. block of code, thus the initialization is added to the front and the
  1673. cleanup to the back. */
  1674. add_expr_to_chain (&block->init, init, true);
  1675. add_expr_to_chain (&block->cleanup, cleanup, false);
  1676. }
  1677. /* Finish up a wrapped block by building a corresponding try-finally expr. */
  1678. tree
  1679. gfc_finish_wrapped_block (gfc_wrapped_block* block)
  1680. {
  1681. tree result;
  1682. gcc_assert (block);
  1683. /* Build the final expression. For this, just add init and body together,
  1684. and put clean-up with that into a TRY_FINALLY_EXPR. */
  1685. result = block->init;
  1686. add_expr_to_chain (&result, block->code, false);
  1687. if (block->cleanup)
  1688. result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
  1689. result, block->cleanup);
  1690. /* Clear the block. */
  1691. block->init = NULL_TREE;
  1692. block->code = NULL_TREE;
  1693. block->cleanup = NULL_TREE;
  1694. return result;
  1695. }
  1696. /* Helper function for marking a boolean expression tree as unlikely. */
  1697. tree
  1698. gfc_unlikely (tree cond, enum br_predictor predictor)
  1699. {
  1700. tree tmp;
  1701. if (optimize)
  1702. {
  1703. cond = fold_convert (long_integer_type_node, cond);
  1704. tmp = build_zero_cst (long_integer_type_node);
  1705. cond = build_call_expr_loc (input_location,
  1706. builtin_decl_explicit (BUILT_IN_EXPECT),
  1707. 3, cond, tmp,
  1708. build_int_cst (integer_type_node,
  1709. predictor));
  1710. }
  1711. cond = fold_convert (boolean_type_node, cond);
  1712. return cond;
  1713. }
  1714. /* Helper function for marking a boolean expression tree as likely. */
  1715. tree
  1716. gfc_likely (tree cond, enum br_predictor predictor)
  1717. {
  1718. tree tmp;
  1719. if (optimize)
  1720. {
  1721. cond = fold_convert (long_integer_type_node, cond);
  1722. tmp = build_one_cst (long_integer_type_node);
  1723. cond = build_call_expr_loc (input_location,
  1724. builtin_decl_explicit (BUILT_IN_EXPECT),
  1725. 3, cond, tmp,
  1726. build_int_cst (integer_type_node,
  1727. predictor));
  1728. }
  1729. cond = fold_convert (boolean_type_node, cond);
  1730. return cond;
  1731. }
  1732. /* Get the string length for a deferred character length component. */
  1733. bool
  1734. gfc_deferred_strlen (gfc_component *c, tree *decl)
  1735. {
  1736. char name[GFC_MAX_SYMBOL_LEN+9];
  1737. gfc_component *strlen;
  1738. if (!(c->ts.type == BT_CHARACTER && c->ts.deferred))
  1739. return false;
  1740. sprintf (name, "_%s_length", c->name);
  1741. for (strlen = c; strlen; strlen = strlen->next)
  1742. if (strcmp (strlen->name, name) == 0)
  1743. break;
  1744. *decl = strlen ? strlen->backend_decl : NULL_TREE;
  1745. return strlen != NULL;
  1746. }