class.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879
  1. /* Implementation of Fortran 2003 Polymorphism.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Richard Thomas <pault@gcc.gnu.org>
  4. and Janus Weil <janus@gcc.gnu.org>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  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. /* class.c -- This file contains the front end functions needed to service
  18. the implementation of Fortran 2003 polymorphism and other
  19. object-oriented features. */
  20. /* Outline of the internal representation:
  21. Each CLASS variable is encapsulated by a class container, which is a
  22. structure with two fields:
  23. * _data: A pointer to the actual data of the variable. This field has the
  24. declared type of the class variable and its attributes
  25. (pointer/allocatable/dimension/...).
  26. * _vptr: A pointer to the vtable entry (see below) of the dynamic type.
  27. Only for unlimited polymorphic classes:
  28. * _len: An integer(4) to store the string length when the unlimited
  29. polymorphic pointer is used to point to a char array. The '_len'
  30. component will be zero when no character array is stored in
  31. '_data'.
  32. For each derived type we set up a "vtable" entry, i.e. a structure with the
  33. following fields:
  34. * _hash: A hash value serving as a unique identifier for this type.
  35. * _size: The size in bytes of the derived type.
  36. * _extends: A pointer to the vtable entry of the parent derived type.
  37. * _def_init: A pointer to a default initialized variable of this type.
  38. * _copy: A procedure pointer to a copying procedure.
  39. * _final: A procedure pointer to a wrapper function, which frees
  40. allocatable components and calls FINAL subroutines.
  41. After these follow procedure pointer components for the specific
  42. type-bound procedures. */
  43. #include "config.h"
  44. #include "system.h"
  45. #include "coretypes.h"
  46. #include "gfortran.h"
  47. #include "constructor.h"
  48. #include "target-memory.h"
  49. /* Inserts a derived type component reference in a data reference chain.
  50. TS: base type of the ref chain so far, in which we will pick the component
  51. REF: the address of the GFC_REF pointer to update
  52. NAME: name of the component to insert
  53. Note that component insertion makes sense only if we are at the end of
  54. the chain (*REF == NULL) or if we are adding a missing "_data" component
  55. to access the actual contents of a class object. */
  56. static void
  57. insert_component_ref (gfc_typespec *ts, gfc_ref **ref, const char * const name)
  58. {
  59. gfc_symbol *type_sym;
  60. gfc_ref *new_ref;
  61. gcc_assert (ts->type == BT_DERIVED || ts->type == BT_CLASS);
  62. type_sym = ts->u.derived;
  63. new_ref = gfc_get_ref ();
  64. new_ref->type = REF_COMPONENT;
  65. new_ref->next = *ref;
  66. new_ref->u.c.sym = type_sym;
  67. new_ref->u.c.component = gfc_find_component (type_sym, name, true, true);
  68. gcc_assert (new_ref->u.c.component);
  69. if (new_ref->next)
  70. {
  71. gfc_ref *next = NULL;
  72. /* We need to update the base type in the trailing reference chain to
  73. that of the new component. */
  74. gcc_assert (strcmp (name, "_data") == 0);
  75. if (new_ref->next->type == REF_COMPONENT)
  76. next = new_ref->next;
  77. else if (new_ref->next->type == REF_ARRAY
  78. && new_ref->next->next
  79. && new_ref->next->next->type == REF_COMPONENT)
  80. next = new_ref->next->next;
  81. if (next != NULL)
  82. {
  83. gcc_assert (new_ref->u.c.component->ts.type == BT_CLASS
  84. || new_ref->u.c.component->ts.type == BT_DERIVED);
  85. next->u.c.sym = new_ref->u.c.component->ts.u.derived;
  86. }
  87. }
  88. *ref = new_ref;
  89. }
  90. /* Tells whether we need to add a "_data" reference to access REF subobject
  91. from an object of type TS. If FIRST_REF_IN_CHAIN is set, then the base
  92. object accessed by REF is a variable; in other words it is a full object,
  93. not a subobject. */
  94. static bool
  95. class_data_ref_missing (gfc_typespec *ts, gfc_ref *ref, bool first_ref_in_chain)
  96. {
  97. /* Only class containers may need the "_data" reference. */
  98. if (ts->type != BT_CLASS)
  99. return false;
  100. /* Accessing a class container with an array reference is certainly wrong. */
  101. if (ref->type != REF_COMPONENT)
  102. return true;
  103. /* Accessing the class container's fields is fine. */
  104. if (ref->u.c.component->name[0] == '_')
  105. return false;
  106. /* At this point we have a class container with a non class container's field
  107. component reference. We don't want to add the "_data" component if we are
  108. at the first reference and the symbol's type is an extended derived type.
  109. In that case, conv_parent_component_references will do the right thing so
  110. it is not absolutely necessary. Omitting it prevents a regression (see
  111. class_41.f03) in the interface mapping mechanism. When evaluating string
  112. lengths depending on dummy arguments, we create a fake symbol with a type
  113. equal to that of the dummy type. However, because of type extension,
  114. the backend type (corresponding to the actual argument) can have a
  115. different (extended) type. Adding the "_data" component explicitly, using
  116. the base type, confuses the gfc_conv_component_ref code which deals with
  117. the extended type. */
  118. if (first_ref_in_chain && ts->u.derived->attr.extension)
  119. return false;
  120. /* We have a class container with a non class container's field component
  121. reference that doesn't fall into the above. */
  122. return true;
  123. }
  124. /* Browse through a data reference chain and add the missing "_data" references
  125. when a subobject of a class object is accessed without it.
  126. Note that it doesn't add the "_data" reference when the class container
  127. is the last element in the reference chain. */
  128. void
  129. gfc_fix_class_refs (gfc_expr *e)
  130. {
  131. gfc_typespec *ts;
  132. gfc_ref **ref;
  133. if ((e->expr_type != EXPR_VARIABLE
  134. && e->expr_type != EXPR_FUNCTION)
  135. || (e->expr_type == EXPR_FUNCTION
  136. && e->value.function.isym != NULL))
  137. return;
  138. if (e->expr_type == EXPR_VARIABLE)
  139. ts = &e->symtree->n.sym->ts;
  140. else
  141. {
  142. gfc_symbol *func;
  143. gcc_assert (e->expr_type == EXPR_FUNCTION);
  144. if (e->value.function.esym != NULL)
  145. func = e->value.function.esym;
  146. else
  147. func = e->symtree->n.sym;
  148. if (func->result != NULL)
  149. ts = &func->result->ts;
  150. else
  151. ts = &func->ts;
  152. }
  153. for (ref = &e->ref; *ref != NULL; ref = &(*ref)->next)
  154. {
  155. if (class_data_ref_missing (ts, *ref, ref == &e->ref))
  156. insert_component_ref (ts, ref, "_data");
  157. if ((*ref)->type == REF_COMPONENT)
  158. ts = &(*ref)->u.c.component->ts;
  159. }
  160. }
  161. /* Insert a reference to the component of the given name.
  162. Only to be used with CLASS containers and vtables. */
  163. void
  164. gfc_add_component_ref (gfc_expr *e, const char *name)
  165. {
  166. gfc_ref **tail = &(e->ref);
  167. gfc_ref *next = NULL;
  168. gfc_symbol *derived = e->symtree->n.sym->ts.u.derived;
  169. while (*tail != NULL)
  170. {
  171. if ((*tail)->type == REF_COMPONENT)
  172. {
  173. if (strcmp ((*tail)->u.c.component->name, "_data") == 0
  174. && (*tail)->next
  175. && (*tail)->next->type == REF_ARRAY
  176. && (*tail)->next->next == NULL)
  177. return;
  178. derived = (*tail)->u.c.component->ts.u.derived;
  179. }
  180. if ((*tail)->type == REF_ARRAY && (*tail)->next == NULL)
  181. break;
  182. tail = &((*tail)->next);
  183. }
  184. if (derived->components->next->ts.type == BT_DERIVED &&
  185. derived->components->next->ts.u.derived == NULL)
  186. {
  187. /* Fix up missing vtype. */
  188. gfc_symbol *vtab = gfc_find_derived_vtab (derived->components->ts.u.derived);
  189. gcc_assert (vtab);
  190. derived->components->next->ts.u.derived = vtab->ts.u.derived;
  191. }
  192. if (*tail != NULL && strcmp (name, "_data") == 0)
  193. next = *tail;
  194. else
  195. /* Avoid losing memory. */
  196. gfc_free_ref_list (*tail);
  197. (*tail) = gfc_get_ref();
  198. (*tail)->next = next;
  199. (*tail)->type = REF_COMPONENT;
  200. (*tail)->u.c.sym = derived;
  201. (*tail)->u.c.component = gfc_find_component (derived, name, true, true);
  202. gcc_assert((*tail)->u.c.component);
  203. if (!next)
  204. e->ts = (*tail)->u.c.component->ts;
  205. }
  206. /* This is used to add both the _data component reference and an array
  207. reference to class expressions. Used in translation of intrinsic
  208. array inquiry functions. */
  209. void
  210. gfc_add_class_array_ref (gfc_expr *e)
  211. {
  212. int rank = CLASS_DATA (e)->as->rank;
  213. gfc_array_spec *as = CLASS_DATA (e)->as;
  214. gfc_ref *ref = NULL;
  215. gfc_add_component_ref (e, "_data");
  216. e->rank = rank;
  217. for (ref = e->ref; ref; ref = ref->next)
  218. if (!ref->next)
  219. break;
  220. if (ref->type != REF_ARRAY)
  221. {
  222. ref->next = gfc_get_ref ();
  223. ref = ref->next;
  224. ref->type = REF_ARRAY;
  225. ref->u.ar.type = AR_FULL;
  226. ref->u.ar.as = as;
  227. }
  228. }
  229. /* Unfortunately, class array expressions can appear in various conditions;
  230. with and without both _data component and an arrayspec. This function
  231. deals with that variability. The previous reference to 'ref' is to a
  232. class array. */
  233. static bool
  234. class_array_ref_detected (gfc_ref *ref, bool *full_array)
  235. {
  236. bool no_data = false;
  237. bool with_data = false;
  238. /* An array reference with no _data component. */
  239. if (ref && ref->type == REF_ARRAY
  240. && !ref->next
  241. && ref->u.ar.type != AR_ELEMENT)
  242. {
  243. if (full_array)
  244. *full_array = ref->u.ar.type == AR_FULL;
  245. no_data = true;
  246. }
  247. /* Cover cases where _data appears, with or without an array ref. */
  248. if (ref && ref->type == REF_COMPONENT
  249. && strcmp (ref->u.c.component->name, "_data") == 0)
  250. {
  251. if (!ref->next)
  252. {
  253. with_data = true;
  254. if (full_array)
  255. *full_array = true;
  256. }
  257. else if (ref->next && ref->next->type == REF_ARRAY
  258. && !ref->next->next
  259. && ref->type == REF_COMPONENT
  260. && ref->next->type == REF_ARRAY
  261. && ref->next->u.ar.type != AR_ELEMENT)
  262. {
  263. with_data = true;
  264. if (full_array)
  265. *full_array = ref->next->u.ar.type == AR_FULL;
  266. }
  267. }
  268. return no_data || with_data;
  269. }
  270. /* Returns true if the expression contains a reference to a class
  271. array. Notice that class array elements return false. */
  272. bool
  273. gfc_is_class_array_ref (gfc_expr *e, bool *full_array)
  274. {
  275. gfc_ref *ref;
  276. if (!e->rank)
  277. return false;
  278. if (full_array)
  279. *full_array= false;
  280. /* Is this a class array object? ie. Is the symbol of type class? */
  281. if (e->symtree
  282. && e->symtree->n.sym->ts.type == BT_CLASS
  283. && CLASS_DATA (e->symtree->n.sym)
  284. && CLASS_DATA (e->symtree->n.sym)->attr.dimension
  285. && class_array_ref_detected (e->ref, full_array))
  286. return true;
  287. /* Or is this a class array component reference? */
  288. for (ref = e->ref; ref; ref = ref->next)
  289. {
  290. if (ref->type == REF_COMPONENT
  291. && ref->u.c.component->ts.type == BT_CLASS
  292. && CLASS_DATA (ref->u.c.component)->attr.dimension
  293. && class_array_ref_detected (ref->next, full_array))
  294. return true;
  295. }
  296. return false;
  297. }
  298. /* Returns true if the expression is a reference to a class
  299. scalar. This function is necessary because such expressions
  300. can be dressed with a reference to the _data component and so
  301. have a type other than BT_CLASS. */
  302. bool
  303. gfc_is_class_scalar_expr (gfc_expr *e)
  304. {
  305. gfc_ref *ref;
  306. if (e->rank)
  307. return false;
  308. /* Is this a class object? */
  309. if (e->symtree
  310. && e->symtree->n.sym->ts.type == BT_CLASS
  311. && CLASS_DATA (e->symtree->n.sym)
  312. && !CLASS_DATA (e->symtree->n.sym)->attr.dimension
  313. && (e->ref == NULL
  314. || (strcmp (e->ref->u.c.component->name, "_data") == 0
  315. && e->ref->next == NULL)))
  316. return true;
  317. /* Or is the final reference BT_CLASS or _data? */
  318. for (ref = e->ref; ref; ref = ref->next)
  319. {
  320. if (ref->type == REF_COMPONENT
  321. && ref->u.c.component->ts.type == BT_CLASS
  322. && CLASS_DATA (ref->u.c.component)
  323. && !CLASS_DATA (ref->u.c.component)->attr.dimension
  324. && (ref->next == NULL
  325. || (strcmp (ref->next->u.c.component->name, "_data") == 0
  326. && ref->next->next == NULL)))
  327. return true;
  328. }
  329. return false;
  330. }
  331. /* Tells whether the expression E is a reference to a (scalar) class container.
  332. Scalar because array class containers usually have an array reference after
  333. them, and gfc_fix_class_refs will add the missing "_data" component reference
  334. in that case. */
  335. bool
  336. gfc_is_class_container_ref (gfc_expr *e)
  337. {
  338. gfc_ref *ref;
  339. bool result;
  340. if (e->expr_type != EXPR_VARIABLE)
  341. return e->ts.type == BT_CLASS;
  342. if (e->symtree->n.sym->ts.type == BT_CLASS)
  343. result = true;
  344. else
  345. result = false;
  346. for (ref = e->ref; ref; ref = ref->next)
  347. {
  348. if (ref->type != REF_COMPONENT)
  349. result = false;
  350. else if (ref->u.c.component->ts.type == BT_CLASS)
  351. result = true;
  352. else
  353. result = false;
  354. }
  355. return result;
  356. }
  357. /* Build an initializer for CLASS pointers,
  358. initializing the _data component to the init_expr (or NULL) and the _vptr
  359. component to the corresponding type (or the declared type, given by ts). */
  360. gfc_expr *
  361. gfc_class_initializer (gfc_typespec *ts, gfc_expr *init_expr)
  362. {
  363. gfc_expr *init;
  364. gfc_component *comp;
  365. gfc_symbol *vtab = NULL;
  366. if (init_expr && init_expr->expr_type != EXPR_NULL)
  367. vtab = gfc_find_vtab (&init_expr->ts);
  368. else
  369. vtab = gfc_find_vtab (ts);
  370. init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
  371. &ts->u.derived->declared_at);
  372. init->ts = *ts;
  373. for (comp = ts->u.derived->components; comp; comp = comp->next)
  374. {
  375. gfc_constructor *ctor = gfc_constructor_get();
  376. if (strcmp (comp->name, "_vptr") == 0 && vtab)
  377. ctor->expr = gfc_lval_expr_from_sym (vtab);
  378. else if (init_expr && init_expr->expr_type != EXPR_NULL)
  379. ctor->expr = gfc_copy_expr (init_expr);
  380. else
  381. ctor->expr = gfc_get_null_expr (NULL);
  382. gfc_constructor_append (&init->value.constructor, ctor);
  383. }
  384. return init;
  385. }
  386. /* Create a unique string identifier for a derived type, composed of its name
  387. and module name. This is used to construct unique names for the class
  388. containers and vtab symbols. */
  389. static void
  390. get_unique_type_string (char *string, gfc_symbol *derived)
  391. {
  392. char dt_name[GFC_MAX_SYMBOL_LEN+1];
  393. if (derived->attr.unlimited_polymorphic)
  394. strcpy (dt_name, "STAR");
  395. else
  396. strcpy (dt_name, derived->name);
  397. dt_name[0] = TOUPPER (dt_name[0]);
  398. if (derived->attr.unlimited_polymorphic)
  399. sprintf (string, "_%s", dt_name);
  400. else if (derived->module)
  401. sprintf (string, "%s_%s", derived->module, dt_name);
  402. else if (derived->ns->proc_name)
  403. sprintf (string, "%s_%s", derived->ns->proc_name->name, dt_name);
  404. else
  405. sprintf (string, "_%s", dt_name);
  406. }
  407. /* A relative of 'get_unique_type_string' which makes sure the generated
  408. string will not be too long (replacing it by a hash string if needed). */
  409. static void
  410. get_unique_hashed_string (char *string, gfc_symbol *derived)
  411. {
  412. char tmp[2*GFC_MAX_SYMBOL_LEN+2];
  413. get_unique_type_string (&tmp[0], derived);
  414. /* If string is too long, use hash value in hex representation (allow for
  415. extra decoration, cf. gfc_build_class_symbol & gfc_find_derived_vtab).
  416. We need space to for 15 characters "__class_" + symbol name + "_%d_%da",
  417. where %d is the (co)rank which can be up to n = 15. */
  418. if (strlen (tmp) > GFC_MAX_SYMBOL_LEN - 15)
  419. {
  420. int h = gfc_hash_value (derived);
  421. sprintf (string, "%X", h);
  422. }
  423. else
  424. strcpy (string, tmp);
  425. }
  426. /* Assign a hash value for a derived type. The algorithm is that of SDBM. */
  427. unsigned int
  428. gfc_hash_value (gfc_symbol *sym)
  429. {
  430. unsigned int hash = 0;
  431. char c[2*(GFC_MAX_SYMBOL_LEN+1)];
  432. int i, len;
  433. get_unique_type_string (&c[0], sym);
  434. len = strlen (c);
  435. for (i = 0; i < len; i++)
  436. hash = (hash << 6) + (hash << 16) - hash + c[i];
  437. /* Return the hash but take the modulus for the sake of module read,
  438. even though this slightly increases the chance of collision. */
  439. return (hash % 100000000);
  440. }
  441. /* Assign a hash value for an intrinsic type. The algorithm is that of SDBM. */
  442. unsigned int
  443. gfc_intrinsic_hash_value (gfc_typespec *ts)
  444. {
  445. unsigned int hash = 0;
  446. const char *c = gfc_typename (ts);
  447. int i, len;
  448. len = strlen (c);
  449. for (i = 0; i < len; i++)
  450. hash = (hash << 6) + (hash << 16) - hash + c[i];
  451. /* Return the hash but take the modulus for the sake of module read,
  452. even though this slightly increases the chance of collision. */
  453. return (hash % 100000000);
  454. }
  455. /* Get the _len component from a class/derived object storing a string.
  456. For unlimited polymorphic entities a ref to the _data component is available
  457. while a ref to the _len component is needed. This routine traverese the
  458. ref-chain and strips the last ref to a _data from it replacing it with a
  459. ref to the _len component. */
  460. gfc_expr *
  461. gfc_get_len_component (gfc_expr *e)
  462. {
  463. gfc_expr *ptr;
  464. gfc_ref *ref, **last;
  465. ptr = gfc_copy_expr (e);
  466. /* We need to remove the last _data component ref from ptr. */
  467. last = &(ptr->ref);
  468. ref = ptr->ref;
  469. while (ref)
  470. {
  471. if (!ref->next
  472. && ref->type == REF_COMPONENT
  473. && strcmp ("_data", ref->u.c.component->name)== 0)
  474. {
  475. gfc_free_ref_list (ref);
  476. *last = NULL;
  477. break;
  478. }
  479. last = &(ref->next);
  480. ref = ref->next;
  481. }
  482. /* And replace if with a ref to the _len component. */
  483. gfc_add_component_ref (ptr, "_len");
  484. return ptr;
  485. }
  486. /* Build a polymorphic CLASS entity, using the symbol that comes from
  487. build_sym. A CLASS entity is represented by an encapsulating type,
  488. which contains the declared type as '_data' component, plus a pointer
  489. component '_vptr' which determines the dynamic type. When this CLASS
  490. entity is unlimited polymorphic, then also add a component '_len' to
  491. store the length of string when that is stored in it. */
  492. bool
  493. gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
  494. gfc_array_spec **as)
  495. {
  496. char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
  497. gfc_symbol *fclass;
  498. gfc_symbol *vtab;
  499. gfc_component *c;
  500. gfc_namespace *ns;
  501. int rank;
  502. gcc_assert (as);
  503. if (*as && (*as)->type == AS_ASSUMED_SIZE)
  504. {
  505. gfc_error ("Assumed size polymorphic objects or components, such "
  506. "as that at %C, have not yet been implemented");
  507. return false;
  508. }
  509. if (attr->class_ok)
  510. /* Class container has already been built. */
  511. return true;
  512. attr->class_ok = attr->dummy || attr->pointer || attr->allocatable
  513. || attr->select_type_temporary || attr->associate_var;
  514. if (!attr->class_ok)
  515. /* We can not build the class container yet. */
  516. return true;
  517. /* Determine the name of the encapsulating type. */
  518. rank = !(*as) || (*as)->rank == -1 ? GFC_MAX_DIMENSIONS : (*as)->rank;
  519. get_unique_hashed_string (tname, ts->u.derived);
  520. if ((*as) && attr->allocatable)
  521. sprintf (name, "__class_%s_%d_%da", tname, rank, (*as)->corank);
  522. else if ((*as) && attr->pointer)
  523. sprintf (name, "__class_%s_%d_%dp", tname, rank, (*as)->corank);
  524. else if ((*as))
  525. sprintf (name, "__class_%s_%d_%dt", tname, rank, (*as)->corank);
  526. else if (attr->pointer)
  527. sprintf (name, "__class_%s_p", tname);
  528. else if (attr->allocatable)
  529. sprintf (name, "__class_%s_a", tname);
  530. else
  531. sprintf (name, "__class_%s_t", tname);
  532. if (ts->u.derived->attr.unlimited_polymorphic)
  533. {
  534. /* Find the top-level namespace. */
  535. for (ns = gfc_current_ns; ns; ns = ns->parent)
  536. if (!ns->parent)
  537. break;
  538. }
  539. else
  540. ns = ts->u.derived->ns;
  541. gfc_find_symbol (name, ns, 0, &fclass);
  542. if (fclass == NULL)
  543. {
  544. gfc_symtree *st;
  545. /* If not there, create a new symbol. */
  546. fclass = gfc_new_symbol (name, ns);
  547. st = gfc_new_symtree (&ns->sym_root, name);
  548. st->n.sym = fclass;
  549. gfc_set_sym_referenced (fclass);
  550. fclass->refs++;
  551. fclass->ts.type = BT_UNKNOWN;
  552. if (!ts->u.derived->attr.unlimited_polymorphic)
  553. fclass->attr.abstract = ts->u.derived->attr.abstract;
  554. fclass->f2k_derived = gfc_get_namespace (NULL, 0);
  555. if (!gfc_add_flavor (&fclass->attr, FL_DERIVED, NULL,
  556. &gfc_current_locus))
  557. return false;
  558. /* Add component '_data'. */
  559. if (!gfc_add_component (fclass, "_data", &c))
  560. return false;
  561. c->ts = *ts;
  562. c->ts.type = BT_DERIVED;
  563. c->attr.access = ACCESS_PRIVATE;
  564. c->ts.u.derived = ts->u.derived;
  565. c->attr.class_pointer = attr->pointer;
  566. c->attr.pointer = attr->pointer || (attr->dummy && !attr->allocatable)
  567. || attr->select_type_temporary;
  568. c->attr.allocatable = attr->allocatable;
  569. c->attr.dimension = attr->dimension;
  570. c->attr.codimension = attr->codimension;
  571. c->attr.abstract = fclass->attr.abstract;
  572. c->as = (*as);
  573. c->initializer = NULL;
  574. /* Add component '_vptr'. */
  575. if (!gfc_add_component (fclass, "_vptr", &c))
  576. return false;
  577. c->ts.type = BT_DERIVED;
  578. c->attr.access = ACCESS_PRIVATE;
  579. c->attr.pointer = 1;
  580. if (ts->u.derived->attr.unlimited_polymorphic)
  581. {
  582. vtab = gfc_find_derived_vtab (ts->u.derived);
  583. gcc_assert (vtab);
  584. c->ts.u.derived = vtab->ts.u.derived;
  585. /* Add component '_len'. Only unlimited polymorphic pointers may
  586. have a string assigned to them, i.e., only those need the _len
  587. component. */
  588. if (!gfc_add_component (fclass, "_len", &c))
  589. return false;
  590. c->ts.type = BT_INTEGER;
  591. c->ts.kind = 4;
  592. c->attr.access = ACCESS_PRIVATE;
  593. c->attr.artificial = 1;
  594. }
  595. else
  596. /* Build vtab later. */
  597. c->ts.u.derived = NULL;
  598. }
  599. if (!ts->u.derived->attr.unlimited_polymorphic)
  600. {
  601. /* Since the extension field is 8 bit wide, we can only have
  602. up to 255 extension levels. */
  603. if (ts->u.derived->attr.extension == 255)
  604. {
  605. gfc_error ("Maximum extension level reached with type %qs at %L",
  606. ts->u.derived->name, &ts->u.derived->declared_at);
  607. return false;
  608. }
  609. fclass->attr.extension = ts->u.derived->attr.extension + 1;
  610. fclass->attr.alloc_comp = ts->u.derived->attr.alloc_comp;
  611. fclass->attr.coarray_comp = ts->u.derived->attr.coarray_comp;
  612. }
  613. fclass->attr.is_class = 1;
  614. ts->u.derived = fclass;
  615. attr->allocatable = attr->pointer = attr->dimension = attr->codimension = 0;
  616. (*as) = NULL;
  617. return true;
  618. }
  619. /* Add a procedure pointer component to the vtype
  620. to represent a specific type-bound procedure. */
  621. static void
  622. add_proc_comp (gfc_symbol *vtype, const char *name, gfc_typebound_proc *tb)
  623. {
  624. gfc_component *c;
  625. if (tb->non_overridable)
  626. return;
  627. c = gfc_find_component (vtype, name, true, true);
  628. if (c == NULL)
  629. {
  630. /* Add procedure component. */
  631. if (!gfc_add_component (vtype, name, &c))
  632. return;
  633. if (!c->tb)
  634. c->tb = XCNEW (gfc_typebound_proc);
  635. *c->tb = *tb;
  636. c->tb->ppc = 1;
  637. c->attr.procedure = 1;
  638. c->attr.proc_pointer = 1;
  639. c->attr.flavor = FL_PROCEDURE;
  640. c->attr.access = ACCESS_PRIVATE;
  641. c->attr.external = 1;
  642. c->attr.untyped = 1;
  643. c->attr.if_source = IFSRC_IFBODY;
  644. }
  645. else if (c->attr.proc_pointer && c->tb)
  646. {
  647. *c->tb = *tb;
  648. c->tb->ppc = 1;
  649. }
  650. if (tb->u.specific)
  651. {
  652. gfc_symbol *ifc = tb->u.specific->n.sym;
  653. c->ts.interface = ifc;
  654. if (!tb->deferred)
  655. c->initializer = gfc_get_variable_expr (tb->u.specific);
  656. c->attr.pure = ifc->attr.pure;
  657. }
  658. }
  659. /* Add all specific type-bound procedures in the symtree 'st' to a vtype. */
  660. static void
  661. add_procs_to_declared_vtab1 (gfc_symtree *st, gfc_symbol *vtype)
  662. {
  663. if (!st)
  664. return;
  665. if (st->left)
  666. add_procs_to_declared_vtab1 (st->left, vtype);
  667. if (st->right)
  668. add_procs_to_declared_vtab1 (st->right, vtype);
  669. if (st->n.tb && !st->n.tb->error
  670. && !st->n.tb->is_generic && st->n.tb->u.specific)
  671. add_proc_comp (vtype, st->name, st->n.tb);
  672. }
  673. /* Copy procedure pointers components from the parent type. */
  674. static void
  675. copy_vtab_proc_comps (gfc_symbol *declared, gfc_symbol *vtype)
  676. {
  677. gfc_component *cmp;
  678. gfc_symbol *vtab;
  679. vtab = gfc_find_derived_vtab (declared);
  680. for (cmp = vtab->ts.u.derived->components; cmp; cmp = cmp->next)
  681. {
  682. if (gfc_find_component (vtype, cmp->name, true, true))
  683. continue;
  684. add_proc_comp (vtype, cmp->name, cmp->tb);
  685. }
  686. }
  687. /* Returns true if any of its nonpointer nonallocatable components or
  688. their nonpointer nonallocatable subcomponents has a finalization
  689. subroutine. */
  690. static bool
  691. has_finalizer_component (gfc_symbol *derived)
  692. {
  693. gfc_component *c;
  694. for (c = derived->components; c; c = c->next)
  695. {
  696. if (c->ts.type == BT_DERIVED && c->ts.u.derived->f2k_derived
  697. && c->ts.u.derived->f2k_derived->finalizers)
  698. return true;
  699. if (c->ts.type == BT_DERIVED
  700. && !c->attr.pointer && !c->attr.allocatable
  701. && has_finalizer_component (c->ts.u.derived))
  702. return true;
  703. }
  704. return false;
  705. }
  706. static bool
  707. comp_is_finalizable (gfc_component *comp)
  708. {
  709. if (comp->attr.proc_pointer)
  710. return false;
  711. else if (comp->attr.allocatable && comp->ts.type != BT_CLASS)
  712. return true;
  713. else if (comp->ts.type == BT_DERIVED && !comp->attr.pointer
  714. && (comp->ts.u.derived->attr.alloc_comp
  715. || has_finalizer_component (comp->ts.u.derived)
  716. || (comp->ts.u.derived->f2k_derived
  717. && comp->ts.u.derived->f2k_derived->finalizers)))
  718. return true;
  719. else if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
  720. && CLASS_DATA (comp)->attr.allocatable)
  721. return true;
  722. else
  723. return false;
  724. }
  725. /* Call DEALLOCATE for the passed component if it is allocatable, if it is
  726. neither allocatable nor a pointer but has a finalizer, call it. If it
  727. is a nonpointer component with allocatable components or has finalizers, walk
  728. them. Either of them is required; other nonallocatables and pointers aren't
  729. handled gracefully.
  730. Note: If the component is allocatable, the DEALLOCATE handling takes care
  731. of calling the appropriate finalizers, coarray deregistering, and
  732. deallocation of allocatable subcomponents. */
  733. static void
  734. finalize_component (gfc_expr *expr, gfc_symbol *derived, gfc_component *comp,
  735. gfc_symbol *stat, gfc_symbol *fini_coarray, gfc_code **code,
  736. gfc_namespace *sub_ns)
  737. {
  738. gfc_expr *e;
  739. gfc_ref *ref;
  740. if (!comp_is_finalizable (comp))
  741. return;
  742. e = gfc_copy_expr (expr);
  743. if (!e->ref)
  744. e->ref = ref = gfc_get_ref ();
  745. else
  746. {
  747. for (ref = e->ref; ref->next; ref = ref->next)
  748. ;
  749. ref->next = gfc_get_ref ();
  750. ref = ref->next;
  751. }
  752. ref->type = REF_COMPONENT;
  753. ref->u.c.sym = derived;
  754. ref->u.c.component = comp;
  755. e->ts = comp->ts;
  756. if (comp->attr.dimension || comp->attr.codimension
  757. || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
  758. && (CLASS_DATA (comp)->attr.dimension
  759. || CLASS_DATA (comp)->attr.codimension)))
  760. {
  761. ref->next = gfc_get_ref ();
  762. ref->next->type = REF_ARRAY;
  763. ref->next->u.ar.dimen = 0;
  764. ref->next->u.ar.as = comp->ts.type == BT_CLASS ? CLASS_DATA (comp)->as
  765. : comp->as;
  766. e->rank = ref->next->u.ar.as->rank;
  767. ref->next->u.ar.type = e->rank ? AR_FULL : AR_ELEMENT;
  768. }
  769. /* Call DEALLOCATE (comp, stat=ignore). */
  770. if (comp->attr.allocatable
  771. || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
  772. && CLASS_DATA (comp)->attr.allocatable))
  773. {
  774. gfc_code *dealloc, *block = NULL;
  775. /* Add IF (fini_coarray). */
  776. if (comp->attr.codimension
  777. || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
  778. && CLASS_DATA (comp)->attr.codimension))
  779. {
  780. block = gfc_get_code (EXEC_IF);
  781. if (*code)
  782. {
  783. (*code)->next = block;
  784. (*code) = (*code)->next;
  785. }
  786. else
  787. (*code) = block;
  788. block->block = gfc_get_code (EXEC_IF);
  789. block = block->block;
  790. block->expr1 = gfc_lval_expr_from_sym (fini_coarray);
  791. }
  792. dealloc = gfc_get_code (EXEC_DEALLOCATE);
  793. dealloc->ext.alloc.list = gfc_get_alloc ();
  794. dealloc->ext.alloc.list->expr = e;
  795. dealloc->expr1 = gfc_lval_expr_from_sym (stat);
  796. gfc_code *cond = gfc_get_code (EXEC_IF);
  797. cond->block = gfc_get_code (EXEC_IF);
  798. cond->block->expr1 = gfc_get_expr ();
  799. cond->block->expr1->expr_type = EXPR_FUNCTION;
  800. gfc_get_sym_tree ("associated", sub_ns, &cond->block->expr1->symtree, false);
  801. cond->block->expr1->symtree->n.sym->attr.flavor = FL_PROCEDURE;
  802. cond->block->expr1->symtree->n.sym->attr.intrinsic = 1;
  803. cond->block->expr1->symtree->n.sym->result = cond->block->expr1->symtree->n.sym;
  804. gfc_commit_symbol (cond->block->expr1->symtree->n.sym);
  805. cond->block->expr1->ts.type = BT_LOGICAL;
  806. cond->block->expr1->ts.kind = gfc_default_logical_kind;
  807. cond->block->expr1->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_ASSOCIATED);
  808. cond->block->expr1->value.function.actual = gfc_get_actual_arglist ();
  809. cond->block->expr1->value.function.actual->expr = gfc_copy_expr (expr);
  810. cond->block->expr1->value.function.actual->next = gfc_get_actual_arglist ();
  811. cond->block->next = dealloc;
  812. if (block)
  813. block->next = cond;
  814. else if (*code)
  815. {
  816. (*code)->next = cond;
  817. (*code) = (*code)->next;
  818. }
  819. else
  820. (*code) = cond;
  821. }
  822. else if (comp->ts.type == BT_DERIVED
  823. && comp->ts.u.derived->f2k_derived
  824. && comp->ts.u.derived->f2k_derived->finalizers)
  825. {
  826. /* Call FINAL_WRAPPER (comp); */
  827. gfc_code *final_wrap;
  828. gfc_symbol *vtab;
  829. gfc_component *c;
  830. vtab = gfc_find_derived_vtab (comp->ts.u.derived);
  831. for (c = vtab->ts.u.derived->components; c; c = c->next)
  832. if (strcmp (c->name, "_final") == 0)
  833. break;
  834. gcc_assert (c);
  835. final_wrap = gfc_get_code (EXEC_CALL);
  836. final_wrap->symtree = c->initializer->symtree;
  837. final_wrap->resolved_sym = c->initializer->symtree->n.sym;
  838. final_wrap->ext.actual = gfc_get_actual_arglist ();
  839. final_wrap->ext.actual->expr = e;
  840. if (*code)
  841. {
  842. (*code)->next = final_wrap;
  843. (*code) = (*code)->next;
  844. }
  845. else
  846. (*code) = final_wrap;
  847. }
  848. else
  849. {
  850. gfc_component *c;
  851. for (c = comp->ts.u.derived->components; c; c = c->next)
  852. finalize_component (e, comp->ts.u.derived, c, stat, fini_coarray, code,
  853. sub_ns);
  854. gfc_free_expr (e);
  855. }
  856. }
  857. /* Generate code equivalent to
  858. CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
  859. + offset, c_ptr), ptr). */
  860. static gfc_code *
  861. finalization_scalarizer (gfc_symbol *array, gfc_symbol *ptr,
  862. gfc_expr *offset, gfc_namespace *sub_ns)
  863. {
  864. gfc_code *block;
  865. gfc_expr *expr, *expr2;
  866. /* C_F_POINTER(). */
  867. block = gfc_get_code (EXEC_CALL);
  868. gfc_get_sym_tree ("c_f_pointer", sub_ns, &block->symtree, true);
  869. block->resolved_sym = block->symtree->n.sym;
  870. block->resolved_sym->attr.flavor = FL_PROCEDURE;
  871. block->resolved_sym->attr.intrinsic = 1;
  872. block->resolved_sym->attr.subroutine = 1;
  873. block->resolved_sym->from_intmod = INTMOD_ISO_C_BINDING;
  874. block->resolved_sym->intmod_sym_id = ISOCBINDING_F_POINTER;
  875. block->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_C_F_POINTER);
  876. gfc_commit_symbol (block->resolved_sym);
  877. /* C_F_POINTER's first argument: TRANSFER ( <addr>, c_intptr_t). */
  878. block->ext.actual = gfc_get_actual_arglist ();
  879. block->ext.actual->next = gfc_get_actual_arglist ();
  880. block->ext.actual->next->expr = gfc_get_int_expr (gfc_index_integer_kind,
  881. NULL, 0);
  882. block->ext.actual->next->next = gfc_get_actual_arglist (); /* SIZE. */
  883. /* The <addr> part: TRANSFER (C_LOC (array), c_intptr_t). */
  884. /* TRANSFER's first argument: C_LOC (array). */
  885. expr = gfc_get_expr ();
  886. expr->expr_type = EXPR_FUNCTION;
  887. gfc_get_sym_tree ("c_loc", sub_ns, &expr->symtree, false);
  888. expr->symtree->n.sym->attr.flavor = FL_PROCEDURE;
  889. expr->symtree->n.sym->intmod_sym_id = ISOCBINDING_LOC;
  890. expr->symtree->n.sym->attr.intrinsic = 1;
  891. expr->symtree->n.sym->from_intmod = INTMOD_ISO_C_BINDING;
  892. expr->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_C_LOC);
  893. expr->value.function.actual = gfc_get_actual_arglist ();
  894. expr->value.function.actual->expr
  895. = gfc_lval_expr_from_sym (array);
  896. expr->symtree->n.sym->result = expr->symtree->n.sym;
  897. gfc_commit_symbol (expr->symtree->n.sym);
  898. expr->ts.type = BT_INTEGER;
  899. expr->ts.kind = gfc_index_integer_kind;
  900. /* TRANSFER. */
  901. expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_TRANSFER, "transfer",
  902. gfc_current_locus, 3, expr,
  903. gfc_get_int_expr (gfc_index_integer_kind,
  904. NULL, 0), NULL);
  905. expr2->ts.type = BT_INTEGER;
  906. expr2->ts.kind = gfc_index_integer_kind;
  907. /* <array addr> + <offset>. */
  908. block->ext.actual->expr = gfc_get_expr ();
  909. block->ext.actual->expr->expr_type = EXPR_OP;
  910. block->ext.actual->expr->value.op.op = INTRINSIC_PLUS;
  911. block->ext.actual->expr->value.op.op1 = expr2;
  912. block->ext.actual->expr->value.op.op2 = offset;
  913. block->ext.actual->expr->ts = expr->ts;
  914. /* C_F_POINTER's 2nd arg: ptr -- and its absent shape=. */
  915. block->ext.actual->next = gfc_get_actual_arglist ();
  916. block->ext.actual->next->expr = gfc_lval_expr_from_sym (ptr);
  917. block->ext.actual->next->next = gfc_get_actual_arglist ();
  918. return block;
  919. }
  920. /* Calculates the offset to the (idx+1)th element of an array, taking the
  921. stride into account. It generates the code:
  922. offset = 0
  923. do idx2 = 1, rank
  924. offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1) * strides(idx2)
  925. end do
  926. offset = offset * byte_stride. */
  927. static gfc_code*
  928. finalization_get_offset (gfc_symbol *idx, gfc_symbol *idx2, gfc_symbol *offset,
  929. gfc_symbol *strides, gfc_symbol *sizes,
  930. gfc_symbol *byte_stride, gfc_expr *rank,
  931. gfc_code *block, gfc_namespace *sub_ns)
  932. {
  933. gfc_iterator *iter;
  934. gfc_expr *expr, *expr2;
  935. /* offset = 0. */
  936. block->next = gfc_get_code (EXEC_ASSIGN);
  937. block = block->next;
  938. block->expr1 = gfc_lval_expr_from_sym (offset);
  939. block->expr2 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  940. /* Create loop. */
  941. iter = gfc_get_iterator ();
  942. iter->var = gfc_lval_expr_from_sym (idx2);
  943. iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  944. iter->end = gfc_copy_expr (rank);
  945. iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  946. block->next = gfc_get_code (EXEC_DO);
  947. block = block->next;
  948. block->ext.iterator = iter;
  949. block->block = gfc_get_code (EXEC_DO);
  950. /* Loop body: offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1)
  951. * strides(idx2). */
  952. /* mod (idx, sizes(idx2)). */
  953. expr = gfc_lval_expr_from_sym (sizes);
  954. expr->ref = gfc_get_ref ();
  955. expr->ref->type = REF_ARRAY;
  956. expr->ref->u.ar.as = sizes->as;
  957. expr->ref->u.ar.type = AR_ELEMENT;
  958. expr->ref->u.ar.dimen = 1;
  959. expr->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  960. expr->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx2);
  961. expr = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_MOD, "mod",
  962. gfc_current_locus, 2,
  963. gfc_lval_expr_from_sym (idx), expr);
  964. expr->ts = idx->ts;
  965. /* (...) / sizes(idx2-1). */
  966. expr2 = gfc_get_expr ();
  967. expr2->expr_type = EXPR_OP;
  968. expr2->value.op.op = INTRINSIC_DIVIDE;
  969. expr2->value.op.op1 = expr;
  970. expr2->value.op.op2 = gfc_lval_expr_from_sym (sizes);
  971. expr2->value.op.op2->ref = gfc_get_ref ();
  972. expr2->value.op.op2->ref->type = REF_ARRAY;
  973. expr2->value.op.op2->ref->u.ar.as = sizes->as;
  974. expr2->value.op.op2->ref->u.ar.type = AR_ELEMENT;
  975. expr2->value.op.op2->ref->u.ar.dimen = 1;
  976. expr2->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  977. expr2->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
  978. expr2->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
  979. expr2->value.op.op2->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
  980. expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1
  981. = gfc_lval_expr_from_sym (idx2);
  982. expr2->value.op.op2->ref->u.ar.start[0]->value.op.op2
  983. = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  984. expr2->value.op.op2->ref->u.ar.start[0]->ts
  985. = expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
  986. expr2->ts = idx->ts;
  987. /* ... * strides(idx2). */
  988. expr = gfc_get_expr ();
  989. expr->expr_type = EXPR_OP;
  990. expr->value.op.op = INTRINSIC_TIMES;
  991. expr->value.op.op1 = expr2;
  992. expr->value.op.op2 = gfc_lval_expr_from_sym (strides);
  993. expr->value.op.op2->ref = gfc_get_ref ();
  994. expr->value.op.op2->ref->type = REF_ARRAY;
  995. expr->value.op.op2->ref->u.ar.type = AR_ELEMENT;
  996. expr->value.op.op2->ref->u.ar.dimen = 1;
  997. expr->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  998. expr->value.op.op2->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx2);
  999. expr->value.op.op2->ref->u.ar.as = strides->as;
  1000. expr->ts = idx->ts;
  1001. /* offset = offset + ... */
  1002. block->block->next = gfc_get_code (EXEC_ASSIGN);
  1003. block->block->next->expr1 = gfc_lval_expr_from_sym (offset);
  1004. block->block->next->expr2 = gfc_get_expr ();
  1005. block->block->next->expr2->expr_type = EXPR_OP;
  1006. block->block->next->expr2->value.op.op = INTRINSIC_PLUS;
  1007. block->block->next->expr2->value.op.op1 = gfc_lval_expr_from_sym (offset);
  1008. block->block->next->expr2->value.op.op2 = expr;
  1009. block->block->next->expr2->ts = idx->ts;
  1010. /* After the loop: offset = offset * byte_stride. */
  1011. block->next = gfc_get_code (EXEC_ASSIGN);
  1012. block = block->next;
  1013. block->expr1 = gfc_lval_expr_from_sym (offset);
  1014. block->expr2 = gfc_get_expr ();
  1015. block->expr2->expr_type = EXPR_OP;
  1016. block->expr2->value.op.op = INTRINSIC_TIMES;
  1017. block->expr2->value.op.op1 = gfc_lval_expr_from_sym (offset);
  1018. block->expr2->value.op.op2 = gfc_lval_expr_from_sym (byte_stride);
  1019. block->expr2->ts = block->expr2->value.op.op1->ts;
  1020. return block;
  1021. }
  1022. /* Insert code of the following form:
  1023. block
  1024. integer(c_intptr_t) :: i
  1025. if ((byte_stride == STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
  1026. && (is_contiguous || !final_rank3->attr.contiguous
  1027. || final_rank3->as->type != AS_ASSUMED_SHAPE))
  1028. || 0 == STORAGE_SIZE (array)) then
  1029. call final_rank3 (array)
  1030. else
  1031. block
  1032. integer(c_intptr_t) :: offset, j
  1033. type(t) :: tmp(shape (array))
  1034. do i = 0, size (array)-1
  1035. offset = obtain_offset(i, strides, sizes, byte_stride)
  1036. addr = transfer (c_loc (array), addr) + offset
  1037. call c_f_pointer (transfer (addr, cptr), ptr)
  1038. addr = transfer (c_loc (tmp), addr)
  1039. + i * STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
  1040. call c_f_pointer (transfer (addr, cptr), ptr2)
  1041. ptr2 = ptr
  1042. end do
  1043. call final_rank3 (tmp)
  1044. end block
  1045. end if
  1046. block */
  1047. static void
  1048. finalizer_insert_packed_call (gfc_code *block, gfc_finalizer *fini,
  1049. gfc_symbol *array, gfc_symbol *byte_stride,
  1050. gfc_symbol *idx, gfc_symbol *ptr,
  1051. gfc_symbol *nelem,
  1052. gfc_symbol *strides, gfc_symbol *sizes,
  1053. gfc_symbol *idx2, gfc_symbol *offset,
  1054. gfc_symbol *is_contiguous, gfc_expr *rank,
  1055. gfc_namespace *sub_ns)
  1056. {
  1057. gfc_symbol *tmp_array, *ptr2;
  1058. gfc_expr *size_expr, *offset2, *expr;
  1059. gfc_namespace *ns;
  1060. gfc_iterator *iter;
  1061. gfc_code *block2;
  1062. int i;
  1063. block->next = gfc_get_code (EXEC_IF);
  1064. block = block->next;
  1065. block->block = gfc_get_code (EXEC_IF);
  1066. block = block->block;
  1067. /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
  1068. size_expr = gfc_get_expr ();
  1069. size_expr->where = gfc_current_locus;
  1070. size_expr->expr_type = EXPR_OP;
  1071. size_expr->value.op.op = INTRINSIC_DIVIDE;
  1072. /* STORAGE_SIZE (array,kind=c_intptr_t). */
  1073. size_expr->value.op.op1
  1074. = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STORAGE_SIZE,
  1075. "storage_size", gfc_current_locus, 2,
  1076. gfc_lval_expr_from_sym (array),
  1077. gfc_get_int_expr (gfc_index_integer_kind,
  1078. NULL, 0));
  1079. /* NUMERIC_STORAGE_SIZE. */
  1080. size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
  1081. gfc_character_storage_size);
  1082. size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
  1083. size_expr->ts = size_expr->value.op.op1->ts;
  1084. /* IF condition: (stride == size_expr
  1085. && ((fini's as->ASSUMED_SIZE && !fini's attr.contiguous)
  1086. || is_contiguous)
  1087. || 0 == size_expr. */
  1088. block->expr1 = gfc_get_expr ();
  1089. block->expr1->ts.type = BT_LOGICAL;
  1090. block->expr1->ts.kind = gfc_default_logical_kind;
  1091. block->expr1->expr_type = EXPR_OP;
  1092. block->expr1->where = gfc_current_locus;
  1093. block->expr1->value.op.op = INTRINSIC_OR;
  1094. /* byte_stride == size_expr */
  1095. expr = gfc_get_expr ();
  1096. expr->ts.type = BT_LOGICAL;
  1097. expr->ts.kind = gfc_default_logical_kind;
  1098. expr->expr_type = EXPR_OP;
  1099. expr->where = gfc_current_locus;
  1100. expr->value.op.op = INTRINSIC_EQ;
  1101. expr->value.op.op1
  1102. = gfc_lval_expr_from_sym (byte_stride);
  1103. expr->value.op.op2 = size_expr;
  1104. /* If strides aren't allowed (not assumed shape or CONTIGUOUS),
  1105. add is_contiguous check. */
  1106. if (fini->proc_tree->n.sym->formal->sym->as->type != AS_ASSUMED_SHAPE
  1107. || fini->proc_tree->n.sym->formal->sym->attr.contiguous)
  1108. {
  1109. gfc_expr *expr2;
  1110. expr2 = gfc_get_expr ();
  1111. expr2->ts.type = BT_LOGICAL;
  1112. expr2->ts.kind = gfc_default_logical_kind;
  1113. expr2->expr_type = EXPR_OP;
  1114. expr2->where = gfc_current_locus;
  1115. expr2->value.op.op = INTRINSIC_AND;
  1116. expr2->value.op.op1 = expr;
  1117. expr2->value.op.op2 = gfc_lval_expr_from_sym (is_contiguous);
  1118. expr = expr2;
  1119. }
  1120. block->expr1->value.op.op1 = expr;
  1121. /* 0 == size_expr */
  1122. block->expr1->value.op.op2 = gfc_get_expr ();
  1123. block->expr1->value.op.op2->ts.type = BT_LOGICAL;
  1124. block->expr1->value.op.op2->ts.kind = gfc_default_logical_kind;
  1125. block->expr1->value.op.op2->expr_type = EXPR_OP;
  1126. block->expr1->value.op.op2->where = gfc_current_locus;
  1127. block->expr1->value.op.op2->value.op.op = INTRINSIC_EQ;
  1128. block->expr1->value.op.op2->value.op.op1 =
  1129. gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1130. block->expr1->value.op.op2->value.op.op2 = gfc_copy_expr (size_expr);
  1131. /* IF body: call final subroutine. */
  1132. block->next = gfc_get_code (EXEC_CALL);
  1133. block->next->symtree = fini->proc_tree;
  1134. block->next->resolved_sym = fini->proc_tree->n.sym;
  1135. block->next->ext.actual = gfc_get_actual_arglist ();
  1136. block->next->ext.actual->expr = gfc_lval_expr_from_sym (array);
  1137. /* ELSE. */
  1138. block->block = gfc_get_code (EXEC_IF);
  1139. block = block->block;
  1140. /* BLOCK ... END BLOCK. */
  1141. block->next = gfc_get_code (EXEC_BLOCK);
  1142. block = block->next;
  1143. ns = gfc_build_block_ns (sub_ns);
  1144. block->ext.block.ns = ns;
  1145. block->ext.block.assoc = NULL;
  1146. gfc_get_symbol ("ptr2", ns, &ptr2);
  1147. ptr2->ts.type = BT_DERIVED;
  1148. ptr2->ts.u.derived = array->ts.u.derived;
  1149. ptr2->attr.flavor = FL_VARIABLE;
  1150. ptr2->attr.pointer = 1;
  1151. ptr2->attr.artificial = 1;
  1152. gfc_set_sym_referenced (ptr2);
  1153. gfc_commit_symbol (ptr2);
  1154. gfc_get_symbol ("tmp_array", ns, &tmp_array);
  1155. tmp_array->ts.type = BT_DERIVED;
  1156. tmp_array->ts.u.derived = array->ts.u.derived;
  1157. tmp_array->attr.flavor = FL_VARIABLE;
  1158. tmp_array->attr.dimension = 1;
  1159. tmp_array->attr.artificial = 1;
  1160. tmp_array->as = gfc_get_array_spec();
  1161. tmp_array->attr.intent = INTENT_INOUT;
  1162. tmp_array->as->type = AS_EXPLICIT;
  1163. tmp_array->as->rank = fini->proc_tree->n.sym->formal->sym->as->rank;
  1164. for (i = 0; i < tmp_array->as->rank; i++)
  1165. {
  1166. gfc_expr *shape_expr;
  1167. tmp_array->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
  1168. NULL, 1);
  1169. /* SIZE (array, dim=i+1, kind=gfc_index_integer_kind). */
  1170. shape_expr
  1171. = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
  1172. gfc_current_locus, 3,
  1173. gfc_lval_expr_from_sym (array),
  1174. gfc_get_int_expr (gfc_default_integer_kind,
  1175. NULL, i+1),
  1176. gfc_get_int_expr (gfc_default_integer_kind,
  1177. NULL,
  1178. gfc_index_integer_kind));
  1179. shape_expr->ts.kind = gfc_index_integer_kind;
  1180. tmp_array->as->upper[i] = shape_expr;
  1181. }
  1182. gfc_set_sym_referenced (tmp_array);
  1183. gfc_commit_symbol (tmp_array);
  1184. /* Create loop. */
  1185. iter = gfc_get_iterator ();
  1186. iter->var = gfc_lval_expr_from_sym (idx);
  1187. iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1188. iter->end = gfc_lval_expr_from_sym (nelem);
  1189. iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1190. block = gfc_get_code (EXEC_DO);
  1191. ns->code = block;
  1192. block->ext.iterator = iter;
  1193. block->block = gfc_get_code (EXEC_DO);
  1194. /* Offset calculation for the new array: idx * size of type (in bytes). */
  1195. offset2 = gfc_get_expr ();
  1196. offset2->expr_type = EXPR_OP;
  1197. offset2->value.op.op = INTRINSIC_TIMES;
  1198. offset2->value.op.op1 = gfc_lval_expr_from_sym (idx);
  1199. offset2->value.op.op2 = gfc_copy_expr (size_expr);
  1200. offset2->ts = byte_stride->ts;
  1201. /* Offset calculation of "array". */
  1202. block2 = finalization_get_offset (idx, idx2, offset, strides, sizes,
  1203. byte_stride, rank, block->block, sub_ns);
  1204. /* Create code for
  1205. CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
  1206. + idx * stride, c_ptr), ptr). */
  1207. block2->next = finalization_scalarizer (array, ptr,
  1208. gfc_lval_expr_from_sym (offset),
  1209. sub_ns);
  1210. block2 = block2->next;
  1211. block2->next = finalization_scalarizer (tmp_array, ptr2, offset2, sub_ns);
  1212. block2 = block2->next;
  1213. /* ptr2 = ptr. */
  1214. block2->next = gfc_get_code (EXEC_ASSIGN);
  1215. block2 = block2->next;
  1216. block2->expr1 = gfc_lval_expr_from_sym (ptr2);
  1217. block2->expr2 = gfc_lval_expr_from_sym (ptr);
  1218. /* Call now the user's final subroutine. */
  1219. block->next = gfc_get_code (EXEC_CALL);
  1220. block = block->next;
  1221. block->symtree = fini->proc_tree;
  1222. block->resolved_sym = fini->proc_tree->n.sym;
  1223. block->ext.actual = gfc_get_actual_arglist ();
  1224. block->ext.actual->expr = gfc_lval_expr_from_sym (tmp_array);
  1225. if (fini->proc_tree->n.sym->formal->sym->attr.intent == INTENT_IN)
  1226. return;
  1227. /* Copy back. */
  1228. /* Loop. */
  1229. iter = gfc_get_iterator ();
  1230. iter->var = gfc_lval_expr_from_sym (idx);
  1231. iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1232. iter->end = gfc_lval_expr_from_sym (nelem);
  1233. iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1234. block->next = gfc_get_code (EXEC_DO);
  1235. block = block->next;
  1236. block->ext.iterator = iter;
  1237. block->block = gfc_get_code (EXEC_DO);
  1238. /* Offset calculation of "array". */
  1239. block2 = finalization_get_offset (idx, idx2, offset, strides, sizes,
  1240. byte_stride, rank, block->block, sub_ns);
  1241. /* Create code for
  1242. CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
  1243. + offset, c_ptr), ptr). */
  1244. block2->next = finalization_scalarizer (array, ptr,
  1245. gfc_lval_expr_from_sym (offset),
  1246. sub_ns);
  1247. block2 = block2->next;
  1248. block2->next = finalization_scalarizer (tmp_array, ptr2,
  1249. gfc_copy_expr (offset2), sub_ns);
  1250. block2 = block2->next;
  1251. /* ptr = ptr2. */
  1252. block2->next = gfc_get_code (EXEC_ASSIGN);
  1253. block2->next->expr1 = gfc_lval_expr_from_sym (ptr);
  1254. block2->next->expr2 = gfc_lval_expr_from_sym (ptr2);
  1255. }
  1256. /* Generate the finalization/polymorphic freeing wrapper subroutine for the
  1257. derived type "derived". The function first calls the approriate FINAL
  1258. subroutine, then it DEALLOCATEs (finalizes/frees) the allocatable
  1259. components (but not the inherited ones). Last, it calls the wrapper
  1260. subroutine of the parent. The generated wrapper procedure takes as argument
  1261. an assumed-rank array.
  1262. If neither allocatable components nor FINAL subroutines exists, the vtab
  1263. will contain a NULL pointer.
  1264. The generated function has the form
  1265. _final(assumed-rank array, stride, skip_corarray)
  1266. where the array has to be contiguous (except of the lowest dimension). The
  1267. stride (in bytes) is used to allow different sizes for ancestor types by
  1268. skipping over the additionally added components in the scalarizer. If
  1269. "fini_coarray" is false, coarray components are not finalized to allow for
  1270. the correct semantic with intrinsic assignment. */
  1271. static void
  1272. generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns,
  1273. const char *tname, gfc_component *vtab_final)
  1274. {
  1275. gfc_symbol *final, *array, *fini_coarray, *byte_stride, *sizes, *strides;
  1276. gfc_symbol *ptr = NULL, *idx, *idx2, *is_contiguous, *offset, *nelem;
  1277. gfc_component *comp;
  1278. gfc_namespace *sub_ns;
  1279. gfc_code *last_code, *block;
  1280. char name[GFC_MAX_SYMBOL_LEN+1];
  1281. bool finalizable_comp = false;
  1282. bool expr_null_wrapper = false;
  1283. gfc_expr *ancestor_wrapper = NULL, *rank;
  1284. gfc_iterator *iter;
  1285. if (derived->attr.unlimited_polymorphic)
  1286. {
  1287. vtab_final->initializer = gfc_get_null_expr (NULL);
  1288. return;
  1289. }
  1290. /* Search for the ancestor's finalizers. */
  1291. if (derived->attr.extension && derived->components
  1292. && (!derived->components->ts.u.derived->attr.abstract
  1293. || has_finalizer_component (derived)))
  1294. {
  1295. gfc_symbol *vtab;
  1296. gfc_component *comp;
  1297. vtab = gfc_find_derived_vtab (derived->components->ts.u.derived);
  1298. for (comp = vtab->ts.u.derived->components; comp; comp = comp->next)
  1299. if (comp->name[0] == '_' && comp->name[1] == 'f')
  1300. {
  1301. ancestor_wrapper = comp->initializer;
  1302. break;
  1303. }
  1304. }
  1305. /* No wrapper of the ancestor and no own FINAL subroutines and allocatable
  1306. components: Return a NULL() expression; we defer this a bit to have have
  1307. an interface declaration. */
  1308. if ((!ancestor_wrapper || ancestor_wrapper->expr_type == EXPR_NULL)
  1309. && !derived->attr.alloc_comp
  1310. && (!derived->f2k_derived || !derived->f2k_derived->finalizers)
  1311. && !has_finalizer_component (derived))
  1312. expr_null_wrapper = true;
  1313. else
  1314. /* Check whether there are new allocatable components. */
  1315. for (comp = derived->components; comp; comp = comp->next)
  1316. {
  1317. if (comp == derived->components && derived->attr.extension
  1318. && ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
  1319. continue;
  1320. finalizable_comp |= comp_is_finalizable (comp);
  1321. }
  1322. /* If there is no new finalizer and no new allocatable, return with
  1323. an expr to the ancestor's one. */
  1324. if (!expr_null_wrapper && !finalizable_comp
  1325. && (!derived->f2k_derived || !derived->f2k_derived->finalizers))
  1326. {
  1327. gcc_assert (ancestor_wrapper && ancestor_wrapper->ref == NULL
  1328. && ancestor_wrapper->expr_type == EXPR_VARIABLE);
  1329. vtab_final->initializer = gfc_copy_expr (ancestor_wrapper);
  1330. vtab_final->ts.interface = vtab_final->initializer->symtree->n.sym;
  1331. return;
  1332. }
  1333. /* We now create a wrapper, which does the following:
  1334. 1. Call the suitable finalization subroutine for this type
  1335. 2. Loop over all noninherited allocatable components and noninherited
  1336. components with allocatable components and DEALLOCATE those; this will
  1337. take care of finalizers, coarray deregistering and allocatable
  1338. nested components.
  1339. 3. Call the ancestor's finalizer. */
  1340. /* Declare the wrapper function; it takes an assumed-rank array
  1341. and a VALUE logical as arguments. */
  1342. /* Set up the namespace. */
  1343. sub_ns = gfc_get_namespace (ns, 0);
  1344. sub_ns->sibling = ns->contained;
  1345. if (!expr_null_wrapper)
  1346. ns->contained = sub_ns;
  1347. sub_ns->resolved = 1;
  1348. /* Set up the procedure symbol. */
  1349. sprintf (name, "__final_%s", tname);
  1350. gfc_get_symbol (name, sub_ns, &final);
  1351. sub_ns->proc_name = final;
  1352. final->attr.flavor = FL_PROCEDURE;
  1353. final->attr.function = 1;
  1354. final->attr.pure = 0;
  1355. final->result = final;
  1356. final->ts.type = BT_INTEGER;
  1357. final->ts.kind = 4;
  1358. final->attr.artificial = 1;
  1359. final->attr.if_source = expr_null_wrapper ? IFSRC_IFBODY : IFSRC_DECL;
  1360. if (ns->proc_name->attr.flavor == FL_MODULE)
  1361. final->module = ns->proc_name->name;
  1362. gfc_set_sym_referenced (final);
  1363. gfc_commit_symbol (final);
  1364. /* Set up formal argument. */
  1365. gfc_get_symbol ("array", sub_ns, &array);
  1366. array->ts.type = BT_DERIVED;
  1367. array->ts.u.derived = derived;
  1368. array->attr.flavor = FL_VARIABLE;
  1369. array->attr.dummy = 1;
  1370. array->attr.contiguous = 1;
  1371. array->attr.dimension = 1;
  1372. array->attr.artificial = 1;
  1373. array->as = gfc_get_array_spec();
  1374. array->as->type = AS_ASSUMED_RANK;
  1375. array->as->rank = -1;
  1376. array->attr.intent = INTENT_INOUT;
  1377. gfc_set_sym_referenced (array);
  1378. final->formal = gfc_get_formal_arglist ();
  1379. final->formal->sym = array;
  1380. gfc_commit_symbol (array);
  1381. /* Set up formal argument. */
  1382. gfc_get_symbol ("byte_stride", sub_ns, &byte_stride);
  1383. byte_stride->ts.type = BT_INTEGER;
  1384. byte_stride->ts.kind = gfc_index_integer_kind;
  1385. byte_stride->attr.flavor = FL_VARIABLE;
  1386. byte_stride->attr.dummy = 1;
  1387. byte_stride->attr.value = 1;
  1388. byte_stride->attr.artificial = 1;
  1389. gfc_set_sym_referenced (byte_stride);
  1390. final->formal->next = gfc_get_formal_arglist ();
  1391. final->formal->next->sym = byte_stride;
  1392. gfc_commit_symbol (byte_stride);
  1393. /* Set up formal argument. */
  1394. gfc_get_symbol ("fini_coarray", sub_ns, &fini_coarray);
  1395. fini_coarray->ts.type = BT_LOGICAL;
  1396. fini_coarray->ts.kind = 1;
  1397. fini_coarray->attr.flavor = FL_VARIABLE;
  1398. fini_coarray->attr.dummy = 1;
  1399. fini_coarray->attr.value = 1;
  1400. fini_coarray->attr.artificial = 1;
  1401. gfc_set_sym_referenced (fini_coarray);
  1402. final->formal->next->next = gfc_get_formal_arglist ();
  1403. final->formal->next->next->sym = fini_coarray;
  1404. gfc_commit_symbol (fini_coarray);
  1405. /* Return with a NULL() expression but with an interface which has
  1406. the formal arguments. */
  1407. if (expr_null_wrapper)
  1408. {
  1409. vtab_final->initializer = gfc_get_null_expr (NULL);
  1410. vtab_final->ts.interface = final;
  1411. return;
  1412. }
  1413. /* Local variables. */
  1414. gfc_get_symbol ("idx", sub_ns, &idx);
  1415. idx->ts.type = BT_INTEGER;
  1416. idx->ts.kind = gfc_index_integer_kind;
  1417. idx->attr.flavor = FL_VARIABLE;
  1418. idx->attr.artificial = 1;
  1419. gfc_set_sym_referenced (idx);
  1420. gfc_commit_symbol (idx);
  1421. gfc_get_symbol ("idx2", sub_ns, &idx2);
  1422. idx2->ts.type = BT_INTEGER;
  1423. idx2->ts.kind = gfc_index_integer_kind;
  1424. idx2->attr.flavor = FL_VARIABLE;
  1425. idx2->attr.artificial = 1;
  1426. gfc_set_sym_referenced (idx2);
  1427. gfc_commit_symbol (idx2);
  1428. gfc_get_symbol ("offset", sub_ns, &offset);
  1429. offset->ts.type = BT_INTEGER;
  1430. offset->ts.kind = gfc_index_integer_kind;
  1431. offset->attr.flavor = FL_VARIABLE;
  1432. offset->attr.artificial = 1;
  1433. gfc_set_sym_referenced (offset);
  1434. gfc_commit_symbol (offset);
  1435. /* Create RANK expression. */
  1436. rank = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_RANK, "rank",
  1437. gfc_current_locus, 1,
  1438. gfc_lval_expr_from_sym (array));
  1439. if (rank->ts.kind != idx->ts.kind)
  1440. gfc_convert_type_warn (rank, &idx->ts, 2, 0);
  1441. /* Create is_contiguous variable. */
  1442. gfc_get_symbol ("is_contiguous", sub_ns, &is_contiguous);
  1443. is_contiguous->ts.type = BT_LOGICAL;
  1444. is_contiguous->ts.kind = gfc_default_logical_kind;
  1445. is_contiguous->attr.flavor = FL_VARIABLE;
  1446. is_contiguous->attr.artificial = 1;
  1447. gfc_set_sym_referenced (is_contiguous);
  1448. gfc_commit_symbol (is_contiguous);
  1449. /* Create "sizes(0..rank)" variable, which contains the multiplied
  1450. up extent of the dimensions, i.e. sizes(0) = 1, sizes(1) = extent(dim=1),
  1451. sizes(2) = sizes(1) * extent(dim=2) etc. */
  1452. gfc_get_symbol ("sizes", sub_ns, &sizes);
  1453. sizes->ts.type = BT_INTEGER;
  1454. sizes->ts.kind = gfc_index_integer_kind;
  1455. sizes->attr.flavor = FL_VARIABLE;
  1456. sizes->attr.dimension = 1;
  1457. sizes->attr.artificial = 1;
  1458. sizes->as = gfc_get_array_spec();
  1459. sizes->attr.intent = INTENT_INOUT;
  1460. sizes->as->type = AS_EXPLICIT;
  1461. sizes->as->rank = 1;
  1462. sizes->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1463. sizes->as->upper[0] = gfc_copy_expr (rank);
  1464. gfc_set_sym_referenced (sizes);
  1465. gfc_commit_symbol (sizes);
  1466. /* Create "strides(1..rank)" variable, which contains the strides per
  1467. dimension. */
  1468. gfc_get_symbol ("strides", sub_ns, &strides);
  1469. strides->ts.type = BT_INTEGER;
  1470. strides->ts.kind = gfc_index_integer_kind;
  1471. strides->attr.flavor = FL_VARIABLE;
  1472. strides->attr.dimension = 1;
  1473. strides->attr.artificial = 1;
  1474. strides->as = gfc_get_array_spec();
  1475. strides->attr.intent = INTENT_INOUT;
  1476. strides->as->type = AS_EXPLICIT;
  1477. strides->as->rank = 1;
  1478. strides->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1479. strides->as->upper[0] = gfc_copy_expr (rank);
  1480. gfc_set_sym_referenced (strides);
  1481. gfc_commit_symbol (strides);
  1482. /* Set return value to 0. */
  1483. last_code = gfc_get_code (EXEC_ASSIGN);
  1484. last_code->expr1 = gfc_lval_expr_from_sym (final);
  1485. last_code->expr2 = gfc_get_int_expr (4, NULL, 0);
  1486. sub_ns->code = last_code;
  1487. /* Set: is_contiguous = .true. */
  1488. last_code->next = gfc_get_code (EXEC_ASSIGN);
  1489. last_code = last_code->next;
  1490. last_code->expr1 = gfc_lval_expr_from_sym (is_contiguous);
  1491. last_code->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
  1492. &gfc_current_locus, true);
  1493. /* Set: sizes(0) = 1. */
  1494. last_code->next = gfc_get_code (EXEC_ASSIGN);
  1495. last_code = last_code->next;
  1496. last_code->expr1 = gfc_lval_expr_from_sym (sizes);
  1497. last_code->expr1->ref = gfc_get_ref ();
  1498. last_code->expr1->ref->type = REF_ARRAY;
  1499. last_code->expr1->ref->u.ar.type = AR_ELEMENT;
  1500. last_code->expr1->ref->u.ar.dimen = 1;
  1501. last_code->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1502. last_code->expr1->ref->u.ar.start[0]
  1503. = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1504. last_code->expr1->ref->u.ar.as = sizes->as;
  1505. last_code->expr2 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
  1506. /* Create:
  1507. DO idx = 1, rank
  1508. strides(idx) = _F._stride (array, dim=idx)
  1509. sizes(idx) = sizes(i-1) * size(array, dim=idx, kind=index_kind)
  1510. if (strides (idx) /= sizes(i-1)) is_contiguous = .false.
  1511. END DO. */
  1512. /* Create loop. */
  1513. iter = gfc_get_iterator ();
  1514. iter->var = gfc_lval_expr_from_sym (idx);
  1515. iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1516. iter->end = gfc_copy_expr (rank);
  1517. iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1518. last_code->next = gfc_get_code (EXEC_DO);
  1519. last_code = last_code->next;
  1520. last_code->ext.iterator = iter;
  1521. last_code->block = gfc_get_code (EXEC_DO);
  1522. /* strides(idx) = _F._stride(array,dim=idx). */
  1523. last_code->block->next = gfc_get_code (EXEC_ASSIGN);
  1524. block = last_code->block->next;
  1525. block->expr1 = gfc_lval_expr_from_sym (strides);
  1526. block->expr1->ref = gfc_get_ref ();
  1527. block->expr1->ref->type = REF_ARRAY;
  1528. block->expr1->ref->u.ar.type = AR_ELEMENT;
  1529. block->expr1->ref->u.ar.dimen = 1;
  1530. block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1531. block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
  1532. block->expr1->ref->u.ar.as = strides->as;
  1533. block->expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STRIDE, "stride",
  1534. gfc_current_locus, 2,
  1535. gfc_lval_expr_from_sym (array),
  1536. gfc_lval_expr_from_sym (idx));
  1537. /* sizes(idx) = sizes(idx-1) * size(array,dim=idx, kind=index_kind). */
  1538. block->next = gfc_get_code (EXEC_ASSIGN);
  1539. block = block->next;
  1540. /* sizes(idx) = ... */
  1541. block->expr1 = gfc_lval_expr_from_sym (sizes);
  1542. block->expr1->ref = gfc_get_ref ();
  1543. block->expr1->ref->type = REF_ARRAY;
  1544. block->expr1->ref->u.ar.type = AR_ELEMENT;
  1545. block->expr1->ref->u.ar.dimen = 1;
  1546. block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1547. block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
  1548. block->expr1->ref->u.ar.as = sizes->as;
  1549. block->expr2 = gfc_get_expr ();
  1550. block->expr2->expr_type = EXPR_OP;
  1551. block->expr2->value.op.op = INTRINSIC_TIMES;
  1552. /* sizes(idx-1). */
  1553. block->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
  1554. block->expr2->value.op.op1->ref = gfc_get_ref ();
  1555. block->expr2->value.op.op1->ref->type = REF_ARRAY;
  1556. block->expr2->value.op.op1->ref->u.ar.as = sizes->as;
  1557. block->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
  1558. block->expr2->value.op.op1->ref->u.ar.dimen = 1;
  1559. block->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1560. block->expr2->value.op.op1->ref->u.ar.start[0] = gfc_get_expr ();
  1561. block->expr2->value.op.op1->ref->u.ar.start[0]->expr_type = EXPR_OP;
  1562. block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
  1563. block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1
  1564. = gfc_lval_expr_from_sym (idx);
  1565. block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op2
  1566. = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1567. block->expr2->value.op.op1->ref->u.ar.start[0]->ts
  1568. = block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1->ts;
  1569. /* size(array, dim=idx, kind=index_kind). */
  1570. block->expr2->value.op.op2
  1571. = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
  1572. gfc_current_locus, 3,
  1573. gfc_lval_expr_from_sym (array),
  1574. gfc_lval_expr_from_sym (idx),
  1575. gfc_get_int_expr (gfc_index_integer_kind,
  1576. NULL,
  1577. gfc_index_integer_kind));
  1578. block->expr2->value.op.op2->ts.kind = gfc_index_integer_kind;
  1579. block->expr2->ts = idx->ts;
  1580. /* if (strides (idx) /= sizes(idx-1)) is_contiguous = .false. */
  1581. block->next = gfc_get_code (EXEC_IF);
  1582. block = block->next;
  1583. block->block = gfc_get_code (EXEC_IF);
  1584. block = block->block;
  1585. /* if condition: strides(idx) /= sizes(idx-1). */
  1586. block->expr1 = gfc_get_expr ();
  1587. block->expr1->ts.type = BT_LOGICAL;
  1588. block->expr1->ts.kind = gfc_default_logical_kind;
  1589. block->expr1->expr_type = EXPR_OP;
  1590. block->expr1->where = gfc_current_locus;
  1591. block->expr1->value.op.op = INTRINSIC_NE;
  1592. block->expr1->value.op.op1 = gfc_lval_expr_from_sym (strides);
  1593. block->expr1->value.op.op1->ref = gfc_get_ref ();
  1594. block->expr1->value.op.op1->ref->type = REF_ARRAY;
  1595. block->expr1->value.op.op1->ref->u.ar.type = AR_ELEMENT;
  1596. block->expr1->value.op.op1->ref->u.ar.dimen = 1;
  1597. block->expr1->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1598. block->expr1->value.op.op1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
  1599. block->expr1->value.op.op1->ref->u.ar.as = strides->as;
  1600. block->expr1->value.op.op2 = gfc_lval_expr_from_sym (sizes);
  1601. block->expr1->value.op.op2->ref = gfc_get_ref ();
  1602. block->expr1->value.op.op2->ref->type = REF_ARRAY;
  1603. block->expr1->value.op.op2->ref->u.ar.as = sizes->as;
  1604. block->expr1->value.op.op2->ref->u.ar.type = AR_ELEMENT;
  1605. block->expr1->value.op.op2->ref->u.ar.dimen = 1;
  1606. block->expr1->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1607. block->expr1->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
  1608. block->expr1->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
  1609. block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
  1610. block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1
  1611. = gfc_lval_expr_from_sym (idx);
  1612. block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op2
  1613. = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1614. block->expr1->value.op.op2->ref->u.ar.start[0]->ts
  1615. = block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
  1616. /* if body: is_contiguous = .false. */
  1617. block->next = gfc_get_code (EXEC_ASSIGN);
  1618. block = block->next;
  1619. block->expr1 = gfc_lval_expr_from_sym (is_contiguous);
  1620. block->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
  1621. &gfc_current_locus, false);
  1622. /* Obtain the size (number of elements) of "array" MINUS ONE,
  1623. which is used in the scalarization. */
  1624. gfc_get_symbol ("nelem", sub_ns, &nelem);
  1625. nelem->ts.type = BT_INTEGER;
  1626. nelem->ts.kind = gfc_index_integer_kind;
  1627. nelem->attr.flavor = FL_VARIABLE;
  1628. nelem->attr.artificial = 1;
  1629. gfc_set_sym_referenced (nelem);
  1630. gfc_commit_symbol (nelem);
  1631. /* nelem = sizes (rank) - 1. */
  1632. last_code->next = gfc_get_code (EXEC_ASSIGN);
  1633. last_code = last_code->next;
  1634. last_code->expr1 = gfc_lval_expr_from_sym (nelem);
  1635. last_code->expr2 = gfc_get_expr ();
  1636. last_code->expr2->expr_type = EXPR_OP;
  1637. last_code->expr2->value.op.op = INTRINSIC_MINUS;
  1638. last_code->expr2->value.op.op2
  1639. = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1640. last_code->expr2->ts = last_code->expr2->value.op.op2->ts;
  1641. last_code->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
  1642. last_code->expr2->value.op.op1->ref = gfc_get_ref ();
  1643. last_code->expr2->value.op.op1->ref->type = REF_ARRAY;
  1644. last_code->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
  1645. last_code->expr2->value.op.op1->ref->u.ar.dimen = 1;
  1646. last_code->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
  1647. last_code->expr2->value.op.op1->ref->u.ar.start[0] = gfc_copy_expr (rank);
  1648. last_code->expr2->value.op.op1->ref->u.ar.as = sizes->as;
  1649. /* Call final subroutines. We now generate code like:
  1650. use iso_c_binding
  1651. integer, pointer :: ptr
  1652. type(c_ptr) :: cptr
  1653. integer(c_intptr_t) :: i, addr
  1654. select case (rank (array))
  1655. case (3)
  1656. ! If needed, the array is packed
  1657. call final_rank3 (array)
  1658. case default:
  1659. do i = 0, size (array)-1
  1660. addr = transfer (c_loc (array), addr) + i * stride
  1661. call c_f_pointer (transfer (addr, cptr), ptr)
  1662. call elemental_final (ptr)
  1663. end do
  1664. end select */
  1665. if (derived->f2k_derived && derived->f2k_derived->finalizers)
  1666. {
  1667. gfc_finalizer *fini, *fini_elem = NULL;
  1668. gfc_get_symbol ("ptr1", sub_ns, &ptr);
  1669. ptr->ts.type = BT_DERIVED;
  1670. ptr->ts.u.derived = derived;
  1671. ptr->attr.flavor = FL_VARIABLE;
  1672. ptr->attr.pointer = 1;
  1673. ptr->attr.artificial = 1;
  1674. gfc_set_sym_referenced (ptr);
  1675. gfc_commit_symbol (ptr);
  1676. /* SELECT CASE (RANK (array)). */
  1677. last_code->next = gfc_get_code (EXEC_SELECT);
  1678. last_code = last_code->next;
  1679. last_code->expr1 = gfc_copy_expr (rank);
  1680. block = NULL;
  1681. for (fini = derived->f2k_derived->finalizers; fini; fini = fini->next)
  1682. {
  1683. gcc_assert (fini->proc_tree); /* Should have been set in gfc_resolve_finalizers. */
  1684. if (fini->proc_tree->n.sym->attr.elemental)
  1685. {
  1686. fini_elem = fini;
  1687. continue;
  1688. }
  1689. /* CASE (fini_rank). */
  1690. if (block)
  1691. {
  1692. block->block = gfc_get_code (EXEC_SELECT);
  1693. block = block->block;
  1694. }
  1695. else
  1696. {
  1697. block = gfc_get_code (EXEC_SELECT);
  1698. last_code->block = block;
  1699. }
  1700. block->ext.block.case_list = gfc_get_case ();
  1701. block->ext.block.case_list->where = gfc_current_locus;
  1702. if (fini->proc_tree->n.sym->formal->sym->attr.dimension)
  1703. block->ext.block.case_list->low
  1704. = gfc_get_int_expr (gfc_default_integer_kind, NULL,
  1705. fini->proc_tree->n.sym->formal->sym->as->rank);
  1706. else
  1707. block->ext.block.case_list->low
  1708. = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
  1709. block->ext.block.case_list->high
  1710. = gfc_copy_expr (block->ext.block.case_list->low);
  1711. /* CALL fini_rank (array) - possibly with packing. */
  1712. if (fini->proc_tree->n.sym->formal->sym->attr.dimension)
  1713. finalizer_insert_packed_call (block, fini, array, byte_stride,
  1714. idx, ptr, nelem, strides,
  1715. sizes, idx2, offset, is_contiguous,
  1716. rank, sub_ns);
  1717. else
  1718. {
  1719. block->next = gfc_get_code (EXEC_CALL);
  1720. block->next->symtree = fini->proc_tree;
  1721. block->next->resolved_sym = fini->proc_tree->n.sym;
  1722. block->next->ext.actual = gfc_get_actual_arglist ();
  1723. block->next->ext.actual->expr = gfc_lval_expr_from_sym (array);
  1724. }
  1725. }
  1726. /* Elemental call - scalarized. */
  1727. if (fini_elem)
  1728. {
  1729. /* CASE DEFAULT. */
  1730. if (block)
  1731. {
  1732. block->block = gfc_get_code (EXEC_SELECT);
  1733. block = block->block;
  1734. }
  1735. else
  1736. {
  1737. block = gfc_get_code (EXEC_SELECT);
  1738. last_code->block = block;
  1739. }
  1740. block->ext.block.case_list = gfc_get_case ();
  1741. /* Create loop. */
  1742. iter = gfc_get_iterator ();
  1743. iter->var = gfc_lval_expr_from_sym (idx);
  1744. iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1745. iter->end = gfc_lval_expr_from_sym (nelem);
  1746. iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1747. block->next = gfc_get_code (EXEC_DO);
  1748. block = block->next;
  1749. block->ext.iterator = iter;
  1750. block->block = gfc_get_code (EXEC_DO);
  1751. /* Offset calculation. */
  1752. block = finalization_get_offset (idx, idx2, offset, strides, sizes,
  1753. byte_stride, rank, block->block,
  1754. sub_ns);
  1755. /* Create code for
  1756. CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
  1757. + offset, c_ptr), ptr). */
  1758. block->next
  1759. = finalization_scalarizer (array, ptr,
  1760. gfc_lval_expr_from_sym (offset),
  1761. sub_ns);
  1762. block = block->next;
  1763. /* CALL final_elemental (array). */
  1764. block->next = gfc_get_code (EXEC_CALL);
  1765. block = block->next;
  1766. block->symtree = fini_elem->proc_tree;
  1767. block->resolved_sym = fini_elem->proc_sym;
  1768. block->ext.actual = gfc_get_actual_arglist ();
  1769. block->ext.actual->expr = gfc_lval_expr_from_sym (ptr);
  1770. }
  1771. }
  1772. /* Finalize and deallocate allocatable components. The same manual
  1773. scalarization is used as above. */
  1774. if (finalizable_comp)
  1775. {
  1776. gfc_symbol *stat;
  1777. gfc_code *block = NULL;
  1778. if (!ptr)
  1779. {
  1780. gfc_get_symbol ("ptr2", sub_ns, &ptr);
  1781. ptr->ts.type = BT_DERIVED;
  1782. ptr->ts.u.derived = derived;
  1783. ptr->attr.flavor = FL_VARIABLE;
  1784. ptr->attr.pointer = 1;
  1785. ptr->attr.artificial = 1;
  1786. gfc_set_sym_referenced (ptr);
  1787. gfc_commit_symbol (ptr);
  1788. }
  1789. gfc_get_symbol ("ignore", sub_ns, &stat);
  1790. stat->attr.flavor = FL_VARIABLE;
  1791. stat->attr.artificial = 1;
  1792. stat->ts.type = BT_INTEGER;
  1793. stat->ts.kind = gfc_default_integer_kind;
  1794. gfc_set_sym_referenced (stat);
  1795. gfc_commit_symbol (stat);
  1796. /* Create loop. */
  1797. iter = gfc_get_iterator ();
  1798. iter->var = gfc_lval_expr_from_sym (idx);
  1799. iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
  1800. iter->end = gfc_lval_expr_from_sym (nelem);
  1801. iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
  1802. last_code->next = gfc_get_code (EXEC_DO);
  1803. last_code = last_code->next;
  1804. last_code->ext.iterator = iter;
  1805. last_code->block = gfc_get_code (EXEC_DO);
  1806. /* Offset calculation. */
  1807. block = finalization_get_offset (idx, idx2, offset, strides, sizes,
  1808. byte_stride, rank, last_code->block,
  1809. sub_ns);
  1810. /* Create code for
  1811. CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
  1812. + idx * stride, c_ptr), ptr). */
  1813. block->next = finalization_scalarizer (array, ptr,
  1814. gfc_lval_expr_from_sym(offset),
  1815. sub_ns);
  1816. block = block->next;
  1817. for (comp = derived->components; comp; comp = comp->next)
  1818. {
  1819. if (comp == derived->components && derived->attr.extension
  1820. && ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
  1821. continue;
  1822. finalize_component (gfc_lval_expr_from_sym (ptr), derived, comp,
  1823. stat, fini_coarray, &block, sub_ns);
  1824. if (!last_code->block->next)
  1825. last_code->block->next = block;
  1826. }
  1827. }
  1828. /* Call the finalizer of the ancestor. */
  1829. if (ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
  1830. {
  1831. last_code->next = gfc_get_code (EXEC_CALL);
  1832. last_code = last_code->next;
  1833. last_code->symtree = ancestor_wrapper->symtree;
  1834. last_code->resolved_sym = ancestor_wrapper->symtree->n.sym;
  1835. last_code->ext.actual = gfc_get_actual_arglist ();
  1836. last_code->ext.actual->expr = gfc_lval_expr_from_sym (array);
  1837. last_code->ext.actual->next = gfc_get_actual_arglist ();
  1838. last_code->ext.actual->next->expr = gfc_lval_expr_from_sym (byte_stride);
  1839. last_code->ext.actual->next->next = gfc_get_actual_arglist ();
  1840. last_code->ext.actual->next->next->expr
  1841. = gfc_lval_expr_from_sym (fini_coarray);
  1842. }
  1843. gfc_free_expr (rank);
  1844. vtab_final->initializer = gfc_lval_expr_from_sym (final);
  1845. vtab_final->ts.interface = final;
  1846. }
  1847. /* Add procedure pointers for all type-bound procedures to a vtab. */
  1848. static void
  1849. add_procs_to_declared_vtab (gfc_symbol *derived, gfc_symbol *vtype)
  1850. {
  1851. gfc_symbol* super_type;
  1852. super_type = gfc_get_derived_super_type (derived);
  1853. if (super_type && (super_type != derived))
  1854. {
  1855. /* Make sure that the PPCs appear in the same order as in the parent. */
  1856. copy_vtab_proc_comps (super_type, vtype);
  1857. /* Only needed to get the PPC initializers right. */
  1858. add_procs_to_declared_vtab (super_type, vtype);
  1859. }
  1860. if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
  1861. add_procs_to_declared_vtab1 (derived->f2k_derived->tb_sym_root, vtype);
  1862. if (derived->f2k_derived && derived->f2k_derived->tb_uop_root)
  1863. add_procs_to_declared_vtab1 (derived->f2k_derived->tb_uop_root, vtype);
  1864. }
  1865. /* Find or generate the symbol for a derived type's vtab. */
  1866. gfc_symbol *
  1867. gfc_find_derived_vtab (gfc_symbol *derived)
  1868. {
  1869. gfc_namespace *ns;
  1870. gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
  1871. gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
  1872. /* Find the top-level namespace. */
  1873. for (ns = gfc_current_ns; ns; ns = ns->parent)
  1874. if (!ns->parent)
  1875. break;
  1876. /* If the type is a class container, use the underlying derived type. */
  1877. if (!derived->attr.unlimited_polymorphic && derived->attr.is_class)
  1878. derived = gfc_get_derived_super_type (derived);
  1879. if (ns)
  1880. {
  1881. char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
  1882. get_unique_hashed_string (tname, derived);
  1883. sprintf (name, "__vtab_%s", tname);
  1884. /* Look for the vtab symbol in various namespaces. */
  1885. gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
  1886. if (vtab == NULL)
  1887. gfc_find_symbol (name, ns, 0, &vtab);
  1888. if (vtab == NULL)
  1889. gfc_find_symbol (name, derived->ns, 0, &vtab);
  1890. if (vtab == NULL)
  1891. {
  1892. gfc_get_symbol (name, ns, &vtab);
  1893. vtab->ts.type = BT_DERIVED;
  1894. if (!gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
  1895. &gfc_current_locus))
  1896. goto cleanup;
  1897. vtab->attr.target = 1;
  1898. vtab->attr.save = SAVE_IMPLICIT;
  1899. vtab->attr.vtab = 1;
  1900. vtab->attr.access = ACCESS_PUBLIC;
  1901. gfc_set_sym_referenced (vtab);
  1902. sprintf (name, "__vtype_%s", tname);
  1903. gfc_find_symbol (name, ns, 0, &vtype);
  1904. if (vtype == NULL)
  1905. {
  1906. gfc_component *c;
  1907. gfc_symbol *parent = NULL, *parent_vtab = NULL;
  1908. gfc_get_symbol (name, ns, &vtype);
  1909. if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
  1910. &gfc_current_locus))
  1911. goto cleanup;
  1912. vtype->attr.access = ACCESS_PUBLIC;
  1913. vtype->attr.vtype = 1;
  1914. gfc_set_sym_referenced (vtype);
  1915. /* Add component '_hash'. */
  1916. if (!gfc_add_component (vtype, "_hash", &c))
  1917. goto cleanup;
  1918. c->ts.type = BT_INTEGER;
  1919. c->ts.kind = 4;
  1920. c->attr.access = ACCESS_PRIVATE;
  1921. c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
  1922. NULL, derived->hash_value);
  1923. /* Add component '_size'. */
  1924. if (!gfc_add_component (vtype, "_size", &c))
  1925. goto cleanup;
  1926. c->ts.type = BT_INTEGER;
  1927. c->ts.kind = 4;
  1928. c->attr.access = ACCESS_PRIVATE;
  1929. /* Remember the derived type in ts.u.derived,
  1930. so that the correct initializer can be set later on
  1931. (in gfc_conv_structure). */
  1932. c->ts.u.derived = derived;
  1933. c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
  1934. NULL, 0);
  1935. /* Add component _extends. */
  1936. if (!gfc_add_component (vtype, "_extends", &c))
  1937. goto cleanup;
  1938. c->attr.pointer = 1;
  1939. c->attr.access = ACCESS_PRIVATE;
  1940. if (!derived->attr.unlimited_polymorphic)
  1941. parent = gfc_get_derived_super_type (derived);
  1942. else
  1943. parent = NULL;
  1944. if (parent)
  1945. {
  1946. parent_vtab = gfc_find_derived_vtab (parent);
  1947. c->ts.type = BT_DERIVED;
  1948. c->ts.u.derived = parent_vtab->ts.u.derived;
  1949. c->initializer = gfc_get_expr ();
  1950. c->initializer->expr_type = EXPR_VARIABLE;
  1951. gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
  1952. 0, &c->initializer->symtree);
  1953. }
  1954. else
  1955. {
  1956. c->ts.type = BT_DERIVED;
  1957. c->ts.u.derived = vtype;
  1958. c->initializer = gfc_get_null_expr (NULL);
  1959. }
  1960. if (!derived->attr.unlimited_polymorphic
  1961. && derived->components == NULL
  1962. && !derived->attr.zero_comp)
  1963. {
  1964. /* At this point an error must have occurred.
  1965. Prevent further errors on the vtype components. */
  1966. found_sym = vtab;
  1967. goto have_vtype;
  1968. }
  1969. /* Add component _def_init. */
  1970. if (!gfc_add_component (vtype, "_def_init", &c))
  1971. goto cleanup;
  1972. c->attr.pointer = 1;
  1973. c->attr.artificial = 1;
  1974. c->attr.access = ACCESS_PRIVATE;
  1975. c->ts.type = BT_DERIVED;
  1976. c->ts.u.derived = derived;
  1977. if (derived->attr.unlimited_polymorphic
  1978. || derived->attr.abstract)
  1979. c->initializer = gfc_get_null_expr (NULL);
  1980. else
  1981. {
  1982. /* Construct default initialization variable. */
  1983. sprintf (name, "__def_init_%s", tname);
  1984. gfc_get_symbol (name, ns, &def_init);
  1985. def_init->attr.target = 1;
  1986. def_init->attr.artificial = 1;
  1987. def_init->attr.save = SAVE_IMPLICIT;
  1988. def_init->attr.access = ACCESS_PUBLIC;
  1989. def_init->attr.flavor = FL_VARIABLE;
  1990. gfc_set_sym_referenced (def_init);
  1991. def_init->ts.type = BT_DERIVED;
  1992. def_init->ts.u.derived = derived;
  1993. def_init->value = gfc_default_initializer (&def_init->ts);
  1994. c->initializer = gfc_lval_expr_from_sym (def_init);
  1995. }
  1996. /* Add component _copy. */
  1997. if (!gfc_add_component (vtype, "_copy", &c))
  1998. goto cleanup;
  1999. c->attr.proc_pointer = 1;
  2000. c->attr.access = ACCESS_PRIVATE;
  2001. c->tb = XCNEW (gfc_typebound_proc);
  2002. c->tb->ppc = 1;
  2003. if (derived->attr.unlimited_polymorphic
  2004. || derived->attr.abstract)
  2005. c->initializer = gfc_get_null_expr (NULL);
  2006. else
  2007. {
  2008. /* Set up namespace. */
  2009. gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
  2010. sub_ns->sibling = ns->contained;
  2011. ns->contained = sub_ns;
  2012. sub_ns->resolved = 1;
  2013. /* Set up procedure symbol. */
  2014. sprintf (name, "__copy_%s", tname);
  2015. gfc_get_symbol (name, sub_ns, &copy);
  2016. sub_ns->proc_name = copy;
  2017. copy->attr.flavor = FL_PROCEDURE;
  2018. copy->attr.subroutine = 1;
  2019. copy->attr.pure = 1;
  2020. copy->attr.artificial = 1;
  2021. copy->attr.if_source = IFSRC_DECL;
  2022. /* This is elemental so that arrays are automatically
  2023. treated correctly by the scalarizer. */
  2024. copy->attr.elemental = 1;
  2025. if (ns->proc_name->attr.flavor == FL_MODULE)
  2026. copy->module = ns->proc_name->name;
  2027. gfc_set_sym_referenced (copy);
  2028. /* Set up formal arguments. */
  2029. gfc_get_symbol ("src", sub_ns, &src);
  2030. src->ts.type = BT_DERIVED;
  2031. src->ts.u.derived = derived;
  2032. src->attr.flavor = FL_VARIABLE;
  2033. src->attr.dummy = 1;
  2034. src->attr.artificial = 1;
  2035. src->attr.intent = INTENT_IN;
  2036. gfc_set_sym_referenced (src);
  2037. copy->formal = gfc_get_formal_arglist ();
  2038. copy->formal->sym = src;
  2039. gfc_get_symbol ("dst", sub_ns, &dst);
  2040. dst->ts.type = BT_DERIVED;
  2041. dst->ts.u.derived = derived;
  2042. dst->attr.flavor = FL_VARIABLE;
  2043. dst->attr.dummy = 1;
  2044. dst->attr.artificial = 1;
  2045. dst->attr.intent = INTENT_INOUT;
  2046. gfc_set_sym_referenced (dst);
  2047. copy->formal->next = gfc_get_formal_arglist ();
  2048. copy->formal->next->sym = dst;
  2049. /* Set up code. */
  2050. sub_ns->code = gfc_get_code (EXEC_INIT_ASSIGN);
  2051. sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
  2052. sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
  2053. /* Set initializer. */
  2054. c->initializer = gfc_lval_expr_from_sym (copy);
  2055. c->ts.interface = copy;
  2056. }
  2057. /* Add component _final, which contains a procedure pointer to
  2058. a wrapper which handles both the freeing of allocatable
  2059. components and the calls to finalization subroutines.
  2060. Note: The actual wrapper function can only be generated
  2061. at resolution time. */
  2062. if (!gfc_add_component (vtype, "_final", &c))
  2063. goto cleanup;
  2064. c->attr.proc_pointer = 1;
  2065. c->attr.access = ACCESS_PRIVATE;
  2066. c->tb = XCNEW (gfc_typebound_proc);
  2067. c->tb->ppc = 1;
  2068. generate_finalization_wrapper (derived, ns, tname, c);
  2069. /* Add procedure pointers for type-bound procedures. */
  2070. if (!derived->attr.unlimited_polymorphic)
  2071. add_procs_to_declared_vtab (derived, vtype);
  2072. }
  2073. have_vtype:
  2074. vtab->ts.u.derived = vtype;
  2075. vtab->value = gfc_default_initializer (&vtab->ts);
  2076. }
  2077. }
  2078. found_sym = vtab;
  2079. cleanup:
  2080. /* It is unexpected to have some symbols added at resolution or code
  2081. generation time. We commit the changes in order to keep a clean state. */
  2082. if (found_sym)
  2083. {
  2084. gfc_commit_symbol (vtab);
  2085. if (vtype)
  2086. gfc_commit_symbol (vtype);
  2087. if (def_init)
  2088. gfc_commit_symbol (def_init);
  2089. if (copy)
  2090. gfc_commit_symbol (copy);
  2091. if (src)
  2092. gfc_commit_symbol (src);
  2093. if (dst)
  2094. gfc_commit_symbol (dst);
  2095. }
  2096. else
  2097. gfc_undo_symbols ();
  2098. return found_sym;
  2099. }
  2100. /* Check if a derived type is finalizable. That is the case if it
  2101. (1) has a FINAL subroutine or
  2102. (2) has a nonpointer nonallocatable component of finalizable type.
  2103. If it is finalizable, return an expression containing the
  2104. finalization wrapper. */
  2105. bool
  2106. gfc_is_finalizable (gfc_symbol *derived, gfc_expr **final_expr)
  2107. {
  2108. gfc_symbol *vtab;
  2109. gfc_component *c;
  2110. /* (1) Check for FINAL subroutines. */
  2111. if (derived->f2k_derived && derived->f2k_derived->finalizers)
  2112. goto yes;
  2113. /* (2) Check for components of finalizable type. */
  2114. for (c = derived->components; c; c = c->next)
  2115. if (c->ts.type == BT_DERIVED
  2116. && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
  2117. && gfc_is_finalizable (c->ts.u.derived, NULL))
  2118. goto yes;
  2119. return false;
  2120. yes:
  2121. /* Make sure vtab is generated. */
  2122. vtab = gfc_find_derived_vtab (derived);
  2123. if (final_expr)
  2124. {
  2125. /* Return finalizer expression. */
  2126. gfc_component *final;
  2127. final = vtab->ts.u.derived->components->next->next->next->next->next;
  2128. gcc_assert (strcmp (final->name, "_final") == 0);
  2129. gcc_assert (final->initializer
  2130. && final->initializer->expr_type != EXPR_NULL);
  2131. *final_expr = final->initializer;
  2132. }
  2133. return true;
  2134. }
  2135. /* Find (or generate) the symbol for an intrinsic type's vtab. This is
  2136. needed to support unlimited polymorphism. */
  2137. static gfc_symbol *
  2138. find_intrinsic_vtab (gfc_typespec *ts)
  2139. {
  2140. gfc_namespace *ns;
  2141. gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL;
  2142. gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
  2143. int charlen = 0;
  2144. if (ts->type == BT_CHARACTER && !ts->deferred && ts->u.cl && ts->u.cl->length
  2145. && ts->u.cl->length->expr_type == EXPR_CONSTANT)
  2146. charlen = mpz_get_si (ts->u.cl->length->value.integer);
  2147. /* Find the top-level namespace. */
  2148. for (ns = gfc_current_ns; ns; ns = ns->parent)
  2149. if (!ns->parent)
  2150. break;
  2151. if (ns)
  2152. {
  2153. char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
  2154. if (ts->type == BT_CHARACTER)
  2155. sprintf (tname, "%s_%d_%d", gfc_basic_typename (ts->type),
  2156. charlen, ts->kind);
  2157. else
  2158. sprintf (tname, "%s_%d_", gfc_basic_typename (ts->type), ts->kind);
  2159. sprintf (name, "__vtab_%s", tname);
  2160. /* Look for the vtab symbol in various namespaces. */
  2161. gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
  2162. if (vtab == NULL)
  2163. gfc_find_symbol (name, ns, 0, &vtab);
  2164. if (vtab == NULL)
  2165. {
  2166. gfc_get_symbol (name, ns, &vtab);
  2167. vtab->ts.type = BT_DERIVED;
  2168. if (!gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
  2169. &gfc_current_locus))
  2170. goto cleanup;
  2171. vtab->attr.target = 1;
  2172. vtab->attr.save = SAVE_IMPLICIT;
  2173. vtab->attr.vtab = 1;
  2174. vtab->attr.access = ACCESS_PUBLIC;
  2175. gfc_set_sym_referenced (vtab);
  2176. sprintf (name, "__vtype_%s", tname);
  2177. gfc_find_symbol (name, ns, 0, &vtype);
  2178. if (vtype == NULL)
  2179. {
  2180. gfc_component *c;
  2181. int hash;
  2182. gfc_namespace *sub_ns;
  2183. gfc_namespace *contained;
  2184. gfc_expr *e;
  2185. gfc_get_symbol (name, ns, &vtype);
  2186. if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
  2187. &gfc_current_locus))
  2188. goto cleanup;
  2189. vtype->attr.access = ACCESS_PUBLIC;
  2190. vtype->attr.vtype = 1;
  2191. gfc_set_sym_referenced (vtype);
  2192. /* Add component '_hash'. */
  2193. if (!gfc_add_component (vtype, "_hash", &c))
  2194. goto cleanup;
  2195. c->ts.type = BT_INTEGER;
  2196. c->ts.kind = 4;
  2197. c->attr.access = ACCESS_PRIVATE;
  2198. hash = gfc_intrinsic_hash_value (ts);
  2199. c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
  2200. NULL, hash);
  2201. /* Add component '_size'. */
  2202. if (!gfc_add_component (vtype, "_size", &c))
  2203. goto cleanup;
  2204. c->ts.type = BT_INTEGER;
  2205. c->ts.kind = 4;
  2206. c->attr.access = ACCESS_PRIVATE;
  2207. /* Build a minimal expression to make use of
  2208. target-memory.c/gfc_element_size for 'size'. Special handling
  2209. for character arrays, that are not constant sized: to support
  2210. len (str) * kind, only the kind information is stored in the
  2211. vtab. */
  2212. e = gfc_get_expr ();
  2213. e->ts = *ts;
  2214. e->expr_type = EXPR_VARIABLE;
  2215. c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
  2216. NULL,
  2217. ts->type == BT_CHARACTER
  2218. && charlen == 0 ?
  2219. ts->kind :
  2220. (int)gfc_element_size (e));
  2221. gfc_free_expr (e);
  2222. /* Add component _extends. */
  2223. if (!gfc_add_component (vtype, "_extends", &c))
  2224. goto cleanup;
  2225. c->attr.pointer = 1;
  2226. c->attr.access = ACCESS_PRIVATE;
  2227. c->ts.type = BT_VOID;
  2228. c->initializer = gfc_get_null_expr (NULL);
  2229. /* Add component _def_init. */
  2230. if (!gfc_add_component (vtype, "_def_init", &c))
  2231. goto cleanup;
  2232. c->attr.pointer = 1;
  2233. c->attr.access = ACCESS_PRIVATE;
  2234. c->ts.type = BT_VOID;
  2235. c->initializer = gfc_get_null_expr (NULL);
  2236. /* Add component _copy. */
  2237. if (!gfc_add_component (vtype, "_copy", &c))
  2238. goto cleanup;
  2239. c->attr.proc_pointer = 1;
  2240. c->attr.access = ACCESS_PRIVATE;
  2241. c->tb = XCNEW (gfc_typebound_proc);
  2242. c->tb->ppc = 1;
  2243. if (ts->type != BT_CHARACTER)
  2244. sprintf (name, "__copy_%s", tname);
  2245. else
  2246. {
  2247. /* __copy is always the same for characters.
  2248. Check to see if copy function already exists. */
  2249. sprintf (name, "__copy_character_%d", ts->kind);
  2250. contained = ns->contained;
  2251. for (; contained; contained = contained->sibling)
  2252. if (contained->proc_name
  2253. && strcmp (name, contained->proc_name->name) == 0)
  2254. {
  2255. copy = contained->proc_name;
  2256. goto got_char_copy;
  2257. }
  2258. }
  2259. /* Set up namespace. */
  2260. sub_ns = gfc_get_namespace (ns, 0);
  2261. sub_ns->sibling = ns->contained;
  2262. ns->contained = sub_ns;
  2263. sub_ns->resolved = 1;
  2264. /* Set up procedure symbol. */
  2265. gfc_get_symbol (name, sub_ns, &copy);
  2266. sub_ns->proc_name = copy;
  2267. copy->attr.flavor = FL_PROCEDURE;
  2268. copy->attr.subroutine = 1;
  2269. copy->attr.pure = 1;
  2270. copy->attr.if_source = IFSRC_DECL;
  2271. /* This is elemental so that arrays are automatically
  2272. treated correctly by the scalarizer. */
  2273. copy->attr.elemental = 1;
  2274. if (ns->proc_name->attr.flavor == FL_MODULE)
  2275. copy->module = ns->proc_name->name;
  2276. gfc_set_sym_referenced (copy);
  2277. /* Set up formal arguments. */
  2278. gfc_get_symbol ("src", sub_ns, &src);
  2279. src->ts.type = ts->type;
  2280. src->ts.kind = ts->kind;
  2281. src->attr.flavor = FL_VARIABLE;
  2282. src->attr.dummy = 1;
  2283. src->attr.intent = INTENT_IN;
  2284. gfc_set_sym_referenced (src);
  2285. copy->formal = gfc_get_formal_arglist ();
  2286. copy->formal->sym = src;
  2287. gfc_get_symbol ("dst", sub_ns, &dst);
  2288. dst->ts.type = ts->type;
  2289. dst->ts.kind = ts->kind;
  2290. dst->attr.flavor = FL_VARIABLE;
  2291. dst->attr.dummy = 1;
  2292. dst->attr.intent = INTENT_INOUT;
  2293. gfc_set_sym_referenced (dst);
  2294. copy->formal->next = gfc_get_formal_arglist ();
  2295. copy->formal->next->sym = dst;
  2296. /* Set up code. */
  2297. sub_ns->code = gfc_get_code (EXEC_INIT_ASSIGN);
  2298. sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
  2299. sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
  2300. got_char_copy:
  2301. /* Set initializer. */
  2302. c->initializer = gfc_lval_expr_from_sym (copy);
  2303. c->ts.interface = copy;
  2304. /* Add component _final. */
  2305. if (!gfc_add_component (vtype, "_final", &c))
  2306. goto cleanup;
  2307. c->attr.proc_pointer = 1;
  2308. c->attr.access = ACCESS_PRIVATE;
  2309. c->tb = XCNEW (gfc_typebound_proc);
  2310. c->tb->ppc = 1;
  2311. c->initializer = gfc_get_null_expr (NULL);
  2312. }
  2313. vtab->ts.u.derived = vtype;
  2314. vtab->value = gfc_default_initializer (&vtab->ts);
  2315. }
  2316. }
  2317. found_sym = vtab;
  2318. cleanup:
  2319. /* It is unexpected to have some symbols added at resolution or code
  2320. generation time. We commit the changes in order to keep a clean state. */
  2321. if (found_sym)
  2322. {
  2323. gfc_commit_symbol (vtab);
  2324. if (vtype)
  2325. gfc_commit_symbol (vtype);
  2326. if (copy)
  2327. gfc_commit_symbol (copy);
  2328. if (src)
  2329. gfc_commit_symbol (src);
  2330. if (dst)
  2331. gfc_commit_symbol (dst);
  2332. }
  2333. else
  2334. gfc_undo_symbols ();
  2335. return found_sym;
  2336. }
  2337. /* Find (or generate) a vtab for an arbitrary type (derived or intrinsic). */
  2338. gfc_symbol *
  2339. gfc_find_vtab (gfc_typespec *ts)
  2340. {
  2341. switch (ts->type)
  2342. {
  2343. case BT_UNKNOWN:
  2344. return NULL;
  2345. case BT_DERIVED:
  2346. return gfc_find_derived_vtab (ts->u.derived);
  2347. case BT_CLASS:
  2348. return gfc_find_derived_vtab (ts->u.derived->components->ts.u.derived);
  2349. default:
  2350. return find_intrinsic_vtab (ts);
  2351. }
  2352. }
  2353. /* General worker function to find either a type-bound procedure or a
  2354. type-bound user operator. */
  2355. static gfc_symtree*
  2356. find_typebound_proc_uop (gfc_symbol* derived, bool* t,
  2357. const char* name, bool noaccess, bool uop,
  2358. locus* where)
  2359. {
  2360. gfc_symtree* res;
  2361. gfc_symtree* root;
  2362. /* Set default to failure. */
  2363. if (t)
  2364. *t = false;
  2365. if (derived->f2k_derived)
  2366. /* Set correct symbol-root. */
  2367. root = (uop ? derived->f2k_derived->tb_uop_root
  2368. : derived->f2k_derived->tb_sym_root);
  2369. else
  2370. return NULL;
  2371. /* Try to find it in the current type's namespace. */
  2372. res = gfc_find_symtree (root, name);
  2373. if (res && res->n.tb && !res->n.tb->error)
  2374. {
  2375. /* We found one. */
  2376. if (t)
  2377. *t = true;
  2378. if (!noaccess && derived->attr.use_assoc
  2379. && res->n.tb->access == ACCESS_PRIVATE)
  2380. {
  2381. if (where)
  2382. gfc_error ("%qs of %qs is PRIVATE at %L",
  2383. name, derived->name, where);
  2384. if (t)
  2385. *t = false;
  2386. }
  2387. return res;
  2388. }
  2389. /* Otherwise, recurse on parent type if derived is an extension. */
  2390. if (derived->attr.extension)
  2391. {
  2392. gfc_symbol* super_type;
  2393. super_type = gfc_get_derived_super_type (derived);
  2394. gcc_assert (super_type);
  2395. return find_typebound_proc_uop (super_type, t, name,
  2396. noaccess, uop, where);
  2397. }
  2398. /* Nothing found. */
  2399. return NULL;
  2400. }
  2401. /* Find a type-bound procedure or user operator by name for a derived-type
  2402. (looking recursively through the super-types). */
  2403. gfc_symtree*
  2404. gfc_find_typebound_proc (gfc_symbol* derived, bool* t,
  2405. const char* name, bool noaccess, locus* where)
  2406. {
  2407. return find_typebound_proc_uop (derived, t, name, noaccess, false, where);
  2408. }
  2409. gfc_symtree*
  2410. gfc_find_typebound_user_op (gfc_symbol* derived, bool* t,
  2411. const char* name, bool noaccess, locus* where)
  2412. {
  2413. return find_typebound_proc_uop (derived, t, name, noaccess, true, where);
  2414. }
  2415. /* Find a type-bound intrinsic operator looking recursively through the
  2416. super-type hierarchy. */
  2417. gfc_typebound_proc*
  2418. gfc_find_typebound_intrinsic_op (gfc_symbol* derived, bool* t,
  2419. gfc_intrinsic_op op, bool noaccess,
  2420. locus* where)
  2421. {
  2422. gfc_typebound_proc* res;
  2423. /* Set default to failure. */
  2424. if (t)
  2425. *t = false;
  2426. /* Try to find it in the current type's namespace. */
  2427. if (derived->f2k_derived)
  2428. res = derived->f2k_derived->tb_op[op];
  2429. else
  2430. res = NULL;
  2431. /* Check access. */
  2432. if (res && !res->error)
  2433. {
  2434. /* We found one. */
  2435. if (t)
  2436. *t = true;
  2437. if (!noaccess && derived->attr.use_assoc
  2438. && res->access == ACCESS_PRIVATE)
  2439. {
  2440. if (where)
  2441. gfc_error ("%qs of %qs is PRIVATE at %L",
  2442. gfc_op2string (op), derived->name, where);
  2443. if (t)
  2444. *t = false;
  2445. }
  2446. return res;
  2447. }
  2448. /* Otherwise, recurse on parent type if derived is an extension. */
  2449. if (derived->attr.extension)
  2450. {
  2451. gfc_symbol* super_type;
  2452. super_type = gfc_get_derived_super_type (derived);
  2453. gcc_assert (super_type);
  2454. return gfc_find_typebound_intrinsic_op (super_type, t, op,
  2455. noaccess, where);
  2456. }
  2457. /* Nothing found. */
  2458. return NULL;
  2459. }
  2460. /* Get a typebound-procedure symtree or create and insert it if not yet
  2461. present. This is like a very simplified version of gfc_get_sym_tree for
  2462. tbp-symtrees rather than regular ones. */
  2463. gfc_symtree*
  2464. gfc_get_tbp_symtree (gfc_symtree **root, const char *name)
  2465. {
  2466. gfc_symtree *result;
  2467. result = gfc_find_symtree (*root, name);
  2468. if (!result)
  2469. {
  2470. result = gfc_new_symtree (root, name);
  2471. gcc_assert (result);
  2472. result->n.tb = NULL;
  2473. }
  2474. return result;
  2475. }