expr.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043
  1. /* Routines for manipulation of expression nodes.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Contributed by Andy Vaught
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "flags.h"
  20. #include "gfortran.h"
  21. #include "arith.h"
  22. #include "match.h"
  23. #include "target-memory.h" /* for gfc_convert_boz */
  24. #include "constructor.h"
  25. /* The following set of functions provide access to gfc_expr* of
  26. various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE.
  27. There are two functions available elsewhere that provide
  28. slightly different flavours of variables. Namely:
  29. expr.c (gfc_get_variable_expr)
  30. symbol.c (gfc_lval_expr_from_sym)
  31. TODO: Merge these functions, if possible. */
  32. /* Get a new expression node. */
  33. gfc_expr *
  34. gfc_get_expr (void)
  35. {
  36. gfc_expr *e;
  37. e = XCNEW (gfc_expr);
  38. gfc_clear_ts (&e->ts);
  39. e->shape = NULL;
  40. e->ref = NULL;
  41. e->symtree = NULL;
  42. return e;
  43. }
  44. /* Get a new expression node that is an array constructor
  45. of given type and kind. */
  46. gfc_expr *
  47. gfc_get_array_expr (bt type, int kind, locus *where)
  48. {
  49. gfc_expr *e;
  50. e = gfc_get_expr ();
  51. e->expr_type = EXPR_ARRAY;
  52. e->value.constructor = NULL;
  53. e->rank = 1;
  54. e->shape = NULL;
  55. e->ts.type = type;
  56. e->ts.kind = kind;
  57. if (where)
  58. e->where = *where;
  59. return e;
  60. }
  61. /* Get a new expression node that is the NULL expression. */
  62. gfc_expr *
  63. gfc_get_null_expr (locus *where)
  64. {
  65. gfc_expr *e;
  66. e = gfc_get_expr ();
  67. e->expr_type = EXPR_NULL;
  68. e->ts.type = BT_UNKNOWN;
  69. if (where)
  70. e->where = *where;
  71. return e;
  72. }
  73. /* Get a new expression node that is an operator expression node. */
  74. gfc_expr *
  75. gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
  76. gfc_expr *op1, gfc_expr *op2)
  77. {
  78. gfc_expr *e;
  79. e = gfc_get_expr ();
  80. e->expr_type = EXPR_OP;
  81. e->value.op.op = op;
  82. e->value.op.op1 = op1;
  83. e->value.op.op2 = op2;
  84. if (where)
  85. e->where = *where;
  86. return e;
  87. }
  88. /* Get a new expression node that is an structure constructor
  89. of given type and kind. */
  90. gfc_expr *
  91. gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
  92. {
  93. gfc_expr *e;
  94. e = gfc_get_expr ();
  95. e->expr_type = EXPR_STRUCTURE;
  96. e->value.constructor = NULL;
  97. e->ts.type = type;
  98. e->ts.kind = kind;
  99. if (where)
  100. e->where = *where;
  101. return e;
  102. }
  103. /* Get a new expression node that is an constant of given type and kind. */
  104. gfc_expr *
  105. gfc_get_constant_expr (bt type, int kind, locus *where)
  106. {
  107. gfc_expr *e;
  108. if (!where)
  109. gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
  110. "NULL");
  111. e = gfc_get_expr ();
  112. e->expr_type = EXPR_CONSTANT;
  113. e->ts.type = type;
  114. e->ts.kind = kind;
  115. e->where = *where;
  116. switch (type)
  117. {
  118. case BT_INTEGER:
  119. mpz_init (e->value.integer);
  120. break;
  121. case BT_REAL:
  122. gfc_set_model_kind (kind);
  123. mpfr_init (e->value.real);
  124. break;
  125. case BT_COMPLEX:
  126. gfc_set_model_kind (kind);
  127. mpc_init2 (e->value.complex, mpfr_get_default_prec());
  128. break;
  129. default:
  130. break;
  131. }
  132. return e;
  133. }
  134. /* Get a new expression node that is an string constant.
  135. If no string is passed, a string of len is allocated,
  136. blanked and null-terminated. */
  137. gfc_expr *
  138. gfc_get_character_expr (int kind, locus *where, const char *src, int len)
  139. {
  140. gfc_expr *e;
  141. gfc_char_t *dest;
  142. if (!src)
  143. {
  144. dest = gfc_get_wide_string (len + 1);
  145. gfc_wide_memset (dest, ' ', len);
  146. dest[len] = '\0';
  147. }
  148. else
  149. dest = gfc_char_to_widechar (src);
  150. e = gfc_get_constant_expr (BT_CHARACTER, kind,
  151. where ? where : &gfc_current_locus);
  152. e->value.character.string = dest;
  153. e->value.character.length = len;
  154. return e;
  155. }
  156. /* Get a new expression node that is an integer constant. */
  157. gfc_expr *
  158. gfc_get_int_expr (int kind, locus *where, int value)
  159. {
  160. gfc_expr *p;
  161. p = gfc_get_constant_expr (BT_INTEGER, kind,
  162. where ? where : &gfc_current_locus);
  163. mpz_set_si (p->value.integer, value);
  164. return p;
  165. }
  166. /* Get a new expression node that is a logical constant. */
  167. gfc_expr *
  168. gfc_get_logical_expr (int kind, locus *where, bool value)
  169. {
  170. gfc_expr *p;
  171. p = gfc_get_constant_expr (BT_LOGICAL, kind,
  172. where ? where : &gfc_current_locus);
  173. p->value.logical = value;
  174. return p;
  175. }
  176. gfc_expr *
  177. gfc_get_iokind_expr (locus *where, io_kind k)
  178. {
  179. gfc_expr *e;
  180. /* Set the types to something compatible with iokind. This is needed to
  181. get through gfc_free_expr later since iokind really has no Basic Type,
  182. BT, of its own. */
  183. e = gfc_get_expr ();
  184. e->expr_type = EXPR_CONSTANT;
  185. e->ts.type = BT_LOGICAL;
  186. e->value.iokind = k;
  187. e->where = *where;
  188. return e;
  189. }
  190. /* Given an expression pointer, return a copy of the expression. This
  191. subroutine is recursive. */
  192. gfc_expr *
  193. gfc_copy_expr (gfc_expr *p)
  194. {
  195. gfc_expr *q;
  196. gfc_char_t *s;
  197. char *c;
  198. if (p == NULL)
  199. return NULL;
  200. q = gfc_get_expr ();
  201. *q = *p;
  202. switch (q->expr_type)
  203. {
  204. case EXPR_SUBSTRING:
  205. s = gfc_get_wide_string (p->value.character.length + 1);
  206. q->value.character.string = s;
  207. memcpy (s, p->value.character.string,
  208. (p->value.character.length + 1) * sizeof (gfc_char_t));
  209. break;
  210. case EXPR_CONSTANT:
  211. /* Copy target representation, if it exists. */
  212. if (p->representation.string)
  213. {
  214. c = XCNEWVEC (char, p->representation.length + 1);
  215. q->representation.string = c;
  216. memcpy (c, p->representation.string, (p->representation.length + 1));
  217. }
  218. /* Copy the values of any pointer components of p->value. */
  219. switch (q->ts.type)
  220. {
  221. case BT_INTEGER:
  222. mpz_init_set (q->value.integer, p->value.integer);
  223. break;
  224. case BT_REAL:
  225. gfc_set_model_kind (q->ts.kind);
  226. mpfr_init (q->value.real);
  227. mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
  228. break;
  229. case BT_COMPLEX:
  230. gfc_set_model_kind (q->ts.kind);
  231. mpc_init2 (q->value.complex, mpfr_get_default_prec());
  232. mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
  233. break;
  234. case BT_CHARACTER:
  235. if (p->representation.string)
  236. q->value.character.string
  237. = gfc_char_to_widechar (q->representation.string);
  238. else
  239. {
  240. s = gfc_get_wide_string (p->value.character.length + 1);
  241. q->value.character.string = s;
  242. /* This is the case for the C_NULL_CHAR named constant. */
  243. if (p->value.character.length == 0
  244. && (p->ts.is_c_interop || p->ts.is_iso_c))
  245. {
  246. *s = '\0';
  247. /* Need to set the length to 1 to make sure the NUL
  248. terminator is copied. */
  249. q->value.character.length = 1;
  250. }
  251. else
  252. memcpy (s, p->value.character.string,
  253. (p->value.character.length + 1) * sizeof (gfc_char_t));
  254. }
  255. break;
  256. case BT_HOLLERITH:
  257. case BT_LOGICAL:
  258. case BT_DERIVED:
  259. case BT_CLASS:
  260. case BT_ASSUMED:
  261. break; /* Already done. */
  262. case BT_PROCEDURE:
  263. case BT_VOID:
  264. /* Should never be reached. */
  265. case BT_UNKNOWN:
  266. gfc_internal_error ("gfc_copy_expr(): Bad expr node");
  267. /* Not reached. */
  268. }
  269. break;
  270. case EXPR_OP:
  271. switch (q->value.op.op)
  272. {
  273. case INTRINSIC_NOT:
  274. case INTRINSIC_PARENTHESES:
  275. case INTRINSIC_UPLUS:
  276. case INTRINSIC_UMINUS:
  277. q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
  278. break;
  279. default: /* Binary operators. */
  280. q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
  281. q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
  282. break;
  283. }
  284. break;
  285. case EXPR_FUNCTION:
  286. q->value.function.actual =
  287. gfc_copy_actual_arglist (p->value.function.actual);
  288. break;
  289. case EXPR_COMPCALL:
  290. case EXPR_PPC:
  291. q->value.compcall.actual =
  292. gfc_copy_actual_arglist (p->value.compcall.actual);
  293. q->value.compcall.tbp = p->value.compcall.tbp;
  294. break;
  295. case EXPR_STRUCTURE:
  296. case EXPR_ARRAY:
  297. q->value.constructor = gfc_constructor_copy (p->value.constructor);
  298. break;
  299. case EXPR_VARIABLE:
  300. case EXPR_NULL:
  301. break;
  302. }
  303. q->shape = gfc_copy_shape (p->shape, p->rank);
  304. q->ref = gfc_copy_ref (p->ref);
  305. return q;
  306. }
  307. void
  308. gfc_clear_shape (mpz_t *shape, int rank)
  309. {
  310. int i;
  311. for (i = 0; i < rank; i++)
  312. mpz_clear (shape[i]);
  313. }
  314. void
  315. gfc_free_shape (mpz_t **shape, int rank)
  316. {
  317. if (*shape == NULL)
  318. return;
  319. gfc_clear_shape (*shape, rank);
  320. free (*shape);
  321. *shape = NULL;
  322. }
  323. /* Workhorse function for gfc_free_expr() that frees everything
  324. beneath an expression node, but not the node itself. This is
  325. useful when we want to simplify a node and replace it with
  326. something else or the expression node belongs to another structure. */
  327. static void
  328. free_expr0 (gfc_expr *e)
  329. {
  330. switch (e->expr_type)
  331. {
  332. case EXPR_CONSTANT:
  333. /* Free any parts of the value that need freeing. */
  334. switch (e->ts.type)
  335. {
  336. case BT_INTEGER:
  337. mpz_clear (e->value.integer);
  338. break;
  339. case BT_REAL:
  340. mpfr_clear (e->value.real);
  341. break;
  342. case BT_CHARACTER:
  343. free (e->value.character.string);
  344. break;
  345. case BT_COMPLEX:
  346. mpc_clear (e->value.complex);
  347. break;
  348. default:
  349. break;
  350. }
  351. /* Free the representation. */
  352. free (e->representation.string);
  353. break;
  354. case EXPR_OP:
  355. if (e->value.op.op1 != NULL)
  356. gfc_free_expr (e->value.op.op1);
  357. if (e->value.op.op2 != NULL)
  358. gfc_free_expr (e->value.op.op2);
  359. break;
  360. case EXPR_FUNCTION:
  361. gfc_free_actual_arglist (e->value.function.actual);
  362. break;
  363. case EXPR_COMPCALL:
  364. case EXPR_PPC:
  365. gfc_free_actual_arglist (e->value.compcall.actual);
  366. break;
  367. case EXPR_VARIABLE:
  368. break;
  369. case EXPR_ARRAY:
  370. case EXPR_STRUCTURE:
  371. gfc_constructor_free (e->value.constructor);
  372. break;
  373. case EXPR_SUBSTRING:
  374. free (e->value.character.string);
  375. break;
  376. case EXPR_NULL:
  377. break;
  378. default:
  379. gfc_internal_error ("free_expr0(): Bad expr type");
  380. }
  381. /* Free a shape array. */
  382. gfc_free_shape (&e->shape, e->rank);
  383. gfc_free_ref_list (e->ref);
  384. memset (e, '\0', sizeof (gfc_expr));
  385. }
  386. /* Free an expression node and everything beneath it. */
  387. void
  388. gfc_free_expr (gfc_expr *e)
  389. {
  390. if (e == NULL)
  391. return;
  392. free_expr0 (e);
  393. free (e);
  394. }
  395. /* Free an argument list and everything below it. */
  396. void
  397. gfc_free_actual_arglist (gfc_actual_arglist *a1)
  398. {
  399. gfc_actual_arglist *a2;
  400. while (a1)
  401. {
  402. a2 = a1->next;
  403. gfc_free_expr (a1->expr);
  404. free (a1);
  405. a1 = a2;
  406. }
  407. }
  408. /* Copy an arglist structure and all of the arguments. */
  409. gfc_actual_arglist *
  410. gfc_copy_actual_arglist (gfc_actual_arglist *p)
  411. {
  412. gfc_actual_arglist *head, *tail, *new_arg;
  413. head = tail = NULL;
  414. for (; p; p = p->next)
  415. {
  416. new_arg = gfc_get_actual_arglist ();
  417. *new_arg = *p;
  418. new_arg->expr = gfc_copy_expr (p->expr);
  419. new_arg->next = NULL;
  420. if (head == NULL)
  421. head = new_arg;
  422. else
  423. tail->next = new_arg;
  424. tail = new_arg;
  425. }
  426. return head;
  427. }
  428. /* Free a list of reference structures. */
  429. void
  430. gfc_free_ref_list (gfc_ref *p)
  431. {
  432. gfc_ref *q;
  433. int i;
  434. for (; p; p = q)
  435. {
  436. q = p->next;
  437. switch (p->type)
  438. {
  439. case REF_ARRAY:
  440. for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
  441. {
  442. gfc_free_expr (p->u.ar.start[i]);
  443. gfc_free_expr (p->u.ar.end[i]);
  444. gfc_free_expr (p->u.ar.stride[i]);
  445. }
  446. break;
  447. case REF_SUBSTRING:
  448. gfc_free_expr (p->u.ss.start);
  449. gfc_free_expr (p->u.ss.end);
  450. break;
  451. case REF_COMPONENT:
  452. break;
  453. }
  454. free (p);
  455. }
  456. }
  457. /* Graft the *src expression onto the *dest subexpression. */
  458. void
  459. gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
  460. {
  461. free_expr0 (dest);
  462. *dest = *src;
  463. free (src);
  464. }
  465. /* Try to extract an integer constant from the passed expression node.
  466. Returns an error message or NULL if the result is set. It is
  467. tempting to generate an error and return true or false, but
  468. failure is OK for some callers. */
  469. const char *
  470. gfc_extract_int (gfc_expr *expr, int *result)
  471. {
  472. if (expr->expr_type != EXPR_CONSTANT)
  473. return _("Constant expression required at %C");
  474. if (expr->ts.type != BT_INTEGER)
  475. return _("Integer expression required at %C");
  476. if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
  477. || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
  478. {
  479. return _("Integer value too large in expression at %C");
  480. }
  481. *result = (int) mpz_get_si (expr->value.integer);
  482. return NULL;
  483. }
  484. /* Recursively copy a list of reference structures. */
  485. gfc_ref *
  486. gfc_copy_ref (gfc_ref *src)
  487. {
  488. gfc_array_ref *ar;
  489. gfc_ref *dest;
  490. if (src == NULL)
  491. return NULL;
  492. dest = gfc_get_ref ();
  493. dest->type = src->type;
  494. switch (src->type)
  495. {
  496. case REF_ARRAY:
  497. ar = gfc_copy_array_ref (&src->u.ar);
  498. dest->u.ar = *ar;
  499. free (ar);
  500. break;
  501. case REF_COMPONENT:
  502. dest->u.c = src->u.c;
  503. break;
  504. case REF_SUBSTRING:
  505. dest->u.ss = src->u.ss;
  506. dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
  507. dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
  508. break;
  509. }
  510. dest->next = gfc_copy_ref (src->next);
  511. return dest;
  512. }
  513. /* Detect whether an expression has any vector index array references. */
  514. int
  515. gfc_has_vector_index (gfc_expr *e)
  516. {
  517. gfc_ref *ref;
  518. int i;
  519. for (ref = e->ref; ref; ref = ref->next)
  520. if (ref->type == REF_ARRAY)
  521. for (i = 0; i < ref->u.ar.dimen; i++)
  522. if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
  523. return 1;
  524. return 0;
  525. }
  526. /* Copy a shape array. */
  527. mpz_t *
  528. gfc_copy_shape (mpz_t *shape, int rank)
  529. {
  530. mpz_t *new_shape;
  531. int n;
  532. if (shape == NULL)
  533. return NULL;
  534. new_shape = gfc_get_shape (rank);
  535. for (n = 0; n < rank; n++)
  536. mpz_init_set (new_shape[n], shape[n]);
  537. return new_shape;
  538. }
  539. /* Copy a shape array excluding dimension N, where N is an integer
  540. constant expression. Dimensions are numbered in Fortran style --
  541. starting with ONE.
  542. So, if the original shape array contains R elements
  543. { s1 ... sN-1 sN sN+1 ... sR-1 sR}
  544. the result contains R-1 elements:
  545. { s1 ... sN-1 sN+1 ... sR-1}
  546. If anything goes wrong -- N is not a constant, its value is out
  547. of range -- or anything else, just returns NULL. */
  548. mpz_t *
  549. gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
  550. {
  551. mpz_t *new_shape, *s;
  552. int i, n;
  553. if (shape == NULL
  554. || rank <= 1
  555. || dim == NULL
  556. || dim->expr_type != EXPR_CONSTANT
  557. || dim->ts.type != BT_INTEGER)
  558. return NULL;
  559. n = mpz_get_si (dim->value.integer);
  560. n--; /* Convert to zero based index. */
  561. if (n < 0 || n >= rank)
  562. return NULL;
  563. s = new_shape = gfc_get_shape (rank - 1);
  564. for (i = 0; i < rank; i++)
  565. {
  566. if (i == n)
  567. continue;
  568. mpz_init_set (*s, shape[i]);
  569. s++;
  570. }
  571. return new_shape;
  572. }
  573. /* Return the maximum kind of two expressions. In general, higher
  574. kind numbers mean more precision for numeric types. */
  575. int
  576. gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
  577. {
  578. return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
  579. }
  580. /* Returns nonzero if the type is numeric, zero otherwise. */
  581. static int
  582. numeric_type (bt type)
  583. {
  584. return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
  585. }
  586. /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
  587. int
  588. gfc_numeric_ts (gfc_typespec *ts)
  589. {
  590. return numeric_type (ts->type);
  591. }
  592. /* Return an expression node with an optional argument list attached.
  593. A variable number of gfc_expr pointers are strung together in an
  594. argument list with a NULL pointer terminating the list. */
  595. gfc_expr *
  596. gfc_build_conversion (gfc_expr *e)
  597. {
  598. gfc_expr *p;
  599. p = gfc_get_expr ();
  600. p->expr_type = EXPR_FUNCTION;
  601. p->symtree = NULL;
  602. p->value.function.actual = NULL;
  603. p->value.function.actual = gfc_get_actual_arglist ();
  604. p->value.function.actual->expr = e;
  605. return p;
  606. }
  607. /* Given an expression node with some sort of numeric binary
  608. expression, insert type conversions required to make the operands
  609. have the same type. Conversion warnings are disabled if wconversion
  610. is set to 0.
  611. The exception is that the operands of an exponential don't have to
  612. have the same type. If possible, the base is promoted to the type
  613. of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
  614. 1.0**2 stays as it is. */
  615. void
  616. gfc_type_convert_binary (gfc_expr *e, int wconversion)
  617. {
  618. gfc_expr *op1, *op2;
  619. op1 = e->value.op.op1;
  620. op2 = e->value.op.op2;
  621. if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
  622. {
  623. gfc_clear_ts (&e->ts);
  624. return;
  625. }
  626. /* Kind conversions of same type. */
  627. if (op1->ts.type == op2->ts.type)
  628. {
  629. if (op1->ts.kind == op2->ts.kind)
  630. {
  631. /* No type conversions. */
  632. e->ts = op1->ts;
  633. goto done;
  634. }
  635. if (op1->ts.kind > op2->ts.kind)
  636. gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
  637. else
  638. gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
  639. e->ts = op1->ts;
  640. goto done;
  641. }
  642. /* Integer combined with real or complex. */
  643. if (op2->ts.type == BT_INTEGER)
  644. {
  645. e->ts = op1->ts;
  646. /* Special case for ** operator. */
  647. if (e->value.op.op == INTRINSIC_POWER)
  648. goto done;
  649. gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
  650. goto done;
  651. }
  652. if (op1->ts.type == BT_INTEGER)
  653. {
  654. e->ts = op2->ts;
  655. gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
  656. goto done;
  657. }
  658. /* Real combined with complex. */
  659. e->ts.type = BT_COMPLEX;
  660. if (op1->ts.kind > op2->ts.kind)
  661. e->ts.kind = op1->ts.kind;
  662. else
  663. e->ts.kind = op2->ts.kind;
  664. if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
  665. gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
  666. if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
  667. gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
  668. done:
  669. return;
  670. }
  671. /* Function to determine if an expression is constant or not. This
  672. function expects that the expression has already been simplified. */
  673. int
  674. gfc_is_constant_expr (gfc_expr *e)
  675. {
  676. gfc_constructor *c;
  677. gfc_actual_arglist *arg;
  678. gfc_symbol *sym;
  679. if (e == NULL)
  680. return 1;
  681. switch (e->expr_type)
  682. {
  683. case EXPR_OP:
  684. return (gfc_is_constant_expr (e->value.op.op1)
  685. && (e->value.op.op2 == NULL
  686. || gfc_is_constant_expr (e->value.op.op2)));
  687. case EXPR_VARIABLE:
  688. return 0;
  689. case EXPR_FUNCTION:
  690. case EXPR_PPC:
  691. case EXPR_COMPCALL:
  692. gcc_assert (e->symtree || e->value.function.esym
  693. || e->value.function.isym);
  694. /* Call to intrinsic with at least one argument. */
  695. if (e->value.function.isym && e->value.function.actual)
  696. {
  697. for (arg = e->value.function.actual; arg; arg = arg->next)
  698. if (!gfc_is_constant_expr (arg->expr))
  699. return 0;
  700. }
  701. /* Specification functions are constant. */
  702. /* F95, 7.1.6.2; F2003, 7.1.7 */
  703. sym = NULL;
  704. if (e->symtree)
  705. sym = e->symtree->n.sym;
  706. if (e->value.function.esym)
  707. sym = e->value.function.esym;
  708. if (sym
  709. && sym->attr.function
  710. && sym->attr.pure
  711. && !sym->attr.intrinsic
  712. && !sym->attr.recursive
  713. && sym->attr.proc != PROC_INTERNAL
  714. && sym->attr.proc != PROC_ST_FUNCTION
  715. && sym->attr.proc != PROC_UNKNOWN
  716. && gfc_sym_get_dummy_args (sym) == NULL)
  717. return 1;
  718. if (e->value.function.isym
  719. && (e->value.function.isym->elemental
  720. || e->value.function.isym->pure
  721. || e->value.function.isym->inquiry
  722. || e->value.function.isym->transformational))
  723. return 1;
  724. return 0;
  725. case EXPR_CONSTANT:
  726. case EXPR_NULL:
  727. return 1;
  728. case EXPR_SUBSTRING:
  729. return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
  730. && gfc_is_constant_expr (e->ref->u.ss.end));
  731. case EXPR_ARRAY:
  732. case EXPR_STRUCTURE:
  733. c = gfc_constructor_first (e->value.constructor);
  734. if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
  735. return gfc_constant_ac (e);
  736. for (; c; c = gfc_constructor_next (c))
  737. if (!gfc_is_constant_expr (c->expr))
  738. return 0;
  739. return 1;
  740. default:
  741. gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
  742. return 0;
  743. }
  744. }
  745. /* Is true if an array reference is followed by a component or substring
  746. reference. */
  747. bool
  748. is_subref_array (gfc_expr * e)
  749. {
  750. gfc_ref * ref;
  751. bool seen_array;
  752. if (e->expr_type != EXPR_VARIABLE)
  753. return false;
  754. if (e->symtree->n.sym->attr.subref_array_pointer)
  755. return true;
  756. seen_array = false;
  757. for (ref = e->ref; ref; ref = ref->next)
  758. {
  759. if (ref->type == REF_ARRAY
  760. && ref->u.ar.type != AR_ELEMENT)
  761. seen_array = true;
  762. if (seen_array
  763. && ref->type != REF_ARRAY)
  764. return seen_array;
  765. }
  766. return false;
  767. }
  768. /* Try to collapse intrinsic expressions. */
  769. static bool
  770. simplify_intrinsic_op (gfc_expr *p, int type)
  771. {
  772. gfc_intrinsic_op op;
  773. gfc_expr *op1, *op2, *result;
  774. if (p->value.op.op == INTRINSIC_USER)
  775. return true;
  776. op1 = p->value.op.op1;
  777. op2 = p->value.op.op2;
  778. op = p->value.op.op;
  779. if (!gfc_simplify_expr (op1, type))
  780. return false;
  781. if (!gfc_simplify_expr (op2, type))
  782. return false;
  783. if (!gfc_is_constant_expr (op1)
  784. || (op2 != NULL && !gfc_is_constant_expr (op2)))
  785. return true;
  786. /* Rip p apart. */
  787. p->value.op.op1 = NULL;
  788. p->value.op.op2 = NULL;
  789. switch (op)
  790. {
  791. case INTRINSIC_PARENTHESES:
  792. result = gfc_parentheses (op1);
  793. break;
  794. case INTRINSIC_UPLUS:
  795. result = gfc_uplus (op1);
  796. break;
  797. case INTRINSIC_UMINUS:
  798. result = gfc_uminus (op1);
  799. break;
  800. case INTRINSIC_PLUS:
  801. result = gfc_add (op1, op2);
  802. break;
  803. case INTRINSIC_MINUS:
  804. result = gfc_subtract (op1, op2);
  805. break;
  806. case INTRINSIC_TIMES:
  807. result = gfc_multiply (op1, op2);
  808. break;
  809. case INTRINSIC_DIVIDE:
  810. result = gfc_divide (op1, op2);
  811. break;
  812. case INTRINSIC_POWER:
  813. result = gfc_power (op1, op2);
  814. break;
  815. case INTRINSIC_CONCAT:
  816. result = gfc_concat (op1, op2);
  817. break;
  818. case INTRINSIC_EQ:
  819. case INTRINSIC_EQ_OS:
  820. result = gfc_eq (op1, op2, op);
  821. break;
  822. case INTRINSIC_NE:
  823. case INTRINSIC_NE_OS:
  824. result = gfc_ne (op1, op2, op);
  825. break;
  826. case INTRINSIC_GT:
  827. case INTRINSIC_GT_OS:
  828. result = gfc_gt (op1, op2, op);
  829. break;
  830. case INTRINSIC_GE:
  831. case INTRINSIC_GE_OS:
  832. result = gfc_ge (op1, op2, op);
  833. break;
  834. case INTRINSIC_LT:
  835. case INTRINSIC_LT_OS:
  836. result = gfc_lt (op1, op2, op);
  837. break;
  838. case INTRINSIC_LE:
  839. case INTRINSIC_LE_OS:
  840. result = gfc_le (op1, op2, op);
  841. break;
  842. case INTRINSIC_NOT:
  843. result = gfc_not (op1);
  844. break;
  845. case INTRINSIC_AND:
  846. result = gfc_and (op1, op2);
  847. break;
  848. case INTRINSIC_OR:
  849. result = gfc_or (op1, op2);
  850. break;
  851. case INTRINSIC_EQV:
  852. result = gfc_eqv (op1, op2);
  853. break;
  854. case INTRINSIC_NEQV:
  855. result = gfc_neqv (op1, op2);
  856. break;
  857. default:
  858. gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
  859. }
  860. if (result == NULL)
  861. {
  862. gfc_free_expr (op1);
  863. gfc_free_expr (op2);
  864. return false;
  865. }
  866. result->rank = p->rank;
  867. result->where = p->where;
  868. gfc_replace_expr (p, result);
  869. return true;
  870. }
  871. /* Subroutine to simplify constructor expressions. Mutually recursive
  872. with gfc_simplify_expr(). */
  873. static bool
  874. simplify_constructor (gfc_constructor_base base, int type)
  875. {
  876. gfc_constructor *c;
  877. gfc_expr *p;
  878. for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
  879. {
  880. if (c->iterator
  881. && (!gfc_simplify_expr(c->iterator->start, type)
  882. || !gfc_simplify_expr (c->iterator->end, type)
  883. || !gfc_simplify_expr (c->iterator->step, type)))
  884. return false;
  885. if (c->expr)
  886. {
  887. /* Try and simplify a copy. Replace the original if successful
  888. but keep going through the constructor at all costs. Not
  889. doing so can make a dog's dinner of complicated things. */
  890. p = gfc_copy_expr (c->expr);
  891. if (!gfc_simplify_expr (p, type))
  892. {
  893. gfc_free_expr (p);
  894. continue;
  895. }
  896. gfc_replace_expr (c->expr, p);
  897. }
  898. }
  899. return true;
  900. }
  901. /* Pull a single array element out of an array constructor. */
  902. static bool
  903. find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
  904. gfc_constructor **rval)
  905. {
  906. unsigned long nelemen;
  907. int i;
  908. mpz_t delta;
  909. mpz_t offset;
  910. mpz_t span;
  911. mpz_t tmp;
  912. gfc_constructor *cons;
  913. gfc_expr *e;
  914. bool t;
  915. t = true;
  916. e = NULL;
  917. mpz_init_set_ui (offset, 0);
  918. mpz_init (delta);
  919. mpz_init (tmp);
  920. mpz_init_set_ui (span, 1);
  921. for (i = 0; i < ar->dimen; i++)
  922. {
  923. if (!gfc_reduce_init_expr (ar->as->lower[i])
  924. || !gfc_reduce_init_expr (ar->as->upper[i]))
  925. {
  926. t = false;
  927. cons = NULL;
  928. goto depart;
  929. }
  930. e = ar->start[i];
  931. if (e->expr_type != EXPR_CONSTANT)
  932. {
  933. cons = NULL;
  934. goto depart;
  935. }
  936. gcc_assert (ar->as->upper[i]->expr_type == EXPR_CONSTANT
  937. && ar->as->lower[i]->expr_type == EXPR_CONSTANT);
  938. /* Check the bounds. */
  939. if ((ar->as->upper[i]
  940. && mpz_cmp (e->value.integer,
  941. ar->as->upper[i]->value.integer) > 0)
  942. || (mpz_cmp (e->value.integer,
  943. ar->as->lower[i]->value.integer) < 0))
  944. {
  945. gfc_error ("Index in dimension %d is out of bounds "
  946. "at %L", i + 1, &ar->c_where[i]);
  947. cons = NULL;
  948. t = false;
  949. goto depart;
  950. }
  951. mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
  952. mpz_mul (delta, delta, span);
  953. mpz_add (offset, offset, delta);
  954. mpz_set_ui (tmp, 1);
  955. mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
  956. mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
  957. mpz_mul (span, span, tmp);
  958. }
  959. for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
  960. cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
  961. {
  962. if (cons->iterator)
  963. {
  964. cons = NULL;
  965. goto depart;
  966. }
  967. }
  968. depart:
  969. mpz_clear (delta);
  970. mpz_clear (offset);
  971. mpz_clear (span);
  972. mpz_clear (tmp);
  973. *rval = cons;
  974. return t;
  975. }
  976. /* Find a component of a structure constructor. */
  977. static gfc_constructor *
  978. find_component_ref (gfc_constructor_base base, gfc_ref *ref)
  979. {
  980. gfc_component *pick = ref->u.c.component;
  981. gfc_constructor *c = gfc_constructor_first (base);
  982. gfc_symbol *dt = ref->u.c.sym;
  983. int ext = dt->attr.extension;
  984. /* For extended types, check if the desired component is in one of the
  985. * parent types. */
  986. while (ext > 0 && gfc_find_component (dt->components->ts.u.derived,
  987. pick->name, true, true))
  988. {
  989. dt = dt->components->ts.u.derived;
  990. c = gfc_constructor_first (c->expr->value.constructor);
  991. ext--;
  992. }
  993. gfc_component *comp = dt->components;
  994. while (comp != pick)
  995. {
  996. comp = comp->next;
  997. c = gfc_constructor_next (c);
  998. }
  999. return c;
  1000. }
  1001. /* Replace an expression with the contents of a constructor, removing
  1002. the subobject reference in the process. */
  1003. static void
  1004. remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
  1005. {
  1006. gfc_expr *e;
  1007. if (cons)
  1008. {
  1009. e = cons->expr;
  1010. cons->expr = NULL;
  1011. }
  1012. else
  1013. e = gfc_copy_expr (p);
  1014. e->ref = p->ref->next;
  1015. p->ref->next = NULL;
  1016. gfc_replace_expr (p, e);
  1017. }
  1018. /* Pull an array section out of an array constructor. */
  1019. static bool
  1020. find_array_section (gfc_expr *expr, gfc_ref *ref)
  1021. {
  1022. int idx;
  1023. int rank;
  1024. int d;
  1025. int shape_i;
  1026. int limit;
  1027. long unsigned one = 1;
  1028. bool incr_ctr;
  1029. mpz_t start[GFC_MAX_DIMENSIONS];
  1030. mpz_t end[GFC_MAX_DIMENSIONS];
  1031. mpz_t stride[GFC_MAX_DIMENSIONS];
  1032. mpz_t delta[GFC_MAX_DIMENSIONS];
  1033. mpz_t ctr[GFC_MAX_DIMENSIONS];
  1034. mpz_t delta_mpz;
  1035. mpz_t tmp_mpz;
  1036. mpz_t nelts;
  1037. mpz_t ptr;
  1038. gfc_constructor_base base;
  1039. gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
  1040. gfc_expr *begin;
  1041. gfc_expr *finish;
  1042. gfc_expr *step;
  1043. gfc_expr *upper;
  1044. gfc_expr *lower;
  1045. bool t;
  1046. t = true;
  1047. base = expr->value.constructor;
  1048. expr->value.constructor = NULL;
  1049. rank = ref->u.ar.as->rank;
  1050. if (expr->shape == NULL)
  1051. expr->shape = gfc_get_shape (rank);
  1052. mpz_init_set_ui (delta_mpz, one);
  1053. mpz_init_set_ui (nelts, one);
  1054. mpz_init (tmp_mpz);
  1055. /* Do the initialization now, so that we can cleanup without
  1056. keeping track of where we were. */
  1057. for (d = 0; d < rank; d++)
  1058. {
  1059. mpz_init (delta[d]);
  1060. mpz_init (start[d]);
  1061. mpz_init (end[d]);
  1062. mpz_init (ctr[d]);
  1063. mpz_init (stride[d]);
  1064. vecsub[d] = NULL;
  1065. }
  1066. /* Build the counters to clock through the array reference. */
  1067. shape_i = 0;
  1068. for (d = 0; d < rank; d++)
  1069. {
  1070. /* Make this stretch of code easier on the eye! */
  1071. begin = ref->u.ar.start[d];
  1072. finish = ref->u.ar.end[d];
  1073. step = ref->u.ar.stride[d];
  1074. lower = ref->u.ar.as->lower[d];
  1075. upper = ref->u.ar.as->upper[d];
  1076. if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
  1077. {
  1078. gfc_constructor *ci;
  1079. gcc_assert (begin);
  1080. if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
  1081. {
  1082. t = false;
  1083. goto cleanup;
  1084. }
  1085. gcc_assert (begin->rank == 1);
  1086. /* Zero-sized arrays have no shape and no elements, stop early. */
  1087. if (!begin->shape)
  1088. {
  1089. mpz_init_set_ui (nelts, 0);
  1090. break;
  1091. }
  1092. vecsub[d] = gfc_constructor_first (begin->value.constructor);
  1093. mpz_set (ctr[d], vecsub[d]->expr->value.integer);
  1094. mpz_mul (nelts, nelts, begin->shape[0]);
  1095. mpz_set (expr->shape[shape_i++], begin->shape[0]);
  1096. /* Check bounds. */
  1097. for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
  1098. {
  1099. if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
  1100. || mpz_cmp (ci->expr->value.integer,
  1101. lower->value.integer) < 0)
  1102. {
  1103. gfc_error ("index in dimension %d is out of bounds "
  1104. "at %L", d + 1, &ref->u.ar.c_where[d]);
  1105. t = false;
  1106. goto cleanup;
  1107. }
  1108. }
  1109. }
  1110. else
  1111. {
  1112. if ((begin && begin->expr_type != EXPR_CONSTANT)
  1113. || (finish && finish->expr_type != EXPR_CONSTANT)
  1114. || (step && step->expr_type != EXPR_CONSTANT))
  1115. {
  1116. t = false;
  1117. goto cleanup;
  1118. }
  1119. /* Obtain the stride. */
  1120. if (step)
  1121. mpz_set (stride[d], step->value.integer);
  1122. else
  1123. mpz_set_ui (stride[d], one);
  1124. if (mpz_cmp_ui (stride[d], 0) == 0)
  1125. mpz_set_ui (stride[d], one);
  1126. /* Obtain the start value for the index. */
  1127. if (begin)
  1128. mpz_set (start[d], begin->value.integer);
  1129. else
  1130. mpz_set (start[d], lower->value.integer);
  1131. mpz_set (ctr[d], start[d]);
  1132. /* Obtain the end value for the index. */
  1133. if (finish)
  1134. mpz_set (end[d], finish->value.integer);
  1135. else
  1136. mpz_set (end[d], upper->value.integer);
  1137. /* Separate 'if' because elements sometimes arrive with
  1138. non-null end. */
  1139. if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
  1140. mpz_set (end [d], begin->value.integer);
  1141. /* Check the bounds. */
  1142. if (mpz_cmp (ctr[d], upper->value.integer) > 0
  1143. || mpz_cmp (end[d], upper->value.integer) > 0
  1144. || mpz_cmp (ctr[d], lower->value.integer) < 0
  1145. || mpz_cmp (end[d], lower->value.integer) < 0)
  1146. {
  1147. gfc_error ("index in dimension %d is out of bounds "
  1148. "at %L", d + 1, &ref->u.ar.c_where[d]);
  1149. t = false;
  1150. goto cleanup;
  1151. }
  1152. /* Calculate the number of elements and the shape. */
  1153. mpz_set (tmp_mpz, stride[d]);
  1154. mpz_add (tmp_mpz, end[d], tmp_mpz);
  1155. mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
  1156. mpz_div (tmp_mpz, tmp_mpz, stride[d]);
  1157. mpz_mul (nelts, nelts, tmp_mpz);
  1158. /* An element reference reduces the rank of the expression; don't
  1159. add anything to the shape array. */
  1160. if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
  1161. mpz_set (expr->shape[shape_i++], tmp_mpz);
  1162. }
  1163. /* Calculate the 'stride' (=delta) for conversion of the
  1164. counter values into the index along the constructor. */
  1165. mpz_set (delta[d], delta_mpz);
  1166. mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
  1167. mpz_add_ui (tmp_mpz, tmp_mpz, one);
  1168. mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
  1169. }
  1170. mpz_init (ptr);
  1171. cons = gfc_constructor_first (base);
  1172. /* Now clock through the array reference, calculating the index in
  1173. the source constructor and transferring the elements to the new
  1174. constructor. */
  1175. for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
  1176. {
  1177. mpz_init_set_ui (ptr, 0);
  1178. incr_ctr = true;
  1179. for (d = 0; d < rank; d++)
  1180. {
  1181. mpz_set (tmp_mpz, ctr[d]);
  1182. mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
  1183. mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
  1184. mpz_add (ptr, ptr, tmp_mpz);
  1185. if (!incr_ctr) continue;
  1186. if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
  1187. {
  1188. gcc_assert(vecsub[d]);
  1189. if (!gfc_constructor_next (vecsub[d]))
  1190. vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
  1191. else
  1192. {
  1193. vecsub[d] = gfc_constructor_next (vecsub[d]);
  1194. incr_ctr = false;
  1195. }
  1196. mpz_set (ctr[d], vecsub[d]->expr->value.integer);
  1197. }
  1198. else
  1199. {
  1200. mpz_add (ctr[d], ctr[d], stride[d]);
  1201. if (mpz_cmp_ui (stride[d], 0) > 0
  1202. ? mpz_cmp (ctr[d], end[d]) > 0
  1203. : mpz_cmp (ctr[d], end[d]) < 0)
  1204. mpz_set (ctr[d], start[d]);
  1205. else
  1206. incr_ctr = false;
  1207. }
  1208. }
  1209. limit = mpz_get_ui (ptr);
  1210. if (limit >= flag_max_array_constructor)
  1211. {
  1212. gfc_error ("The number of elements in the array constructor "
  1213. "at %L requires an increase of the allowed %d "
  1214. "upper limit. See -fmax-array-constructor "
  1215. "option", &expr->where, flag_max_array_constructor);
  1216. return false;
  1217. }
  1218. cons = gfc_constructor_lookup (base, limit);
  1219. gcc_assert (cons);
  1220. gfc_constructor_append_expr (&expr->value.constructor,
  1221. gfc_copy_expr (cons->expr), NULL);
  1222. }
  1223. mpz_clear (ptr);
  1224. cleanup:
  1225. mpz_clear (delta_mpz);
  1226. mpz_clear (tmp_mpz);
  1227. mpz_clear (nelts);
  1228. for (d = 0; d < rank; d++)
  1229. {
  1230. mpz_clear (delta[d]);
  1231. mpz_clear (start[d]);
  1232. mpz_clear (end[d]);
  1233. mpz_clear (ctr[d]);
  1234. mpz_clear (stride[d]);
  1235. }
  1236. gfc_constructor_free (base);
  1237. return t;
  1238. }
  1239. /* Pull a substring out of an expression. */
  1240. static bool
  1241. find_substring_ref (gfc_expr *p, gfc_expr **newp)
  1242. {
  1243. int end;
  1244. int start;
  1245. int length;
  1246. gfc_char_t *chr;
  1247. if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
  1248. || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
  1249. return false;
  1250. *newp = gfc_copy_expr (p);
  1251. free ((*newp)->value.character.string);
  1252. end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
  1253. start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
  1254. length = end - start + 1;
  1255. chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
  1256. (*newp)->value.character.length = length;
  1257. memcpy (chr, &p->value.character.string[start - 1],
  1258. length * sizeof (gfc_char_t));
  1259. chr[length] = '\0';
  1260. return true;
  1261. }
  1262. /* Simplify a subobject reference of a constructor. This occurs when
  1263. parameter variable values are substituted. */
  1264. static bool
  1265. simplify_const_ref (gfc_expr *p)
  1266. {
  1267. gfc_constructor *cons, *c;
  1268. gfc_expr *newp;
  1269. gfc_ref *last_ref;
  1270. while (p->ref)
  1271. {
  1272. switch (p->ref->type)
  1273. {
  1274. case REF_ARRAY:
  1275. switch (p->ref->u.ar.type)
  1276. {
  1277. case AR_ELEMENT:
  1278. /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
  1279. will generate this. */
  1280. if (p->expr_type != EXPR_ARRAY)
  1281. {
  1282. remove_subobject_ref (p, NULL);
  1283. break;
  1284. }
  1285. if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
  1286. return false;
  1287. if (!cons)
  1288. return true;
  1289. remove_subobject_ref (p, cons);
  1290. break;
  1291. case AR_SECTION:
  1292. if (!find_array_section (p, p->ref))
  1293. return false;
  1294. p->ref->u.ar.type = AR_FULL;
  1295. /* Fall through. */
  1296. case AR_FULL:
  1297. if (p->ref->next != NULL
  1298. && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
  1299. {
  1300. for (c = gfc_constructor_first (p->value.constructor);
  1301. c; c = gfc_constructor_next (c))
  1302. {
  1303. c->expr->ref = gfc_copy_ref (p->ref->next);
  1304. if (!simplify_const_ref (c->expr))
  1305. return false;
  1306. }
  1307. if (p->ts.type == BT_DERIVED
  1308. && p->ref->next
  1309. && (c = gfc_constructor_first (p->value.constructor)))
  1310. {
  1311. /* There may have been component references. */
  1312. p->ts = c->expr->ts;
  1313. }
  1314. last_ref = p->ref;
  1315. for (; last_ref->next; last_ref = last_ref->next) {};
  1316. if (p->ts.type == BT_CHARACTER
  1317. && last_ref->type == REF_SUBSTRING)
  1318. {
  1319. /* If this is a CHARACTER array and we possibly took
  1320. a substring out of it, update the type-spec's
  1321. character length according to the first element
  1322. (as all should have the same length). */
  1323. int string_len;
  1324. if ((c = gfc_constructor_first (p->value.constructor)))
  1325. {
  1326. const gfc_expr* first = c->expr;
  1327. gcc_assert (first->expr_type == EXPR_CONSTANT);
  1328. gcc_assert (first->ts.type == BT_CHARACTER);
  1329. string_len = first->value.character.length;
  1330. }
  1331. else
  1332. string_len = 0;
  1333. if (!p->ts.u.cl)
  1334. p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
  1335. NULL);
  1336. else
  1337. gfc_free_expr (p->ts.u.cl->length);
  1338. p->ts.u.cl->length
  1339. = gfc_get_int_expr (gfc_default_integer_kind,
  1340. NULL, string_len);
  1341. }
  1342. }
  1343. gfc_free_ref_list (p->ref);
  1344. p->ref = NULL;
  1345. break;
  1346. default:
  1347. return true;
  1348. }
  1349. break;
  1350. case REF_COMPONENT:
  1351. cons = find_component_ref (p->value.constructor, p->ref);
  1352. remove_subobject_ref (p, cons);
  1353. break;
  1354. case REF_SUBSTRING:
  1355. if (!find_substring_ref (p, &newp))
  1356. return false;
  1357. gfc_replace_expr (p, newp);
  1358. gfc_free_ref_list (p->ref);
  1359. p->ref = NULL;
  1360. break;
  1361. }
  1362. }
  1363. return true;
  1364. }
  1365. /* Simplify a chain of references. */
  1366. static bool
  1367. simplify_ref_chain (gfc_ref *ref, int type)
  1368. {
  1369. int n;
  1370. for (; ref; ref = ref->next)
  1371. {
  1372. switch (ref->type)
  1373. {
  1374. case REF_ARRAY:
  1375. for (n = 0; n < ref->u.ar.dimen; n++)
  1376. {
  1377. if (!gfc_simplify_expr (ref->u.ar.start[n], type))
  1378. return false;
  1379. if (!gfc_simplify_expr (ref->u.ar.end[n], type))
  1380. return false;
  1381. if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
  1382. return false;
  1383. }
  1384. break;
  1385. case REF_SUBSTRING:
  1386. if (!gfc_simplify_expr (ref->u.ss.start, type))
  1387. return false;
  1388. if (!gfc_simplify_expr (ref->u.ss.end, type))
  1389. return false;
  1390. break;
  1391. default:
  1392. break;
  1393. }
  1394. }
  1395. return true;
  1396. }
  1397. /* Try to substitute the value of a parameter variable. */
  1398. static bool
  1399. simplify_parameter_variable (gfc_expr *p, int type)
  1400. {
  1401. gfc_expr *e;
  1402. bool t;
  1403. e = gfc_copy_expr (p->symtree->n.sym->value);
  1404. if (e == NULL)
  1405. return false;
  1406. e->rank = p->rank;
  1407. /* Do not copy subobject refs for constant. */
  1408. if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
  1409. e->ref = gfc_copy_ref (p->ref);
  1410. t = gfc_simplify_expr (e, type);
  1411. /* Only use the simplification if it eliminated all subobject references. */
  1412. if (t && !e->ref)
  1413. gfc_replace_expr (p, e);
  1414. else
  1415. gfc_free_expr (e);
  1416. return t;
  1417. }
  1418. /* Given an expression, simplify it by collapsing constant
  1419. expressions. Most simplification takes place when the expression
  1420. tree is being constructed. If an intrinsic function is simplified
  1421. at some point, we get called again to collapse the result against
  1422. other constants.
  1423. We work by recursively simplifying expression nodes, simplifying
  1424. intrinsic functions where possible, which can lead to further
  1425. constant collapsing. If an operator has constant operand(s), we
  1426. rip the expression apart, and rebuild it, hoping that it becomes
  1427. something simpler.
  1428. The expression type is defined for:
  1429. 0 Basic expression parsing
  1430. 1 Simplifying array constructors -- will substitute
  1431. iterator values.
  1432. Returns false on error, true otherwise.
  1433. NOTE: Will return true even if the expression can not be simplified. */
  1434. bool
  1435. gfc_simplify_expr (gfc_expr *p, int type)
  1436. {
  1437. gfc_actual_arglist *ap;
  1438. if (p == NULL)
  1439. return true;
  1440. switch (p->expr_type)
  1441. {
  1442. case EXPR_CONSTANT:
  1443. case EXPR_NULL:
  1444. break;
  1445. case EXPR_FUNCTION:
  1446. for (ap = p->value.function.actual; ap; ap = ap->next)
  1447. if (!gfc_simplify_expr (ap->expr, type))
  1448. return false;
  1449. if (p->value.function.isym != NULL
  1450. && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
  1451. return false;
  1452. break;
  1453. case EXPR_SUBSTRING:
  1454. if (!simplify_ref_chain (p->ref, type))
  1455. return false;
  1456. if (gfc_is_constant_expr (p))
  1457. {
  1458. gfc_char_t *s;
  1459. int start, end;
  1460. start = 0;
  1461. if (p->ref && p->ref->u.ss.start)
  1462. {
  1463. gfc_extract_int (p->ref->u.ss.start, &start);
  1464. start--; /* Convert from one-based to zero-based. */
  1465. }
  1466. end = p->value.character.length;
  1467. if (p->ref && p->ref->u.ss.end)
  1468. gfc_extract_int (p->ref->u.ss.end, &end);
  1469. if (end < start)
  1470. end = start;
  1471. s = gfc_get_wide_string (end - start + 2);
  1472. memcpy (s, p->value.character.string + start,
  1473. (end - start) * sizeof (gfc_char_t));
  1474. s[end - start + 1] = '\0'; /* TODO: C-style string. */
  1475. free (p->value.character.string);
  1476. p->value.character.string = s;
  1477. p->value.character.length = end - start;
  1478. p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
  1479. p->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
  1480. NULL,
  1481. p->value.character.length);
  1482. gfc_free_ref_list (p->ref);
  1483. p->ref = NULL;
  1484. p->expr_type = EXPR_CONSTANT;
  1485. }
  1486. break;
  1487. case EXPR_OP:
  1488. if (!simplify_intrinsic_op (p, type))
  1489. return false;
  1490. break;
  1491. case EXPR_VARIABLE:
  1492. /* Only substitute array parameter variables if we are in an
  1493. initialization expression, or we want a subsection. */
  1494. if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
  1495. && (gfc_init_expr_flag || p->ref
  1496. || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
  1497. {
  1498. if (!simplify_parameter_variable (p, type))
  1499. return false;
  1500. break;
  1501. }
  1502. if (type == 1)
  1503. {
  1504. gfc_simplify_iterator_var (p);
  1505. }
  1506. /* Simplify subcomponent references. */
  1507. if (!simplify_ref_chain (p->ref, type))
  1508. return false;
  1509. break;
  1510. case EXPR_STRUCTURE:
  1511. case EXPR_ARRAY:
  1512. if (!simplify_ref_chain (p->ref, type))
  1513. return false;
  1514. if (!simplify_constructor (p->value.constructor, type))
  1515. return false;
  1516. if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
  1517. && p->ref->u.ar.type == AR_FULL)
  1518. gfc_expand_constructor (p, false);
  1519. if (!simplify_const_ref (p))
  1520. return false;
  1521. break;
  1522. case EXPR_COMPCALL:
  1523. case EXPR_PPC:
  1524. break;
  1525. }
  1526. return true;
  1527. }
  1528. /* Returns the type of an expression with the exception that iterator
  1529. variables are automatically integers no matter what else they may
  1530. be declared as. */
  1531. static bt
  1532. et0 (gfc_expr *e)
  1533. {
  1534. if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
  1535. return BT_INTEGER;
  1536. return e->ts.type;
  1537. }
  1538. /* Scalarize an expression for an elemental intrinsic call. */
  1539. static bool
  1540. scalarize_intrinsic_call (gfc_expr *e)
  1541. {
  1542. gfc_actual_arglist *a, *b;
  1543. gfc_constructor_base ctor;
  1544. gfc_constructor *args[5];
  1545. gfc_constructor *ci, *new_ctor;
  1546. gfc_expr *expr, *old;
  1547. int n, i, rank[5], array_arg;
  1548. /* Find which, if any, arguments are arrays. Assume that the old
  1549. expression carries the type information and that the first arg
  1550. that is an array expression carries all the shape information.*/
  1551. n = array_arg = 0;
  1552. a = e->value.function.actual;
  1553. for (; a; a = a->next)
  1554. {
  1555. n++;
  1556. if (!a->expr || a->expr->expr_type != EXPR_ARRAY)
  1557. continue;
  1558. array_arg = n;
  1559. expr = gfc_copy_expr (a->expr);
  1560. break;
  1561. }
  1562. if (!array_arg)
  1563. return false;
  1564. old = gfc_copy_expr (e);
  1565. gfc_constructor_free (expr->value.constructor);
  1566. expr->value.constructor = NULL;
  1567. expr->ts = old->ts;
  1568. expr->where = old->where;
  1569. expr->expr_type = EXPR_ARRAY;
  1570. /* Copy the array argument constructors into an array, with nulls
  1571. for the scalars. */
  1572. n = 0;
  1573. a = old->value.function.actual;
  1574. for (; a; a = a->next)
  1575. {
  1576. /* Check that this is OK for an initialization expression. */
  1577. if (a->expr && !gfc_check_init_expr (a->expr))
  1578. goto cleanup;
  1579. rank[n] = 0;
  1580. if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
  1581. {
  1582. rank[n] = a->expr->rank;
  1583. ctor = a->expr->symtree->n.sym->value->value.constructor;
  1584. args[n] = gfc_constructor_first (ctor);
  1585. }
  1586. else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
  1587. {
  1588. if (a->expr->rank)
  1589. rank[n] = a->expr->rank;
  1590. else
  1591. rank[n] = 1;
  1592. ctor = gfc_constructor_copy (a->expr->value.constructor);
  1593. args[n] = gfc_constructor_first (ctor);
  1594. }
  1595. else
  1596. args[n] = NULL;
  1597. n++;
  1598. }
  1599. /* Using the array argument as the master, step through the array
  1600. calling the function for each element and advancing the array
  1601. constructors together. */
  1602. for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
  1603. {
  1604. new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
  1605. gfc_copy_expr (old), NULL);
  1606. gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
  1607. a = NULL;
  1608. b = old->value.function.actual;
  1609. for (i = 0; i < n; i++)
  1610. {
  1611. if (a == NULL)
  1612. new_ctor->expr->value.function.actual
  1613. = a = gfc_get_actual_arglist ();
  1614. else
  1615. {
  1616. a->next = gfc_get_actual_arglist ();
  1617. a = a->next;
  1618. }
  1619. if (args[i])
  1620. a->expr = gfc_copy_expr (args[i]->expr);
  1621. else
  1622. a->expr = gfc_copy_expr (b->expr);
  1623. b = b->next;
  1624. }
  1625. /* Simplify the function calls. If the simplification fails, the
  1626. error will be flagged up down-stream or the library will deal
  1627. with it. */
  1628. gfc_simplify_expr (new_ctor->expr, 0);
  1629. for (i = 0; i < n; i++)
  1630. if (args[i])
  1631. args[i] = gfc_constructor_next (args[i]);
  1632. for (i = 1; i < n; i++)
  1633. if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
  1634. || (args[i] == NULL && args[array_arg - 1] != NULL)))
  1635. goto compliance;
  1636. }
  1637. free_expr0 (e);
  1638. *e = *expr;
  1639. /* Free "expr" but not the pointers it contains. */
  1640. free (expr);
  1641. gfc_free_expr (old);
  1642. return true;
  1643. compliance:
  1644. gfc_error_now ("elemental function arguments at %C are not compliant");
  1645. cleanup:
  1646. gfc_free_expr (expr);
  1647. gfc_free_expr (old);
  1648. return false;
  1649. }
  1650. static bool
  1651. check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
  1652. {
  1653. gfc_expr *op1 = e->value.op.op1;
  1654. gfc_expr *op2 = e->value.op.op2;
  1655. if (!(*check_function)(op1))
  1656. return false;
  1657. switch (e->value.op.op)
  1658. {
  1659. case INTRINSIC_UPLUS:
  1660. case INTRINSIC_UMINUS:
  1661. if (!numeric_type (et0 (op1)))
  1662. goto not_numeric;
  1663. break;
  1664. case INTRINSIC_EQ:
  1665. case INTRINSIC_EQ_OS:
  1666. case INTRINSIC_NE:
  1667. case INTRINSIC_NE_OS:
  1668. case INTRINSIC_GT:
  1669. case INTRINSIC_GT_OS:
  1670. case INTRINSIC_GE:
  1671. case INTRINSIC_GE_OS:
  1672. case INTRINSIC_LT:
  1673. case INTRINSIC_LT_OS:
  1674. case INTRINSIC_LE:
  1675. case INTRINSIC_LE_OS:
  1676. if (!(*check_function)(op2))
  1677. return false;
  1678. if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
  1679. && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
  1680. {
  1681. gfc_error ("Numeric or CHARACTER operands are required in "
  1682. "expression at %L", &e->where);
  1683. return false;
  1684. }
  1685. break;
  1686. case INTRINSIC_PLUS:
  1687. case INTRINSIC_MINUS:
  1688. case INTRINSIC_TIMES:
  1689. case INTRINSIC_DIVIDE:
  1690. case INTRINSIC_POWER:
  1691. if (!(*check_function)(op2))
  1692. return false;
  1693. if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
  1694. goto not_numeric;
  1695. break;
  1696. case INTRINSIC_CONCAT:
  1697. if (!(*check_function)(op2))
  1698. return false;
  1699. if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
  1700. {
  1701. gfc_error ("Concatenation operator in expression at %L "
  1702. "must have two CHARACTER operands", &op1->where);
  1703. return false;
  1704. }
  1705. if (op1->ts.kind != op2->ts.kind)
  1706. {
  1707. gfc_error ("Concat operator at %L must concatenate strings of the "
  1708. "same kind", &e->where);
  1709. return false;
  1710. }
  1711. break;
  1712. case INTRINSIC_NOT:
  1713. if (et0 (op1) != BT_LOGICAL)
  1714. {
  1715. gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
  1716. "operand", &op1->where);
  1717. return false;
  1718. }
  1719. break;
  1720. case INTRINSIC_AND:
  1721. case INTRINSIC_OR:
  1722. case INTRINSIC_EQV:
  1723. case INTRINSIC_NEQV:
  1724. if (!(*check_function)(op2))
  1725. return false;
  1726. if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
  1727. {
  1728. gfc_error ("LOGICAL operands are required in expression at %L",
  1729. &e->where);
  1730. return false;
  1731. }
  1732. break;
  1733. case INTRINSIC_PARENTHESES:
  1734. break;
  1735. default:
  1736. gfc_error ("Only intrinsic operators can be used in expression at %L",
  1737. &e->where);
  1738. return false;
  1739. }
  1740. return true;
  1741. not_numeric:
  1742. gfc_error ("Numeric operands are required in expression at %L", &e->where);
  1743. return false;
  1744. }
  1745. /* F2003, 7.1.7 (3): In init expression, allocatable components
  1746. must not be data-initialized. */
  1747. static bool
  1748. check_alloc_comp_init (gfc_expr *e)
  1749. {
  1750. gfc_component *comp;
  1751. gfc_constructor *ctor;
  1752. gcc_assert (e->expr_type == EXPR_STRUCTURE);
  1753. gcc_assert (e->ts.type == BT_DERIVED);
  1754. for (comp = e->ts.u.derived->components,
  1755. ctor = gfc_constructor_first (e->value.constructor);
  1756. comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
  1757. {
  1758. if (comp->attr.allocatable && ctor->expr
  1759. && ctor->expr->expr_type != EXPR_NULL)
  1760. {
  1761. gfc_error ("Invalid initialization expression for ALLOCATABLE "
  1762. "component %qs in structure constructor at %L",
  1763. comp->name, &ctor->expr->where);
  1764. return false;
  1765. }
  1766. }
  1767. return true;
  1768. }
  1769. static match
  1770. check_init_expr_arguments (gfc_expr *e)
  1771. {
  1772. gfc_actual_arglist *ap;
  1773. for (ap = e->value.function.actual; ap; ap = ap->next)
  1774. if (!gfc_check_init_expr (ap->expr))
  1775. return MATCH_ERROR;
  1776. return MATCH_YES;
  1777. }
  1778. static bool check_restricted (gfc_expr *);
  1779. /* F95, 7.1.6.1, Initialization expressions, (7)
  1780. F2003, 7.1.7 Initialization expression, (8) */
  1781. static match
  1782. check_inquiry (gfc_expr *e, int not_restricted)
  1783. {
  1784. const char *name;
  1785. const char *const *functions;
  1786. static const char *const inquiry_func_f95[] = {
  1787. "lbound", "shape", "size", "ubound",
  1788. "bit_size", "len", "kind",
  1789. "digits", "epsilon", "huge", "maxexponent", "minexponent",
  1790. "precision", "radix", "range", "tiny",
  1791. NULL
  1792. };
  1793. static const char *const inquiry_func_f2003[] = {
  1794. "lbound", "shape", "size", "ubound",
  1795. "bit_size", "len", "kind",
  1796. "digits", "epsilon", "huge", "maxexponent", "minexponent",
  1797. "precision", "radix", "range", "tiny",
  1798. "new_line", NULL
  1799. };
  1800. int i = 0;
  1801. gfc_actual_arglist *ap;
  1802. if (!e->value.function.isym
  1803. || !e->value.function.isym->inquiry)
  1804. return MATCH_NO;
  1805. /* An undeclared parameter will get us here (PR25018). */
  1806. if (e->symtree == NULL)
  1807. return MATCH_NO;
  1808. if (e->symtree->n.sym->from_intmod)
  1809. {
  1810. if (e->symtree->n.sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
  1811. && e->symtree->n.sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS
  1812. && e->symtree->n.sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION)
  1813. return MATCH_NO;
  1814. if (e->symtree->n.sym->from_intmod == INTMOD_ISO_C_BINDING
  1815. && e->symtree->n.sym->intmod_sym_id != ISOCBINDING_C_SIZEOF)
  1816. return MATCH_NO;
  1817. }
  1818. else
  1819. {
  1820. name = e->symtree->n.sym->name;
  1821. functions = (gfc_option.warn_std & GFC_STD_F2003)
  1822. ? inquiry_func_f2003 : inquiry_func_f95;
  1823. for (i = 0; functions[i]; i++)
  1824. if (strcmp (functions[i], name) == 0)
  1825. break;
  1826. if (functions[i] == NULL)
  1827. return MATCH_ERROR;
  1828. }
  1829. /* At this point we have an inquiry function with a variable argument. The
  1830. type of the variable might be undefined, but we need it now, because the
  1831. arguments of these functions are not allowed to be undefined. */
  1832. for (ap = e->value.function.actual; ap; ap = ap->next)
  1833. {
  1834. if (!ap->expr)
  1835. continue;
  1836. if (ap->expr->ts.type == BT_UNKNOWN)
  1837. {
  1838. if (ap->expr->symtree->n.sym->ts.type == BT_UNKNOWN
  1839. && !gfc_set_default_type (ap->expr->symtree->n.sym, 0, gfc_current_ns))
  1840. return MATCH_NO;
  1841. ap->expr->ts = ap->expr->symtree->n.sym->ts;
  1842. }
  1843. /* Assumed character length will not reduce to a constant expression
  1844. with LEN, as required by the standard. */
  1845. if (i == 5 && not_restricted
  1846. && ap->expr->symtree->n.sym->ts.type == BT_CHARACTER
  1847. && (ap->expr->symtree->n.sym->ts.u.cl->length == NULL
  1848. || ap->expr->symtree->n.sym->ts.deferred))
  1849. {
  1850. gfc_error ("Assumed or deferred character length variable %qs "
  1851. " in constant expression at %L",
  1852. ap->expr->symtree->n.sym->name,
  1853. &ap->expr->where);
  1854. return MATCH_ERROR;
  1855. }
  1856. else if (not_restricted && !gfc_check_init_expr (ap->expr))
  1857. return MATCH_ERROR;
  1858. if (not_restricted == 0
  1859. && ap->expr->expr_type != EXPR_VARIABLE
  1860. && !check_restricted (ap->expr))
  1861. return MATCH_ERROR;
  1862. if (not_restricted == 0
  1863. && ap->expr->expr_type == EXPR_VARIABLE
  1864. && ap->expr->symtree->n.sym->attr.dummy
  1865. && ap->expr->symtree->n.sym->attr.optional)
  1866. return MATCH_NO;
  1867. }
  1868. return MATCH_YES;
  1869. }
  1870. /* F95, 7.1.6.1, Initialization expressions, (5)
  1871. F2003, 7.1.7 Initialization expression, (5) */
  1872. static match
  1873. check_transformational (gfc_expr *e)
  1874. {
  1875. static const char * const trans_func_f95[] = {
  1876. "repeat", "reshape", "selected_int_kind",
  1877. "selected_real_kind", "transfer", "trim", NULL
  1878. };
  1879. static const char * const trans_func_f2003[] = {
  1880. "all", "any", "count", "dot_product", "matmul", "null", "pack",
  1881. "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
  1882. "selected_real_kind", "spread", "sum", "transfer", "transpose",
  1883. "trim", "unpack", NULL
  1884. };
  1885. int i;
  1886. const char *name;
  1887. const char *const *functions;
  1888. if (!e->value.function.isym
  1889. || !e->value.function.isym->transformational)
  1890. return MATCH_NO;
  1891. name = e->symtree->n.sym->name;
  1892. functions = (gfc_option.allow_std & GFC_STD_F2003)
  1893. ? trans_func_f2003 : trans_func_f95;
  1894. /* NULL() is dealt with below. */
  1895. if (strcmp ("null", name) == 0)
  1896. return MATCH_NO;
  1897. for (i = 0; functions[i]; i++)
  1898. if (strcmp (functions[i], name) == 0)
  1899. break;
  1900. if (functions[i] == NULL)
  1901. {
  1902. gfc_error ("transformational intrinsic %qs at %L is not permitted "
  1903. "in an initialization expression", name, &e->where);
  1904. return MATCH_ERROR;
  1905. }
  1906. return check_init_expr_arguments (e);
  1907. }
  1908. /* F95, 7.1.6.1, Initialization expressions, (6)
  1909. F2003, 7.1.7 Initialization expression, (6) */
  1910. static match
  1911. check_null (gfc_expr *e)
  1912. {
  1913. if (strcmp ("null", e->symtree->n.sym->name) != 0)
  1914. return MATCH_NO;
  1915. return check_init_expr_arguments (e);
  1916. }
  1917. static match
  1918. check_elemental (gfc_expr *e)
  1919. {
  1920. if (!e->value.function.isym
  1921. || !e->value.function.isym->elemental)
  1922. return MATCH_NO;
  1923. if (e->ts.type != BT_INTEGER
  1924. && e->ts.type != BT_CHARACTER
  1925. && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard "
  1926. "initialization expression at %L", &e->where))
  1927. return MATCH_ERROR;
  1928. return check_init_expr_arguments (e);
  1929. }
  1930. static match
  1931. check_conversion (gfc_expr *e)
  1932. {
  1933. if (!e->value.function.isym
  1934. || !e->value.function.isym->conversion)
  1935. return MATCH_NO;
  1936. return check_init_expr_arguments (e);
  1937. }
  1938. /* Verify that an expression is an initialization expression. A side
  1939. effect is that the expression tree is reduced to a single constant
  1940. node if all goes well. This would normally happen when the
  1941. expression is constructed but function references are assumed to be
  1942. intrinsics in the context of initialization expressions. If
  1943. false is returned an error message has been generated. */
  1944. bool
  1945. gfc_check_init_expr (gfc_expr *e)
  1946. {
  1947. match m;
  1948. bool t;
  1949. if (e == NULL)
  1950. return true;
  1951. switch (e->expr_type)
  1952. {
  1953. case EXPR_OP:
  1954. t = check_intrinsic_op (e, gfc_check_init_expr);
  1955. if (t)
  1956. t = gfc_simplify_expr (e, 0);
  1957. break;
  1958. case EXPR_FUNCTION:
  1959. t = false;
  1960. {
  1961. gfc_intrinsic_sym* isym;
  1962. gfc_symbol* sym = e->symtree->n.sym;
  1963. /* Special case for IEEE_SELECTED_REAL_KIND from the intrinsic
  1964. module IEEE_ARITHMETIC, which is allowed in initialization
  1965. expressions. */
  1966. if (!strcmp(sym->name, "ieee_selected_real_kind")
  1967. && sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
  1968. {
  1969. gfc_expr *new_expr = gfc_simplify_ieee_selected_real_kind (e);
  1970. if (new_expr)
  1971. {
  1972. gfc_replace_expr (e, new_expr);
  1973. t = true;
  1974. break;
  1975. }
  1976. }
  1977. if (!gfc_is_intrinsic (sym, 0, e->where)
  1978. || (m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)
  1979. {
  1980. gfc_error ("Function %qs in initialization expression at %L "
  1981. "must be an intrinsic function",
  1982. e->symtree->n.sym->name, &e->where);
  1983. break;
  1984. }
  1985. if ((m = check_conversion (e)) == MATCH_NO
  1986. && (m = check_inquiry (e, 1)) == MATCH_NO
  1987. && (m = check_null (e)) == MATCH_NO
  1988. && (m = check_transformational (e)) == MATCH_NO
  1989. && (m = check_elemental (e)) == MATCH_NO)
  1990. {
  1991. gfc_error ("Intrinsic function %qs at %L is not permitted "
  1992. "in an initialization expression",
  1993. e->symtree->n.sym->name, &e->where);
  1994. m = MATCH_ERROR;
  1995. }
  1996. if (m == MATCH_ERROR)
  1997. return false;
  1998. /* Try to scalarize an elemental intrinsic function that has an
  1999. array argument. */
  2000. isym = gfc_find_function (e->symtree->n.sym->name);
  2001. if (isym && isym->elemental
  2002. && (t = scalarize_intrinsic_call(e)))
  2003. break;
  2004. }
  2005. if (m == MATCH_YES)
  2006. t = gfc_simplify_expr (e, 0);
  2007. break;
  2008. case EXPR_VARIABLE:
  2009. t = true;
  2010. if (gfc_check_iter_variable (e))
  2011. break;
  2012. if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
  2013. {
  2014. /* A PARAMETER shall not be used to define itself, i.e.
  2015. REAL, PARAMETER :: x = transfer(0, x)
  2016. is invalid. */
  2017. if (!e->symtree->n.sym->value)
  2018. {
  2019. gfc_error ("PARAMETER %qs is used at %L before its definition "
  2020. "is complete", e->symtree->n.sym->name, &e->where);
  2021. t = false;
  2022. }
  2023. else
  2024. t = simplify_parameter_variable (e, 0);
  2025. break;
  2026. }
  2027. if (gfc_in_match_data ())
  2028. break;
  2029. t = false;
  2030. if (e->symtree->n.sym->as)
  2031. {
  2032. switch (e->symtree->n.sym->as->type)
  2033. {
  2034. case AS_ASSUMED_SIZE:
  2035. gfc_error ("Assumed size array %qs at %L is not permitted "
  2036. "in an initialization expression",
  2037. e->symtree->n.sym->name, &e->where);
  2038. break;
  2039. case AS_ASSUMED_SHAPE:
  2040. gfc_error ("Assumed shape array %qs at %L is not permitted "
  2041. "in an initialization expression",
  2042. e->symtree->n.sym->name, &e->where);
  2043. break;
  2044. case AS_DEFERRED:
  2045. gfc_error ("Deferred array %qs at %L is not permitted "
  2046. "in an initialization expression",
  2047. e->symtree->n.sym->name, &e->where);
  2048. break;
  2049. case AS_EXPLICIT:
  2050. gfc_error ("Array %qs at %L is a variable, which does "
  2051. "not reduce to a constant expression",
  2052. e->symtree->n.sym->name, &e->where);
  2053. break;
  2054. default:
  2055. gcc_unreachable();
  2056. }
  2057. }
  2058. else
  2059. gfc_error ("Parameter %qs at %L has not been declared or is "
  2060. "a variable, which does not reduce to a constant "
  2061. "expression", e->symtree->n.sym->name, &e->where);
  2062. break;
  2063. case EXPR_CONSTANT:
  2064. case EXPR_NULL:
  2065. t = true;
  2066. break;
  2067. case EXPR_SUBSTRING:
  2068. t = gfc_check_init_expr (e->ref->u.ss.start);
  2069. if (!t)
  2070. break;
  2071. t = gfc_check_init_expr (e->ref->u.ss.end);
  2072. if (t)
  2073. t = gfc_simplify_expr (e, 0);
  2074. break;
  2075. case EXPR_STRUCTURE:
  2076. t = e->ts.is_iso_c ? true : false;
  2077. if (t)
  2078. break;
  2079. t = check_alloc_comp_init (e);
  2080. if (!t)
  2081. break;
  2082. t = gfc_check_constructor (e, gfc_check_init_expr);
  2083. if (!t)
  2084. break;
  2085. break;
  2086. case EXPR_ARRAY:
  2087. t = gfc_check_constructor (e, gfc_check_init_expr);
  2088. if (!t)
  2089. break;
  2090. t = gfc_expand_constructor (e, true);
  2091. if (!t)
  2092. break;
  2093. t = gfc_check_constructor_type (e);
  2094. break;
  2095. default:
  2096. gfc_internal_error ("check_init_expr(): Unknown expression type");
  2097. }
  2098. return t;
  2099. }
  2100. /* Reduces a general expression to an initialization expression (a constant).
  2101. This used to be part of gfc_match_init_expr.
  2102. Note that this function doesn't free the given expression on false. */
  2103. bool
  2104. gfc_reduce_init_expr (gfc_expr *expr)
  2105. {
  2106. bool t;
  2107. gfc_init_expr_flag = true;
  2108. t = gfc_resolve_expr (expr);
  2109. if (t)
  2110. t = gfc_check_init_expr (expr);
  2111. gfc_init_expr_flag = false;
  2112. if (!t)
  2113. return false;
  2114. if (expr->expr_type == EXPR_ARRAY)
  2115. {
  2116. if (!gfc_check_constructor_type (expr))
  2117. return false;
  2118. if (!gfc_expand_constructor (expr, true))
  2119. return false;
  2120. }
  2121. return true;
  2122. }
  2123. /* Match an initialization expression. We work by first matching an
  2124. expression, then reducing it to a constant. */
  2125. match
  2126. gfc_match_init_expr (gfc_expr **result)
  2127. {
  2128. gfc_expr *expr;
  2129. match m;
  2130. bool t;
  2131. expr = NULL;
  2132. gfc_init_expr_flag = true;
  2133. m = gfc_match_expr (&expr);
  2134. if (m != MATCH_YES)
  2135. {
  2136. gfc_init_expr_flag = false;
  2137. return m;
  2138. }
  2139. t = gfc_reduce_init_expr (expr);
  2140. if (!t)
  2141. {
  2142. gfc_free_expr (expr);
  2143. gfc_init_expr_flag = false;
  2144. return MATCH_ERROR;
  2145. }
  2146. *result = expr;
  2147. gfc_init_expr_flag = false;
  2148. return MATCH_YES;
  2149. }
  2150. /* Given an actual argument list, test to see that each argument is a
  2151. restricted expression and optionally if the expression type is
  2152. integer or character. */
  2153. static bool
  2154. restricted_args (gfc_actual_arglist *a)
  2155. {
  2156. for (; a; a = a->next)
  2157. {
  2158. if (!check_restricted (a->expr))
  2159. return false;
  2160. }
  2161. return true;
  2162. }
  2163. /************* Restricted/specification expressions *************/
  2164. /* Make sure a non-intrinsic function is a specification function. */
  2165. static bool
  2166. external_spec_function (gfc_expr *e)
  2167. {
  2168. gfc_symbol *f;
  2169. f = e->value.function.esym;
  2170. if (f->attr.proc == PROC_ST_FUNCTION)
  2171. {
  2172. gfc_error ("Specification function %qs at %L cannot be a statement "
  2173. "function", f->name, &e->where);
  2174. return false;
  2175. }
  2176. if (f->attr.proc == PROC_INTERNAL)
  2177. {
  2178. gfc_error ("Specification function %qs at %L cannot be an internal "
  2179. "function", f->name, &e->where);
  2180. return false;
  2181. }
  2182. if (!f->attr.pure && !f->attr.elemental)
  2183. {
  2184. gfc_error ("Specification function %qs at %L must be PURE", f->name,
  2185. &e->where);
  2186. return false;
  2187. }
  2188. if (f->attr.recursive)
  2189. {
  2190. gfc_error ("Specification function %qs at %L cannot be RECURSIVE",
  2191. f->name, &e->where);
  2192. return false;
  2193. }
  2194. return restricted_args (e->value.function.actual);
  2195. }
  2196. /* Check to see that a function reference to an intrinsic is a
  2197. restricted expression. */
  2198. static bool
  2199. restricted_intrinsic (gfc_expr *e)
  2200. {
  2201. /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
  2202. if (check_inquiry (e, 0) == MATCH_YES)
  2203. return true;
  2204. return restricted_args (e->value.function.actual);
  2205. }
  2206. /* Check the expressions of an actual arglist. Used by check_restricted. */
  2207. static bool
  2208. check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*))
  2209. {
  2210. for (; arg; arg = arg->next)
  2211. if (!checker (arg->expr))
  2212. return false;
  2213. return true;
  2214. }
  2215. /* Check the subscription expressions of a reference chain with a checking
  2216. function; used by check_restricted. */
  2217. static bool
  2218. check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
  2219. {
  2220. int dim;
  2221. if (!ref)
  2222. return true;
  2223. switch (ref->type)
  2224. {
  2225. case REF_ARRAY:
  2226. for (dim = 0; dim != ref->u.ar.dimen; ++dim)
  2227. {
  2228. if (!checker (ref->u.ar.start[dim]))
  2229. return false;
  2230. if (!checker (ref->u.ar.end[dim]))
  2231. return false;
  2232. if (!checker (ref->u.ar.stride[dim]))
  2233. return false;
  2234. }
  2235. break;
  2236. case REF_COMPONENT:
  2237. /* Nothing needed, just proceed to next reference. */
  2238. break;
  2239. case REF_SUBSTRING:
  2240. if (!checker (ref->u.ss.start))
  2241. return false;
  2242. if (!checker (ref->u.ss.end))
  2243. return false;
  2244. break;
  2245. default:
  2246. gcc_unreachable ();
  2247. break;
  2248. }
  2249. return check_references (ref->next, checker);
  2250. }
  2251. /* Verify that an expression is a restricted expression. Like its
  2252. cousin check_init_expr(), an error message is generated if we
  2253. return false. */
  2254. static bool
  2255. check_restricted (gfc_expr *e)
  2256. {
  2257. gfc_symbol* sym;
  2258. bool t;
  2259. if (e == NULL)
  2260. return true;
  2261. switch (e->expr_type)
  2262. {
  2263. case EXPR_OP:
  2264. t = check_intrinsic_op (e, check_restricted);
  2265. if (t)
  2266. t = gfc_simplify_expr (e, 0);
  2267. break;
  2268. case EXPR_FUNCTION:
  2269. if (e->value.function.esym)
  2270. {
  2271. t = check_arglist (e->value.function.actual, &check_restricted);
  2272. if (t)
  2273. t = external_spec_function (e);
  2274. }
  2275. else
  2276. {
  2277. if (e->value.function.isym && e->value.function.isym->inquiry)
  2278. t = true;
  2279. else
  2280. t = check_arglist (e->value.function.actual, &check_restricted);
  2281. if (t)
  2282. t = restricted_intrinsic (e);
  2283. }
  2284. break;
  2285. case EXPR_VARIABLE:
  2286. sym = e->symtree->n.sym;
  2287. t = false;
  2288. /* If a dummy argument appears in a context that is valid for a
  2289. restricted expression in an elemental procedure, it will have
  2290. already been simplified away once we get here. Therefore we
  2291. don't need to jump through hoops to distinguish valid from
  2292. invalid cases. */
  2293. if (sym->attr.dummy && sym->ns == gfc_current_ns
  2294. && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
  2295. {
  2296. gfc_error ("Dummy argument %qs not allowed in expression at %L",
  2297. sym->name, &e->where);
  2298. break;
  2299. }
  2300. if (sym->attr.optional)
  2301. {
  2302. gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
  2303. sym->name, &e->where);
  2304. break;
  2305. }
  2306. if (sym->attr.intent == INTENT_OUT)
  2307. {
  2308. gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
  2309. sym->name, &e->where);
  2310. break;
  2311. }
  2312. /* Check reference chain if any. */
  2313. if (!check_references (e->ref, &check_restricted))
  2314. break;
  2315. /* gfc_is_formal_arg broadcasts that a formal argument list is being
  2316. processed in resolve.c(resolve_formal_arglist). This is done so
  2317. that host associated dummy array indices are accepted (PR23446).
  2318. This mechanism also does the same for the specification expressions
  2319. of array-valued functions. */
  2320. if (e->error
  2321. || sym->attr.in_common
  2322. || sym->attr.use_assoc
  2323. || sym->attr.dummy
  2324. || sym->attr.implied_index
  2325. || sym->attr.flavor == FL_PARAMETER
  2326. || (sym->ns && sym->ns == gfc_current_ns->parent)
  2327. || (sym->ns && gfc_current_ns->parent
  2328. && sym->ns == gfc_current_ns->parent->parent)
  2329. || (sym->ns->proc_name != NULL
  2330. && sym->ns->proc_name->attr.flavor == FL_MODULE)
  2331. || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
  2332. {
  2333. t = true;
  2334. break;
  2335. }
  2336. gfc_error ("Variable %qs cannot appear in the expression at %L",
  2337. sym->name, &e->where);
  2338. /* Prevent a repetition of the error. */
  2339. e->error = 1;
  2340. break;
  2341. case EXPR_NULL:
  2342. case EXPR_CONSTANT:
  2343. t = true;
  2344. break;
  2345. case EXPR_SUBSTRING:
  2346. t = gfc_specification_expr (e->ref->u.ss.start);
  2347. if (!t)
  2348. break;
  2349. t = gfc_specification_expr (e->ref->u.ss.end);
  2350. if (t)
  2351. t = gfc_simplify_expr (e, 0);
  2352. break;
  2353. case EXPR_STRUCTURE:
  2354. t = gfc_check_constructor (e, check_restricted);
  2355. break;
  2356. case EXPR_ARRAY:
  2357. t = gfc_check_constructor (e, check_restricted);
  2358. break;
  2359. default:
  2360. gfc_internal_error ("check_restricted(): Unknown expression type");
  2361. }
  2362. return t;
  2363. }
  2364. /* Check to see that an expression is a specification expression. If
  2365. we return false, an error has been generated. */
  2366. bool
  2367. gfc_specification_expr (gfc_expr *e)
  2368. {
  2369. gfc_component *comp;
  2370. if (e == NULL)
  2371. return true;
  2372. if (e->ts.type != BT_INTEGER)
  2373. {
  2374. gfc_error ("Expression at %L must be of INTEGER type, found %s",
  2375. &e->where, gfc_basic_typename (e->ts.type));
  2376. return false;
  2377. }
  2378. comp = gfc_get_proc_ptr_comp (e);
  2379. if (e->expr_type == EXPR_FUNCTION
  2380. && !e->value.function.isym
  2381. && !e->value.function.esym
  2382. && !gfc_pure (e->symtree->n.sym)
  2383. && (!comp || !comp->attr.pure))
  2384. {
  2385. gfc_error ("Function %qs at %L must be PURE",
  2386. e->symtree->n.sym->name, &e->where);
  2387. /* Prevent repeat error messages. */
  2388. e->symtree->n.sym->attr.pure = 1;
  2389. return false;
  2390. }
  2391. if (e->rank != 0)
  2392. {
  2393. gfc_error ("Expression at %L must be scalar", &e->where);
  2394. return false;
  2395. }
  2396. if (!gfc_simplify_expr (e, 0))
  2397. return false;
  2398. return check_restricted (e);
  2399. }
  2400. /************** Expression conformance checks. *************/
  2401. /* Given two expressions, make sure that the arrays are conformable. */
  2402. bool
  2403. gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
  2404. {
  2405. int op1_flag, op2_flag, d;
  2406. mpz_t op1_size, op2_size;
  2407. bool t;
  2408. va_list argp;
  2409. char buffer[240];
  2410. if (op1->rank == 0 || op2->rank == 0)
  2411. return true;
  2412. va_start (argp, optype_msgid);
  2413. vsnprintf (buffer, 240, optype_msgid, argp);
  2414. va_end (argp);
  2415. if (op1->rank != op2->rank)
  2416. {
  2417. gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
  2418. op1->rank, op2->rank, &op1->where);
  2419. return false;
  2420. }
  2421. t = true;
  2422. for (d = 0; d < op1->rank; d++)
  2423. {
  2424. op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
  2425. op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
  2426. if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
  2427. {
  2428. gfc_error ("Different shape for %s at %L on dimension %d "
  2429. "(%d and %d)", _(buffer), &op1->where, d + 1,
  2430. (int) mpz_get_si (op1_size),
  2431. (int) mpz_get_si (op2_size));
  2432. t = false;
  2433. }
  2434. if (op1_flag)
  2435. mpz_clear (op1_size);
  2436. if (op2_flag)
  2437. mpz_clear (op2_size);
  2438. if (!t)
  2439. return false;
  2440. }
  2441. return true;
  2442. }
  2443. /* Given an assignable expression and an arbitrary expression, make
  2444. sure that the assignment can take place. */
  2445. bool
  2446. gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
  2447. {
  2448. gfc_symbol *sym;
  2449. gfc_ref *ref;
  2450. int has_pointer;
  2451. sym = lvalue->symtree->n.sym;
  2452. /* See if this is the component or subcomponent of a pointer. */
  2453. has_pointer = sym->attr.pointer;
  2454. for (ref = lvalue->ref; ref; ref = ref->next)
  2455. if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
  2456. {
  2457. has_pointer = 1;
  2458. break;
  2459. }
  2460. /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
  2461. variable local to a function subprogram. Its existence begins when
  2462. execution of the function is initiated and ends when execution of the
  2463. function is terminated...
  2464. Therefore, the left hand side is no longer a variable, when it is: */
  2465. if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
  2466. && !sym->attr.external)
  2467. {
  2468. bool bad_proc;
  2469. bad_proc = false;
  2470. /* (i) Use associated; */
  2471. if (sym->attr.use_assoc)
  2472. bad_proc = true;
  2473. /* (ii) The assignment is in the main program; or */
  2474. if (gfc_current_ns->proc_name
  2475. && gfc_current_ns->proc_name->attr.is_main_program)
  2476. bad_proc = true;
  2477. /* (iii) A module or internal procedure... */
  2478. if (gfc_current_ns->proc_name
  2479. && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
  2480. || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
  2481. && gfc_current_ns->parent
  2482. && (!(gfc_current_ns->parent->proc_name->attr.function
  2483. || gfc_current_ns->parent->proc_name->attr.subroutine)
  2484. || gfc_current_ns->parent->proc_name->attr.is_main_program))
  2485. {
  2486. /* ... that is not a function... */
  2487. if (gfc_current_ns->proc_name
  2488. && !gfc_current_ns->proc_name->attr.function)
  2489. bad_proc = true;
  2490. /* ... or is not an entry and has a different name. */
  2491. if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
  2492. bad_proc = true;
  2493. }
  2494. /* (iv) Host associated and not the function symbol or the
  2495. parent result. This picks up sibling references, which
  2496. cannot be entries. */
  2497. if (!sym->attr.entry
  2498. && sym->ns == gfc_current_ns->parent
  2499. && sym != gfc_current_ns->proc_name
  2500. && sym != gfc_current_ns->parent->proc_name->result)
  2501. bad_proc = true;
  2502. if (bad_proc)
  2503. {
  2504. gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where);
  2505. return false;
  2506. }
  2507. }
  2508. if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
  2509. {
  2510. gfc_error ("Incompatible ranks %d and %d in assignment at %L",
  2511. lvalue->rank, rvalue->rank, &lvalue->where);
  2512. return false;
  2513. }
  2514. if (lvalue->ts.type == BT_UNKNOWN)
  2515. {
  2516. gfc_error ("Variable type is UNKNOWN in assignment at %L",
  2517. &lvalue->where);
  2518. return false;
  2519. }
  2520. if (rvalue->expr_type == EXPR_NULL)
  2521. {
  2522. if (has_pointer && (ref == NULL || ref->next == NULL)
  2523. && lvalue->symtree->n.sym->attr.data)
  2524. return true;
  2525. else
  2526. {
  2527. gfc_error ("NULL appears on right-hand side in assignment at %L",
  2528. &rvalue->where);
  2529. return false;
  2530. }
  2531. }
  2532. /* This is possibly a typo: x = f() instead of x => f(). */
  2533. if (warn_surprising
  2534. && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer)
  2535. gfc_warning (OPT_Wsurprising,
  2536. "POINTER-valued function appears on right-hand side of "
  2537. "assignment at %L", &rvalue->where);
  2538. /* Check size of array assignments. */
  2539. if (lvalue->rank != 0 && rvalue->rank != 0
  2540. && !gfc_check_conformance (lvalue, rvalue, "array assignment"))
  2541. return false;
  2542. if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER
  2543. && lvalue->symtree->n.sym->attr.data
  2544. && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L used to "
  2545. "initialize non-integer variable %qs",
  2546. &rvalue->where, lvalue->symtree->n.sym->name))
  2547. return false;
  2548. else if (rvalue->is_boz && !lvalue->symtree->n.sym->attr.data
  2549. && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
  2550. "a DATA statement and outside INT/REAL/DBLE/CMPLX",
  2551. &rvalue->where))
  2552. return false;
  2553. /* Handle the case of a BOZ literal on the RHS. */
  2554. if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER)
  2555. {
  2556. int rc;
  2557. if (warn_surprising)
  2558. gfc_warning (OPT_Wsurprising,
  2559. "BOZ literal at %L is bitwise transferred "
  2560. "non-integer symbol %qs", &rvalue->where,
  2561. lvalue->symtree->n.sym->name);
  2562. if (!gfc_convert_boz (rvalue, &lvalue->ts))
  2563. return false;
  2564. if ((rc = gfc_range_check (rvalue)) != ARITH_OK)
  2565. {
  2566. if (rc == ARITH_UNDERFLOW)
  2567. gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
  2568. ". This check can be disabled with the option "
  2569. "%<-fno-range-check%>", &rvalue->where);
  2570. else if (rc == ARITH_OVERFLOW)
  2571. gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
  2572. ". This check can be disabled with the option "
  2573. "%<-fno-range-check%>", &rvalue->where);
  2574. else if (rc == ARITH_NAN)
  2575. gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
  2576. ". This check can be disabled with the option "
  2577. "%<-fno-range-check%>", &rvalue->where);
  2578. return false;
  2579. }
  2580. }
  2581. /* Warn about type-changing conversions for REAL or COMPLEX constants.
  2582. If lvalue and rvalue are mixed REAL and complex, gfc_compare_types
  2583. will warn anyway, so there is no need to to so here. */
  2584. if (rvalue->expr_type == EXPR_CONSTANT && lvalue->ts.type == rvalue->ts.type
  2585. && (lvalue->ts.type == BT_REAL || lvalue->ts.type == BT_COMPLEX))
  2586. {
  2587. if (lvalue->ts.kind < rvalue->ts.kind && warn_conversion)
  2588. {
  2589. /* As a special bonus, don't warn about REAL rvalues which are not
  2590. changed by the conversion if -Wconversion is specified. */
  2591. if (rvalue->ts.type == BT_REAL && mpfr_number_p (rvalue->value.real))
  2592. {
  2593. /* Calculate the difference between the constant and the rounded
  2594. value and check it against zero. */
  2595. mpfr_t rv, diff;
  2596. gfc_set_model_kind (lvalue->ts.kind);
  2597. mpfr_init (rv);
  2598. gfc_set_model_kind (rvalue->ts.kind);
  2599. mpfr_init (diff);
  2600. mpfr_set (rv, rvalue->value.real, GFC_RND_MODE);
  2601. mpfr_sub (diff, rv, rvalue->value.real, GFC_RND_MODE);
  2602. if (!mpfr_zero_p (diff))
  2603. gfc_warning (OPT_Wconversion,
  2604. "Change of value in conversion from "
  2605. " %qs to %qs at %L", gfc_typename (&rvalue->ts),
  2606. gfc_typename (&lvalue->ts), &rvalue->where);
  2607. mpfr_clear (rv);
  2608. mpfr_clear (diff);
  2609. }
  2610. else
  2611. gfc_warning (OPT_Wconversion,
  2612. "Possible change of value in conversion from %qs "
  2613. "to %qs at %L", gfc_typename (&rvalue->ts),
  2614. gfc_typename (&lvalue->ts), &rvalue->where);
  2615. }
  2616. else if (warn_conversion_extra && lvalue->ts.kind > rvalue->ts.kind)
  2617. {
  2618. gfc_warning (OPT_Wconversion_extra,
  2619. "Conversion from %qs to %qs at %L",
  2620. gfc_typename (&rvalue->ts),
  2621. gfc_typename (&lvalue->ts), &rvalue->where);
  2622. }
  2623. }
  2624. if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
  2625. return true;
  2626. /* Only DATA Statements come here. */
  2627. if (!conform)
  2628. {
  2629. /* Numeric can be converted to any other numeric. And Hollerith can be
  2630. converted to any other type. */
  2631. if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
  2632. || rvalue->ts.type == BT_HOLLERITH)
  2633. return true;
  2634. if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
  2635. return true;
  2636. gfc_error ("Incompatible types in DATA statement at %L; attempted "
  2637. "conversion of %s to %s", &lvalue->where,
  2638. gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
  2639. return false;
  2640. }
  2641. /* Assignment is the only case where character variables of different
  2642. kind values can be converted into one another. */
  2643. if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
  2644. {
  2645. if (lvalue->ts.kind != rvalue->ts.kind)
  2646. gfc_convert_chartype (rvalue, &lvalue->ts);
  2647. return true;
  2648. }
  2649. return gfc_convert_type (rvalue, &lvalue->ts, 1);
  2650. }
  2651. /* Check that a pointer assignment is OK. We first check lvalue, and
  2652. we only check rvalue if it's not an assignment to NULL() or a
  2653. NULLIFY statement. */
  2654. bool
  2655. gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
  2656. {
  2657. symbol_attribute attr, lhs_attr;
  2658. gfc_ref *ref;
  2659. bool is_pure, is_implicit_pure, rank_remap;
  2660. int proc_pointer;
  2661. lhs_attr = gfc_expr_attr (lvalue);
  2662. if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer)
  2663. {
  2664. gfc_error ("Pointer assignment target is not a POINTER at %L",
  2665. &lvalue->where);
  2666. return false;
  2667. }
  2668. if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc
  2669. && !lhs_attr.proc_pointer)
  2670. {
  2671. gfc_error ("%qs in the pointer assignment at %L cannot be an "
  2672. "l-value since it is a procedure",
  2673. lvalue->symtree->n.sym->name, &lvalue->where);
  2674. return false;
  2675. }
  2676. proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
  2677. rank_remap = false;
  2678. for (ref = lvalue->ref; ref; ref = ref->next)
  2679. {
  2680. if (ref->type == REF_COMPONENT)
  2681. proc_pointer = ref->u.c.component->attr.proc_pointer;
  2682. if (ref->type == REF_ARRAY && ref->next == NULL)
  2683. {
  2684. int dim;
  2685. if (ref->u.ar.type == AR_FULL)
  2686. break;
  2687. if (ref->u.ar.type != AR_SECTION)
  2688. {
  2689. gfc_error ("Expected bounds specification for %qs at %L",
  2690. lvalue->symtree->n.sym->name, &lvalue->where);
  2691. return false;
  2692. }
  2693. if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification "
  2694. "for %qs in pointer assignment at %L",
  2695. lvalue->symtree->n.sym->name, &lvalue->where))
  2696. return false;
  2697. /* When bounds are given, all lbounds are necessary and either all
  2698. or none of the upper bounds; no strides are allowed. If the
  2699. upper bounds are present, we may do rank remapping. */
  2700. for (dim = 0; dim < ref->u.ar.dimen; ++dim)
  2701. {
  2702. if (!ref->u.ar.start[dim]
  2703. || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
  2704. {
  2705. gfc_error ("Lower bound has to be present at %L",
  2706. &lvalue->where);
  2707. return false;
  2708. }
  2709. if (ref->u.ar.stride[dim])
  2710. {
  2711. gfc_error ("Stride must not be present at %L",
  2712. &lvalue->where);
  2713. return false;
  2714. }
  2715. if (dim == 0)
  2716. rank_remap = (ref->u.ar.end[dim] != NULL);
  2717. else
  2718. {
  2719. if ((rank_remap && !ref->u.ar.end[dim])
  2720. || (!rank_remap && ref->u.ar.end[dim]))
  2721. {
  2722. gfc_error ("Either all or none of the upper bounds"
  2723. " must be specified at %L", &lvalue->where);
  2724. return false;
  2725. }
  2726. }
  2727. }
  2728. }
  2729. }
  2730. is_pure = gfc_pure (NULL);
  2731. is_implicit_pure = gfc_implicit_pure (NULL);
  2732. /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
  2733. kind, etc for lvalue and rvalue must match, and rvalue must be a
  2734. pure variable if we're in a pure function. */
  2735. if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
  2736. return true;
  2737. /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
  2738. if (lvalue->expr_type == EXPR_VARIABLE
  2739. && gfc_is_coindexed (lvalue))
  2740. {
  2741. gfc_ref *ref;
  2742. for (ref = lvalue->ref; ref; ref = ref->next)
  2743. if (ref->type == REF_ARRAY && ref->u.ar.codimen)
  2744. {
  2745. gfc_error ("Pointer object at %L shall not have a coindex",
  2746. &lvalue->where);
  2747. return false;
  2748. }
  2749. }
  2750. /* Checks on rvalue for procedure pointer assignments. */
  2751. if (proc_pointer)
  2752. {
  2753. char err[200];
  2754. gfc_symbol *s1,*s2;
  2755. gfc_component *comp;
  2756. const char *name;
  2757. attr = gfc_expr_attr (rvalue);
  2758. if (!((rvalue->expr_type == EXPR_NULL)
  2759. || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
  2760. || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
  2761. || (rvalue->expr_type == EXPR_VARIABLE
  2762. && attr.flavor == FL_PROCEDURE)))
  2763. {
  2764. gfc_error ("Invalid procedure pointer assignment at %L",
  2765. &rvalue->where);
  2766. return false;
  2767. }
  2768. if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer)
  2769. {
  2770. /* Check for intrinsics. */
  2771. gfc_symbol *sym = rvalue->symtree->n.sym;
  2772. if (!sym->attr.intrinsic
  2773. && (gfc_is_intrinsic (sym, 0, sym->declared_at)
  2774. || gfc_is_intrinsic (sym, 1, sym->declared_at)))
  2775. {
  2776. sym->attr.intrinsic = 1;
  2777. gfc_resolve_intrinsic (sym, &rvalue->where);
  2778. attr = gfc_expr_attr (rvalue);
  2779. }
  2780. /* Check for result of embracing function. */
  2781. if (sym->attr.function && sym->result == sym)
  2782. {
  2783. gfc_namespace *ns;
  2784. for (ns = gfc_current_ns; ns; ns = ns->parent)
  2785. if (sym == ns->proc_name)
  2786. {
  2787. gfc_error ("Function result %qs is invalid as proc-target "
  2788. "in procedure pointer assignment at %L",
  2789. sym->name, &rvalue->where);
  2790. return false;
  2791. }
  2792. }
  2793. }
  2794. if (attr.abstract)
  2795. {
  2796. gfc_error ("Abstract interface %qs is invalid "
  2797. "in procedure pointer assignment at %L",
  2798. rvalue->symtree->name, &rvalue->where);
  2799. return false;
  2800. }
  2801. /* Check for F08:C729. */
  2802. if (attr.flavor == FL_PROCEDURE)
  2803. {
  2804. if (attr.proc == PROC_ST_FUNCTION)
  2805. {
  2806. gfc_error ("Statement function %qs is invalid "
  2807. "in procedure pointer assignment at %L",
  2808. rvalue->symtree->name, &rvalue->where);
  2809. return false;
  2810. }
  2811. if (attr.proc == PROC_INTERNAL &&
  2812. !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
  2813. "is invalid in procedure pointer assignment "
  2814. "at %L", rvalue->symtree->name, &rvalue->where))
  2815. return false;
  2816. if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name,
  2817. attr.subroutine) == 0)
  2818. {
  2819. gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
  2820. "assignment", rvalue->symtree->name, &rvalue->where);
  2821. return false;
  2822. }
  2823. }
  2824. /* Check for F08:C730. */
  2825. if (attr.elemental && !attr.intrinsic)
  2826. {
  2827. gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
  2828. "in procedure pointer assignment at %L",
  2829. rvalue->symtree->name, &rvalue->where);
  2830. return false;
  2831. }
  2832. /* Ensure that the calling convention is the same. As other attributes
  2833. such as DLLEXPORT may differ, one explicitly only tests for the
  2834. calling conventions. */
  2835. if (rvalue->expr_type == EXPR_VARIABLE
  2836. && lvalue->symtree->n.sym->attr.ext_attr
  2837. != rvalue->symtree->n.sym->attr.ext_attr)
  2838. {
  2839. symbol_attribute calls;
  2840. calls.ext_attr = 0;
  2841. gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
  2842. gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
  2843. gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
  2844. if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
  2845. != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
  2846. {
  2847. gfc_error ("Mismatch in the procedure pointer assignment "
  2848. "at %L: mismatch in the calling convention",
  2849. &rvalue->where);
  2850. return false;
  2851. }
  2852. }
  2853. comp = gfc_get_proc_ptr_comp (lvalue);
  2854. if (comp)
  2855. s1 = comp->ts.interface;
  2856. else
  2857. {
  2858. s1 = lvalue->symtree->n.sym;
  2859. if (s1->ts.interface)
  2860. s1 = s1->ts.interface;
  2861. }
  2862. comp = gfc_get_proc_ptr_comp (rvalue);
  2863. if (comp)
  2864. {
  2865. if (rvalue->expr_type == EXPR_FUNCTION)
  2866. {
  2867. s2 = comp->ts.interface->result;
  2868. name = s2->name;
  2869. }
  2870. else
  2871. {
  2872. s2 = comp->ts.interface;
  2873. name = comp->name;
  2874. }
  2875. }
  2876. else if (rvalue->expr_type == EXPR_FUNCTION)
  2877. {
  2878. if (rvalue->value.function.esym)
  2879. s2 = rvalue->value.function.esym->result;
  2880. else
  2881. s2 = rvalue->symtree->n.sym->result;
  2882. name = s2->name;
  2883. }
  2884. else
  2885. {
  2886. s2 = rvalue->symtree->n.sym;
  2887. name = s2->name;
  2888. }
  2889. if (s2 && s2->attr.proc_pointer && s2->ts.interface)
  2890. s2 = s2->ts.interface;
  2891. if (s1 == s2 || !s1 || !s2)
  2892. return true;
  2893. /* F08:7.2.2.4 (4) */
  2894. if (s1->attr.if_source == IFSRC_UNKNOWN
  2895. && gfc_explicit_interface_required (s2, err, sizeof(err)))
  2896. {
  2897. gfc_error ("Explicit interface required for %qs at %L: %s",
  2898. s1->name, &lvalue->where, err);
  2899. return false;
  2900. }
  2901. if (s2->attr.if_source == IFSRC_UNKNOWN
  2902. && gfc_explicit_interface_required (s1, err, sizeof(err)))
  2903. {
  2904. gfc_error ("Explicit interface required for %qs at %L: %s",
  2905. s2->name, &rvalue->where, err);
  2906. return false;
  2907. }
  2908. if (!gfc_compare_interfaces (s1, s2, name, 0, 1,
  2909. err, sizeof(err), NULL, NULL))
  2910. {
  2911. gfc_error ("Interface mismatch in procedure pointer assignment "
  2912. "at %L: %s", &rvalue->where, err);
  2913. return false;
  2914. }
  2915. /* Check F2008Cor2, C729. */
  2916. if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN
  2917. && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function)
  2918. {
  2919. gfc_error ("Procedure pointer target %qs at %L must be either an "
  2920. "intrinsic, host or use associated, referenced or have "
  2921. "the EXTERNAL attribute", s2->name, &rvalue->where);
  2922. return false;
  2923. }
  2924. return true;
  2925. }
  2926. if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
  2927. {
  2928. /* Check for F03:C717. */
  2929. if (UNLIMITED_POLY (rvalue)
  2930. && !(UNLIMITED_POLY (lvalue)
  2931. || (lvalue->ts.type == BT_DERIVED
  2932. && (lvalue->ts.u.derived->attr.is_bind_c
  2933. || lvalue->ts.u.derived->attr.sequence))))
  2934. gfc_error ("Data-pointer-object &L must be unlimited "
  2935. "polymorphic, a sequence derived type or of a "
  2936. "type with the BIND attribute assignment at %L "
  2937. "to be compatible with an unlimited polymorphic "
  2938. "target", &lvalue->where);
  2939. else
  2940. gfc_error ("Different types in pointer assignment at %L; "
  2941. "attempted assignment of %s to %s", &lvalue->where,
  2942. gfc_typename (&rvalue->ts),
  2943. gfc_typename (&lvalue->ts));
  2944. return false;
  2945. }
  2946. if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
  2947. {
  2948. gfc_error ("Different kind type parameters in pointer "
  2949. "assignment at %L", &lvalue->where);
  2950. return false;
  2951. }
  2952. if (lvalue->rank != rvalue->rank && !rank_remap)
  2953. {
  2954. gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
  2955. return false;
  2956. }
  2957. /* Make sure the vtab is present. */
  2958. if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
  2959. gfc_find_vtab (&rvalue->ts);
  2960. /* Check rank remapping. */
  2961. if (rank_remap)
  2962. {
  2963. mpz_t lsize, rsize;
  2964. /* If this can be determined, check that the target must be at least as
  2965. large as the pointer assigned to it is. */
  2966. if (gfc_array_size (lvalue, &lsize)
  2967. && gfc_array_size (rvalue, &rsize)
  2968. && mpz_cmp (rsize, lsize) < 0)
  2969. {
  2970. gfc_error ("Rank remapping target is smaller than size of the"
  2971. " pointer (%ld < %ld) at %L",
  2972. mpz_get_si (rsize), mpz_get_si (lsize),
  2973. &lvalue->where);
  2974. return false;
  2975. }
  2976. /* The target must be either rank one or it must be simply contiguous
  2977. and F2008 must be allowed. */
  2978. if (rvalue->rank != 1)
  2979. {
  2980. if (!gfc_is_simply_contiguous (rvalue, true))
  2981. {
  2982. gfc_error ("Rank remapping target must be rank 1 or"
  2983. " simply contiguous at %L", &rvalue->where);
  2984. return false;
  2985. }
  2986. if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not "
  2987. "rank 1 at %L", &rvalue->where))
  2988. return false;
  2989. }
  2990. }
  2991. /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
  2992. if (rvalue->expr_type == EXPR_NULL)
  2993. return true;
  2994. if (lvalue->ts.type == BT_CHARACTER)
  2995. {
  2996. bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
  2997. if (!t)
  2998. return false;
  2999. }
  3000. if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
  3001. lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
  3002. attr = gfc_expr_attr (rvalue);
  3003. if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
  3004. {
  3005. gfc_error ("Target expression in pointer assignment "
  3006. "at %L must deliver a pointer result",
  3007. &rvalue->where);
  3008. return false;
  3009. }
  3010. if (!attr.target && !attr.pointer)
  3011. {
  3012. gfc_error ("Pointer assignment target is neither TARGET "
  3013. "nor POINTER at %L", &rvalue->where);
  3014. return false;
  3015. }
  3016. if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
  3017. {
  3018. gfc_error ("Bad target in pointer assignment in PURE "
  3019. "procedure at %L", &rvalue->where);
  3020. }
  3021. if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
  3022. gfc_unset_implicit_pure (gfc_current_ns->proc_name);
  3023. if (gfc_has_vector_index (rvalue))
  3024. {
  3025. gfc_error ("Pointer assignment with vector subscript "
  3026. "on rhs at %L", &rvalue->where);
  3027. return false;
  3028. }
  3029. if (attr.is_protected && attr.use_assoc
  3030. && !(attr.pointer || attr.proc_pointer))
  3031. {
  3032. gfc_error ("Pointer assignment target has PROTECTED "
  3033. "attribute at %L", &rvalue->where);
  3034. return false;
  3035. }
  3036. /* F2008, C725. For PURE also C1283. */
  3037. if (rvalue->expr_type == EXPR_VARIABLE
  3038. && gfc_is_coindexed (rvalue))
  3039. {
  3040. gfc_ref *ref;
  3041. for (ref = rvalue->ref; ref; ref = ref->next)
  3042. if (ref->type == REF_ARRAY && ref->u.ar.codimen)
  3043. {
  3044. gfc_error ("Data target at %L shall not have a coindex",
  3045. &rvalue->where);
  3046. return false;
  3047. }
  3048. }
  3049. /* Warn if it is the LHS pointer may lives longer than the RHS target. */
  3050. if (warn_target_lifetime
  3051. && rvalue->expr_type == EXPR_VARIABLE
  3052. && !rvalue->symtree->n.sym->attr.save
  3053. && !attr.pointer && !rvalue->symtree->n.sym->attr.host_assoc
  3054. && !rvalue->symtree->n.sym->attr.in_common
  3055. && !rvalue->symtree->n.sym->attr.use_assoc
  3056. && !rvalue->symtree->n.sym->attr.dummy)
  3057. {
  3058. bool warn;
  3059. gfc_namespace *ns;
  3060. warn = lvalue->symtree->n.sym->attr.dummy
  3061. || lvalue->symtree->n.sym->attr.result
  3062. || lvalue->symtree->n.sym->attr.function
  3063. || (lvalue->symtree->n.sym->attr.host_assoc
  3064. && lvalue->symtree->n.sym->ns
  3065. != rvalue->symtree->n.sym->ns)
  3066. || lvalue->symtree->n.sym->attr.use_assoc
  3067. || lvalue->symtree->n.sym->attr.in_common;
  3068. if (rvalue->symtree->n.sym->ns->proc_name
  3069. && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE
  3070. && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM)
  3071. for (ns = rvalue->symtree->n.sym->ns;
  3072. ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE;
  3073. ns = ns->parent)
  3074. if (ns->parent == lvalue->symtree->n.sym->ns)
  3075. {
  3076. warn = true;
  3077. break;
  3078. }
  3079. if (warn)
  3080. gfc_warning (OPT_Wtarget_lifetime,
  3081. "Pointer at %L in pointer assignment might outlive the "
  3082. "pointer target", &lvalue->where);
  3083. }
  3084. return true;
  3085. }
  3086. /* Relative of gfc_check_assign() except that the lvalue is a single
  3087. symbol. Used for initialization assignments. */
  3088. bool
  3089. gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
  3090. {
  3091. gfc_expr lvalue;
  3092. bool r;
  3093. bool pointer, proc_pointer;
  3094. memset (&lvalue, '\0', sizeof (gfc_expr));
  3095. lvalue.expr_type = EXPR_VARIABLE;
  3096. lvalue.ts = sym->ts;
  3097. if (sym->as)
  3098. lvalue.rank = sym->as->rank;
  3099. lvalue.symtree = XCNEW (gfc_symtree);
  3100. lvalue.symtree->n.sym = sym;
  3101. lvalue.where = sym->declared_at;
  3102. if (comp)
  3103. {
  3104. lvalue.ref = gfc_get_ref ();
  3105. lvalue.ref->type = REF_COMPONENT;
  3106. lvalue.ref->u.c.component = comp;
  3107. lvalue.ref->u.c.sym = sym;
  3108. lvalue.ts = comp->ts;
  3109. lvalue.rank = comp->as ? comp->as->rank : 0;
  3110. lvalue.where = comp->loc;
  3111. pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp)
  3112. ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
  3113. proc_pointer = comp->attr.proc_pointer;
  3114. }
  3115. else
  3116. {
  3117. pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym)
  3118. ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
  3119. proc_pointer = sym->attr.proc_pointer;
  3120. }
  3121. if (pointer || proc_pointer)
  3122. r = gfc_check_pointer_assign (&lvalue, rvalue);
  3123. else
  3124. r = gfc_check_assign (&lvalue, rvalue, 1);
  3125. free (lvalue.symtree);
  3126. free (lvalue.ref);
  3127. if (!r)
  3128. return r;
  3129. if (pointer && rvalue->expr_type != EXPR_NULL)
  3130. {
  3131. /* F08:C461. Additional checks for pointer initialization. */
  3132. symbol_attribute attr;
  3133. attr = gfc_expr_attr (rvalue);
  3134. if (attr.allocatable)
  3135. {
  3136. gfc_error ("Pointer initialization target at %L "
  3137. "must not be ALLOCATABLE", &rvalue->where);
  3138. return false;
  3139. }
  3140. if (!attr.target || attr.pointer)
  3141. {
  3142. gfc_error ("Pointer initialization target at %L "
  3143. "must have the TARGET attribute", &rvalue->where);
  3144. return false;
  3145. }
  3146. if (!attr.save && rvalue->expr_type == EXPR_VARIABLE
  3147. && rvalue->symtree->n.sym->ns->proc_name
  3148. && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program)
  3149. {
  3150. rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT;
  3151. attr.save = SAVE_IMPLICIT;
  3152. }
  3153. if (!attr.save)
  3154. {
  3155. gfc_error ("Pointer initialization target at %L "
  3156. "must have the SAVE attribute", &rvalue->where);
  3157. return false;
  3158. }
  3159. }
  3160. if (proc_pointer && rvalue->expr_type != EXPR_NULL)
  3161. {
  3162. /* F08:C1220. Additional checks for procedure pointer initialization. */
  3163. symbol_attribute attr = gfc_expr_attr (rvalue);
  3164. if (attr.proc_pointer)
  3165. {
  3166. gfc_error ("Procedure pointer initialization target at %L "
  3167. "may not be a procedure pointer", &rvalue->where);
  3168. return false;
  3169. }
  3170. }
  3171. return true;
  3172. }
  3173. /* Check for default initializer; sym->value is not enough
  3174. as it is also set for EXPR_NULL of allocatables. */
  3175. bool
  3176. gfc_has_default_initializer (gfc_symbol *der)
  3177. {
  3178. gfc_component *c;
  3179. gcc_assert (der->attr.flavor == FL_DERIVED);
  3180. for (c = der->components; c; c = c->next)
  3181. if (c->ts.type == BT_DERIVED)
  3182. {
  3183. if (!c->attr.pointer
  3184. && gfc_has_default_initializer (c->ts.u.derived))
  3185. return true;
  3186. if (c->attr.pointer && c->initializer)
  3187. return true;
  3188. }
  3189. else
  3190. {
  3191. if (c->initializer)
  3192. return true;
  3193. }
  3194. return false;
  3195. }
  3196. /* Get an expression for a default initializer. */
  3197. gfc_expr *
  3198. gfc_default_initializer (gfc_typespec *ts)
  3199. {
  3200. gfc_expr *init;
  3201. gfc_component *comp;
  3202. /* See if we have a default initializer in this, but not in nested
  3203. types (otherwise we could use gfc_has_default_initializer()). */
  3204. for (comp = ts->u.derived->components; comp; comp = comp->next)
  3205. if (comp->initializer || comp->attr.allocatable
  3206. || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
  3207. && CLASS_DATA (comp)->attr.allocatable))
  3208. break;
  3209. if (!comp)
  3210. return NULL;
  3211. init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
  3212. &ts->u.derived->declared_at);
  3213. init->ts = *ts;
  3214. for (comp = ts->u.derived->components; comp; comp = comp->next)
  3215. {
  3216. gfc_constructor *ctor = gfc_constructor_get();
  3217. if (comp->initializer)
  3218. {
  3219. ctor->expr = gfc_copy_expr (comp->initializer);
  3220. if ((comp->ts.type != comp->initializer->ts.type
  3221. || comp->ts.kind != comp->initializer->ts.kind)
  3222. && !comp->attr.pointer && !comp->attr.proc_pointer)
  3223. gfc_convert_type_warn (ctor->expr, &comp->ts, 2, false);
  3224. }
  3225. if (comp->attr.allocatable
  3226. || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
  3227. {
  3228. ctor->expr = gfc_get_expr ();
  3229. ctor->expr->expr_type = EXPR_NULL;
  3230. ctor->expr->ts = comp->ts;
  3231. }
  3232. gfc_constructor_append (&init->value.constructor, ctor);
  3233. }
  3234. return init;
  3235. }
  3236. /* Given a symbol, create an expression node with that symbol as a
  3237. variable. If the symbol is array valued, setup a reference of the
  3238. whole array. */
  3239. gfc_expr *
  3240. gfc_get_variable_expr (gfc_symtree *var)
  3241. {
  3242. gfc_expr *e;
  3243. e = gfc_get_expr ();
  3244. e->expr_type = EXPR_VARIABLE;
  3245. e->symtree = var;
  3246. e->ts = var->n.sym->ts;
  3247. if (var->n.sym->attr.flavor != FL_PROCEDURE
  3248. && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
  3249. || (var->n.sym->ts.type == BT_CLASS && CLASS_DATA (var->n.sym)
  3250. && CLASS_DATA (var->n.sym)->as)))
  3251. {
  3252. e->rank = var->n.sym->ts.type == BT_CLASS
  3253. ? CLASS_DATA (var->n.sym)->as->rank : var->n.sym->as->rank;
  3254. e->ref = gfc_get_ref ();
  3255. e->ref->type = REF_ARRAY;
  3256. e->ref->u.ar.type = AR_FULL;
  3257. e->ref->u.ar.as = gfc_copy_array_spec (var->n.sym->ts.type == BT_CLASS
  3258. ? CLASS_DATA (var->n.sym)->as
  3259. : var->n.sym->as);
  3260. }
  3261. return e;
  3262. }
  3263. /* Adds a full array reference to an expression, as needed. */
  3264. void
  3265. gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
  3266. {
  3267. gfc_ref *ref;
  3268. for (ref = e->ref; ref; ref = ref->next)
  3269. if (!ref->next)
  3270. break;
  3271. if (ref)
  3272. {
  3273. ref->next = gfc_get_ref ();
  3274. ref = ref->next;
  3275. }
  3276. else
  3277. {
  3278. e->ref = gfc_get_ref ();
  3279. ref = e->ref;
  3280. }
  3281. ref->type = REF_ARRAY;
  3282. ref->u.ar.type = AR_FULL;
  3283. ref->u.ar.dimen = e->rank;
  3284. ref->u.ar.where = e->where;
  3285. ref->u.ar.as = as;
  3286. }
  3287. gfc_expr *
  3288. gfc_lval_expr_from_sym (gfc_symbol *sym)
  3289. {
  3290. gfc_expr *lval;
  3291. lval = gfc_get_expr ();
  3292. lval->expr_type = EXPR_VARIABLE;
  3293. lval->where = sym->declared_at;
  3294. lval->ts = sym->ts;
  3295. lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
  3296. /* It will always be a full array. */
  3297. lval->rank = sym->as ? sym->as->rank : 0;
  3298. if (lval->rank)
  3299. gfc_add_full_array_ref (lval, sym->ts.type == BT_CLASS ?
  3300. CLASS_DATA (sym)->as : sym->as);
  3301. return lval;
  3302. }
  3303. /* Returns the array_spec of a full array expression. A NULL is
  3304. returned otherwise. */
  3305. gfc_array_spec *
  3306. gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
  3307. {
  3308. gfc_array_spec *as;
  3309. gfc_ref *ref;
  3310. if (expr->rank == 0)
  3311. return NULL;
  3312. /* Follow any component references. */
  3313. if (expr->expr_type == EXPR_VARIABLE
  3314. || expr->expr_type == EXPR_CONSTANT)
  3315. {
  3316. as = expr->symtree->n.sym->as;
  3317. for (ref = expr->ref; ref; ref = ref->next)
  3318. {
  3319. switch (ref->type)
  3320. {
  3321. case REF_COMPONENT:
  3322. as = ref->u.c.component->as;
  3323. continue;
  3324. case REF_SUBSTRING:
  3325. continue;
  3326. case REF_ARRAY:
  3327. {
  3328. switch (ref->u.ar.type)
  3329. {
  3330. case AR_ELEMENT:
  3331. case AR_SECTION:
  3332. case AR_UNKNOWN:
  3333. as = NULL;
  3334. continue;
  3335. case AR_FULL:
  3336. break;
  3337. }
  3338. break;
  3339. }
  3340. }
  3341. }
  3342. }
  3343. else
  3344. as = NULL;
  3345. return as;
  3346. }
  3347. /* General expression traversal function. */
  3348. bool
  3349. gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
  3350. bool (*func)(gfc_expr *, gfc_symbol *, int*),
  3351. int f)
  3352. {
  3353. gfc_array_ref ar;
  3354. gfc_ref *ref;
  3355. gfc_actual_arglist *args;
  3356. gfc_constructor *c;
  3357. int i;
  3358. if (!expr)
  3359. return false;
  3360. if ((*func) (expr, sym, &f))
  3361. return true;
  3362. if (expr->ts.type == BT_CHARACTER
  3363. && expr->ts.u.cl
  3364. && expr->ts.u.cl->length
  3365. && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
  3366. && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
  3367. return true;
  3368. switch (expr->expr_type)
  3369. {
  3370. case EXPR_PPC:
  3371. case EXPR_COMPCALL:
  3372. case EXPR_FUNCTION:
  3373. for (args = expr->value.function.actual; args; args = args->next)
  3374. {
  3375. if (gfc_traverse_expr (args->expr, sym, func, f))
  3376. return true;
  3377. }
  3378. break;
  3379. case EXPR_VARIABLE:
  3380. case EXPR_CONSTANT:
  3381. case EXPR_NULL:
  3382. case EXPR_SUBSTRING:
  3383. break;
  3384. case EXPR_STRUCTURE:
  3385. case EXPR_ARRAY:
  3386. for (c = gfc_constructor_first (expr->value.constructor);
  3387. c; c = gfc_constructor_next (c))
  3388. {
  3389. if (gfc_traverse_expr (c->expr, sym, func, f))
  3390. return true;
  3391. if (c->iterator)
  3392. {
  3393. if (gfc_traverse_expr (c->iterator->var, sym, func, f))
  3394. return true;
  3395. if (gfc_traverse_expr (c->iterator->start, sym, func, f))
  3396. return true;
  3397. if (gfc_traverse_expr (c->iterator->end, sym, func, f))
  3398. return true;
  3399. if (gfc_traverse_expr (c->iterator->step, sym, func, f))
  3400. return true;
  3401. }
  3402. }
  3403. break;
  3404. case EXPR_OP:
  3405. if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
  3406. return true;
  3407. if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
  3408. return true;
  3409. break;
  3410. default:
  3411. gcc_unreachable ();
  3412. break;
  3413. }
  3414. ref = expr->ref;
  3415. while (ref != NULL)
  3416. {
  3417. switch (ref->type)
  3418. {
  3419. case REF_ARRAY:
  3420. ar = ref->u.ar;
  3421. for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
  3422. {
  3423. if (gfc_traverse_expr (ar.start[i], sym, func, f))
  3424. return true;
  3425. if (gfc_traverse_expr (ar.end[i], sym, func, f))
  3426. return true;
  3427. if (gfc_traverse_expr (ar.stride[i], sym, func, f))
  3428. return true;
  3429. }
  3430. break;
  3431. case REF_SUBSTRING:
  3432. if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
  3433. return true;
  3434. if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
  3435. return true;
  3436. break;
  3437. case REF_COMPONENT:
  3438. if (ref->u.c.component->ts.type == BT_CHARACTER
  3439. && ref->u.c.component->ts.u.cl
  3440. && ref->u.c.component->ts.u.cl->length
  3441. && ref->u.c.component->ts.u.cl->length->expr_type
  3442. != EXPR_CONSTANT
  3443. && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
  3444. sym, func, f))
  3445. return true;
  3446. if (ref->u.c.component->as)
  3447. for (i = 0; i < ref->u.c.component->as->rank
  3448. + ref->u.c.component->as->corank; i++)
  3449. {
  3450. if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
  3451. sym, func, f))
  3452. return true;
  3453. if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
  3454. sym, func, f))
  3455. return true;
  3456. }
  3457. break;
  3458. default:
  3459. gcc_unreachable ();
  3460. }
  3461. ref = ref->next;
  3462. }
  3463. return false;
  3464. }
  3465. /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
  3466. static bool
  3467. expr_set_symbols_referenced (gfc_expr *expr,
  3468. gfc_symbol *sym ATTRIBUTE_UNUSED,
  3469. int *f ATTRIBUTE_UNUSED)
  3470. {
  3471. if (expr->expr_type != EXPR_VARIABLE)
  3472. return false;
  3473. gfc_set_sym_referenced (expr->symtree->n.sym);
  3474. return false;
  3475. }
  3476. void
  3477. gfc_expr_set_symbols_referenced (gfc_expr *expr)
  3478. {
  3479. gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
  3480. }
  3481. /* Determine if an expression is a procedure pointer component and return
  3482. the component in that case. Otherwise return NULL. */
  3483. gfc_component *
  3484. gfc_get_proc_ptr_comp (gfc_expr *expr)
  3485. {
  3486. gfc_ref *ref;
  3487. if (!expr || !expr->ref)
  3488. return NULL;
  3489. ref = expr->ref;
  3490. while (ref->next)
  3491. ref = ref->next;
  3492. if (ref->type == REF_COMPONENT
  3493. && ref->u.c.component->attr.proc_pointer)
  3494. return ref->u.c.component;
  3495. return NULL;
  3496. }
  3497. /* Determine if an expression is a procedure pointer component. */
  3498. bool
  3499. gfc_is_proc_ptr_comp (gfc_expr *expr)
  3500. {
  3501. return (gfc_get_proc_ptr_comp (expr) != NULL);
  3502. }
  3503. /* Determine if an expression is a function with an allocatable class scalar
  3504. result. */
  3505. bool
  3506. gfc_is_alloc_class_scalar_function (gfc_expr *expr)
  3507. {
  3508. if (expr->expr_type == EXPR_FUNCTION
  3509. && expr->value.function.esym
  3510. && expr->value.function.esym->result
  3511. && expr->value.function.esym->result->ts.type == BT_CLASS
  3512. && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
  3513. && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
  3514. return true;
  3515. return false;
  3516. }
  3517. /* Determine if an expression is a function with an allocatable class array
  3518. result. */
  3519. bool
  3520. gfc_is_alloc_class_array_function (gfc_expr *expr)
  3521. {
  3522. if (expr->expr_type == EXPR_FUNCTION
  3523. && expr->value.function.esym
  3524. && expr->value.function.esym->result
  3525. && expr->value.function.esym->result->ts.type == BT_CLASS
  3526. && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
  3527. && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
  3528. return true;
  3529. return false;
  3530. }
  3531. /* Walk an expression tree and check each variable encountered for being typed.
  3532. If strict is not set, a top-level variable is tolerated untyped in -std=gnu
  3533. mode as is a basic arithmetic expression using those; this is for things in
  3534. legacy-code like:
  3535. INTEGER :: arr(n), n
  3536. INTEGER :: arr(n + 1), n
  3537. The namespace is needed for IMPLICIT typing. */
  3538. static gfc_namespace* check_typed_ns;
  3539. static bool
  3540. expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
  3541. int* f ATTRIBUTE_UNUSED)
  3542. {
  3543. bool t;
  3544. if (e->expr_type != EXPR_VARIABLE)
  3545. return false;
  3546. gcc_assert (e->symtree);
  3547. t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
  3548. true, e->where);
  3549. return (!t);
  3550. }
  3551. bool
  3552. gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
  3553. {
  3554. bool error_found;
  3555. /* If this is a top-level variable or EXPR_OP, do the check with strict given
  3556. to us. */
  3557. if (!strict)
  3558. {
  3559. if (e->expr_type == EXPR_VARIABLE && !e->ref)
  3560. return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
  3561. if (e->expr_type == EXPR_OP)
  3562. {
  3563. bool t = true;
  3564. gcc_assert (e->value.op.op1);
  3565. t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
  3566. if (t && e->value.op.op2)
  3567. t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
  3568. return t;
  3569. }
  3570. }
  3571. /* Otherwise, walk the expression and do it strictly. */
  3572. check_typed_ns = ns;
  3573. error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
  3574. return error_found ? false : true;
  3575. }
  3576. bool
  3577. gfc_ref_this_image (gfc_ref *ref)
  3578. {
  3579. int n;
  3580. gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
  3581. for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
  3582. if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
  3583. return false;
  3584. return true;
  3585. }
  3586. bool
  3587. gfc_is_coindexed (gfc_expr *e)
  3588. {
  3589. gfc_ref *ref;
  3590. for (ref = e->ref; ref; ref = ref->next)
  3591. if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
  3592. return !gfc_ref_this_image (ref);
  3593. return false;
  3594. }
  3595. /* Coarrays are variables with a corank but not being coindexed. However, also
  3596. the following is a coarray: A subobject of a coarray is a coarray if it does
  3597. not have any cosubscripts, vector subscripts, allocatable component
  3598. selection, or pointer component selection. (F2008, 2.4.7) */
  3599. bool
  3600. gfc_is_coarray (gfc_expr *e)
  3601. {
  3602. gfc_ref *ref;
  3603. gfc_symbol *sym;
  3604. gfc_component *comp;
  3605. bool coindexed;
  3606. bool coarray;
  3607. int i;
  3608. if (e->expr_type != EXPR_VARIABLE)
  3609. return false;
  3610. coindexed = false;
  3611. sym = e->symtree->n.sym;
  3612. if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
  3613. coarray = CLASS_DATA (sym)->attr.codimension;
  3614. else
  3615. coarray = sym->attr.codimension;
  3616. for (ref = e->ref; ref; ref = ref->next)
  3617. switch (ref->type)
  3618. {
  3619. case REF_COMPONENT:
  3620. comp = ref->u.c.component;
  3621. if (comp->ts.type == BT_CLASS && comp->attr.class_ok
  3622. && (CLASS_DATA (comp)->attr.class_pointer
  3623. || CLASS_DATA (comp)->attr.allocatable))
  3624. {
  3625. coindexed = false;
  3626. coarray = CLASS_DATA (comp)->attr.codimension;
  3627. }
  3628. else if (comp->attr.pointer || comp->attr.allocatable)
  3629. {
  3630. coindexed = false;
  3631. coarray = comp->attr.codimension;
  3632. }
  3633. break;
  3634. case REF_ARRAY:
  3635. if (!coarray)
  3636. break;
  3637. if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
  3638. {
  3639. coindexed = true;
  3640. break;
  3641. }
  3642. for (i = 0; i < ref->u.ar.dimen; i++)
  3643. if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
  3644. {
  3645. coarray = false;
  3646. break;
  3647. }
  3648. break;
  3649. case REF_SUBSTRING:
  3650. break;
  3651. }
  3652. return coarray && !coindexed;
  3653. }
  3654. int
  3655. gfc_get_corank (gfc_expr *e)
  3656. {
  3657. int corank;
  3658. gfc_ref *ref;
  3659. if (!gfc_is_coarray (e))
  3660. return 0;
  3661. if (e->ts.type == BT_CLASS && e->ts.u.derived->components)
  3662. corank = e->ts.u.derived->components->as
  3663. ? e->ts.u.derived->components->as->corank : 0;
  3664. else
  3665. corank = e->symtree->n.sym->as ? e->symtree->n.sym->as->corank : 0;
  3666. for (ref = e->ref; ref; ref = ref->next)
  3667. {
  3668. if (ref->type == REF_ARRAY)
  3669. corank = ref->u.ar.as->corank;
  3670. gcc_assert (ref->type != REF_SUBSTRING);
  3671. }
  3672. return corank;
  3673. }
  3674. /* Check whether the expression has an ultimate allocatable component.
  3675. Being itself allocatable does not count. */
  3676. bool
  3677. gfc_has_ultimate_allocatable (gfc_expr *e)
  3678. {
  3679. gfc_ref *ref, *last = NULL;
  3680. if (e->expr_type != EXPR_VARIABLE)
  3681. return false;
  3682. for (ref = e->ref; ref; ref = ref->next)
  3683. if (ref->type == REF_COMPONENT)
  3684. last = ref;
  3685. if (last && last->u.c.component->ts.type == BT_CLASS)
  3686. return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
  3687. else if (last && last->u.c.component->ts.type == BT_DERIVED)
  3688. return last->u.c.component->ts.u.derived->attr.alloc_comp;
  3689. else if (last)
  3690. return false;
  3691. if (e->ts.type == BT_CLASS)
  3692. return CLASS_DATA (e)->attr.alloc_comp;
  3693. else if (e->ts.type == BT_DERIVED)
  3694. return e->ts.u.derived->attr.alloc_comp;
  3695. else
  3696. return false;
  3697. }
  3698. /* Check whether the expression has an pointer component.
  3699. Being itself a pointer does not count. */
  3700. bool
  3701. gfc_has_ultimate_pointer (gfc_expr *e)
  3702. {
  3703. gfc_ref *ref, *last = NULL;
  3704. if (e->expr_type != EXPR_VARIABLE)
  3705. return false;
  3706. for (ref = e->ref; ref; ref = ref->next)
  3707. if (ref->type == REF_COMPONENT)
  3708. last = ref;
  3709. if (last && last->u.c.component->ts.type == BT_CLASS)
  3710. return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
  3711. else if (last && last->u.c.component->ts.type == BT_DERIVED)
  3712. return last->u.c.component->ts.u.derived->attr.pointer_comp;
  3713. else if (last)
  3714. return false;
  3715. if (e->ts.type == BT_CLASS)
  3716. return CLASS_DATA (e)->attr.pointer_comp;
  3717. else if (e->ts.type == BT_DERIVED)
  3718. return e->ts.u.derived->attr.pointer_comp;
  3719. else
  3720. return false;
  3721. }
  3722. /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
  3723. Note: A scalar is not regarded as "simply contiguous" by the standard.
  3724. if bool is not strict, some further checks are done - for instance,
  3725. a "(::1)" is accepted. */
  3726. bool
  3727. gfc_is_simply_contiguous (gfc_expr *expr, bool strict)
  3728. {
  3729. bool colon;
  3730. int i;
  3731. gfc_array_ref *ar = NULL;
  3732. gfc_ref *ref, *part_ref = NULL;
  3733. gfc_symbol *sym;
  3734. if (expr->expr_type == EXPR_FUNCTION)
  3735. return expr->value.function.esym
  3736. ? expr->value.function.esym->result->attr.contiguous : false;
  3737. else if (expr->expr_type != EXPR_VARIABLE)
  3738. return false;
  3739. if (expr->rank == 0)
  3740. return false;
  3741. for (ref = expr->ref; ref; ref = ref->next)
  3742. {
  3743. if (ar)
  3744. return false; /* Array shall be last part-ref. */
  3745. if (ref->type == REF_COMPONENT)
  3746. part_ref = ref;
  3747. else if (ref->type == REF_SUBSTRING)
  3748. return false;
  3749. else if (ref->u.ar.type != AR_ELEMENT)
  3750. ar = &ref->u.ar;
  3751. }
  3752. sym = expr->symtree->n.sym;
  3753. if (expr->ts.type != BT_CLASS
  3754. && ((part_ref
  3755. && !part_ref->u.c.component->attr.contiguous
  3756. && part_ref->u.c.component->attr.pointer)
  3757. || (!part_ref
  3758. && !sym->attr.contiguous
  3759. && (sym->attr.pointer
  3760. || sym->as->type == AS_ASSUMED_RANK
  3761. || sym->as->type == AS_ASSUMED_SHAPE))))
  3762. return false;
  3763. if (!ar || ar->type == AR_FULL)
  3764. return true;
  3765. gcc_assert (ar->type == AR_SECTION);
  3766. /* Check for simply contiguous array */
  3767. colon = true;
  3768. for (i = 0; i < ar->dimen; i++)
  3769. {
  3770. if (ar->dimen_type[i] == DIMEN_VECTOR)
  3771. return false;
  3772. if (ar->dimen_type[i] == DIMEN_ELEMENT)
  3773. {
  3774. colon = false;
  3775. continue;
  3776. }
  3777. gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
  3778. /* If the previous section was not contiguous, that's an error,
  3779. unless we have effective only one element and checking is not
  3780. strict. */
  3781. if (!colon && (strict || !ar->start[i] || !ar->end[i]
  3782. || ar->start[i]->expr_type != EXPR_CONSTANT
  3783. || ar->end[i]->expr_type != EXPR_CONSTANT
  3784. || mpz_cmp (ar->start[i]->value.integer,
  3785. ar->end[i]->value.integer) != 0))
  3786. return false;
  3787. /* Following the standard, "(::1)" or - if known at compile time -
  3788. "(lbound:ubound)" are not simply contiguous; if strict
  3789. is false, they are regarded as simply contiguous. */
  3790. if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
  3791. || ar->stride[i]->ts.type != BT_INTEGER
  3792. || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
  3793. return false;
  3794. if (ar->start[i]
  3795. && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
  3796. || !ar->as->lower[i]
  3797. || ar->as->lower[i]->expr_type != EXPR_CONSTANT
  3798. || mpz_cmp (ar->start[i]->value.integer,
  3799. ar->as->lower[i]->value.integer) != 0))
  3800. colon = false;
  3801. if (ar->end[i]
  3802. && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
  3803. || !ar->as->upper[i]
  3804. || ar->as->upper[i]->expr_type != EXPR_CONSTANT
  3805. || mpz_cmp (ar->end[i]->value.integer,
  3806. ar->as->upper[i]->value.integer) != 0))
  3807. colon = false;
  3808. }
  3809. return true;
  3810. }
  3811. /* Build call to an intrinsic procedure. The number of arguments has to be
  3812. passed (rather than ending the list with a NULL value) because we may
  3813. want to add arguments but with a NULL-expression. */
  3814. gfc_expr*
  3815. gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
  3816. locus where, unsigned numarg, ...)
  3817. {
  3818. gfc_expr* result;
  3819. gfc_actual_arglist* atail;
  3820. gfc_intrinsic_sym* isym;
  3821. va_list ap;
  3822. unsigned i;
  3823. const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
  3824. isym = gfc_intrinsic_function_by_id (id);
  3825. gcc_assert (isym);
  3826. result = gfc_get_expr ();
  3827. result->expr_type = EXPR_FUNCTION;
  3828. result->ts = isym->ts;
  3829. result->where = where;
  3830. result->value.function.name = mangled_name;
  3831. result->value.function.isym = isym;
  3832. gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
  3833. gfc_commit_symbol (result->symtree->n.sym);
  3834. gcc_assert (result->symtree
  3835. && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
  3836. || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
  3837. result->symtree->n.sym->intmod_sym_id = id;
  3838. result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
  3839. result->symtree->n.sym->attr.intrinsic = 1;
  3840. result->symtree->n.sym->attr.artificial = 1;
  3841. va_start (ap, numarg);
  3842. atail = NULL;
  3843. for (i = 0; i < numarg; ++i)
  3844. {
  3845. if (atail)
  3846. {
  3847. atail->next = gfc_get_actual_arglist ();
  3848. atail = atail->next;
  3849. }
  3850. else
  3851. atail = result->value.function.actual = gfc_get_actual_arglist ();
  3852. atail->expr = va_arg (ap, gfc_expr*);
  3853. }
  3854. va_end (ap);
  3855. return result;
  3856. }
  3857. /* Check if an expression may appear in a variable definition context
  3858. (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
  3859. This is called from the various places when resolving
  3860. the pieces that make up such a context.
  3861. If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
  3862. variables), some checks are not performed.
  3863. Optionally, a possible error message can be suppressed if context is NULL
  3864. and just the return status (true / false) be requested. */
  3865. bool
  3866. gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
  3867. bool own_scope, const char* context)
  3868. {
  3869. gfc_symbol* sym = NULL;
  3870. bool is_pointer;
  3871. bool check_intentin;
  3872. bool ptr_component;
  3873. symbol_attribute attr;
  3874. gfc_ref* ref;
  3875. int i;
  3876. if (e->expr_type == EXPR_VARIABLE)
  3877. {
  3878. gcc_assert (e->symtree);
  3879. sym = e->symtree->n.sym;
  3880. }
  3881. else if (e->expr_type == EXPR_FUNCTION)
  3882. {
  3883. gcc_assert (e->symtree);
  3884. sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
  3885. }
  3886. attr = gfc_expr_attr (e);
  3887. if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
  3888. {
  3889. if (!(gfc_option.allow_std & GFC_STD_F2008))
  3890. {
  3891. if (context)
  3892. gfc_error ("Fortran 2008: Pointer functions in variable definition"
  3893. " context (%s) at %L", context, &e->where);
  3894. return false;
  3895. }
  3896. }
  3897. else if (e->expr_type != EXPR_VARIABLE)
  3898. {
  3899. if (context)
  3900. gfc_error ("Non-variable expression in variable definition context (%s)"
  3901. " at %L", context, &e->where);
  3902. return false;
  3903. }
  3904. if (!pointer && sym->attr.flavor == FL_PARAMETER)
  3905. {
  3906. if (context)
  3907. gfc_error ("Named constant %qs in variable definition context (%s)"
  3908. " at %L", sym->name, context, &e->where);
  3909. return false;
  3910. }
  3911. if (!pointer && sym->attr.flavor != FL_VARIABLE
  3912. && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
  3913. && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
  3914. {
  3915. if (context)
  3916. gfc_error ("%qs in variable definition context (%s) at %L is not"
  3917. " a variable", sym->name, context, &e->where);
  3918. return false;
  3919. }
  3920. /* Find out whether the expr is a pointer; this also means following
  3921. component references to the last one. */
  3922. is_pointer = (attr.pointer || attr.proc_pointer);
  3923. if (pointer && !is_pointer)
  3924. {
  3925. if (context)
  3926. gfc_error ("Non-POINTER in pointer association context (%s)"
  3927. " at %L", context, &e->where);
  3928. return false;
  3929. }
  3930. /* F2008, C1303. */
  3931. if (!alloc_obj
  3932. && (attr.lock_comp
  3933. || (e->ts.type == BT_DERIVED
  3934. && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
  3935. && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
  3936. {
  3937. if (context)
  3938. gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
  3939. context, &e->where);
  3940. return false;
  3941. }
  3942. /* INTENT(IN) dummy argument. Check this, unless the object itself is the
  3943. component of sub-component of a pointer; we need to distinguish
  3944. assignment to a pointer component from pointer-assignment to a pointer
  3945. component. Note that (normal) assignment to procedure pointers is not
  3946. possible. */
  3947. check_intentin = !own_scope;
  3948. ptr_component = (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
  3949. ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
  3950. for (ref = e->ref; ref && check_intentin; ref = ref->next)
  3951. {
  3952. if (ptr_component && ref->type == REF_COMPONENT)
  3953. check_intentin = false;
  3954. if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
  3955. {
  3956. ptr_component = true;
  3957. if (!pointer)
  3958. check_intentin = false;
  3959. }
  3960. }
  3961. if (check_intentin && sym->attr.intent == INTENT_IN)
  3962. {
  3963. if (pointer && is_pointer)
  3964. {
  3965. if (context)
  3966. gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
  3967. " association context (%s) at %L",
  3968. sym->name, context, &e->where);
  3969. return false;
  3970. }
  3971. if (!pointer && !is_pointer && !sym->attr.pointer)
  3972. {
  3973. if (context)
  3974. gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
  3975. " definition context (%s) at %L",
  3976. sym->name, context, &e->where);
  3977. return false;
  3978. }
  3979. }
  3980. /* PROTECTED and use-associated. */
  3981. if (sym->attr.is_protected && sym->attr.use_assoc && check_intentin)
  3982. {
  3983. if (pointer && is_pointer)
  3984. {
  3985. if (context)
  3986. gfc_error ("Variable %qs is PROTECTED and can not appear in a"
  3987. " pointer association context (%s) at %L",
  3988. sym->name, context, &e->where);
  3989. return false;
  3990. }
  3991. if (!pointer && !is_pointer)
  3992. {
  3993. if (context)
  3994. gfc_error ("Variable %qs is PROTECTED and can not appear in a"
  3995. " variable definition context (%s) at %L",
  3996. sym->name, context, &e->where);
  3997. return false;
  3998. }
  3999. }
  4000. /* Variable not assignable from a PURE procedure but appears in
  4001. variable definition context. */
  4002. if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
  4003. {
  4004. if (context)
  4005. gfc_error ("Variable %qs can not appear in a variable definition"
  4006. " context (%s) at %L in PURE procedure",
  4007. sym->name, context, &e->where);
  4008. return false;
  4009. }
  4010. if (!pointer && context && gfc_implicit_pure (NULL)
  4011. && gfc_impure_variable (sym))
  4012. {
  4013. gfc_namespace *ns;
  4014. gfc_symbol *sym;
  4015. for (ns = gfc_current_ns; ns; ns = ns->parent)
  4016. {
  4017. sym = ns->proc_name;
  4018. if (sym == NULL)
  4019. break;
  4020. if (sym->attr.flavor == FL_PROCEDURE)
  4021. {
  4022. sym->attr.implicit_pure = 0;
  4023. break;
  4024. }
  4025. }
  4026. }
  4027. /* Check variable definition context for associate-names. */
  4028. if (!pointer && sym->assoc)
  4029. {
  4030. const char* name;
  4031. gfc_association_list* assoc;
  4032. gcc_assert (sym->assoc->target);
  4033. /* If this is a SELECT TYPE temporary (the association is used internally
  4034. for SELECT TYPE), silently go over to the target. */
  4035. if (sym->attr.select_type_temporary)
  4036. {
  4037. gfc_expr* t = sym->assoc->target;
  4038. gcc_assert (t->expr_type == EXPR_VARIABLE);
  4039. name = t->symtree->name;
  4040. if (t->symtree->n.sym->assoc)
  4041. assoc = t->symtree->n.sym->assoc;
  4042. else
  4043. assoc = sym->assoc;
  4044. }
  4045. else
  4046. {
  4047. name = sym->name;
  4048. assoc = sym->assoc;
  4049. }
  4050. gcc_assert (name && assoc);
  4051. /* Is association to a valid variable? */
  4052. if (!assoc->variable)
  4053. {
  4054. if (context)
  4055. {
  4056. if (assoc->target->expr_type == EXPR_VARIABLE)
  4057. gfc_error ("%qs at %L associated to vector-indexed target can"
  4058. " not be used in a variable definition context (%s)",
  4059. name, &e->where, context);
  4060. else
  4061. gfc_error ("%qs at %L associated to expression can"
  4062. " not be used in a variable definition context (%s)",
  4063. name, &e->where, context);
  4064. }
  4065. return false;
  4066. }
  4067. /* Target must be allowed to appear in a variable definition context. */
  4068. if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL))
  4069. {
  4070. if (context)
  4071. gfc_error_1 ("Associate-name '%s' can not appear in a variable"
  4072. " definition context (%s) at %L because its target"
  4073. " at %L can not, either",
  4074. name, context, &e->where,
  4075. &assoc->target->where);
  4076. return false;
  4077. }
  4078. }
  4079. /* Check for same value in vector expression subscript. */
  4080. if (e->rank > 0)
  4081. for (ref = e->ref; ref != NULL; ref = ref->next)
  4082. if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
  4083. for (i = 0; i < GFC_MAX_DIMENSIONS
  4084. && ref->u.ar.dimen_type[i] != 0; i++)
  4085. if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
  4086. {
  4087. gfc_expr *arr = ref->u.ar.start[i];
  4088. if (arr->expr_type == EXPR_ARRAY)
  4089. {
  4090. gfc_constructor *c, *n;
  4091. gfc_expr *ec, *en;
  4092. for (c = gfc_constructor_first (arr->value.constructor);
  4093. c != NULL; c = gfc_constructor_next (c))
  4094. {
  4095. if (c == NULL || c->iterator != NULL)
  4096. continue;
  4097. ec = c->expr;
  4098. for (n = gfc_constructor_next (c); n != NULL;
  4099. n = gfc_constructor_next (n))
  4100. {
  4101. if (n->iterator != NULL)
  4102. continue;
  4103. en = n->expr;
  4104. if (gfc_dep_compare_expr (ec, en) == 0)
  4105. {
  4106. if (context)
  4107. gfc_error_now_1 ("Elements with the same value "
  4108. "at %L and %L in vector "
  4109. "subscript in a variable "
  4110. "definition context (%s)",
  4111. &(ec->where), &(en->where),
  4112. context);
  4113. return false;
  4114. }
  4115. }
  4116. }
  4117. }
  4118. }
  4119. return true;
  4120. }