parse.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576
  1. /* Main parser.
  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 <setjmp.h>
  19. #include "coretypes.h"
  20. #include "flags.h"
  21. #include "gfortran.h"
  22. #include "match.h"
  23. #include "parse.h"
  24. #include "debug.h"
  25. /* Current statement label. Zero means no statement label. Because new_st
  26. can get wiped during statement matching, we have to keep it separate. */
  27. gfc_st_label *gfc_statement_label;
  28. static locus label_locus;
  29. static jmp_buf eof_buf;
  30. gfc_state_data *gfc_state_stack;
  31. static bool last_was_use_stmt = false;
  32. /* TODO: Re-order functions to kill these forward decls. */
  33. static void check_statement_label (gfc_statement);
  34. static void undo_new_statement (void);
  35. static void reject_statement (void);
  36. /* A sort of half-matching function. We try to match the word on the
  37. input with the passed string. If this succeeds, we call the
  38. keyword-dependent matching function that will match the rest of the
  39. statement. For single keywords, the matching subroutine is
  40. gfc_match_eos(). */
  41. static match
  42. match_word (const char *str, match (*subr) (void), locus *old_locus)
  43. {
  44. match m;
  45. if (str != NULL)
  46. {
  47. m = gfc_match (str);
  48. if (m != MATCH_YES)
  49. return m;
  50. }
  51. m = (*subr) ();
  52. if (m != MATCH_YES)
  53. {
  54. gfc_current_locus = *old_locus;
  55. reject_statement ();
  56. }
  57. return m;
  58. }
  59. /* Like match_word, but if str is matched, set a flag that it
  60. was matched. */
  61. static match
  62. match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
  63. bool *simd_matched)
  64. {
  65. match m;
  66. if (str != NULL)
  67. {
  68. m = gfc_match (str);
  69. if (m != MATCH_YES)
  70. return m;
  71. *simd_matched = true;
  72. }
  73. m = (*subr) ();
  74. if (m != MATCH_YES)
  75. {
  76. gfc_current_locus = *old_locus;
  77. reject_statement ();
  78. }
  79. return m;
  80. }
  81. /* Load symbols from all USE statements encountered in this scoping unit. */
  82. static void
  83. use_modules (void)
  84. {
  85. gfc_error_buf old_error_1;
  86. output_buffer old_error;
  87. gfc_push_error (&old_error, &old_error_1);
  88. gfc_buffer_error (false);
  89. gfc_use_modules ();
  90. gfc_buffer_error (true);
  91. gfc_pop_error (&old_error, &old_error_1);
  92. gfc_commit_symbols ();
  93. gfc_warning_check ();
  94. gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
  95. gfc_current_ns->old_equiv = gfc_current_ns->equiv;
  96. gfc_current_ns->old_data = gfc_current_ns->data;
  97. last_was_use_stmt = false;
  98. }
  99. /* Figure out what the next statement is, (mostly) regardless of
  100. proper ordering. The do...while(0) is there to prevent if/else
  101. ambiguity. */
  102. #define match(keyword, subr, st) \
  103. do { \
  104. if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
  105. return st; \
  106. else \
  107. undo_new_statement (); \
  108. } while (0);
  109. /* This is a specialist version of decode_statement that is used
  110. for the specification statements in a function, whose
  111. characteristics are deferred into the specification statements.
  112. eg.: INTEGER (king = mykind) foo ()
  113. USE mymodule, ONLY mykind.....
  114. The KIND parameter needs a return after USE or IMPORT, whereas
  115. derived type declarations can occur anywhere, up the executable
  116. block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
  117. out of the correct kind of specification statements. */
  118. static gfc_statement
  119. decode_specification_statement (void)
  120. {
  121. gfc_statement st;
  122. locus old_locus;
  123. char c;
  124. if (gfc_match_eos () == MATCH_YES)
  125. return ST_NONE;
  126. old_locus = gfc_current_locus;
  127. if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
  128. {
  129. last_was_use_stmt = true;
  130. return ST_USE;
  131. }
  132. else
  133. {
  134. undo_new_statement ();
  135. if (last_was_use_stmt)
  136. use_modules ();
  137. }
  138. match ("import", gfc_match_import, ST_IMPORT);
  139. if (gfc_current_block ()->result->ts.type != BT_DERIVED)
  140. goto end_of_block;
  141. match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
  142. match (NULL, gfc_match_data_decl, ST_DATA_DECL);
  143. match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
  144. /* General statement matching: Instead of testing every possible
  145. statement, we eliminate most possibilities by peeking at the
  146. first character. */
  147. c = gfc_peek_ascii_char ();
  148. switch (c)
  149. {
  150. case 'a':
  151. match ("abstract% interface", gfc_match_abstract_interface,
  152. ST_INTERFACE);
  153. match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
  154. match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
  155. break;
  156. case 'b':
  157. match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
  158. break;
  159. case 'c':
  160. match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
  161. match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
  162. break;
  163. case 'd':
  164. match ("data", gfc_match_data, ST_DATA);
  165. match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
  166. break;
  167. case 'e':
  168. match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
  169. match ("entry% ", gfc_match_entry, ST_ENTRY);
  170. match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
  171. match ("external", gfc_match_external, ST_ATTR_DECL);
  172. break;
  173. case 'f':
  174. match ("format", gfc_match_format, ST_FORMAT);
  175. break;
  176. case 'g':
  177. break;
  178. case 'i':
  179. match ("implicit", gfc_match_implicit, ST_IMPLICIT);
  180. match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
  181. match ("interface", gfc_match_interface, ST_INTERFACE);
  182. match ("intent", gfc_match_intent, ST_ATTR_DECL);
  183. match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
  184. break;
  185. case 'm':
  186. break;
  187. case 'n':
  188. match ("namelist", gfc_match_namelist, ST_NAMELIST);
  189. break;
  190. case 'o':
  191. match ("optional", gfc_match_optional, ST_ATTR_DECL);
  192. break;
  193. case 'p':
  194. match ("parameter", gfc_match_parameter, ST_PARAMETER);
  195. match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
  196. if (gfc_match_private (&st) == MATCH_YES)
  197. return st;
  198. match ("procedure", gfc_match_procedure, ST_PROCEDURE);
  199. if (gfc_match_public (&st) == MATCH_YES)
  200. return st;
  201. match ("protected", gfc_match_protected, ST_ATTR_DECL);
  202. break;
  203. case 'r':
  204. break;
  205. case 's':
  206. match ("save", gfc_match_save, ST_ATTR_DECL);
  207. break;
  208. case 't':
  209. match ("target", gfc_match_target, ST_ATTR_DECL);
  210. match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
  211. break;
  212. case 'u':
  213. break;
  214. case 'v':
  215. match ("value", gfc_match_value, ST_ATTR_DECL);
  216. match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
  217. break;
  218. case 'w':
  219. break;
  220. }
  221. /* This is not a specification statement. See if any of the matchers
  222. has stored an error message of some sort. */
  223. end_of_block:
  224. gfc_clear_error ();
  225. gfc_buffer_error (false);
  226. gfc_current_locus = old_locus;
  227. return ST_GET_FCN_CHARACTERISTICS;
  228. }
  229. /* This is the primary 'decode_statement'. */
  230. static gfc_statement
  231. decode_statement (void)
  232. {
  233. gfc_namespace *ns;
  234. gfc_statement st;
  235. locus old_locus;
  236. match m;
  237. char c;
  238. gfc_enforce_clean_symbol_state ();
  239. gfc_clear_error (); /* Clear any pending errors. */
  240. gfc_clear_warning (); /* Clear any pending warnings. */
  241. gfc_matching_function = false;
  242. if (gfc_match_eos () == MATCH_YES)
  243. return ST_NONE;
  244. if (gfc_current_state () == COMP_FUNCTION
  245. && gfc_current_block ()->result->ts.kind == -1)
  246. return decode_specification_statement ();
  247. old_locus = gfc_current_locus;
  248. c = gfc_peek_ascii_char ();
  249. if (c == 'u')
  250. {
  251. if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
  252. {
  253. last_was_use_stmt = true;
  254. return ST_USE;
  255. }
  256. else
  257. undo_new_statement ();
  258. }
  259. if (last_was_use_stmt)
  260. use_modules ();
  261. /* Try matching a data declaration or function declaration. The
  262. input "REALFUNCTIONA(N)" can mean several things in different
  263. contexts, so it (and its relatives) get special treatment. */
  264. if (gfc_current_state () == COMP_NONE
  265. || gfc_current_state () == COMP_INTERFACE
  266. || gfc_current_state () == COMP_CONTAINS)
  267. {
  268. gfc_matching_function = true;
  269. m = gfc_match_function_decl ();
  270. if (m == MATCH_YES)
  271. return ST_FUNCTION;
  272. else if (m == MATCH_ERROR)
  273. reject_statement ();
  274. else
  275. gfc_undo_symbols ();
  276. gfc_current_locus = old_locus;
  277. }
  278. gfc_matching_function = false;
  279. /* Match statements whose error messages are meant to be overwritten
  280. by something better. */
  281. match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
  282. match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
  283. match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
  284. match (NULL, gfc_match_data_decl, ST_DATA_DECL);
  285. match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
  286. /* Try to match a subroutine statement, which has the same optional
  287. prefixes that functions can have. */
  288. if (gfc_match_subroutine () == MATCH_YES)
  289. return ST_SUBROUTINE;
  290. gfc_undo_symbols ();
  291. gfc_current_locus = old_locus;
  292. /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
  293. statements, which might begin with a block label. The match functions for
  294. these statements are unusual in that their keyword is not seen before
  295. the matcher is called. */
  296. if (gfc_match_if (&st) == MATCH_YES)
  297. return st;
  298. gfc_undo_symbols ();
  299. gfc_current_locus = old_locus;
  300. if (gfc_match_where (&st) == MATCH_YES)
  301. return st;
  302. gfc_undo_symbols ();
  303. gfc_current_locus = old_locus;
  304. if (gfc_match_forall (&st) == MATCH_YES)
  305. return st;
  306. gfc_undo_symbols ();
  307. gfc_current_locus = old_locus;
  308. match (NULL, gfc_match_do, ST_DO);
  309. match (NULL, gfc_match_block, ST_BLOCK);
  310. match (NULL, gfc_match_associate, ST_ASSOCIATE);
  311. match (NULL, gfc_match_critical, ST_CRITICAL);
  312. match (NULL, gfc_match_select, ST_SELECT_CASE);
  313. gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
  314. match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
  315. ns = gfc_current_ns;
  316. gfc_current_ns = gfc_current_ns->parent;
  317. gfc_free_namespace (ns);
  318. /* General statement matching: Instead of testing every possible
  319. statement, we eliminate most possibilities by peeking at the
  320. first character. */
  321. switch (c)
  322. {
  323. case 'a':
  324. match ("abstract% interface", gfc_match_abstract_interface,
  325. ST_INTERFACE);
  326. match ("allocate", gfc_match_allocate, ST_ALLOCATE);
  327. match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
  328. match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
  329. match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
  330. break;
  331. case 'b':
  332. match ("backspace", gfc_match_backspace, ST_BACKSPACE);
  333. match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
  334. match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
  335. break;
  336. case 'c':
  337. match ("call", gfc_match_call, ST_CALL);
  338. match ("close", gfc_match_close, ST_CLOSE);
  339. match ("continue", gfc_match_continue, ST_CONTINUE);
  340. match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
  341. match ("cycle", gfc_match_cycle, ST_CYCLE);
  342. match ("case", gfc_match_case, ST_CASE);
  343. match ("common", gfc_match_common, ST_COMMON);
  344. match ("contains", gfc_match_eos, ST_CONTAINS);
  345. match ("class", gfc_match_class_is, ST_CLASS_IS);
  346. match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
  347. break;
  348. case 'd':
  349. match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
  350. match ("data", gfc_match_data, ST_DATA);
  351. match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
  352. break;
  353. case 'e':
  354. match ("end file", gfc_match_endfile, ST_END_FILE);
  355. match ("exit", gfc_match_exit, ST_EXIT);
  356. match ("else", gfc_match_else, ST_ELSE);
  357. match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
  358. match ("else if", gfc_match_elseif, ST_ELSEIF);
  359. match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
  360. match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
  361. if (gfc_match_end (&st) == MATCH_YES)
  362. return st;
  363. match ("entry% ", gfc_match_entry, ST_ENTRY);
  364. match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
  365. match ("external", gfc_match_external, ST_ATTR_DECL);
  366. break;
  367. case 'f':
  368. match ("final", gfc_match_final_decl, ST_FINAL);
  369. match ("flush", gfc_match_flush, ST_FLUSH);
  370. match ("format", gfc_match_format, ST_FORMAT);
  371. break;
  372. case 'g':
  373. match ("generic", gfc_match_generic, ST_GENERIC);
  374. match ("go to", gfc_match_goto, ST_GOTO);
  375. break;
  376. case 'i':
  377. match ("inquire", gfc_match_inquire, ST_INQUIRE);
  378. match ("implicit", gfc_match_implicit, ST_IMPLICIT);
  379. match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
  380. match ("import", gfc_match_import, ST_IMPORT);
  381. match ("interface", gfc_match_interface, ST_INTERFACE);
  382. match ("intent", gfc_match_intent, ST_ATTR_DECL);
  383. match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
  384. break;
  385. case 'l':
  386. match ("lock", gfc_match_lock, ST_LOCK);
  387. break;
  388. case 'm':
  389. match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
  390. match ("module", gfc_match_module, ST_MODULE);
  391. break;
  392. case 'n':
  393. match ("nullify", gfc_match_nullify, ST_NULLIFY);
  394. match ("namelist", gfc_match_namelist, ST_NAMELIST);
  395. break;
  396. case 'o':
  397. match ("open", gfc_match_open, ST_OPEN);
  398. match ("optional", gfc_match_optional, ST_ATTR_DECL);
  399. break;
  400. case 'p':
  401. match ("print", gfc_match_print, ST_WRITE);
  402. match ("parameter", gfc_match_parameter, ST_PARAMETER);
  403. match ("pause", gfc_match_pause, ST_PAUSE);
  404. match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
  405. if (gfc_match_private (&st) == MATCH_YES)
  406. return st;
  407. match ("procedure", gfc_match_procedure, ST_PROCEDURE);
  408. match ("program", gfc_match_program, ST_PROGRAM);
  409. if (gfc_match_public (&st) == MATCH_YES)
  410. return st;
  411. match ("protected", gfc_match_protected, ST_ATTR_DECL);
  412. break;
  413. case 'r':
  414. match ("read", gfc_match_read, ST_READ);
  415. match ("return", gfc_match_return, ST_RETURN);
  416. match ("rewind", gfc_match_rewind, ST_REWIND);
  417. break;
  418. case 's':
  419. match ("sequence", gfc_match_eos, ST_SEQUENCE);
  420. match ("stop", gfc_match_stop, ST_STOP);
  421. match ("save", gfc_match_save, ST_ATTR_DECL);
  422. match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
  423. match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
  424. match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
  425. break;
  426. case 't':
  427. match ("target", gfc_match_target, ST_ATTR_DECL);
  428. match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
  429. match ("type is", gfc_match_type_is, ST_TYPE_IS);
  430. break;
  431. case 'u':
  432. match ("unlock", gfc_match_unlock, ST_UNLOCK);
  433. break;
  434. case 'v':
  435. match ("value", gfc_match_value, ST_ATTR_DECL);
  436. match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
  437. break;
  438. case 'w':
  439. match ("wait", gfc_match_wait, ST_WAIT);
  440. match ("write", gfc_match_write, ST_WRITE);
  441. break;
  442. }
  443. /* All else has failed, so give up. See if any of the matchers has
  444. stored an error message of some sort. */
  445. if (!gfc_error_check ())
  446. gfc_error_now ("Unclassifiable statement at %C");
  447. reject_statement ();
  448. gfc_error_recovery ();
  449. return ST_NONE;
  450. }
  451. /* Like match, but set a flag simd_matched if keyword matched. */
  452. #define matchs(keyword, subr, st) \
  453. do { \
  454. if (match_word_omp_simd (keyword, subr, &old_locus, \
  455. &simd_matched) == MATCH_YES) \
  456. return st; \
  457. else \
  458. undo_new_statement (); \
  459. } while (0);
  460. /* Like match, but don't match anything if not -fopenmp. */
  461. #define matcho(keyword, subr, st) \
  462. do { \
  463. if (!flag_openmp) \
  464. ; \
  465. else if (match_word (keyword, subr, &old_locus) \
  466. == MATCH_YES) \
  467. return st; \
  468. else \
  469. undo_new_statement (); \
  470. } while (0);
  471. static gfc_statement
  472. decode_oacc_directive (void)
  473. {
  474. locus old_locus;
  475. char c;
  476. gfc_enforce_clean_symbol_state ();
  477. gfc_clear_error (); /* Clear any pending errors. */
  478. gfc_clear_warning (); /* Clear any pending warnings. */
  479. if (gfc_pure (NULL))
  480. {
  481. gfc_error_now ("OpenACC directives at %C may not appear in PURE "
  482. "procedures");
  483. gfc_error_recovery ();
  484. return ST_NONE;
  485. }
  486. gfc_unset_implicit_pure (NULL);
  487. old_locus = gfc_current_locus;
  488. /* General OpenACC directive matching: Instead of testing every possible
  489. statement, we eliminate most possibilities by peeking at the
  490. first character. */
  491. c = gfc_peek_ascii_char ();
  492. switch (c)
  493. {
  494. case 'c':
  495. match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
  496. break;
  497. case 'd':
  498. match ("data", gfc_match_oacc_data, ST_OACC_DATA);
  499. match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
  500. break;
  501. case 'e':
  502. match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
  503. match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
  504. match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
  505. match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
  506. match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
  507. match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
  508. match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
  509. match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
  510. match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
  511. break;
  512. case 'h':
  513. match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
  514. break;
  515. case 'p':
  516. match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
  517. match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
  518. break;
  519. case 'k':
  520. match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
  521. match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
  522. break;
  523. case 'l':
  524. match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
  525. break;
  526. case 'r':
  527. match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
  528. break;
  529. case 'u':
  530. match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
  531. break;
  532. case 'w':
  533. match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
  534. break;
  535. }
  536. /* Directive not found or stored an error message.
  537. Check and give up. */
  538. if (gfc_error_check () == 0)
  539. gfc_error_now ("Unclassifiable OpenACC directive at %C");
  540. reject_statement ();
  541. gfc_error_recovery ();
  542. return ST_NONE;
  543. }
  544. static gfc_statement
  545. decode_omp_directive (void)
  546. {
  547. locus old_locus;
  548. char c;
  549. bool simd_matched = false;
  550. gfc_enforce_clean_symbol_state ();
  551. gfc_clear_error (); /* Clear any pending errors. */
  552. gfc_clear_warning (); /* Clear any pending warnings. */
  553. if (gfc_pure (NULL))
  554. {
  555. gfc_error_now ("OpenMP directives at %C may not appear in PURE "
  556. "or ELEMENTAL procedures");
  557. gfc_error_recovery ();
  558. return ST_NONE;
  559. }
  560. gfc_unset_implicit_pure (NULL);
  561. old_locus = gfc_current_locus;
  562. /* General OpenMP directive matching: Instead of testing every possible
  563. statement, we eliminate most possibilities by peeking at the
  564. first character. */
  565. c = gfc_peek_ascii_char ();
  566. /* match is for directives that should be recognized only if
  567. -fopenmp, matchs for directives that should be recognized
  568. if either -fopenmp or -fopenmp-simd. */
  569. switch (c)
  570. {
  571. case 'a':
  572. matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
  573. break;
  574. case 'b':
  575. matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
  576. break;
  577. case 'c':
  578. matcho ("cancellation% point", gfc_match_omp_cancellation_point,
  579. ST_OMP_CANCELLATION_POINT);
  580. matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
  581. matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
  582. break;
  583. case 'd':
  584. matchs ("declare reduction", gfc_match_omp_declare_reduction,
  585. ST_OMP_DECLARE_REDUCTION);
  586. matchs ("declare simd", gfc_match_omp_declare_simd,
  587. ST_OMP_DECLARE_SIMD);
  588. matcho ("declare target", gfc_match_omp_declare_target,
  589. ST_OMP_DECLARE_TARGET);
  590. matchs ("distribute parallel do simd",
  591. gfc_match_omp_distribute_parallel_do_simd,
  592. ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
  593. matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
  594. ST_OMP_DISTRIBUTE_PARALLEL_DO);
  595. matchs ("distribute simd", gfc_match_omp_distribute_simd,
  596. ST_OMP_DISTRIBUTE_SIMD);
  597. matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
  598. matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
  599. matcho ("do", gfc_match_omp_do, ST_OMP_DO);
  600. break;
  601. case 'e':
  602. matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
  603. matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
  604. matchs ("end distribute parallel do simd", gfc_match_omp_eos,
  605. ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
  606. matcho ("end distribute parallel do", gfc_match_omp_eos,
  607. ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
  608. matchs ("end distribute simd", gfc_match_omp_eos,
  609. ST_OMP_END_DISTRIBUTE_SIMD);
  610. matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
  611. matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
  612. matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
  613. matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
  614. matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
  615. matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
  616. matchs ("end parallel do simd", gfc_match_omp_eos,
  617. ST_OMP_END_PARALLEL_DO_SIMD);
  618. matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
  619. matcho ("end parallel sections", gfc_match_omp_eos,
  620. ST_OMP_END_PARALLEL_SECTIONS);
  621. matcho ("end parallel workshare", gfc_match_omp_eos,
  622. ST_OMP_END_PARALLEL_WORKSHARE);
  623. matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
  624. matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
  625. matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
  626. matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
  627. matchs ("end target teams distribute parallel do simd",
  628. gfc_match_omp_eos,
  629. ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
  630. matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
  631. ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
  632. matchs ("end target teams distribute simd", gfc_match_omp_eos,
  633. ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
  634. matcho ("end target teams distribute", gfc_match_omp_eos,
  635. ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
  636. matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
  637. matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
  638. matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
  639. matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
  640. matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
  641. ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
  642. matcho ("end teams distribute parallel do", gfc_match_omp_eos,
  643. ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
  644. matchs ("end teams distribute simd", gfc_match_omp_eos,
  645. ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
  646. matcho ("end teams distribute", gfc_match_omp_eos,
  647. ST_OMP_END_TEAMS_DISTRIBUTE);
  648. matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
  649. matcho ("end workshare", gfc_match_omp_end_nowait,
  650. ST_OMP_END_WORKSHARE);
  651. break;
  652. case 'f':
  653. matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
  654. break;
  655. case 'm':
  656. matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
  657. break;
  658. case 'o':
  659. matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
  660. break;
  661. case 'p':
  662. matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
  663. ST_OMP_PARALLEL_DO_SIMD);
  664. matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
  665. matcho ("parallel sections", gfc_match_omp_parallel_sections,
  666. ST_OMP_PARALLEL_SECTIONS);
  667. matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
  668. ST_OMP_PARALLEL_WORKSHARE);
  669. matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
  670. break;
  671. case 's':
  672. matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
  673. matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
  674. matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
  675. matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
  676. break;
  677. case 't':
  678. matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
  679. matchs ("target teams distribute parallel do simd",
  680. gfc_match_omp_target_teams_distribute_parallel_do_simd,
  681. ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
  682. matcho ("target teams distribute parallel do",
  683. gfc_match_omp_target_teams_distribute_parallel_do,
  684. ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
  685. matchs ("target teams distribute simd",
  686. gfc_match_omp_target_teams_distribute_simd,
  687. ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
  688. matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
  689. ST_OMP_TARGET_TEAMS_DISTRIBUTE);
  690. matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
  691. matcho ("target update", gfc_match_omp_target_update,
  692. ST_OMP_TARGET_UPDATE);
  693. matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
  694. matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
  695. matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
  696. matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
  697. matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
  698. matchs ("teams distribute parallel do simd",
  699. gfc_match_omp_teams_distribute_parallel_do_simd,
  700. ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
  701. matcho ("teams distribute parallel do",
  702. gfc_match_omp_teams_distribute_parallel_do,
  703. ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
  704. matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
  705. ST_OMP_TEAMS_DISTRIBUTE_SIMD);
  706. matcho ("teams distribute", gfc_match_omp_teams_distribute,
  707. ST_OMP_TEAMS_DISTRIBUTE);
  708. matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
  709. matcho ("threadprivate", gfc_match_omp_threadprivate,
  710. ST_OMP_THREADPRIVATE);
  711. break;
  712. case 'w':
  713. matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
  714. break;
  715. }
  716. /* All else has failed, so give up. See if any of the matchers has
  717. stored an error message of some sort. Don't error out if
  718. not -fopenmp and simd_matched is false, i.e. if a directive other
  719. than one marked with match has been seen. */
  720. if (flag_openmp || simd_matched)
  721. {
  722. if (!gfc_error_check ())
  723. gfc_error_now ("Unclassifiable OpenMP directive at %C");
  724. }
  725. reject_statement ();
  726. gfc_error_recovery ();
  727. return ST_NONE;
  728. }
  729. static gfc_statement
  730. decode_gcc_attribute (void)
  731. {
  732. locus old_locus;
  733. gfc_enforce_clean_symbol_state ();
  734. gfc_clear_error (); /* Clear any pending errors. */
  735. gfc_clear_warning (); /* Clear any pending warnings. */
  736. old_locus = gfc_current_locus;
  737. match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
  738. /* All else has failed, so give up. See if any of the matchers has
  739. stored an error message of some sort. */
  740. if (!gfc_error_check ())
  741. gfc_error_now ("Unclassifiable GCC directive at %C");
  742. reject_statement ();
  743. gfc_error_recovery ();
  744. return ST_NONE;
  745. }
  746. #undef match
  747. /* Assert next length characters to be equal to token in free form. */
  748. static void
  749. verify_token_free (const char* token, int length, bool last_was_use_stmt)
  750. {
  751. int i;
  752. char c;
  753. c = gfc_next_ascii_char ();
  754. for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
  755. gcc_assert (c == token[i]);
  756. gcc_assert (gfc_is_whitespace(c));
  757. gfc_gobble_whitespace ();
  758. if (last_was_use_stmt)
  759. use_modules ();
  760. }
  761. /* Get the next statement in free form source. */
  762. static gfc_statement
  763. next_free (void)
  764. {
  765. match m;
  766. int i, cnt, at_bol;
  767. char c;
  768. at_bol = gfc_at_bol ();
  769. gfc_gobble_whitespace ();
  770. c = gfc_peek_ascii_char ();
  771. if (ISDIGIT (c))
  772. {
  773. char d;
  774. /* Found a statement label? */
  775. m = gfc_match_st_label (&gfc_statement_label);
  776. d = gfc_peek_ascii_char ();
  777. if (m != MATCH_YES || !gfc_is_whitespace (d))
  778. {
  779. gfc_match_small_literal_int (&i, &cnt);
  780. if (cnt > 5)
  781. gfc_error_now ("Too many digits in statement label at %C");
  782. if (i == 0)
  783. gfc_error_now ("Zero is not a valid statement label at %C");
  784. do
  785. c = gfc_next_ascii_char ();
  786. while (ISDIGIT(c));
  787. if (!gfc_is_whitespace (c))
  788. gfc_error_now ("Non-numeric character in statement label at %C");
  789. return ST_NONE;
  790. }
  791. else
  792. {
  793. label_locus = gfc_current_locus;
  794. gfc_gobble_whitespace ();
  795. if (at_bol && gfc_peek_ascii_char () == ';')
  796. {
  797. gfc_error_now ("Semicolon at %C needs to be preceded by "
  798. "statement");
  799. gfc_next_ascii_char (); /* Eat up the semicolon. */
  800. return ST_NONE;
  801. }
  802. if (gfc_match_eos () == MATCH_YES)
  803. {
  804. gfc_warning_now (0, "Ignoring statement label in empty statement "
  805. "at %L", &label_locus);
  806. gfc_free_st_label (gfc_statement_label);
  807. gfc_statement_label = NULL;
  808. return ST_NONE;
  809. }
  810. }
  811. }
  812. else if (c == '!')
  813. {
  814. /* Comments have already been skipped by the time we get here,
  815. except for GCC attributes and OpenMP/OpenACC directives. */
  816. gfc_next_ascii_char (); /* Eat up the exclamation sign. */
  817. c = gfc_peek_ascii_char ();
  818. if (c == 'g')
  819. {
  820. int i;
  821. c = gfc_next_ascii_char ();
  822. for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
  823. gcc_assert (c == "gcc$"[i]);
  824. gfc_gobble_whitespace ();
  825. return decode_gcc_attribute ();
  826. }
  827. else if (c == '$')
  828. {
  829. /* Since both OpenMP and OpenACC directives starts with
  830. !$ character sequence, we must check all flags combinations */
  831. if ((flag_openmp || flag_openmp_simd)
  832. && !flag_openacc)
  833. {
  834. verify_token_free ("$omp", 4, last_was_use_stmt);
  835. return decode_omp_directive ();
  836. }
  837. else if ((flag_openmp || flag_openmp_simd)
  838. && flag_openacc)
  839. {
  840. gfc_next_ascii_char (); /* Eat up dollar character */
  841. c = gfc_peek_ascii_char ();
  842. if (c == 'o')
  843. {
  844. verify_token_free ("omp", 3, last_was_use_stmt);
  845. return decode_omp_directive ();
  846. }
  847. else if (c == 'a')
  848. {
  849. verify_token_free ("acc", 3, last_was_use_stmt);
  850. return decode_oacc_directive ();
  851. }
  852. }
  853. else if (flag_openacc)
  854. {
  855. verify_token_free ("$acc", 4, last_was_use_stmt);
  856. return decode_oacc_directive ();
  857. }
  858. }
  859. gcc_unreachable ();
  860. }
  861. if (at_bol && c == ';')
  862. {
  863. if (!(gfc_option.allow_std & GFC_STD_F2008))
  864. gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
  865. "statement");
  866. gfc_next_ascii_char (); /* Eat up the semicolon. */
  867. return ST_NONE;
  868. }
  869. return decode_statement ();
  870. }
  871. /* Assert next length characters to be equal to token in fixed form. */
  872. static bool
  873. verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
  874. {
  875. int i;
  876. char c = gfc_next_char_literal (NONSTRING);
  877. for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
  878. gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
  879. if (c != ' ' && c != '0')
  880. {
  881. gfc_buffer_error (false);
  882. gfc_error ("Bad continuation line at %C");
  883. return false;
  884. }
  885. if (last_was_use_stmt)
  886. use_modules ();
  887. return true;
  888. }
  889. /* Get the next statement in fixed-form source. */
  890. static gfc_statement
  891. next_fixed (void)
  892. {
  893. int label, digit_flag, i;
  894. locus loc;
  895. gfc_char_t c;
  896. if (!gfc_at_bol ())
  897. return decode_statement ();
  898. /* Skip past the current label field, parsing a statement label if
  899. one is there. This is a weird number parser, since the number is
  900. contained within five columns and can have any kind of embedded
  901. spaces. We also check for characters that make the rest of the
  902. line a comment. */
  903. label = 0;
  904. digit_flag = 0;
  905. for (i = 0; i < 5; i++)
  906. {
  907. c = gfc_next_char_literal (NONSTRING);
  908. switch (c)
  909. {
  910. case ' ':
  911. break;
  912. case '0':
  913. case '1':
  914. case '2':
  915. case '3':
  916. case '4':
  917. case '5':
  918. case '6':
  919. case '7':
  920. case '8':
  921. case '9':
  922. label = label * 10 + ((unsigned char) c - '0');
  923. label_locus = gfc_current_locus;
  924. digit_flag = 1;
  925. break;
  926. /* Comments have already been skipped by the time we get
  927. here, except for GCC attributes and OpenMP directives. */
  928. case '*':
  929. c = gfc_next_char_literal (NONSTRING);
  930. if (TOLOWER (c) == 'g')
  931. {
  932. for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
  933. gcc_assert (TOLOWER (c) == "gcc$"[i]);
  934. return decode_gcc_attribute ();
  935. }
  936. else if (c == '$')
  937. {
  938. if ((flag_openmp || flag_openmp_simd)
  939. && !flag_openacc)
  940. {
  941. if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
  942. return ST_NONE;
  943. return decode_omp_directive ();
  944. }
  945. else if ((flag_openmp || flag_openmp_simd)
  946. && flag_openacc)
  947. {
  948. c = gfc_next_char_literal(NONSTRING);
  949. if (c == 'o' || c == 'O')
  950. {
  951. if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
  952. return ST_NONE;
  953. return decode_omp_directive ();
  954. }
  955. else if (c == 'a' || c == 'A')
  956. {
  957. if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
  958. return ST_NONE;
  959. return decode_oacc_directive ();
  960. }
  961. }
  962. else if (flag_openacc)
  963. {
  964. if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
  965. return ST_NONE;
  966. return decode_oacc_directive ();
  967. }
  968. }
  969. /* FALLTHROUGH */
  970. /* Comments have already been skipped by the time we get
  971. here so don't bother checking for them. */
  972. default:
  973. gfc_buffer_error (false);
  974. gfc_error ("Non-numeric character in statement label at %C");
  975. return ST_NONE;
  976. }
  977. }
  978. if (digit_flag)
  979. {
  980. if (label == 0)
  981. gfc_warning_now (0, "Zero is not a valid statement label at %C");
  982. else
  983. {
  984. /* We've found a valid statement label. */
  985. gfc_statement_label = gfc_get_st_label (label);
  986. }
  987. }
  988. /* Since this line starts a statement, it cannot be a continuation
  989. of a previous statement. If we see something here besides a
  990. space or zero, it must be a bad continuation line. */
  991. c = gfc_next_char_literal (NONSTRING);
  992. if (c == '\n')
  993. goto blank_line;
  994. if (c != ' ' && c != '0')
  995. {
  996. gfc_buffer_error (false);
  997. gfc_error ("Bad continuation line at %C");
  998. return ST_NONE;
  999. }
  1000. /* Now that we've taken care of the statement label columns, we have
  1001. to make sure that the first nonblank character is not a '!'. If
  1002. it is, the rest of the line is a comment. */
  1003. do
  1004. {
  1005. loc = gfc_current_locus;
  1006. c = gfc_next_char_literal (NONSTRING);
  1007. }
  1008. while (gfc_is_whitespace (c));
  1009. if (c == '!')
  1010. goto blank_line;
  1011. gfc_current_locus = loc;
  1012. if (c == ';')
  1013. {
  1014. if (digit_flag)
  1015. gfc_error_now ("Semicolon at %C needs to be preceded by statement");
  1016. else if (!(gfc_option.allow_std & GFC_STD_F2008))
  1017. gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
  1018. "statement");
  1019. return ST_NONE;
  1020. }
  1021. if (gfc_match_eos () == MATCH_YES)
  1022. goto blank_line;
  1023. /* At this point, we've got a nonblank statement to parse. */
  1024. return decode_statement ();
  1025. blank_line:
  1026. if (digit_flag)
  1027. gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
  1028. &label_locus);
  1029. gfc_current_locus.lb->truncated = 0;
  1030. gfc_advance_line ();
  1031. return ST_NONE;
  1032. }
  1033. /* Return the next non-ST_NONE statement to the caller. We also worry
  1034. about including files and the ends of include files at this stage. */
  1035. static gfc_statement
  1036. next_statement (void)
  1037. {
  1038. gfc_statement st;
  1039. locus old_locus;
  1040. gfc_enforce_clean_symbol_state ();
  1041. gfc_new_block = NULL;
  1042. gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
  1043. gfc_current_ns->old_equiv = gfc_current_ns->equiv;
  1044. gfc_current_ns->old_data = gfc_current_ns->data;
  1045. for (;;)
  1046. {
  1047. gfc_statement_label = NULL;
  1048. gfc_buffer_error (true);
  1049. if (gfc_at_eol ())
  1050. gfc_advance_line ();
  1051. gfc_skip_comments ();
  1052. if (gfc_at_end ())
  1053. {
  1054. st = ST_NONE;
  1055. break;
  1056. }
  1057. if (gfc_define_undef_line ())
  1058. continue;
  1059. old_locus = gfc_current_locus;
  1060. st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
  1061. if (st != ST_NONE)
  1062. break;
  1063. }
  1064. gfc_buffer_error (false);
  1065. if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
  1066. {
  1067. gfc_free_st_label (gfc_statement_label);
  1068. gfc_statement_label = NULL;
  1069. gfc_current_locus = old_locus;
  1070. }
  1071. if (st != ST_NONE)
  1072. check_statement_label (st);
  1073. return st;
  1074. }
  1075. /****************************** Parser ***********************************/
  1076. /* The parser subroutines are of type 'try' that fail if the file ends
  1077. unexpectedly. */
  1078. /* Macros that expand to case-labels for various classes of
  1079. statements. Start with executable statements that directly do
  1080. things. */
  1081. #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
  1082. case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
  1083. case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
  1084. case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
  1085. case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
  1086. case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
  1087. case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
  1088. case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
  1089. case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
  1090. case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
  1091. case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
  1092. case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
  1093. case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
  1094. case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
  1095. /* Statements that mark other executable statements. */
  1096. #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
  1097. case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
  1098. case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
  1099. case ST_OMP_PARALLEL: \
  1100. case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
  1101. case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
  1102. case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
  1103. case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
  1104. case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
  1105. case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
  1106. case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
  1107. case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
  1108. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
  1109. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
  1110. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
  1111. case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
  1112. case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
  1113. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
  1114. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
  1115. case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
  1116. case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
  1117. case ST_CRITICAL: \
  1118. case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
  1119. case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: case ST_OACC_KERNELS_LOOP
  1120. /* Declaration statements */
  1121. #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
  1122. case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
  1123. case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
  1124. case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
  1125. case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE
  1126. /* Block end statements. Errors associated with interchanging these
  1127. are detected in gfc_match_end(). */
  1128. #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
  1129. case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
  1130. case ST_END_BLOCK: case ST_END_ASSOCIATE
  1131. /* Push a new state onto the stack. */
  1132. static void
  1133. push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
  1134. {
  1135. p->state = new_state;
  1136. p->previous = gfc_state_stack;
  1137. p->sym = sym;
  1138. p->head = p->tail = NULL;
  1139. p->do_variable = NULL;
  1140. if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
  1141. p->ext.oacc_declare_clauses = NULL;
  1142. /* If this the state of a construct like BLOCK, DO or IF, the corresponding
  1143. construct statement was accepted right before pushing the state. Thus,
  1144. the construct's gfc_code is available as tail of the parent state. */
  1145. gcc_assert (gfc_state_stack);
  1146. p->construct = gfc_state_stack->tail;
  1147. gfc_state_stack = p;
  1148. }
  1149. /* Pop the current state. */
  1150. static void
  1151. pop_state (void)
  1152. {
  1153. gfc_state_stack = gfc_state_stack->previous;
  1154. }
  1155. /* Try to find the given state in the state stack. */
  1156. bool
  1157. gfc_find_state (gfc_compile_state state)
  1158. {
  1159. gfc_state_data *p;
  1160. for (p = gfc_state_stack; p; p = p->previous)
  1161. if (p->state == state)
  1162. break;
  1163. return (p == NULL) ? false : true;
  1164. }
  1165. /* Starts a new level in the statement list. */
  1166. static gfc_code *
  1167. new_level (gfc_code *q)
  1168. {
  1169. gfc_code *p;
  1170. p = q->block = gfc_get_code (EXEC_NOP);
  1171. gfc_state_stack->head = gfc_state_stack->tail = p;
  1172. return p;
  1173. }
  1174. /* Add the current new_st code structure and adds it to the current
  1175. program unit. As a side-effect, it zeroes the new_st. */
  1176. static gfc_code *
  1177. add_statement (void)
  1178. {
  1179. gfc_code *p;
  1180. p = XCNEW (gfc_code);
  1181. *p = new_st;
  1182. p->loc = gfc_current_locus;
  1183. if (gfc_state_stack->head == NULL)
  1184. gfc_state_stack->head = p;
  1185. else
  1186. gfc_state_stack->tail->next = p;
  1187. while (p->next != NULL)
  1188. p = p->next;
  1189. gfc_state_stack->tail = p;
  1190. gfc_clear_new_st ();
  1191. return p;
  1192. }
  1193. /* Frees everything associated with the current statement. */
  1194. static void
  1195. undo_new_statement (void)
  1196. {
  1197. gfc_free_statements (new_st.block);
  1198. gfc_free_statements (new_st.next);
  1199. gfc_free_statement (&new_st);
  1200. gfc_clear_new_st ();
  1201. }
  1202. /* If the current statement has a statement label, make sure that it
  1203. is allowed to, or should have one. */
  1204. static void
  1205. check_statement_label (gfc_statement st)
  1206. {
  1207. gfc_sl_type type;
  1208. if (gfc_statement_label == NULL)
  1209. {
  1210. if (st == ST_FORMAT)
  1211. gfc_error ("FORMAT statement at %L does not have a statement label",
  1212. &new_st.loc);
  1213. return;
  1214. }
  1215. switch (st)
  1216. {
  1217. case ST_END_PROGRAM:
  1218. case ST_END_FUNCTION:
  1219. case ST_END_SUBROUTINE:
  1220. case ST_ENDDO:
  1221. case ST_ENDIF:
  1222. case ST_END_SELECT:
  1223. case ST_END_CRITICAL:
  1224. case ST_END_BLOCK:
  1225. case ST_END_ASSOCIATE:
  1226. case_executable:
  1227. case_exec_markers:
  1228. if (st == ST_ENDDO || st == ST_CONTINUE)
  1229. type = ST_LABEL_DO_TARGET;
  1230. else
  1231. type = ST_LABEL_TARGET;
  1232. break;
  1233. case ST_FORMAT:
  1234. type = ST_LABEL_FORMAT;
  1235. break;
  1236. /* Statement labels are not restricted from appearing on a
  1237. particular line. However, there are plenty of situations
  1238. where the resulting label can't be referenced. */
  1239. default:
  1240. type = ST_LABEL_BAD_TARGET;
  1241. break;
  1242. }
  1243. gfc_define_st_label (gfc_statement_label, type, &label_locus);
  1244. new_st.here = gfc_statement_label;
  1245. }
  1246. /* Figures out what the enclosing program unit is. This will be a
  1247. function, subroutine, program, block data or module. */
  1248. gfc_state_data *
  1249. gfc_enclosing_unit (gfc_compile_state * result)
  1250. {
  1251. gfc_state_data *p;
  1252. for (p = gfc_state_stack; p; p = p->previous)
  1253. if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
  1254. || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
  1255. || p->state == COMP_PROGRAM)
  1256. {
  1257. if (result != NULL)
  1258. *result = p->state;
  1259. return p;
  1260. }
  1261. if (result != NULL)
  1262. *result = COMP_PROGRAM;
  1263. return NULL;
  1264. }
  1265. /* Translate a statement enum to a string. */
  1266. const char *
  1267. gfc_ascii_statement (gfc_statement st)
  1268. {
  1269. const char *p;
  1270. switch (st)
  1271. {
  1272. case ST_ARITHMETIC_IF:
  1273. p = _("arithmetic IF");
  1274. break;
  1275. case ST_ALLOCATE:
  1276. p = "ALLOCATE";
  1277. break;
  1278. case ST_ASSOCIATE:
  1279. p = "ASSOCIATE";
  1280. break;
  1281. case ST_ATTR_DECL:
  1282. p = _("attribute declaration");
  1283. break;
  1284. case ST_BACKSPACE:
  1285. p = "BACKSPACE";
  1286. break;
  1287. case ST_BLOCK:
  1288. p = "BLOCK";
  1289. break;
  1290. case ST_BLOCK_DATA:
  1291. p = "BLOCK DATA";
  1292. break;
  1293. case ST_CALL:
  1294. p = "CALL";
  1295. break;
  1296. case ST_CASE:
  1297. p = "CASE";
  1298. break;
  1299. case ST_CLOSE:
  1300. p = "CLOSE";
  1301. break;
  1302. case ST_COMMON:
  1303. p = "COMMON";
  1304. break;
  1305. case ST_CONTINUE:
  1306. p = "CONTINUE";
  1307. break;
  1308. case ST_CONTAINS:
  1309. p = "CONTAINS";
  1310. break;
  1311. case ST_CRITICAL:
  1312. p = "CRITICAL";
  1313. break;
  1314. case ST_CYCLE:
  1315. p = "CYCLE";
  1316. break;
  1317. case ST_DATA_DECL:
  1318. p = _("data declaration");
  1319. break;
  1320. case ST_DATA:
  1321. p = "DATA";
  1322. break;
  1323. case ST_DEALLOCATE:
  1324. p = "DEALLOCATE";
  1325. break;
  1326. case ST_DERIVED_DECL:
  1327. p = _("derived type declaration");
  1328. break;
  1329. case ST_DO:
  1330. p = "DO";
  1331. break;
  1332. case ST_ELSE:
  1333. p = "ELSE";
  1334. break;
  1335. case ST_ELSEIF:
  1336. p = "ELSE IF";
  1337. break;
  1338. case ST_ELSEWHERE:
  1339. p = "ELSEWHERE";
  1340. break;
  1341. case ST_END_ASSOCIATE:
  1342. p = "END ASSOCIATE";
  1343. break;
  1344. case ST_END_BLOCK:
  1345. p = "END BLOCK";
  1346. break;
  1347. case ST_END_BLOCK_DATA:
  1348. p = "END BLOCK DATA";
  1349. break;
  1350. case ST_END_CRITICAL:
  1351. p = "END CRITICAL";
  1352. break;
  1353. case ST_ENDDO:
  1354. p = "END DO";
  1355. break;
  1356. case ST_END_FILE:
  1357. p = "END FILE";
  1358. break;
  1359. case ST_END_FORALL:
  1360. p = "END FORALL";
  1361. break;
  1362. case ST_END_FUNCTION:
  1363. p = "END FUNCTION";
  1364. break;
  1365. case ST_ENDIF:
  1366. p = "END IF";
  1367. break;
  1368. case ST_END_INTERFACE:
  1369. p = "END INTERFACE";
  1370. break;
  1371. case ST_END_MODULE:
  1372. p = "END MODULE";
  1373. break;
  1374. case ST_END_PROGRAM:
  1375. p = "END PROGRAM";
  1376. break;
  1377. case ST_END_SELECT:
  1378. p = "END SELECT";
  1379. break;
  1380. case ST_END_SUBROUTINE:
  1381. p = "END SUBROUTINE";
  1382. break;
  1383. case ST_END_WHERE:
  1384. p = "END WHERE";
  1385. break;
  1386. case ST_END_TYPE:
  1387. p = "END TYPE";
  1388. break;
  1389. case ST_ENTRY:
  1390. p = "ENTRY";
  1391. break;
  1392. case ST_EQUIVALENCE:
  1393. p = "EQUIVALENCE";
  1394. break;
  1395. case ST_ERROR_STOP:
  1396. p = "ERROR STOP";
  1397. break;
  1398. case ST_EXIT:
  1399. p = "EXIT";
  1400. break;
  1401. case ST_FLUSH:
  1402. p = "FLUSH";
  1403. break;
  1404. case ST_FORALL_BLOCK: /* Fall through */
  1405. case ST_FORALL:
  1406. p = "FORALL";
  1407. break;
  1408. case ST_FORMAT:
  1409. p = "FORMAT";
  1410. break;
  1411. case ST_FUNCTION:
  1412. p = "FUNCTION";
  1413. break;
  1414. case ST_GENERIC:
  1415. p = "GENERIC";
  1416. break;
  1417. case ST_GOTO:
  1418. p = "GOTO";
  1419. break;
  1420. case ST_IF_BLOCK:
  1421. p = _("block IF");
  1422. break;
  1423. case ST_IMPLICIT:
  1424. p = "IMPLICIT";
  1425. break;
  1426. case ST_IMPLICIT_NONE:
  1427. p = "IMPLICIT NONE";
  1428. break;
  1429. case ST_IMPLIED_ENDDO:
  1430. p = _("implied END DO");
  1431. break;
  1432. case ST_IMPORT:
  1433. p = "IMPORT";
  1434. break;
  1435. case ST_INQUIRE:
  1436. p = "INQUIRE";
  1437. break;
  1438. case ST_INTERFACE:
  1439. p = "INTERFACE";
  1440. break;
  1441. case ST_LOCK:
  1442. p = "LOCK";
  1443. break;
  1444. case ST_PARAMETER:
  1445. p = "PARAMETER";
  1446. break;
  1447. case ST_PRIVATE:
  1448. p = "PRIVATE";
  1449. break;
  1450. case ST_PUBLIC:
  1451. p = "PUBLIC";
  1452. break;
  1453. case ST_MODULE:
  1454. p = "MODULE";
  1455. break;
  1456. case ST_PAUSE:
  1457. p = "PAUSE";
  1458. break;
  1459. case ST_MODULE_PROC:
  1460. p = "MODULE PROCEDURE";
  1461. break;
  1462. case ST_NAMELIST:
  1463. p = "NAMELIST";
  1464. break;
  1465. case ST_NULLIFY:
  1466. p = "NULLIFY";
  1467. break;
  1468. case ST_OPEN:
  1469. p = "OPEN";
  1470. break;
  1471. case ST_PROGRAM:
  1472. p = "PROGRAM";
  1473. break;
  1474. case ST_PROCEDURE:
  1475. p = "PROCEDURE";
  1476. break;
  1477. case ST_READ:
  1478. p = "READ";
  1479. break;
  1480. case ST_RETURN:
  1481. p = "RETURN";
  1482. break;
  1483. case ST_REWIND:
  1484. p = "REWIND";
  1485. break;
  1486. case ST_STOP:
  1487. p = "STOP";
  1488. break;
  1489. case ST_SYNC_ALL:
  1490. p = "SYNC ALL";
  1491. break;
  1492. case ST_SYNC_IMAGES:
  1493. p = "SYNC IMAGES";
  1494. break;
  1495. case ST_SYNC_MEMORY:
  1496. p = "SYNC MEMORY";
  1497. break;
  1498. case ST_SUBROUTINE:
  1499. p = "SUBROUTINE";
  1500. break;
  1501. case ST_TYPE:
  1502. p = "TYPE";
  1503. break;
  1504. case ST_UNLOCK:
  1505. p = "UNLOCK";
  1506. break;
  1507. case ST_USE:
  1508. p = "USE";
  1509. break;
  1510. case ST_WHERE_BLOCK: /* Fall through */
  1511. case ST_WHERE:
  1512. p = "WHERE";
  1513. break;
  1514. case ST_WAIT:
  1515. p = "WAIT";
  1516. break;
  1517. case ST_WRITE:
  1518. p = "WRITE";
  1519. break;
  1520. case ST_ASSIGNMENT:
  1521. p = _("assignment");
  1522. break;
  1523. case ST_POINTER_ASSIGNMENT:
  1524. p = _("pointer assignment");
  1525. break;
  1526. case ST_SELECT_CASE:
  1527. p = "SELECT CASE";
  1528. break;
  1529. case ST_SELECT_TYPE:
  1530. p = "SELECT TYPE";
  1531. break;
  1532. case ST_TYPE_IS:
  1533. p = "TYPE IS";
  1534. break;
  1535. case ST_CLASS_IS:
  1536. p = "CLASS IS";
  1537. break;
  1538. case ST_SEQUENCE:
  1539. p = "SEQUENCE";
  1540. break;
  1541. case ST_SIMPLE_IF:
  1542. p = _("simple IF");
  1543. break;
  1544. case ST_STATEMENT_FUNCTION:
  1545. p = "STATEMENT FUNCTION";
  1546. break;
  1547. case ST_LABEL_ASSIGNMENT:
  1548. p = "LABEL ASSIGNMENT";
  1549. break;
  1550. case ST_ENUM:
  1551. p = "ENUM DEFINITION";
  1552. break;
  1553. case ST_ENUMERATOR:
  1554. p = "ENUMERATOR DEFINITION";
  1555. break;
  1556. case ST_END_ENUM:
  1557. p = "END ENUM";
  1558. break;
  1559. case ST_OACC_PARALLEL_LOOP:
  1560. p = "!$ACC PARALLEL LOOP";
  1561. break;
  1562. case ST_OACC_END_PARALLEL_LOOP:
  1563. p = "!$ACC END PARALLEL LOOP";
  1564. break;
  1565. case ST_OACC_PARALLEL:
  1566. p = "!$ACC PARALLEL";
  1567. break;
  1568. case ST_OACC_END_PARALLEL:
  1569. p = "!$ACC END PARALLEL";
  1570. break;
  1571. case ST_OACC_KERNELS:
  1572. p = "!$ACC KERNELS";
  1573. break;
  1574. case ST_OACC_END_KERNELS:
  1575. p = "!$ACC END KERNELS";
  1576. break;
  1577. case ST_OACC_KERNELS_LOOP:
  1578. p = "!$ACC KERNELS LOOP";
  1579. break;
  1580. case ST_OACC_END_KERNELS_LOOP:
  1581. p = "!$ACC END KERNELS LOOP";
  1582. break;
  1583. case ST_OACC_DATA:
  1584. p = "!$ACC DATA";
  1585. break;
  1586. case ST_OACC_END_DATA:
  1587. p = "!$ACC END DATA";
  1588. break;
  1589. case ST_OACC_HOST_DATA:
  1590. p = "!$ACC HOST_DATA";
  1591. break;
  1592. case ST_OACC_END_HOST_DATA:
  1593. p = "!$ACC END HOST_DATA";
  1594. break;
  1595. case ST_OACC_LOOP:
  1596. p = "!$ACC LOOP";
  1597. break;
  1598. case ST_OACC_END_LOOP:
  1599. p = "!$ACC END LOOP";
  1600. break;
  1601. case ST_OACC_DECLARE:
  1602. p = "!$ACC DECLARE";
  1603. break;
  1604. case ST_OACC_UPDATE:
  1605. p = "!$ACC UPDATE";
  1606. break;
  1607. case ST_OACC_WAIT:
  1608. p = "!$ACC WAIT";
  1609. break;
  1610. case ST_OACC_CACHE:
  1611. p = "!$ACC CACHE";
  1612. break;
  1613. case ST_OACC_ENTER_DATA:
  1614. p = "!$ACC ENTER DATA";
  1615. break;
  1616. case ST_OACC_EXIT_DATA:
  1617. p = "!$ACC EXIT DATA";
  1618. break;
  1619. case ST_OACC_ROUTINE:
  1620. p = "!$ACC ROUTINE";
  1621. break;
  1622. case ST_OMP_ATOMIC:
  1623. p = "!$OMP ATOMIC";
  1624. break;
  1625. case ST_OMP_BARRIER:
  1626. p = "!$OMP BARRIER";
  1627. break;
  1628. case ST_OMP_CANCEL:
  1629. p = "!$OMP CANCEL";
  1630. break;
  1631. case ST_OMP_CANCELLATION_POINT:
  1632. p = "!$OMP CANCELLATION POINT";
  1633. break;
  1634. case ST_OMP_CRITICAL:
  1635. p = "!$OMP CRITICAL";
  1636. break;
  1637. case ST_OMP_DECLARE_REDUCTION:
  1638. p = "!$OMP DECLARE REDUCTION";
  1639. break;
  1640. case ST_OMP_DECLARE_SIMD:
  1641. p = "!$OMP DECLARE SIMD";
  1642. break;
  1643. case ST_OMP_DECLARE_TARGET:
  1644. p = "!$OMP DECLARE TARGET";
  1645. break;
  1646. case ST_OMP_DISTRIBUTE:
  1647. p = "!$OMP DISTRIBUTE";
  1648. break;
  1649. case ST_OMP_DISTRIBUTE_PARALLEL_DO:
  1650. p = "!$OMP DISTRIBUTE PARALLEL DO";
  1651. break;
  1652. case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
  1653. p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
  1654. break;
  1655. case ST_OMP_DISTRIBUTE_SIMD:
  1656. p = "!$OMP DISTRIBUTE SIMD";
  1657. break;
  1658. case ST_OMP_DO:
  1659. p = "!$OMP DO";
  1660. break;
  1661. case ST_OMP_DO_SIMD:
  1662. p = "!$OMP DO SIMD";
  1663. break;
  1664. case ST_OMP_END_ATOMIC:
  1665. p = "!$OMP END ATOMIC";
  1666. break;
  1667. case ST_OMP_END_CRITICAL:
  1668. p = "!$OMP END CRITICAL";
  1669. break;
  1670. case ST_OMP_END_DISTRIBUTE:
  1671. p = "!$OMP END DISTRIBUTE";
  1672. break;
  1673. case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
  1674. p = "!$OMP END DISTRIBUTE PARALLEL DO";
  1675. break;
  1676. case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
  1677. p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
  1678. break;
  1679. case ST_OMP_END_DISTRIBUTE_SIMD:
  1680. p = "!$OMP END DISTRIBUTE SIMD";
  1681. break;
  1682. case ST_OMP_END_DO:
  1683. p = "!$OMP END DO";
  1684. break;
  1685. case ST_OMP_END_DO_SIMD:
  1686. p = "!$OMP END DO SIMD";
  1687. break;
  1688. case ST_OMP_END_SIMD:
  1689. p = "!$OMP END SIMD";
  1690. break;
  1691. case ST_OMP_END_MASTER:
  1692. p = "!$OMP END MASTER";
  1693. break;
  1694. case ST_OMP_END_ORDERED:
  1695. p = "!$OMP END ORDERED";
  1696. break;
  1697. case ST_OMP_END_PARALLEL:
  1698. p = "!$OMP END PARALLEL";
  1699. break;
  1700. case ST_OMP_END_PARALLEL_DO:
  1701. p = "!$OMP END PARALLEL DO";
  1702. break;
  1703. case ST_OMP_END_PARALLEL_DO_SIMD:
  1704. p = "!$OMP END PARALLEL DO SIMD";
  1705. break;
  1706. case ST_OMP_END_PARALLEL_SECTIONS:
  1707. p = "!$OMP END PARALLEL SECTIONS";
  1708. break;
  1709. case ST_OMP_END_PARALLEL_WORKSHARE:
  1710. p = "!$OMP END PARALLEL WORKSHARE";
  1711. break;
  1712. case ST_OMP_END_SECTIONS:
  1713. p = "!$OMP END SECTIONS";
  1714. break;
  1715. case ST_OMP_END_SINGLE:
  1716. p = "!$OMP END SINGLE";
  1717. break;
  1718. case ST_OMP_END_TASK:
  1719. p = "!$OMP END TASK";
  1720. break;
  1721. case ST_OMP_END_TARGET:
  1722. p = "!$OMP END TARGET";
  1723. break;
  1724. case ST_OMP_END_TARGET_DATA:
  1725. p = "!$OMP END TARGET DATA";
  1726. break;
  1727. case ST_OMP_END_TARGET_TEAMS:
  1728. p = "!$OMP END TARGET TEAMS";
  1729. break;
  1730. case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
  1731. p = "!$OMP END TARGET TEAMS DISTRIBUTE";
  1732. break;
  1733. case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
  1734. p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
  1735. break;
  1736. case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  1737. p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
  1738. break;
  1739. case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
  1740. p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
  1741. break;
  1742. case ST_OMP_END_TASKGROUP:
  1743. p = "!$OMP END TASKGROUP";
  1744. break;
  1745. case ST_OMP_END_TEAMS:
  1746. p = "!$OMP END TEAMS";
  1747. break;
  1748. case ST_OMP_END_TEAMS_DISTRIBUTE:
  1749. p = "!$OMP END TEAMS DISTRIBUTE";
  1750. break;
  1751. case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
  1752. p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
  1753. break;
  1754. case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  1755. p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
  1756. break;
  1757. case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
  1758. p = "!$OMP END TEAMS DISTRIBUTE SIMD";
  1759. break;
  1760. case ST_OMP_END_WORKSHARE:
  1761. p = "!$OMP END WORKSHARE";
  1762. break;
  1763. case ST_OMP_FLUSH:
  1764. p = "!$OMP FLUSH";
  1765. break;
  1766. case ST_OMP_MASTER:
  1767. p = "!$OMP MASTER";
  1768. break;
  1769. case ST_OMP_ORDERED:
  1770. p = "!$OMP ORDERED";
  1771. break;
  1772. case ST_OMP_PARALLEL:
  1773. p = "!$OMP PARALLEL";
  1774. break;
  1775. case ST_OMP_PARALLEL_DO:
  1776. p = "!$OMP PARALLEL DO";
  1777. break;
  1778. case ST_OMP_PARALLEL_DO_SIMD:
  1779. p = "!$OMP PARALLEL DO SIMD";
  1780. break;
  1781. case ST_OMP_PARALLEL_SECTIONS:
  1782. p = "!$OMP PARALLEL SECTIONS";
  1783. break;
  1784. case ST_OMP_PARALLEL_WORKSHARE:
  1785. p = "!$OMP PARALLEL WORKSHARE";
  1786. break;
  1787. case ST_OMP_SECTIONS:
  1788. p = "!$OMP SECTIONS";
  1789. break;
  1790. case ST_OMP_SECTION:
  1791. p = "!$OMP SECTION";
  1792. break;
  1793. case ST_OMP_SIMD:
  1794. p = "!$OMP SIMD";
  1795. break;
  1796. case ST_OMP_SINGLE:
  1797. p = "!$OMP SINGLE";
  1798. break;
  1799. case ST_OMP_TARGET:
  1800. p = "!$OMP TARGET";
  1801. break;
  1802. case ST_OMP_TARGET_DATA:
  1803. p = "!$OMP TARGET DATA";
  1804. break;
  1805. case ST_OMP_TARGET_TEAMS:
  1806. p = "!$OMP TARGET TEAMS";
  1807. break;
  1808. case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
  1809. p = "!$OMP TARGET TEAMS DISTRIBUTE";
  1810. break;
  1811. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
  1812. p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
  1813. break;
  1814. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  1815. p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
  1816. break;
  1817. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
  1818. p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
  1819. break;
  1820. case ST_OMP_TARGET_UPDATE:
  1821. p = "!$OMP TARGET UPDATE";
  1822. break;
  1823. case ST_OMP_TASK:
  1824. p = "!$OMP TASK";
  1825. break;
  1826. case ST_OMP_TASKGROUP:
  1827. p = "!$OMP TASKGROUP";
  1828. break;
  1829. case ST_OMP_TASKWAIT:
  1830. p = "!$OMP TASKWAIT";
  1831. break;
  1832. case ST_OMP_TASKYIELD:
  1833. p = "!$OMP TASKYIELD";
  1834. break;
  1835. case ST_OMP_TEAMS:
  1836. p = "!$OMP TEAMS";
  1837. break;
  1838. case ST_OMP_TEAMS_DISTRIBUTE:
  1839. p = "!$OMP TEAMS DISTRIBUTE";
  1840. break;
  1841. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
  1842. p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
  1843. break;
  1844. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  1845. p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
  1846. break;
  1847. case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
  1848. p = "!$OMP TEAMS DISTRIBUTE SIMD";
  1849. break;
  1850. case ST_OMP_THREADPRIVATE:
  1851. p = "!$OMP THREADPRIVATE";
  1852. break;
  1853. case ST_OMP_WORKSHARE:
  1854. p = "!$OMP WORKSHARE";
  1855. break;
  1856. default:
  1857. gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
  1858. }
  1859. return p;
  1860. }
  1861. /* Create a symbol for the main program and assign it to ns->proc_name. */
  1862. static void
  1863. main_program_symbol (gfc_namespace *ns, const char *name)
  1864. {
  1865. gfc_symbol *main_program;
  1866. symbol_attribute attr;
  1867. gfc_get_symbol (name, ns, &main_program);
  1868. gfc_clear_attr (&attr);
  1869. attr.flavor = FL_PROGRAM;
  1870. attr.proc = PROC_UNKNOWN;
  1871. attr.subroutine = 1;
  1872. attr.access = ACCESS_PUBLIC;
  1873. attr.is_main_program = 1;
  1874. main_program->attr = attr;
  1875. main_program->declared_at = gfc_current_locus;
  1876. ns->proc_name = main_program;
  1877. gfc_commit_symbols ();
  1878. }
  1879. /* Do whatever is necessary to accept the last statement. */
  1880. static void
  1881. accept_statement (gfc_statement st)
  1882. {
  1883. switch (st)
  1884. {
  1885. case ST_IMPLICIT_NONE:
  1886. case ST_IMPLICIT:
  1887. break;
  1888. case ST_FUNCTION:
  1889. case ST_SUBROUTINE:
  1890. case ST_MODULE:
  1891. gfc_current_ns->proc_name = gfc_new_block;
  1892. break;
  1893. /* If the statement is the end of a block, lay down a special code
  1894. that allows a branch to the end of the block from within the
  1895. construct. IF and SELECT are treated differently from DO
  1896. (where EXEC_NOP is added inside the loop) for two
  1897. reasons:
  1898. 1. END DO has a meaning in the sense that after a GOTO to
  1899. it, the loop counter must be increased.
  1900. 2. IF blocks and SELECT blocks can consist of multiple
  1901. parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
  1902. Putting the label before the END IF would make the jump
  1903. from, say, the ELSE IF block to the END IF illegal. */
  1904. case ST_ENDIF:
  1905. case ST_END_SELECT:
  1906. case ST_END_CRITICAL:
  1907. if (gfc_statement_label != NULL)
  1908. {
  1909. new_st.op = EXEC_END_NESTED_BLOCK;
  1910. add_statement ();
  1911. }
  1912. break;
  1913. /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
  1914. one parallel block. Thus, we add the special code to the nested block
  1915. itself, instead of the parent one. */
  1916. case ST_END_BLOCK:
  1917. case ST_END_ASSOCIATE:
  1918. if (gfc_statement_label != NULL)
  1919. {
  1920. new_st.op = EXEC_END_BLOCK;
  1921. add_statement ();
  1922. }
  1923. break;
  1924. /* The end-of-program unit statements do not get the special
  1925. marker and require a statement of some sort if they are a
  1926. branch target. */
  1927. case ST_END_PROGRAM:
  1928. case ST_END_FUNCTION:
  1929. case ST_END_SUBROUTINE:
  1930. if (gfc_statement_label != NULL)
  1931. {
  1932. new_st.op = EXEC_RETURN;
  1933. add_statement ();
  1934. }
  1935. else
  1936. {
  1937. new_st.op = EXEC_END_PROCEDURE;
  1938. add_statement ();
  1939. }
  1940. break;
  1941. case ST_ENTRY:
  1942. case_executable:
  1943. case_exec_markers:
  1944. add_statement ();
  1945. break;
  1946. default:
  1947. break;
  1948. }
  1949. gfc_commit_symbols ();
  1950. gfc_warning_check ();
  1951. gfc_clear_new_st ();
  1952. }
  1953. /* Undo anything tentative that has been built for the current
  1954. statement. */
  1955. static void
  1956. reject_statement (void)
  1957. {
  1958. /* Revert to the previous charlen chain. */
  1959. gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
  1960. gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
  1961. gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
  1962. gfc_current_ns->equiv = gfc_current_ns->old_equiv;
  1963. gfc_reject_data (gfc_current_ns);
  1964. gfc_new_block = NULL;
  1965. gfc_undo_symbols ();
  1966. gfc_clear_warning ();
  1967. undo_new_statement ();
  1968. }
  1969. /* Generic complaint about an out of order statement. We also do
  1970. whatever is necessary to clean up. */
  1971. static void
  1972. unexpected_statement (gfc_statement st)
  1973. {
  1974. gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
  1975. reject_statement ();
  1976. }
  1977. /* Given the next statement seen by the matcher, make sure that it is
  1978. in proper order with the last. This subroutine is initialized by
  1979. calling it with an argument of ST_NONE. If there is a problem, we
  1980. issue an error and return false. Otherwise we return true.
  1981. Individual parsers need to verify that the statements seen are
  1982. valid before calling here, i.e., ENTRY statements are not allowed in
  1983. INTERFACE blocks. The following diagram is taken from the standard:
  1984. +---------------------------------------+
  1985. | program subroutine function module |
  1986. +---------------------------------------+
  1987. | use |
  1988. +---------------------------------------+
  1989. | import |
  1990. +---------------------------------------+
  1991. | | implicit none |
  1992. | +-----------+------------------+
  1993. | | parameter | implicit |
  1994. | +-----------+------------------+
  1995. | format | | derived type |
  1996. | entry | parameter | interface |
  1997. | | data | specification |
  1998. | | | statement func |
  1999. | +-----------+------------------+
  2000. | | data | executable |
  2001. +--------+-----------+------------------+
  2002. | contains |
  2003. +---------------------------------------+
  2004. | internal module/subprogram |
  2005. +---------------------------------------+
  2006. | end |
  2007. +---------------------------------------+
  2008. */
  2009. enum state_order
  2010. {
  2011. ORDER_START,
  2012. ORDER_USE,
  2013. ORDER_IMPORT,
  2014. ORDER_IMPLICIT_NONE,
  2015. ORDER_IMPLICIT,
  2016. ORDER_SPEC,
  2017. ORDER_EXEC
  2018. };
  2019. typedef struct
  2020. {
  2021. enum state_order state;
  2022. gfc_statement last_statement;
  2023. locus where;
  2024. }
  2025. st_state;
  2026. static bool
  2027. verify_st_order (st_state *p, gfc_statement st, bool silent)
  2028. {
  2029. switch (st)
  2030. {
  2031. case ST_NONE:
  2032. p->state = ORDER_START;
  2033. break;
  2034. case ST_USE:
  2035. if (p->state > ORDER_USE)
  2036. goto order;
  2037. p->state = ORDER_USE;
  2038. break;
  2039. case ST_IMPORT:
  2040. if (p->state > ORDER_IMPORT)
  2041. goto order;
  2042. p->state = ORDER_IMPORT;
  2043. break;
  2044. case ST_IMPLICIT_NONE:
  2045. if (p->state > ORDER_IMPLICIT)
  2046. goto order;
  2047. /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
  2048. statement disqualifies a USE but not an IMPLICIT NONE.
  2049. Duplicate IMPLICIT NONEs are caught when the implicit types
  2050. are set. */
  2051. p->state = ORDER_IMPLICIT_NONE;
  2052. break;
  2053. case ST_IMPLICIT:
  2054. if (p->state > ORDER_IMPLICIT)
  2055. goto order;
  2056. p->state = ORDER_IMPLICIT;
  2057. break;
  2058. case ST_FORMAT:
  2059. case ST_ENTRY:
  2060. if (p->state < ORDER_IMPLICIT_NONE)
  2061. p->state = ORDER_IMPLICIT_NONE;
  2062. break;
  2063. case ST_PARAMETER:
  2064. if (p->state >= ORDER_EXEC)
  2065. goto order;
  2066. if (p->state < ORDER_IMPLICIT)
  2067. p->state = ORDER_IMPLICIT;
  2068. break;
  2069. case ST_DATA:
  2070. if (p->state < ORDER_SPEC)
  2071. p->state = ORDER_SPEC;
  2072. break;
  2073. case ST_PUBLIC:
  2074. case ST_PRIVATE:
  2075. case ST_DERIVED_DECL:
  2076. case ST_OACC_DECLARE:
  2077. case_decl:
  2078. if (p->state >= ORDER_EXEC)
  2079. goto order;
  2080. if (p->state < ORDER_SPEC)
  2081. p->state = ORDER_SPEC;
  2082. break;
  2083. case_executable:
  2084. case_exec_markers:
  2085. if (p->state < ORDER_EXEC)
  2086. p->state = ORDER_EXEC;
  2087. break;
  2088. default:
  2089. return false;
  2090. }
  2091. /* All is well, record the statement in case we need it next time. */
  2092. p->where = gfc_current_locus;
  2093. p->last_statement = st;
  2094. return true;
  2095. order:
  2096. if (!silent)
  2097. gfc_error_1 ("%s statement at %C cannot follow %s statement at %L",
  2098. gfc_ascii_statement (st),
  2099. gfc_ascii_statement (p->last_statement), &p->where);
  2100. return false;
  2101. }
  2102. /* Handle an unexpected end of file. This is a show-stopper... */
  2103. static void unexpected_eof (void) ATTRIBUTE_NORETURN;
  2104. static void
  2105. unexpected_eof (void)
  2106. {
  2107. gfc_state_data *p;
  2108. gfc_error ("Unexpected end of file in %qs", gfc_source_file);
  2109. /* Memory cleanup. Move to "second to last". */
  2110. for (p = gfc_state_stack; p && p->previous && p->previous->previous;
  2111. p = p->previous);
  2112. gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
  2113. gfc_done_2 ();
  2114. longjmp (eof_buf, 1);
  2115. }
  2116. /* Parse the CONTAINS section of a derived type definition. */
  2117. gfc_access gfc_typebound_default_access;
  2118. static bool
  2119. parse_derived_contains (void)
  2120. {
  2121. gfc_state_data s;
  2122. bool seen_private = false;
  2123. bool seen_comps = false;
  2124. bool error_flag = false;
  2125. bool to_finish;
  2126. gcc_assert (gfc_current_state () == COMP_DERIVED);
  2127. gcc_assert (gfc_current_block ());
  2128. /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
  2129. section. */
  2130. if (gfc_current_block ()->attr.sequence)
  2131. gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
  2132. " section at %C", gfc_current_block ()->name);
  2133. if (gfc_current_block ()->attr.is_bind_c)
  2134. gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
  2135. " section at %C", gfc_current_block ()->name);
  2136. accept_statement (ST_CONTAINS);
  2137. push_state (&s, COMP_DERIVED_CONTAINS, NULL);
  2138. gfc_typebound_default_access = ACCESS_PUBLIC;
  2139. to_finish = false;
  2140. while (!to_finish)
  2141. {
  2142. gfc_statement st;
  2143. st = next_statement ();
  2144. switch (st)
  2145. {
  2146. case ST_NONE:
  2147. unexpected_eof ();
  2148. break;
  2149. case ST_DATA_DECL:
  2150. gfc_error ("Components in TYPE at %C must precede CONTAINS");
  2151. goto error;
  2152. case ST_PROCEDURE:
  2153. if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
  2154. goto error;
  2155. accept_statement (ST_PROCEDURE);
  2156. seen_comps = true;
  2157. break;
  2158. case ST_GENERIC:
  2159. if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
  2160. goto error;
  2161. accept_statement (ST_GENERIC);
  2162. seen_comps = true;
  2163. break;
  2164. case ST_FINAL:
  2165. if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
  2166. " at %C"))
  2167. goto error;
  2168. accept_statement (ST_FINAL);
  2169. seen_comps = true;
  2170. break;
  2171. case ST_END_TYPE:
  2172. to_finish = true;
  2173. if (!seen_comps
  2174. && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
  2175. "at %C with empty CONTAINS section")))
  2176. goto error;
  2177. /* ST_END_TYPE is accepted by parse_derived after return. */
  2178. break;
  2179. case ST_PRIVATE:
  2180. if (!gfc_find_state (COMP_MODULE))
  2181. {
  2182. gfc_error ("PRIVATE statement in TYPE at %C must be inside "
  2183. "a MODULE");
  2184. goto error;
  2185. }
  2186. if (seen_comps)
  2187. {
  2188. gfc_error ("PRIVATE statement at %C must precede procedure"
  2189. " bindings");
  2190. goto error;
  2191. }
  2192. if (seen_private)
  2193. {
  2194. gfc_error ("Duplicate PRIVATE statement at %C");
  2195. goto error;
  2196. }
  2197. accept_statement (ST_PRIVATE);
  2198. gfc_typebound_default_access = ACCESS_PRIVATE;
  2199. seen_private = true;
  2200. break;
  2201. case ST_SEQUENCE:
  2202. gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
  2203. goto error;
  2204. case ST_CONTAINS:
  2205. gfc_error ("Already inside a CONTAINS block at %C");
  2206. goto error;
  2207. default:
  2208. unexpected_statement (st);
  2209. break;
  2210. }
  2211. continue;
  2212. error:
  2213. error_flag = true;
  2214. reject_statement ();
  2215. }
  2216. pop_state ();
  2217. gcc_assert (gfc_current_state () == COMP_DERIVED);
  2218. return error_flag;
  2219. }
  2220. /* Parse a derived type. */
  2221. static void
  2222. parse_derived (void)
  2223. {
  2224. int compiling_type, seen_private, seen_sequence, seen_component;
  2225. gfc_statement st;
  2226. gfc_state_data s;
  2227. gfc_symbol *sym;
  2228. gfc_component *c, *lock_comp = NULL;
  2229. accept_statement (ST_DERIVED_DECL);
  2230. push_state (&s, COMP_DERIVED, gfc_new_block);
  2231. gfc_new_block->component_access = ACCESS_PUBLIC;
  2232. seen_private = 0;
  2233. seen_sequence = 0;
  2234. seen_component = 0;
  2235. compiling_type = 1;
  2236. while (compiling_type)
  2237. {
  2238. st = next_statement ();
  2239. switch (st)
  2240. {
  2241. case ST_NONE:
  2242. unexpected_eof ();
  2243. case ST_DATA_DECL:
  2244. case ST_PROCEDURE:
  2245. accept_statement (st);
  2246. seen_component = 1;
  2247. break;
  2248. case ST_FINAL:
  2249. gfc_error ("FINAL declaration at %C must be inside CONTAINS");
  2250. break;
  2251. case ST_END_TYPE:
  2252. endType:
  2253. compiling_type = 0;
  2254. if (!seen_component)
  2255. gfc_notify_std (GFC_STD_F2003, "Derived type "
  2256. "definition at %C without components");
  2257. accept_statement (ST_END_TYPE);
  2258. break;
  2259. case ST_PRIVATE:
  2260. if (!gfc_find_state (COMP_MODULE))
  2261. {
  2262. gfc_error ("PRIVATE statement in TYPE at %C must be inside "
  2263. "a MODULE");
  2264. break;
  2265. }
  2266. if (seen_component)
  2267. {
  2268. gfc_error ("PRIVATE statement at %C must precede "
  2269. "structure components");
  2270. break;
  2271. }
  2272. if (seen_private)
  2273. gfc_error ("Duplicate PRIVATE statement at %C");
  2274. s.sym->component_access = ACCESS_PRIVATE;
  2275. accept_statement (ST_PRIVATE);
  2276. seen_private = 1;
  2277. break;
  2278. case ST_SEQUENCE:
  2279. if (seen_component)
  2280. {
  2281. gfc_error ("SEQUENCE statement at %C must precede "
  2282. "structure components");
  2283. break;
  2284. }
  2285. if (gfc_current_block ()->attr.sequence)
  2286. gfc_warning (0, "SEQUENCE attribute at %C already specified in "
  2287. "TYPE statement");
  2288. if (seen_sequence)
  2289. {
  2290. gfc_error ("Duplicate SEQUENCE statement at %C");
  2291. }
  2292. seen_sequence = 1;
  2293. gfc_add_sequence (&gfc_current_block ()->attr,
  2294. gfc_current_block ()->name, NULL);
  2295. break;
  2296. case ST_CONTAINS:
  2297. gfc_notify_std (GFC_STD_F2003,
  2298. "CONTAINS block in derived type"
  2299. " definition at %C");
  2300. accept_statement (ST_CONTAINS);
  2301. parse_derived_contains ();
  2302. goto endType;
  2303. default:
  2304. unexpected_statement (st);
  2305. break;
  2306. }
  2307. }
  2308. /* need to verify that all fields of the derived type are
  2309. * interoperable with C if the type is declared to be bind(c)
  2310. */
  2311. sym = gfc_current_block ();
  2312. for (c = sym->components; c; c = c->next)
  2313. {
  2314. bool coarray, lock_type, allocatable, pointer;
  2315. coarray = lock_type = allocatable = pointer = false;
  2316. /* Look for allocatable components. */
  2317. if (c->attr.allocatable
  2318. || (c->ts.type == BT_CLASS && c->attr.class_ok
  2319. && CLASS_DATA (c)->attr.allocatable)
  2320. || (c->ts.type == BT_DERIVED && !c->attr.pointer
  2321. && c->ts.u.derived->attr.alloc_comp))
  2322. {
  2323. allocatable = true;
  2324. sym->attr.alloc_comp = 1;
  2325. }
  2326. /* Look for pointer components. */
  2327. if (c->attr.pointer
  2328. || (c->ts.type == BT_CLASS && c->attr.class_ok
  2329. && CLASS_DATA (c)->attr.class_pointer)
  2330. || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
  2331. {
  2332. pointer = true;
  2333. sym->attr.pointer_comp = 1;
  2334. }
  2335. /* Look for procedure pointer components. */
  2336. if (c->attr.proc_pointer
  2337. || (c->ts.type == BT_DERIVED
  2338. && c->ts.u.derived->attr.proc_pointer_comp))
  2339. sym->attr.proc_pointer_comp = 1;
  2340. /* Looking for coarray components. */
  2341. if (c->attr.codimension
  2342. || (c->ts.type == BT_CLASS && c->attr.class_ok
  2343. && CLASS_DATA (c)->attr.codimension))
  2344. {
  2345. coarray = true;
  2346. sym->attr.coarray_comp = 1;
  2347. }
  2348. if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
  2349. && !c->attr.pointer)
  2350. {
  2351. coarray = true;
  2352. sym->attr.coarray_comp = 1;
  2353. }
  2354. /* Looking for lock_type components. */
  2355. if ((c->ts.type == BT_DERIVED
  2356. && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
  2357. && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
  2358. || (c->ts.type == BT_CLASS && c->attr.class_ok
  2359. && CLASS_DATA (c)->ts.u.derived->from_intmod
  2360. == INTMOD_ISO_FORTRAN_ENV
  2361. && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
  2362. == ISOFORTRAN_LOCK_TYPE)
  2363. || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
  2364. && !allocatable && !pointer))
  2365. {
  2366. lock_type = 1;
  2367. lock_comp = c;
  2368. sym->attr.lock_comp = 1;
  2369. }
  2370. /* Check for F2008, C1302 - and recall that pointers may not be coarrays
  2371. (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
  2372. unless there are nondirect [allocatable or pointer] components
  2373. involved (cf. 1.3.33.1 and 1.3.33.3). */
  2374. if (pointer && !coarray && lock_type)
  2375. gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
  2376. "codimension or be a subcomponent of a coarray, "
  2377. "which is not possible as the component has the "
  2378. "pointer attribute", c->name, &c->loc);
  2379. else if (pointer && !coarray && c->ts.type == BT_DERIVED
  2380. && c->ts.u.derived->attr.lock_comp)
  2381. gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
  2382. "of type LOCK_TYPE, which must have a codimension or be a "
  2383. "subcomponent of a coarray", c->name, &c->loc);
  2384. if (lock_type && allocatable && !coarray)
  2385. gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
  2386. "a codimension", c->name, &c->loc);
  2387. else if (lock_type && allocatable && c->ts.type == BT_DERIVED
  2388. && c->ts.u.derived->attr.lock_comp)
  2389. gfc_error ("Allocatable component %s at %L must have a codimension as "
  2390. "it has a noncoarray subcomponent of type LOCK_TYPE",
  2391. c->name, &c->loc);
  2392. if (sym->attr.coarray_comp && !coarray && lock_type)
  2393. gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
  2394. "subcomponent of type LOCK_TYPE must have a codimension or "
  2395. "be a subcomponent of a coarray. (Variables of type %s may "
  2396. "not have a codimension as already a coarray "
  2397. "subcomponent exists)", c->name, &c->loc, sym->name);
  2398. if (sym->attr.lock_comp && coarray && !lock_type)
  2399. gfc_error_1 ("Noncoarray component %s at %L of type LOCK_TYPE or with "
  2400. "subcomponent of type LOCK_TYPE must have a codimension or "
  2401. "be a subcomponent of a coarray. (Variables of type %s may "
  2402. "not have a codimension as %s at %L has a codimension or a "
  2403. "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
  2404. sym->name, c->name, &c->loc);
  2405. /* Look for private components. */
  2406. if (sym->component_access == ACCESS_PRIVATE
  2407. || c->attr.access == ACCESS_PRIVATE
  2408. || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
  2409. sym->attr.private_comp = 1;
  2410. }
  2411. if (!seen_component)
  2412. sym->attr.zero_comp = 1;
  2413. pop_state ();
  2414. }
  2415. /* Parse an ENUM. */
  2416. static void
  2417. parse_enum (void)
  2418. {
  2419. gfc_statement st;
  2420. int compiling_enum;
  2421. gfc_state_data s;
  2422. int seen_enumerator = 0;
  2423. push_state (&s, COMP_ENUM, gfc_new_block);
  2424. compiling_enum = 1;
  2425. while (compiling_enum)
  2426. {
  2427. st = next_statement ();
  2428. switch (st)
  2429. {
  2430. case ST_NONE:
  2431. unexpected_eof ();
  2432. break;
  2433. case ST_ENUMERATOR:
  2434. seen_enumerator = 1;
  2435. accept_statement (st);
  2436. break;
  2437. case ST_END_ENUM:
  2438. compiling_enum = 0;
  2439. if (!seen_enumerator)
  2440. gfc_error ("ENUM declaration at %C has no ENUMERATORS");
  2441. accept_statement (st);
  2442. break;
  2443. default:
  2444. gfc_free_enum_history ();
  2445. unexpected_statement (st);
  2446. break;
  2447. }
  2448. }
  2449. pop_state ();
  2450. }
  2451. /* Parse an interface. We must be able to deal with the possibility
  2452. of recursive interfaces. The parse_spec() subroutine is mutually
  2453. recursive with parse_interface(). */
  2454. static gfc_statement parse_spec (gfc_statement);
  2455. static void
  2456. parse_interface (void)
  2457. {
  2458. gfc_compile_state new_state = COMP_NONE, current_state;
  2459. gfc_symbol *prog_unit, *sym;
  2460. gfc_interface_info save;
  2461. gfc_state_data s1, s2;
  2462. gfc_statement st;
  2463. accept_statement (ST_INTERFACE);
  2464. current_interface.ns = gfc_current_ns;
  2465. save = current_interface;
  2466. sym = (current_interface.type == INTERFACE_GENERIC
  2467. || current_interface.type == INTERFACE_USER_OP)
  2468. ? gfc_new_block : NULL;
  2469. push_state (&s1, COMP_INTERFACE, sym);
  2470. current_state = COMP_NONE;
  2471. loop:
  2472. gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
  2473. st = next_statement ();
  2474. switch (st)
  2475. {
  2476. case ST_NONE:
  2477. unexpected_eof ();
  2478. case ST_SUBROUTINE:
  2479. case ST_FUNCTION:
  2480. if (st == ST_SUBROUTINE)
  2481. new_state = COMP_SUBROUTINE;
  2482. else if (st == ST_FUNCTION)
  2483. new_state = COMP_FUNCTION;
  2484. if (gfc_new_block->attr.pointer)
  2485. {
  2486. gfc_new_block->attr.pointer = 0;
  2487. gfc_new_block->attr.proc_pointer = 1;
  2488. }
  2489. if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
  2490. gfc_new_block->formal, NULL))
  2491. {
  2492. reject_statement ();
  2493. gfc_free_namespace (gfc_current_ns);
  2494. goto loop;
  2495. }
  2496. break;
  2497. case ST_PROCEDURE:
  2498. case ST_MODULE_PROC: /* The module procedure matcher makes
  2499. sure the context is correct. */
  2500. accept_statement (st);
  2501. gfc_free_namespace (gfc_current_ns);
  2502. goto loop;
  2503. case ST_END_INTERFACE:
  2504. gfc_free_namespace (gfc_current_ns);
  2505. gfc_current_ns = current_interface.ns;
  2506. goto done;
  2507. default:
  2508. gfc_error ("Unexpected %s statement in INTERFACE block at %C",
  2509. gfc_ascii_statement (st));
  2510. reject_statement ();
  2511. gfc_free_namespace (gfc_current_ns);
  2512. goto loop;
  2513. }
  2514. /* Make sure that the generic name has the right attribute. */
  2515. if (current_interface.type == INTERFACE_GENERIC
  2516. && current_state == COMP_NONE)
  2517. {
  2518. if (new_state == COMP_FUNCTION && sym)
  2519. gfc_add_function (&sym->attr, sym->name, NULL);
  2520. else if (new_state == COMP_SUBROUTINE && sym)
  2521. gfc_add_subroutine (&sym->attr, sym->name, NULL);
  2522. current_state = new_state;
  2523. }
  2524. if (current_interface.type == INTERFACE_ABSTRACT)
  2525. {
  2526. gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
  2527. if (gfc_is_intrinsic_typename (gfc_new_block->name))
  2528. gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
  2529. "cannot be the same as an intrinsic type",
  2530. gfc_new_block->name);
  2531. }
  2532. push_state (&s2, new_state, gfc_new_block);
  2533. accept_statement (st);
  2534. prog_unit = gfc_new_block;
  2535. prog_unit->formal_ns = gfc_current_ns;
  2536. if (prog_unit == prog_unit->formal_ns->proc_name
  2537. && prog_unit->ns != prog_unit->formal_ns)
  2538. prog_unit->refs++;
  2539. decl:
  2540. /* Read data declaration statements. */
  2541. st = parse_spec (ST_NONE);
  2542. /* Since the interface block does not permit an IMPLICIT statement,
  2543. the default type for the function or the result must be taken
  2544. from the formal namespace. */
  2545. if (new_state == COMP_FUNCTION)
  2546. {
  2547. if (prog_unit->result == prog_unit
  2548. && prog_unit->ts.type == BT_UNKNOWN)
  2549. gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
  2550. else if (prog_unit->result != prog_unit
  2551. && prog_unit->result->ts.type == BT_UNKNOWN)
  2552. gfc_set_default_type (prog_unit->result, 1,
  2553. prog_unit->formal_ns);
  2554. }
  2555. if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
  2556. {
  2557. gfc_error ("Unexpected %s statement at %C in INTERFACE body",
  2558. gfc_ascii_statement (st));
  2559. reject_statement ();
  2560. goto decl;
  2561. }
  2562. /* Add EXTERNAL attribute to function or subroutine. */
  2563. if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
  2564. gfc_add_external (&prog_unit->attr, &gfc_current_locus);
  2565. current_interface = save;
  2566. gfc_add_interface (prog_unit);
  2567. pop_state ();
  2568. if (current_interface.ns
  2569. && current_interface.ns->proc_name
  2570. && strcmp (current_interface.ns->proc_name->name,
  2571. prog_unit->name) == 0)
  2572. gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
  2573. "enclosing procedure", prog_unit->name,
  2574. &current_interface.ns->proc_name->declared_at);
  2575. goto loop;
  2576. done:
  2577. pop_state ();
  2578. }
  2579. /* Associate function characteristics by going back to the function
  2580. declaration and rematching the prefix. */
  2581. static match
  2582. match_deferred_characteristics (gfc_typespec * ts)
  2583. {
  2584. locus loc;
  2585. match m = MATCH_ERROR;
  2586. char name[GFC_MAX_SYMBOL_LEN + 1];
  2587. loc = gfc_current_locus;
  2588. gfc_current_locus = gfc_current_block ()->declared_at;
  2589. gfc_clear_error ();
  2590. gfc_buffer_error (true);
  2591. m = gfc_match_prefix (ts);
  2592. gfc_buffer_error (false);
  2593. if (ts->type == BT_DERIVED)
  2594. {
  2595. ts->kind = 0;
  2596. if (!ts->u.derived)
  2597. m = MATCH_ERROR;
  2598. }
  2599. /* Only permit one go at the characteristic association. */
  2600. if (ts->kind == -1)
  2601. ts->kind = 0;
  2602. /* Set the function locus correctly. If we have not found the
  2603. function name, there is an error. */
  2604. if (m == MATCH_YES
  2605. && gfc_match ("function% %n", name) == MATCH_YES
  2606. && strcmp (name, gfc_current_block ()->name) == 0)
  2607. {
  2608. gfc_current_block ()->declared_at = gfc_current_locus;
  2609. gfc_commit_symbols ();
  2610. }
  2611. else
  2612. {
  2613. gfc_error_check ();
  2614. gfc_undo_symbols ();
  2615. }
  2616. gfc_current_locus =loc;
  2617. return m;
  2618. }
  2619. /* Check specification-expressions in the function result of the currently
  2620. parsed block and ensure they are typed (give an IMPLICIT type if necessary).
  2621. For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
  2622. scope are not yet parsed so this has to be delayed up to parse_spec. */
  2623. static void
  2624. check_function_result_typed (void)
  2625. {
  2626. gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
  2627. gcc_assert (gfc_current_state () == COMP_FUNCTION);
  2628. gcc_assert (ts->type != BT_UNKNOWN);
  2629. /* Check type-parameters, at the moment only CHARACTER lengths possible. */
  2630. /* TODO: Extend when KIND type parameters are implemented. */
  2631. if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
  2632. gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
  2633. }
  2634. /* Parse a set of specification statements. Returns the statement
  2635. that doesn't fit. */
  2636. static gfc_statement
  2637. parse_spec (gfc_statement st)
  2638. {
  2639. st_state ss;
  2640. bool function_result_typed = false;
  2641. bool bad_characteristic = false;
  2642. gfc_typespec *ts;
  2643. verify_st_order (&ss, ST_NONE, false);
  2644. if (st == ST_NONE)
  2645. st = next_statement ();
  2646. /* If we are not inside a function or don't have a result specified so far,
  2647. do nothing special about it. */
  2648. if (gfc_current_state () != COMP_FUNCTION)
  2649. function_result_typed = true;
  2650. else
  2651. {
  2652. gfc_symbol* proc = gfc_current_ns->proc_name;
  2653. gcc_assert (proc);
  2654. if (proc->result->ts.type == BT_UNKNOWN)
  2655. function_result_typed = true;
  2656. }
  2657. loop:
  2658. /* If we're inside a BLOCK construct, some statements are disallowed.
  2659. Check this here. Attribute declaration statements like INTENT, OPTIONAL
  2660. or VALUE are also disallowed, but they don't have a particular ST_*
  2661. key so we have to check for them individually in their matcher routine. */
  2662. if (gfc_current_state () == COMP_BLOCK)
  2663. switch (st)
  2664. {
  2665. case ST_IMPLICIT:
  2666. case ST_IMPLICIT_NONE:
  2667. case ST_NAMELIST:
  2668. case ST_COMMON:
  2669. case ST_EQUIVALENCE:
  2670. case ST_STATEMENT_FUNCTION:
  2671. gfc_error ("%s statement is not allowed inside of BLOCK at %C",
  2672. gfc_ascii_statement (st));
  2673. reject_statement ();
  2674. break;
  2675. default:
  2676. break;
  2677. }
  2678. else if (gfc_current_state () == COMP_BLOCK_DATA)
  2679. /* Fortran 2008, C1116. */
  2680. switch (st)
  2681. {
  2682. case ST_DATA_DECL:
  2683. case ST_COMMON:
  2684. case ST_DATA:
  2685. case ST_TYPE:
  2686. case ST_END_BLOCK_DATA:
  2687. case ST_ATTR_DECL:
  2688. case ST_EQUIVALENCE:
  2689. case ST_PARAMETER:
  2690. case ST_IMPLICIT:
  2691. case ST_IMPLICIT_NONE:
  2692. case ST_DERIVED_DECL:
  2693. case ST_USE:
  2694. break;
  2695. case ST_NONE:
  2696. break;
  2697. default:
  2698. gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
  2699. gfc_ascii_statement (st));
  2700. reject_statement ();
  2701. break;
  2702. }
  2703. /* If we find a statement that can not be followed by an IMPLICIT statement
  2704. (and thus we can expect to see none any further), type the function result
  2705. if it has not yet been typed. Be careful not to give the END statement
  2706. to verify_st_order! */
  2707. if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
  2708. {
  2709. bool verify_now = false;
  2710. if (st == ST_END_FUNCTION || st == ST_CONTAINS)
  2711. verify_now = true;
  2712. else
  2713. {
  2714. st_state dummyss;
  2715. verify_st_order (&dummyss, ST_NONE, false);
  2716. verify_st_order (&dummyss, st, false);
  2717. if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
  2718. verify_now = true;
  2719. }
  2720. if (verify_now)
  2721. {
  2722. check_function_result_typed ();
  2723. function_result_typed = true;
  2724. }
  2725. }
  2726. switch (st)
  2727. {
  2728. case ST_NONE:
  2729. unexpected_eof ();
  2730. case ST_IMPLICIT_NONE:
  2731. case ST_IMPLICIT:
  2732. if (!function_result_typed)
  2733. {
  2734. check_function_result_typed ();
  2735. function_result_typed = true;
  2736. }
  2737. goto declSt;
  2738. case ST_FORMAT:
  2739. case ST_ENTRY:
  2740. case ST_DATA: /* Not allowed in interfaces */
  2741. if (gfc_current_state () == COMP_INTERFACE)
  2742. break;
  2743. /* Fall through */
  2744. case ST_USE:
  2745. case ST_IMPORT:
  2746. case ST_PARAMETER:
  2747. case ST_PUBLIC:
  2748. case ST_PRIVATE:
  2749. case ST_DERIVED_DECL:
  2750. case_decl:
  2751. declSt:
  2752. if (!verify_st_order (&ss, st, false))
  2753. {
  2754. reject_statement ();
  2755. st = next_statement ();
  2756. goto loop;
  2757. }
  2758. switch (st)
  2759. {
  2760. case ST_INTERFACE:
  2761. parse_interface ();
  2762. break;
  2763. case ST_DERIVED_DECL:
  2764. parse_derived ();
  2765. break;
  2766. case ST_PUBLIC:
  2767. case ST_PRIVATE:
  2768. if (gfc_current_state () != COMP_MODULE)
  2769. {
  2770. gfc_error ("%s statement must appear in a MODULE",
  2771. gfc_ascii_statement (st));
  2772. reject_statement ();
  2773. break;
  2774. }
  2775. if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
  2776. {
  2777. gfc_error ("%s statement at %C follows another accessibility "
  2778. "specification", gfc_ascii_statement (st));
  2779. reject_statement ();
  2780. break;
  2781. }
  2782. gfc_current_ns->default_access = (st == ST_PUBLIC)
  2783. ? ACCESS_PUBLIC : ACCESS_PRIVATE;
  2784. break;
  2785. case ST_STATEMENT_FUNCTION:
  2786. if (gfc_current_state () == COMP_MODULE)
  2787. {
  2788. unexpected_statement (st);
  2789. break;
  2790. }
  2791. default:
  2792. break;
  2793. }
  2794. accept_statement (st);
  2795. st = next_statement ();
  2796. goto loop;
  2797. case ST_ENUM:
  2798. accept_statement (st);
  2799. parse_enum();
  2800. st = next_statement ();
  2801. goto loop;
  2802. case ST_GET_FCN_CHARACTERISTICS:
  2803. /* This statement triggers the association of a function's result
  2804. characteristics. */
  2805. ts = &gfc_current_block ()->result->ts;
  2806. if (match_deferred_characteristics (ts) != MATCH_YES)
  2807. bad_characteristic = true;
  2808. st = next_statement ();
  2809. goto loop;
  2810. case ST_OACC_DECLARE:
  2811. if (!verify_st_order(&ss, st, false))
  2812. {
  2813. reject_statement ();
  2814. st = next_statement ();
  2815. goto loop;
  2816. }
  2817. if (gfc_state_stack->ext.oacc_declare_clauses == NULL)
  2818. gfc_state_stack->ext.oacc_declare_clauses = new_st.ext.omp_clauses;
  2819. accept_statement (st);
  2820. st = next_statement ();
  2821. goto loop;
  2822. default:
  2823. break;
  2824. }
  2825. /* If match_deferred_characteristics failed, then there is an error. */
  2826. if (bad_characteristic)
  2827. {
  2828. ts = &gfc_current_block ()->result->ts;
  2829. if (ts->type != BT_DERIVED)
  2830. gfc_error ("Bad kind expression for function %qs at %L",
  2831. gfc_current_block ()->name,
  2832. &gfc_current_block ()->declared_at);
  2833. else
  2834. gfc_error ("The type for function %qs at %L is not accessible",
  2835. gfc_current_block ()->name,
  2836. &gfc_current_block ()->declared_at);
  2837. gfc_current_block ()->ts.kind = 0;
  2838. /* Keep the derived type; if it's bad, it will be discovered later. */
  2839. if (!(ts->type == BT_DERIVED && ts->u.derived))
  2840. ts->type = BT_UNKNOWN;
  2841. }
  2842. return st;
  2843. }
  2844. /* Parse a WHERE block, (not a simple WHERE statement). */
  2845. static void
  2846. parse_where_block (void)
  2847. {
  2848. int seen_empty_else;
  2849. gfc_code *top, *d;
  2850. gfc_state_data s;
  2851. gfc_statement st;
  2852. accept_statement (ST_WHERE_BLOCK);
  2853. top = gfc_state_stack->tail;
  2854. push_state (&s, COMP_WHERE, gfc_new_block);
  2855. d = add_statement ();
  2856. d->expr1 = top->expr1;
  2857. d->op = EXEC_WHERE;
  2858. top->expr1 = NULL;
  2859. top->block = d;
  2860. seen_empty_else = 0;
  2861. do
  2862. {
  2863. st = next_statement ();
  2864. switch (st)
  2865. {
  2866. case ST_NONE:
  2867. unexpected_eof ();
  2868. case ST_WHERE_BLOCK:
  2869. parse_where_block ();
  2870. break;
  2871. case ST_ASSIGNMENT:
  2872. case ST_WHERE:
  2873. accept_statement (st);
  2874. break;
  2875. case ST_ELSEWHERE:
  2876. if (seen_empty_else)
  2877. {
  2878. gfc_error ("ELSEWHERE statement at %C follows previous "
  2879. "unmasked ELSEWHERE");
  2880. reject_statement ();
  2881. break;
  2882. }
  2883. if (new_st.expr1 == NULL)
  2884. seen_empty_else = 1;
  2885. d = new_level (gfc_state_stack->head);
  2886. d->op = EXEC_WHERE;
  2887. d->expr1 = new_st.expr1;
  2888. accept_statement (st);
  2889. break;
  2890. case ST_END_WHERE:
  2891. accept_statement (st);
  2892. break;
  2893. default:
  2894. gfc_error ("Unexpected %s statement in WHERE block at %C",
  2895. gfc_ascii_statement (st));
  2896. reject_statement ();
  2897. break;
  2898. }
  2899. }
  2900. while (st != ST_END_WHERE);
  2901. pop_state ();
  2902. }
  2903. /* Parse a FORALL block (not a simple FORALL statement). */
  2904. static void
  2905. parse_forall_block (void)
  2906. {
  2907. gfc_code *top, *d;
  2908. gfc_state_data s;
  2909. gfc_statement st;
  2910. accept_statement (ST_FORALL_BLOCK);
  2911. top = gfc_state_stack->tail;
  2912. push_state (&s, COMP_FORALL, gfc_new_block);
  2913. d = add_statement ();
  2914. d->op = EXEC_FORALL;
  2915. top->block = d;
  2916. do
  2917. {
  2918. st = next_statement ();
  2919. switch (st)
  2920. {
  2921. case ST_ASSIGNMENT:
  2922. case ST_POINTER_ASSIGNMENT:
  2923. case ST_WHERE:
  2924. case ST_FORALL:
  2925. accept_statement (st);
  2926. break;
  2927. case ST_WHERE_BLOCK:
  2928. parse_where_block ();
  2929. break;
  2930. case ST_FORALL_BLOCK:
  2931. parse_forall_block ();
  2932. break;
  2933. case ST_END_FORALL:
  2934. accept_statement (st);
  2935. break;
  2936. case ST_NONE:
  2937. unexpected_eof ();
  2938. default:
  2939. gfc_error ("Unexpected %s statement in FORALL block at %C",
  2940. gfc_ascii_statement (st));
  2941. reject_statement ();
  2942. break;
  2943. }
  2944. }
  2945. while (st != ST_END_FORALL);
  2946. pop_state ();
  2947. }
  2948. static gfc_statement parse_executable (gfc_statement);
  2949. /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
  2950. static void
  2951. parse_if_block (void)
  2952. {
  2953. gfc_code *top, *d;
  2954. gfc_statement st;
  2955. locus else_locus;
  2956. gfc_state_data s;
  2957. int seen_else;
  2958. seen_else = 0;
  2959. accept_statement (ST_IF_BLOCK);
  2960. top = gfc_state_stack->tail;
  2961. push_state (&s, COMP_IF, gfc_new_block);
  2962. new_st.op = EXEC_IF;
  2963. d = add_statement ();
  2964. d->expr1 = top->expr1;
  2965. top->expr1 = NULL;
  2966. top->block = d;
  2967. do
  2968. {
  2969. st = parse_executable (ST_NONE);
  2970. switch (st)
  2971. {
  2972. case ST_NONE:
  2973. unexpected_eof ();
  2974. case ST_ELSEIF:
  2975. if (seen_else)
  2976. {
  2977. gfc_error_1 ("ELSE IF statement at %C cannot follow ELSE "
  2978. "statement at %L", &else_locus);
  2979. reject_statement ();
  2980. break;
  2981. }
  2982. d = new_level (gfc_state_stack->head);
  2983. d->op = EXEC_IF;
  2984. d->expr1 = new_st.expr1;
  2985. accept_statement (st);
  2986. break;
  2987. case ST_ELSE:
  2988. if (seen_else)
  2989. {
  2990. gfc_error ("Duplicate ELSE statements at %L and %C",
  2991. &else_locus);
  2992. reject_statement ();
  2993. break;
  2994. }
  2995. seen_else = 1;
  2996. else_locus = gfc_current_locus;
  2997. d = new_level (gfc_state_stack->head);
  2998. d->op = EXEC_IF;
  2999. accept_statement (st);
  3000. break;
  3001. case ST_ENDIF:
  3002. break;
  3003. default:
  3004. unexpected_statement (st);
  3005. break;
  3006. }
  3007. }
  3008. while (st != ST_ENDIF);
  3009. pop_state ();
  3010. accept_statement (st);
  3011. }
  3012. /* Parse a SELECT block. */
  3013. static void
  3014. parse_select_block (void)
  3015. {
  3016. gfc_statement st;
  3017. gfc_code *cp;
  3018. gfc_state_data s;
  3019. accept_statement (ST_SELECT_CASE);
  3020. cp = gfc_state_stack->tail;
  3021. push_state (&s, COMP_SELECT, gfc_new_block);
  3022. /* Make sure that the next statement is a CASE or END SELECT. */
  3023. for (;;)
  3024. {
  3025. st = next_statement ();
  3026. if (st == ST_NONE)
  3027. unexpected_eof ();
  3028. if (st == ST_END_SELECT)
  3029. {
  3030. /* Empty SELECT CASE is OK. */
  3031. accept_statement (st);
  3032. pop_state ();
  3033. return;
  3034. }
  3035. if (st == ST_CASE)
  3036. break;
  3037. gfc_error ("Expected a CASE or END SELECT statement following SELECT "
  3038. "CASE at %C");
  3039. reject_statement ();
  3040. }
  3041. /* At this point, we're got a nonempty select block. */
  3042. cp = new_level (cp);
  3043. *cp = new_st;
  3044. accept_statement (st);
  3045. do
  3046. {
  3047. st = parse_executable (ST_NONE);
  3048. switch (st)
  3049. {
  3050. case ST_NONE:
  3051. unexpected_eof ();
  3052. case ST_CASE:
  3053. cp = new_level (gfc_state_stack->head);
  3054. *cp = new_st;
  3055. gfc_clear_new_st ();
  3056. accept_statement (st);
  3057. /* Fall through */
  3058. case ST_END_SELECT:
  3059. break;
  3060. /* Can't have an executable statement because of
  3061. parse_executable(). */
  3062. default:
  3063. unexpected_statement (st);
  3064. break;
  3065. }
  3066. }
  3067. while (st != ST_END_SELECT);
  3068. pop_state ();
  3069. accept_statement (st);
  3070. }
  3071. /* Pop the current selector from the SELECT TYPE stack. */
  3072. static void
  3073. select_type_pop (void)
  3074. {
  3075. gfc_select_type_stack *old = select_type_stack;
  3076. select_type_stack = old->prev;
  3077. free (old);
  3078. }
  3079. /* Parse a SELECT TYPE construct (F03:R821). */
  3080. static void
  3081. parse_select_type_block (void)
  3082. {
  3083. gfc_statement st;
  3084. gfc_code *cp;
  3085. gfc_state_data s;
  3086. accept_statement (ST_SELECT_TYPE);
  3087. cp = gfc_state_stack->tail;
  3088. push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
  3089. /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
  3090. or END SELECT. */
  3091. for (;;)
  3092. {
  3093. st = next_statement ();
  3094. if (st == ST_NONE)
  3095. unexpected_eof ();
  3096. if (st == ST_END_SELECT)
  3097. /* Empty SELECT CASE is OK. */
  3098. goto done;
  3099. if (st == ST_TYPE_IS || st == ST_CLASS_IS)
  3100. break;
  3101. gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
  3102. "following SELECT TYPE at %C");
  3103. reject_statement ();
  3104. }
  3105. /* At this point, we're got a nonempty select block. */
  3106. cp = new_level (cp);
  3107. *cp = new_st;
  3108. accept_statement (st);
  3109. do
  3110. {
  3111. st = parse_executable (ST_NONE);
  3112. switch (st)
  3113. {
  3114. case ST_NONE:
  3115. unexpected_eof ();
  3116. case ST_TYPE_IS:
  3117. case ST_CLASS_IS:
  3118. cp = new_level (gfc_state_stack->head);
  3119. *cp = new_st;
  3120. gfc_clear_new_st ();
  3121. accept_statement (st);
  3122. /* Fall through */
  3123. case ST_END_SELECT:
  3124. break;
  3125. /* Can't have an executable statement because of
  3126. parse_executable(). */
  3127. default:
  3128. unexpected_statement (st);
  3129. break;
  3130. }
  3131. }
  3132. while (st != ST_END_SELECT);
  3133. done:
  3134. pop_state ();
  3135. accept_statement (st);
  3136. gfc_current_ns = gfc_current_ns->parent;
  3137. select_type_pop ();
  3138. }
  3139. /* Given a symbol, make sure it is not an iteration variable for a DO
  3140. statement. This subroutine is called when the symbol is seen in a
  3141. context that causes it to become redefined. If the symbol is an
  3142. iterator, we generate an error message and return nonzero. */
  3143. int
  3144. gfc_check_do_variable (gfc_symtree *st)
  3145. {
  3146. gfc_state_data *s;
  3147. for (s=gfc_state_stack; s; s = s->previous)
  3148. if (s->do_variable == st)
  3149. {
  3150. gfc_error_now_1 ("Variable '%s' at %C cannot be redefined inside "
  3151. "loop beginning at %L", st->name, &s->head->loc);
  3152. return 1;
  3153. }
  3154. return 0;
  3155. }
  3156. /* Checks to see if the current statement label closes an enddo.
  3157. Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
  3158. an error) if it incorrectly closes an ENDDO. */
  3159. static int
  3160. check_do_closure (void)
  3161. {
  3162. gfc_state_data *p;
  3163. if (gfc_statement_label == NULL)
  3164. return 0;
  3165. for (p = gfc_state_stack; p; p = p->previous)
  3166. if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
  3167. break;
  3168. if (p == NULL)
  3169. return 0; /* No loops to close */
  3170. if (p->ext.end_do_label == gfc_statement_label)
  3171. {
  3172. if (p == gfc_state_stack)
  3173. return 1;
  3174. gfc_error ("End of nonblock DO statement at %C is within another block");
  3175. return 2;
  3176. }
  3177. /* At this point, the label doesn't terminate the innermost loop.
  3178. Make sure it doesn't terminate another one. */
  3179. for (; p; p = p->previous)
  3180. if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
  3181. && p->ext.end_do_label == gfc_statement_label)
  3182. {
  3183. gfc_error ("End of nonblock DO statement at %C is interwoven "
  3184. "with another DO loop");
  3185. return 2;
  3186. }
  3187. return 0;
  3188. }
  3189. /* Parse a series of contained program units. */
  3190. static void parse_progunit (gfc_statement);
  3191. /* Parse a CRITICAL block. */
  3192. static void
  3193. parse_critical_block (void)
  3194. {
  3195. gfc_code *top, *d;
  3196. gfc_state_data s, *sd;
  3197. gfc_statement st;
  3198. for (sd = gfc_state_stack; sd; sd = sd->previous)
  3199. if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
  3200. gfc_error_now (is_oacc (sd)
  3201. ? "CRITICAL block inside of OpenACC region at %C"
  3202. : "CRITICAL block inside of OpenMP region at %C");
  3203. s.ext.end_do_label = new_st.label1;
  3204. accept_statement (ST_CRITICAL);
  3205. top = gfc_state_stack->tail;
  3206. push_state (&s, COMP_CRITICAL, gfc_new_block);
  3207. d = add_statement ();
  3208. d->op = EXEC_CRITICAL;
  3209. top->block = d;
  3210. do
  3211. {
  3212. st = parse_executable (ST_NONE);
  3213. switch (st)
  3214. {
  3215. case ST_NONE:
  3216. unexpected_eof ();
  3217. break;
  3218. case ST_END_CRITICAL:
  3219. if (s.ext.end_do_label != NULL
  3220. && s.ext.end_do_label != gfc_statement_label)
  3221. gfc_error_now ("Statement label in END CRITICAL at %C does not "
  3222. "match CRITICAL label");
  3223. if (gfc_statement_label != NULL)
  3224. {
  3225. new_st.op = EXEC_NOP;
  3226. add_statement ();
  3227. }
  3228. break;
  3229. default:
  3230. unexpected_statement (st);
  3231. break;
  3232. }
  3233. }
  3234. while (st != ST_END_CRITICAL);
  3235. pop_state ();
  3236. accept_statement (st);
  3237. }
  3238. /* Set up the local namespace for a BLOCK construct. */
  3239. gfc_namespace*
  3240. gfc_build_block_ns (gfc_namespace *parent_ns)
  3241. {
  3242. gfc_namespace* my_ns;
  3243. static int numblock = 1;
  3244. my_ns = gfc_get_namespace (parent_ns, 1);
  3245. my_ns->construct_entities = 1;
  3246. /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
  3247. code generation (so it must not be NULL).
  3248. We set its recursive argument if our container procedure is recursive, so
  3249. that local variables are accordingly placed on the stack when it
  3250. will be necessary. */
  3251. if (gfc_new_block)
  3252. my_ns->proc_name = gfc_new_block;
  3253. else
  3254. {
  3255. bool t;
  3256. char buffer[20]; /* Enough to hold "block@2147483648\n". */
  3257. snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
  3258. gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
  3259. t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
  3260. my_ns->proc_name->name, NULL);
  3261. gcc_assert (t);
  3262. gfc_commit_symbol (my_ns->proc_name);
  3263. }
  3264. if (parent_ns->proc_name)
  3265. my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
  3266. return my_ns;
  3267. }
  3268. /* Parse a BLOCK construct. */
  3269. static void
  3270. parse_block_construct (void)
  3271. {
  3272. gfc_namespace* my_ns;
  3273. gfc_state_data s;
  3274. gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
  3275. my_ns = gfc_build_block_ns (gfc_current_ns);
  3276. new_st.op = EXEC_BLOCK;
  3277. new_st.ext.block.ns = my_ns;
  3278. new_st.ext.block.assoc = NULL;
  3279. accept_statement (ST_BLOCK);
  3280. push_state (&s, COMP_BLOCK, my_ns->proc_name);
  3281. gfc_current_ns = my_ns;
  3282. parse_progunit (ST_NONE);
  3283. gfc_current_ns = gfc_current_ns->parent;
  3284. pop_state ();
  3285. }
  3286. /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
  3287. behind the scenes with compiler-generated variables. */
  3288. static void
  3289. parse_associate (void)
  3290. {
  3291. gfc_namespace* my_ns;
  3292. gfc_state_data s;
  3293. gfc_statement st;
  3294. gfc_association_list* a;
  3295. gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
  3296. my_ns = gfc_build_block_ns (gfc_current_ns);
  3297. new_st.op = EXEC_BLOCK;
  3298. new_st.ext.block.ns = my_ns;
  3299. gcc_assert (new_st.ext.block.assoc);
  3300. /* Add all associate-names as BLOCK variables. Creating them is enough
  3301. for now, they'll get their values during trans-* phase. */
  3302. gfc_current_ns = my_ns;
  3303. for (a = new_st.ext.block.assoc; a; a = a->next)
  3304. {
  3305. gfc_symbol* sym;
  3306. if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
  3307. gcc_unreachable ();
  3308. sym = a->st->n.sym;
  3309. sym->attr.flavor = FL_VARIABLE;
  3310. sym->assoc = a;
  3311. sym->declared_at = a->where;
  3312. gfc_set_sym_referenced (sym);
  3313. /* Initialize the typespec. It is not available in all cases,
  3314. however, as it may only be set on the target during resolution.
  3315. Still, sometimes it helps to have it right now -- especially
  3316. for parsing component references on the associate-name
  3317. in case of association to a derived-type. */
  3318. sym->ts = a->target->ts;
  3319. }
  3320. accept_statement (ST_ASSOCIATE);
  3321. push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
  3322. loop:
  3323. st = parse_executable (ST_NONE);
  3324. switch (st)
  3325. {
  3326. case ST_NONE:
  3327. unexpected_eof ();
  3328. case_end:
  3329. accept_statement (st);
  3330. my_ns->code = gfc_state_stack->head;
  3331. break;
  3332. default:
  3333. unexpected_statement (st);
  3334. goto loop;
  3335. }
  3336. gfc_current_ns = gfc_current_ns->parent;
  3337. pop_state ();
  3338. }
  3339. /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
  3340. handled inside of parse_executable(), because they aren't really
  3341. loop statements. */
  3342. static void
  3343. parse_do_block (void)
  3344. {
  3345. gfc_statement st;
  3346. gfc_code *top;
  3347. gfc_state_data s;
  3348. gfc_symtree *stree;
  3349. gfc_exec_op do_op;
  3350. do_op = new_st.op;
  3351. s.ext.end_do_label = new_st.label1;
  3352. if (new_st.ext.iterator != NULL)
  3353. stree = new_st.ext.iterator->var->symtree;
  3354. else
  3355. stree = NULL;
  3356. accept_statement (ST_DO);
  3357. top = gfc_state_stack->tail;
  3358. push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
  3359. gfc_new_block);
  3360. s.do_variable = stree;
  3361. top->block = new_level (top);
  3362. top->block->op = EXEC_DO;
  3363. loop:
  3364. st = parse_executable (ST_NONE);
  3365. switch (st)
  3366. {
  3367. case ST_NONE:
  3368. unexpected_eof ();
  3369. case ST_ENDDO:
  3370. if (s.ext.end_do_label != NULL
  3371. && s.ext.end_do_label != gfc_statement_label)
  3372. gfc_error_now ("Statement label in ENDDO at %C doesn't match "
  3373. "DO label");
  3374. if (gfc_statement_label != NULL)
  3375. {
  3376. new_st.op = EXEC_NOP;
  3377. add_statement ();
  3378. }
  3379. break;
  3380. case ST_IMPLIED_ENDDO:
  3381. /* If the do-stmt of this DO construct has a do-construct-name,
  3382. the corresponding end-do must be an end-do-stmt (with a matching
  3383. name, but in that case we must have seen ST_ENDDO first).
  3384. We only complain about this in pedantic mode. */
  3385. if (gfc_current_block () != NULL)
  3386. gfc_error_now ("Named block DO at %L requires matching ENDDO name",
  3387. &gfc_current_block()->declared_at);
  3388. break;
  3389. default:
  3390. unexpected_statement (st);
  3391. goto loop;
  3392. }
  3393. pop_state ();
  3394. accept_statement (st);
  3395. }
  3396. /* Parse the statements of OpenMP do/parallel do. */
  3397. static gfc_statement
  3398. parse_omp_do (gfc_statement omp_st)
  3399. {
  3400. gfc_statement st;
  3401. gfc_code *cp, *np;
  3402. gfc_state_data s;
  3403. accept_statement (omp_st);
  3404. cp = gfc_state_stack->tail;
  3405. push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
  3406. np = new_level (cp);
  3407. np->op = cp->op;
  3408. np->block = NULL;
  3409. for (;;)
  3410. {
  3411. st = next_statement ();
  3412. if (st == ST_NONE)
  3413. unexpected_eof ();
  3414. else if (st == ST_DO)
  3415. break;
  3416. else
  3417. unexpected_statement (st);
  3418. }
  3419. parse_do_block ();
  3420. if (gfc_statement_label != NULL
  3421. && gfc_state_stack->previous != NULL
  3422. && gfc_state_stack->previous->state == COMP_DO
  3423. && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
  3424. {
  3425. /* In
  3426. DO 100 I=1,10
  3427. !$OMP DO
  3428. DO J=1,10
  3429. ...
  3430. 100 CONTINUE
  3431. there should be no !$OMP END DO. */
  3432. pop_state ();
  3433. return ST_IMPLIED_ENDDO;
  3434. }
  3435. check_do_closure ();
  3436. pop_state ();
  3437. st = next_statement ();
  3438. gfc_statement omp_end_st = ST_OMP_END_DO;
  3439. switch (omp_st)
  3440. {
  3441. case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
  3442. case ST_OMP_DISTRIBUTE_PARALLEL_DO:
  3443. omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
  3444. break;
  3445. case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
  3446. omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
  3447. break;
  3448. case ST_OMP_DISTRIBUTE_SIMD:
  3449. omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
  3450. break;
  3451. case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
  3452. case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
  3453. case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
  3454. case ST_OMP_PARALLEL_DO_SIMD:
  3455. omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
  3456. break;
  3457. case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
  3458. case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
  3459. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
  3460. break;
  3461. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
  3462. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
  3463. break;
  3464. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  3465. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
  3466. break;
  3467. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
  3468. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
  3469. break;
  3470. case ST_OMP_TEAMS_DISTRIBUTE:
  3471. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
  3472. break;
  3473. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
  3474. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
  3475. break;
  3476. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  3477. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
  3478. break;
  3479. case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
  3480. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
  3481. break;
  3482. default: gcc_unreachable ();
  3483. }
  3484. if (st == omp_end_st)
  3485. {
  3486. if (new_st.op == EXEC_OMP_END_NOWAIT)
  3487. cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
  3488. else
  3489. gcc_assert (new_st.op == EXEC_NOP);
  3490. gfc_clear_new_st ();
  3491. gfc_commit_symbols ();
  3492. gfc_warning_check ();
  3493. st = next_statement ();
  3494. }
  3495. return st;
  3496. }
  3497. /* Parse the statements of OpenMP atomic directive. */
  3498. static gfc_statement
  3499. parse_omp_atomic (void)
  3500. {
  3501. gfc_statement st;
  3502. gfc_code *cp, *np;
  3503. gfc_state_data s;
  3504. int count;
  3505. accept_statement (ST_OMP_ATOMIC);
  3506. cp = gfc_state_stack->tail;
  3507. push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
  3508. np = new_level (cp);
  3509. np->op = cp->op;
  3510. np->block = NULL;
  3511. count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
  3512. == GFC_OMP_ATOMIC_CAPTURE);
  3513. while (count)
  3514. {
  3515. st = next_statement ();
  3516. if (st == ST_NONE)
  3517. unexpected_eof ();
  3518. else if (st == ST_ASSIGNMENT)
  3519. {
  3520. accept_statement (st);
  3521. count--;
  3522. }
  3523. else
  3524. unexpected_statement (st);
  3525. }
  3526. pop_state ();
  3527. st = next_statement ();
  3528. if (st == ST_OMP_END_ATOMIC)
  3529. {
  3530. gfc_clear_new_st ();
  3531. gfc_commit_symbols ();
  3532. gfc_warning_check ();
  3533. st = next_statement ();
  3534. }
  3535. else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
  3536. == GFC_OMP_ATOMIC_CAPTURE)
  3537. gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
  3538. return st;
  3539. }
  3540. /* Parse the statements of an OpenACC structured block. */
  3541. static void
  3542. parse_oacc_structured_block (gfc_statement acc_st)
  3543. {
  3544. gfc_statement st, acc_end_st;
  3545. gfc_code *cp, *np;
  3546. gfc_state_data s, *sd;
  3547. for (sd = gfc_state_stack; sd; sd = sd->previous)
  3548. if (sd->state == COMP_CRITICAL)
  3549. gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
  3550. accept_statement (acc_st);
  3551. cp = gfc_state_stack->tail;
  3552. push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
  3553. np = new_level (cp);
  3554. np->op = cp->op;
  3555. np->block = NULL;
  3556. switch (acc_st)
  3557. {
  3558. case ST_OACC_PARALLEL:
  3559. acc_end_st = ST_OACC_END_PARALLEL;
  3560. break;
  3561. case ST_OACC_KERNELS:
  3562. acc_end_st = ST_OACC_END_KERNELS;
  3563. break;
  3564. case ST_OACC_DATA:
  3565. acc_end_st = ST_OACC_END_DATA;
  3566. break;
  3567. case ST_OACC_HOST_DATA:
  3568. acc_end_st = ST_OACC_END_HOST_DATA;
  3569. break;
  3570. default:
  3571. gcc_unreachable ();
  3572. }
  3573. do
  3574. {
  3575. st = parse_executable (ST_NONE);
  3576. if (st == ST_NONE)
  3577. unexpected_eof ();
  3578. else if (st != acc_end_st)
  3579. gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
  3580. reject_statement ();
  3581. }
  3582. while (st != acc_end_st);
  3583. gcc_assert (new_st.op == EXEC_NOP);
  3584. gfc_clear_new_st ();
  3585. gfc_commit_symbols ();
  3586. gfc_warning_check ();
  3587. pop_state ();
  3588. }
  3589. /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
  3590. static gfc_statement
  3591. parse_oacc_loop (gfc_statement acc_st)
  3592. {
  3593. gfc_statement st;
  3594. gfc_code *cp, *np;
  3595. gfc_state_data s, *sd;
  3596. for (sd = gfc_state_stack; sd; sd = sd->previous)
  3597. if (sd->state == COMP_CRITICAL)
  3598. gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
  3599. accept_statement (acc_st);
  3600. cp = gfc_state_stack->tail;
  3601. push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
  3602. np = new_level (cp);
  3603. np->op = cp->op;
  3604. np->block = NULL;
  3605. for (;;)
  3606. {
  3607. st = next_statement ();
  3608. if (st == ST_NONE)
  3609. unexpected_eof ();
  3610. else if (st == ST_DO)
  3611. break;
  3612. else
  3613. {
  3614. gfc_error ("Expected DO loop at %C");
  3615. reject_statement ();
  3616. }
  3617. }
  3618. parse_do_block ();
  3619. if (gfc_statement_label != NULL
  3620. && gfc_state_stack->previous != NULL
  3621. && gfc_state_stack->previous->state == COMP_DO
  3622. && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
  3623. {
  3624. pop_state ();
  3625. return ST_IMPLIED_ENDDO;
  3626. }
  3627. check_do_closure ();
  3628. pop_state ();
  3629. st = next_statement ();
  3630. if (st == ST_OACC_END_LOOP)
  3631. gfc_warning (0, "Redundant !$ACC END LOOP at %C");
  3632. if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
  3633. (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
  3634. (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
  3635. {
  3636. gcc_assert (new_st.op == EXEC_NOP);
  3637. gfc_clear_new_st ();
  3638. gfc_commit_symbols ();
  3639. gfc_warning_check ();
  3640. st = next_statement ();
  3641. }
  3642. return st;
  3643. }
  3644. /* Parse the statements of an OpenMP structured block. */
  3645. static void
  3646. parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
  3647. {
  3648. gfc_statement st, omp_end_st;
  3649. gfc_code *cp, *np;
  3650. gfc_state_data s;
  3651. accept_statement (omp_st);
  3652. cp = gfc_state_stack->tail;
  3653. push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
  3654. np = new_level (cp);
  3655. np->op = cp->op;
  3656. np->block = NULL;
  3657. switch (omp_st)
  3658. {
  3659. case ST_OMP_PARALLEL:
  3660. omp_end_st = ST_OMP_END_PARALLEL;
  3661. break;
  3662. case ST_OMP_PARALLEL_SECTIONS:
  3663. omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
  3664. break;
  3665. case ST_OMP_SECTIONS:
  3666. omp_end_st = ST_OMP_END_SECTIONS;
  3667. break;
  3668. case ST_OMP_ORDERED:
  3669. omp_end_st = ST_OMP_END_ORDERED;
  3670. break;
  3671. case ST_OMP_CRITICAL:
  3672. omp_end_st = ST_OMP_END_CRITICAL;
  3673. break;
  3674. case ST_OMP_MASTER:
  3675. omp_end_st = ST_OMP_END_MASTER;
  3676. break;
  3677. case ST_OMP_SINGLE:
  3678. omp_end_st = ST_OMP_END_SINGLE;
  3679. break;
  3680. case ST_OMP_TARGET:
  3681. omp_end_st = ST_OMP_END_TARGET;
  3682. break;
  3683. case ST_OMP_TARGET_DATA:
  3684. omp_end_st = ST_OMP_END_TARGET_DATA;
  3685. break;
  3686. case ST_OMP_TARGET_TEAMS:
  3687. omp_end_st = ST_OMP_END_TARGET_TEAMS;
  3688. break;
  3689. case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
  3690. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
  3691. break;
  3692. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
  3693. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
  3694. break;
  3695. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  3696. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
  3697. break;
  3698. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
  3699. omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
  3700. break;
  3701. case ST_OMP_TASK:
  3702. omp_end_st = ST_OMP_END_TASK;
  3703. break;
  3704. case ST_OMP_TASKGROUP:
  3705. omp_end_st = ST_OMP_END_TASKGROUP;
  3706. break;
  3707. case ST_OMP_TEAMS:
  3708. omp_end_st = ST_OMP_END_TEAMS;
  3709. break;
  3710. case ST_OMP_TEAMS_DISTRIBUTE:
  3711. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
  3712. break;
  3713. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
  3714. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
  3715. break;
  3716. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  3717. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
  3718. break;
  3719. case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
  3720. omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
  3721. break;
  3722. case ST_OMP_DISTRIBUTE:
  3723. omp_end_st = ST_OMP_END_DISTRIBUTE;
  3724. break;
  3725. case ST_OMP_DISTRIBUTE_PARALLEL_DO:
  3726. omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
  3727. break;
  3728. case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
  3729. omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
  3730. break;
  3731. case ST_OMP_DISTRIBUTE_SIMD:
  3732. omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
  3733. break;
  3734. case ST_OMP_WORKSHARE:
  3735. omp_end_st = ST_OMP_END_WORKSHARE;
  3736. break;
  3737. case ST_OMP_PARALLEL_WORKSHARE:
  3738. omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
  3739. break;
  3740. default:
  3741. gcc_unreachable ();
  3742. }
  3743. do
  3744. {
  3745. if (workshare_stmts_only)
  3746. {
  3747. /* Inside of !$omp workshare, only
  3748. scalar assignments
  3749. array assignments
  3750. where statements and constructs
  3751. forall statements and constructs
  3752. !$omp atomic
  3753. !$omp critical
  3754. !$omp parallel
  3755. are allowed. For !$omp critical these
  3756. restrictions apply recursively. */
  3757. bool cycle = true;
  3758. st = next_statement ();
  3759. for (;;)
  3760. {
  3761. switch (st)
  3762. {
  3763. case ST_NONE:
  3764. unexpected_eof ();
  3765. case ST_ASSIGNMENT:
  3766. case ST_WHERE:
  3767. case ST_FORALL:
  3768. accept_statement (st);
  3769. break;
  3770. case ST_WHERE_BLOCK:
  3771. parse_where_block ();
  3772. break;
  3773. case ST_FORALL_BLOCK:
  3774. parse_forall_block ();
  3775. break;
  3776. case ST_OMP_PARALLEL:
  3777. case ST_OMP_PARALLEL_SECTIONS:
  3778. parse_omp_structured_block (st, false);
  3779. break;
  3780. case ST_OMP_PARALLEL_WORKSHARE:
  3781. case ST_OMP_CRITICAL:
  3782. parse_omp_structured_block (st, true);
  3783. break;
  3784. case ST_OMP_PARALLEL_DO:
  3785. case ST_OMP_PARALLEL_DO_SIMD:
  3786. st = parse_omp_do (st);
  3787. continue;
  3788. case ST_OMP_ATOMIC:
  3789. st = parse_omp_atomic ();
  3790. continue;
  3791. default:
  3792. cycle = false;
  3793. break;
  3794. }
  3795. if (!cycle)
  3796. break;
  3797. st = next_statement ();
  3798. }
  3799. }
  3800. else
  3801. st = parse_executable (ST_NONE);
  3802. if (st == ST_NONE)
  3803. unexpected_eof ();
  3804. else if (st == ST_OMP_SECTION
  3805. && (omp_st == ST_OMP_SECTIONS
  3806. || omp_st == ST_OMP_PARALLEL_SECTIONS))
  3807. {
  3808. np = new_level (np);
  3809. np->op = cp->op;
  3810. np->block = NULL;
  3811. }
  3812. else if (st != omp_end_st)
  3813. unexpected_statement (st);
  3814. }
  3815. while (st != omp_end_st);
  3816. switch (new_st.op)
  3817. {
  3818. case EXEC_OMP_END_NOWAIT:
  3819. cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
  3820. break;
  3821. case EXEC_OMP_CRITICAL:
  3822. if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
  3823. || (new_st.ext.omp_name != NULL
  3824. && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
  3825. gfc_error ("Name after !$omp critical and !$omp end critical does "
  3826. "not match at %C");
  3827. free (CONST_CAST (char *, new_st.ext.omp_name));
  3828. break;
  3829. case EXEC_OMP_END_SINGLE:
  3830. cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
  3831. = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
  3832. new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
  3833. gfc_free_omp_clauses (new_st.ext.omp_clauses);
  3834. break;
  3835. case EXEC_NOP:
  3836. break;
  3837. default:
  3838. gcc_unreachable ();
  3839. }
  3840. gfc_clear_new_st ();
  3841. gfc_commit_symbols ();
  3842. gfc_warning_check ();
  3843. pop_state ();
  3844. }
  3845. /* Accept a series of executable statements. We return the first
  3846. statement that doesn't fit to the caller. Any block statements are
  3847. passed on to the correct handler, which usually passes the buck
  3848. right back here. */
  3849. static gfc_statement
  3850. parse_executable (gfc_statement st)
  3851. {
  3852. int close_flag;
  3853. if (st == ST_NONE)
  3854. st = next_statement ();
  3855. for (;;)
  3856. {
  3857. close_flag = check_do_closure ();
  3858. if (close_flag)
  3859. switch (st)
  3860. {
  3861. case ST_GOTO:
  3862. case ST_END_PROGRAM:
  3863. case ST_RETURN:
  3864. case ST_EXIT:
  3865. case ST_END_FUNCTION:
  3866. case ST_CYCLE:
  3867. case ST_PAUSE:
  3868. case ST_STOP:
  3869. case ST_ERROR_STOP:
  3870. case ST_END_SUBROUTINE:
  3871. case ST_DO:
  3872. case ST_FORALL:
  3873. case ST_WHERE:
  3874. case ST_SELECT_CASE:
  3875. gfc_error ("%s statement at %C cannot terminate a non-block "
  3876. "DO loop", gfc_ascii_statement (st));
  3877. break;
  3878. default:
  3879. break;
  3880. }
  3881. switch (st)
  3882. {
  3883. case ST_NONE:
  3884. unexpected_eof ();
  3885. case ST_DATA:
  3886. gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
  3887. "first executable statement");
  3888. /* Fall through. */
  3889. case ST_FORMAT:
  3890. case ST_ENTRY:
  3891. case_executable:
  3892. accept_statement (st);
  3893. if (close_flag == 1)
  3894. return ST_IMPLIED_ENDDO;
  3895. break;
  3896. case ST_BLOCK:
  3897. parse_block_construct ();
  3898. break;
  3899. case ST_ASSOCIATE:
  3900. parse_associate ();
  3901. break;
  3902. case ST_IF_BLOCK:
  3903. parse_if_block ();
  3904. break;
  3905. case ST_SELECT_CASE:
  3906. parse_select_block ();
  3907. break;
  3908. case ST_SELECT_TYPE:
  3909. parse_select_type_block();
  3910. break;
  3911. case ST_DO:
  3912. parse_do_block ();
  3913. if (check_do_closure () == 1)
  3914. return ST_IMPLIED_ENDDO;
  3915. break;
  3916. case ST_CRITICAL:
  3917. parse_critical_block ();
  3918. break;
  3919. case ST_WHERE_BLOCK:
  3920. parse_where_block ();
  3921. break;
  3922. case ST_FORALL_BLOCK:
  3923. parse_forall_block ();
  3924. break;
  3925. case ST_OACC_PARALLEL_LOOP:
  3926. case ST_OACC_KERNELS_LOOP:
  3927. case ST_OACC_LOOP:
  3928. st = parse_oacc_loop (st);
  3929. if (st == ST_IMPLIED_ENDDO)
  3930. return st;
  3931. continue;
  3932. case ST_OACC_PARALLEL:
  3933. case ST_OACC_KERNELS:
  3934. case ST_OACC_DATA:
  3935. case ST_OACC_HOST_DATA:
  3936. parse_oacc_structured_block (st);
  3937. break;
  3938. case ST_OMP_PARALLEL:
  3939. case ST_OMP_PARALLEL_SECTIONS:
  3940. case ST_OMP_SECTIONS:
  3941. case ST_OMP_ORDERED:
  3942. case ST_OMP_CRITICAL:
  3943. case ST_OMP_MASTER:
  3944. case ST_OMP_SINGLE:
  3945. case ST_OMP_TARGET:
  3946. case ST_OMP_TARGET_DATA:
  3947. case ST_OMP_TARGET_TEAMS:
  3948. case ST_OMP_TEAMS:
  3949. case ST_OMP_TASK:
  3950. case ST_OMP_TASKGROUP:
  3951. parse_omp_structured_block (st, false);
  3952. break;
  3953. case ST_OMP_WORKSHARE:
  3954. case ST_OMP_PARALLEL_WORKSHARE:
  3955. parse_omp_structured_block (st, true);
  3956. break;
  3957. case ST_OMP_DISTRIBUTE:
  3958. case ST_OMP_DISTRIBUTE_PARALLEL_DO:
  3959. case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
  3960. case ST_OMP_DISTRIBUTE_SIMD:
  3961. case ST_OMP_DO:
  3962. case ST_OMP_DO_SIMD:
  3963. case ST_OMP_PARALLEL_DO:
  3964. case ST_OMP_PARALLEL_DO_SIMD:
  3965. case ST_OMP_SIMD:
  3966. case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
  3967. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
  3968. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  3969. case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
  3970. case ST_OMP_TEAMS_DISTRIBUTE:
  3971. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
  3972. case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
  3973. case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
  3974. st = parse_omp_do (st);
  3975. if (st == ST_IMPLIED_ENDDO)
  3976. return st;
  3977. continue;
  3978. case ST_OMP_ATOMIC:
  3979. st = parse_omp_atomic ();
  3980. continue;
  3981. default:
  3982. return st;
  3983. }
  3984. st = next_statement ();
  3985. }
  3986. }
  3987. /* Fix the symbols for sibling functions. These are incorrectly added to
  3988. the child namespace as the parser didn't know about this procedure. */
  3989. static void
  3990. gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
  3991. {
  3992. gfc_namespace *ns;
  3993. gfc_symtree *st;
  3994. gfc_symbol *old_sym;
  3995. for (ns = siblings; ns; ns = ns->sibling)
  3996. {
  3997. st = gfc_find_symtree (ns->sym_root, sym->name);
  3998. if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
  3999. goto fixup_contained;
  4000. if ((st->n.sym->attr.flavor == FL_DERIVED
  4001. && sym->attr.generic && sym->attr.function)
  4002. ||(sym->attr.flavor == FL_DERIVED
  4003. && st->n.sym->attr.generic && st->n.sym->attr.function))
  4004. goto fixup_contained;
  4005. old_sym = st->n.sym;
  4006. if (old_sym->ns == ns
  4007. && !old_sym->attr.contained
  4008. /* By 14.6.1.3, host association should be excluded
  4009. for the following. */
  4010. && !(old_sym->attr.external
  4011. || (old_sym->ts.type != BT_UNKNOWN
  4012. && !old_sym->attr.implicit_type)
  4013. || old_sym->attr.flavor == FL_PARAMETER
  4014. || old_sym->attr.use_assoc
  4015. || old_sym->attr.in_common
  4016. || old_sym->attr.in_equivalence
  4017. || old_sym->attr.data
  4018. || old_sym->attr.dummy
  4019. || old_sym->attr.result
  4020. || old_sym->attr.dimension
  4021. || old_sym->attr.allocatable
  4022. || old_sym->attr.intrinsic
  4023. || old_sym->attr.generic
  4024. || old_sym->attr.flavor == FL_NAMELIST
  4025. || old_sym->attr.flavor == FL_LABEL
  4026. || old_sym->attr.proc == PROC_ST_FUNCTION))
  4027. {
  4028. /* Replace it with the symbol from the parent namespace. */
  4029. st->n.sym = sym;
  4030. sym->refs++;
  4031. gfc_release_symbol (old_sym);
  4032. }
  4033. fixup_contained:
  4034. /* Do the same for any contained procedures. */
  4035. gfc_fixup_sibling_symbols (sym, ns->contained);
  4036. }
  4037. }
  4038. static void
  4039. parse_contained (int module)
  4040. {
  4041. gfc_namespace *ns, *parent_ns, *tmp;
  4042. gfc_state_data s1, s2;
  4043. gfc_statement st;
  4044. gfc_symbol *sym;
  4045. gfc_entry_list *el;
  4046. int contains_statements = 0;
  4047. int seen_error = 0;
  4048. push_state (&s1, COMP_CONTAINS, NULL);
  4049. parent_ns = gfc_current_ns;
  4050. do
  4051. {
  4052. gfc_current_ns = gfc_get_namespace (parent_ns, 1);
  4053. gfc_current_ns->sibling = parent_ns->contained;
  4054. parent_ns->contained = gfc_current_ns;
  4055. next:
  4056. /* Process the next available statement. We come here if we got an error
  4057. and rejected the last statement. */
  4058. st = next_statement ();
  4059. switch (st)
  4060. {
  4061. case ST_NONE:
  4062. unexpected_eof ();
  4063. case ST_FUNCTION:
  4064. case ST_SUBROUTINE:
  4065. contains_statements = 1;
  4066. accept_statement (st);
  4067. push_state (&s2,
  4068. (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
  4069. gfc_new_block);
  4070. /* For internal procedures, create/update the symbol in the
  4071. parent namespace. */
  4072. if (!module)
  4073. {
  4074. if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
  4075. gfc_error ("Contained procedure %qs at %C is already "
  4076. "ambiguous", gfc_new_block->name);
  4077. else
  4078. {
  4079. if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
  4080. sym->name,
  4081. &gfc_new_block->declared_at))
  4082. {
  4083. if (st == ST_FUNCTION)
  4084. gfc_add_function (&sym->attr, sym->name,
  4085. &gfc_new_block->declared_at);
  4086. else
  4087. gfc_add_subroutine (&sym->attr, sym->name,
  4088. &gfc_new_block->declared_at);
  4089. }
  4090. }
  4091. gfc_commit_symbols ();
  4092. }
  4093. else
  4094. sym = gfc_new_block;
  4095. /* Mark this as a contained function, so it isn't replaced
  4096. by other module functions. */
  4097. sym->attr.contained = 1;
  4098. /* Set implicit_pure so that it can be reset if any of the
  4099. tests for purity fail. This is used for some optimisation
  4100. during translation. */
  4101. if (!sym->attr.pure)
  4102. sym->attr.implicit_pure = 1;
  4103. parse_progunit (ST_NONE);
  4104. /* Fix up any sibling functions that refer to this one. */
  4105. gfc_fixup_sibling_symbols (sym, gfc_current_ns);
  4106. /* Or refer to any of its alternate entry points. */
  4107. for (el = gfc_current_ns->entries; el; el = el->next)
  4108. gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
  4109. gfc_current_ns->code = s2.head;
  4110. gfc_current_ns = parent_ns;
  4111. pop_state ();
  4112. break;
  4113. /* These statements are associated with the end of the host unit. */
  4114. case ST_END_FUNCTION:
  4115. case ST_END_MODULE:
  4116. case ST_END_PROGRAM:
  4117. case ST_END_SUBROUTINE:
  4118. accept_statement (st);
  4119. gfc_current_ns->code = s1.head;
  4120. break;
  4121. default:
  4122. gfc_error ("Unexpected %s statement in CONTAINS section at %C",
  4123. gfc_ascii_statement (st));
  4124. reject_statement ();
  4125. seen_error = 1;
  4126. goto next;
  4127. break;
  4128. }
  4129. }
  4130. while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
  4131. && st != ST_END_MODULE && st != ST_END_PROGRAM);
  4132. /* The first namespace in the list is guaranteed to not have
  4133. anything (worthwhile) in it. */
  4134. tmp = gfc_current_ns;
  4135. gfc_current_ns = parent_ns;
  4136. if (seen_error && tmp->refs > 1)
  4137. gfc_free_namespace (tmp);
  4138. ns = gfc_current_ns->contained;
  4139. gfc_current_ns->contained = ns->sibling;
  4140. gfc_free_namespace (ns);
  4141. pop_state ();
  4142. if (!contains_statements)
  4143. gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
  4144. "FUNCTION or SUBROUTINE statement at %C");
  4145. }
  4146. /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
  4147. static void
  4148. parse_progunit (gfc_statement st)
  4149. {
  4150. gfc_state_data *p;
  4151. int n;
  4152. st = parse_spec (st);
  4153. switch (st)
  4154. {
  4155. case ST_NONE:
  4156. unexpected_eof ();
  4157. case ST_CONTAINS:
  4158. /* This is not allowed within BLOCK! */
  4159. if (gfc_current_state () != COMP_BLOCK)
  4160. goto contains;
  4161. break;
  4162. case_end:
  4163. accept_statement (st);
  4164. goto done;
  4165. default:
  4166. break;
  4167. }
  4168. if (gfc_current_state () == COMP_FUNCTION)
  4169. gfc_check_function_type (gfc_current_ns);
  4170. loop:
  4171. for (;;)
  4172. {
  4173. st = parse_executable (st);
  4174. switch (st)
  4175. {
  4176. case ST_NONE:
  4177. unexpected_eof ();
  4178. case ST_CONTAINS:
  4179. /* This is not allowed within BLOCK! */
  4180. if (gfc_current_state () != COMP_BLOCK)
  4181. goto contains;
  4182. break;
  4183. case_end:
  4184. accept_statement (st);
  4185. goto done;
  4186. default:
  4187. break;
  4188. }
  4189. unexpected_statement (st);
  4190. reject_statement ();
  4191. st = next_statement ();
  4192. }
  4193. contains:
  4194. n = 0;
  4195. for (p = gfc_state_stack; p; p = p->previous)
  4196. if (p->state == COMP_CONTAINS)
  4197. n++;
  4198. if (gfc_find_state (COMP_MODULE) == true)
  4199. n--;
  4200. if (n > 0)
  4201. {
  4202. gfc_error ("CONTAINS statement at %C is already in a contained "
  4203. "program unit");
  4204. reject_statement ();
  4205. st = next_statement ();
  4206. goto loop;
  4207. }
  4208. parse_contained (0);
  4209. done:
  4210. gfc_current_ns->code = gfc_state_stack->head;
  4211. if (gfc_state_stack->state == COMP_PROGRAM
  4212. || gfc_state_stack->state == COMP_MODULE
  4213. || gfc_state_stack->state == COMP_SUBROUTINE
  4214. || gfc_state_stack->state == COMP_FUNCTION
  4215. || gfc_state_stack->state == COMP_BLOCK)
  4216. gfc_current_ns->oacc_declare_clauses
  4217. = gfc_state_stack->ext.oacc_declare_clauses;
  4218. }
  4219. /* Come here to complain about a global symbol already in use as
  4220. something else. */
  4221. void
  4222. gfc_global_used (gfc_gsymbol *sym, locus *where)
  4223. {
  4224. const char *name;
  4225. if (where == NULL)
  4226. where = &gfc_current_locus;
  4227. switch(sym->type)
  4228. {
  4229. case GSYM_PROGRAM:
  4230. name = "PROGRAM";
  4231. break;
  4232. case GSYM_FUNCTION:
  4233. name = "FUNCTION";
  4234. break;
  4235. case GSYM_SUBROUTINE:
  4236. name = "SUBROUTINE";
  4237. break;
  4238. case GSYM_COMMON:
  4239. name = "COMMON";
  4240. break;
  4241. case GSYM_BLOCK_DATA:
  4242. name = "BLOCK DATA";
  4243. break;
  4244. case GSYM_MODULE:
  4245. name = "MODULE";
  4246. break;
  4247. default:
  4248. gfc_internal_error ("gfc_global_used(): Bad type");
  4249. name = NULL;
  4250. }
  4251. if (sym->binding_label)
  4252. gfc_error_1 ("Global binding name '%s' at %L is already being used as a %s "
  4253. "at %L", sym->binding_label, where, name, &sym->where);
  4254. else
  4255. gfc_error_1 ("Global name '%s' at %L is already being used as a %s at %L",
  4256. sym->name, where, name, &sym->where);
  4257. }
  4258. /* Parse a block data program unit. */
  4259. static void
  4260. parse_block_data (void)
  4261. {
  4262. gfc_statement st;
  4263. static locus blank_locus;
  4264. static int blank_block=0;
  4265. gfc_gsymbol *s;
  4266. gfc_current_ns->proc_name = gfc_new_block;
  4267. gfc_current_ns->is_block_data = 1;
  4268. if (gfc_new_block == NULL)
  4269. {
  4270. if (blank_block)
  4271. gfc_error ("Blank BLOCK DATA at %C conflicts with "
  4272. "prior BLOCK DATA at %L", &blank_locus);
  4273. else
  4274. {
  4275. blank_block = 1;
  4276. blank_locus = gfc_current_locus;
  4277. }
  4278. }
  4279. else
  4280. {
  4281. s = gfc_get_gsymbol (gfc_new_block->name);
  4282. if (s->defined
  4283. || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
  4284. gfc_global_used (s, &gfc_new_block->declared_at);
  4285. else
  4286. {
  4287. s->type = GSYM_BLOCK_DATA;
  4288. s->where = gfc_new_block->declared_at;
  4289. s->defined = 1;
  4290. }
  4291. }
  4292. st = parse_spec (ST_NONE);
  4293. while (st != ST_END_BLOCK_DATA)
  4294. {
  4295. gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
  4296. gfc_ascii_statement (st));
  4297. reject_statement ();
  4298. st = next_statement ();
  4299. }
  4300. }
  4301. /* Parse a module subprogram. */
  4302. static void
  4303. parse_module (void)
  4304. {
  4305. gfc_statement st;
  4306. gfc_gsymbol *s;
  4307. bool error;
  4308. s = gfc_get_gsymbol (gfc_new_block->name);
  4309. if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
  4310. gfc_global_used (s, &gfc_new_block->declared_at);
  4311. else
  4312. {
  4313. s->type = GSYM_MODULE;
  4314. s->where = gfc_new_block->declared_at;
  4315. s->defined = 1;
  4316. }
  4317. st = parse_spec (ST_NONE);
  4318. error = false;
  4319. loop:
  4320. switch (st)
  4321. {
  4322. case ST_NONE:
  4323. unexpected_eof ();
  4324. case ST_CONTAINS:
  4325. parse_contained (1);
  4326. break;
  4327. case ST_END_MODULE:
  4328. accept_statement (st);
  4329. break;
  4330. default:
  4331. gfc_error ("Unexpected %s statement in MODULE at %C",
  4332. gfc_ascii_statement (st));
  4333. error = true;
  4334. reject_statement ();
  4335. st = next_statement ();
  4336. goto loop;
  4337. }
  4338. /* Make sure not to free the namespace twice on error. */
  4339. if (!error)
  4340. s->ns = gfc_current_ns;
  4341. }
  4342. /* Add a procedure name to the global symbol table. */
  4343. static void
  4344. add_global_procedure (bool sub)
  4345. {
  4346. gfc_gsymbol *s;
  4347. /* Only in Fortran 2003: For procedures with a binding label also the Fortran
  4348. name is a global identifier. */
  4349. if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
  4350. {
  4351. s = gfc_get_gsymbol (gfc_new_block->name);
  4352. if (s->defined
  4353. || (s->type != GSYM_UNKNOWN
  4354. && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
  4355. {
  4356. gfc_global_used (s, &gfc_new_block->declared_at);
  4357. /* Silence follow-up errors. */
  4358. gfc_new_block->binding_label = NULL;
  4359. }
  4360. else
  4361. {
  4362. s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
  4363. s->sym_name = gfc_new_block->name;
  4364. s->where = gfc_new_block->declared_at;
  4365. s->defined = 1;
  4366. s->ns = gfc_current_ns;
  4367. }
  4368. }
  4369. /* Don't add the symbol multiple times. */
  4370. if (gfc_new_block->binding_label
  4371. && (!gfc_notification_std (GFC_STD_F2008)
  4372. || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
  4373. {
  4374. s = gfc_get_gsymbol (gfc_new_block->binding_label);
  4375. if (s->defined
  4376. || (s->type != GSYM_UNKNOWN
  4377. && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
  4378. {
  4379. gfc_global_used (s, &gfc_new_block->declared_at);
  4380. /* Silence follow-up errors. */
  4381. gfc_new_block->binding_label = NULL;
  4382. }
  4383. else
  4384. {
  4385. s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
  4386. s->sym_name = gfc_new_block->name;
  4387. s->binding_label = gfc_new_block->binding_label;
  4388. s->where = gfc_new_block->declared_at;
  4389. s->defined = 1;
  4390. s->ns = gfc_current_ns;
  4391. }
  4392. }
  4393. }
  4394. /* Add a program to the global symbol table. */
  4395. static void
  4396. add_global_program (void)
  4397. {
  4398. gfc_gsymbol *s;
  4399. if (gfc_new_block == NULL)
  4400. return;
  4401. s = gfc_get_gsymbol (gfc_new_block->name);
  4402. if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
  4403. gfc_global_used (s, &gfc_new_block->declared_at);
  4404. else
  4405. {
  4406. s->type = GSYM_PROGRAM;
  4407. s->where = gfc_new_block->declared_at;
  4408. s->defined = 1;
  4409. s->ns = gfc_current_ns;
  4410. }
  4411. }
  4412. /* Resolve all the program units. */
  4413. static void
  4414. resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
  4415. {
  4416. gfc_free_dt_list ();
  4417. gfc_current_ns = gfc_global_ns_list;
  4418. for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
  4419. {
  4420. if (gfc_current_ns->proc_name
  4421. && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
  4422. continue; /* Already resolved. */
  4423. if (gfc_current_ns->proc_name)
  4424. gfc_current_locus = gfc_current_ns->proc_name->declared_at;
  4425. gfc_resolve (gfc_current_ns);
  4426. gfc_current_ns->derived_types = gfc_derived_types;
  4427. gfc_derived_types = NULL;
  4428. }
  4429. }
  4430. static void
  4431. clean_up_modules (gfc_gsymbol *gsym)
  4432. {
  4433. if (gsym == NULL)
  4434. return;
  4435. clean_up_modules (gsym->left);
  4436. clean_up_modules (gsym->right);
  4437. if (gsym->type != GSYM_MODULE || !gsym->ns)
  4438. return;
  4439. gfc_current_ns = gsym->ns;
  4440. gfc_derived_types = gfc_current_ns->derived_types;
  4441. gfc_done_2 ();
  4442. gsym->ns = NULL;
  4443. return;
  4444. }
  4445. /* Translate all the program units. This could be in a different order
  4446. to resolution if there are forward references in the file. */
  4447. static void
  4448. translate_all_program_units (gfc_namespace *gfc_global_ns_list)
  4449. {
  4450. int errors;
  4451. gfc_current_ns = gfc_global_ns_list;
  4452. gfc_get_errors (NULL, &errors);
  4453. /* We first translate all modules to make sure that later parts
  4454. of the program can use the decl. Then we translate the nonmodules. */
  4455. for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
  4456. {
  4457. if (!gfc_current_ns->proc_name
  4458. || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
  4459. continue;
  4460. gfc_current_locus = gfc_current_ns->proc_name->declared_at;
  4461. gfc_derived_types = gfc_current_ns->derived_types;
  4462. gfc_generate_module_code (gfc_current_ns);
  4463. gfc_current_ns->translated = 1;
  4464. }
  4465. gfc_current_ns = gfc_global_ns_list;
  4466. for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
  4467. {
  4468. if (gfc_current_ns->proc_name
  4469. && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
  4470. continue;
  4471. gfc_current_locus = gfc_current_ns->proc_name->declared_at;
  4472. gfc_derived_types = gfc_current_ns->derived_types;
  4473. gfc_generate_code (gfc_current_ns);
  4474. gfc_current_ns->translated = 1;
  4475. }
  4476. /* Clean up all the namespaces after translation. */
  4477. gfc_current_ns = gfc_global_ns_list;
  4478. for (;gfc_current_ns;)
  4479. {
  4480. gfc_namespace *ns;
  4481. if (gfc_current_ns->proc_name
  4482. && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
  4483. {
  4484. gfc_current_ns = gfc_current_ns->sibling;
  4485. continue;
  4486. }
  4487. ns = gfc_current_ns->sibling;
  4488. gfc_derived_types = gfc_current_ns->derived_types;
  4489. gfc_done_2 ();
  4490. gfc_current_ns = ns;
  4491. }
  4492. clean_up_modules (gfc_gsym_root);
  4493. }
  4494. /* Top level parser. */
  4495. bool
  4496. gfc_parse_file (void)
  4497. {
  4498. int seen_program, errors_before, errors;
  4499. gfc_state_data top, s;
  4500. gfc_statement st;
  4501. locus prog_locus;
  4502. gfc_namespace *next;
  4503. gfc_start_source_files ();
  4504. top.state = COMP_NONE;
  4505. top.sym = NULL;
  4506. top.previous = NULL;
  4507. top.head = top.tail = NULL;
  4508. top.do_variable = NULL;
  4509. gfc_state_stack = &top;
  4510. gfc_clear_new_st ();
  4511. gfc_statement_label = NULL;
  4512. if (setjmp (eof_buf))
  4513. return false; /* Come here on unexpected EOF */
  4514. /* Prepare the global namespace that will contain the
  4515. program units. */
  4516. gfc_global_ns_list = next = NULL;
  4517. seen_program = 0;
  4518. errors_before = 0;
  4519. /* Exit early for empty files. */
  4520. if (gfc_at_eof ())
  4521. goto done;
  4522. loop:
  4523. gfc_init_2 ();
  4524. st = next_statement ();
  4525. switch (st)
  4526. {
  4527. case ST_NONE:
  4528. gfc_done_2 ();
  4529. goto done;
  4530. case ST_PROGRAM:
  4531. if (seen_program)
  4532. goto duplicate_main;
  4533. seen_program = 1;
  4534. prog_locus = gfc_current_locus;
  4535. push_state (&s, COMP_PROGRAM, gfc_new_block);
  4536. main_program_symbol(gfc_current_ns, gfc_new_block->name);
  4537. accept_statement (st);
  4538. add_global_program ();
  4539. parse_progunit (ST_NONE);
  4540. goto prog_units;
  4541. break;
  4542. case ST_SUBROUTINE:
  4543. add_global_procedure (true);
  4544. push_state (&s, COMP_SUBROUTINE, gfc_new_block);
  4545. accept_statement (st);
  4546. parse_progunit (ST_NONE);
  4547. goto prog_units;
  4548. break;
  4549. case ST_FUNCTION:
  4550. add_global_procedure (false);
  4551. push_state (&s, COMP_FUNCTION, gfc_new_block);
  4552. accept_statement (st);
  4553. parse_progunit (ST_NONE);
  4554. goto prog_units;
  4555. break;
  4556. case ST_BLOCK_DATA:
  4557. push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
  4558. accept_statement (st);
  4559. parse_block_data ();
  4560. break;
  4561. case ST_MODULE:
  4562. push_state (&s, COMP_MODULE, gfc_new_block);
  4563. accept_statement (st);
  4564. gfc_get_errors (NULL, &errors_before);
  4565. parse_module ();
  4566. break;
  4567. /* Anything else starts a nameless main program block. */
  4568. default:
  4569. if (seen_program)
  4570. goto duplicate_main;
  4571. seen_program = 1;
  4572. prog_locus = gfc_current_locus;
  4573. push_state (&s, COMP_PROGRAM, gfc_new_block);
  4574. main_program_symbol (gfc_current_ns, "MAIN__");
  4575. parse_progunit (st);
  4576. goto prog_units;
  4577. break;
  4578. }
  4579. /* Handle the non-program units. */
  4580. gfc_current_ns->code = s.head;
  4581. gfc_resolve (gfc_current_ns);
  4582. /* Dump the parse tree if requested. */
  4583. if (flag_dump_fortran_original)
  4584. gfc_dump_parse_tree (gfc_current_ns, stdout);
  4585. gfc_get_errors (NULL, &errors);
  4586. if (s.state == COMP_MODULE)
  4587. {
  4588. gfc_dump_module (s.sym->name, errors_before == errors);
  4589. gfc_current_ns->derived_types = gfc_derived_types;
  4590. gfc_derived_types = NULL;
  4591. goto prog_units;
  4592. }
  4593. else
  4594. {
  4595. if (errors == 0)
  4596. gfc_generate_code (gfc_current_ns);
  4597. pop_state ();
  4598. gfc_done_2 ();
  4599. }
  4600. goto loop;
  4601. prog_units:
  4602. /* The main program and non-contained procedures are put
  4603. in the global namespace list, so that they can be processed
  4604. later and all their interfaces resolved. */
  4605. gfc_current_ns->code = s.head;
  4606. if (next)
  4607. {
  4608. for (; next->sibling; next = next->sibling)
  4609. ;
  4610. next->sibling = gfc_current_ns;
  4611. }
  4612. else
  4613. gfc_global_ns_list = gfc_current_ns;
  4614. next = gfc_current_ns;
  4615. pop_state ();
  4616. goto loop;
  4617. done:
  4618. /* Do the resolution. */
  4619. resolve_all_program_units (gfc_global_ns_list);
  4620. /* Do the parse tree dump. */
  4621. gfc_current_ns
  4622. = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
  4623. for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
  4624. if (!gfc_current_ns->proc_name
  4625. || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
  4626. {
  4627. gfc_dump_parse_tree (gfc_current_ns, stdout);
  4628. fputs ("------------------------------------------\n\n", stdout);
  4629. }
  4630. /* Do the translation. */
  4631. translate_all_program_units (gfc_global_ns_list);
  4632. gfc_end_source_files ();
  4633. return true;
  4634. duplicate_main:
  4635. /* If we see a duplicate main program, shut down. If the second
  4636. instance is an implied main program, i.e. data decls or executable
  4637. statements, we're in for lots of errors. */
  4638. gfc_error_1 ("Two main PROGRAMs at %L and %C", &prog_locus);
  4639. reject_statement ();
  4640. gfc_done_2 ();
  4641. return true;
  4642. }
  4643. /* Return true if this state data represents an OpenACC region. */
  4644. bool
  4645. is_oacc (gfc_state_data *sd)
  4646. {
  4647. switch (sd->construct->op)
  4648. {
  4649. case EXEC_OACC_PARALLEL_LOOP:
  4650. case EXEC_OACC_PARALLEL:
  4651. case EXEC_OACC_KERNELS_LOOP:
  4652. case EXEC_OACC_KERNELS:
  4653. case EXEC_OACC_DATA:
  4654. case EXEC_OACC_HOST_DATA:
  4655. case EXEC_OACC_LOOP:
  4656. case EXEC_OACC_UPDATE:
  4657. case EXEC_OACC_WAIT:
  4658. case EXEC_OACC_CACHE:
  4659. case EXEC_OACC_ENTER_DATA:
  4660. case EXEC_OACC_EXIT_DATA:
  4661. return true;
  4662. default:
  4663. return false;
  4664. }
  4665. }