trans-types.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252
  1. /* Backend support for Fortran 95 basic types and derived types.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. and Steven Bosscher <s.bosscher@student.tudelft.nl>
  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. /* trans-types.c -- gfortran backend types */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h" /* For INTMAX_TYPE, INT8_TYPE, INT16_TYPE, INT32_TYPE,
  22. INT64_TYPE, INT_LEAST8_TYPE, INT_LEAST16_TYPE,
  23. INT_LEAST32_TYPE, INT_LEAST64_TYPE, INT_FAST8_TYPE,
  24. INT_FAST16_TYPE, INT_FAST32_TYPE, INT_FAST64_TYPE,
  25. BOOL_TYPE_SIZE, BITS_PER_UNIT, POINTER_SIZE,
  26. INT_TYPE_SIZE, CHAR_TYPE_SIZE, SHORT_TYPE_SIZE,
  27. LONG_TYPE_SIZE, LONG_LONG_TYPE_SIZE,
  28. FLOAT_TYPE_SIZE, DOUBLE_TYPE_SIZE and
  29. LONG_DOUBLE_TYPE_SIZE. */
  30. #include "hash-set.h"
  31. #include "machmode.h"
  32. #include "vec.h"
  33. #include "double-int.h"
  34. #include "input.h"
  35. #include "alias.h"
  36. #include "symtab.h"
  37. #include "wide-int.h"
  38. #include "inchash.h"
  39. #include "real.h"
  40. #include "tree.h"
  41. #include "fold-const.h"
  42. #include "stor-layout.h"
  43. #include "stringpool.h"
  44. #include "langhooks.h" /* For iso-c-bindings.def. */
  45. #include "target.h"
  46. #include "ggc.h"
  47. #include "gfortran.h"
  48. #include "diagnostic-core.h" /* For fatal_error. */
  49. #include "toplev.h" /* For rest_of_decl_compilation. */
  50. #include "trans.h"
  51. #include "trans-types.h"
  52. #include "trans-const.h"
  53. #include "flags.h"
  54. #include "dwarf2out.h" /* For struct array_descr_info. */
  55. #if (GFC_MAX_DIMENSIONS < 10)
  56. #define GFC_RANK_DIGITS 1
  57. #define GFC_RANK_PRINTF_FORMAT "%01d"
  58. #elif (GFC_MAX_DIMENSIONS < 100)
  59. #define GFC_RANK_DIGITS 2
  60. #define GFC_RANK_PRINTF_FORMAT "%02d"
  61. #else
  62. #error If you really need >99 dimensions, continue the sequence above...
  63. #endif
  64. /* array of structs so we don't have to worry about xmalloc or free */
  65. CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
  66. tree gfc_array_index_type;
  67. tree gfc_array_range_type;
  68. tree gfc_character1_type_node;
  69. tree pvoid_type_node;
  70. tree prvoid_type_node;
  71. tree ppvoid_type_node;
  72. tree pchar_type_node;
  73. tree pfunc_type_node;
  74. tree gfc_charlen_type_node;
  75. tree float128_type_node = NULL_TREE;
  76. tree complex_float128_type_node = NULL_TREE;
  77. bool gfc_real16_is_float128 = false;
  78. static GTY(()) tree gfc_desc_dim_type;
  79. static GTY(()) tree gfc_max_array_element_size;
  80. static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
  81. static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
  82. /* Arrays for all integral and real kinds. We'll fill this in at runtime
  83. after the target has a chance to process command-line options. */
  84. #define MAX_INT_KINDS 5
  85. gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
  86. gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
  87. static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
  88. static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
  89. #define MAX_REAL_KINDS 5
  90. gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
  91. static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
  92. static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
  93. #define MAX_CHARACTER_KINDS 2
  94. gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
  95. static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
  96. static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
  97. static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
  98. /* The integer kind to use for array indices. This will be set to the
  99. proper value based on target information from the backend. */
  100. int gfc_index_integer_kind;
  101. /* The default kinds of the various types. */
  102. int gfc_default_integer_kind;
  103. int gfc_max_integer_kind;
  104. int gfc_default_real_kind;
  105. int gfc_default_double_kind;
  106. int gfc_default_character_kind;
  107. int gfc_default_logical_kind;
  108. int gfc_default_complex_kind;
  109. int gfc_c_int_kind;
  110. int gfc_atomic_int_kind;
  111. int gfc_atomic_logical_kind;
  112. /* The kind size used for record offsets. If the target system supports
  113. kind=8, this will be set to 8, otherwise it is set to 4. */
  114. int gfc_intio_kind;
  115. /* The integer kind used to store character lengths. */
  116. int gfc_charlen_int_kind;
  117. /* The size of the numeric storage unit and character storage unit. */
  118. int gfc_numeric_storage_size;
  119. int gfc_character_storage_size;
  120. bool
  121. gfc_check_any_c_kind (gfc_typespec *ts)
  122. {
  123. int i;
  124. for (i = 0; i < ISOCBINDING_NUMBER; i++)
  125. {
  126. /* Check for any C interoperable kind for the given type/kind in ts.
  127. This can be used after verify_c_interop to make sure that the
  128. Fortran kind being used exists in at least some form for C. */
  129. if (c_interop_kinds_table[i].f90_type == ts->type &&
  130. c_interop_kinds_table[i].value == ts->kind)
  131. return true;
  132. }
  133. return false;
  134. }
  135. static int
  136. get_real_kind_from_node (tree type)
  137. {
  138. int i;
  139. for (i = 0; gfc_real_kinds[i].kind != 0; i++)
  140. if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
  141. return gfc_real_kinds[i].kind;
  142. return -4;
  143. }
  144. static int
  145. get_int_kind_from_node (tree type)
  146. {
  147. int i;
  148. if (!type)
  149. return -2;
  150. for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
  151. if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
  152. return gfc_integer_kinds[i].kind;
  153. return -1;
  154. }
  155. /* Return a typenode for the "standard" C type with a given name. */
  156. static tree
  157. get_typenode_from_name (const char *name)
  158. {
  159. if (name == NULL || *name == '\0')
  160. return NULL_TREE;
  161. if (strcmp (name, "char") == 0)
  162. return char_type_node;
  163. if (strcmp (name, "unsigned char") == 0)
  164. return unsigned_char_type_node;
  165. if (strcmp (name, "signed char") == 0)
  166. return signed_char_type_node;
  167. if (strcmp (name, "short int") == 0)
  168. return short_integer_type_node;
  169. if (strcmp (name, "short unsigned int") == 0)
  170. return short_unsigned_type_node;
  171. if (strcmp (name, "int") == 0)
  172. return integer_type_node;
  173. if (strcmp (name, "unsigned int") == 0)
  174. return unsigned_type_node;
  175. if (strcmp (name, "long int") == 0)
  176. return long_integer_type_node;
  177. if (strcmp (name, "long unsigned int") == 0)
  178. return long_unsigned_type_node;
  179. if (strcmp (name, "long long int") == 0)
  180. return long_long_integer_type_node;
  181. if (strcmp (name, "long long unsigned int") == 0)
  182. return long_long_unsigned_type_node;
  183. gcc_unreachable ();
  184. }
  185. static int
  186. get_int_kind_from_name (const char *name)
  187. {
  188. return get_int_kind_from_node (get_typenode_from_name (name));
  189. }
  190. /* Get the kind number corresponding to an integer of given size,
  191. following the required return values for ISO_FORTRAN_ENV INT* constants:
  192. -2 is returned if we support a kind of larger size, -1 otherwise. */
  193. int
  194. gfc_get_int_kind_from_width_isofortranenv (int size)
  195. {
  196. int i;
  197. /* Look for a kind with matching storage size. */
  198. for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
  199. if (gfc_integer_kinds[i].bit_size == size)
  200. return gfc_integer_kinds[i].kind;
  201. /* Look for a kind with larger storage size. */
  202. for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
  203. if (gfc_integer_kinds[i].bit_size > size)
  204. return -2;
  205. return -1;
  206. }
  207. /* Get the kind number corresponding to a real of given storage size,
  208. following the required return values for ISO_FORTRAN_ENV REAL* constants:
  209. -2 is returned if we support a kind of larger size, -1 otherwise. */
  210. int
  211. gfc_get_real_kind_from_width_isofortranenv (int size)
  212. {
  213. int i;
  214. size /= 8;
  215. /* Look for a kind with matching storage size. */
  216. for (i = 0; gfc_real_kinds[i].kind != 0; i++)
  217. if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
  218. return gfc_real_kinds[i].kind;
  219. /* Look for a kind with larger storage size. */
  220. for (i = 0; gfc_real_kinds[i].kind != 0; i++)
  221. if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
  222. return -2;
  223. return -1;
  224. }
  225. static int
  226. get_int_kind_from_width (int size)
  227. {
  228. int i;
  229. for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
  230. if (gfc_integer_kinds[i].bit_size == size)
  231. return gfc_integer_kinds[i].kind;
  232. return -2;
  233. }
  234. static int
  235. get_int_kind_from_minimal_width (int size)
  236. {
  237. int i;
  238. for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
  239. if (gfc_integer_kinds[i].bit_size >= size)
  240. return gfc_integer_kinds[i].kind;
  241. return -2;
  242. }
  243. /* Generate the CInteropKind_t objects for the C interoperable
  244. kinds. */
  245. void
  246. gfc_init_c_interop_kinds (void)
  247. {
  248. int i;
  249. /* init all pointers in the list to NULL */
  250. for (i = 0; i < ISOCBINDING_NUMBER; i++)
  251. {
  252. /* Initialize the name and value fields. */
  253. c_interop_kinds_table[i].name[0] = '\0';
  254. c_interop_kinds_table[i].value = -100;
  255. c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
  256. }
  257. #define NAMED_INTCST(a,b,c,d) \
  258. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  259. c_interop_kinds_table[a].f90_type = BT_INTEGER; \
  260. c_interop_kinds_table[a].value = c;
  261. #define NAMED_REALCST(a,b,c,d) \
  262. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  263. c_interop_kinds_table[a].f90_type = BT_REAL; \
  264. c_interop_kinds_table[a].value = c;
  265. #define NAMED_CMPXCST(a,b,c,d) \
  266. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  267. c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
  268. c_interop_kinds_table[a].value = c;
  269. #define NAMED_LOGCST(a,b,c) \
  270. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  271. c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
  272. c_interop_kinds_table[a].value = c;
  273. #define NAMED_CHARKNDCST(a,b,c) \
  274. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  275. c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
  276. c_interop_kinds_table[a].value = c;
  277. #define NAMED_CHARCST(a,b,c) \
  278. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  279. c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
  280. c_interop_kinds_table[a].value = c;
  281. #define DERIVED_TYPE(a,b,c) \
  282. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  283. c_interop_kinds_table[a].f90_type = BT_DERIVED; \
  284. c_interop_kinds_table[a].value = c;
  285. #define NAMED_FUNCTION(a,b,c,d) \
  286. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  287. c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
  288. c_interop_kinds_table[a].value = c;
  289. #define NAMED_SUBROUTINE(a,b,c,d) \
  290. strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
  291. c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
  292. c_interop_kinds_table[a].value = c;
  293. #include "iso-c-binding.def"
  294. }
  295. /* Query the target to determine which machine modes are available for
  296. computation. Choose KIND numbers for them. */
  297. void
  298. gfc_init_kinds (void)
  299. {
  300. unsigned int mode;
  301. int i_index, r_index, kind;
  302. bool saw_i4 = false, saw_i8 = false;
  303. bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
  304. for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
  305. {
  306. int kind, bitsize;
  307. if (!targetm.scalar_mode_supported_p ((machine_mode) mode))
  308. continue;
  309. /* The middle end doesn't support constants larger than 2*HWI.
  310. Perhaps the target hook shouldn't have accepted these either,
  311. but just to be safe... */
  312. bitsize = GET_MODE_BITSIZE ((machine_mode) mode);
  313. if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
  314. continue;
  315. gcc_assert (i_index != MAX_INT_KINDS);
  316. /* Let the kind equal the bit size divided by 8. This insulates the
  317. programmer from the underlying byte size. */
  318. kind = bitsize / 8;
  319. if (kind == 4)
  320. saw_i4 = true;
  321. if (kind == 8)
  322. saw_i8 = true;
  323. gfc_integer_kinds[i_index].kind = kind;
  324. gfc_integer_kinds[i_index].radix = 2;
  325. gfc_integer_kinds[i_index].digits = bitsize - 1;
  326. gfc_integer_kinds[i_index].bit_size = bitsize;
  327. gfc_logical_kinds[i_index].kind = kind;
  328. gfc_logical_kinds[i_index].bit_size = bitsize;
  329. i_index += 1;
  330. }
  331. /* Set the kind used to match GFC_INT_IO in libgfortran. This is
  332. used for large file access. */
  333. if (saw_i8)
  334. gfc_intio_kind = 8;
  335. else
  336. gfc_intio_kind = 4;
  337. /* If we do not at least have kind = 4, everything is pointless. */
  338. gcc_assert(saw_i4);
  339. /* Set the maximum integer kind. Used with at least BOZ constants. */
  340. gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
  341. for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
  342. {
  343. const struct real_format *fmt =
  344. REAL_MODE_FORMAT ((machine_mode) mode);
  345. int kind;
  346. if (fmt == NULL)
  347. continue;
  348. if (!targetm.scalar_mode_supported_p ((machine_mode) mode))
  349. continue;
  350. /* Only let float, double, long double and __float128 go through.
  351. Runtime support for others is not provided, so they would be
  352. useless. */
  353. if (!targetm.libgcc_floating_mode_supported_p ((machine_mode)
  354. mode))
  355. continue;
  356. if (mode != TYPE_MODE (float_type_node)
  357. && (mode != TYPE_MODE (double_type_node))
  358. && (mode != TYPE_MODE (long_double_type_node))
  359. #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
  360. && (mode != TFmode)
  361. #endif
  362. )
  363. continue;
  364. /* Let the kind equal the precision divided by 8, rounding up. Again,
  365. this insulates the programmer from the underlying byte size.
  366. Also, it effectively deals with IEEE extended formats. There, the
  367. total size of the type may equal 16, but it's got 6 bytes of padding
  368. and the increased size can get in the way of a real IEEE quad format
  369. which may also be supported by the target.
  370. We round up so as to handle IA-64 __floatreg (RFmode), which is an
  371. 82 bit type. Not to be confused with __float80 (XFmode), which is
  372. an 80 bit type also supported by IA-64. So XFmode should come out
  373. to be kind=10, and RFmode should come out to be kind=11. Egads. */
  374. kind = (GET_MODE_PRECISION (mode) + 7) / 8;
  375. if (kind == 4)
  376. saw_r4 = true;
  377. if (kind == 8)
  378. saw_r8 = true;
  379. if (kind == 10)
  380. saw_r10 = true;
  381. if (kind == 16)
  382. saw_r16 = true;
  383. /* Careful we don't stumble a weird internal mode. */
  384. gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
  385. /* Or have too many modes for the allocated space. */
  386. gcc_assert (r_index != MAX_REAL_KINDS);
  387. gfc_real_kinds[r_index].kind = kind;
  388. gfc_real_kinds[r_index].radix = fmt->b;
  389. gfc_real_kinds[r_index].digits = fmt->p;
  390. gfc_real_kinds[r_index].min_exponent = fmt->emin;
  391. gfc_real_kinds[r_index].max_exponent = fmt->emax;
  392. if (fmt->pnan < fmt->p)
  393. /* This is an IBM extended double format (or the MIPS variant)
  394. made up of two IEEE doubles. The value of the long double is
  395. the sum of the values of the two parts. The most significant
  396. part is required to be the value of the long double rounded
  397. to the nearest double. If we use emax of 1024 then we can't
  398. represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
  399. rounding will make the most significant part overflow. */
  400. gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
  401. gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
  402. r_index += 1;
  403. }
  404. /* Choose the default integer kind. We choose 4 unless the user directs us
  405. otherwise. Even if the user specified that the default integer kind is 8,
  406. the numeric storage size is not 64 bits. In this case, a warning will be
  407. issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
  408. gfc_numeric_storage_size = 4 * 8;
  409. if (flag_default_integer)
  410. {
  411. if (!saw_i8)
  412. gfc_fatal_error ("INTEGER(KIND=8) is not available for "
  413. "%<-fdefault-integer-8%> option");
  414. gfc_default_integer_kind = 8;
  415. }
  416. else if (flag_integer4_kind == 8)
  417. {
  418. if (!saw_i8)
  419. gfc_fatal_error ("INTEGER(KIND=8) is not available for "
  420. "%<-finteger-4-integer-8%> option");
  421. gfc_default_integer_kind = 8;
  422. }
  423. else if (saw_i4)
  424. {
  425. gfc_default_integer_kind = 4;
  426. }
  427. else
  428. {
  429. gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
  430. gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
  431. }
  432. /* Choose the default real kind. Again, we choose 4 when possible. */
  433. if (flag_default_real)
  434. {
  435. if (!saw_r8)
  436. gfc_fatal_error ("REAL(KIND=8) is not available for "
  437. "%<-fdefault-real-8%> option");
  438. gfc_default_real_kind = 8;
  439. }
  440. else if (flag_real4_kind == 8)
  441. {
  442. if (!saw_r8)
  443. gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
  444. "option");
  445. gfc_default_real_kind = 8;
  446. }
  447. else if (flag_real4_kind == 10)
  448. {
  449. if (!saw_r10)
  450. gfc_fatal_error ("REAL(KIND=10) is not available for "
  451. "%<-freal-4-real-10%> option");
  452. gfc_default_real_kind = 10;
  453. }
  454. else if (flag_real4_kind == 16)
  455. {
  456. if (!saw_r16)
  457. gfc_fatal_error ("REAL(KIND=16) is not available for "
  458. "%<-freal-4-real-16%> option");
  459. gfc_default_real_kind = 16;
  460. }
  461. else if (saw_r4)
  462. gfc_default_real_kind = 4;
  463. else
  464. gfc_default_real_kind = gfc_real_kinds[0].kind;
  465. /* Choose the default double kind. If -fdefault-real and -fdefault-double
  466. are specified, we use kind=8, if it's available. If -fdefault-real is
  467. specified without -fdefault-double, we use kind=16, if it's available.
  468. Otherwise we do not change anything. */
  469. if (flag_default_double && !flag_default_real)
  470. gfc_fatal_error ("Use of %<-fdefault-double-8%> requires "
  471. "%<-fdefault-real-8%>");
  472. if (flag_default_real && flag_default_double && saw_r8)
  473. gfc_default_double_kind = 8;
  474. else if (flag_default_real && saw_r16)
  475. gfc_default_double_kind = 16;
  476. else if (flag_real8_kind == 4)
  477. {
  478. if (!saw_r4)
  479. gfc_fatal_error ("REAL(KIND=4) is not available for "
  480. "%<-freal-8-real-4%> option");
  481. gfc_default_double_kind = 4;
  482. }
  483. else if (flag_real8_kind == 10 )
  484. {
  485. if (!saw_r10)
  486. gfc_fatal_error ("REAL(KIND=10) is not available for "
  487. "%<-freal-8-real-10%> option");
  488. gfc_default_double_kind = 10;
  489. }
  490. else if (flag_real8_kind == 16 )
  491. {
  492. if (!saw_r16)
  493. gfc_fatal_error ("REAL(KIND=10) is not available for "
  494. "%<-freal-8-real-16%> option");
  495. gfc_default_double_kind = 16;
  496. }
  497. else if (saw_r4 && saw_r8)
  498. gfc_default_double_kind = 8;
  499. else
  500. {
  501. /* F95 14.6.3.1: A nonpointer scalar object of type double precision
  502. real ... occupies two contiguous numeric storage units.
  503. Therefore we must be supplied a kind twice as large as we chose
  504. for single precision. There are loopholes, in that double
  505. precision must *occupy* two storage units, though it doesn't have
  506. to *use* two storage units. Which means that you can make this
  507. kind artificially wide by padding it. But at present there are
  508. no GCC targets for which a two-word type does not exist, so we
  509. just let gfc_validate_kind abort and tell us if something breaks. */
  510. gfc_default_double_kind
  511. = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
  512. }
  513. /* The default logical kind is constrained to be the same as the
  514. default integer kind. Similarly with complex and real. */
  515. gfc_default_logical_kind = gfc_default_integer_kind;
  516. gfc_default_complex_kind = gfc_default_real_kind;
  517. /* We only have two character kinds: ASCII and UCS-4.
  518. ASCII corresponds to a 8-bit integer type, if one is available.
  519. UCS-4 corresponds to a 32-bit integer type, if one is available. */
  520. i_index = 0;
  521. if ((kind = get_int_kind_from_width (8)) > 0)
  522. {
  523. gfc_character_kinds[i_index].kind = kind;
  524. gfc_character_kinds[i_index].bit_size = 8;
  525. gfc_character_kinds[i_index].name = "ascii";
  526. i_index++;
  527. }
  528. if ((kind = get_int_kind_from_width (32)) > 0)
  529. {
  530. gfc_character_kinds[i_index].kind = kind;
  531. gfc_character_kinds[i_index].bit_size = 32;
  532. gfc_character_kinds[i_index].name = "iso_10646";
  533. i_index++;
  534. }
  535. /* Choose the smallest integer kind for our default character. */
  536. gfc_default_character_kind = gfc_character_kinds[0].kind;
  537. gfc_character_storage_size = gfc_default_character_kind * 8;
  538. gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
  539. /* Pick a kind the same size as the C "int" type. */
  540. gfc_c_int_kind = INT_TYPE_SIZE / 8;
  541. /* Choose atomic kinds to match C's int. */
  542. gfc_atomic_int_kind = gfc_c_int_kind;
  543. gfc_atomic_logical_kind = gfc_c_int_kind;
  544. }
  545. /* Make sure that a valid kind is present. Returns an index into the
  546. associated kinds array, -1 if the kind is not present. */
  547. static int
  548. validate_integer (int kind)
  549. {
  550. int i;
  551. for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
  552. if (gfc_integer_kinds[i].kind == kind)
  553. return i;
  554. return -1;
  555. }
  556. static int
  557. validate_real (int kind)
  558. {
  559. int i;
  560. for (i = 0; gfc_real_kinds[i].kind != 0; i++)
  561. if (gfc_real_kinds[i].kind == kind)
  562. return i;
  563. return -1;
  564. }
  565. static int
  566. validate_logical (int kind)
  567. {
  568. int i;
  569. for (i = 0; gfc_logical_kinds[i].kind; i++)
  570. if (gfc_logical_kinds[i].kind == kind)
  571. return i;
  572. return -1;
  573. }
  574. static int
  575. validate_character (int kind)
  576. {
  577. int i;
  578. for (i = 0; gfc_character_kinds[i].kind; i++)
  579. if (gfc_character_kinds[i].kind == kind)
  580. return i;
  581. return -1;
  582. }
  583. /* Validate a kind given a basic type. The return value is the same
  584. for the child functions, with -1 indicating nonexistence of the
  585. type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
  586. int
  587. gfc_validate_kind (bt type, int kind, bool may_fail)
  588. {
  589. int rc;
  590. switch (type)
  591. {
  592. case BT_REAL: /* Fall through */
  593. case BT_COMPLEX:
  594. rc = validate_real (kind);
  595. break;
  596. case BT_INTEGER:
  597. rc = validate_integer (kind);
  598. break;
  599. case BT_LOGICAL:
  600. rc = validate_logical (kind);
  601. break;
  602. case BT_CHARACTER:
  603. rc = validate_character (kind);
  604. break;
  605. default:
  606. gfc_internal_error ("gfc_validate_kind(): Got bad type");
  607. }
  608. if (rc < 0 && !may_fail)
  609. gfc_internal_error ("gfc_validate_kind(): Got bad kind");
  610. return rc;
  611. }
  612. /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
  613. Reuse common type nodes where possible. Recognize if the kind matches up
  614. with a C type. This will be used later in determining which routines may
  615. be scarfed from libm. */
  616. static tree
  617. gfc_build_int_type (gfc_integer_info *info)
  618. {
  619. int mode_precision = info->bit_size;
  620. if (mode_precision == CHAR_TYPE_SIZE)
  621. info->c_char = 1;
  622. if (mode_precision == SHORT_TYPE_SIZE)
  623. info->c_short = 1;
  624. if (mode_precision == INT_TYPE_SIZE)
  625. info->c_int = 1;
  626. if (mode_precision == LONG_TYPE_SIZE)
  627. info->c_long = 1;
  628. if (mode_precision == LONG_LONG_TYPE_SIZE)
  629. info->c_long_long = 1;
  630. if (TYPE_PRECISION (intQI_type_node) == mode_precision)
  631. return intQI_type_node;
  632. if (TYPE_PRECISION (intHI_type_node) == mode_precision)
  633. return intHI_type_node;
  634. if (TYPE_PRECISION (intSI_type_node) == mode_precision)
  635. return intSI_type_node;
  636. if (TYPE_PRECISION (intDI_type_node) == mode_precision)
  637. return intDI_type_node;
  638. if (TYPE_PRECISION (intTI_type_node) == mode_precision)
  639. return intTI_type_node;
  640. return make_signed_type (mode_precision);
  641. }
  642. tree
  643. gfc_build_uint_type (int size)
  644. {
  645. if (size == CHAR_TYPE_SIZE)
  646. return unsigned_char_type_node;
  647. if (size == SHORT_TYPE_SIZE)
  648. return short_unsigned_type_node;
  649. if (size == INT_TYPE_SIZE)
  650. return unsigned_type_node;
  651. if (size == LONG_TYPE_SIZE)
  652. return long_unsigned_type_node;
  653. if (size == LONG_LONG_TYPE_SIZE)
  654. return long_long_unsigned_type_node;
  655. return make_unsigned_type (size);
  656. }
  657. static tree
  658. gfc_build_real_type (gfc_real_info *info)
  659. {
  660. int mode_precision = info->mode_precision;
  661. tree new_type;
  662. if (mode_precision == FLOAT_TYPE_SIZE)
  663. info->c_float = 1;
  664. if (mode_precision == DOUBLE_TYPE_SIZE)
  665. info->c_double = 1;
  666. if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
  667. info->c_long_double = 1;
  668. if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
  669. {
  670. info->c_float128 = 1;
  671. gfc_real16_is_float128 = true;
  672. }
  673. if (TYPE_PRECISION (float_type_node) == mode_precision)
  674. return float_type_node;
  675. if (TYPE_PRECISION (double_type_node) == mode_precision)
  676. return double_type_node;
  677. if (TYPE_PRECISION (long_double_type_node) == mode_precision)
  678. return long_double_type_node;
  679. new_type = make_node (REAL_TYPE);
  680. TYPE_PRECISION (new_type) = mode_precision;
  681. layout_type (new_type);
  682. return new_type;
  683. }
  684. static tree
  685. gfc_build_complex_type (tree scalar_type)
  686. {
  687. tree new_type;
  688. if (scalar_type == NULL)
  689. return NULL;
  690. if (scalar_type == float_type_node)
  691. return complex_float_type_node;
  692. if (scalar_type == double_type_node)
  693. return complex_double_type_node;
  694. if (scalar_type == long_double_type_node)
  695. return complex_long_double_type_node;
  696. new_type = make_node (COMPLEX_TYPE);
  697. TREE_TYPE (new_type) = scalar_type;
  698. layout_type (new_type);
  699. return new_type;
  700. }
  701. static tree
  702. gfc_build_logical_type (gfc_logical_info *info)
  703. {
  704. int bit_size = info->bit_size;
  705. tree new_type;
  706. if (bit_size == BOOL_TYPE_SIZE)
  707. {
  708. info->c_bool = 1;
  709. return boolean_type_node;
  710. }
  711. new_type = make_unsigned_type (bit_size);
  712. TREE_SET_CODE (new_type, BOOLEAN_TYPE);
  713. TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
  714. TYPE_PRECISION (new_type) = 1;
  715. return new_type;
  716. }
  717. /* Create the backend type nodes. We map them to their
  718. equivalent C type, at least for now. We also give
  719. names to the types here, and we push them in the
  720. global binding level context.*/
  721. void
  722. gfc_init_types (void)
  723. {
  724. char name_buf[18];
  725. int index;
  726. tree type;
  727. unsigned n;
  728. /* Create and name the types. */
  729. #define PUSH_TYPE(name, node) \
  730. pushdecl (build_decl (input_location, \
  731. TYPE_DECL, get_identifier (name), node))
  732. for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
  733. {
  734. type = gfc_build_int_type (&gfc_integer_kinds[index]);
  735. /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
  736. if (TYPE_STRING_FLAG (type))
  737. type = make_signed_type (gfc_integer_kinds[index].bit_size);
  738. gfc_integer_types[index] = type;
  739. snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
  740. gfc_integer_kinds[index].kind);
  741. PUSH_TYPE (name_buf, type);
  742. }
  743. for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
  744. {
  745. type = gfc_build_logical_type (&gfc_logical_kinds[index]);
  746. gfc_logical_types[index] = type;
  747. snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
  748. gfc_logical_kinds[index].kind);
  749. PUSH_TYPE (name_buf, type);
  750. }
  751. for (index = 0; gfc_real_kinds[index].kind != 0; index++)
  752. {
  753. type = gfc_build_real_type (&gfc_real_kinds[index]);
  754. gfc_real_types[index] = type;
  755. snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
  756. gfc_real_kinds[index].kind);
  757. PUSH_TYPE (name_buf, type);
  758. if (gfc_real_kinds[index].c_float128)
  759. float128_type_node = type;
  760. type = gfc_build_complex_type (type);
  761. gfc_complex_types[index] = type;
  762. snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
  763. gfc_real_kinds[index].kind);
  764. PUSH_TYPE (name_buf, type);
  765. if (gfc_real_kinds[index].c_float128)
  766. complex_float128_type_node = type;
  767. }
  768. for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
  769. {
  770. type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
  771. type = build_qualified_type (type, TYPE_UNQUALIFIED);
  772. snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
  773. gfc_character_kinds[index].kind);
  774. PUSH_TYPE (name_buf, type);
  775. gfc_character_types[index] = type;
  776. gfc_pcharacter_types[index] = build_pointer_type (type);
  777. }
  778. gfc_character1_type_node = gfc_character_types[0];
  779. PUSH_TYPE ("byte", unsigned_char_type_node);
  780. PUSH_TYPE ("void", void_type_node);
  781. /* DBX debugging output gets upset if these aren't set. */
  782. if (!TYPE_NAME (integer_type_node))
  783. PUSH_TYPE ("c_integer", integer_type_node);
  784. if (!TYPE_NAME (char_type_node))
  785. PUSH_TYPE ("c_char", char_type_node);
  786. #undef PUSH_TYPE
  787. pvoid_type_node = build_pointer_type (void_type_node);
  788. prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
  789. ppvoid_type_node = build_pointer_type (pvoid_type_node);
  790. pchar_type_node = build_pointer_type (gfc_character1_type_node);
  791. pfunc_type_node
  792. = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
  793. gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
  794. /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
  795. since this function is called before gfc_init_constants. */
  796. gfc_array_range_type
  797. = build_range_type (gfc_array_index_type,
  798. build_int_cst (gfc_array_index_type, 0),
  799. NULL_TREE);
  800. /* The maximum array element size that can be handled is determined
  801. by the number of bits available to store this field in the array
  802. descriptor. */
  803. n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
  804. gfc_max_array_element_size
  805. = wide_int_to_tree (size_type_node,
  806. wi::mask (n, UNSIGNED,
  807. TYPE_PRECISION (size_type_node)));
  808. boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
  809. boolean_true_node = build_int_cst (boolean_type_node, 1);
  810. boolean_false_node = build_int_cst (boolean_type_node, 0);
  811. /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
  812. gfc_charlen_int_kind = 4;
  813. gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
  814. }
  815. /* Get the type node for the given type and kind. */
  816. tree
  817. gfc_get_int_type (int kind)
  818. {
  819. int index = gfc_validate_kind (BT_INTEGER, kind, true);
  820. return index < 0 ? 0 : gfc_integer_types[index];
  821. }
  822. tree
  823. gfc_get_real_type (int kind)
  824. {
  825. int index = gfc_validate_kind (BT_REAL, kind, true);
  826. return index < 0 ? 0 : gfc_real_types[index];
  827. }
  828. tree
  829. gfc_get_complex_type (int kind)
  830. {
  831. int index = gfc_validate_kind (BT_COMPLEX, kind, true);
  832. return index < 0 ? 0 : gfc_complex_types[index];
  833. }
  834. tree
  835. gfc_get_logical_type (int kind)
  836. {
  837. int index = gfc_validate_kind (BT_LOGICAL, kind, true);
  838. return index < 0 ? 0 : gfc_logical_types[index];
  839. }
  840. tree
  841. gfc_get_char_type (int kind)
  842. {
  843. int index = gfc_validate_kind (BT_CHARACTER, kind, true);
  844. return index < 0 ? 0 : gfc_character_types[index];
  845. }
  846. tree
  847. gfc_get_pchar_type (int kind)
  848. {
  849. int index = gfc_validate_kind (BT_CHARACTER, kind, true);
  850. return index < 0 ? 0 : gfc_pcharacter_types[index];
  851. }
  852. /* Create a character type with the given kind and length. */
  853. tree
  854. gfc_get_character_type_len_for_eltype (tree eltype, tree len)
  855. {
  856. tree bounds, type;
  857. bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
  858. type = build_array_type (eltype, bounds);
  859. TYPE_STRING_FLAG (type) = 1;
  860. return type;
  861. }
  862. tree
  863. gfc_get_character_type_len (int kind, tree len)
  864. {
  865. gfc_validate_kind (BT_CHARACTER, kind, false);
  866. return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
  867. }
  868. /* Get a type node for a character kind. */
  869. tree
  870. gfc_get_character_type (int kind, gfc_charlen * cl)
  871. {
  872. tree len;
  873. len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
  874. return gfc_get_character_type_len (kind, len);
  875. }
  876. /* Covert a basic type. This will be an array for character types. */
  877. tree
  878. gfc_typenode_for_spec (gfc_typespec * spec)
  879. {
  880. tree basetype;
  881. switch (spec->type)
  882. {
  883. case BT_UNKNOWN:
  884. gcc_unreachable ();
  885. case BT_INTEGER:
  886. /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
  887. has been resolved. This is done so we can convert C_PTR and
  888. C_FUNPTR to simple variables that get translated to (void *). */
  889. if (spec->f90_type == BT_VOID)
  890. {
  891. if (spec->u.derived
  892. && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
  893. basetype = ptr_type_node;
  894. else
  895. basetype = pfunc_type_node;
  896. }
  897. else
  898. basetype = gfc_get_int_type (spec->kind);
  899. break;
  900. case BT_REAL:
  901. basetype = gfc_get_real_type (spec->kind);
  902. break;
  903. case BT_COMPLEX:
  904. basetype = gfc_get_complex_type (spec->kind);
  905. break;
  906. case BT_LOGICAL:
  907. basetype = gfc_get_logical_type (spec->kind);
  908. break;
  909. case BT_CHARACTER:
  910. basetype = gfc_get_character_type (spec->kind, spec->u.cl);
  911. break;
  912. case BT_HOLLERITH:
  913. /* Since this cannot be used, return a length one character. */
  914. basetype = gfc_get_character_type_len (gfc_default_character_kind,
  915. gfc_index_one_node);
  916. break;
  917. case BT_DERIVED:
  918. case BT_CLASS:
  919. basetype = gfc_get_derived_type (spec->u.derived);
  920. if (spec->type == BT_CLASS)
  921. GFC_CLASS_TYPE_P (basetype) = 1;
  922. /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
  923. type and kind to fit a (void *) and the basetype returned was a
  924. ptr_type_node. We need to pass up this new information to the
  925. symbol that was declared of type C_PTR or C_FUNPTR. */
  926. if (spec->u.derived->ts.f90_type == BT_VOID)
  927. {
  928. spec->type = BT_INTEGER;
  929. spec->kind = gfc_index_integer_kind;
  930. spec->f90_type = BT_VOID;
  931. }
  932. break;
  933. case BT_VOID:
  934. case BT_ASSUMED:
  935. /* This is for the second arg to c_f_pointer and c_f_procpointer
  936. of the iso_c_binding module, to accept any ptr type. */
  937. basetype = ptr_type_node;
  938. if (spec->f90_type == BT_VOID)
  939. {
  940. if (spec->u.derived
  941. && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
  942. basetype = ptr_type_node;
  943. else
  944. basetype = pfunc_type_node;
  945. }
  946. break;
  947. default:
  948. gcc_unreachable ();
  949. }
  950. return basetype;
  951. }
  952. /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
  953. static tree
  954. gfc_conv_array_bound (gfc_expr * expr)
  955. {
  956. /* If expr is an integer constant, return that. */
  957. if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
  958. return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
  959. /* Otherwise return NULL. */
  960. return NULL_TREE;
  961. }
  962. /* Return the type of an element of the array. Note that scalar coarrays
  963. are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
  964. (with POINTER_TYPE stripped) is returned. */
  965. tree
  966. gfc_get_element_type (tree type)
  967. {
  968. tree element;
  969. if (GFC_ARRAY_TYPE_P (type))
  970. {
  971. if (TREE_CODE (type) == POINTER_TYPE)
  972. type = TREE_TYPE (type);
  973. if (GFC_TYPE_ARRAY_RANK (type) == 0)
  974. {
  975. gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
  976. element = type;
  977. }
  978. else
  979. {
  980. gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
  981. element = TREE_TYPE (type);
  982. }
  983. }
  984. else
  985. {
  986. gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
  987. element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
  988. gcc_assert (TREE_CODE (element) == POINTER_TYPE);
  989. element = TREE_TYPE (element);
  990. /* For arrays, which are not scalar coarrays. */
  991. if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
  992. element = TREE_TYPE (element);
  993. }
  994. return element;
  995. }
  996. /* Build an array. This function is called from gfc_sym_type().
  997. Actually returns array descriptor type.
  998. Format of array descriptors is as follows:
  999. struct gfc_array_descriptor
  1000. {
  1001. array *data
  1002. index offset;
  1003. index dtype;
  1004. struct descriptor_dimension dimension[N_DIM];
  1005. }
  1006. struct descriptor_dimension
  1007. {
  1008. index stride;
  1009. index lbound;
  1010. index ubound;
  1011. }
  1012. Translation code should use gfc_conv_descriptor_* rather than
  1013. accessing the descriptor directly. Any changes to the array
  1014. descriptor type will require changes in gfc_conv_descriptor_* and
  1015. gfc_build_array_initializer.
  1016. This is represented internally as a RECORD_TYPE. The index nodes
  1017. are gfc_array_index_type and the data node is a pointer to the
  1018. data. See below for the handling of character types.
  1019. The dtype member is formatted as follows:
  1020. rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
  1021. type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
  1022. size = dtype >> GFC_DTYPE_SIZE_SHIFT
  1023. I originally used nested ARRAY_TYPE nodes to represent arrays, but
  1024. this generated poor code for assumed/deferred size arrays. These
  1025. require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
  1026. of the GENERIC grammar. Also, there is no way to explicitly set
  1027. the array stride, so all data must be packed(1). I've tried to
  1028. mark all the functions which would require modification with a GCC
  1029. ARRAYS comment.
  1030. The data component points to the first element in the array. The
  1031. offset field is the position of the origin of the array (i.e. element
  1032. (0, 0 ...)). This may be outside the bounds of the array.
  1033. An element is accessed by
  1034. data[offset + index0*stride0 + index1*stride1 + index2*stride2]
  1035. This gives good performance as the computation does not involve the
  1036. bounds of the array. For packed arrays, this is optimized further
  1037. by substituting the known strides.
  1038. This system has one problem: all array bounds must be within 2^31
  1039. elements of the origin (2^63 on 64-bit machines). For example
  1040. integer, dimension (80000:90000, 80000:90000, 2) :: array
  1041. may not work properly on 32-bit machines because 80000*80000 >
  1042. 2^31, so the calculation for stride2 would overflow. This may
  1043. still work, but I haven't checked, and it relies on the overflow
  1044. doing the right thing.
  1045. The way to fix this problem is to access elements as follows:
  1046. data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
  1047. Obviously this is much slower. I will make this a compile time
  1048. option, something like -fsmall-array-offsets. Mixing code compiled
  1049. with and without this switch will work.
  1050. (1) This can be worked around by modifying the upper bound of the
  1051. previous dimension. This requires extra fields in the descriptor
  1052. (both real_ubound and fake_ubound). */
  1053. /* Returns true if the array sym does not require a descriptor. */
  1054. int
  1055. gfc_is_nodesc_array (gfc_symbol * sym)
  1056. {
  1057. gcc_assert (sym->attr.dimension || sym->attr.codimension);
  1058. /* We only want local arrays. */
  1059. if (sym->attr.pointer || sym->attr.allocatable)
  1060. return 0;
  1061. /* We want a descriptor for associate-name arrays that do not have an
  1062. explicitly known shape already. */
  1063. if (sym->assoc && sym->as->type != AS_EXPLICIT)
  1064. return 0;
  1065. if (sym->attr.dummy)
  1066. return sym->as->type != AS_ASSUMED_SHAPE
  1067. && sym->as->type != AS_ASSUMED_RANK;
  1068. if (sym->attr.result || sym->attr.function)
  1069. return 0;
  1070. gcc_assert (sym->as->type == AS_EXPLICIT || sym->as->cp_was_assumed);
  1071. return 1;
  1072. }
  1073. /* Create an array descriptor type. */
  1074. static tree
  1075. gfc_build_array_type (tree type, gfc_array_spec * as,
  1076. enum gfc_array_kind akind, bool restricted,
  1077. bool contiguous)
  1078. {
  1079. tree lbound[GFC_MAX_DIMENSIONS];
  1080. tree ubound[GFC_MAX_DIMENSIONS];
  1081. int n, corank;
  1082. /* Assumed-shape arrays do not have codimension information stored in the
  1083. descriptor. */
  1084. corank = as->corank;
  1085. if (as->type == AS_ASSUMED_SHAPE ||
  1086. (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
  1087. corank = 0;
  1088. if (as->type == AS_ASSUMED_RANK)
  1089. for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
  1090. {
  1091. lbound[n] = NULL_TREE;
  1092. ubound[n] = NULL_TREE;
  1093. }
  1094. for (n = 0; n < as->rank; n++)
  1095. {
  1096. /* Create expressions for the known bounds of the array. */
  1097. if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
  1098. lbound[n] = gfc_index_one_node;
  1099. else
  1100. lbound[n] = gfc_conv_array_bound (as->lower[n]);
  1101. ubound[n] = gfc_conv_array_bound (as->upper[n]);
  1102. }
  1103. for (n = as->rank; n < as->rank + corank; n++)
  1104. {
  1105. if (as->type != AS_DEFERRED && as->lower[n] == NULL)
  1106. lbound[n] = gfc_index_one_node;
  1107. else
  1108. lbound[n] = gfc_conv_array_bound (as->lower[n]);
  1109. if (n < as->rank + corank - 1)
  1110. ubound[n] = gfc_conv_array_bound (as->upper[n]);
  1111. }
  1112. if (as->type == AS_ASSUMED_SHAPE)
  1113. akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
  1114. : GFC_ARRAY_ASSUMED_SHAPE;
  1115. else if (as->type == AS_ASSUMED_RANK)
  1116. akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
  1117. : GFC_ARRAY_ASSUMED_RANK;
  1118. return gfc_get_array_type_bounds (type, as->rank == -1
  1119. ? GFC_MAX_DIMENSIONS : as->rank,
  1120. corank, lbound,
  1121. ubound, 0, akind, restricted);
  1122. }
  1123. /* Returns the struct descriptor_dimension type. */
  1124. static tree
  1125. gfc_get_desc_dim_type (void)
  1126. {
  1127. tree type;
  1128. tree decl, *chain = NULL;
  1129. if (gfc_desc_dim_type)
  1130. return gfc_desc_dim_type;
  1131. /* Build the type node. */
  1132. type = make_node (RECORD_TYPE);
  1133. TYPE_NAME (type) = get_identifier ("descriptor_dimension");
  1134. TYPE_PACKED (type) = 1;
  1135. /* Consists of the stride, lbound and ubound members. */
  1136. decl = gfc_add_field_to_struct_1 (type,
  1137. get_identifier ("stride"),
  1138. gfc_array_index_type, &chain);
  1139. TREE_NO_WARNING (decl) = 1;
  1140. decl = gfc_add_field_to_struct_1 (type,
  1141. get_identifier ("lbound"),
  1142. gfc_array_index_type, &chain);
  1143. TREE_NO_WARNING (decl) = 1;
  1144. decl = gfc_add_field_to_struct_1 (type,
  1145. get_identifier ("ubound"),
  1146. gfc_array_index_type, &chain);
  1147. TREE_NO_WARNING (decl) = 1;
  1148. /* Finish off the type. */
  1149. gfc_finish_type (type);
  1150. TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
  1151. gfc_desc_dim_type = type;
  1152. return type;
  1153. }
  1154. /* Return the DTYPE for an array. This describes the type and type parameters
  1155. of the array. */
  1156. /* TODO: Only call this when the value is actually used, and make all the
  1157. unknown cases abort. */
  1158. tree
  1159. gfc_get_dtype_rank_type (int rank, tree etype)
  1160. {
  1161. tree size;
  1162. int n;
  1163. HOST_WIDE_INT i;
  1164. tree tmp;
  1165. tree dtype;
  1166. switch (TREE_CODE (etype))
  1167. {
  1168. case INTEGER_TYPE:
  1169. n = BT_INTEGER;
  1170. break;
  1171. case BOOLEAN_TYPE:
  1172. n = BT_LOGICAL;
  1173. break;
  1174. case REAL_TYPE:
  1175. n = BT_REAL;
  1176. break;
  1177. case COMPLEX_TYPE:
  1178. n = BT_COMPLEX;
  1179. break;
  1180. /* We will never have arrays of arrays. */
  1181. case RECORD_TYPE:
  1182. n = BT_DERIVED;
  1183. break;
  1184. case ARRAY_TYPE:
  1185. n = BT_CHARACTER;
  1186. break;
  1187. case POINTER_TYPE:
  1188. n = BT_ASSUMED;
  1189. break;
  1190. default:
  1191. /* TODO: Don't do dtype for temporary descriptorless arrays. */
  1192. /* We can strange array types for temporary arrays. */
  1193. return gfc_index_zero_node;
  1194. }
  1195. gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
  1196. size = TYPE_SIZE_UNIT (etype);
  1197. i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
  1198. if (size && INTEGER_CST_P (size))
  1199. {
  1200. if (tree_int_cst_lt (gfc_max_array_element_size, size))
  1201. gfc_fatal_error ("Array element size too big at %C");
  1202. i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
  1203. }
  1204. dtype = build_int_cst (gfc_array_index_type, i);
  1205. if (size && !INTEGER_CST_P (size))
  1206. {
  1207. tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
  1208. tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
  1209. gfc_array_index_type,
  1210. fold_convert (gfc_array_index_type, size), tmp);
  1211. dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
  1212. tmp, dtype);
  1213. }
  1214. /* If we don't know the size we leave it as zero. This should never happen
  1215. for anything that is actually used. */
  1216. /* TODO: Check this is actually true, particularly when repacking
  1217. assumed size parameters. */
  1218. return dtype;
  1219. }
  1220. tree
  1221. gfc_get_dtype (tree type)
  1222. {
  1223. tree dtype;
  1224. tree etype;
  1225. int rank;
  1226. gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
  1227. if (GFC_TYPE_ARRAY_DTYPE (type))
  1228. return GFC_TYPE_ARRAY_DTYPE (type);
  1229. rank = GFC_TYPE_ARRAY_RANK (type);
  1230. etype = gfc_get_element_type (type);
  1231. dtype = gfc_get_dtype_rank_type (rank, etype);
  1232. GFC_TYPE_ARRAY_DTYPE (type) = dtype;
  1233. return dtype;
  1234. }
  1235. /* Build an array type for use without a descriptor, packed according
  1236. to the value of PACKED. */
  1237. tree
  1238. gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
  1239. bool restricted)
  1240. {
  1241. tree range;
  1242. tree type;
  1243. tree tmp;
  1244. int n;
  1245. int known_stride;
  1246. int known_offset;
  1247. mpz_t offset;
  1248. mpz_t stride;
  1249. mpz_t delta;
  1250. gfc_expr *expr;
  1251. mpz_init_set_ui (offset, 0);
  1252. mpz_init_set_ui (stride, 1);
  1253. mpz_init (delta);
  1254. /* We don't use build_array_type because this does not include include
  1255. lang-specific information (i.e. the bounds of the array) when checking
  1256. for duplicates. */
  1257. if (as->rank)
  1258. type = make_node (ARRAY_TYPE);
  1259. else
  1260. type = build_variant_type_copy (etype);
  1261. GFC_ARRAY_TYPE_P (type) = 1;
  1262. TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
  1263. known_stride = (packed != PACKED_NO);
  1264. known_offset = 1;
  1265. for (n = 0; n < as->rank; n++)
  1266. {
  1267. /* Fill in the stride and bound components of the type. */
  1268. if (known_stride)
  1269. tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
  1270. else
  1271. tmp = NULL_TREE;
  1272. GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
  1273. expr = as->lower[n];
  1274. if (expr->expr_type == EXPR_CONSTANT)
  1275. {
  1276. tmp = gfc_conv_mpz_to_tree (expr->value.integer,
  1277. gfc_index_integer_kind);
  1278. }
  1279. else
  1280. {
  1281. known_stride = 0;
  1282. tmp = NULL_TREE;
  1283. }
  1284. GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
  1285. if (known_stride)
  1286. {
  1287. /* Calculate the offset. */
  1288. mpz_mul (delta, stride, as->lower[n]->value.integer);
  1289. mpz_sub (offset, offset, delta);
  1290. }
  1291. else
  1292. known_offset = 0;
  1293. expr = as->upper[n];
  1294. if (expr && expr->expr_type == EXPR_CONSTANT)
  1295. {
  1296. tmp = gfc_conv_mpz_to_tree (expr->value.integer,
  1297. gfc_index_integer_kind);
  1298. }
  1299. else
  1300. {
  1301. tmp = NULL_TREE;
  1302. known_stride = 0;
  1303. }
  1304. GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
  1305. if (known_stride)
  1306. {
  1307. /* Calculate the stride. */
  1308. mpz_sub (delta, as->upper[n]->value.integer,
  1309. as->lower[n]->value.integer);
  1310. mpz_add_ui (delta, delta, 1);
  1311. mpz_mul (stride, stride, delta);
  1312. }
  1313. /* Only the first stride is known for partial packed arrays. */
  1314. if (packed == PACKED_NO || packed == PACKED_PARTIAL)
  1315. known_stride = 0;
  1316. }
  1317. for (n = as->rank; n < as->rank + as->corank; n++)
  1318. {
  1319. expr = as->lower[n];
  1320. if (expr->expr_type == EXPR_CONSTANT)
  1321. tmp = gfc_conv_mpz_to_tree (expr->value.integer,
  1322. gfc_index_integer_kind);
  1323. else
  1324. tmp = NULL_TREE;
  1325. GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
  1326. expr = as->upper[n];
  1327. if (expr && expr->expr_type == EXPR_CONSTANT)
  1328. tmp = gfc_conv_mpz_to_tree (expr->value.integer,
  1329. gfc_index_integer_kind);
  1330. else
  1331. tmp = NULL_TREE;
  1332. if (n < as->rank + as->corank - 1)
  1333. GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
  1334. }
  1335. if (known_offset)
  1336. {
  1337. GFC_TYPE_ARRAY_OFFSET (type) =
  1338. gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
  1339. }
  1340. else
  1341. GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
  1342. if (known_stride)
  1343. {
  1344. GFC_TYPE_ARRAY_SIZE (type) =
  1345. gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
  1346. }
  1347. else
  1348. GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
  1349. GFC_TYPE_ARRAY_RANK (type) = as->rank;
  1350. GFC_TYPE_ARRAY_CORANK (type) = as->corank;
  1351. GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
  1352. range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
  1353. NULL_TREE);
  1354. /* TODO: use main type if it is unbounded. */
  1355. GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
  1356. build_pointer_type (build_array_type (etype, range));
  1357. if (restricted)
  1358. GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
  1359. build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
  1360. TYPE_QUAL_RESTRICT);
  1361. if (as->rank == 0)
  1362. {
  1363. if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
  1364. {
  1365. type = build_pointer_type (type);
  1366. if (restricted)
  1367. type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
  1368. GFC_ARRAY_TYPE_P (type) = 1;
  1369. TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
  1370. }
  1371. return type;
  1372. }
  1373. if (known_stride)
  1374. {
  1375. mpz_sub_ui (stride, stride, 1);
  1376. range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
  1377. }
  1378. else
  1379. range = NULL_TREE;
  1380. range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
  1381. TYPE_DOMAIN (type) = range;
  1382. build_pointer_type (etype);
  1383. TREE_TYPE (type) = etype;
  1384. layout_type (type);
  1385. mpz_clear (offset);
  1386. mpz_clear (stride);
  1387. mpz_clear (delta);
  1388. /* Represent packed arrays as multi-dimensional if they have rank >
  1389. 1 and with proper bounds, instead of flat arrays. This makes for
  1390. better debug info. */
  1391. if (known_offset)
  1392. {
  1393. tree gtype = etype, rtype, type_decl;
  1394. for (n = as->rank - 1; n >= 0; n--)
  1395. {
  1396. rtype = build_range_type (gfc_array_index_type,
  1397. GFC_TYPE_ARRAY_LBOUND (type, n),
  1398. GFC_TYPE_ARRAY_UBOUND (type, n));
  1399. gtype = build_array_type (gtype, rtype);
  1400. }
  1401. TYPE_NAME (type) = type_decl = build_decl (input_location,
  1402. TYPE_DECL, NULL, gtype);
  1403. DECL_ORIGINAL_TYPE (type_decl) = gtype;
  1404. }
  1405. if (packed != PACKED_STATIC || !known_stride
  1406. || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
  1407. {
  1408. /* For dummy arrays and automatic (heap allocated) arrays we
  1409. want a pointer to the array. */
  1410. type = build_pointer_type (type);
  1411. if (restricted)
  1412. type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
  1413. GFC_ARRAY_TYPE_P (type) = 1;
  1414. TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
  1415. }
  1416. return type;
  1417. }
  1418. /* Return or create the base type for an array descriptor. */
  1419. static tree
  1420. gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted,
  1421. enum gfc_array_kind akind)
  1422. {
  1423. tree fat_type, decl, arraytype, *chain = NULL;
  1424. char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
  1425. int idx;
  1426. /* Assumed-rank array. */
  1427. if (dimen == -1)
  1428. dimen = GFC_MAX_DIMENSIONS;
  1429. idx = 2 * (codimen + dimen) + restricted;
  1430. gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
  1431. if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
  1432. {
  1433. if (gfc_array_descriptor_base_caf[idx])
  1434. return gfc_array_descriptor_base_caf[idx];
  1435. }
  1436. else if (gfc_array_descriptor_base[idx])
  1437. return gfc_array_descriptor_base[idx];
  1438. /* Build the type node. */
  1439. fat_type = make_node (RECORD_TYPE);
  1440. sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
  1441. TYPE_NAME (fat_type) = get_identifier (name);
  1442. TYPE_NAMELESS (fat_type) = 1;
  1443. /* Add the data member as the first element of the descriptor. */
  1444. decl = gfc_add_field_to_struct_1 (fat_type,
  1445. get_identifier ("data"),
  1446. (restricted
  1447. ? prvoid_type_node
  1448. : ptr_type_node), &chain);
  1449. /* Add the base component. */
  1450. decl = gfc_add_field_to_struct_1 (fat_type,
  1451. get_identifier ("offset"),
  1452. gfc_array_index_type, &chain);
  1453. TREE_NO_WARNING (decl) = 1;
  1454. /* Add the dtype component. */
  1455. decl = gfc_add_field_to_struct_1 (fat_type,
  1456. get_identifier ("dtype"),
  1457. gfc_array_index_type, &chain);
  1458. TREE_NO_WARNING (decl) = 1;
  1459. /* Build the array type for the stride and bound components. */
  1460. if (dimen + codimen > 0)
  1461. {
  1462. arraytype =
  1463. build_array_type (gfc_get_desc_dim_type (),
  1464. build_range_type (gfc_array_index_type,
  1465. gfc_index_zero_node,
  1466. gfc_rank_cst[codimen + dimen - 1]));
  1467. decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
  1468. arraytype, &chain);
  1469. TREE_NO_WARNING (decl) = 1;
  1470. }
  1471. if (flag_coarray == GFC_FCOARRAY_LIB && codimen
  1472. && akind == GFC_ARRAY_ALLOCATABLE)
  1473. {
  1474. decl = gfc_add_field_to_struct_1 (fat_type,
  1475. get_identifier ("token"),
  1476. prvoid_type_node, &chain);
  1477. TREE_NO_WARNING (decl) = 1;
  1478. }
  1479. /* Finish off the type. */
  1480. gfc_finish_type (fat_type);
  1481. TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
  1482. if (flag_coarray == GFC_FCOARRAY_LIB && codimen
  1483. && akind == GFC_ARRAY_ALLOCATABLE)
  1484. gfc_array_descriptor_base_caf[idx] = fat_type;
  1485. else
  1486. gfc_array_descriptor_base[idx] = fat_type;
  1487. return fat_type;
  1488. }
  1489. /* Build an array (descriptor) type with given bounds. */
  1490. tree
  1491. gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
  1492. tree * ubound, int packed,
  1493. enum gfc_array_kind akind, bool restricted)
  1494. {
  1495. char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
  1496. tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
  1497. const char *type_name;
  1498. int n;
  1499. base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted, akind);
  1500. fat_type = build_distinct_type_copy (base_type);
  1501. /* Make sure that nontarget and target array type have the same canonical
  1502. type (and same stub decl for debug info). */
  1503. base_type = gfc_get_array_descriptor_base (dimen, codimen, false, akind);
  1504. TYPE_CANONICAL (fat_type) = base_type;
  1505. TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
  1506. tmp = TYPE_NAME (etype);
  1507. if (tmp && TREE_CODE (tmp) == TYPE_DECL)
  1508. tmp = DECL_NAME (tmp);
  1509. if (tmp)
  1510. type_name = IDENTIFIER_POINTER (tmp);
  1511. else
  1512. type_name = "unknown";
  1513. sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
  1514. GFC_MAX_SYMBOL_LEN, type_name);
  1515. TYPE_NAME (fat_type) = get_identifier (name);
  1516. TYPE_NAMELESS (fat_type) = 1;
  1517. GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
  1518. TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
  1519. GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
  1520. GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
  1521. GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
  1522. GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
  1523. /* Build an array descriptor record type. */
  1524. if (packed != 0)
  1525. stride = gfc_index_one_node;
  1526. else
  1527. stride = NULL_TREE;
  1528. for (n = 0; n < dimen + codimen; n++)
  1529. {
  1530. if (n < dimen)
  1531. GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
  1532. if (lbound)
  1533. lower = lbound[n];
  1534. else
  1535. lower = NULL_TREE;
  1536. if (lower != NULL_TREE)
  1537. {
  1538. if (INTEGER_CST_P (lower))
  1539. GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
  1540. else
  1541. lower = NULL_TREE;
  1542. }
  1543. if (codimen && n == dimen + codimen - 1)
  1544. break;
  1545. upper = ubound[n];
  1546. if (upper != NULL_TREE)
  1547. {
  1548. if (INTEGER_CST_P (upper))
  1549. GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
  1550. else
  1551. upper = NULL_TREE;
  1552. }
  1553. if (n >= dimen)
  1554. continue;
  1555. if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
  1556. {
  1557. tmp = fold_build2_loc (input_location, MINUS_EXPR,
  1558. gfc_array_index_type, upper, lower);
  1559. tmp = fold_build2_loc (input_location, PLUS_EXPR,
  1560. gfc_array_index_type, tmp,
  1561. gfc_index_one_node);
  1562. stride = fold_build2_loc (input_location, MULT_EXPR,
  1563. gfc_array_index_type, tmp, stride);
  1564. /* Check the folding worked. */
  1565. gcc_assert (INTEGER_CST_P (stride));
  1566. }
  1567. else
  1568. stride = NULL_TREE;
  1569. }
  1570. GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
  1571. /* TODO: known offsets for descriptors. */
  1572. GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
  1573. if (dimen == 0)
  1574. {
  1575. arraytype = build_pointer_type (etype);
  1576. if (restricted)
  1577. arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
  1578. GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
  1579. return fat_type;
  1580. }
  1581. /* We define data as an array with the correct size if possible.
  1582. Much better than doing pointer arithmetic. */
  1583. if (stride)
  1584. rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
  1585. int_const_binop (MINUS_EXPR, stride,
  1586. build_int_cst (TREE_TYPE (stride), 1)));
  1587. else
  1588. rtype = gfc_array_range_type;
  1589. arraytype = build_array_type (etype, rtype);
  1590. arraytype = build_pointer_type (arraytype);
  1591. if (restricted)
  1592. arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
  1593. GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
  1594. /* This will generate the base declarations we need to emit debug
  1595. information for this type. FIXME: there must be a better way to
  1596. avoid divergence between compilations with and without debug
  1597. information. */
  1598. {
  1599. struct array_descr_info info;
  1600. gfc_get_array_descr_info (fat_type, &info);
  1601. gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
  1602. }
  1603. return fat_type;
  1604. }
  1605. /* Build a pointer type. This function is called from gfc_sym_type(). */
  1606. static tree
  1607. gfc_build_pointer_type (gfc_symbol * sym, tree type)
  1608. {
  1609. /* Array pointer types aren't actually pointers. */
  1610. if (sym->attr.dimension)
  1611. return type;
  1612. else
  1613. return build_pointer_type (type);
  1614. }
  1615. static tree gfc_nonrestricted_type (tree t);
  1616. /* Given two record or union type nodes TO and FROM, ensure
  1617. that all fields in FROM have a corresponding field in TO,
  1618. their type being nonrestrict variants. This accepts a TO
  1619. node that already has a prefix of the fields in FROM. */
  1620. static void
  1621. mirror_fields (tree to, tree from)
  1622. {
  1623. tree fto, ffrom;
  1624. tree *chain;
  1625. /* Forward to the end of TOs fields. */
  1626. fto = TYPE_FIELDS (to);
  1627. ffrom = TYPE_FIELDS (from);
  1628. chain = &TYPE_FIELDS (to);
  1629. while (fto)
  1630. {
  1631. gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
  1632. chain = &DECL_CHAIN (fto);
  1633. fto = DECL_CHAIN (fto);
  1634. ffrom = DECL_CHAIN (ffrom);
  1635. }
  1636. /* Now add all fields remaining in FROM (starting with ffrom). */
  1637. for (; ffrom; ffrom = DECL_CHAIN (ffrom))
  1638. {
  1639. tree newfield = copy_node (ffrom);
  1640. DECL_CONTEXT (newfield) = to;
  1641. /* The store to DECL_CHAIN might seem redundant with the
  1642. stores to *chain, but not clearing it here would mean
  1643. leaving a chain into the old fields. If ever
  1644. our called functions would look at them confusion
  1645. will arise. */
  1646. DECL_CHAIN (newfield) = NULL_TREE;
  1647. *chain = newfield;
  1648. chain = &DECL_CHAIN (newfield);
  1649. if (TREE_CODE (ffrom) == FIELD_DECL)
  1650. {
  1651. tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
  1652. TREE_TYPE (newfield) = elemtype;
  1653. }
  1654. }
  1655. *chain = NULL_TREE;
  1656. }
  1657. /* Given a type T, returns a different type of the same structure,
  1658. except that all types it refers to (recursively) are always
  1659. non-restrict qualified types. */
  1660. static tree
  1661. gfc_nonrestricted_type (tree t)
  1662. {
  1663. tree ret = t;
  1664. /* If the type isn't laid out yet, don't copy it. If something
  1665. needs it for real it should wait until the type got finished. */
  1666. if (!TYPE_SIZE (t))
  1667. return t;
  1668. if (!TYPE_LANG_SPECIFIC (t))
  1669. TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
  1670. /* If we're dealing with this very node already further up
  1671. the call chain (recursion via pointers and struct members)
  1672. we haven't yet determined if we really need a new type node.
  1673. Assume we don't, return T itself. */
  1674. if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
  1675. return t;
  1676. /* If we have calculated this all already, just return it. */
  1677. if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
  1678. return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
  1679. /* Mark this type. */
  1680. TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
  1681. switch (TREE_CODE (t))
  1682. {
  1683. default:
  1684. break;
  1685. case POINTER_TYPE:
  1686. case REFERENCE_TYPE:
  1687. {
  1688. tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
  1689. if (totype == TREE_TYPE (t))
  1690. ret = t;
  1691. else if (TREE_CODE (t) == POINTER_TYPE)
  1692. ret = build_pointer_type (totype);
  1693. else
  1694. ret = build_reference_type (totype);
  1695. ret = build_qualified_type (ret,
  1696. TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
  1697. }
  1698. break;
  1699. case ARRAY_TYPE:
  1700. {
  1701. tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
  1702. if (elemtype == TREE_TYPE (t))
  1703. ret = t;
  1704. else
  1705. {
  1706. ret = build_variant_type_copy (t);
  1707. TREE_TYPE (ret) = elemtype;
  1708. if (TYPE_LANG_SPECIFIC (t)
  1709. && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
  1710. {
  1711. tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
  1712. dataptr_type = gfc_nonrestricted_type (dataptr_type);
  1713. if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
  1714. {
  1715. TYPE_LANG_SPECIFIC (ret)
  1716. = ggc_cleared_alloc<struct lang_type> ();
  1717. *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
  1718. GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
  1719. }
  1720. }
  1721. }
  1722. }
  1723. break;
  1724. case RECORD_TYPE:
  1725. case UNION_TYPE:
  1726. case QUAL_UNION_TYPE:
  1727. {
  1728. tree field;
  1729. /* First determine if we need a new type at all.
  1730. Careful, the two calls to gfc_nonrestricted_type per field
  1731. might return different values. That happens exactly when
  1732. one of the fields reaches back to this very record type
  1733. (via pointers). The first calls will assume that we don't
  1734. need to copy T (see the error_mark_node marking). If there
  1735. are any reasons for copying T apart from having to copy T,
  1736. we'll indeed copy it, and the second calls to
  1737. gfc_nonrestricted_type will use that new node if they
  1738. reach back to T. */
  1739. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  1740. if (TREE_CODE (field) == FIELD_DECL)
  1741. {
  1742. tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
  1743. if (elemtype != TREE_TYPE (field))
  1744. break;
  1745. }
  1746. if (!field)
  1747. break;
  1748. ret = build_variant_type_copy (t);
  1749. TYPE_FIELDS (ret) = NULL_TREE;
  1750. /* Here we make sure that as soon as we know we have to copy
  1751. T, that also fields reaching back to us will use the new
  1752. copy. It's okay if that copy still contains the old fields,
  1753. we won't look at them. */
  1754. TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
  1755. mirror_fields (ret, t);
  1756. }
  1757. break;
  1758. }
  1759. TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
  1760. return ret;
  1761. }
  1762. /* Return the type for a symbol. Special handling is required for character
  1763. types to get the correct level of indirection.
  1764. For functions return the return type.
  1765. For subroutines return void_type_node.
  1766. Calling this multiple times for the same symbol should be avoided,
  1767. especially for character and array types. */
  1768. tree
  1769. gfc_sym_type (gfc_symbol * sym)
  1770. {
  1771. tree type;
  1772. int byref;
  1773. bool restricted;
  1774. /* Procedure Pointers inside COMMON blocks. */
  1775. if (sym->attr.proc_pointer && sym->attr.in_common)
  1776. {
  1777. /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
  1778. sym->attr.proc_pointer = 0;
  1779. type = build_pointer_type (gfc_get_function_type (sym));
  1780. sym->attr.proc_pointer = 1;
  1781. return type;
  1782. }
  1783. if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
  1784. return void_type_node;
  1785. /* In the case of a function the fake result variable may have a
  1786. type different from the function type, so don't return early in
  1787. that case. */
  1788. if (sym->backend_decl && !sym->attr.function)
  1789. return TREE_TYPE (sym->backend_decl);
  1790. if (sym->ts.type == BT_CHARACTER
  1791. && ((sym->attr.function && sym->attr.is_bind_c)
  1792. || (sym->attr.result
  1793. && sym->ns->proc_name
  1794. && sym->ns->proc_name->attr.is_bind_c)
  1795. || (sym->ts.deferred && (!sym->ts.u.cl
  1796. || !sym->ts.u.cl->backend_decl))))
  1797. type = gfc_character1_type_node;
  1798. else
  1799. type = gfc_typenode_for_spec (&sym->ts);
  1800. if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
  1801. byref = 1;
  1802. else
  1803. byref = 0;
  1804. restricted = !sym->attr.target && !sym->attr.pointer
  1805. && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
  1806. if (!restricted)
  1807. type = gfc_nonrestricted_type (type);
  1808. if (sym->attr.dimension || sym->attr.codimension)
  1809. {
  1810. if (gfc_is_nodesc_array (sym))
  1811. {
  1812. /* If this is a character argument of unknown length, just use the
  1813. base type. */
  1814. if (sym->ts.type != BT_CHARACTER
  1815. || !(sym->attr.dummy || sym->attr.function)
  1816. || sym->ts.u.cl->backend_decl)
  1817. {
  1818. type = gfc_get_nodesc_array_type (type, sym->as,
  1819. byref ? PACKED_FULL
  1820. : PACKED_STATIC,
  1821. restricted);
  1822. byref = 0;
  1823. }
  1824. }
  1825. else
  1826. {
  1827. enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
  1828. if (sym->attr.pointer)
  1829. akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
  1830. : GFC_ARRAY_POINTER;
  1831. else if (sym->attr.allocatable)
  1832. akind = GFC_ARRAY_ALLOCATABLE;
  1833. type = gfc_build_array_type (type, sym->as, akind, restricted,
  1834. sym->attr.contiguous);
  1835. }
  1836. }
  1837. else
  1838. {
  1839. if (sym->attr.allocatable || sym->attr.pointer
  1840. || gfc_is_associate_pointer (sym))
  1841. type = gfc_build_pointer_type (sym, type);
  1842. }
  1843. /* We currently pass all parameters by reference.
  1844. See f95_get_function_decl. For dummy function parameters return the
  1845. function type. */
  1846. if (byref)
  1847. {
  1848. /* We must use pointer types for potentially absent variables. The
  1849. optimizers assume a reference type argument is never NULL. */
  1850. if (sym->attr.optional
  1851. || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
  1852. type = build_pointer_type (type);
  1853. else
  1854. {
  1855. type = build_reference_type (type);
  1856. if (restricted)
  1857. type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
  1858. }
  1859. }
  1860. return (type);
  1861. }
  1862. /* Layout and output debug info for a record type. */
  1863. void
  1864. gfc_finish_type (tree type)
  1865. {
  1866. tree decl;
  1867. decl = build_decl (input_location,
  1868. TYPE_DECL, NULL_TREE, type);
  1869. TYPE_STUB_DECL (type) = decl;
  1870. layout_type (type);
  1871. rest_of_type_compilation (type, 1);
  1872. rest_of_decl_compilation (decl, 1, 0);
  1873. }
  1874. /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
  1875. or RECORD_TYPE pointed to by CONTEXT. The new field is chained
  1876. to the end of the field list pointed to by *CHAIN.
  1877. Returns a pointer to the new field. */
  1878. static tree
  1879. gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
  1880. {
  1881. tree decl = build_decl (input_location, FIELD_DECL, name, type);
  1882. DECL_CONTEXT (decl) = context;
  1883. DECL_CHAIN (decl) = NULL_TREE;
  1884. if (TYPE_FIELDS (context) == NULL_TREE)
  1885. TYPE_FIELDS (context) = decl;
  1886. if (chain != NULL)
  1887. {
  1888. if (*chain != NULL)
  1889. **chain = decl;
  1890. *chain = &DECL_CHAIN (decl);
  1891. }
  1892. return decl;
  1893. }
  1894. /* Like `gfc_add_field_to_struct_1', but adds alignment
  1895. information. */
  1896. tree
  1897. gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
  1898. {
  1899. tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
  1900. DECL_INITIAL (decl) = 0;
  1901. DECL_ALIGN (decl) = 0;
  1902. DECL_USER_ALIGN (decl) = 0;
  1903. return decl;
  1904. }
  1905. /* Copy the backend_decl and component backend_decls if
  1906. the two derived type symbols are "equal", as described
  1907. in 4.4.2 and resolved by gfc_compare_derived_types. */
  1908. int
  1909. gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
  1910. bool from_gsym)
  1911. {
  1912. gfc_component *to_cm;
  1913. gfc_component *from_cm;
  1914. if (from == to)
  1915. return 1;
  1916. if (from->backend_decl == NULL
  1917. || !gfc_compare_derived_types (from, to))
  1918. return 0;
  1919. to->backend_decl = from->backend_decl;
  1920. to_cm = to->components;
  1921. from_cm = from->components;
  1922. /* Copy the component declarations. If a component is itself
  1923. a derived type, we need a copy of its component declarations.
  1924. This is done by recursing into gfc_get_derived_type and
  1925. ensures that the component's component declarations have
  1926. been built. If it is a character, we need the character
  1927. length, as well. */
  1928. for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
  1929. {
  1930. to_cm->backend_decl = from_cm->backend_decl;
  1931. if (from_cm->ts.type == BT_DERIVED
  1932. && (!from_cm->attr.pointer || from_gsym))
  1933. gfc_get_derived_type (to_cm->ts.u.derived);
  1934. else if (from_cm->ts.type == BT_CLASS
  1935. && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
  1936. gfc_get_derived_type (to_cm->ts.u.derived);
  1937. else if (from_cm->ts.type == BT_CHARACTER)
  1938. to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
  1939. }
  1940. return 1;
  1941. }
  1942. /* Build a tree node for a procedure pointer component. */
  1943. tree
  1944. gfc_get_ppc_type (gfc_component* c)
  1945. {
  1946. tree t;
  1947. /* Explicit interface. */
  1948. if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
  1949. return build_pointer_type (gfc_get_function_type (c->ts.interface));
  1950. /* Implicit interface (only return value may be known). */
  1951. if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
  1952. t = gfc_typenode_for_spec (&c->ts);
  1953. else
  1954. t = void_type_node;
  1955. return build_pointer_type (build_function_type_list (t, NULL_TREE));
  1956. }
  1957. /* Build a tree node for a derived type. If there are equal
  1958. derived types, with different local names, these are built
  1959. at the same time. If an equal derived type has been built
  1960. in a parent namespace, this is used. */
  1961. tree
  1962. gfc_get_derived_type (gfc_symbol * derived)
  1963. {
  1964. tree typenode = NULL, field = NULL, field_type = NULL;
  1965. tree canonical = NULL_TREE;
  1966. tree *chain = NULL;
  1967. bool got_canonical = false;
  1968. bool unlimited_entity = false;
  1969. gfc_component *c;
  1970. gfc_dt_list *dt;
  1971. gfc_namespace *ns;
  1972. if (derived->attr.unlimited_polymorphic
  1973. || (flag_coarray == GFC_FCOARRAY_LIB
  1974. && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
  1975. && derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE))
  1976. return ptr_type_node;
  1977. if (derived && derived->attr.flavor == FL_PROCEDURE
  1978. && derived->attr.generic)
  1979. derived = gfc_find_dt_in_generic (derived);
  1980. /* See if it's one of the iso_c_binding derived types. */
  1981. if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
  1982. {
  1983. if (derived->backend_decl)
  1984. return derived->backend_decl;
  1985. if (derived->intmod_sym_id == ISOCBINDING_PTR)
  1986. derived->backend_decl = ptr_type_node;
  1987. else
  1988. derived->backend_decl = pfunc_type_node;
  1989. derived->ts.kind = gfc_index_integer_kind;
  1990. derived->ts.type = BT_INTEGER;
  1991. /* Set the f90_type to BT_VOID as a way to recognize something of type
  1992. BT_INTEGER that needs to fit a void * for the purpose of the
  1993. iso_c_binding derived types. */
  1994. derived->ts.f90_type = BT_VOID;
  1995. return derived->backend_decl;
  1996. }
  1997. /* If use associated, use the module type for this one. */
  1998. if (derived->backend_decl == NULL
  1999. && derived->attr.use_assoc
  2000. && derived->module
  2001. && gfc_get_module_backend_decl (derived))
  2002. goto copy_derived_types;
  2003. /* The derived types from an earlier namespace can be used as the
  2004. canonical type. */
  2005. if (derived->backend_decl == NULL && !derived->attr.use_assoc
  2006. && gfc_global_ns_list)
  2007. {
  2008. for (ns = gfc_global_ns_list;
  2009. ns->translated && !got_canonical;
  2010. ns = ns->sibling)
  2011. {
  2012. dt = ns->derived_types;
  2013. for (; dt && !canonical; dt = dt->next)
  2014. {
  2015. gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
  2016. if (derived->backend_decl)
  2017. got_canonical = true;
  2018. }
  2019. }
  2020. }
  2021. /* Store up the canonical type to be added to this one. */
  2022. if (got_canonical)
  2023. {
  2024. if (TYPE_CANONICAL (derived->backend_decl))
  2025. canonical = TYPE_CANONICAL (derived->backend_decl);
  2026. else
  2027. canonical = derived->backend_decl;
  2028. derived->backend_decl = NULL_TREE;
  2029. }
  2030. /* derived->backend_decl != 0 means we saw it before, but its
  2031. components' backend_decl may have not been built. */
  2032. if (derived->backend_decl)
  2033. {
  2034. /* Its components' backend_decl have been built or we are
  2035. seeing recursion through the formal arglist of a procedure
  2036. pointer component. */
  2037. if (TYPE_FIELDS (derived->backend_decl))
  2038. return derived->backend_decl;
  2039. else if (derived->attr.abstract
  2040. && derived->attr.proc_pointer_comp)
  2041. {
  2042. /* If an abstract derived type with procedure pointer
  2043. components has no other type of component, return the
  2044. backend_decl. Otherwise build the components if any of the
  2045. non-procedure pointer components have no backend_decl. */
  2046. for (c = derived->components; c; c = c->next)
  2047. {
  2048. if (!c->attr.proc_pointer && c->backend_decl == NULL)
  2049. break;
  2050. else if (c->next == NULL)
  2051. return derived->backend_decl;
  2052. }
  2053. typenode = derived->backend_decl;
  2054. }
  2055. else
  2056. typenode = derived->backend_decl;
  2057. }
  2058. else
  2059. {
  2060. /* We see this derived type first time, so build the type node. */
  2061. typenode = make_node (RECORD_TYPE);
  2062. TYPE_NAME (typenode) = get_identifier (derived->name);
  2063. TYPE_PACKED (typenode) = flag_pack_derived;
  2064. derived->backend_decl = typenode;
  2065. }
  2066. if (derived->components
  2067. && derived->components->ts.type == BT_DERIVED
  2068. && strcmp (derived->components->name, "_data") == 0
  2069. && derived->components->ts.u.derived->attr.unlimited_polymorphic)
  2070. unlimited_entity = true;
  2071. /* Go through the derived type components, building them as
  2072. necessary. The reason for doing this now is that it is
  2073. possible to recurse back to this derived type through a
  2074. pointer component (PR24092). If this happens, the fields
  2075. will be built and so we can return the type. */
  2076. for (c = derived->components; c; c = c->next)
  2077. {
  2078. if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
  2079. continue;
  2080. if ((!c->attr.pointer && !c->attr.proc_pointer)
  2081. || c->ts.u.derived->backend_decl == NULL)
  2082. c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
  2083. if (c->ts.u.derived->attr.is_iso_c)
  2084. {
  2085. /* Need to copy the modified ts from the derived type. The
  2086. typespec was modified because C_PTR/C_FUNPTR are translated
  2087. into (void *) from derived types. */
  2088. c->ts.type = c->ts.u.derived->ts.type;
  2089. c->ts.kind = c->ts.u.derived->ts.kind;
  2090. c->ts.f90_type = c->ts.u.derived->ts.f90_type;
  2091. if (c->initializer)
  2092. {
  2093. c->initializer->ts.type = c->ts.type;
  2094. c->initializer->ts.kind = c->ts.kind;
  2095. c->initializer->ts.f90_type = c->ts.f90_type;
  2096. c->initializer->expr_type = EXPR_NULL;
  2097. }
  2098. }
  2099. }
  2100. if (TYPE_FIELDS (derived->backend_decl))
  2101. return derived->backend_decl;
  2102. /* Build the type member list. Install the newly created RECORD_TYPE
  2103. node as DECL_CONTEXT of each FIELD_DECL. */
  2104. for (c = derived->components; c; c = c->next)
  2105. {
  2106. if (c->attr.proc_pointer)
  2107. field_type = gfc_get_ppc_type (c);
  2108. else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
  2109. field_type = c->ts.u.derived->backend_decl;
  2110. else
  2111. {
  2112. if (c->ts.type == BT_CHARACTER && !c->ts.deferred)
  2113. {
  2114. /* Evaluate the string length. */
  2115. gfc_conv_const_charlen (c->ts.u.cl);
  2116. gcc_assert (c->ts.u.cl->backend_decl);
  2117. }
  2118. else if (c->ts.type == BT_CHARACTER)
  2119. c->ts.u.cl->backend_decl
  2120. = build_int_cst (gfc_charlen_type_node, 0);
  2121. field_type = gfc_typenode_for_spec (&c->ts);
  2122. }
  2123. /* This returns an array descriptor type. Initialization may be
  2124. required. */
  2125. if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
  2126. {
  2127. if (c->attr.pointer || c->attr.allocatable)
  2128. {
  2129. enum gfc_array_kind akind;
  2130. if (c->attr.pointer)
  2131. akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
  2132. : GFC_ARRAY_POINTER;
  2133. else
  2134. akind = GFC_ARRAY_ALLOCATABLE;
  2135. /* Pointers to arrays aren't actually pointer types. The
  2136. descriptors are separate, but the data is common. */
  2137. field_type = gfc_build_array_type (field_type, c->as, akind,
  2138. !c->attr.target
  2139. && !c->attr.pointer,
  2140. c->attr.contiguous);
  2141. }
  2142. else
  2143. field_type = gfc_get_nodesc_array_type (field_type, c->as,
  2144. PACKED_STATIC,
  2145. !c->attr.target);
  2146. }
  2147. else if ((c->attr.pointer || c->attr.allocatable)
  2148. && !c->attr.proc_pointer
  2149. && !(unlimited_entity && c == derived->components))
  2150. field_type = build_pointer_type (field_type);
  2151. if (c->attr.pointer)
  2152. field_type = gfc_nonrestricted_type (field_type);
  2153. /* vtype fields can point to different types to the base type. */
  2154. if (c->ts.type == BT_DERIVED
  2155. && c->ts.u.derived && c->ts.u.derived->attr.vtype)
  2156. field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
  2157. ptr_mode, true);
  2158. /* Ensure that the CLASS language specific flag is set. */
  2159. if (c->ts.type == BT_CLASS)
  2160. {
  2161. if (POINTER_TYPE_P (field_type))
  2162. GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
  2163. else
  2164. GFC_CLASS_TYPE_P (field_type) = 1;
  2165. }
  2166. field = gfc_add_field_to_struct (typenode,
  2167. get_identifier (c->name),
  2168. field_type, &chain);
  2169. if (c->loc.lb)
  2170. gfc_set_decl_location (field, &c->loc);
  2171. else if (derived->declared_at.lb)
  2172. gfc_set_decl_location (field, &derived->declared_at);
  2173. gfc_finish_decl_attrs (field, &c->attr);
  2174. DECL_PACKED (field) |= TYPE_PACKED (typenode);
  2175. gcc_assert (field);
  2176. if (!c->backend_decl)
  2177. c->backend_decl = field;
  2178. }
  2179. /* Now lay out the derived type, including the fields. */
  2180. if (canonical)
  2181. TYPE_CANONICAL (typenode) = canonical;
  2182. gfc_finish_type (typenode);
  2183. gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
  2184. if (derived->module && derived->ns->proc_name
  2185. && derived->ns->proc_name->attr.flavor == FL_MODULE)
  2186. {
  2187. if (derived->ns->proc_name->backend_decl
  2188. && TREE_CODE (derived->ns->proc_name->backend_decl)
  2189. == NAMESPACE_DECL)
  2190. {
  2191. TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
  2192. DECL_CONTEXT (TYPE_STUB_DECL (typenode))
  2193. = derived->ns->proc_name->backend_decl;
  2194. }
  2195. }
  2196. derived->backend_decl = typenode;
  2197. copy_derived_types:
  2198. for (dt = gfc_derived_types; dt; dt = dt->next)
  2199. gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
  2200. return derived->backend_decl;
  2201. }
  2202. int
  2203. gfc_return_by_reference (gfc_symbol * sym)
  2204. {
  2205. if (!sym->attr.function)
  2206. return 0;
  2207. if (sym->attr.dimension)
  2208. return 1;
  2209. if (sym->ts.type == BT_CHARACTER
  2210. && !sym->attr.is_bind_c
  2211. && (!sym->attr.result
  2212. || !sym->ns->proc_name
  2213. || !sym->ns->proc_name->attr.is_bind_c))
  2214. return 1;
  2215. /* Possibly return complex numbers by reference for g77 compatibility.
  2216. We don't do this for calls to intrinsics (as the library uses the
  2217. -fno-f2c calling convention), nor for calls to functions which always
  2218. require an explicit interface, as no compatibility problems can
  2219. arise there. */
  2220. if (flag_f2c && sym->ts.type == BT_COMPLEX
  2221. && !sym->attr.intrinsic && !sym->attr.always_explicit)
  2222. return 1;
  2223. return 0;
  2224. }
  2225. static tree
  2226. gfc_get_mixed_entry_union (gfc_namespace *ns)
  2227. {
  2228. tree type;
  2229. tree *chain = NULL;
  2230. char name[GFC_MAX_SYMBOL_LEN + 1];
  2231. gfc_entry_list *el, *el2;
  2232. gcc_assert (ns->proc_name->attr.mixed_entry_master);
  2233. gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
  2234. snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
  2235. /* Build the type node. */
  2236. type = make_node (UNION_TYPE);
  2237. TYPE_NAME (type) = get_identifier (name);
  2238. for (el = ns->entries; el; el = el->next)
  2239. {
  2240. /* Search for duplicates. */
  2241. for (el2 = ns->entries; el2 != el; el2 = el2->next)
  2242. if (el2->sym->result == el->sym->result)
  2243. break;
  2244. if (el == el2)
  2245. gfc_add_field_to_struct_1 (type,
  2246. get_identifier (el->sym->result->name),
  2247. gfc_sym_type (el->sym->result), &chain);
  2248. }
  2249. /* Finish off the type. */
  2250. gfc_finish_type (type);
  2251. TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
  2252. return type;
  2253. }
  2254. /* Create a "fn spec" based on the formal arguments;
  2255. cf. create_function_arglist. */
  2256. static tree
  2257. create_fn_spec (gfc_symbol *sym, tree fntype)
  2258. {
  2259. char spec[150];
  2260. size_t spec_len;
  2261. gfc_formal_arglist *f;
  2262. tree tmp;
  2263. memset (&spec, 0, sizeof (spec));
  2264. spec[0] = '.';
  2265. spec_len = 1;
  2266. if (sym->attr.entry_master)
  2267. spec[spec_len++] = 'R';
  2268. if (gfc_return_by_reference (sym))
  2269. {
  2270. gfc_symbol *result = sym->result ? sym->result : sym;
  2271. if (result->attr.pointer || sym->attr.proc_pointer)
  2272. spec[spec_len++] = '.';
  2273. else
  2274. spec[spec_len++] = 'w';
  2275. if (sym->ts.type == BT_CHARACTER)
  2276. spec[spec_len++] = 'R';
  2277. }
  2278. for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
  2279. if (spec_len < sizeof (spec))
  2280. {
  2281. if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
  2282. || f->sym->attr.external || f->sym->attr.cray_pointer
  2283. || (f->sym->ts.type == BT_DERIVED
  2284. && (f->sym->ts.u.derived->attr.proc_pointer_comp
  2285. || f->sym->ts.u.derived->attr.pointer_comp))
  2286. || (f->sym->ts.type == BT_CLASS
  2287. && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
  2288. || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
  2289. spec[spec_len++] = '.';
  2290. else if (f->sym->attr.intent == INTENT_IN)
  2291. spec[spec_len++] = 'r';
  2292. else if (f->sym)
  2293. spec[spec_len++] = 'w';
  2294. }
  2295. tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
  2296. tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
  2297. return build_type_attribute_variant (fntype, tmp);
  2298. }
  2299. tree
  2300. gfc_get_function_type (gfc_symbol * sym)
  2301. {
  2302. tree type;
  2303. vec<tree, va_gc> *typelist = NULL;
  2304. gfc_formal_arglist *f;
  2305. gfc_symbol *arg;
  2306. int alternate_return = 0;
  2307. bool is_varargs = true;
  2308. /* Make sure this symbol is a function, a subroutine or the main
  2309. program. */
  2310. gcc_assert (sym->attr.flavor == FL_PROCEDURE
  2311. || sym->attr.flavor == FL_PROGRAM);
  2312. /* To avoid recursing infinitely on recursive types, we use error_mark_node
  2313. so that they can be detected here and handled further down. */
  2314. if (sym->backend_decl == NULL)
  2315. sym->backend_decl = error_mark_node;
  2316. else if (sym->backend_decl == error_mark_node)
  2317. goto arg_type_list_done;
  2318. else if (sym->attr.proc_pointer)
  2319. return TREE_TYPE (TREE_TYPE (sym->backend_decl));
  2320. else
  2321. return TREE_TYPE (sym->backend_decl);
  2322. if (sym->attr.entry_master)
  2323. /* Additional parameter for selecting an entry point. */
  2324. vec_safe_push (typelist, gfc_array_index_type);
  2325. if (sym->result)
  2326. arg = sym->result;
  2327. else
  2328. arg = sym;
  2329. if (arg->ts.type == BT_CHARACTER)
  2330. gfc_conv_const_charlen (arg->ts.u.cl);
  2331. /* Some functions we use an extra parameter for the return value. */
  2332. if (gfc_return_by_reference (sym))
  2333. {
  2334. type = gfc_sym_type (arg);
  2335. if (arg->ts.type == BT_COMPLEX
  2336. || arg->attr.dimension
  2337. || arg->ts.type == BT_CHARACTER)
  2338. type = build_reference_type (type);
  2339. vec_safe_push (typelist, type);
  2340. if (arg->ts.type == BT_CHARACTER)
  2341. {
  2342. if (!arg->ts.deferred)
  2343. /* Transfer by value. */
  2344. vec_safe_push (typelist, gfc_charlen_type_node);
  2345. else
  2346. /* Deferred character lengths are transferred by reference
  2347. so that the value can be returned. */
  2348. vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
  2349. }
  2350. }
  2351. /* Build the argument types for the function. */
  2352. for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
  2353. {
  2354. arg = f->sym;
  2355. if (arg)
  2356. {
  2357. /* Evaluate constant character lengths here so that they can be
  2358. included in the type. */
  2359. if (arg->ts.type == BT_CHARACTER)
  2360. gfc_conv_const_charlen (arg->ts.u.cl);
  2361. if (arg->attr.flavor == FL_PROCEDURE)
  2362. {
  2363. type = gfc_get_function_type (arg);
  2364. type = build_pointer_type (type);
  2365. }
  2366. else
  2367. type = gfc_sym_type (arg);
  2368. /* Parameter Passing Convention
  2369. We currently pass all parameters by reference.
  2370. Parameters with INTENT(IN) could be passed by value.
  2371. The problem arises if a function is called via an implicit
  2372. prototype. In this situation the INTENT is not known.
  2373. For this reason all parameters to global functions must be
  2374. passed by reference. Passing by value would potentially
  2375. generate bad code. Worse there would be no way of telling that
  2376. this code was bad, except that it would give incorrect results.
  2377. Contained procedures could pass by value as these are never
  2378. used without an explicit interface, and cannot be passed as
  2379. actual parameters for a dummy procedure. */
  2380. vec_safe_push (typelist, type);
  2381. }
  2382. else
  2383. {
  2384. if (sym->attr.subroutine)
  2385. alternate_return = 1;
  2386. }
  2387. }
  2388. /* Add hidden string length parameters. */
  2389. for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
  2390. {
  2391. arg = f->sym;
  2392. if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
  2393. {
  2394. if (!arg->ts.deferred)
  2395. /* Transfer by value. */
  2396. type = gfc_charlen_type_node;
  2397. else
  2398. /* Deferred character lengths are transferred by reference
  2399. so that the value can be returned. */
  2400. type = build_pointer_type (gfc_charlen_type_node);
  2401. vec_safe_push (typelist, type);
  2402. }
  2403. }
  2404. if (!vec_safe_is_empty (typelist)
  2405. || sym->attr.is_main_program
  2406. || sym->attr.if_source != IFSRC_UNKNOWN)
  2407. is_varargs = false;
  2408. if (sym->backend_decl == error_mark_node)
  2409. sym->backend_decl = NULL_TREE;
  2410. arg_type_list_done:
  2411. if (alternate_return)
  2412. type = integer_type_node;
  2413. else if (!sym->attr.function || gfc_return_by_reference (sym))
  2414. type = void_type_node;
  2415. else if (sym->attr.mixed_entry_master)
  2416. type = gfc_get_mixed_entry_union (sym->ns);
  2417. else if (flag_f2c && sym->ts.type == BT_REAL
  2418. && sym->ts.kind == gfc_default_real_kind
  2419. && !sym->attr.always_explicit)
  2420. {
  2421. /* Special case: f2c calling conventions require that (scalar)
  2422. default REAL functions return the C type double instead. f2c
  2423. compatibility is only an issue with functions that don't
  2424. require an explicit interface, as only these could be
  2425. implemented in Fortran 77. */
  2426. sym->ts.kind = gfc_default_double_kind;
  2427. type = gfc_typenode_for_spec (&sym->ts);
  2428. sym->ts.kind = gfc_default_real_kind;
  2429. }
  2430. else if (sym->result && sym->result->attr.proc_pointer)
  2431. /* Procedure pointer return values. */
  2432. {
  2433. if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
  2434. {
  2435. /* Unset proc_pointer as gfc_get_function_type
  2436. is called recursively. */
  2437. sym->result->attr.proc_pointer = 0;
  2438. type = build_pointer_type (gfc_get_function_type (sym->result));
  2439. sym->result->attr.proc_pointer = 1;
  2440. }
  2441. else
  2442. type = gfc_sym_type (sym->result);
  2443. }
  2444. else
  2445. type = gfc_sym_type (sym);
  2446. if (is_varargs)
  2447. type = build_varargs_function_type_vec (type, typelist);
  2448. else
  2449. type = build_function_type_vec (type, typelist);
  2450. type = create_fn_spec (sym, type);
  2451. return type;
  2452. }
  2453. /* Language hooks for middle-end access to type nodes. */
  2454. /* Return an integer type with BITS bits of precision,
  2455. that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
  2456. tree
  2457. gfc_type_for_size (unsigned bits, int unsignedp)
  2458. {
  2459. if (!unsignedp)
  2460. {
  2461. int i;
  2462. for (i = 0; i <= MAX_INT_KINDS; ++i)
  2463. {
  2464. tree type = gfc_integer_types[i];
  2465. if (type && bits == TYPE_PRECISION (type))
  2466. return type;
  2467. }
  2468. /* Handle TImode as a special case because it is used by some backends
  2469. (e.g. ARM) even though it is not available for normal use. */
  2470. #if HOST_BITS_PER_WIDE_INT >= 64
  2471. if (bits == TYPE_PRECISION (intTI_type_node))
  2472. return intTI_type_node;
  2473. #endif
  2474. if (bits <= TYPE_PRECISION (intQI_type_node))
  2475. return intQI_type_node;
  2476. if (bits <= TYPE_PRECISION (intHI_type_node))
  2477. return intHI_type_node;
  2478. if (bits <= TYPE_PRECISION (intSI_type_node))
  2479. return intSI_type_node;
  2480. if (bits <= TYPE_PRECISION (intDI_type_node))
  2481. return intDI_type_node;
  2482. if (bits <= TYPE_PRECISION (intTI_type_node))
  2483. return intTI_type_node;
  2484. }
  2485. else
  2486. {
  2487. if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
  2488. return unsigned_intQI_type_node;
  2489. if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
  2490. return unsigned_intHI_type_node;
  2491. if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
  2492. return unsigned_intSI_type_node;
  2493. if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
  2494. return unsigned_intDI_type_node;
  2495. if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
  2496. return unsigned_intTI_type_node;
  2497. }
  2498. return NULL_TREE;
  2499. }
  2500. /* Return a data type that has machine mode MODE. If the mode is an
  2501. integer, then UNSIGNEDP selects between signed and unsigned types. */
  2502. tree
  2503. gfc_type_for_mode (machine_mode mode, int unsignedp)
  2504. {
  2505. int i;
  2506. tree *base;
  2507. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  2508. base = gfc_real_types;
  2509. else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
  2510. base = gfc_complex_types;
  2511. else if (SCALAR_INT_MODE_P (mode))
  2512. {
  2513. tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
  2514. return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
  2515. }
  2516. else if (VECTOR_MODE_P (mode))
  2517. {
  2518. machine_mode inner_mode = GET_MODE_INNER (mode);
  2519. tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
  2520. if (inner_type != NULL_TREE)
  2521. return build_vector_type_for_mode (inner_type, mode);
  2522. return NULL_TREE;
  2523. }
  2524. else
  2525. return NULL_TREE;
  2526. for (i = 0; i <= MAX_REAL_KINDS; ++i)
  2527. {
  2528. tree type = base[i];
  2529. if (type && mode == TYPE_MODE (type))
  2530. return type;
  2531. }
  2532. return NULL_TREE;
  2533. }
  2534. /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
  2535. in that case. */
  2536. bool
  2537. gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
  2538. {
  2539. int rank, dim;
  2540. bool indirect = false;
  2541. tree etype, ptype, field, t, base_decl;
  2542. tree data_off, dim_off, dim_size, elem_size;
  2543. tree lower_suboff, upper_suboff, stride_suboff;
  2544. if (! GFC_DESCRIPTOR_TYPE_P (type))
  2545. {
  2546. if (! POINTER_TYPE_P (type))
  2547. return false;
  2548. type = TREE_TYPE (type);
  2549. if (! GFC_DESCRIPTOR_TYPE_P (type))
  2550. return false;
  2551. indirect = true;
  2552. }
  2553. rank = GFC_TYPE_ARRAY_RANK (type);
  2554. if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
  2555. return false;
  2556. etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
  2557. gcc_assert (POINTER_TYPE_P (etype));
  2558. etype = TREE_TYPE (etype);
  2559. /* If the type is not a scalar coarray. */
  2560. if (TREE_CODE (etype) == ARRAY_TYPE)
  2561. etype = TREE_TYPE (etype);
  2562. /* Can't handle variable sized elements yet. */
  2563. if (int_size_in_bytes (etype) <= 0)
  2564. return false;
  2565. /* Nor non-constant lower bounds in assumed shape arrays. */
  2566. if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
  2567. || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
  2568. {
  2569. for (dim = 0; dim < rank; dim++)
  2570. if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
  2571. || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
  2572. return false;
  2573. }
  2574. memset (info, '\0', sizeof (*info));
  2575. info->ndimensions = rank;
  2576. info->ordering = array_descr_ordering_column_major;
  2577. info->element_type = etype;
  2578. ptype = build_pointer_type (gfc_array_index_type);
  2579. base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
  2580. if (!base_decl)
  2581. {
  2582. base_decl = make_node (DEBUG_EXPR_DECL);
  2583. DECL_ARTIFICIAL (base_decl) = 1;
  2584. TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
  2585. DECL_MODE (base_decl) = TYPE_MODE (TREE_TYPE (base_decl));
  2586. GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
  2587. }
  2588. info->base_decl = base_decl;
  2589. if (indirect)
  2590. base_decl = build1 (INDIRECT_REF, ptype, base_decl);
  2591. if (GFC_TYPE_ARRAY_SPAN (type))
  2592. elem_size = GFC_TYPE_ARRAY_SPAN (type);
  2593. else
  2594. elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
  2595. field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
  2596. data_off = byte_position (field);
  2597. field = DECL_CHAIN (field);
  2598. field = DECL_CHAIN (field);
  2599. field = DECL_CHAIN (field);
  2600. dim_off = byte_position (field);
  2601. dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
  2602. field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
  2603. stride_suboff = byte_position (field);
  2604. field = DECL_CHAIN (field);
  2605. lower_suboff = byte_position (field);
  2606. field = DECL_CHAIN (field);
  2607. upper_suboff = byte_position (field);
  2608. t = base_decl;
  2609. if (!integer_zerop (data_off))
  2610. t = fold_build_pointer_plus (t, data_off);
  2611. t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
  2612. info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
  2613. if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
  2614. info->allocated = build2 (NE_EXPR, boolean_type_node,
  2615. info->data_location, null_pointer_node);
  2616. else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
  2617. || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
  2618. info->associated = build2 (NE_EXPR, boolean_type_node,
  2619. info->data_location, null_pointer_node);
  2620. for (dim = 0; dim < rank; dim++)
  2621. {
  2622. t = fold_build_pointer_plus (base_decl,
  2623. size_binop (PLUS_EXPR,
  2624. dim_off, lower_suboff));
  2625. t = build1 (INDIRECT_REF, gfc_array_index_type, t);
  2626. info->dimen[dim].lower_bound = t;
  2627. t = fold_build_pointer_plus (base_decl,
  2628. size_binop (PLUS_EXPR,
  2629. dim_off, upper_suboff));
  2630. t = build1 (INDIRECT_REF, gfc_array_index_type, t);
  2631. info->dimen[dim].upper_bound = t;
  2632. if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
  2633. || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
  2634. {
  2635. /* Assumed shape arrays have known lower bounds. */
  2636. info->dimen[dim].upper_bound
  2637. = build2 (MINUS_EXPR, gfc_array_index_type,
  2638. info->dimen[dim].upper_bound,
  2639. info->dimen[dim].lower_bound);
  2640. info->dimen[dim].lower_bound
  2641. = fold_convert (gfc_array_index_type,
  2642. GFC_TYPE_ARRAY_LBOUND (type, dim));
  2643. info->dimen[dim].upper_bound
  2644. = build2 (PLUS_EXPR, gfc_array_index_type,
  2645. info->dimen[dim].lower_bound,
  2646. info->dimen[dim].upper_bound);
  2647. }
  2648. t = fold_build_pointer_plus (base_decl,
  2649. size_binop (PLUS_EXPR,
  2650. dim_off, stride_suboff));
  2651. t = build1 (INDIRECT_REF, gfc_array_index_type, t);
  2652. t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
  2653. info->dimen[dim].stride = t;
  2654. dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
  2655. }
  2656. return true;
  2657. }
  2658. /* Create a type to handle vector subscripts for coarray library calls. It
  2659. has the form:
  2660. struct caf_vector_t {
  2661. size_t nvec; // size of the vector
  2662. union {
  2663. struct {
  2664. void *vector;
  2665. int kind;
  2666. } v;
  2667. struct {
  2668. ptrdiff_t lower_bound;
  2669. ptrdiff_t upper_bound;
  2670. ptrdiff_t stride;
  2671. } triplet;
  2672. } u;
  2673. }
  2674. where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
  2675. size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
  2676. tree
  2677. gfc_get_caf_vector_type (int dim)
  2678. {
  2679. static tree vector_types[GFC_MAX_DIMENSIONS];
  2680. static tree vec_type = NULL_TREE;
  2681. tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
  2682. if (vector_types[dim-1] != NULL_TREE)
  2683. return vector_types[dim-1];
  2684. if (vec_type == NULL_TREE)
  2685. {
  2686. chain = 0;
  2687. vect_struct_type = make_node (RECORD_TYPE);
  2688. tmp = gfc_add_field_to_struct_1 (vect_struct_type,
  2689. get_identifier ("vector"),
  2690. pvoid_type_node, &chain);
  2691. TREE_NO_WARNING (tmp) = 1;
  2692. tmp = gfc_add_field_to_struct_1 (vect_struct_type,
  2693. get_identifier ("kind"),
  2694. integer_type_node, &chain);
  2695. TREE_NO_WARNING (tmp) = 1;
  2696. gfc_finish_type (vect_struct_type);
  2697. chain = 0;
  2698. triplet_struct_type = make_node (RECORD_TYPE);
  2699. tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
  2700. get_identifier ("lower_bound"),
  2701. gfc_array_index_type, &chain);
  2702. TREE_NO_WARNING (tmp) = 1;
  2703. tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
  2704. get_identifier ("upper_bound"),
  2705. gfc_array_index_type, &chain);
  2706. TREE_NO_WARNING (tmp) = 1;
  2707. tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
  2708. gfc_array_index_type, &chain);
  2709. TREE_NO_WARNING (tmp) = 1;
  2710. gfc_finish_type (triplet_struct_type);
  2711. chain = 0;
  2712. union_type = make_node (UNION_TYPE);
  2713. tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
  2714. vect_struct_type, &chain);
  2715. TREE_NO_WARNING (tmp) = 1;
  2716. tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
  2717. triplet_struct_type, &chain);
  2718. TREE_NO_WARNING (tmp) = 1;
  2719. gfc_finish_type (union_type);
  2720. chain = 0;
  2721. vec_type = make_node (RECORD_TYPE);
  2722. tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
  2723. size_type_node, &chain);
  2724. TREE_NO_WARNING (tmp) = 1;
  2725. tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
  2726. union_type, &chain);
  2727. TREE_NO_WARNING (tmp) = 1;
  2728. gfc_finish_type (vec_type);
  2729. TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
  2730. }
  2731. tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
  2732. gfc_rank_cst[dim-1]);
  2733. vector_types[dim-1] = build_array_type (vec_type, tmp);
  2734. return vector_types[dim-1];
  2735. }
  2736. #include "gt-fortran-trans-types.h"